From 6fa061c5f96120e631678e3f449566f786397014 Mon Sep 17 00:00:00 2001 From: Matthew Woodcraft Date: Sat, 16 Apr 2022 21:58:36 +0100 Subject: [PATCH 1/2] Document rounding for floating-point primitive operations State that the four primitive operations honour IEEE 754 roundTiesToEven. Documenting under "Primitive Type f32"; f64 refers to that. --- library/core/src/primitive_docs.rs | 13 +++++++++++++ library/std/src/primitive_docs.rs | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/library/core/src/primitive_docs.rs b/library/core/src/primitive_docs.rs index 225a679efd2..2d4d773dcad 100644 --- a/library/core/src/primitive_docs.rs +++ b/library/core/src/primitive_docs.rs @@ -982,6 +982,19 @@ mod prim_tuple {} /// it is considered infectious as almost all calculations where one of the /// operands is NaN will also result in NaN. /// +/// When the number resulting from a primitive operation (addition, +/// subtraction, multiplication, or division) on this type is not exactly +/// representable as `f32`, it is rounded according to the roundTiesToEven +/// direction defined in IEEE 754-2008. That means: +/// +/// - The result is the representable value closest to the true value, if there +/// is a unique closest representable value. +/// - If the true value is exactly half-way between two representable values, +/// the result is the one with an even least-significant binary digit. +/// - If the true value's magnitude is ≥ `f32::MAX` + 2(`f32::MAX_EXP` − +/// `f32::MANTISSA_DIGITS` − 1), the result is ∞ or −∞ (preserving the +/// true value's sign). +/// /// For more information on floating point numbers, see [Wikipedia][wikipedia]. /// /// *[See also the `std::f32::consts` module](crate::f32::consts).* diff --git a/library/std/src/primitive_docs.rs b/library/std/src/primitive_docs.rs index 225a679efd2..2d4d773dcad 100644 --- a/library/std/src/primitive_docs.rs +++ b/library/std/src/primitive_docs.rs @@ -982,6 +982,19 @@ mod prim_tuple {} /// it is considered infectious as almost all calculations where one of the /// operands is NaN will also result in NaN. /// +/// When the number resulting from a primitive operation (addition, +/// subtraction, multiplication, or division) on this type is not exactly +/// representable as `f32`, it is rounded according to the roundTiesToEven +/// direction defined in IEEE 754-2008. That means: +/// +/// - The result is the representable value closest to the true value, if there +/// is a unique closest representable value. +/// - If the true value is exactly half-way between two representable values, +/// the result is the one with an even least-significant binary digit. +/// - If the true value's magnitude is ≥ `f32::MAX` + 2(`f32::MAX_EXP` − +/// `f32::MANTISSA_DIGITS` − 1), the result is ∞ or −∞ (preserving the +/// true value's sign). +/// /// For more information on floating point numbers, see [Wikipedia][wikipedia]. /// /// *[See also the `std::f32::consts` module](crate::f32::consts).* From 16c81fa9a6b0e5b56320baa7ca569e024425344e Mon Sep 17 00:00:00 2001 From: Matthew Woodcraft Date: Sat, 16 Apr 2022 22:00:37 +0100 Subject: [PATCH 2/2] Document the numeric value returned by string parsing for floats --- library/core/src/num/dec2flt/mod.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/core/src/num/dec2flt/mod.rs b/library/core/src/num/dec2flt/mod.rs index 541adb69b8e..df0e7431f1f 100644 --- a/library/core/src/num/dec2flt/mod.rs +++ b/library/core/src/num/dec2flt/mod.rs @@ -143,8 +143,10 @@ macro_rules! from_str_float_impl { /// # Return value /// /// `Err(ParseFloatError)` if the string did not represent a valid - /// number. Otherwise, `Ok(n)` where `n` is the floating-point - /// number represented by `src`. + /// number. Otherwise, `Ok(n)` where `n` is the closest + /// representable floating-point number to the number represented + /// by `src` (following the same rules for rounding as for the + /// results of primitive operations). #[inline] fn from_str(src: &str) -> Result { dec2flt(src)