Skip to content

Commit 50d3ddc

Browse files
committed
make ln_1p examples more representative of use
With this commit, the examples for ln_1p would fail if (x + 1.0).ln() is used instead of x.ln_1p().
1 parent 37ce212 commit 50d3ddc

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

library/std/src/f32.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -740,12 +740,13 @@ impl f32 {
740740
/// # Examples
741741
///
742742
/// ```
743-
/// let x = std::f32::consts::E - 1.0;
743+
/// let x = 1e-8_f32;
744744
///
745-
/// // ln(1 + (e - 1)) == ln(e) == 1
746-
/// 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();
747748
///
748-
/// assert!(abs_difference <= f32::EPSILON);
749+
/// assert!(abs_difference < 1e-10);
749750
/// ```
750751
#[must_use = "method returns a new number and does not mutate the original value"]
751752
#[stable(feature = "rust1", since = "1.0.0")]

library/std/src/f64.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -742,12 +742,13 @@ impl f64 {
742742
/// # Examples
743743
///
744744
/// ```
745-
/// let x = std::f64::consts::E - 1.0;
745+
/// let x = 1e-16_f64;
746746
///
747-
/// // ln(1 + (e - 1)) == ln(e) == 1
748-
/// 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();
749750
///
750-
/// assert!(abs_difference < 1e-10);
751+
/// assert!(abs_difference < 1e-20);
751752
/// ```
752753
#[must_use = "method returns a new number and does not mutate the original value"]
753754
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)