Skip to content

Commit e4933b9

Browse files
authored
Rollup merge of #121861 - tbu-:pr_floating_point_exact_examples, r=workingjubilee
Use the guaranteed precision of a couple of float functions in docs
2 parents 8e11997 + bcccab8 commit e4933b9

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

library/std/src/f32.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,8 @@ impl f32 {
186186
/// let x = 3.5_f32;
187187
/// let y = -3.5_f32;
188188
///
189-
/// let abs_difference_x = (x.abs() - x).abs();
190-
/// let abs_difference_y = (y.abs() - (-y)).abs();
191-
///
192-
/// assert!(abs_difference_x <= f32::EPSILON);
193-
/// assert!(abs_difference_y <= f32::EPSILON);
189+
/// assert_eq!(x.abs(), x);
190+
/// assert_eq!(y.abs(), -y);
194191
///
195192
/// assert!(f32::NAN.abs().is_nan());
196193
/// ```
@@ -276,10 +273,17 @@ impl f32 {
276273
/// let x = 4.0_f32;
277274
/// let b = 60.0_f32;
278275
///
279-
/// // 100.0
280-
/// let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();
276+
/// assert_eq!(m.mul_add(x, b), 100.0);
277+
/// assert_eq!(m * x + b, 100.0);
281278
///
282-
/// assert!(abs_difference <= f32::EPSILON);
279+
/// let one_plus_eps = 1.0_f32 + f32::EPSILON;
280+
/// let one_minus_eps = 1.0_f32 - f32::EPSILON;
281+
/// let minus_one = -1.0_f32;
282+
///
283+
/// // The exact result (1 + eps) * (1 - eps) = 1 - eps * eps.
284+
/// assert_eq!(one_plus_eps.mul_add(one_minus_eps, minus_one), -f32::EPSILON * f32::EPSILON);
285+
/// // Different rounding with the non-fused multiply and add.
286+
/// assert_eq!(one_plus_eps * one_minus_eps + minus_one, 0.0);
283287
/// ```
284288
#[rustc_allow_incoherent_impl]
285289
#[must_use = "method returns a new number and does not mutate the original value"]
@@ -426,9 +430,7 @@ impl f32 {
426430
/// let negative = -4.0_f32;
427431
/// let negative_zero = -0.0_f32;
428432
///
429-
/// let abs_difference = (positive.sqrt() - 2.0).abs();
430-
///
431-
/// assert!(abs_difference <= f32::EPSILON);
433+
/// assert_eq!(positive.sqrt(), 2.0);
432434
/// assert!(negative.sqrt().is_nan());
433435
/// assert!(negative_zero.sqrt() == negative_zero);
434436
/// ```

library/std/src/f64.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,8 @@ impl f64 {
186186
/// let x = 3.5_f64;
187187
/// let y = -3.5_f64;
188188
///
189-
/// let abs_difference_x = (x.abs() - x).abs();
190-
/// let abs_difference_y = (y.abs() - (-y)).abs();
191-
///
192-
/// assert!(abs_difference_x < 1e-10);
193-
/// assert!(abs_difference_y < 1e-10);
189+
/// assert_eq!(x.abs(), x);
190+
/// assert_eq!(y.abs(), -y);
194191
///
195192
/// assert!(f64::NAN.abs().is_nan());
196193
/// ```
@@ -276,10 +273,17 @@ impl f64 {
276273
/// let x = 4.0_f64;
277274
/// let b = 60.0_f64;
278275
///
279-
/// // 100.0
280-
/// let abs_difference = (m.mul_add(x, b) - ((m * x) + b)).abs();
276+
/// assert_eq!(m.mul_add(x, b), 100.0);
277+
/// assert_eq!(m * x + b, 100.0);
281278
///
282-
/// assert!(abs_difference < 1e-10);
279+
/// let one_plus_eps = 1.0_f64 + f64::EPSILON;
280+
/// let one_minus_eps = 1.0_f64 - f64::EPSILON;
281+
/// let minus_one = -1.0_f64;
282+
///
283+
/// // The exact result (1 + eps) * (1 - eps) = 1 - eps * eps.
284+
/// assert_eq!(one_plus_eps.mul_add(one_minus_eps, minus_one), -f64::EPSILON * f64::EPSILON);
285+
/// // Different rounding with the non-fused multiply and add.
286+
/// assert_eq!(one_plus_eps * one_minus_eps + minus_one, 0.0);
283287
/// ```
284288
#[rustc_allow_incoherent_impl]
285289
#[must_use = "method returns a new number and does not mutate the original value"]
@@ -426,9 +430,7 @@ impl f64 {
426430
/// let negative = -4.0_f64;
427431
/// let negative_zero = -0.0_f64;
428432
///
429-
/// let abs_difference = (positive.sqrt() - 2.0).abs();
430-
///
431-
/// assert!(abs_difference < 1e-10);
433+
/// assert_eq!(positive.sqrt(), 2.0);
432434
/// assert!(negative.sqrt().is_nan());
433435
/// assert!(negative_zero.sqrt() == negative_zero);
434436
/// ```

0 commit comments

Comments
 (0)