From 0eba4c206a3c47c375cb9ead20d37cf00f6351cf Mon Sep 17 00:00:00 2001 From: Trevor Spiteri Date: Mon, 8 Jan 2018 19:22:37 +0100 Subject: [PATCH 1/2] doc: show that `f32::log` and `f64::log` are not correctly rounded --- src/libstd/f32.rs | 19 +++++++++---------- src/libstd/f64.rs | 19 +++++++++---------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/libstd/f32.rs b/src/libstd/f32.rs index 6d76c7e722c45..8a15f19e44067 100644 --- a/src/libstd/f32.rs +++ b/src/libstd/f32.rs @@ -470,22 +470,21 @@ impl f32 { return unsafe { intrinsics::logf32(self) }; } - /// Returns the logarithm of the number with respect to an arbitrary base. + /// Returns the logarithm of the number with respect to an arbitrary base, + /// calculated as `self.ln() / base.ln()`. + /// + /// `self.log2()` can produce more accurate results for base 2, and + /// `self.log10()` can produce more accurate results for base 10. /// /// ``` /// use std::f32; /// - /// let ten = 10.0f32; - /// let two = 2.0f32; - /// - /// // log10(10) - 1 == 0 - /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs(); + /// let five = 5.0f32; /// - /// // log2(2) - 1 == 0 - /// let abs_difference_2 = (two.log(2.0) - 1.0).abs(); + /// // log5(5) - 1 == 0 + /// let abs_difference = (five.log(5.0) - 1.0).abs(); /// - /// assert!(abs_difference_10 <= f32::EPSILON); - /// assert!(abs_difference_2 <= f32::EPSILON); + /// assert!(abs_difference <= f32::EPSILON); /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] diff --git a/src/libstd/f64.rs b/src/libstd/f64.rs index dee9566f1fc68..33f447b794c9b 100644 --- a/src/libstd/f64.rs +++ b/src/libstd/f64.rs @@ -430,20 +430,19 @@ impl f64 { self.log_wrapper(|n| { unsafe { intrinsics::logf64(n) } }) } - /// Returns the logarithm of the number with respect to an arbitrary base. + /// Returns the logarithm of the number with respect to an arbitrary base, + /// calculated as `self.ln() / base.ln()`. /// - /// ``` - /// let ten = 10.0_f64; - /// let two = 2.0_f64; + /// `self.log2()` can produce more accurate results for base 2, and + /// `self.log10()` can produce more accurate results for base 10. /// - /// // log10(10) - 1 == 0 - /// let abs_difference_10 = (ten.log(10.0) - 1.0).abs(); + /// ``` + /// let five = 5.0_f64; /// - /// // log2(2) - 1 == 0 - /// let abs_difference_2 = (two.log(2.0) - 1.0).abs(); + /// // log5(5) - 1 == 0 + /// let abs_difference = (five.log(5.0) - 1.0).abs(); /// - /// assert!(abs_difference_10 < 1e-10); - /// assert!(abs_difference_2 < 1e-10); + /// assert!(abs_difference < 1e-10); /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] From 6d82e7814f1a387c0510ee525c6e0ac8fa890c40 Mon Sep 17 00:00:00 2001 From: Trevor Spiteri Date: Tue, 9 Jan 2018 12:26:00 +0100 Subject: [PATCH 2/2] remove implementation detail from doc --- src/libstd/f32.rs | 4 ++-- src/libstd/f64.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libstd/f32.rs b/src/libstd/f32.rs index 8a15f19e44067..5e5695f15ac3f 100644 --- a/src/libstd/f32.rs +++ b/src/libstd/f32.rs @@ -470,9 +470,9 @@ impl f32 { return unsafe { intrinsics::logf32(self) }; } - /// Returns the logarithm of the number with respect to an arbitrary base, - /// calculated as `self.ln() / base.ln()`. + /// Returns the logarithm of the number with respect to an arbitrary base. /// + /// The result may not be correctly rounded owing to implementation details; /// `self.log2()` can produce more accurate results for base 2, and /// `self.log10()` can produce more accurate results for base 10. /// diff --git a/src/libstd/f64.rs b/src/libstd/f64.rs index 33f447b794c9b..e4eea745bb77c 100644 --- a/src/libstd/f64.rs +++ b/src/libstd/f64.rs @@ -430,9 +430,9 @@ impl f64 { self.log_wrapper(|n| { unsafe { intrinsics::logf64(n) } }) } - /// Returns the logarithm of the number with respect to an arbitrary base, - /// calculated as `self.ln() / base.ln()`. + /// Returns the logarithm of the number with respect to an arbitrary base. /// + /// The result may not be correctly rounded owing to implementation details; /// `self.log2()` can produce more accurate results for base 2, and /// `self.log10()` can produce more accurate results for base 10. ///