diff options
| author | Dylan DPC <99973273+Dylan-DPC@users.noreply.github.com> | 2022-05-23 15:11:02 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-05-23 15:11:02 +0200 |
| commit | 98a8035bedd2c660912d1e6ecfe86c895e588feb (patch) | |
| tree | f126f37017f2bc9adf59edfda8d0dc35a73413f5 | |
| parent | 32c8c5df06c025441ad04791d7982d65c79a60e4 (diff) | |
| parent | 16c81fa9a6b0e5b56320baa7ca569e024425344e (diff) | |
| download | rust-98a8035bedd2c660912d1e6ecfe86c895e588feb.tar.gz rust-98a8035bedd2c660912d1e6ecfe86c895e588feb.zip | |
Rollup merge of #96129 - mattheww:2022-04_float_rounding, r=Dylan-DPC
Document rounding for floating-point primitive operations and string parsing The docs for floating point don't have much to say at present about either the precision of their results or rounding behaviour. As I understand it[^1][^2], Rust doesn't support operating with non-default rounding directions, so we need only describe roundTiesToEven. [^1]: https://github.com/rust-lang/rust/issues/41753#issuecomment-299322887 [^2]: https://github.com/llvm/llvm-project/issues/8472#issuecomment-980888781 This PR makes a start by documenting that for primitive operations and `from_str()`.
| -rw-r--r-- | library/core/src/num/dec2flt/mod.rs | 6 | ||||
| -rw-r--r-- | library/core/src/primitive_docs.rs | 13 | ||||
| -rw-r--r-- | library/std/src/primitive_docs.rs | 13 |
3 files changed, 30 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<Self, ParseFloatError> { dec2flt(src) diff --git a/library/core/src/primitive_docs.rs b/library/core/src/primitive_docs.rs index ac4e668112b..631cc313fa0 100644 --- a/library/core/src/primitive_docs.rs +++ b/library/core/src/primitive_docs.rs @@ -994,6 +994,19 @@ mod prim_tuple {} /// surprising results upon inspecting the bit patterns, /// as the same calculations might produce NaNs with different bit patterns. /// +/// 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<sup>(`f32::MAX_EXP` − +/// `f32::MANTISSA_DIGITS` − 1)</sup>, 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 ac4e668112b..631cc313fa0 100644 --- a/library/std/src/primitive_docs.rs +++ b/library/std/src/primitive_docs.rs @@ -994,6 +994,19 @@ mod prim_tuple {} /// surprising results upon inspecting the bit patterns, /// as the same calculations might produce NaNs with different bit patterns. /// +/// 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<sup>(`f32::MAX_EXP` − +/// `f32::MANTISSA_DIGITS` − 1)</sup>, 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).* |
