ezEngine  Release 25.03
ezMath Namespace Reference

This namespace provides common math-functionality as functions. More...

Functions

template<typename TYPE >
constexpr TYPE Pi ()
 Returns the natural constant Pi.
 
template<typename TYPE >
constexpr TYPE e ()
 Returns the natural constant e.
 
template<typename TYPE >
constexpr bool SupportsNaN ()
 Returns whether the template type supports specialized values to represent NaN.
 
template<typename TYPE >
constexpr TYPE NaN ()
 Returns the value for NaN as the template type. Returns zero, if the type does not support NaN. More...
 
template<typename TYPE >
constexpr bool SupportsInfinity ()
 Returns whether the template type supports specialized values to represent Infinity.
 
template<typename TYPE >
constexpr TYPE Infinity ()
 Returns the value for Infinity as the template type. Returns zero, if the type does not support Infinity.
 
template<typename TYPE >
constexpr TYPE MaxValue ()
 Returns the largest possible positive value (that is not infinity).
 
template<typename TYPE >
constexpr TYPE MinValue ()
 Returns the smallest possible value (that is not -infinity). Usually zero or -MaxValue(). For signed integers this will be -MaxValue() - 1.
 
template<typename TYPE >
constexpr TYPE HighValue ()
 A very large value, that is slightly smaller than sqrt(MaxValue()). More...
 
template<typename TYPE >
constexpr TYPE FloatEpsilon ()
 The difference between 1.0 and the next representable value for the given type.
 
template<typename TYPE >
constexpr TYPE SmallEpsilon ()
 
template<typename TYPE >
constexpr TYPE DefaultEpsilon ()
 
template<typename TYPE >
constexpr TYPE LargeEpsilon ()
 
template<typename TYPE >
constexpr TYPE HugeEpsilon ()
 
template<typename TYPE >
constexpr ezUInt32 NumBits ()
 Returns the number of bits in the given type. Mostly useful for unsigned integer types.
 
double GetCurveValue_Linear (double t)
 
double GetCurveValue_ConstantZero (double t)
 
double GetCurveValue_ConstantOne (double t)
 
double GetCurveValue_EaseInSine (double t)
 
double GetCurveValue_EaseOutSine (double t)
 
double GetCurveValue_EaseInOutSine (double t)
 
double GetCurveValue_EaseInQuad (double t)
 
double GetCurveValue_EaseOutQuad (double t)
 
double GetCurveValue_EaseInOutQuad (double t)
 
double GetCurveValue_EaseInCubic (double t)
 
double GetCurveValue_EaseOutCubic (double t)
 
double GetCurveValue_EaseInOutCubic (double t)
 
double GetCurveValue_EaseInQuartic (double t)
 
double GetCurveValue_EaseOutQuartic (double t)
 
double GetCurveValue_EaseInOutQuartic (double t)
 
double GetCurveValue_EaseInQuintic (double t)
 
double GetCurveValue_EaseOutQuintic (double t)
 
double GetCurveValue_EaseInOutQuintic (double t)
 
double GetCurveValue_EaseInExpo (double t)
 
double GetCurveValue_EaseOutExpo (double t)
 
double GetCurveValue_EaseInOutExpo (double t)
 
double GetCurveValue_EaseInCirc (double t)
 
double GetCurveValue_EaseOutCirc (double t)
 
double GetCurveValue_EaseInOutCirc (double t)
 
double GetCurveValue_EaseInBack (double t)
 
double GetCurveValue_EaseOutBack (double t)
 
double GetCurveValue_EaseInOutBack (double t)
 
double GetCurveValue_EaseInElastic (double t)
 
double GetCurveValue_EaseOutElastic (double t)
 
double GetCurveValue_EaseInOutElastic (double t)
 
double GetCurveValue_EaseInBounce (double t)
 
double GetCurveValue_EaseOutBounce (double t)
 
double GetCurveValue_EaseInOutBounce (double t)
 
double GetCurveValue_Conical (double t)
 
double GetCurveValue_FadeInHoldFadeOut (double t)
 
double GetCurveValue_FadeInFadeOut (double t)
 
double GetCurveValue_Bell (double t)
 
template<>
constexpr float Pi ()
 Returns the natural constant Pi.
 
template<>
constexpr float e ()
 Returns the natural constant e.
 
template<>
constexpr bool SupportsNaN< float > ()
 
template<>
constexpr bool SupportsNaN< double > ()
 
template<>
constexpr float NaN ()
 
template<>
constexpr bool SupportsInfinity< float > ()
 
