math
let math = import("math")Constants
Section titled “Constants”| Constant | Type | Description |
|---|---|---|
PI |
float |
pi |
TAU |
float |
2*pi |
E |
float |
Euler’s number |
printf("pi = {:.6f}", math.PI)Roots and powers
Section titled “Roots and powers”| Function | Returns | Description |
|---|---|---|
sqrt(x: int | float) |
float |
Square root |
cbrt(x: int | float) |
float |
Cube root |
pow(b: int | float, e: int | float) |
float |
base raised to exp |
hypot(x: int | float, y: int | float) |
float |
sqrt(x*x + y*y) |
exp(x: int | float) |
float |
e^x |
log(x: int | float) |
float |
Natural logarithm |
log2(x: int | float) |
float |
Base-2 logarithm |
log10(x: int | float) |
float |
Base-10 logarithm |
Trigonometry
Section titled “Trigonometry”All angles are in radians.
| Function | Returns | Description |
|---|---|---|
sin(x: int | float) |
float |
Sine |
cos(x: int | float) |
float |
Cosine |
tan(x: int | float) |
float |
Tangent |
asin(x: int | float) |
float |
Inverse sine |
acos(x: int | float) |
float |
Inverse cosine |
atan(x: int | float) |
float |
Inverse tangent |
atan2(y: int | float, x: int | float) |
float |
Angle of the vector (x, y) |
sinh(x: int | float) |
float |
Hyperbolic sine |
cosh(x: int | float) |
float |
Hyperbolic cosine |
tanh(x: int | float) |
float |
Hyperbolic tangent |
radians(d: int | float) |
float |
Degrees to radians |
degrees(r: int | float) |
float |
Radians to degrees |
printf("sin(pi/2) = {}", math.sin(math.PI / 2))printf("90 degrees = {} radians", math.radians(90))Rounding
Section titled “Rounding”| Function | Returns | Description |
|---|---|---|
floor(x: int | float) |
int |
Largest integer not greater than x; returns int |
ceil(x: int | float) |
int |
Smallest integer not less than x; returns int |
round(x: int | float) |
int |
Nearest int, half away from zero |
trunc(x: int | float) |
int |
Truncate toward zero; returns int |
math.floor(3.9) # 3math.ceil(3.1) # 4math.round(2.5) # 3math.round(-2.5) # -3math.trunc(-3.9) # -3round is half away from zero, not banker’s rounding.
General
Section titled “General”| Function | Returns | Description |
|---|---|---|
abs(x: int | float) |
int | float |
Absolute value; preserves int or float |
sign(x: int | float) |
int |
-1, 0, or 1 as an int |
min(a: int | float, b: int | float) |
int | float |
Smaller value |
max(a: int | float, b: int | float) |
int | float |
Larger value |
clamp(v: int | float, lo: int | float, hi: int | float) |
int | float |
Clamp v into [lo, hi] |
math.abs(-4) # 4math.sign(-2.5) # -1math.min(3, 8) # 3math.clamp(15, 0, 10) # 10Random
Section titled “Random”| Function | Returns | Description |
|---|---|---|
random() |
float |
Float in [0.0, 1.0) |
randint(a: int, b: int) |
int |
Int in [a, b], both ends inclusive; requires a <= b |
randfloat(lo: int | float, hi: int | float) |
float |
Float in [lo, hi) |
rand_seed(seed: int) |
none |
Reseed the generator; makes a sequence reproducible |
rand_choice(arr: array) |
any |
A random element; raises on an empty array |
rand_shuffle(arr: array) |
none |
Shuffle the array in place with Fisher-Yates |
let math = import("math")
math.rand_seed(42)print(math.random()) # 0.0 <= n < 1.0print(math.randint(1, 6)) # die roll, 1..6print(math.randfloat(0.0, 1.0)) # 0.0 <= n < 1.0
let deck = [1, 2, 3, 4, 5]math.rand_shuffle(deck)print(math.rand_choice(deck))The generator is a per-VM xoshiro256** PRNG. It is fast and deterministic under rand_seed, but not cryptographically secure: use crypto.random for secrets and tokens.
- Pyle has no
**operator. Usemath.pow(base, exp). - Division and modulo by zero raise a runtime error. There is no
infornanin the language. - Integer arithmetic wraps on overflow; use floats when you need a wider range.
math.floor,math.ceil,math.round,math.trunc, andmath.signreturn ints. The rest return floats unless the input type dictates otherwise.
Related
Section titled “Related”- Operators for arithmetic and bitwise behavior.
- Types & Values for numeric types.