PMath Class Reference

Detailed Description

The PMath class is a toolbox to do some standard numeric operations (clamp, NaN handling, linear interpolation, random numbers...)

You cannot create instances of PMath.

It can be used to clamp numbers, compute linear interpolation, compare floating point values or test for NaN.

Definition at line 32 of file PMath.h.

#include <PMath.h>

Member Function Documentation

◆ AreAlmostEqual() [1/2]

static bool PMath::AreAlmostEqual ( float  v1,
float  v2,
float  absoluteError = 1e-5f 
)
inlinestatic

Returns true if and only if the two specified values are equal according to the specified absolute error.

This function can typically be used to compare two floating-point values in the range 0..1.

See also
AreAlmostEqual(float, float, papillon::int32)

Definition at line 140 of file PMath.h.

◆ AreAlmostEqual() [2/2]

static bool PMath::AreAlmostEqual ( double  v1,
double  v2,
double  absoluteError = 1e-7 
)
inlinestatic

Returns true if and only if the two specified values are equal according to the specified absolute error.

This function can typically be used to compare two floating-point values in the range 0..1.

Definition at line 148 of file PMath.h.

◆ AreAlmostEqualULP()

static bool PMath::AreAlmostEqualULP ( float  v1,
float  v2,
papillon::int32  maxULP = 4 
)
static

Returns true if and only if the two specified values are equal according to the specified relative error.

◆ Clamp() [1/3]

static int PMath::Clamp ( papillon::int32  value,
papillon::int32  min,
papillon::int32  max 
)
inlinestatic

Clamps the value to the specified range.

Definition at line 71 of file PMath.h.

◆ Clamp() [2/3]

static float PMath::Clamp ( float  value,
float  min,
float  max 
)
inlinestatic

Clamps the value to the specified range.

Definition at line 76 of file PMath.h.

◆ Clamp() [3/3]

static double PMath::Clamp ( double  value,
double  min,
double  max 
)
inlinestatic

Clamps the value to the specified range.

Definition at line 81 of file PMath.h.

◆ GCD()

static papillon::uint32 PMath::GCD ( papillon::uint32  a,
papillon::uint32  b 
)
inlinestatic

Returns Greatest Common Divisor between 2 unsigned integers.

Definition at line 159 of file PMath.h.

◆ IntMax()

static papillon::int32 PMath::IntMax ( )
static

Returns the maximum value for an int32.

◆ IntMin()

static papillon::int32 PMath::IntMin ( )
static

Returns the minimum value for an int32.

◆ IsNaN() [1/2]

static bool PMath::IsNaN ( float  v)
inlinestatic

Returns true if the specified number is NaN (Not A Number), false otherwise.

Definition at line 61 of file PMath.h.

◆ IsNaN() [2/2]

static bool PMath::IsNaN ( double  v)
inlinestatic

Returns true if the specified number is NaN (Not A Number), false otherwise.

Definition at line 66 of file PMath.h.

◆ IsNthBitSet()

static bool PMath::IsNthBitSet ( papillon::int32  value,
papillon::int32  n 
)
inlinestatic

Returns true if the nth bit of the specified value is set, false otherwise.

Definition at line 185 of file PMath.h.

◆ IsPow2()

static bool PMath::IsPow2 ( papillon::uint32  n)
inlinestatic

Returns true if the integer argument is a power of 2.

Definition at line 164 of file PMath.h.

◆ LERP() [1/2]

static float PMath::LERP ( float  value,
float  min,
float  max 
)
inlinestatic

Performs a linear interpolation (v = v * (max - min) + min).

Example: LERP(0, 2, 10) -> 2 Example: LERP(1, 2, 10) -> 10 Example: LERP(0.5, 2, 10) -> 6

Definition at line 94 of file PMath.h.

◆ LERP() [2/2]

static double PMath::LERP ( double  value,
double  min,
double  max 
)
inlinestatic

Performs a linear interpolation (v = v * (max - min) + min).

Example: LERP(0, 2, 10) -> 2 Example: LERP(1, 2, 10) -> 10 Example: LERP(0.5, 2, 10) -> 6

Definition at line 102 of file PMath.h.

◆ NaNd()

static double PMath::NaNd ( )
inlinestatic

Returns NaN (Not a Number) value for double numbers.

Definition at line 46 of file PMath.h.

◆ NaNf()

static float PMath::NaNf ( )
inlinestatic

Returns NaN (Not a Number) value for float numbers.

Definition at line 41 of file PMath.h.

◆ Pow2Down()

static papillon::uint32 PMath::Pow2Down ( papillon::uint32  n)
static

Returns the greatest power of two equal to or smaller than the argument.

Returns 0 if n is 0.

◆ Pow2Nearest()

static papillon::uint32 PMath::Pow2Nearest ( papillon::uint32  n)
static

Returns the nearest power of two for the argument.

◆ Pow2Up()

static papillon::uint32 PMath::Pow2Up ( papillon::uint32  n)
static

Returns the smallest power of two equal to or larger than the argument.

◆ RandBool()

static bool PMath::RandBool ( )
static

Returns a random boolean value.

This function can be used safely by multiple threads at the same time, i.e. several different threads will get different random values.

◆ RandUInt32()

static papillon::uint32 PMath::RandUInt32 ( )
static

Returns a 32-bit unsigned int random value.

This function can be used safely by multiple threads at the same time, i.e. several different threads will get different random values.

◆ RandUInt63()

static papillon::uint64 PMath::RandUInt63 ( )
static

Returns a 63-bit unsigned int random value (converted to Int64, it will always be a positive value).

This function can be used safely by multiple threads at the same time, i.e. several different threads will get different random values.

◆ RandUInt64()

static papillon::uint64 PMath::RandUInt64 ( )
static

Returns a 64-bit unsigned int random value.

This function can be used safely by multiple threads at the same time, i.e. several different threads will get different random values.

◆ SetNthBit()

static papillon::int32 PMath::SetNthBit ( papillon::int32  value,
papillon::int32  n 
)
inlinestatic

Sets the nth bit of the specified value.

Definition at line 190 of file PMath.h.

◆ Sigmoid()

static double PMath::Sigmoid ( double  x)
inlinestatic

Computes the sigmoid (or logistic) function of x: y=1/(1+e^(-x))

Definition at line 86 of file PMath.h.

◆ UnsetNthBit()

static papillon::int32 PMath::UnsetNthBit ( papillon::int32  value,
papillon::int32  n 
)
inlinestatic

Unsets the nth bit of the specified value.

Definition at line 195 of file PMath.h.