int
Functions for parsing, converting, and comparing integers.
Summary
| Function | Signature | Description |
|---|---|---|
abs | (Int) -> Int | Absolute value |
clamp | (Int, Int, Int) -> Int | Clamp value to [lo, hi] |
max | (Int, Int) -> Int | Larger of two values |
min | (Int, Int) -> Int | Smaller of two values |
parse | (String) -> Result(Int, IntError) | Parse string to integer |
to_float | (Int) -> Float | Convert to float |
int.abs
int.abs(n: Int) -> Int
Returns the absolute value. Runtime error if n is Int minimum
(-9223372036854775808) since the result cannot be represented.
import int
fn main() {
println(int.abs(-42)) -- 42
println(int.abs(7)) -- 7
}open in playgroundint.clamp
int.clamp(x: Int, lo: Int, hi: Int) -> Int
Returns x constrained to the inclusive range [lo, hi]: lo if
x < lo, hi if x > hi, otherwise x. Runtime error if lo > hi
(invalid bounds).
import int
fn main() {
println(int.clamp(5, 0, 10)) -- 5
println(int.clamp(-3, 0, 10)) -- 0
println(int.clamp(42, 0, 10)) -- 10
}open in playgroundint.max
int.max(a: Int, b: Int) -> Int
Returns the larger of two integers.
import int
fn main() {
println(int.max(3, 7)) -- 7
}open in playgroundint.min
int.min(a: Int, b: Int) -> Int
Returns the smaller of two integers.
import int
fn main() {
println(int.min(3, 7)) -- 3
}open in playgroundint.parse
int.parse(s: String) -> Result(Int, IntError)
Parses a string as an integer. Leading/trailing whitespace is trimmed. Returns
Ok(n) on success, Err(IntError) on failure — a typed enum with variants
Empty, InvalidDigit(offset), Overflow, Underflow and Other(message).
Match on the variant to handle specific cases, or fall back to e.message() for
a human-readable default.
import int
fn main() {
match int.parse("42") {
Ok(n) -> println(n)
Err(e) -> println("parse error: {e.message()}")
}
-- Pattern-match on specific failure modes:
match int.parse("") {
Ok(_) -> ()
Err(int.Empty) -> println("cannot parse empty input")
Err(int.InvalidDigit(i)) -> println("bad digit at byte {i}")
Err(int.Overflow) -> println("too large")
Err(int.Underflow) -> println("too small")
Err(int.Other(message)) -> println(message)
}
}open in playgroundint.to_float
int.to_float(n: Int) -> Float
Converts an integer to a float.
import int
fn main() {
let f = int.to_float(42)
println(f) -- 42.0
}open in playgroundfloat
Functions for parsing, rounding, converting, and comparing floats.
Floats are always finite: a
Floatis never NaN or infinite. An operation whose result would be NaN or infinite raises a runtime error instead, the wayIntoverflow does:x / 0.0raises “float division by zero”,+,-and*raise “float overflow”, andmath.sqrtof a negative number,math.logof a number that is not positive, andmath.powoverflow raise too. Guard the input when it can be out of range, for example with amatchon the divisor that has an arm for0.0.
Note:
round,ceil, andfloorreturnFloat, notInt. Usefloat.to_intto convert the result to an integer.
How a float is shown:
println,"{f}"andstring.from(f)write aFloatas its digits with a decimal point and no exponent, so a whole one ends in.0(3.0, never3);float.format(f, decimals)writes exactly the decimal places asked for, which may be none.
Summary
| Function | Signature | Description |
|---|---|---|
abs | (Float) -> Float | Absolute value |
ceil | (Float) -> Float | Round up to nearest integer (as Float) |
clamp | (Float, Float, Float) -> Float | Clamp value to [lo, hi] |
floor | (Float) -> Float | Round down to nearest integer (as Float) |
format | (Float, Int) -> String | Decimal text with that many decimal places |
max | (Float, Float) -> Float | Larger of two values |
min | (Float, Float) -> Float | Smaller of two values |
parse | (String) -> Result(Float, FloatError) | Parse string to float |
round | (Float) -> Float | Round to nearest integer (as Float) |
to_int | (Float) -> Int | Truncate to integer |
| Constants | ||
float.max_value | Float | Maximum finite value (1.7976931348623157e+308) |
float.min_value | Float | Minimum finite value (-1.7976931348623157e+308) |
float.epsilon | Float | Machine epsilon (2.220446049250313e-16) |
float.min_positive | Float | Smallest positive normal (2.2250738585072014e-308) |
float.abs
float.abs(f: Float) -> Float
Returns the absolute value.
import float
fn main() {
println(float.abs(-3.14)) -- 3.14
}open in playgroundfloat.ceil
float.ceil(f: Float) -> Float
Rounds up to the nearest integer, returned as a Float.
import float
fn main() {
println(float.ceil(3.2)) -- 4.0
println(float.ceil(-3.2)) -- -3.0
}open in playgroundfloat.clamp
float.clamp(x: Float, lo: Float, hi: Float) -> Float
Returns x constrained to the inclusive range [lo, hi]: lo if
x < lo, hi if x > hi, otherwise x. Runtime error if lo > hi
(invalid bounds).
import float
fn main() {
println(float.clamp(0.5, 0.0, 1.0)) -- 0.5
println(float.clamp(-0.2, 0.0, 1.0)) -- 0.0
println(float.clamp(1.5, 0.0, 1.0)) -- 1.0
}open in playgroundfloat.floor
float.floor(f: Float) -> Float
Rounds down to the nearest integer, returned as a Float.
import float
fn main() {
println(float.floor(3.9)) -- 3.0
println(float.floor(-3.2)) -- -4.0
}open in playgroundfloat.format
float.format(f: Float, decimals: Int) -> String
The decimal text of a float with exactly decimals decimal places, rounded
to the nearest. A number that rounds to zero is written without a sign.
decimals must not be negative. For the text a float is shown as (in
println, in "{f}"), use string.from(f).
import float
fn main() {
println(float.format(3.14159, 2)) -- 3.14
println(float.format(42.0, 1)) -- 42.0
println(float.format(2.5, 0)) -- 2
println(float.format(-0.04, 1)) -- 0.0
}open in playgroundfloat.max
float.max(a: Float, b: Float) -> Float
Returns the larger of two floats.
import float
fn main() {
println(float.max(1.5, 2.5)) -- 2.5
}open in playgroundfloat.min
float.min(a: Float, b: Float) -> Float
Returns the smaller of two floats.
import float
fn main() {
println(float.min(1.5, 2.5)) -- 1.5
}open in playgroundfloat.parse
float.parse(s: String) -> Result(Float, FloatError)
Parses a string as a float. Leading/trailing whitespace is trimmed. Returns
Ok(f) on success, Err(FloatError) on failure — an enum of its own with
the variants int.parse has (Empty, InvalidDigit(offset), Overflow,
Underflow, Other(message)). A Float is always finite, so "NaN" and "inf" are
rejected as InvalidDigit(0), and a literal outside the Float range
as Overflow ("1e400") or Underflow ("-1e400").
import float
fn main() {
match float.parse("3.14") {
Ok(f) -> println(f)
Err(e) -> println("error: {e.message()}")
}
}open in playgroundfloat.round
float.round(f: Float) -> Float
Rounds to the nearest integer, returned as a Float. Ties round away from zero.
import float
fn main() {
println(float.round(3.6)) -- 4.0
println(float.round(3.4)) -- 3.0
}open in playgroundfloat.to_int
float.to_int(f: Float) -> Int
Truncates toward zero, converting to an integer. Raises a runtime error if
the value is outside the Int range.
import float
fn main() {
println(float.to_int(3.9)) -- 3
println(float.to_int(-3.9)) -- -3
}open in playgroundFloat Constants
| Constant | Type | Value |
|---|---|---|
float.max_value | Float | 1.7976931348623157e+308 |
float.min_value | Float | -1.7976931348623157e+308 |
float.epsilon | Float | 2.220446049250313e-16 |
float.min_positive | Float | 2.2250738585072014e-308 |