Skip to content

Commit 6fa061c

Browse files
committed
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.
1 parent 2fa9789 commit 6fa061c

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

library/core/src/primitive_docs.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,19 @@ mod prim_tuple {}
982982
/// it is considered infectious as almost all calculations where one of the
983983
/// operands is NaN will also result in NaN.
984984
///
985+
/// When the number resulting from a primitive operation (addition,
986+
/// subtraction, multiplication, or division) on this type is not exactly
987+
/// representable as `f32`, it is rounded according to the roundTiesToEven
988+
/// direction defined in IEEE 754-2008. That means:
989+
///
990+
/// - The result is the representable value closest to the true value, if there
991+
/// is a unique closest representable value.
992+
/// - If the true value is exactly half-way between two representable values,
993+
/// the result is the one with an even least-significant binary digit.
994+
/// - If the true value's magnitude is ≥ `f32::MAX` + 2<sup>(`f32::MAX_EXP` −
995+
/// `f32::MANTISSA_DIGITS` − 1)</sup>, the result is ∞ or −∞ (preserving the
996+
/// true value's sign).
997+
///
985998
/// For more information on floating point numbers, see [Wikipedia][wikipedia].
986999
///
9871000
/// *[See also the `std::f32::consts` module](crate::f32::consts).*

library/std/src/primitive_docs.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,19 @@ mod prim_tuple {}
982982
/// it is considered infectious as almost all calculations where one of the
983983
/// operands is NaN will also result in NaN.
984984
///
985+
/// When the number resulting from a primitive operation (addition,
986+
/// subtraction, multiplication, or division) on this type is not exactly
987+
/// representable as `f32`, it is rounded according to the roundTiesToEven
988+
/// direction defined in IEEE 754-2008. That means:
989+
///
990+
/// - The result is the representable value closest to the true value, if there
991+
/// is a unique closest representable value.
992+
/// - If the true value is exactly half-way between two representable values,
993+
/// the result is the one with an even least-significant binary digit.
994+
/// - If the true value's magnitude is ≥ `f32::MAX` + 2<sup>(`f32::MAX_EXP` −
995+
/// `f32::MANTISSA_DIGITS` − 1)</sup>, the result is ∞ or −∞ (preserving the
996+
/// true value's sign).
997+
///
985998
/// For more information on floating point numbers, see [Wikipedia][wikipedia].
986999
///
9871000
/// *[See also the `std::f32::consts` module](crate::f32::consts).*

0 commit comments

Comments
 (0)