int

Functions for parsing, converting, and comparing integers.

Summary

FunctionSignatureDescription
abs(Int) -> IntAbsolute value
clamp(Int, Int, Int) -> IntClamp value to [lo, hi]
max(Int, Int) -> IntLarger of two values
min(Int, Int) -> IntSmaller of two values
parse(String) -> Result(Int, IntError)Parse string to integer
to_float(Int) -> FloatConvert 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 playground

int.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 playground

int.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 playground

int.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 playground

int.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 playground

int.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 playground

float

Functions for parsing, rounding, converting, and comparing floats.

Floats are always finite: a Float is never NaN or infinite. An operation whose result would be NaN or infinite raises a runtime error instead, the way Int overflow does: x / 0.0 raises “float division by zero”, +, - and * raise “float overflow”, and math.sqrt of a negative number, math.log of a number that is not positive, and math.pow overflow raise too. Guard the input when it can be out of range, for example with a match on the divisor that has an arm for 0.0.

Note: round, ceil, and floor return Float, not Int. Use float.to_int to convert the result to an integer.

How a float is shown: println, "{f}" and string.from(f) write a Float as its digits with a decimal point and no exponent, so a whole one ends in .0 (3.0, never 3); float.format(f, decimals) writes exactly the decimal places asked for, which may be none.

Summary

FunctionSignatureDescription
abs(Float) -> FloatAbsolute value
ceil(Float) -> FloatRound up to nearest integer (as Float)
clamp(Float, Float, Float) -> FloatClamp value to [lo, hi]
floor(Float) -> FloatRound down to nearest integer (as Float)
format(Float, Int) -> StringDecimal text with that many decimal places
max(Float, Float) -> FloatLarger of two values
min(Float, Float) -> FloatSmaller of two values
parse(String) -> Result(Float, FloatError)Parse string to float
round(Float) -> FloatRound to nearest integer (as Float)
to_int(Float) -> IntTruncate to integer
Constants
float.max_valueFloatMaximum finite value (1.7976931348623157e+308)
float.min_valueFloatMinimum finite value (-1.7976931348623157e+308)
float.epsilonFloatMachine epsilon (2.220446049250313e-16)
float.min_positiveFloatSmallest 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 playground

float.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 playground

float.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 playground

float.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 playground

float.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 playground

float.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 playground

float.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 playground

float.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 playground

float.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 playground

float.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 playground

Float Constants

ConstantTypeValue
float.max_valueFloat1.7976931348623157e+308
float.min_valueFloat-1.7976931348623157e+308
float.epsilonFloat2.220446049250313e-16
float.min_positiveFloat2.2250738585072014e-308