template<>
constexpr bool SupportsInfinity< double > ()
 
template<>
constexpr float Infinity ()
 
template<>
constexpr ezUInt8 MaxValue ()
 Returns the largest possible positive value (that is not infinity).
 
template<>
constexpr ezUInt8 MinValue ()
 Returns the smallest possible value (that is not -infinity). Usually zero or -MaxValue(). For signed integers this will be -MaxValue() - 1.
 
template<>
constexpr float HighValue ()
 A very large value, that is slightly smaller than sqrt(MaxValue()). More...
 
template<>
constexpr float FloatEpsilon ()
 The difference between 1.0 and the next representable value for the given type.
 
template<>
constexpr ezUInt32 NumBits< ezUInt8 > ()
 
template<>
constexpr ezUInt32 NumBits< ezUInt16 > ()
 
template<>
constexpr ezUInt32 NumBits< ezUInt32 > ()
 
template<>
constexpr ezUInt32 NumBits< ezUInt64 > ()
 
template<>
constexpr ezUInt32 NumBits< ezInt8 > ()
 
template<>
constexpr ezUInt32 NumBits< ezInt16 > ()
 
template<>
constexpr ezUInt32 NumBits< ezInt32 > ()
 
template<>
constexpr ezUInt32 NumBits< ezInt64 > ()
 
template<>
constexpr ezUInt32 NumBits< float > ()
 
template<>
constexpr ezUInt32 NumBits< double > ()
 
template<typename T >
constexpr EZ_ALWAYS_INLINE T Square (T f)
 Returns f * f.
 
template<typename T >
constexpr EZ_ALWAYS_INLINE T Sign (T f)
 Returns the sign of f (i.e: -1, 1 or 0)
 
template<typename T >
constexpr EZ_ALWAYS_INLINE T Abs (T f)
 Returns the absolute value of f.
 
template<typename T >
constexpr EZ_ALWAYS_INLINE T Min (T f1, T f2)
 Returns the smaller value, f1 or f2.
 
template<typename T , typename... ARGS>
constexpr EZ_ALWAYS_INLINE T Min (T f1, T f2, ARGS... f)
 Returns the smaller value, f1 or f2 or ...
 
template<typename T >
constexpr EZ_ALWAYS_INLINE T Max (T f1, T f2)
 Returns the greater value, f1 or f2.
 
template<typename T , typename... ARGS>
constexpr EZ_ALWAYS_INLINE T Max (T f1, T f2, ARGS... f)
 
template<typename T >
constexpr EZ_ALWAYS_INLINE T Clamp (T value, T min_val, T max_val)
 Clamps "value" to the range [min; max]. Returns "value", if it is inside the range already.
 
template<typename T >
constexpr EZ_ALWAYS_INLINE T Saturate (T value)
 Clamps "value" to the range [0; 1]. Returns "value", if it is inside the range already.
 
template<typename Type >
constexpr Type Invert (Type f)
 Returns 1 / f.
 
EZ_ALWAYS_INLINE ezUInt32 FirstBitLow (ezUInt32 uiBitmask)
 Returns the index of the least significant bit set. More...
 
EZ_ALWAYS_INLINE ezUInt32 FirstBitLow (ezUInt64 uiBitmask)
 Returns the index of the least significant bit set. More...
 
EZ_ALWAYS_INLINE ezUInt32 FirstBitHigh (ezUInt32 uiBitmask)
 Returns the index of the most significant bit set. More...
 
EZ_ALWAYS_INLINE ezUInt32 FirstBitHigh (ezUInt64 uiBitmask)
 Returns the index of the most significant bit set. More...
 
EZ_ALWAYS_INLINE ezUInt32 CountTrailingZeros (ezUInt32 uiBitmask)
 
EZ_ALWAYS_INLINE ezUInt32 CountTrailingZeros (ezUInt64 uiBitmask)
 64 bit overload for CountTrailingZeros()
 
EZ_ALWAYS_INLINE ezUInt32 CountLeadingZeros (ezUInt32 uiBitmask)
 
EZ_ALWAYS_INLINE ezUInt32 CountBits (ezUInt32 value)
 Returns the number of bits set.
 
EZ_ALWAYS_INLINE ezUInt32 CountBits (ezUInt64 value)
 Returns the number of bits set.
 
template<typename Type >
constexpr EZ_ALWAYS_INLINE Type Bitmask_LowN (ezUInt32 uiNumBitsToSet)
 Creates a bitmask in which the low N bits are set. For example for N=5, this would be '0000 ... 0001 1111'. More...
 
