Skip to content

Float

fn from_int(n: Int) -> Float

Every Int is exactly representable as a Float.

fn parse(s: String) -> Float fails ParseError

Reads a decimal number in the forms that to_string writes: digits with an optional . and fraction, an optional exponent (1e+21), an optional leading - or +, and Infinity, -Infinity, NaN. Fails with ParseError otherwise (including spaces).

fn to_string(x: Float) -> String

The shortest digits that round-trip. A finite integral value gets a .0 suffix, as in 1.0 ([trait.builtin]).

fn floor(x: Float) -> Int

Rounds toward negative infinity.

fn ceil(x: Float) -> Int

Rounds toward positive infinity.

fn round(x: Float) -> Int

Rounds to the nearest integer, with ties away from zero (2.5 is 3, -2.5 is -3).

fn truncate(x: Float) -> Int

Rounds toward zero.

fn is_nan(x: Float) -> Bool

True only for NaN, the only Float unequal to itself under IEEE comparison.

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

Orders NaN after all other values, with all NaNs comparing Equal. Positive and negative zero compare Equal. Unlike ==, two NaNs compare Equal here.

fn max(a: Float, b: Float) -> Float

Returns the larger value under compare; either NaN makes the result NaN. Returns a on Equal, preserving its sign when the arguments are signed zeros.

fn min(a: Float, b: Float) -> Float

Returns the smaller value under compare; a single NaN selects the other value. Returns a on Equal, including two NaNs or signed zeros.

fn abs(x: Float) -> Float

Absolute value, mapping either signed zero to positive zero and leaving NaN unchanged.