Skip to content

Commit 4ebd553

Browse files
authored
Rollup merge of #77099 - tspiteri:exp_m1-examples, r=m-ou-se
make exp_m1 and ln_1p examples more representative of use With this PR, the examples for `exp_m1` would fail if `x.exp() - 1.0` is used instead of `x.exp_m1()`, and the examples for `ln_1p` would fail if `(x + 1.0).ln()` is used instead of `x.ln_1p()`.
2 parents 76b8b00 + 50d3ddc commit 4ebd553

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

library/std/src/f32.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -719,12 +719,13 @@ impl f32 {
719719
/// # Examples
720720
///
721721
/// ```
722-
/// let x = 6.0f32;
722+
/// let x = 1e-8_f32;
723723
///
724-
/// // e^(ln(6)) - 1
725-
/// let abs_difference = (x.ln().exp_m1() - 5.0).abs();
724+
/// // for very small x, e^x is approximately 1 + x + x^2 / 2
725+
/// let approx = x + x * x / 2.0;
726+
/// let abs_difference = (x.exp_m1() - approx).abs();
726727
///
727-
/// assert!(abs_difference <= f32::EPSILON);
728+
/// assert!(abs_difference < 1e-10);
728729
/// ```
729730
#[must_use = "method returns a new number and does not mutate the original value"]
730731
#[stable(feature = "rust1", since = "1.0.0")]
@@ -739,12 +740,13 @@ impl f32 {
739740
/// # Examples
740741
///
741742
/// ```
742-
/// let x = std::f32::consts::E - 1.0;
743+
/// let x = 1e-8_f32;
743744
///
744-
/// // ln(1 + (e - 1)) == ln(e) == 1
745-
/// let abs_difference = (x.ln_1p() - 1.0).abs();
745+
/// // for very small x, ln(1 + x) is approximately x - x^2 / 2
746+
/// let approx = x - x * x / 2.0;
747+
/// let abs_difference = (x.ln_1p() - approx).abs();
746748
///
747-
/// assert!(abs_difference <= f32::EPSILON);
749+
/// assert!(abs_difference < 1e-10);
748750
/// ```
749751
#[must_use = "method returns a new number and does not mutate the original value"]
750752
#[stable(feature = "rust1", since = "1.0.0")]

library/std/src/f64.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -721,12 +721,13 @@ impl f64 {
721721
/// # Examples
722722
///
723723
/// ```
724-
/// let x = 7.0_f64;
724+
/// let x = 1e-16_f64;
725725
///
726-
/// // e^(ln(7)) - 1
727-
/// let abs_difference = (x.ln().exp_m1() - 6.0).abs();
726+
/// // for very small x, e^x is approximately 1 + x + x^2 / 2
727+
/// let approx = x + x * x / 2.0;
728+
/// let abs_difference = (x.exp_m1() - approx).abs();
728729
///
729-
/// assert!(abs_difference < 1e-10);
730+
/// assert!(abs_difference < 1e-20);
730731
/// ```
731732
#[must_use = "method returns a new number and does not mutate the original value"]
732733
#[stable(feature = "rust1", since = "1.0.0")]
@@ -741,12 +742,13 @@ impl f64 {
741742
/// # Examples
742743
///
743744
/// ```
744-
/// let x = std::f64::consts::E - 1.0;
745+
/// let x = 1e-16_f64;
745746
///
746-
/// // ln(1 + (e - 1)) == ln(e) == 1
747-
/// let abs_difference = (x.ln_1p() - 1.0).abs();
747+
/// // for very small x, ln(1 + x) is approximately x - x^2 / 2
748+
/// let approx = x - x * x / 2.0;
749+
/// let abs_difference = (x.ln_1p() - approx).abs();
748750
///
749-
/// assert!(abs_difference < 1e-10);
751+
/// assert!(abs_difference < 1e-20);
750752
/// ```
751753
#[must_use = "method returns a new number and does not mutate the original value"]
752754
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)