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.

Classes

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.

NameTypeDescription
$aintThe first integer.
$bintThe 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.

NameTypeDescription
$aintThe value to constrain.
$minintThe lower bound.
$maxintThe upper bound.

Returns int The clamped value.

Decrement

MathsInteger::Decrement(int $a): int

Return the integer reduced by one.

NameTypeDescription
$aintThe 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.

NameTypeDescription
$aintThe dividend.
$bintThe divisor.

Returns int The quotient, rounded down.

Equal

MathsInteger::Equal(int $a, int $b): bool

Whether two integers are equal.

NameTypeDescription
$aintThe first integer.
$bintThe second integer.

Returns bool True when the values are equal.

FromString

MathsInteger::FromString(string $a): int

Parse an integer from a string.

NameTypeDescription
$astringThe 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.

NameTypeDescription
$aintThe first integer.
$bintThe 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.

NameTypeDescription
$aintThe first integer.
$bintThe 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.

NameTypeDescription
$aintThe first integer.
$bintThe second integer.
$onGreaterCallableCalled 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.

NameTypeDescription
$aintThe 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.

NameTypeDescription
$aintThe first integer.
$bintThe 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.

NameTypeDescription
$aintThe dividend.
$bintThe divisor.

Returns int The remainder.

Multiply

MathsInteger::Multiply(int $a, int $b): int

Multiply two integers, rounding the result down.

NameTypeDescription
$aintThe first integer.
$bintThe second integer.

Returns int The product, rounded down.

Real

MathsInteger::Real(int $a): float

Convert an integer to a floating point value.

NameTypeDescription
$aintThe integer to convert.

Returns float The value as a float.

String

MathsInteger::String(int $a): string

Convert an integer to its string form.

NameTypeDescription
$aintThe 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.

NameTypeDescription
$aintThe value to subtract from.
$bintThe 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.

NameTypeDescription
$number1floatThe first number.
$number2floatThe 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.

NameTypeDescription
$numberfloatThe value to constrain.
$minfloatThe lower bound.
$maxfloatThe upper bound.

Returns float The clamped value.

Divide

MathsReal::Divide(float $number1, float $number2): float

Divide one floating point number by another.

NameTypeDescription
$number1floatThe dividend.
$number2floatThe 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.

NameTypeDescription
$afloatThe first number.
$bfloatThe 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.

NameTypeDescription
$number1floatThe first number.
$number2floatThe second number.

Returns float The larger value.

Minimum

MathsReal::Minimum(float $number1, float $number2): float

The smaller of two floating point numbers.

NameTypeDescription
$number1floatThe first number.
$number2floatThe second number.

Returns float The smaller value.

Multiply

MathsReal::Multiply(float $number1, float $number2): float

Multiply two floating point numbers.

NameTypeDescription
$number1floatThe first number.
$number2floatThe second number.

Returns float The product.

RoundDown

MathsReal::RoundDown(float $number): float

Round a floating point number down to the nearest whole number.

NameTypeDescription
$numberfloatThe value to round down.

Returns float The floored value.

SquareRoot

MathsReal::SquareRoot(float $number): float

The square root of a floating point number.

NameTypeDescription
$numberfloatThe 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.

NameTypeDescription
$afloatThe 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.

NameTypeDescription
$number1floatThe value to subtract from.
$number2floatThe 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

PropertyTypeDescription
$q->xfloatThe x component (readonly).
$q->yfloatThe y component (readonly).
$q->zfloatThe z component (readonly).
$q->wfloatThe 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.

NameTypeDescription
$xfloatThe x component.
$yfloatThe y component.
$zfloatThe z component.
$wfloatThe w component.

Returns MathsQuaternionData

FromArray

MathsQuaternionData::FromArray(array $m): self

Build a quaternion from a map with x, y, z and w keys.

NameTypeDescription
$marrayA 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.

NameTypeDescription
$oselfThe 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

PropertyTypeDescription
$v->xfloatThe x component (readonly).
$v->yfloatThe 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.

NameTypeDescription
$xfloatThe x component.
$yfloatThe y component.

Returns MathsVector2Data

FromArray

MathsVector2Data::FromArray(array $m): self

Build a 2 component vector from a map with x and y keys.

NameTypeDescription
$marrayA 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.

NameTypeDescription
$oselfThe 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

PropertyTypeDescription
$v->xfloatThe x component (readonly).
$v->yfloatThe y component (readonly).
$v->zfloatThe 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.

NameTypeDescription
$xfloatThe x component.
$yfloatThe y component.
$zfloatThe z component.

Returns MathsVector3Data

FromArray

MathsVector3Data::FromArray(array $m): self

Build a 3 component vector from a map with x, y and z keys.

NameTypeDescription
$marrayA 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.

NameTypeDescription
$oselfThe 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

PropertyTypeDescription
$v->xfloatThe x component (readonly).
$v->yfloatThe y component (readonly).
$v->zfloatThe z component (readonly).
$v->wfloatThe 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.

NameTypeDescription
$xfloatThe x component.
$yfloatThe y component.
$zfloatThe z component.
$wfloatThe 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.

NameTypeDescription
$marrayA 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.

NameTypeDescription
$oselfThe vector to convert.

Returns array A map with x, y, z and w keys.