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.
Types
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;
}
to_buf writes fixed-point text with six fractional digits into a buffer of at least F64_MAX_DIGITS (32) bytes, to_buf_with takes the precision, and to_buf_roundtrip — f64 only — writes the shortest decimal string that re-parses to the identical bit value.
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.
Methods
is_negative_zero(&this) -> boolean;
is_negative_zero(&this) -> boolean;IEEE 754: -0.0 has bit pattern 0x8000_0000_0000_0000. The cheap check is 1.0 / v == -Infinity, but we'd rather not divide; the direct comparison relies on value == 0.0 being true for both +/-0 while the sign bit still drives the output branch.
to_buf(&this, buffer: u8*) -> u64;
to_buf(&this, buffer: u8*) -> u64;Write this into buffer. Returns bytes written. buffer must hold at least F64_MAX_DIGITS bytes.
to_buf_with(&this, buffer: u8*, decimals: u64) -> u64;
to_buf_with(&this, buffer: u8*, decimals: u64) -> u64;Variant with caller-chosen precision. decimals applies to the fixed-point form and to the mantissa of the scientific form.
to_buf_roundtrip(&this, buffer: u8*) -> u64;
to_buf_roundtrip(&this, buffer: u8*) -> u64;Write this as the SHORTEST decimal string that re-parses (via strtod) to the identical f64 bit value - i.e. a round-trippable representation. Tries 15, then 16, then 17 significant digits (every finite double round-trips at 17) and returns the first that reparses exactly, so typical values stay compact (0.1, not the noisy 0.10000000000000001). Returns bytes written (excluding the NUL).
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.
Methods
to_buf(&this, buffer: u8*) -> u64;
to_buf_with(&this, buffer: u8*, decimals: u64) -> u64;
is_negative_zero(&this) -> boolean;
Free Functions
function parse_f64(s: string) -> Option<f64>;
function parse_f64(s: string) -> Option<f64>;Parse a leading f64 from s using C strtod semantics: skip leading whitespace, read the longest valid float prefix (decimal or scientific; no hex floats). Returns None when s has no numeric prefix. The single funnel every float-from-text caller routes through, replacing scattered sscanf("%lf"). Caller-supplied strings must already be cleaned of digit separators (_), which strtod - like sscanf - would stop at.
Integer formatting is on the primitives themselves in core::primitives, and integer parsing goes through TryFrom<Str> in collections::str.
Constants
const DEFAULT_DECIMALS: u64 = 6;
const LARGE_THRESHOLD: f64 = 1.0e16;
const SMALL_THRESHOLD: f64 = 1.0e-4;
const F64_MAX_DIGITS: u64 = 32;implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
f64::is_negative_zero(&this) -> booleanIEEE 754: -0.0 has bit pattern 0x8000_0000_0000_0000. The cheap check is 1.0 / v == -Infinity, but we'd rather not divide; the direct comparison relies on value == 0.0 being true for both +/-0 while the sign bit still drives the output branch.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
f64::to_buf(&this, buffer: u8*) -> u64Write this into buffer. Returns bytes written. buffer must hold at least F64_MAX_DIGITS bytes.
(parameter) buffer: u8*f64::to_buf_with(&this, buffer: u8*, decimals: u64) -> u64Variant with caller-chosen precision. decimals applies to the fixed-point form and to the mantissa of the scientific form.
(parameter) decimals: u64f64::to_buf_roundtrip(&this, buffer: u8*) -> u64Write this as the SHORTEST decimal string that re-parses (via strtod) to the identical f64 bit value - i.e. a round-trippable representation.
f32::to_buf(&this, buffer: u8*) -> u64f32::to_buf_with(&this, buffer: u8*, decimals: u64) -> u64f32::is_negative_zero(&this) -> booleanconst NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const DEFAULT_DECIMALS: u64 = 6;const LARGE_THRESHOLD: f64 = 1.0e16;const SMALL_THRESHOLD: f64 = 1.0e-4;const F64_MAX_DIGITS: u64 = 32;import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
const F64_MAX_DIGITS: u64 = 32;function name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
function parse_f64(s: string) -> Option<f64>Parse a leading f64 from s using C strtod semantics: skip leading whitespace, read the longest valid float prefix (decimal or scientific; no hex floats). Returns None when s has no numeric prefix. The single funnel every float-from-text caller routes through, replacing scattered sscanf("%lf").
(parameter) s: stringprimitive stringNUL-terminated raw string (u8*), matching the C ABI. Length-typed text is Str / String.
Option::Noneextern "C" function sscanf(s: u8*, fmt: u8*, ...) -> i32type trait TryFrom<T> {
static try_from(value: T) -> Result<This, ConversionError>;
}type struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::str