Path: blob/master/thirdparty/jolt_physics/Jolt/Math/FindRoot.h
9913 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56JPH_NAMESPACE_BEGIN78/// Find the roots of \f$inA \: x^2 + inB \: x + inC = 0\f$.9/// @return The number of roots, actual roots in outX1 and outX2.10/// If number of roots returned is 1 then outX1 == outX2.11template <typename T>12inline int FindRoot(const T inA, const T inB, const T inC, T &outX1, T &outX2)13{14// Check if this is a linear equation15if (inA == T(0))16{17// Check if this is a constant equation18if (inB == T(0))19return 0;2021// Linear equation with 1 solution22outX1 = outX2 = -inC / inB;23return 1;24}2526// See Numerical Recipes in C, Chapter 5.6 Quadratic and Cubic Equations27T det = Square(inB) - T(4) * inA * inC;28if (det < T(0))29return 0;30T q = (inB + Sign(inB) * sqrt(det)) / T(-2);31outX1 = q / inA;32if (q == T(0))33{34outX2 = outX1;35return 1;36}37outX2 = inC / q;38return 2;39}4041JPH_NAMESPACE_END424344