maths
Table of Contents
Classes
- Dimension
- Defines the canonical keys of a two-dimensional size pair.
Functions
- aspectFit() : array{width: int, height: int}
- Scales a dimension pair to a target width or height while preserving the original aspect ratio (the locked-ratio behaviour of an aspect-ratio box).
- bearing() : float
- Calculates the initial bearing (sometimes referred to as forward azimuth) which if followed in a straight line along a great-circle arc will take you from the start point to the end point (in degrees).
- cartesianToPolar() : array{angle: float, radius: float}
- Converts a cartesian vector to polar coordinates.
- ceilValue() : float
- Rounds and returns the ceiling of the specified number or expression.
- factorial() : int
- Computes the factorial `n!` of a non-negative integer.
- fixAngle() : float
- Normalize an angle in degrees to the range [0, 360).
- floorValue() : float
- Rounds and returns a number by a count of floating points, using floor.
- gcd() : int
- Calculate the Greatest Common Divisor (GCD) of two integers using the Euclidean algorithm.
- haversine() : float
- Calculate the great-circle distance between two points on a sphere using the Haversine formula.
- isPrime() : bool
- Tells whether an integer is a prime number.
- mean() : float
- Computes the arithmetic mean (average) of a list of numbers.
- median() : float
- Computes the median (middle value) of a list of numbers.
- polarToCartesian() : array{x: float, y: float}
- Converts a polar coordinate to a cartesian vector.
- roundValue() : float
- Rounds and returns the rounded value of the specified number or expression.
- stddev() : float
- Computes the standard deviation of a list of numbers.
- variance() : float
- Computes the variance of a list of numbers.
Functions
aspectFit()
Scales a dimension pair to a target width or height while preserving the original aspect ratio (the locked-ratio behaviour of an aspect-ratio box).
aspectFit(int $width, int $height[, int|null $targetWidth = null ][, int|null $targetHeight = null ]) : array{width: int, height: int}
Provide $targetWidth to derive the matching height, or $targetHeight to
derive the matching width. If both are given, $targetWidth takes precedence
and $targetHeight is ignored. If both are null, the original pair is
returned. A non-positive original $width/$height yields an undefined
ratio: the provided targets (or the originals) are returned unchanged.
Parameters
- $width : int
-
The original (reference) width.
- $height : int
-
The original (reference) height.
- $targetWidth : int|null = null
-
The desired width, or null.
- $targetHeight : int|null = null
-
The desired height, or null.
Tags
Return values
array{width: int, height: int} —The scaled dimensions preserving the ratio.
bearing()
Calculates the initial bearing (sometimes referred to as forward azimuth) which if followed in a straight line along a great-circle arc will take you from the start point to the end point (in degrees).
bearing(float $latitude1, float $longitude1, float $latitude2, float $longitude2) : float
Parameters
- $latitude1 : float
-
The first latitude coordinate in degrees.
- $longitude1 : float
-
The first longitude coordinate in degrees.
- $latitude2 : float
-
The second latitude coordinate in degrees.
- $longitude2 : float
-
The second longitude coordinate in degrees.
Tags
Return values
float —The bearing in degrees from North.
cartesianToPolar()
Converts a cartesian vector to polar coordinates.
cartesianToPolar(array{x?: float|int, y?: float|int} $vector[, bool $degrees = true ][, bool $throwable = false ]) : array{angle: float, radius: float}
Parameters
- $vector : array{x?: float|int, y?: float|int}
-
Cartesian coordinates with keys 'x' and 'y'.
- $degrees : bool = true
-
Whether the returned angle should be in degrees (default: true).
- $throwable : bool = false
-
Whether to throw an exception if keys are missing (default: false).
Tags
Return values
array{angle: float, radius: float} —Polar coordinates with keys 'angle' and 'radius'.
ceilValue()
Rounds and returns the ceiling of the specified number or expression.
ceilValue(int|float $value[, int $floatCount = 0 ]) : float
The ceiling of a number is the closest integer that is greater than or equal to the number.
Parameters
- $value : int|float
-
The number to round.
- $floatCount : int = 0
-
The number of decimal places to round up to.
Tags
Return values
float —The rounded number.
factorial()
Computes the factorial `n!` of a non-negative integer.
factorial(int $n) : int
factorial(0) is 1. The argument is capped at 20 because 21! exceeds
PHP_INT_MAX and could no longer be represented exactly as an int.
Parameters
- $n : int
-
A non-negative integer in the range
[0, 20].
Tags
Return values
int —The factorial of $n.
fixAngle()
Normalize an angle in degrees to the range [0, 360).
fixAngle(float|int $angle) : float
If the input is not numeric (NaN, null, etc.), the function will return 0.
Parameters
- $angle : float|int
-
The angle in degrees.
Tags
Return values
float —The normalized angle between 0 (inclusive) and 360 (exclusive).
floorValue()
Rounds and returns a number by a count of floating points, using floor.
floorValue(int|float $value[, int $floatCount = 0 ]) : float
The floor of a number is the closest integer less than or equal to the number.
Parameters
- $value : int|float
-
The number to round.
- $floatCount : int = 0
-
The number of decimal places to round down to.
Tags
Return values
float —The rounded number.
gcd()
Calculate the Greatest Common Divisor (GCD) of two integers using the Euclidean algorithm.
gcd(int $a, int $b[, bool $throwable = false ]) : int
This function returns the absolute value of the GCD. If both numbers
are zero, it either returns 0 or throws an exception depending on
the $throwable parameter.
Parameters
- $a : int
-
The first integer.
- $b : int
-
The second integer.
- $throwable : bool = false
-
If true, throws an exception when both numbers are zero.
Tags
Return values
int —The greatest common divisor of $a and $b.
haversine()
Calculate the great-circle distance between two points on a sphere using the Haversine formula.
haversine(float $latitude1, float $longitude1, float $latitude2, float $longitude2[, float $radius = 6371000 ][, int|null $precision = null ]) : float
This is commonly used for distances on Earth. It's faster than the Vincenty formula but less precise.
Parameters
- $latitude1 : float
-
The latitude of the first point in degrees.
- $longitude1 : float
-
The longitude of the first point in degrees.
- $latitude2 : float
-
The latitude of the second point in degrees.
- $longitude2 : float
-
The longitude of the second point in degrees.
- $radius : float = 6371000
-
The radius of the sphere (default is Earth's mean radius: 6,371,000 meters).
- $precision : int|null = null
-
Number of decimal places to round to. If null, returns full precision.
Tags
Return values
float —The distance between the two points in meters.
isPrime()
Tells whether an integer is a prime number.
isPrime(int $n) : bool
Any integer lower than 2 is not prime. The test uses 6k ± 1 trial division up
to √n.
Parameters
- $n : int
-
The integer to test.
Tags
Return values
bool —true if $n is prime, false otherwise.
mean()
Computes the arithmetic mean (average) of a list of numbers.
mean(array<int, int|float> $values) : float
Parameters
- $values : array<int, int|float>
-
A non-empty list of numeric values.
Tags
Return values
float —The arithmetic mean of the values.
median()
Computes the median (middle value) of a list of numbers.
median(array<int, int|float> $values) : float
The values are sorted numerically. For an odd number of values the middle one is returned ; for an even number, the average of the two central values is returned.
Parameters
- $values : array<int, int|float>
-
A non-empty list of numeric values.
Tags
Return values
float —The median of the values.
polarToCartesian()
Converts a polar coordinate to a cartesian vector.
polarToCartesian(array{angle?: float, radius?: float} $vector[, bool $degrees = true ][, bool $throwable = false ]) : array{x: float, y: float}
Parameters
- $vector : array{angle?: float, radius?: float}
-
Polar coordinates with keys 'angle' and 'radius'. Both are optional — a missing one defaults to
0, or throws when$throwableis true. - $degrees : bool = true
-
Whether the angle is in degrees (default: true).
- $throwable : bool = false
-
Whether to throw when the 'angle'/'radius' keys are missing (default: false).
Tags
Return values
array{x: float, y: float} —Cartesian representation with keys 'x' and 'y'.
roundValue()
Rounds and returns the rounded value of the specified number or expression.
roundValue(int|float $value[, int $floatCount = 0 ]) : float
Parameters
- $value : int|float
-
The number to round.
- $floatCount : int = 0
-
The number of decimal places to round to.
Tags
Return values
float —The rounded number.
stddev()
Computes the standard deviation of a list of numbers.
stddev(array<int, int|float> $values[, bool $sample = false ]) : float
The standard deviation is the square root of the variance(). By default the
population standard deviation is returned ; when $sample is true, the sample
standard deviation is returned (Bessel's correction, N - 1).
Parameters
- $values : array<int, int|float>
-
A list of numeric values (at least one for population, two for sample).
- $sample : bool = false
-
Whether to compute the sample standard deviation (
N - 1) instead of the population one (N).
Tags
Return values
float —The standard deviation of the values.
variance()
Computes the variance of a list of numbers.
variance(array<int, int|float> $values[, bool $sample = false ]) : float
By default the population variance is returned (sum of squared deviations divided
by N). When $sample is true, the sample variance is returned instead (divided
by N - 1, Bessel's correction).
Parameters
- $values : array<int, int|float>
-
A list of numeric values (at least one for population, two for sample).
- $sample : bool = false
-
Whether to compute the sample variance (
N - 1) instead of the population one (N).
Tags
Return values
float —The variance of the values.