Skip to main content

Math, Numbers, and Statistics

@Math — mathematical functions (62 methods)

Basic operations

ZYMBA
echo @Math.abs(-5); // 5
echo @Math.neg(5); // -5
echo @Math.sgn(-5); // -1 (sign: -1, 0, or 1)
echo @Math.round(3.7); // 4
echo @Math.floor(3.7); // 3
echo @Math.ceil(3.2); // 4
echo @Math.truncate(3.7); // 3 (toward zero)
echo @Math.sqrt(16); // 4
echo @Math.cbrt(27); // 3 (cube root)
echo @Math.sqr(5); // 25 (square)
echo @Math.recip(4); // 0.25 (reciprocal)
echo @Math.pow(2, 10); // 1024
echo @Math.mod(10, 3); // 1
echo @Math.gcd(12, 8); // 4 (greatest common divisor)
echo @Math.hypot(3, 4); // 5
echo @Math.dim(10, 3); // 7 (positive difference)

Trigonometry

ZYMBA
echo @Math.sin(0); // 0
echo @Math.cos(0); // 1
echo @Math.tan(@Math.PI / 4); // ~1
echo @Math.asin(0.5); // 0.5236...
echo @Math.acos(0.5); // 1.0472...
echo @Math.atan(1); // 0.7854...
echo @Math.atan2(1, 1); // 0.7854...

// Hyperbolic
echo @Math.sinh(1); // 1.1752...
echo @Math.cosh(1); // 1.5431...
echo @Math.tanh(1); // 0.7616...

// Angle conversion
echo @Math.fromDegrees(180); // 3.14159... (PI)
echo @Math.toDegrees(@Math.PI); // 180

Additional: "sec, csc, cot, acot, asec, acsc, and their hyperbolic variants."

Logarithms and exponentials

ZYMBA
echo @Math.ln(@Math.E); // 1 (natural log)
echo @Math.ln1p(0.5); // ln(1.5)
echo @Math.log(100, 10); // 2 (log base 10)
echo @Math.exp(1); // 2.71828...
echo @Math.expm1(0.5); // exp(0.5) - 1

Special functions

ZYMBA
echo @Math.fib(10); // 55 (Fibonacci)
echo @Math.gamma(5); // 24 (gamma function)
echo @Math.erf(1); // 0.8427... (error function)
echo @Math.rand(); // Random float 0-1

Modular arithmetic

ZYMBA
echo @Math.euclideanMod(7, 3); // 1 (always non-negative)
echo @Math.flooredMod(-7, 3); // 2
echo @Math.truncatedDiv(7, 3); // 2

Constants

ConstantValue
@Math.PI3.14159265358979...
@Math.E2.71828182845904...
@Math.LN20.69314...
@Math.LN102.30258...
@Math.LNPI1.14472...
@Math.LOG2E1.44269...
@Math.LOG10E0.43429...
@Math.SQRT21.41421...
@Math.SQRT31.73205...
@Math.SQRTPI1.77245...

@Number — number utilities (42 methods)

Formatting

ZYMBA
echo @Number.format(1234.5, 2, ".", ","); // "1,234.50"
echo @Number.toFixed(3.14159, 2); // "3.14"
echo @Number.toExponential(12345, 2); // "1.23e+4"
echo @Number.toPrecision(3.14159, 4); // "3.142"
echo @Number.normalize(42.0); // 42 (remove trailing zeros)

Min, max, random

ZYMBA
echo @Number.max(3, 7, 1, 9, 2); // 9
echo @Number.min(3, 7, 1, 9, 2); // 1
echo @Number.randomInt(1, 100); // Random integer 1-100
echo @Number.randomFloat(0.0, 1.0); // Random float 0-1

Type checks

ZYMBA
echo @Number.isEven(4); // true
echo @Number.isOdd(3); // true
echo @Number.isPrime(7); // true
echo @Number.isNatural(5); // true (non-negative integer)
echo @Number.isPositive(5); // true
echo @Number.isNegative(-5); // true
echo @Number.isZero(0); // true
echo @Number.isFinite(42); // true
echo @Number.isInfinite(1/0); // true
echo @Number.isNaN(0.0 / 0); // true
echo @Number.isSafeInteger(42); // true

Base conversion

ZYMBA
echo @Number.toHex(255); // "ff"
echo @Number.toBinary(10); // "1010"
echo @Number.toOctal(8); // "10"
echo @Number.fromHex("ff"); // 255
echo @Number.fromBinary("1010"); // 10
echo @Number.fromOctal("10"); // 8

Rounding variants

ZYMBA
echo @Number.roundHalfUp(2.5, 0); // 3
echo @Number.roundHalfDown(2.5, 0); // 2
echo @Number.roundHalfToEven(2.5, 0); // 2 (banker's rounding)
echo @Number.roundHalfToOdd(2.5, 0); // 3
echo @Number.roundHalfToInfinity(2.5, 0); // 3
echo @Number.roundHalfToZero(2.5, 0); // 2
echo @Number.roundUp(2.1, 0); // 3
echo @Number.roundDown(2.9, 0); // 2

Constants

ConstantDescription
@Number.EPSILONSmallest representable difference
@Number.INFPositive infinity
@Number.NANNot a Number
@Number.MAXINTMaximum integer value
@Number.MININTMinimum integer value
@Number.MAXFLOATMaximum float value
@Number.MINFLOATMinimum positive float

@Statistic — statistical analysis (29 methods)

ZYMBA
$data = [4, 8, 15, 16, 23, 42];

// Central tendency
echo @Statistic.arithmeticMean($data); // 18
echo @Statistic.average($data); // 18 (alias)
echo @Statistic.median($data); // 15.5
echo @Statistic.mode([1,2,2,3,3,3]); // [3]
echo @Statistic.geometricMean($data); // 13.97
echo @Statistic.harmonicMean($data); // 10.50
echo @Statistic.midrange($data); // 23

// Spread
echo @Statistic.minimum($data); // 4
echo @Statistic.maximum($data); // 42
echo @Statistic.range($data); // 38
echo @Statistic.sum($data); // 108
echo @Statistic.count($data); // 6
echo @Statistic.averageDeviation($data); // 9.67
echo @Statistic.medianDeviation($data); // 7.5

// Higher moments
echo @Statistic.centralMoment($data, 2); // 2nd central moment
echo @Statistic.centralMoment($data, 3); // 3rd central moment

// Moving averages
echo @Statistic.linearMovingAverage($data, 3); // Linear MA
echo @Statistic.exponentialMovingAverage($data, 3); // Exponential MA