template<typename Type >
constexpr EZ_ALWAYS_INLINE Type Bitmask_HighN (ezUInt32 uiNumBitsToSet)
 Creates a bitmask in which the high N bits are set. For example for N=5, this would be '1111 1000 ... 0000'. More...
 
template<typename T >
EZ_ALWAYS_INLINE void Swap (T &ref_f1, T &ref_f2)
 Swaps the values in the two variables f1 and f2.
 
template<typename T >
EZ_FORCE_INLINE T Lerp (T f1, T f2, float fFactor)
 Returns the linear interpolation of f1 and f2. factor is a value between 0 and 1.
 
template<typename T >
EZ_FORCE_INLINE T Lerp (T f1, T f2, double fFactor)
 Returns the linear interpolation of f1 and f2. factor is a value between 0 and 1.
 
template<typename T >
constexpr EZ_FORCE_INLINE float Unlerp (T fMin, T fMax, T fValue)
 Returns the interpolation factor such that Lerp(fMin, fMax, factor) == fValue.
 
template<typename T >
constexpr EZ_FORCE_INLINE T Step (T value, T edge)
 Returns 0, if value < edge, and 1, if value >= edge.
 
constexpr EZ_FORCE_INLINE bool IsPowerOf2 (ezInt32 value)
 Returns true, if there exists some x with 2^x == value.
 
constexpr EZ_FORCE_INLINE bool IsPowerOf2 (ezUInt32 value)
 Returns true, if there exists some x with 2^x == value.
 
constexpr EZ_FORCE_INLINE bool IsPowerOf2 (ezUInt64 value)
 Returns true, if there exists some x with 2^x == value.
 
template<typename Type >
constexpr bool IsEqual (Type lhs, Type rhs, Type fEpsilon)
 Checks, whether fValue is in the range [fDesired - fMaxImprecision; fDesired + fMaxImprecision].
 
template<typename T >
constexpr bool IsInRange (T value, T minVal, T maxVal)
 Checks whether the value of the first parameter lies between the value of the second and third.
 
template<typename Type >
bool IsZero (Type f, Type fEpsilon)
 Checks whether the given number is close to zero.
 
template<typename Type >
EZ_ALWAYS_INLINE Type Trunc (Type f)
 Returns the integer-part of f (removes the fraction).
 
template<typename Type >
EZ_ALWAYS_INLINE Type Fraction (Type f)
 Returns the fraction-part of f.
 
template<typename Type >
Type SmoothStep (Type value, Type edge1, Type edge2)
 Returns 0, if value is <= edge1, 1 if value >= edge2 and the hermite interpolation in between.
 
template<typename Type >
Type SmootherStep (Type value, Type edge1, Type edge2)
 Returns 0, if value is <= edge1, 1 if value >= edge2 and the second order hermite interpolation in between.
 
template<ezUInt32 NumBits>
ezUInt32 ColorFloatToUnsignedInt (float value)
 Converts a color value from float [0;1] range to unsigned int with the given number of bits, with proper rounding.
 
EZ_ALWAYS_INLINE ezUInt8 ColorFloatToByte (float value)
 Converts a color value from float [0;1] range to unsigned byte [0;255] range, with proper rounding.
 
EZ_ALWAYS_INLINE ezUInt16 ColorFloatToShort (float value)
 Converts a color value from float [0;1] range to unsigned short [0;65535] range, with proper rounding.
 
ezInt8 ColorFloatToSignedByte (float value)
 Converts a color value from float [-1;1] range to signed byte [-127;127] range, with proper rounding.
 
ezInt16 ColorFloatToSignedShort (float value)
 Converts a color value from float [-1;1] range to signed short [-32767;32767] range, with proper rounding.
 
constexpr float ColorByteToFloat (ezUInt8 value)
 Converts a color value from unsigned byte [0;255] range to float [0;1] range, with proper rounding.
 
constexpr float ColorShortToFloat (ezUInt16 value)
 Converts a color value from unsigned short [0;65535] range to float [0;1] range, with proper rounding.
 
constexpr float ColorSignedByteToFloat (ezInt8 value)
 Converts a color value from signed byte [-128;127] range to float [-1;1] range, with proper rounding.
 
constexpr float ColorSignedShortToFloat (ezInt16 value)
 Converts a color value from signed short [-32768;32767] range to float [0;1] range, with proper rounding.
 
