maths
Table of Contents
Functions
- 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() : int|float
- Rounds and returns the ceiling of the specified number or expression.
- fixAngle() : float
- Normalize an angle in degrees to the range [0, 360).
- floorValue() : int|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.
- polarToCartesian() : array{x: float, y: float}
- Converts a polar coordinate to a cartesian vector.
- roundValue() : int|float
- Rounds and returns the rounded value of the specified number or expression.
Functions
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 ]) : int|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
int|float —The rounded number.
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 ]) : int|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
int|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.
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'.
- $degrees : bool = true
-
Whether the angle is in degrees (default: true).
- $throwable : bool = 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 ]) : int|float
Parameters
- $value : int|float
-
The number to round.
- $floatCount : int = 0
-
The number of decimal places to round to.
Tags
Return values
int|float —The rounded number.