Open 3D Engine EMotionFX Gem API Reference 23.10.0
O3DE is an open-source, fully-featured, high-fidelity, modular 3D engine for building games and simulations, available to every industry.
MCore::Math Class Reference

#include <FastMath.h>

Public Member Functions

template<typename T >
MCORE_INLINE T Align (T inValue, T alignment)
 

Static Public Member Functions

static MCORE_INLINE float Floor (float x)
 
static MCORE_INLINE bool IsPositive (float x)
 
static MCORE_INLINE bool IsNegative (float x)
 
static MCORE_INLINE bool IsFloatZero (float x)
 
static MCORE_INLINE bool IsFloatEqual (float x, float y)
 
static MCORE_INLINE float SignOfFloat (float x)
 
static MCORE_INLINE float Abs (float x)
 
static MCORE_INLINE float Ceil (float x)
 
static MCORE_INLINE float RadiansToDegrees (float rad)
 
static MCORE_INLINE float DegreesToRadians (float deg)
 
static MCORE_INLINE float Sin (float x)
 
static MCORE_INLINE float Cos (float x)
 
static MCORE_INLINE float Tan (float x)
 
static MCORE_INLINE float ASin (float x)
 
static MCORE_INLINE float ACos (float x)
 
static MCORE_INLINE float ATan (float x)
 
static MCORE_INLINE float ATan2 (float y, float x)
 
static MCORE_INLINE float SignOfCos (float y)
 
static MCORE_INLINE float SignOfSin (float y)
 
static MCORE_INLINE float Exp (float x)
 
static MCORE_INLINE float Log (float x)
 
static MCORE_INLINE float Log2 (float x)
 
static MCORE_INLINE float Pow (float base, float exponent)
 
static MCORE_INLINE float Sqrt (float x)
 
static MCORE_INLINE float SafeSqrt (float x)
 
static MCORE_INLINE float FastSqrt (float x)
 
static MCORE_INLINE float FastSqrt2 (float x)
 
static MCORE_INLINE float InvSqrt (float x)
 
static MCORE_INLINE float FastInvSqrt (float x)
 
static MCORE_INLINE uint32 NextPowerOfTwo (uint32 x)
 
static MCORE_INLINE float FMod (float x, float y)
 
static MCORE_INLINE float SafeFMod (float x, float y)
 
template<typename T >
static MCORE_INLINE T Align (T inValue, T alignment)
 
static MCORE_INLINE void MulSign (float &dest, float const &source)
 

Static Public Attributes

static const float pi
 
static const float twoPi
 
static const float halfPi
 
static const float invPi
 
static const float epsilon
 
static const float sqrtHalf
 
static const float sqrt2
 
static const float sqrt3
 
static const float halfSqrt2
 
static const float halfSqrt3
 

Detailed Description

The math class. All methods and attributes are static.

Member Function Documentation

◆ Abs()

MCORE_INLINE float Math::Abs ( float  x)
static

Calculates the absolute value. The Abs function returns the absolute value of its parameter. Examples: The absolute value of -4 is 4 The absolute value of -41567 is 41567 The absolute value of -3.141593 is 3.141593

Parameters
xThe value to calculate the absolute value from.
Returns
The absolute value of the parameter.

◆ ACos()

MCORE_INLINE float Math::ACos ( float  x)
static

Arccosine function.

Parameters
xAngle in radians.
Returns
The arccosine of x.

◆ Align()

template<typename T >
static MCORE_INLINE T MCore::Math::Align ( inValue,
alignment 
)
static

Align a given size_t value to a given alignment. For example when the input value of inOutValue contains a value of 50, and the alignment is set to 16, then the aligned return value would be 64.

Parameters
inValueThe input value, which would be 50 in our above example.
alignmentThe alignment touse, which would be 16 in our above example.
Returns
The value returned is the input value aligned to the given alignment. In our example it would return a value of 64.

◆ ASin()

MCORE_INLINE float Math::ASin ( float  x)
static

Arcsine function.

Parameters
xAngle in radians.
Returns
The arcsine of x.

◆ ATan()

MCORE_INLINE float Math::ATan ( float  x)
static

Arctangent function.

Parameters
xAngle in radians.
Returns
The arctangent of x.

◆ ATan2()

MCORE_INLINE float Math::ATan2 ( float  y,
float  x 
)
static

Calculate the arctangent of y/x.

Parameters
yThe value of y.
xThe value of x.
Returns
The arctangent of y/x.

◆ Ceil()