template<typename T , typename T2 >
EvaluateBezierCurve (T2 t, const T &startPoint, const T &controlPoint1, const T &controlPoint2, const T &endPoint)
 Evaluates the cubic spline defined by four control points at time t and returns the interpolated result. Can be used with T as float, vec2, vec3 or vec4.
 
EZ_ALWAYS_INLINE bool IsFinite (double value)
 
EZ_ALWAYS_INLINE bool IsNaN (double value)
 
EZ_ALWAYS_INLINE double Floor (double f)
 
EZ_ALWAYS_INLINE double Ceil (double f)
 
EZ_ALWAYS_INLINE double Round (double f)
 Rounds f to the next integer. More...
 
double RoundDown (double f, double fMultiple)
 Returns a multiple of fMultiple that is smaller than f.
 
double RoundUp (double f, double fMultiple)
 Returns a multiple of fMultiple that is larger than f.
 
EZ_ALWAYS_INLINE double RoundToMultiple (double f, double fMultiple)
 Rounds f to the closest value of multiple.
 
EZ_ALWAYS_INLINE double Exp (double f)
 
EZ_ALWAYS_INLINE double Ln (double f)
 
EZ_ALWAYS_INLINE double Log2 (double f)
 
EZ_ALWAYS_INLINE double Log10 (double f)
 
EZ_ALWAYS_INLINE double Log (double fBase, double f)
 
EZ_ALWAYS_INLINE double Pow2 (double f)
 
EZ_ALWAYS_INLINE double Pow (double fBase, double fExp)
 
EZ_ALWAYS_INLINE double Root (double f, double fNthRoot)
 
EZ_ALWAYS_INLINE double Sqrt (double f)
 Returns the square root of f.
 
EZ_ALWAYS_INLINE double Mod (double f, double fDiv)
 Returns "value mod div" for doubles. This also works with negative numbers, both for value and for div.
 
EZ_ALWAYS_INLINE bool IsFinite (float value)
 
EZ_ALWAYS_INLINE bool IsNaN (float value)
 
EZ_ALWAYS_INLINE float Floor (float f)
 Returns the next smaller integer, closest to f. Also the SMALLER value, if f is negative.
 
EZ_ALWAYS_INLINE ezInt32 FloorToInt (float f)
 Returns the next smaller integer, closest to f. Also the SMALLER value, if f is negative. More...
 
EZ_ALWAYS_INLINE float Ceil (float f)
 Returns the next higher integer, closest to f. Also the HIGHER value, if f is negative.
 
EZ_ALWAYS_INLINE ezInt32 CeilToInt (float f)
 Returns the next higher integer, closest to f. Also the HIGHER value, if f is negative. More...
 
EZ_ALWAYS_INLINE float Round (float f)
 Rounds f to the next integer. More...
 
EZ_ALWAYS_INLINE ezInt32 RoundToInt (float f)
 Rounds f to the next integer. More...
 
EZ_ALWAYS_INLINE float RoundToMultiple (float f, float fMultiple)
 Rounds f to the closest value of multiple.
 
float RoundDown (float f, float fMultiple)
 Returns a multiple of fMultiple that is smaller than f.
 
float RoundUp (float f, float fMultiple)
 Returns a multiple of fMultiple that is larger than f.
 
EZ_ALWAYS_INLINE float Sin (ezAngle a)
 ***** Trigonometric Functions ***** More...
 
EZ_ALWAYS_INLINE float Cos (ezAngle a)
 Takes an angle, returns its cosine.
 
EZ_ALWAYS_INLINE float Tan (ezAngle a)
 Takes an angle, returns its tangent.
 
EZ_ALWAYS_INLINE ezAngle ASin (float f)
 Returns the arcus sinus of f.
 
EZ_ALWAYS_INLINE ezAngle ACos (float f)
 Returns the arcus cosinus of f.
 
EZ_ALWAYS_INLINE ezAngle ATan (float f)
 Returns the arcus tangent of f.
 
EZ_ALWAYS_INLINE ezAngle ATan2 (float y, float x)
 Returns the atan2 of x and y.
 
EZ_ALWAYS_INLINE float Exp (float f)
 Returns e^f.
 
EZ_ALWAYS_INLINE float Ln (float f)
 Returns the logarithmus naturalis of f.
 
EZ_ALWAYS_INLINE float Log2 (float f)
 Returns log (f), to the base 2.
 
EZ_ALWAYS_INLINE float Log10 (float f)
 Returns log (f), to the base 10.
 
EZ_ALWAYS_INLINE float Log (float fBase, float f)
 Returns log (f), to the base fBase.
 
