math
Scalar math: Python's math module plus the math-flavored built-ins (abs/min/max/pow). Every function is pure and allocation-free — it operates on double / long long by value and never touches the heap.
import io
import math
io.print(math.sqrt(2.0)) # 1.4142135623730951
io.print(math.gcd(48, 18)) # 6
io.print(math.abs(-3), math.max(1, 9, 4)) # 3 9What's here
Constants —
pi,e,tau,inf,nan.Built-ins —
abs,min,max(variadic),pow.Roots & rounding —
sqrt,cbrt,fabs,floor,ceil,trunc,round.Exp & logs —
exp,log,log2,log10.Trigonometry —
sin,cos,tan,asin,acos,atan,atan2,hypot.Float utilities —
fmod,copysign,degrees,radians,isnan,isinf,isfinite.Integer —
gcd(O(log min)),factorial(O(n)).
Per-function docs (parameters, runtime complexity, heap behavior) are in math.hpp. Tested in ../tests/math_test.cpp; ASan + Valgrind clean via the QA gate (security/run-valgrind.sh).
Functions
Square root.
Returns NaN (rather than throwing) for a negative radicand; sqrt(-0.0) is -0.0 and sqrt(+inf) is +inf.
x | radicand (NaN if |
√x.
O(1).
none.
CheatahMath.ScalarFunctionsMathCompileRun.SqrtCube root.
Defined for the whole real line, including negatives (unlike sqrt): the result keeps the sign of x, so cbrt(-8) is -2.
x | any real. |
∛x.
O(1).
none.
MathCompileRun.CbrtStdlibE2E.MathAbsolute value of a double.
x | any real. |
|x|.
O(1).
none.
MathCompileRun.FabsStdlibE2E.Math SystemApps.MonteCarloRound toward −∞.
Returns the largest integral value not greater than x as a double; already-integral, NaN, and ±∞ inputs are returned unchanged, and the sign of zero is preserved.
x | any real. |
⌊x⌋.
O(1).
none.
CheatahMath.ScalarFunctionsMathCompileRun.FloorStdlibE2E.MathRound toward +∞.
Returns the smallest integral value not less than x as a double; already-integral, NaN, and ±∞ inputs are returned unchanged. For x in (−1, 0) the result is -0.0.
x | any real. |
⌈x⌉.
O(1).
none.
CheatahMath.ScalarFunctionsMathCompileRun.CeilStdlibE2E.MathRound toward zero.
Discards the fractional part, rounding toward zero rather than ±∞ (so it differs from floor on negatives, e.g. trunc(-2.7) is -2.0); the sign of x, NaN, and ±∞ are preserved.
x | any real. |
x with the fraction dropped.
O(1).
none.
MathCompileRun.TruncStdlibE2E.MathRound to nearest (half away from zero).
Halfway cases are rounded away from zero, not to even, so round(2.5) is 3 and round(-2.5) is -3 — this differs from Python's banker's rounding; NaN and ±∞ pass through unchanged.
x | any real. |
rounded x.
O(1).
none.
CheatahMath.ScalarFunctionsMathCompileRun.RoundExponential.
Overflows to +inf for large x and underflows to 0 for large negative x; exp(-inf) is 0 and exp(+inf) is +inf.
x | any real. |
e^x.
O(1).
none.
MathCompileRun.ExpStdlibE2E.MathNatural logarithm.
Returns -inf for x == 0 and NaN (rather than throwing) for negative x; out-of-domain input never raises an exception.
x | > 0. |
ln(x).
O(1).
none.
MathCompileRun.LogStdlibE2E.MathBase-2 logarithm.
Returns -inf for x == 0 and NaN for negative x, matching log's out-of-domain behavior.
x | > 0. |
log₂(x).
O(1).
none.
CheatahMath.ScalarFunctionsMathCompileRun.Log2StdlibE2E.MathBase-10 logarithm.
Returns -inf for x == 0 and NaN for negative x, matching log's out-of-domain behavior.
x | > 0. |
log₁₀(x).
O(1).
none.
MathCompileRun.Log10StdlibE2E.MathSine.
The argument is interpreted in radians; precision degrades for very large magnitudes due to argument reduction, and sin(±inf) is NaN.
x | radians. |
sin(x).
O(1).
none.
CheatahMath.TrigonometryMathCompileRun.SinStdlibE2E.MathCosine.
The argument is interpreted in radians; precision degrades for very large magnitudes due to argument reduction, and cos(±inf) is NaN.
x | radians. |
cos(x).
O(1).
none.
CheatahMath.TrigonometryMathCompileRun.CosStdlibE2E.MathTangent.
The argument is in radians; near the poles (odd multiples of π/2, which are not exactly representable) the result is a large finite value rather than ±∞, and tan(±inf) is NaN.
x | radians. |
tan(x).
O(1).
none.
CheatahMath.TrigonometryMathCompileRun.TanStdlibE2E.MathArcsine.
Returns a value in [−π/2, π/2]; arguments outside [−1, 1] yield NaN rather than throwing.
x | in [−1, 1]. |
asin(x) in radians.
O(1).
none.
CheatahMath.TrigonometryMathCompileRun.AsinStdlibE2E.MathArccosine.
Returns a value in [0, π]; arguments outside [−1, 1] yield NaN rather than throwing.
x | in [−1, 1]. |
acos(x) in radians.
O(1).
none.
CheatahMath.TrigonometryMathCompileRun.AcosStdlibE2E.MathArctangent.
Accepts the whole real line and returns a value in (−π/2, π/2), approaching ±π/2 as x → ±∞.
x | any real. |
atan(x) in radians.
O(1).
none.
CheatahMath.TrigonometryMathCompileRun.AtanStdlibE2E.MathTwo-argument arctangent.
Uses the signs of both arguments to select the correct quadrant, returning a value in (−π, π]; it is well-defined when x is zero (including the atan2(0, 0) case, which returns 0).
y, x | the coordinates. |
atan2(y, x) in radians.
O(1).
none.
CheatahMath.TrigonometryMathCompileRun.Atan2StdlibE2E.MathHypotenuse.
Computes the 2-norm while avoiding intermediate overflow/underflow that a naive sqrt(x*x + y*y) would suffer; returns +inf if either argument is infinite (even when the other is NaN).
x, y | the legs. |
√(x²+ without overflow.y²)
O(1).
none.
CheatahMath.ScalarFunctionsMathCompileRun.HypotStdlibE2E.MathFloating-point remainder.
Returns x - n*y for the integer n truncated toward zero, so the result takes the sign of the dividend x (unlike a Python-style modulo); a zero divisor yields NaN rather than throwing.
x, y | dividend, divisor. |
x mod y.
O(1).
none.
MathCompileRun.FmodStdlibE2E.MathCopy sign.
Takes the magnitude from x and the sign bit from y; because it copies the IEEE sign bit, it distinguishes +0.0 from -0.0 and works even when x is NaN.
x | magnitude source, |
y | sign source. |
|x| with y's sign.
O(1).
none.
MathCompileRun.CopysignStdlibE2E.MathRadians → degrees.
radians | angle in radians. |
the angle in degrees.
O(1).
none.
CheatahMath.ScalarFunctionsMathCompileRun.DegreesStdlibE2E.MathDegrees → radians.
degrees | angle in degrees. |
the angle in radians.
O(1).
none.
MathCompileRun.RadiansStdlibE2E.MathIs NaN?
The reliable NaN test, since NaN compares unequal to everything including itself (so x != x is the only other portable check).
x | any real. |
true iff x is NaN.
O(1).
none.
CheatahMath.IsFiniteIsNanIsInfMathCompileRun.IsnanStdlibE2E.MathIs infinite?
True for both +inf and -inf; false for NaN (use isnan for that) and for every finite value.
x | any real. |
true iff x is ±∞.
O(1).
none.
CheatahMath.IsFiniteIsNanIsInfMathCompileRun.IsinfStdlibE2E.MathIs finite?
x | any real. |
true iff x is neither NaN nor ±∞.
O(1).
none.
CheatahMath.IsFiniteIsNanIsInfMathCompileRun.IsfiniteStdlibE2E.MathGreatest common divisor.
Operates on the absolute values via the Euclidean algorithm, so the result is non-negative; gcd(0, 0) is 0 and gcd(n, 0) is |n|. Passing LLONG_MIN overflows when negated.
a, b | integers. |
gcd(|a|, |b|).
O(log min(a,b)) time.
none.
CheatahMath.IntegerMathCompileRun.GcdStdlibE2E.MathFactorial.
Computed by an iterative product from 2; n of 0 or 1 returns 1, and any negative n also returns 1 since the loop never executes (no error is raised). Results past 20! silently overflow long long.
n | ≥ 0 (small; overflows |
n!.
O(n) time.
none.
CheatahMath.IntegerMathCompileRun.FactorialStdlibE2E.MathAbsolute value.
x | any signed value. |
|x|.
O(1) time.
none.
For a signed integer type the most-negative value cannot be negated: abs of it overflows (undefined behavior).
CheatahMath.BuiltinLikeOpsMathCompileRun.AbsStdlibE2E.Mathmin · 2 overloads
Smallest of two-or-more values (variadic; the overloads chain to fold extra args).
Returns a reference bound to whichever argument compares smaller; on a tie (neither b < a) it returns a. Because the result is a reference into the caller's arguments, it dangles when the operands are temporaries.
a, b | the values to compare ( |
a reference to the minimum.
O(n) in the argument count.
none.
CheatahMath.BuiltinLikeOpsMathCompileRun.MinStdlibE2E.Math SystemApps.GradeReportmax · 2 overloads
Largest of two-or-more values (variadic; the overloads chain to fold extra args).
Returns a reference bound to whichever argument compares larger; on a tie (neither a < b) it returns a. As with min, the returned reference dangles if the operands are temporaries.
a, b | the values to compare ( |
a reference to the maximum.
O(n) in the argument count.
none.
CheatahMath.BuiltinLikeOpsMathCompileRun.MaxStdlibE2E.Math SystemApps.GradeReportPower.
Both operands are cast to double and forwarded to std::pow, so this follows IEEE-754 semantics (e.g. pow(0, 0) is 1, and a negative base with a non-integer exponent yields NaN); integer arguments lose exactness beyond 2^53.
base | the base. |
exp | the exponent. |
base raised to exp (computed as double).
O(1) time.
none.
CheatahMath.BuiltinLikeOpsMathCompileRun.PowStdlibE2E.MathConstants & variables
π.
Euler's number e.
τ = 2π.
+∞.
quiet NaN.