MCORE_INLINE float Math::Ceil ( float  x)
static

Calculates the ceiling of a value. The ceil function returns a value representing the smallest integer that is greater than or equal to x. Examples: The ceil of 2.8 is 3.000000 The ceil of -2.8 is -2.000000

Parameters
xThe value to ceil.
Returns
The value representing the smallest integer that is greater than or equal to x.

◆ Cos()

MCORE_INLINE float Math::Cos ( float  x)
static

Cosine function.

Parameters
xAngle in radians.
Returns
The cosine of x.

◆ DegreesToRadians()

MCORE_INLINE float Math::DegreesToRadians ( float  deg)
static

Degree to radians Conversion.

Parameters
degThe angle (degrees) to convert.
Returns
The converted angle in radians.

◆ Exp()

MCORE_INLINE float Math::Exp ( float  x)
static

Calculates the exponential.

Parameters
xThe value.
Returns
The exponential of x.

◆ FastInvSqrt()

MCORE_INLINE float Math::FastInvSqrt ( float  x)
static

Calculates a fast approximation (very good approx) to 1/sqrt(x). This one is more accurate than FastInvSqrt2, but a bit slower, but still much faster than the standard 1.0f / sqrt(x).

Parameters
xThe value in the equation 1/sqrt(x).
Returns
The inverse square root of x.

◆ FastSqrt()

MCORE_INLINE float Math::FastSqrt ( float  x)
static

Fast square root. On the PC and MSVC compiler an optimized SSE version is used. On other platforms or other compilers it uses the standard square root. The precision is 22 bits here.

Returns
The square root of x.

◆ FastSqrt2()

MCORE_INLINE float Math::FastSqrt2 ( float  x)
static

Fast square root method 2. On the PC and MSVC compiler an optimized SSE version is used. On other platforms or other compilers it uses the standard square root. The precision is 11 bits here.

Returns
The square root of x.

◆ Floor()

MCORE_INLINE float Math::Floor ( float  x)
static

Calculates the floored version of a value. The floor function returns a value representing the largest integer that is less than or equal to x. Examples: The floor of 2.8 is 2.000000 The floor of -2.8 is -3.000000

Parameters
xThe value to floor.
Returns
The value representing the largest integer that is less than or equal to x.

◆ FMod()

MCORE_INLINE float Math::FMod ( float  x,
float  y 
)
static

Perform the modulus calculation on a float. Please keep in mind that the value of y is not allowed to be zero. If you don't know if y will be zero, use SafeFMod instead.

Parameters
xThe value to take the modulus on.
yThe modulus value, which is NOT allowed to be zero.
Returns
The result of the calculation.

◆ InvSqrt()

MCORE_INLINE float Math::InvSqrt ( float  x)
static

Calculates the inverse square root (1 / Sqrt(x)). Parameter has to be nonnegative.

Parameters
xNonnegative value.
Returns
The inverse square root.

◆ IsFloatEqual()

MCORE_INLINE bool Math::IsFloatEqual ( float  x,
float  y 
)
static

Tests if two numbers are equal to each other. This uses the epsilon value as tolerance. Basically it does "return (Abs(x-y) < epsilon)".

Parameters
xThe first value.
yThe second value.
Returns
True if x is equal to y, otherwise false is returned.

◆ IsFloatZero()

MCORE_INLINE bool Math::IsFloatZero ( float  x)
static

Tests if a number is equal to zero. This uses the epsilon value as tolerance. Basically it does "return (Abs(x) < epsilon)".

Parameters
xThe tested value.
Returns
True if x is zero, otherwise false is returned.

◆ IsNegative()

MCORE_INLINE bool Math::IsNegative ( float  x)
static

Tests if a number is negative.

Parameters
xThe tested value.
Returns
True if the number is negative, false if it is positive or zero.

◆ IsPositive()

MCORE_INLINE bool Math::IsPositive ( float  x)
static

Tests if a number is positive.

Parameters
xThe tested value.
Returns
True if the number is positive, false if it is negative or zero.

◆ Log()

MCORE_INLINE float Math::Log ( float  x)
static

Calculates logarithms. The log functions return the logarithm of x. If x is negative, these functions return an indefinite, by default. If x is 0, they return INF (infinite).

Parameters
xThe value.
Returns
The logarithm of x.

◆ Log2()

MCORE_INLINE float Math::Log2 ( float  x)
static

Calculates logarithms. The log2 functions return the logarithm of x. If x is negative, these functions return an indefinite, by default. If x is 0, they return INF (infinite).