EZ_ALWAYS_INLINE float Pow2 (float f)
 Returns 2^f.
 
EZ_ALWAYS_INLINE float Pow (float fBase, float fExp)
 Returns base^exp.
 
EZ_ALWAYS_INLINE float Root (float f, float fNthRoot)
 Returns the n-th root of f.
 
EZ_ALWAYS_INLINE float Sqrt (float f)
 Returns the square root of f.
 
EZ_ALWAYS_INLINE float Mod (float value, float fDiv)
 Returns "value mod div" for floats. This also works with negative numbers, both for value and for div.
 
constexpr EZ_ALWAYS_INLINE ezInt32 RoundUp (ezInt32 value, ezUInt16 uiMultiple)
 Returns a multiple of the given multiple that is larger than or equal to value.
 
constexpr EZ_ALWAYS_INLINE ezInt32 RoundDown (ezInt32 value, ezUInt16 uiMultiple)
 Returns a multiple of the given multiple that is smaller than or equal to value.
 
constexpr EZ_ALWAYS_INLINE ezUInt32 RoundUp (ezUInt32 value, ezUInt16 uiMultiple)
 Returns a multiple of the given multiple that is greater than or equal to value.
 
constexpr EZ_ALWAYS_INLINE ezUInt32 RoundDown (ezUInt32 value, ezUInt16 uiMultiple)
 Returns a multiple of the given multiple that is smaller than or equal to value.
 
constexpr EZ_ALWAYS_INLINE bool IsOdd (ezInt32 i)
 Returns true, if i is an odd number.
 
constexpr EZ_ALWAYS_INLINE bool IsEven (ezInt32 i)
 Returns true, if i is an even number.
 
EZ_ALWAYS_INLINE ezUInt32 Log2i (ezUInt32 uiVal)
 Returns the integral logarithm to the base 2, that comes closest to the given integer.
 
constexpr EZ_ALWAYS_INLINE int Pow2 (int i)
 
int Pow (int iBase, int iExp)
 
constexpr ezInt32 Pow2 (ezInt32 i)
 Returns 2^f.
 
ezInt32 Pow (ezInt32 iBase, ezInt32 iExp)
 Returns base^exp.
 
template<typename T >
constexpr T Square (T f)
 Returns f * f.
 
template<typename T >
constexpr T Sign (T f)
 Returns the sign of f (i.e: -1, 1 or 0)
 
template<typename T >
constexpr T Abs (T f)
 Returns the absolute value of f.
 
template<typename T >
constexpr T Min (T f1, T f2)
 Returns the smaller value, f1 or f2.
 
template<typename T , typename... ARGS>
constexpr T Min (T f1, T f2, ARGS... f)
 Returns the smaller value, f1 or f2 or ...
 
template<typename T >
constexpr T Max (T f1, T f2)
 Returns the greater value, f1 or f2.
 
template<typename T >
constexpr T Clamp (T value, T min_val, T max_val)
 Clamps "value" to the range [min; max]. Returns "value", if it is inside the range already.
 
constexpr ezUInt32 WrapUInt (ezUInt32 uiValue, ezUInt32 uiExcludedMaxValue)
 Wraps uiValue around the maximum value, so that it stays within the range [0; uiExcludedMaxValue-1]. More...
 
constexpr ezInt32 WrapInt (ezInt32 iValue, ezUInt32 uiExcludedMaxValue)
 Wraps iValue around the maximum value, so that it stays within the range [0; uiExcludedMaxValue-1]. More...
 
constexpr ezInt32 WrapInt (ezInt32 iValue, ezInt32 iMinValue, ezInt32 iExcludedMaxValue)
 Wraps iValue around the minimum and maximum value, so that it stays within the range [iMinValue; iExcludedMaxValue-1]. More...
 
float WrapFloat01 (float fValue)
 Wraps a float value around to stay within the [0; 1] range. More...
 
float WrapFloat (float fValue, float fMinValue, float fMaxValue)
 Wraps a float value around to stay within the [min; max] range. More...
 
template<typename T >
constexpr T Saturate (T value)
 Clamps "value" to the range [0; 1]. Returns "value", if it is inside the range already.
 
template<typename Type >
Type Trunc (Type f)
 Returns the integer-part of f (removes the fraction).
 
constexpr ezInt32 FloatToInt (float value)
 Casts the float to an integer, removes the fractional part. More...
 
constexpr ezInt64 FloatToInt (double value)
 Casts the float to an integer, removes the fractional part. More...
 
template<typename Type >
Type Fraction (Type f)
 Returns the fraction-part of f.
 
