Skip to content

Commit

Permalink
make ln_1p examples more representative of use
Browse files Browse the repository at this point in the history
With this commit, the examples for ln_1p would fail if (x + 1.0).ln()
is used instead of x.ln_1p().
  • Loading branch information
tspiteri committed Sep 23, 2020
1 parent 37ce212 commit 50d3ddc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
9 changes: 5 additions & 4 deletions library/std/src/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,12 +740,13 @@ impl f32 {
/// # Examples
///
/// ```
/// let x = std::f32::consts::E - 1.0;
/// let x = 1e-8_f32;
///
/// // ln(1 + (e - 1)) == ln(e) == 1
/// let abs_difference = (x.ln_1p() - 1.0).abs();
/// // for very small x, ln(1 + x) is approximately x - x^2 / 2
/// let approx = x - x * x / 2.0;
/// let abs_difference = (x.ln_1p() - approx).abs();
///
/// assert!(abs_difference <= f32::EPSILON);
/// assert!(abs_difference < 1e-10);
/// ```
#[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
9 changes: 5 additions & 4 deletions library/std/src/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,12 +742,13 @@ impl f64 {
/// # Examples
///
/// ```
/// let x = std::f64::consts::E - 1.0;
/// let x = 1e-16_f64;
///
/// // ln(1 + (e - 1)) == ln(e) == 1
/// let abs_difference = (x.ln_1p() - 1.0).abs();
/// // for very small x, ln(1 + x) is approximately x - x^2 / 2
/// let approx = x - x * x / 2.0;
/// let abs_difference = (x.ln_1p() - approx).abs();
///
/// assert!(abs_difference < 1e-10);
/// assert!(abs_difference < 1e-20);
/// ```
#[must_use = "method returns a new number and does not mutate the original value"]
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down

0 comments on commit 50d3ddc

Please sign in to comment.