Maths
Reference for the Maths module: small static helpers for integer and real arithmetic and comparison, and immutable data objects for 2, 3 and 4 component vectors and quaternions, each with array conversions.
This page is a pure reference. For how the framework fits together, see the Guide.
MathsInteger- integer arithmetic, comparison and conversion helpers.MathsReal- floating point arithmetic, comparison and conversion helpers.MathsQuaternionData- immutable quaternion with x, y, z and w components.MathsVector2Data- immutable 2 component vector with x and y.MathsVector3Data- immutable 3 component vector with x, y and z.MathsVector4Data- immutable 4 component vector with x, y, z and w.
MathsInteger
Static helpers for integer arithmetic, comparison, conversion to and from strings, clamping and conditional evaluation.
Add
MathsInteger::Add(int $a, int $b): int
Add two integers.
| Name | Type | Description |
|---|---|---|
$a | int | The first integer. |
$b | int | The second integer. |
Returns int The sum.
Clamp
MathsInteger::Clamp(int $a, int $min, int $max): int
Constrain an integer to lie between a minimum and maximum.
| Name | Type | Description |
|---|---|---|
$a | int | The value to constrain. |
$min | int | The lower bound. |
$max | int | The upper bound. |
Returns int The clamped value.
Decrement
MathsInteger::Decrement(int $a): int
Return the integer reduced by one.
| Name | Type | Description |
|---|---|---|
$a | int | The value to decrement. |
Returns int The value minus one.
Divide
MathsInteger::Divide(int $a, int $b): int
Divide one integer by another, rounding the result down.
| Name | Type | Description |
|---|---|---|
$a | int | The dividend. |
$b | int | The divisor. |
Returns int The quotient, rounded down.
Equal
MathsInteger::Equal(int $a, int $b): bool
Whether two integers are equal.
| Name | Type | Description |
|---|---|---|
$a | int | The first integer. |
$b | int | The second integer. |
Returns bool True when the values are equal.
FromString
MathsInteger::FromString(string $a): int
Parse an integer from a string.
| Name | Type | Description |
|---|---|---|
$a | string | The string to parse. |
Returns int The parsed integer.
GreaterThan
MathsInteger::GreaterThan(int $a, int $b): bool
Whether the first integer is greater than the second.
| Name | Type | Description |
|---|---|---|
$a | int | The first integer. |
$b | int | The second integer. |
Returns bool True when the first is greater.
GreaterThanOrEqual
MathsInteger::GreaterThanOrEqual(int $a, int $b): bool
Whether the first integer is greater than or equal to the second.
| Name | Type | Description |
|---|---|---|
$a | int | The first integer. |
$b | int | The second integer. |
Returns bool True when the first is greater or equal.
IfGreater
MathsInteger::IfGreater(int $a, int $b, Callable $onGreater): mixed
Call the callback and return its result when the first integer is greater than the second, otherwise return null.
| Name | Type | Description |
|---|---|---|
$a | int | The first integer. |
$b | int | The second integer. |
$onGreater | Callable | Called when the first integer is greater. |
Returns mixed The callback result, or null.
Increment
MathsInteger::Increment(int $a): int
Return the integer increased by one.
| Name | Type | Description |
|---|---|---|
$a | int | The value to increment. |
Returns int The value plus one.
LessThan
MathsInteger::LessThan(int $a, int $b): bool
Whether the first integer is less than the second.
| Name | Type | Description |
|---|---|---|
$a | int | The first integer. |
$b | int | The second integer. |
Returns bool True when the first is less.
Modulo
MathsInteger::Modulo(int $a, int $b): int
The remainder after dividing the first integer by the second.
| Name | Type | Description |
|---|---|---|
$a | int | The dividend. |
$b | int | The divisor. |
Returns int The remainder.
Multiply
MathsInteger::Multiply(int $a, int $b): int
Multiply two integers, rounding the result down.
| Name | Type | Description |
|---|---|---|
$a | int | The first integer. |
$b | int | The second integer. |
Returns int The product, rounded down.
Real
MathsInteger::Real(int $a): float
Convert an integer to a floating point value.
| Name | Type | Description |
|---|---|---|
$a | int | The integer to convert. |
Returns float The value as a float.
String
MathsInteger::String(int $a): string
Convert an integer to its string form.
| Name | Type | Description |
|---|---|---|
$a | int | The integer to convert. |
Returns string The value as a string.
Subtract
MathsInteger::Subtract(int $a, int $b): int
Subtract the second integer from the first.
| Name | Type | Description |
|---|---|---|
$a | int | The value to subtract from. |
$b | int | The value to subtract. |
Returns int The difference.
MathsReal
Static helpers for floating point arithmetic, rounding, square root, minimum and maximum, clamping, comparison and conversion to a string.
Add
MathsReal::Add(float $number1, float $number2): float
Add two floating point numbers.
| Name | Type | Description |
|---|---|---|
$number1 | float | The first number. |
$number2 | float | The second number. |
Returns float The sum.
Clamp
MathsReal::Clamp(float $number, float $min, float $max): float
Constrain a floating point number to lie between a minimum and maximum.
| Name | Type | Description |
|---|---|---|
$number | float | The value to constrain. |
$min | float | The lower bound. |
$max | float | The upper bound. |
Returns float The clamped value.
Divide
MathsReal::Divide(float $number1, float $number2): float
Divide one floating point number by another.
| Name | Type | Description |
|---|---|---|
$number1 | float | The dividend. |
$number2 | float | The divisor. |
Returns float The quotient.
GreaterOrEqual
MathsReal::GreaterOrEqual(float $a, float $b): bool
Whether the first number is greater than or equal to the second.
| Name | Type | Description |
|---|---|---|
$a | float | The first number. |
$b | float | The second number. |
Returns bool True when the first is greater or equal.
Maximum
MathsReal::Maximum(float $number1, float $number2): float
The larger of two floating point numbers.
| Name | Type | Description |
|---|---|---|
$number1 | float | The first number. |
$number2 | float | The second number. |
Returns float The larger value.
Minimum
MathsReal::Minimum(float $number1, float $number2): float
The smaller of two floating point numbers.
| Name | Type | Description |
|---|---|---|
$number1 | float | The first number. |
$number2 | float | The second number. |
Returns float The smaller value.
Multiply
MathsReal::Multiply(float $number1, float $number2): float
Multiply two floating point numbers.
| Name | Type | Description |
|---|---|---|
$number1 | float | The first number. |
$number2 | float | The second number. |
Returns float The product.
RoundDown
MathsReal::RoundDown(float $number): float
Round a floating point number down to the nearest whole number.
| Name | Type | Description |
|---|---|---|
$number | float | The value to round down. |
Returns float The floored value.
SquareRoot
MathsReal::SquareRoot(float $number): float
The square root of a floating point number.
| Name | Type | Description |
|---|---|---|
$number | float | The value to take the root of. |
Returns float The square root.
String
MathsReal::String(float $a): string
Convert a floating point number to its string form.
| Name | Type | Description |
|---|---|---|
$a | float | The number to convert. |
Returns string The value as a string.
Subtract
MathsReal::Subtract(float $number1, float $number2): float
Subtract the second floating point number from the first.
| Name | Type | Description |
|---|---|---|
$number1 | float | The value to subtract from. |
$number2 | float | The value to subtract. |
Returns float The difference.
MathsQuaternionData
An immutable quaternion with x, y, z and w components, matching the StorageObject record MathsQuaternion. Fields, a constructor and array conversions.
Properties
| Property | Type | Description |
|---|---|---|
$q->x | float | The x component (readonly). |
$q->y | float | The y component (readonly). |
$q->z | float | The z component (readonly). |
$q->w | float | The w component (readonly). |
__construct
new MathsQuaternionData(float $x = 0.0, float $y = 0.0, float $z = 0.0, float $w = 1.0)
Create a quaternion from its four components. The w component defaults to 1.0.
| Name | Type | Description |
|---|---|---|
$x | float | The x component. |
$y | float | The y component. |
$z | float | The z component. |
$w | float | The w component. |
Returns MathsQuaternionData
FromArray
MathsQuaternionData::FromArray(array $m): self
Build a quaternion from a map with x, y, z and w keys.
| Name | Type | Description |
|---|---|---|
$m | array | A map with x, y, z and w keys. |
Returns self The quaternion.
ToArray
MathsQuaternionData::ToArray(self $o): array
Convert a quaternion to a map with x, y, z and w keys.
| Name | Type | Description |
|---|---|---|
$o | self | The quaternion to convert. |
Returns array A map with x, y, z and w keys.
MathsVector2Data
An immutable 2 component vector with x and y, matching the StorageObject record MathsVector2. Fields, a constructor and array conversions.
Properties
| Property | Type | Description |
|---|---|---|
$v->x | float | The x component (readonly). |
$v->y | float | The y component (readonly). |
__construct
new MathsVector2Data(float $x = 0.0, float $y = 0.0)
Create a 2 component vector from its x and y components.
| Name | Type | Description |
|---|---|---|
$x | float | The x component. |
$y | float | The y component. |
Returns MathsVector2Data
FromArray
MathsVector2Data::FromArray(array $m): self
Build a 2 component vector from a map with x and y keys.
| Name | Type | Description |
|---|---|---|
$m | array | A map with x and y keys. |
Returns self The vector.
ToArray
MathsVector2Data::ToArray(self $o): array
Convert a 2 component vector to a map with x and y keys.
| Name | Type | Description |
|---|---|---|
$o | self | The vector to convert. |
Returns array A map with x and y keys.
MathsVector3Data
An immutable 3 component vector with x, y and z, matching the StorageObject record MathsVector3. Fields, a constructor and array conversions.
Properties
| Property | Type | Description |
|---|---|---|
$v->x | float | The x component (readonly). |
$v->y | float | The y component (readonly). |
$v->z | float | The z component (readonly). |
__construct
new MathsVector3Data(float $x = 0.0, float $y = 0.0, float $z = 0.0)
Create a 3 component vector from its x, y and z components.
| Name | Type | Description |
|---|---|---|
$x | float | The x component. |
$y | float | The y component. |
$z | float | The z component. |
Returns MathsVector3Data
FromArray
MathsVector3Data::FromArray(array $m): self
Build a 3 component vector from a map with x, y and z keys.
| Name | Type | Description |
|---|---|---|
$m | array | A map with x, y and z keys. |
Returns self The vector.
ToArray
MathsVector3Data::ToArray(self $o): array
Convert a 3 component vector to a map with x, y and z keys.
| Name | Type | Description |
|---|---|---|
$o | self | The vector to convert. |
Returns array A map with x, y and z keys.
MathsVector4Data
An immutable 4 component vector with x, y, z and w, matching the StorageObject record MathsVector4. Fields, a constructor and array conversions.
Properties
| Property | Type | Description |
|---|---|---|
$v->x | float | The x component (readonly). |
$v->y | float | The y component (readonly). |
$v->z | float | The z component (readonly). |
$v->w | float | The w component (readonly). |
__construct
new MathsVector4Data(float $x = 0.0, float $y = 0.0, float $z = 0.0, float $w = 0.0)
Create a 4 component vector from its x, y, z and w components.
| Name | Type | Description |
|---|---|---|
$x | float | The x component. |
$y | float | The y component. |
$z | float | The z component. |
$w | float | The w component. |
Returns MathsVector4Data
FromArray
MathsVector4Data::FromArray(array $m): self
Build a 4 component vector from a map with x, y, z and w keys.
| Name | Type | Description |
|---|---|---|
$m | array | A map with x, y, z and w keys. |
Returns self The vector.
ToArray
MathsVector4Data::ToArray(self $o): array
Convert a 4 component vector to a map with x, y, z and w keys.
| Name | Type | Description |
|---|---|---|
$o | self | The vector to convert. |
Returns array A map with x, y, z and w keys.