Skip to content

Commit 4558658

Browse files
Rollup merge of rust-lang#47277 - tspiteri:log-correctness, r=frewsxcv
doc: show that `f32::log` and `f64::log` are not correctly rounded Fixes rust-lang#47273. One thing I'm not sure about is whether the "calculated as `self.ln() / base.ln()`" bit is being too specific, maybe we do not want to make this such a strong commitment. I think it's fine, but we should not make commitments in the API documentation by accident. In case that is removed, the added sentence "`self.log2()` can ... base 10." still makes it amply clear that the `log` methods can be more inaccurate than other methods. If the above clause is removed, this second sentence can be moved to the first paragraph, kind of like the accuracy comment for the [`mul_add`](https://doc.rust-lang.org/std/primitive.f32.html#method.mul_add) method.
2 parents 5f8f59a + 6d82e78 commit 4558658

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

src/libstd/f32.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -472,20 +472,19 @@ impl f32 {
472472

473473
/// Returns the logarithm of the number with respect to an arbitrary base.
474474
///
475+
/// The result may not be correctly rounded owing to implementation details;
476+
/// `self.log2()` can produce more accurate results for base 2, and
477+
/// `self.log10()` can produce more accurate results for base 10.
478+
///
475479
/// ```
476480
/// use std::f32;
477481
///
478-
/// let ten = 10.0f32;
479-
/// let two = 2.0f32;
480-
///
481-
/// // log10(10) - 1 == 0
482-
/// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
482+
/// let five = 5.0f32;
483483
///
484-
/// // log2(2) - 1 == 0
485-
/// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
484+
/// // log5(5) - 1 == 0
485+
/// let abs_difference = (five.log(5.0) - 1.0).abs();
486486
///
487-
/// assert!(abs_difference_10 <= f32::EPSILON);
488-
/// assert!(abs_difference_2 <= f32::EPSILON);
487+
/// assert!(abs_difference <= f32::EPSILON);
489488
/// ```
490489
#[stable(feature = "rust1", since = "1.0.0")]
491490
#[inline]

src/libstd/f64.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -432,18 +432,17 @@ impl f64 {
432432

433433
/// Returns the logarithm of the number with respect to an arbitrary base.
434434
///
435-
/// ```
436-
/// let ten = 10.0_f64;
437-
/// let two = 2.0_f64;
435+
/// The result may not be correctly rounded owing to implementation details;
436+
/// `self.log2()` can produce more accurate results for base 2, and
437+
/// `self.log10()` can produce more accurate results for base 10.
438438
///
439-
/// // log10(10) - 1 == 0
440-
/// let abs_difference_10 = (ten.log(10.0) - 1.0).abs();
439+
/// ```
440+
/// let five = 5.0_f64;
441441
///
442-
/// // log2(2) - 1 == 0
443-
/// let abs_difference_2 = (two.log(2.0) - 1.0).abs();
442+
/// // log5(5) - 1 == 0
443+
/// let abs_difference = (five.log(5.0) - 1.0).abs();
444444
///
445-
/// assert!(abs_difference_10 < 1e-10);
446-
/// assert!(abs_difference_2 < 1e-10);
445+
/// assert!(abs_difference < 1e-10);
447446
/// ```
448447
#[stable(feature = "rust1", since = "1.0.0")]
449448
#[inline]

0 commit comments

Comments
 (0)