Skip to content

Commit

Permalink
Rollup merge of rust-lang#96129 - mattheww:2022-04_float_rounding, r=…
Browse files Browse the repository at this point in the history
…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]: rust-lang#41753 (comment)
[^2]: llvm/llvm-project#8472 (comment)

This PR makes a start by documenting that for primitive operations and `from_str()`.
  • Loading branch information
Dylan-DPC authored May 23, 2022
2 parents 32c8c5d + 16c81fa commit 98a8035
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
6 changes: 4 additions & 2 deletions library/core/src/num/dec2flt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions library/core/src/primitive_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).*
Expand Down
13 changes: 13 additions & 0 deletions library/std/src/primitive_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).*
Expand Down

0 comments on commit 98a8035

Please sign in to comment.