typestar

The math library in Lua

floor and ceil, // and %, and one number type in two flavors.

-- The math library, and Lua's one number type wearing two faces.
print(math.floor(7.8), math.ceil(7.2))
print(math.max(12, 45, 3), math.min(12, 45, 3))
print("Root of 2: " .. string.format("%.4f", math.sqrt(2)))

-- Integers and floats coexist; // divides toward the floor.
print(7 / 2)
print(7 // 2)
print(7 % 3)
print(math.type(7), math.type(7.0))

-- Deterministic rolls come from a seeded generator.
math.randomseed(20)
print("Seeded roll: " .. math.random(1, 6))

How it works

  1. math.floor/ceil round; max/min take any count.
  2. // floor-divides; math.type tells integer from float.
  3. math.randomseed makes rolls reproducible.

Keywords and builtins used here

The run, in numbers

Lines
14
Characters to type
468
Tokens
91
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 84 seconds.

Type this snippet

Step 1 of 2 in The library, step 23 of 27 in Language basics.

← Previous Next →