Skip to content

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.

<?nvs
echo 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";
Output
7 2.5 -1
9 10
3 1.23 2
1 2 -1
3 1 1.5
4 5 2
1 180
ff 255 101
1,234,567.89
6 12
nan 9223372036854775807

Members

39 members.

MemberSignature
absabs(T $n): T
signsign(int|float|decimal $n): int
minmin(T $a, T $b): T
maxmax(T $a, T $b): T
clampclamp(T $n, T $low, T $high): T
ceilceil(float $n): float
floorfloor(float $n): float
truncatetruncate(float $n): float
roundround(float $n, { precision?: int, mode?: Core\RoundMode }): float
quotientquotient(int $a, int $b): int
remainderremainder(float $a, float $b): float
dividedivide(float $a, float $b): float
gcdgcd(int $a, int $b): int
lcmlcm(int $a, int $b): int
sqrtsqrt(float $n): float
cbrtcbrt(float $n): float
hypothypot(float $a, float $b): float
expexp(float $n): float
loglog(float $n, { base?: float }): float
sinsin(float $radians): float
coscos(float $radians): float
tantan(float $radians): float
asinasin(float $n): float
acosacos(float $n): float
atanatan(float $n): float
atan2atan2(float $y, float $x): float
sinhsinh(float $n): float
coshcosh(float $n): float
tanhtanh(float $n): float
asinhasinh(float $n): float
acoshacosh(float $n): float
atanhatanh(float $n): float
toRadianstoRadians(float $degrees): float
toDegreestoDegrees(float $radians): float
isNanisNan(float $n): bool
isFiniteisFinite(float $n): bool
toBasetoBase(int $n, uint $base): string
fromBasefromBase(string $s, uint $base): int
formatformat(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 nearest float.
  • Core\Math::TAUThe ratio of a circle's circumference to its radius, 2 * PI or 6.28318… as the nearest float. This is a full turn in radians.
  • Core\Math::EEuler's number, the base of the natural logarithm, 2.71828… as the nearest float. It is the default base of log.
  • Core\Math::EPSILONThe smallest float that added to 1.0 gives a value other than 1.0, 2.220446049250313e-16. Use it as the tolerance when you compare two floats, in place of ==.
  • Core\Math::INT_MAXThe largest int, 9223372036854775807.
  • Core\Math::INT_MINThe smallest int, -9223372036854775808. It is the one value abs cannot return as an int.
  • Core\Math::UINT_MAXThe largest uint, 18446744073709551615.
  • Core\Math::FLOAT_MAXThe largest finite float, 1.7976931348623157e308. The most negative one is -FLOAT_MAX.
  • Core\Math::FLOAT_MINThe smallest positive normal float, 2.2250738585072014e-308. The most negative float is -FLOAT_MAX.
  • Core\Math::NANThe float that is not a number. It is equal to nothing, itself included, so isNan is the only test for it.
  • Core\Math::INFINITYPositive infinity as a float, larger than every finite value. Negative infinity is -INFINITY, and isFinite is false for both.