Arithmetic and numbers#
Rounding and sign#
floor rounds toward minus infinity, ceil toward plus infinity, fix toward zero, and round to the nearest value. Negative inputs make the difference obvious.
floor(3.2)
floor(3.8)
floor(-4.2)Interactive examples could not load. You can still copy the expressions.
ceil(-3.8)
fix(-3.8)
round(pi,3)
abs(-5)
sign(-5)Interactive examples could not load. You can still copy the expressions.
Powers, roots, and logarithms#
Use ^, sqrt, cbrt, nthRoot, exp, log, log2, and log10. For log, the base goes last: log(value, base). Near zero, prefer log1p and expm1, which keep precision. See Complex numbers for complex roots.
sqrt(25)
cbrt(-8)
nthRoot(16,4)
log(1000,10)Interactive examples could not load. You can still copy the expressions.
Exact and large values#
By default, Number uses floating-point arithmetic. Change the note default with Numeric Type in Math settings, or pick a type per expression:
fraction(1,3) + fraction(1,6)
bignumber("0.1") + bignumber("0.2")
bigint("12345678901234567890")Interactive examples could not load. You can still copy the expressions.
A fraction is an exact rational. BigNumber keeps decimals at higher precision. BigInt holds integers only, so it does not suit ordinary fractional work. Build large precise values from strings, otherwise Number rounds them first.
Integers and special functions#
The reference also covers factorial, gamma and log-gamma, gcd and lcm, modular inverse, extended GCD, primality, combinatorics, Bernoulli numbers, Catalan, Bell and Stirling numbers, the error function, and zeta.
factorial(5)
gcd(12,18)
lcm(12,18)
invmod(3,11)
isPrime(7)Interactive examples could not load. You can still copy the expressions.
Bits and predicates#
bitAnd, bitOr, bitXor, bitNot, leftShift, rightArithShift, and rightLogShift work on integer bits. isFinite, isNaN, isInteger, isPositive, and isZero test values. Check the individual overloads before using BigInt.