template<typename Type >
constexpr Type Bitmask_LowN (ezUInt32 uiNumBitsToSet)
 Creates a bitmask in which the low N bits are set. For example for N=5, this would be '0000 ... 0001 1111'. More...
 
template<typename Type >
constexpr Type Bitmask_HighN (ezUInt32 uiNumBitsToSet)
 Creates a bitmask in which the high N bits are set. For example for N=5, this would be '1111 1000 ... 0000'. More...
 
template<typename T >
void Swap (T &ref_f1, T &ref_f2)
 Swaps the values in the two variables f1 and f2.
 
template<typename T >
Lerp (T f1, T f2, float fFactor)
 Returns the linear interpolation of f1 and f2. factor is a value between 0 and 1.
 
template<typename T >
Lerp (T f1, T f2, double fFactor)
 Returns the linear interpolation of f1 and f2. factor is a value between 0 and 1.
 
template<typename T >
constexpr float Unlerp (T fMin, T fMax, T fValue)
 Returns the interpolation factor such that Lerp(fMin, fMax, factor) == fValue.
 
template<typename T >
constexpr T Step (T value, T edge)
 Returns 0, if value < edge, and 1, if value >= edge.
 
EZ_FOUNDATION_DLL bool IsPowerOf (ezInt32 value, ezInt32 iBase)
 Returns true, if there exists some x with base^x == value.
 
EZ_FOUNDATION_DLL ezUInt32 PowerOfTwo_Floor (ezUInt32 value)
 Returns the next power-of-two that is <= value.
 
EZ_FOUNDATION_DLL ezUInt64 PowerOfTwo_Floor (ezUInt64 value)
 Returns the next power-of-two that is <= value.
 
EZ_FOUNDATION_DLL ezUInt32 PowerOfTwo_Ceil (ezUInt32 value)
 Returns the next power-of-two that is >= value.
 
EZ_FOUNDATION_DLL ezUInt64 PowerOfTwo_Ceil (ezUInt64 value)
 Returns the next power-of-two that is >= value.
 
EZ_FOUNDATION_DLL ezUInt32 GreatestCommonDivisor (ezUInt32 a, ezUInt32 b)
 Returns the greatest common divisor.
 
EZ_FOUNDATION_DLL ezResult TryMultiply32 (ezUInt32 &out_uiResult, ezUInt32 a, ezUInt32 b, ezUInt32 c=1, ezUInt32 d=1)
 out_Result = a * b. If an overflow happens, EZ_FAILURE is returned.
 
EZ_FOUNDATION_DLL ezUInt32 SafeMultiply32 (ezUInt32 a, ezUInt32 b, ezUInt32 c=1, ezUInt32 d=1)
 returns a * b. If an overflow happens, the program is terminated.
 
EZ_FOUNDATION_DLL ezResult TryMultiply64 (ezUInt64 &out_uiResult, ezUInt64 a, ezUInt64 b, ezUInt64 c=1, ezUInt64 d=1)
 out_Result = a * b. If an overflow happens, EZ_FAILURE is returned.
 
EZ_FOUNDATION_DLL ezUInt64 SafeMultiply64 (ezUInt64 a, ezUInt64 b, ezUInt64 c=1, ezUInt64 d=1)
 returns a * b. If an overflow happens, the program is terminated.
 
ezResult TryConvertToSizeT (size_t &out_uiResult, ezUInt64 uiValue)
 Checks whether the given 64bit value actually fits into size_t, If it doesn't EZ_FAILURE is returned.
 
EZ_FOUNDATION_DLL size_t SafeConvertToSizeT (ezUInt64 uiValue)
 Checks whether the given 64bit value actually fits into size_t, If it doesn't the program is terminated.
 
EZ_FOUNDATION_DLL float ReplaceNaN (float fValue, float fFallback)
 If 'value' is not-a-number (NaN) 'fallback' is returned, otherwise 'value' is passed through unmodified.
 
EZ_FOUNDATION_DLL double ReplaceNaN (double fValue, double fFallback)
 If 'value' is not-a-number (NaN) 'fallback' is returned, otherwise 'value' is passed through unmodified.
 
constexpr ezUInt64 MakeUInt64 (ezUInt32 uiHigh32, ezUInt32 uiLow32)
 Combines the two 32 bit uint values into one 64 bit value.
 
ezVariant Lerp (const ezVariant &a, const ezVariant &b, double fFactor)
 An overload of ezMath::Lerp to interpolate variants. A and b must have the same type. More...
 

