Float
from_int
Section titled “from_int”fn from_int(n: Int) -> FloatEvery Int is exactly representable as a Float.
fn parse(s: String) -> Float fails ParseErrorReads 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).
to_string
Section titled “to_string”fn to_string(x: Float) -> StringThe shortest digits that round-trip. A finite integral value gets a .0 suffix, as in 1.0
([trait.builtin]).
fn floor(x: Float) -> IntRounds toward negative infinity.
fn ceil(x: Float) -> IntRounds toward positive infinity.
fn round(x: Float) -> IntRounds to the nearest integer, with ties away from zero (2.5 is 3, -2.5 is -3).
truncate
Section titled “truncate”fn truncate(x: Float) -> IntRounds toward zero.
is_nan
Section titled “is_nan”fn is_nan(x: Float) -> BoolTrue only for NaN, the only Float unequal to itself under IEEE comparison.
compare
Section titled “compare”fn compare(a: Float, b: Float) -> OrderingOrders 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) -> FloatReturns 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) -> FloatReturns 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) -> FloatAbsolute value, mapping either signed zero to positive zero and leaving NaN unchanged.