numbers
Table of Contents
Functions
- clamp() : int|float
- Bounds a number value between two numbers.
- clip() : int|float
- Bounds a number value between two numbers.
- isEven() : bool
- Tells whether an integer is even.
- isOdd() : bool
- Tells whether an integer is odd.
- lerp() : float
- Linearly interpolates between two values.
- mapRange() : float
- Re-maps a value from one range to another.
- modf() : array{0: float, 1: float}
- Splits a number into its integral and fractional parts, like the C `modf()` function.
- percentage() : float
- Computes which percentage a part represents of a total.
- sign() : int
- Returns the sign of a number.
Functions
clamp()
Bounds a number value between two numbers.
clamp(int|float $value, int|float $min, int|float $max) : int|float
This is a clarified alias of clip() ; both names are kept for convenience.
Parameters
- $value : int|float
-
The value to clamp.
- $min : int|float
-
The minimum value of the range.
- $max : int|float
-
The maximum value of the range.
Tags
Return values
int|float —A value bounded between $min and $max.
clip()
Bounds a number value between two numbers.
clip(int|float $value, int|float $min, int|float $max) : int|float
Parameters
- $value : int|float
-
The value to clamp.
- $min : int|float
-
The minimum value of the range.
- $max : int|float
-
The maximum value of the range.
Tags
Return values
int|float —A value bounded between $min and $max.
isEven()
Tells whether an integer is even.
isEven(int $value) : bool
Parameters
- $value : int
-
The integer to test.
Tags
Return values
bool —true if $value is even, false otherwise.
isOdd()
Tells whether an integer is odd.
isOdd(int $value) : bool
Parameters
- $value : int
-
The integer to test.
Tags
Return values
bool —true if $value is odd, false otherwise.
lerp()
Linearly interpolates between two values.
lerp(float $a, float $b, float $t) : float
Computes $a + ( $b - $a ) * $t. The factor $t is not bounded: values outside
the [0, 1] range extrapolate beyond the [$a, $b] segment.
Parameters
- $a : float
-
The start value (returned when
$t === 0.0). - $b : float
-
The end value (returned when
$t === 1.0). - $t : float
-
The interpolation factor, typically in
[0, 1].
Tags
Return values
float —The interpolated value.
mapRange()
Re-maps a value from one range to another.
mapRange(float $value, float $inMin, float $inMax, float $outMin, float $outMax) : float
Linearly transforms $value from the input range [$inMin, $inMax] to the
output range [$outMin, $outMax] using
$outMin + ( $value - $inMin ) * ( $outMax - $outMin ) / ( $inMax - $inMin ).
The result is not clamped: values outside the input range map outside the output range.
Parameters
- $value : float
-
The value to re-map.
- $inMin : float
-
The lower bound of the input range.
- $inMax : float
-
The upper bound of the input range.
- $outMin : float
-
The lower bound of the output range.
- $outMax : float
-
The upper bound of the output range.
Tags
Return values
float —The value re-mapped into the output range.
modf()
Splits a number into its integral and fractional parts, like the C `modf()` function.
modf(float $number) : array{0: float, 1: float}
The number is truncated toward zero, so the sign is preserved on both parts :
modf( -1.5 ) returns [ -1.0 , -0.5 ] (and not [ -2.0 , 0.5 ] as a floor()
based split would). Both parts are always returned as float.
Special values follow the C behavior :
modf( INF )returns[ INF , 0.0 ](idem with-INF).modf( NAN )returns[ NAN , NAN ].
Note: unlike C's and Python's modf, the integral part comes first
(natural reading order : 1.5 is 1 + 0.5), the fractional part second.
Parameters
- $number : float
-
The number to split.
Tags
Return values
array{0: float, 1: float} —A two-element array : [ integral part , fractional part ].
percentage()
Computes which percentage a part represents of a total.
percentage(int|float $part, int|float $total) : float
Returns ( $part / $total ) * 100. As a guard against division by zero, a
$total of 0 yields 0.0 rather than raising an error.
Parameters
- $part : int|float
-
The partial amount.
- $total : int|float
-
The total amount.
Tags
Return values
float —The percentage in the [0, 100] range for in-bounds inputs, 0.0 when $total is 0.
sign()
Returns the sign of a number.
sign(int|float $value) : int
Parameters
- $value : int|float
-
The value to evaluate.
Tags
Return values
int —-1 if the value is negative, 1 if it is positive, 0 if it is zero.