Home / Docs / Specification / 29. Numerics
29.3 Operations
All operations are total: none fails, none has a precondition, and none generates a proof obligation. Every result is the IEEE 754-2019 result in roundTiesToEven, with a NaN result replaced by the canonical NaN. The rounding mode cannot be changed.
-
Operators.
+,-,*and/on twoF64operands are the IEEE operations, correctly rounded. Unary-is IEEEnegate: it flips the sign bit, exactly.modonF64is U2001, with a repair torem. -
Division by zero gives infinities or NaN, not a failure.
1.0 / 0.0isinfinity(),1.0 / -0.0is-infinity(), and0.0 / 0.0is NaN. AnF64division has nodivisionobligation, and float code runs under any program counter (spec/07 §3). -
Methods. These are built-in methods resolved by the receiver, with labeled arguments in the order written here:
Method Result Semantics x.abs()F64clears the sign bit: (-0.0).abs()is0.0x.sqrt()F64IEEE squareRoot;(-0.0).sqrt()is-0.0; a negative argument gives NaNx.fma(times: y, plus: z)F64IEEE fusedMultiplyAdd:x * y + zrounded oncex.rem(by: y)F64x - y * nwithnthe quotientx / ytruncated toward zero, computed exactly: the result has the sign ofx,(-1.0).rem(by: 1.0)is-0.0, andx.rem(by: 0.0)andinfinity().rem(by: y)are NaNx.floor(),x.ceil(),x.trunc()F64IEEE roundToIntegraltoward -∞, +∞ and 0:(-0.5).ceil()is-0.0x.round_half_even()F64IEEE roundToIntegralTiesToEven:0.5gives0.0,1.5and2.5give2.0,-0.5gives-0.0x.round_half_away()F64IEEE roundToIntegralTiesToAway:2.5gives3.0,-0.5gives-1.0,0.49999999999999994gives0.0x.copy_sign(from: y)F64xwith the sign bit ofyx.ieee_min(other: y),x.ieee_max(other: y)F64IEEE 754-2019 minimumandmaximum: NaN when either is NaN, and-0.0below0.0x.clamp(lo: a, hi: b)F64x.ieee_max(other: a).ieee_min(other: b): NaN when any of the three is NaN,bwhena > bx.ieee_eq(other: y),ieee_ne,ieee_lt,ieee_le,ieee_gt,ieee_geBoolthe IEEE quiet comparison predicates: -0.0equals0.0, NaN is unordered, so every one butieee_neisfalsewhen an operand is NaNx.is_nan(),x.is_finite(),x.is_infinite(),x.is_sign_negative()Boolclassification; nan().is_sign_negative()isfalsex.is_zero(),x.is_positive_zero(),x.is_negative_zero()Boolxis0.0or-0.0;xis0.0;xis-0.0x.to_bits()U64the bits; for a NaN, 0x7FF8000000000000The label of
copy_sign's argument is the keywordfrom, which a call's argument may take as its label in edition0.3, and nothing else may (spec/02). A rounding method keeps NaN, the infinities and the sign of a zero. A method name that is not in this table, on anF64receiver, resolves as a function of the package (spec/10 §5). -
No
round(). There is no method namedround: the name says which tie rule runs. -
minandmaxof the prelude order by §5:min(-0.0, b: 0.0)is-0.0,max(nan(), b: 1.0)is NaN, andmin(nan(), b: 1.0)is1.0, since NaN is the greatestF64. A clamp written with them,min(max(x, b: lo), b: hi), maps NaN tohi;x.clamp(lo:, hi:)keeps NaN. -
absof the prelude staysabs(x: Int) -> Int:abs(x)with anF64is U2001;x.abs()is the method. -
No transcendental functions yet:
exp,log,sin,cos,tan,powand the others wait for a correctly rounded implementation (ADR 0114 rule 18). NoF64function is ever computed by the platform's math library (§6). -
No floating-point environment. There are no exception flags, no traps and no rounding modes in the language.
-
The prelude gains:
Function Type Meaning nan()F64the canonical NaN infinity()F64the positive infinity f64_from_bits(bits: U64)F64the value with those bits; any NaN gives the canonical NaN parse_f64(t: Text)F64 fails ParseError§7 rule 3 as_f64(j: Json)Option[F64]§10 json_number(x: F64)Option[Json]§10 A package declaration MAY reuse one of these names and wins over it, as for any prelude name (spec/10 §11 rule 5).