float
import std::fmt::float; · source
Float formatting lives here rather than in core::primitives because it needs libm, and keeping libc out of core matters more. The API is extension methods on the primitives.
f64
implement f64 {
is_negative_zero(&this) -> boolean;
to_buf(&this, buffer: u8*) -> u64;
to_buf_with(&this, buffer: u8*, decimals: u64) -> u64;
to_buf_roundtrip(&this, buffer: u8*) -> u64;
}
const F64_MAX_DIGITS: u64 = 32;
| Method | Notes |
|---|---|
to_buf(buffer) | Fixed-point with six fractional digits. Buffer must hold at least F64_MAX_DIGITS (32) bytes. |
to_buf_with(buffer, decimals) | Caller-chosen precision. |
to_buf_roundtrip(buffer) | The shortest decimal string that re-parses to the identical bit value. f64 only. |
is_negative_zero() |
Values beyond |v| >= 1e16 or below 0 < |v| < 1e-4 switch to scientific notation, so no precision disappears into a giant zero prefix or a run of trailing zeros. NaN, both infinities, and both zeros are handled explicitly — negative zero prints as -0 so the sign is not lost.
to_bufis a first-cut formatter: correct for human-readable output but not round-trippable, since the fixed six decimals silently truncate (3.141592653589793becomes3.141593). Anything that must preserve the value — JSON serialization, notably — should useto_buf_roundtrip, which tries 15, then 16, then 17 significant digits and returns the first that re-parses exactly. Typical values stay compact:0.1, not0.10000000000000001.
f32
implement f32 {
to_buf(&this, buffer: u8*) -> u64;
to_buf_with(&this, buffer: u8*, decimals: u64) -> u64;
is_negative_zero(&this) -> boolean;
}
The same fixed-point writers at single precision. There is no to_buf_roundtrip for f32; widen to f64 for a round-trippable rendering.
parse_f64
function parse_f64(s: string) -> Option<f64>;
parse_f64 is the reverse, with C strtod semantics: skip leading whitespace, read the longest valid float prefix, decimal or scientific, no hex floats. It returns None when there is no numeric prefix. Strip digit separators (_) before calling — strtod stops at them.
Integer formatting is on the primitives themselves in core::primitives, and integer parsing goes through TryFrom<Str> in collections::str.