Detailed Description

This namespace provides common math-functionality as functions.

It is a namespace, instead of a static class, because that allows it to be extended at other locations, which is especially useful when adding custom types.

Function Documentation

◆ Bitmask_HighN() [1/2]

template<typename Type >
constexpr EZ_ALWAYS_INLINE Type ezMath::Bitmask_HighN ( ezUInt32  uiNumBitsToSet)
constexpr

Creates a bitmask in which the high N bits are set. For example for N=5, this would be '1111 1000 ... 0000'.

For N >= 32 all bits will be set.

◆ Bitmask_HighN() [2/2]

template<typename Type >
constexpr Type ezMath::Bitmask_HighN ( ezUInt32  uiNumBitsToSet)
constexpr

Creates a bitmask in which the high N bits are set. For example for N=5, this would be '1111 1000 ... 0000'.

For N >= 32 all bits will be set.

◆ Bitmask_LowN() [1/2]

template<typename Type >
constexpr EZ_ALWAYS_INLINE Type ezMath::Bitmask_LowN ( ezUInt32  uiNumBitsToSet)
constexpr

Creates a bitmask in which the low N bits are set. For example for N=5, this would be '0000 ... 0001 1111'.

For N >= 32 all bits will be set.

◆ Bitmask_LowN() [2/2]

template<typename Type >
constexpr Type ezMath::Bitmask_LowN ( ezUInt32  uiNumBitsToSet)
constexpr

Creates a bitmask in which the low N bits are set. For example for N=5, this would be '0000 ... 0001 1111'.

For N >= 32 all bits will be set.

◆ CeilToInt()

ezInt32 ezMath::CeilToInt ( float  f)

Returns the next higher integer, closest to f. Also the HIGHER value, if f is negative.

This function is identical to 'Ceil()' except that it already returns the result cast to an int.

◆ CountLeadingZeros()

ezUInt32 ezMath::CountLeadingZeros ( ezUInt32  uiBitmask)

Returns the number of zeros at the start (most significant part) of a bitmask.

E.g. 0b0111 -> 29 0b0011 -> 30 0b0001 -> 31 0b0000 -> 32 Returns 32 when the input is 0

◆ CountTrailingZeros()

ezUInt32 ezMath::CountTrailingZeros ( ezUInt32  uiBitmask)

Returns the number of zeros at the end (least significant part) of a bitmask.

E.g. 0b0111 -> 0 0b0110 -> 1 0b0100 -> 2 Returns 32 when the input is 0

◆ FirstBitHigh() [1/2]

ezUInt32 ezMath::FirstBitHigh ( ezUInt32  uiBitmask)

Returns the index of the most significant bit set.

Asserts that bitmask is not 0.

◆ FirstBitHigh() [2/2]

ezUInt32 ezMath::FirstBitHigh ( ezUInt64  uiBitmask)

Returns the index of the most significant bit set.

Asserts that bitmask is not 0.

◆ FirstBitLow() [1/2]

ezUInt32 ezMath::FirstBitLow ( ezUInt32  uiBitmask)

Returns the index of the least significant bit set.

Asserts that bitmask is not 0.

◆ FirstBitLow() [2/2]

ezUInt32 ezMath::FirstBitLow ( ezUInt64  uiBitmask)

Returns the index of the least significant bit set.

Asserts that bitmask is not 0.

◆ FloatToInt() [1/2]

constexpr EZ_FORCE_INLINE ezInt64 ezMath::FloatToInt ( double  value)
constexpr

Casts the float to an integer, removes the fractional part.

See also
Trunc, Round, Floor, Ceil

◆ FloatToInt() [2/2]

constexpr EZ_FORCE_INLINE ezInt32 ezMath::FloatToInt ( float  value)
constexpr

Casts the float to an integer, removes the fractional part.

See also
Trunc, Round, Floor, Ceil

◆ FloorToInt()

ezInt32 ezMath::FloorToInt ( float  f)

Returns the next smaller integer, closest to f. Also the SMALLER value, if f is negative.

This function is identical to 'Floor()' except that it already returns the result cast to an int.

◆ HighValue() [1/2]

template<typename TYPE >
constexpr TYPE ezMath::HighValue ( )
constexpr

A very large value, that is slightly smaller than sqrt(MaxValue()).

Useful to default initialize values, that may get squared in subsequent operations.

◆ HighValue() [2/2]

template<>
constexpr double ezMath::HighValue ( )
constexpr

A very large value, that is slightly smaller than sqrt(MaxValue()).

