0177:
One floating-point type, f64, with IEEE 754
semantics
Status: Proposed (pending maintainer)
Context
Harness has only integers (ADR-0012, ADR-0044). Measurements, ratios and statistics in CLI tools need fractions. The language forbids implicit conversions, keeps left-to-right evaluation and requires the interpreter and the C backend to agree byte for byte (ADR-0003).
Decision
- One type
f64: IEEE 754 binary64, round-to-nearest-even. Nof32. - Literals are decimal with a fraction and an optional exponent:
1.5,2.0e-3,1.0E10. No suffixes, no hexadecimal floats, no1.,.5or1e5(E0006, with a fix spelling the accepted form). A literal that rounds to infinity is E0007. Literals never take an integer type and integer literals never becomef64:let x: f64 = 1is a mismatch whose fix is1.0. - No implicit int/float conversion.
f64(x)converts any integer (round to nearest) or f64.i64(x)truncates toward zero and trapsfloat conversion out of rangewhen the result is NaN, infinite or outside i64; other integer conversions take integers only (u8(i64(x))).x.saturating_i64()never traps: NaN gives 0, out-of-range values clamp. - Operators:
+ - * /, unary-,== != < <= > >=. No%. Arithmetic never traps: division by zero gives ±inf or NaN; overflow gives ±inf. - Equality and ordering are IEEE: NaN is unequal to everything
including itself and every ordering with NaN is false;
-0.0 == 0.0. Records, unions, vectors andcontainscompare f64 fields with the same==, so a value holding NaN is not equal to itself.sort_bytakes the user's comparator. - Methods:
abs floor ceil round trunc sqrt is_nan is_finite min max saturating_i64.roundrounds half away from zero.min/maxfollow IEEE 754-2019minimum/maximum: NaN propagates and-0.0 < 0.0, so the result never depends on argument order or the C library. f64is not a map key (equality is not an equivalence relation), and is rejected wherever key types are checked.- Printing, interpolation and
parse_f64are defined in ADR-0178.
Consequences
f64 joins the printable scalars. Range proofs
(ADR-0066) ignore floats; only i64(x) adds a value
check, so checkfree rejects it. Because NaN breaks
reflexivity, generic code that relies on x == x must
not assume it for f64.
Alternatives
f32andf64: doubles the surface for no current consumer.- Trapping on NaN/inf: deviates from every libm and hardware; tools that compute ratios would need guards on every division.
- Total order (
-0.0 < 0.0, NaN equal to NaN): makes==disagree with C and IEEE and surprises users; a latertotal_cmpmethod can add it. - Implicit integer-literal promotion
(
let x: f64 = 1): convenient, but the language has no implicit conversions and literals stay unambiguous.