Debian Codegraph @

thirdparty/jolt_physics/Jolt/Math/FindRoot.h

raw symbols SCIP
1 // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2 // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3 // SPDX-License-Identifier: MIT
4
5 #pragma once
6
7 JPH_NAMESPACE_BEGIN
8
9 /// Find the roots of \f$inA \: x^2 + inB \: x + inC = 0\f$.
10 /// @return The number of roots, actual roots in outX1 and outX2.
11 /// If number of roots returned is 1 then outX1 == outX2.
12 template <typename T>
13 inline int FindRoot(const T inA, const T inB, const T inC, T &outX1, T &outX2)
14 {
15 // Check if this is a linear equation
16 if (inA == T(0))
17 {
18 // Check if this is a constant equation
19 if (inB == T(0))
20 return 0;
21
22 // Linear equation with 1 solution
23 outX1 = outX2 = -inC / inB;
24 return 1;
25 }
26
27 // See Numerical Recipes in C, Chapter 5.6 Quadratic and Cubic Equations
28 T det = Square(inB) - T(4) * inA * inC;
29 if (det < T(0))
30 return 0;
31 T q = (inB + Sign(inB) * sqrt(det)) / T(-2);
32 outX1 = q / inA;
33 if (q == T(0))
34 {
35 outX2 = outX1;
36 return 1;
37 }
38 outX2 = inC / q;
39 return 2;
40 }
41
42 JPH_NAMESPACE_END