Math
Module math
import math
| Module | math |
| Source | math/math.zirr, math/module-docs.zirr |
Arithmetic that the operators do not cover.
math operates on prelude.Number, the union of prelude.Int and prelude.Float. Functions that can only answer in fractions (sqrt, floor, pow) return Float; the ones that merely pick or transform a value (abs, min, max, clamp) return whichever kind they were given.
Conversions are never implicit — use toInt and toFloat when you need to cross between them.
Contents
- Functions —
abs,ceil,clamp,floor,isInfinite,isNaN,max,min,pow,round,sign,sqrt,toFloat,toInt,trunc - Constants —
e,pi
Functions
abs
`math/math.zirr:16`
extern fn abs(v: Number) -> Number
Returns v without its sign, as the same type it was given.
ceil
`math/math.zirr:38`
extern fn ceil(v: Number) -> Float
Returns the least whole number not below v.
clamp
`math/math.zirr:53`
fn clamp(v: Number, low: Number, high: Number) -> Number
Returns v limited to the range from low to high, as the type it was given.
floor
`math/math.zirr:35`
extern fn floor(v: Number) -> Float
Returns the greatest whole number not above v.
isInfinite
`math/math.zirr:29`
extern fn isInfinite(v: Number) -> Bool
Returns whether v is positive or negative infinity, as produced by dividing a non-zero Float by zero. An Int is never infinite, and NaN is not infinite either.
isNaN
`math/math.zirr:26`
extern fn isNaN(v: Number) -> Bool
Returns whether v is not a number, as produced by an undefined operation such as 0.0 / 0.0 or the square root of a negative. An Int is never NaN.
NaN is equal to nothing, itself included, so this is the only way to detect it.
max
`math/math.zirr:22`
extern fn max(a: Number, b: Number) -> Number
Returns whichever of a and b is larger, as the type it was given.
min
`math/math.zirr:19`
extern fn min(a: Number, b: Number) -> Number
Returns whichever of a and b is smaller, as the type it was given.
pow
`math/math.zirr:50`
extern fn pow(base: Number, exponent: Number) -> Float
Returns base raised to the power of exponent.
round
`math/math.zirr:41`
extern fn round(v: Number) -> Float
Returns v rounded to the nearest whole number, halves away from zero.
sign
`math/math.zirr:32`
extern fn sign(v: Number) -> Int
Returns -1, 0 or 1 according to whether v is negative, zero or positive.
sqrt
`math/math.zirr:47`
extern fn sqrt(v: Number) -> Float
Returns the square root of v.
toFloat
`math/math.zirr:10`
extern fn toFloat(v: Number) -> Float
Returns v as a Float.
toInt
`math/math.zirr:13`
extern fn toInt(v: Number) -> Int
Returns v as an Int, discarding any fractional part rather than rounding.
trunc
`math/math.zirr:44`
extern fn trunc(v: Number) -> Float
Returns v with its fractional part discarded, rounding toward zero.
Constants
e
`math/math.zirr:7`
extern const e: Float
Euler’s number, the base of the natural logarithm.
pi
`math/math.zirr:4`
extern const pi: Float
The ratio of a circle’s circumference to its diameter.