Skip to content

std/float

Operations on the IEEE 754 double-precision Float type: the special values (nan, ±infinity), classification predicates, rounding, and conversion to Int. The arithmetic, Display/Debug/Equatable/Hashable/ Comparable impls give Float a total order and ==-consistent hashing (NaN and signed zero normalized).

Import with import std/float.

Types

type Float

64-bit floating point number.

fn nan(): Float

type function on Float

Returns IEEE 754 NaN (“not a number”). Produced by indeterminate operations like 0.0 / 0.0. NaN propagates through arithmetic and comparisons; use Float.nan? to test for it.

fn positive_infinity(): Float

type function on Float

Returns IEEE 754 positive infinity. Produced by overflow and by dividing a positive Float by 0.0. Use Float.infinite? to test for it.

fn negative_infinity(): Float

type function on Float

Returns IEEE 754 negative infinity. Produced by overflow and by dividing a negative Float by 0.0. Use Float.infinite? to test for it.

fn nan?(x: Float): Bool

type function on Float

True if x is NaN.

fn infinite?(x: Float): Bool

type function on Float

True if x is +Infinity or -Infinity.

fn finite?(x: Float): Bool

type function on Float

True if x is a finite real number — not NaN, not ±Infinity.

fn to_int(x: Float): Maybe<Int>

type function on Float

Converts a Float to an Int by truncating toward zero. Returns None for NaN, ±Infinity, and values outside Int64’s range (~±9.22e18). Use Float.round/floor/ceil first if a different rounding rule is needed.

Interactive Tests

assert Float.to_int(3.7) == Maybe.Some(3)
assert Float.to_int(-3.7) == Maybe.Some(-3)
assert Float.to_int(42.0) == Maybe.Some(42)
fn round(x: Float): Float

type function on Float

Rounds a Float to the nearest integer-valued Float using banker’s rounding (half-to-even). NaN and ±Infinity pass through unchanged.

Interactive Tests

assert Float.round(3.5) == 4.0
assert Float.round(2.5) == 2.0
assert Float.round(3.7) == 4.0
fn floor(x: Float): Float

type function on Float

Rounds a Float toward -∞. NaN and ±Infinity pass through unchanged.

Interactive Tests

assert Float.floor(3.7) == 3.0
assert Float.floor(-3.2) == -4.0
fn ceil(x: Float): Float

type function on Float

Rounds a Float toward +∞. NaN and ±Infinity pass through unchanged.

Interactive Tests

assert Float.ceil(3.2) == 4.0
assert Float.ceil(-3.7) == -3.0
fn trunc(x: Float): Float

type function on Float

Rounds a Float toward zero. NaN and ±Infinity pass through unchanged.

Interactive Tests

assert Float.trunc(3.7) == 3.0
assert Float.trunc(-3.7) == -3.0

impl Display

fn to_string(x: Float): String

impl Display.to_string

Returns the canonical string form of x, matching the runtime stringifier used by IO.print and string interpolation. NaN renders as NaN, ±Infinity as +Inf / -Inf.

Interactive Tests

assert Float.to_string(3.14) == "3.14"
assert Float.to_string(1e20) == "100000000000000000000.0"

impl Add

fn add(lhs: Float, rhs: Float): Float

impl Add.add

impl Subtract

fn subtract(lhs: Float, rhs: Float): Float

impl Subtract.subtract

impl Multiply

fn multiply(lhs: Float, rhs: Float): Float

impl Multiply.multiply

impl Divide

fn divide(lhs: Float, rhs: Float): Float

impl Divide.divide

impl Debug

fn inspect(x: Float): String

impl Debug.inspect

impl Equatable

fn equal?(a: Float, b: Float): Bool

impl Equatable.equal?

impl Hashable

fn hash(x: Float): Int

impl Hashable.hash

impl Comparable

fn compare(a: Float, b: Float): Ordering

impl Comparable.compare