Skip to content

Numbers ​

int is a 64-bit integer, float a 64-bit float. An integer that overflows becomes a float. A float always prints with a point.

mzs
7 / 2  # → 3
7.0 / 2  # → 3.5
-7 % 3  # → 2
2 ** 10  # → 1024
2 ** 0.5  # → 1.4142135623730951
1_000_000 + 0xff  # → 1000255
0.1 + 0.2  # → 0.30000000000000004

For money, use the decimal module.

Conversion ​

FunctionWhat it does
int / floatto an integer (truncated toward zero) / to a float
str(base = 10)a string; base 2–36 for integers only
chrthe character with this code
absabsolute value
pow(n)same as **
mzs
3.7.int  # → 3
(-3.7).int  # → -3
3.float  # → 3.0
2.0.str  # → "2.0"
255.str(16)  # → "ff"
255.str(2)  # → "11111111"
1055.chr  # → "П"
(-5).abs  # → 5
2.pow(-1)  # → 0.5

A negative number as a method receiver goes in parentheses: (-5).abs.

Rounding ​

round rounds half away from zero (not banker's). Without digits the result is an integer, with digits > 0 a float, with a negative one it rounds to tens, hundreds…

FunctionWhat it does
round(digits = 0)to the nearest
ceil(digits = 0) / floor(digits = 0)up / down
clamp(lo, hi)clamps into [lo, hi]
mzs
2.5.round  # → 3
(-2.5).round  # → -3
1.256.round(2)  # → 1.26
1250.round(-2)  # → 1300
2.1.ceil  # → 3
(-2.1).ceil  # → -2
1.238.floor(2)  # → 1.23
5.clamp(1, 3)  # → 3
0.clamp(1, 3)  # → 1

Division and divisors ​

FunctionWhat it does
div(a, b)division rounded down (the pair of %)
sign(x)-1, 0 or 1
gcd(a, b) / lcm(a, b)GCD / LCM
mzs
-7 / 2  # → -3
div(-7, 2)  # → -4
sign(-5)  # → -1
sign(2.5)  # → 1
gcd(12, 18)  # → 6
lcm(4, 6)  # → 12
mzs
div(7, 0)
text
zero-division: divided by 0

Checks ​

FunctionWhat it does
zero / positive / negativezero / above / below zero
even / oddeven / odd (integers only)
is_nan / is_infNaN / infinity
mzs
0.0.zero  # → true
(-1).negative  # → true
4.even  # → true
3.odd  # → true
(0.0 / 0).is_nan  # → true
(1.0 / 0).is_inf  # → true

Loops ​

Without a closure they return an array; with one they iterate and return the number itself.

FunctionGives
n.times0 … n-1
a.upto(b) / a.downto(b)a … b up / down
a.step(limit, by)a, a+by, … while within limit
mzs
3.times  # → [0,1,2]
1.upto(4)  # → [1,2,3,4]
4.downto(1)  # → [4,3,2,1]
0.step(10, 3)  # → [0,3,6,9]
10.step(0, -3)  # → [10,7,4,1]
3.times { print(it) }
println()
text
012

Durations ​

With --time, numbers get seconds minutes hours days weeks, in whole seconds.

mzs
[90.minutes, 2.hours, 1.days]  # → [5400,7200,86400]

Bits ​

There are no bitwise operators, only functions. Right shift is arithmetic; overflow does not turn into a float.

FunctionWhat it does
band(a, b) / bor(a, b) / bxor(a, b)AND / OR / XOR
bnot(a)bit inversion
shl(a, n) / shr(a, n)shift left / right
popcount(a)number of set bits
bit(a, i)whether bit i is set
mzs
band(12, 10)  # → 8
bor(12, 10)  # → 14
bxor(12, 10)  # → 6
bnot(0)  # → -1
shl(1, 4)  # → 16
shr(-8, 1)  # → -4
popcount(255)  # → 8
bit(5, 0)  # → true
12.band(10)  # → 8
mzs
12 & 10
text
use band(a, b)