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.
Exports
Section titled “Exports”Types
type Float
Section titled “type Float”type Float
64-bit floating point number.
Float.nan
Section titled “Float.nan”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.
Float.positive_infinity
Section titled “Float.positive_infinity”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.
Float.negative_infinity
Section titled “Float.negative_infinity”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.
Float.nan?
Section titled “Float.nan?”fn nan?(x: Float): Bool
type function on Float
True if x is NaN.
Float.infinite?
Section titled “Float.infinite?”fn infinite?(x: Float): Bool
type function on Float
True if x is +Infinity or -Infinity.
Float.finite?
Section titled “Float.finite?”fn finite?(x: Float): Bool
type function on Float
True if x is a finite real number — not NaN, not ±Infinity.
Float.to_int
Section titled “Float.to_int”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)
Float.round
Section titled “Float.round”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
Float.floor
Section titled “Float.floor”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
Float.ceil
Section titled “Float.ceil”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
Float.trunc
Section titled “Float.trunc”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
Float.to_string
Section titled “Float.to_string”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"
Float.add
Section titled “Float.add”impl Add
fn add(lhs: Float, rhs: Float): Float
impl Add.add
Float.subtract
Section titled “Float.subtract”impl Subtract
fn subtract(lhs: Float, rhs: Float): Float
impl Subtract.subtract
Float.multiply
Section titled “Float.multiply”impl Multiply
fn multiply(lhs: Float, rhs: Float): Float
impl Multiply.multiply
Float.divide
Section titled “Float.divide”impl Divide
fn divide(lhs: Float, rhs: Float): Float
impl Divide.divide
Float.inspect
Section titled “Float.inspect”impl Debug
fn inspect(x: Float): String
impl Debug.inspect
Float.equal?
Section titled “Float.equal?”impl Equatable
fn equal?(a: Float, b: Float): Bool
impl Equatable.equal?
Float.hash
Section titled “Float.hash”impl Hashable
fn hash(x: Float): Int
impl Hashable.hash
Float.compare
Section titled “Float.compare”impl Comparable
fn compare(a: Float, b: Float): Ordering
impl Comparable.compare