Parameters
xThe value.
Returns
The logarithm of x.

◆ MulSign()

MCORE_INLINE void Math::MulSign ( float &  dest,
float const &  source 
)
static

Multiply a float value by its sign. This basically does:

dest = source > 0.0f ? source : -source;
or
dest = source * sgn(source);

Using bit operations the required if statement is eliminated.

◆ NextPowerOfTwo()

MCORE_INLINE uint32 Math::NextPowerOfTwo ( uint32  x)
static

Calculates a fast (but less accurate) approximation to 1/sqrt(x). The precision starts to show changes after the second digit in most cases.

Parameters
xThe value in the equation 1/sqrt(x).
Returns
The inverse square root of x. Calculates a fast (but less accurate) approximation to 1/sqrt(x). This version is more accurate (but a little slower) than the FastInvSqrt2.
Parameters
xThe value in the equation 1/sqrt(x).
Returns
The inverse square root of x. Calculates the next power of two.
Parameters
xThe value.
Returns
The next power of two of the parameter.

◆ Pow()

MCORE_INLINE float Math::Pow ( float  base,
float  exponent 
)
static

Calculates x raised to the power of y.

Parameters
baseThe base.
exponentThe exponent.
Returns
X raised to the power of y.

◆ RadiansToDegrees()

MCORE_INLINE float Math::RadiansToDegrees ( float  rad)
static

Radians to degree Conversion.

Parameters
radThe angle(rads) to convert.
Returns
The converted angle in degrees.

◆ SafeFMod()

MCORE_INLINE float Math::SafeFMod ( float  x,
float  y 
)
static

Perform the modulus calculation on a float. The value of y is allowed to be zero. If you know that the value of y will never be zero, please use FMod instead, as that is a bit faster.

Parameters
xThe value to take the modulus on.
yThe modulus value, which is allowed to be zero as well.
Returns
The result of the calculation.

◆ SafeSqrt()

MCORE_INLINE float Math::SafeSqrt ( float  x)
static

Calculates the square root. Parameter has to be nonnegative. The difference between Sqrt and SafeSqrt is that SafeSqrt checks if the parameter value is bigger than epsilon or not, otherwise it returns zero.

Parameters
xNonnegative value.
Returns
The square root of x.

◆ SignOfCos()

MCORE_INLINE float Math::SignOfCos ( float  y)
static

Returns the sign of the cosine of the argument.

◆ SignOfFloat()

MCORE_INLINE float Math::SignOfFloat ( float  x)
static

Returns the sign of a real number.

Parameters
xThe tested number.
Returns
1 for positive, -1 for negative, 0 for zero.

◆ SignOfSin()

MCORE_INLINE float Math::SignOfSin ( float  y)
static

Returns the sign of the sine of the argument.

◆ Sin()

MCORE_INLINE float Math::Sin ( float  x)
static

Sine function.

Parameters
xAngle in radians.
Returns
The sine of x.

◆ Sqrt()

MCORE_INLINE float Math::Sqrt ( float  x)
static

Calculates the square root. Parameter has to be nonnegative and non-zero.

Parameters
xNonnegative value.
Returns
The square root of x.

◆ Tan()

MCORE_INLINE float Math::Tan ( float  x)
static

Tangent function.

Parameters
xAngle in radians.
Returns
The tangent of x.

Member Data Documentation

◆ epsilon

const float MCore::Math::epsilon
static

A very small number, almost equal to zero (0.000001).

◆ halfPi

const float MCore::Math::halfPi
static

PI divided by two

◆ halfSqrt2

const float MCore::Math::halfSqrt2
static

Half the square root of two.

◆ halfSqrt3

const float MCore::Math::halfSqrt3
static

Half the square root of three.

◆ invPi

const float MCore::Math::invPi
static

1.0 divided by PI

◆ pi

const float MCore::Math::pi
static

Check if a given float is positive.

Parameters
fThe float to check.
Returns
Returns true when positive, otherwise false is returned. Check if a given float is negative.
Parameters
fThe float to check.
Returns
Returns true when negative, otherwise false is returned. The value of PI, which is 3.1415926

◆ sqrt2

const float MCore::Math::sqrt2
static

The square root of 2.

◆ sqrt3

const float MCore::Math::sqrt3
static

The square root of 3.

◆ sqrtHalf

const float MCore::Math::sqrtHalf
static

The square root of 0.5.

◆ twoPi

const float MCore::Math::twoPi
static

PI multiplied by two


The documentation for this class was generated from the following files: