Skip to content

Commit 66175a7

Browse files
committed
clarify non-determinism docs for algebraic operations
1 parent 7c329d6 commit 66175a7

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

library/core/src/primitive_docs.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,10 +1333,12 @@ mod prim_f16 {}
13331333
/// such as NaN, +/-Inf, or -0.0 may behave in unexpected ways, but these operations
13341334
/// will never cause undefined behavior.
13351335
///
1336-
/// Because of the unpredictable nature of compiler optimizations, the same inputs may produce
1337-
/// different results even within a single program run. **Unsafe code must not rely on any property
1338-
/// of the return value for soundness.** However, implementations will generally do their best to
1339-
/// pick a reasonable tradeoff between performance and accuracy of the result.
1336+
/// Algebraic operations are non-deterministic. This means that two invocations of such an operation
1337+
/// with the same inputs may produce different results even within a single program run. No
1338+
/// guarantees are made about the results of individual operations, except that they produce *some*
1339+
/// valid floating-point value. **Unsafe code must not rely on any property of the return value for
1340+
/// soundness.** However, implementations will generally do their best to pick a reasonable tradeoff
1341+
/// between performance and accuracy of the result.
13401342
///
13411343
/// For example:
13421344
///
@@ -1362,6 +1364,21 @@ mod prim_f16 {}
13621364
/// x = ((a + b) + c) + d; // As written
13631365
/// x = (a + c) + (b + d); // Reordered to shorten critical path and enable vectorization
13641366
/// ```
1367+
///
1368+
/// The following example demonstrates the non-determinism:
1369+
///
1370+
/// ```
1371+
/// # #![allow(unused_assignments)]
1372+
/// # let a: f32 = 1.0;
1373+
/// # let b: f32 = 2.0;
1374+
/// let x1 = a.algebraic_add(b);
1375+
/// let x2 = a.algebraic_add(b);
1376+
/// assert_eq!(x1.to_bits(), x1.to_bits()); // this is guaranteed
1377+
/// # if false {
1378+
/// assert_eq!(x1.to_bits(), x2.to_bits()); // but this may fail
1379+
/// assert!(!x2.is_nan()); // this may also fail, even if there was no NaN input
1380+
/// # }
1381+
/// ```
13651382
#[stable(feature = "rust1", since = "1.0.0")]
13661383
mod prim_f32 {}
13671384

0 commit comments

Comments
 (0)