p-math
The math module provides all JavaScript Math methods and lowercase constant aliases.
use p-math as mConstants
Section titled “Constants”| Name | Value | Description |
|---|---|---|
pi | 3.141592653589793 | Ratio of a circle’s circumference to its diameter |
e | 2.718281828459045 | Euler’s number |
ln2 | 0.6931471805599453 | Natural logarithm of 2 |
ln10 | 2.302585092994046 | Natural logarithm of 10 |
log2e | 1.4426950408889634 | Base-2 logarithm of e |
log10e | 0.4342944819032518 | Base-10 logarithm of e |
sqrt2 | 1.4142135623730951 | Square root of 2 |
sqrt1_2 | 0.7071067811865476 | Square root of 1/2 |
use p-math as mconsole.log[m.pi] -- 3.141592653589793console.log[m.e] -- 2.718281828459045Rounding
Section titled “Rounding”floor[x]
Section titled “floor[x]”Returns the largest integer less than or equal to x.
ceil[x]
Section titled “ceil[x]”Returns the smallest integer greater than or equal to x.
round[x]
Section titled “round[x]”Returns x rounded to the nearest integer.
trunc[x]
Section titled “trunc[x]”Returns the integer part of x by removing any fractional digits.
use p-math as mm.floor[4.7] -- 4m.ceil[4.1] -- 5m.round[4.5] -- 5m.trunc[4.9] -- 4Arithmetic
Section titled “Arithmetic”abs[x]
Section titled “abs[x]”Returns the absolute value of x.
sign[x]
Section titled “sign[x]”Returns -1, 0, or 1 indicating the sign of x.
max[a; b; ...]
Section titled “max[a; b; ...]”Returns the largest of the given numbers.
min[a; b; ...]
Section titled “min[a; b; ...]”Returns the smallest of the given numbers.
pow[base; exp]
Section titled “pow[base; exp]”Returns base raised to the power exp.
sqrt[x]
Section titled “sqrt[x]”Returns the square root of x.
cbrt[x]
Section titled “cbrt[x]”Returns the cube root of x.
hypot[a; b; ...]
Section titled “hypot[a; b; ...]”Returns the square root of the sum of squares of its arguments.
clz32[x]
Section titled “clz32[x]”Returns the number of leading zero bits in the 32-bit integer representation of x.
fround[x]
Section titled “fround[x]”Returns the nearest 32-bit single precision float representation of x.
imul[a; b]
Section titled “imul[a; b]”Returns the result of the C-like 32-bit multiplication of a and b.
use p-math as mm.abs[-5] -- 5m.max[1; 2; 3] -- 3m.min[1; 2; 3] -- 1m.sqrt[16] -- 4m.hypot[3; 4] -- 5Trigonometry
Section titled “Trigonometry”sin[x] / cos[x] / tan[x]
Section titled “sin[x] / cos[x] / tan[x]”Standard trigonometric functions. x is in radians.
asin[x] / acos[x] / atan[x]
Section titled “asin[x] / acos[x] / atan[x]”Inverse trigonometric functions. Returns radians.
atan2[y; x]
Section titled “atan2[y; x]”Returns the angle in radians between the positive x-axis and the point (x, y).
sinh[x] / cosh[x] / tanh[x]
Section titled “sinh[x] / cosh[x] / tanh[x]”Hyperbolic functions.
asinh[x] / acosh[x] / atanh[x]
Section titled “asinh[x] / acosh[x] / atanh[x]”Inverse hyperbolic functions.
use p-math as mm.sin[m.pi div 2] -- 1m.cos[0] -- 1m.atan2[1; 1] -- 0.7853... (π/4)Logarithms and Exponents
Section titled “Logarithms and Exponents”log[x]
Section titled “log[x]”Returns the natural logarithm (base e) of x.
log2[x]
Section titled “log2[x]”Returns the base-2 logarithm of x.
log10[x]
Section titled “log10[x]”Returns the base-10 logarithm of x.
exp[x]
Section titled “exp[x]”Returns e raised to the power x.
expm1[x]
Section titled “expm1[x]”Returns e^x - 1, accurate for small values of x.
log1p[x]
Section titled “log1p[x]”Returns the natural logarithm of 1 + x, accurate for small values of x.
use p-math as mm.log[m.e] -- 1m.log2[8] -- 3m.log10[1000] -- 3m.exp[1] -- 2.718281828459045Random
Section titled “Random”random[]
Section titled “random[]”Returns a floating-point number in [0, 1). For more random utilities, see the random module.
use p-math as mconst x be m.random[] -- e.g. 0.4231...