nylon.math
nylon.math has the arithmetic a reading needs. A program imports the namespace, and then uses each function by its
name:
import namespace nylon.math
let side = sqrt(2.0)
println(clamp(12, 0, 10))
Output
10
Whole numbers and floats
Each of these works on any integer or float, and gives back the same type.
| Function | What it gives |
|---|---|
abs(x) | x without its sign |
min(a, b) | The smaller of the two |
max(a, b) | The larger of the two |
clamp(x, low, high) | x, kept between low and high |
import namespace nylon.math
println(abs(-3))
println(min(3, 7))
println(max(i64(3), i64(7)))
println(clamp(12, 0, 10))
Output
3
3
7
10
Floats
Each of these takes an f32 or an f64, and gives back the same type.
| Function | What it gives |
|---|---|
sqrt(x) | The square root |
floor(x) | The largest whole number not above x |
ceil(x) | The smallest whole number not below x |
round(x) | The nearest whole number, a half rounding away from zero: round(2.5) is 3 |
A result is still a float: floor(2.7) is 2.0. To get an integer, convert it, as i32(round(x)) does.
import namespace nylon.math
let root = sqrt(2.0)
let down = floor(2.7)
let up = ceil(2.2)
println(i32(round(2.5)))
Output
3
Angles
Angles are in radians, with PI to write one.
| Name | What it gives |
|---|---|
sin(x), cos(x), tan(x) | The sine, cosine and tangent of x radians |
atan2(y, x) | The angle of the point (x, y), from -PI to PI |
PI | π, as an f64 |
E | e, as an f64 |
import namespace nylon.math
let half = sin(PI / 6.0)
let heading = atan2(1.0, 1.0)