Useful to default initialize values, that may get squared in subsequent operations.

◆ Lerp()

EZ_FOUNDATION_DLL ezVariant ezMath::Lerp ( const ezVariant a,
const ezVariant b,
double  fFactor 
)

An overload of ezMath::Lerp to interpolate variants. A and b must have the same type.

If the type can't be interpolated like e.g. strings, a is returned for a fFactor less than 0.5, b is returned for a fFactor greater or equal to 0.5.

◆ NaN()

template<typename TYPE >
constexpr TYPE ezMath::NaN ( )
constexpr

Returns the value for NaN as the template type. Returns zero, if the type does not support NaN.

Do not use this for comparisons, it will fail. Use it to initialize data (e.g. in debug builds), to detect uninitialized variables. Use the function IsNaN() to check whether a value is not a number.

◆ Round() [1/2]

double ezMath::Round ( double  f)

Rounds f to the next integer.

If f is positive 0.5 is rounded UP (i.e. to 1), if f is negative, -0.5 is rounded DOWN (i.e. to -1).

◆ Round() [2/2]

float ezMath::Round ( float  f)

Rounds f to the next integer.

If f is positive 0.5 is rounded UP (i.e. to 1), if f is negative, -0.5 is rounded DOWN (i.e. to -1).

◆ RoundToInt()

ezInt32 ezMath::RoundToInt ( float  f)

Rounds f to the next integer.

If f is positive 0.5 is rounded UP (i.e. to 1), if f is negative, -0.5 is rounded DOWN (i.e. to -1).

This function is identical to 'Round()' except that it already returns the result cast to an int.

◆ Sin()

float ezMath::Sin ( ezAngle  a)

***** Trigonometric Functions *****

Takes an angle, returns its sine

◆ WrapFloat()

EZ_ALWAYS_INLINE float ezMath::WrapFloat ( float  fValue,
float  fMinValue,
float  fMaxValue 
)

Wraps a float value around to stay within the [min; max] range.

Both fMinValue and fMaxValue are inclusive. Both values may be negative, but fMinValue has to be strictly smaller than fMaxValue.

◆ WrapFloat01()

EZ_ALWAYS_INLINE float ezMath::WrapFloat01 ( float  fValue)

Wraps a float value around to stay within the [0; 1] range.

Wrapping happens in both positive and negative direction. Ie -0.1f will be wrapped to 0.9f and 1.1f will be wrapped to 0.1f. Note that here the value 1.0f is included in the range. Only values larger than 1.0f get wrapped back to zero. Therefore it is different to what 'Fraction' would return.

◆ WrapInt() [1/2]

constexpr EZ_ALWAYS_INLINE ezInt32 ezMath::WrapInt ( ezInt32  iValue,
ezInt32  iMinValue,
ezInt32  iExcludedMaxValue 
)
constexpr

Wraps iValue around the minimum and maximum value, so that it stays within the range [iMinValue; iExcludedMaxValue-1].

Ie a value of iExcludedMaxValue would be wrapped to iMinValue, and (iExcludedMaxValue+1) to (iMinValue+1), etc. Values below iMinValue are wrapped back around to a large value, ie (iMinValue-1) would be wrapped to (iExcludedMaxValue-1).

Both iMinValue and iExcludedMaxValue can be negative, but iMinValue has to be strictly smaller than iExcludedMaxValue.

◆ WrapInt() [2/2]

constexpr EZ_ALWAYS_INLINE ezInt32 ezMath::WrapInt ( ezInt32  iValue,
ezUInt32  uiExcludedMaxValue 
)
constexpr

Wraps iValue around the maximum value, so that it stays within the range [0; uiExcludedMaxValue-1].

Ie a value of uiExcludedMaxValue would be wrapped to 0, and (uiExcludedMaxValue+1) to 1, etc. Negative values are wrapped back around to a large value, ie -1 would be wrapped to (uiExcludedMaxValue-1). A value of 0 for uiExcludedMaxValue is invalid and results in a division by zero error.

◆ WrapUInt()

constexpr EZ_ALWAYS_INLINE ezUInt32 ezMath::WrapUInt ( ezUInt32  uiValue,
ezUInt32  uiExcludedMaxValue 
)
constexpr

Wraps uiValue around the maximum value, so that it stays within the range [0; uiExcludedMaxValue-1].

Ie a value of uiExcludedMaxValue would be wrapped to 0, and (uiExcludedMaxValue+1) to 1, etc. A value of 0 for uiExcludedMaxValue is invalid and results in a division by zero error.