Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
FindRoot.h
Go to the documentation of this file.
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
8
12template <typename T>
13inline 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
#define JPH_NAMESPACE_END
Definition: Core.h:378
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
JPH_NAMESPACE_BEGIN int FindRoot(const T inA, const T inB, const T inC, T &outX1, T &outX2)
Definition: FindRoot.h:13
JPH_INLINE constexpr T Sign(T inV)
Get the sign of a value.
Definition: Math.h:66
JPH_INLINE constexpr T Square(T inV)
Square a value.
Definition: Math.h:52