Core\Math
Functions and constants for numbers: rounding, powers, roots, logarithms, angles and limits. A result that does not fit its type throws an ArithmeticError, and a value outside a function's range, such as sqrt(-1.0), gives NaN.
Core\Math is the numeric function set; the arithmetic operators + - * / % ** and <=> are the language's and have no member here. abs, sign, min, max and clamp take an int, a float or a decimal and answer in the argument's own type; the rounding, root, exponential, logarithmic and trigonometric members take and answer float; quotient, gcd, lcm, toBase and fromBase work over int. round names its tie rule as a Core\RoundMode case and defaults to HalfUp — and answers a float, so round(6.0, { decimals: 2 }) prints 6; a number shown with a fixed count of decimals is format's job, not round's. format groups digits only when asked, since there is no locale. A refusal throws — fromBase on a digit outside the base, format on an infinity — never answers false. The constants are class constants: Core\Math::PI, INT_MAX, EPSILON, NAN, INFINITY.
<?nvsecho Core\Math::abs(-7), ' ', Core\Math::abs(-2.5), ' ', Core\Math::sign(-3), "\n";echo Core\Math::max(3, 9), ' ', Core\Math::clamp(15, 0, 10), "\n";echo Core\Math::round(2.5), ' ', Core\Math::round(1.2345, { precision: 2 }), ' ', Core\Math::round(2.5, { mode: Core\RoundMode::HalfEven }), "\n";echo Core\Math::floor(1.8), ' ', Core\Math::ceil(1.2), ' ', Core\Math::truncate(-1.7), "\n";echo Core\Math::quotient(7, 2), ' ', 7 % 2, ' ', Core\Math::remainder(7.5, 2.0), "\n";echo Core\Math::sqrt(16.0), ' ', Core\Math::hypot(3.0, 4.0), ' ', Core\Math::log(100.0, { base: 10.0 }), "\n";echo Core\Math::round(Core\Math::sin(Core\Math::PI / 2), { precision: 3 }), ' ', Core\Math::toDegrees(Core\Math::PI), "\n";echo Core\Math::toBase(255, 16), ' ', Core\Math::fromBase('ff', 16), ' ', Core\Math::toBase(5, 2), "\n";echo Core\Math::format(1234567.891, { decimals: 2, groupSeparator: ',' }), "\n";echo Core\Math::gcd(12, 18), ' ', Core\Math::lcm(4, 6), "\n";echo Core\Math::isNan(Core\Math::NAN) ? 'nan' : 'number', ' ', Core\Math::INT_MAX, "\n";7 2.5 -19 103 1.23 21 2 -13 1 1.54 5 21 180ff 255 1011,234,567.896 12nan 9223372036854775807Members
39 members.
| Member | Signature |
|---|---|
abs | abs(T $n): T |
sign | sign(int|float|decimal $n): int |
min | min(T $a, T $b): T |
max | max(T $a, T $b): T |
clamp | clamp(T $n, T $low, T $high): T |
ceil | ceil(float $n): float |
floor | floor(float $n): float |
truncate | truncate(float $n): float |
round | round(float $n, { precision?: int, mode?: Core\RoundMode }): float |
quotient | quotient(int $a, int $b): int |
remainder | remainder(float $a, float $b): float |
divide | divide(float $a, float $b): float |
gcd | gcd(int $a, int $b): int |
lcm | lcm(int $a, int $b): int |
sqrt | sqrt(float $n): float |
cbrt | cbrt(float $n): float |
hypot | hypot(float $a, float $b): float |
exp | exp(float $n): float |
log | log(float $n, { base?: float }): float |
sin | sin(float $radians): float |
cos | cos(float $radians): float |
tan | tan(float $radians): float |
asin | asin(float $n): float |
acos | acos(float $n): float |
atan | atan(float $n): float |
atan2 | atan2(float $y, float $x): float |
sinh | sinh(float $n): float |
cosh | cosh(float $n): float |
tanh | tanh(float $n): float |
asinh | asinh(float $n): float |
acosh | acosh(float $n): float |
atanh | atanh(float $n): float |
toRadians | toRadians(float $degrees): float |
toDegrees | toDegrees(float $radians): float |
isNan | isNan(float $n): bool |
isFinite | isFinite(float $n): bool |
toBase | toBase(int $n, uint $base): string |
fromBase | fromBase(string $s, uint $base): int |
format | format(int|float|decimal $n, { decimals?: uint, decimalSeparator?: string, groupSeparator?: string }): string |
Constants
Core\Math::PIThe ratio of a circle's circumference to its diameter,3.14159…as the nearestfloat.Core\Math::TAUThe ratio of a circle's circumference to its radius,2 * PIor6.28318…as the nearestfloat. This is a full turn in radians.Core\Math::EEuler's number, the base of the natural logarithm,2.71828…as the nearestfloat. It is the default base oflog.Core\Math::EPSILONThe smallestfloatthat added to1.0gives a value other than1.0,2.220446049250313e-16. Use it as the tolerance when you compare two floats, in place of==.Core\Math::INT_MAXThe largestint,9223372036854775807.Core\Math::INT_MINThe smallestint,-9223372036854775808. It is the one valueabscannot return as anint.Core\Math::UINT_MAXThe largestuint,18446744073709551615.Core\Math::FLOAT_MAXThe largest finitefloat,1.7976931348623157e308. The most negative one is-FLOAT_MAX.Core\Math::FLOAT_MINThe smallest positive normalfloat,2.2250738585072014e-308. The most negativefloatis-FLOAT_MAX.Core\Math::NANThefloatthat is not a number. It is equal to nothing, itself included, soisNanis the only test for it.Core\Math::INFINITYPositive infinity as afloat, larger than every finite value. Negative infinity is-INFINITY, andisFiniteisfalsefor both.
