Skip to content

Commit 0eba4c2

Browse files
committed
doc: show that f32::log and f64::log are not correctly rounded
1 parent b5392f5 commit 0eba4c2

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

src/libstd/f32.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -470,22 +470,21 @@ impl f32 {
470470
return unsafe { intrinsics::logf32(self) };
471471
}
472472

473-
/// Returns the logarithm of the number with respect to an arbitrary base.
473+
/// Returns the logarithm of the number with respect to an arbitrary base,
474+
/// calculated as `self.ln() / base.ln()`.
475+
///
476+
/// `self.log2()` can produce more accurate results for base 2, and
477+
/// `self.log10()` can produce more accurate results for base 10.
474478
///
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

+9-10
Original file line numberDiff line numberDiff line change
@@ -430,20 +430,19 @@ impl f64 {
430430
self.log_wrapper(|n| { unsafe { intrinsics::logf64(n) } })
431431
}
432432

433-
/// Returns the logarithm of the number with respect to an arbitrary base.
433+
/// Returns the logarithm of the number with respect to an arbitrary base,
434+
/// calculated as `self.ln() / base.ln()`.
434435
///
435-
/// ```
436-
/// let ten = 10.0_f64;
437-
/// let two = 2.0_f64;
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)