From 9b09dce3e1a9935bbe443a976e47ed9f71227883 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Tue, 7 May 2013 03:09:09 +1000 Subject: [PATCH] Switch to using 'ln' for the natural logarithm and 'log' for arbitrary base logarithms --- src/libcore/num/f32.rs | 26 ++++++++++++++------------ src/libcore/num/f64.rs | 26 ++++++++++++++------------ src/libcore/num/float.rs | 26 +++++++++++++++++--------- src/libcore/num/num.rs | 7 ++++--- 4 files changed, 49 insertions(+), 36 deletions(-) diff --git a/src/libcore/num/f32.rs b/src/libcore/num/f32.rs index 7c13f136a80f..8a0b6567898e 100644 --- a/src/libcore/num/f32.rs +++ b/src/libcore/num/f32.rs @@ -195,11 +195,6 @@ pub mod consts { pub static ln_10: f32 = 2.30258509299404568401799145468436421_f32; } -#[inline(always)] -pub fn logarithm(n: f32, b: f32) -> f32 { - return log2(n) / log2(b); -} - impl Num for f32 {} #[cfg(notest)] @@ -422,12 +417,19 @@ impl Exponential for f32 { #[inline(always)] fn expm1(&self) -> f32 { expm1(*self) } + /// Returns the natural logarithm of the number #[inline(always)] - fn log(&self) -> f32 { ln(*self) } + fn ln(&self) -> f32 { ln(*self) } + /// Returns the logarithm of the number with respect to an arbitrary base + #[inline(always)] + fn log(&self, base: f32) -> f32 { self.ln() / base.ln() } + + /// Returns the base 2 logarithm of the number #[inline(always)] fn log2(&self) -> f32 { log2(*self) } + /// Returns the base 10 logarithm of the number #[inline(always)] fn log10(&self) -> f32 { log10(*self) } } @@ -504,13 +506,13 @@ impl Real for f32 { #[inline(always)] fn log10_e() -> f32 { 0.434294481903251827651128918916605082 } - /// log(2.0) + /// ln(2.0) #[inline(always)] - fn log_2() -> f32 { 0.693147180559945309417232121458176568 } + fn ln_2() -> f32 { 0.693147180559945309417232121458176568 } - /// log(10.0) + /// ln(10.0) #[inline(always)] - fn log_10() -> f32 { 2.30258509299404568401799145468436421 } + fn ln_10() -> f32 { 2.30258509299404568401799145468436421 } /// Converts to degrees, assuming the number is in radians #[inline(always)] @@ -938,8 +940,8 @@ mod tests { assert_approx_eq!(Real::frac_1_sqrt2::(), 1f32 / 2f32.sqrt()); assert_approx_eq!(Real::log2_e::(), Real::e::().log2()); assert_approx_eq!(Real::log10_e::(), Real::e::().log10()); - assert_approx_eq!(Real::log_2::(), 2f32.log()); - assert_approx_eq!(Real::log_10::(), 10f32.log()); + assert_approx_eq!(Real::ln_2::(), 2f32.ln()); + assert_approx_eq!(Real::ln_10::(), 10f32.ln()); } #[test] diff --git a/src/libcore/num/f64.rs b/src/libcore/num/f64.rs index e5f10c23ecd8..6d2fd3d1b81b 100644 --- a/src/libcore/num/f64.rs +++ b/src/libcore/num/f64.rs @@ -218,11 +218,6 @@ pub mod consts { pub static ln_10: f64 = 2.30258509299404568401799145468436421_f64; } -#[inline(always)] -pub fn logarithm(n: f64, b: f64) -> f64 { - return log2(n) / log2(b); -} - impl Num for f64 {} #[cfg(notest)] @@ -435,12 +430,19 @@ impl Exponential for f64 { #[inline(always)] fn expm1(&self) -> f64 { expm1(*self) } + /// Returns the natural logarithm of the number #[inline(always)] - fn log(&self) -> f64 { ln(*self) } + fn ln(&self) -> f64 { ln(*self) } + /// Returns the logarithm of the number with respect to an arbitrary base + #[inline(always)] + fn log(&self, base: f64) -> f64 { self.ln() / base.ln() } + + /// Returns the base 2 logarithm of the number #[inline(always)] fn log2(&self) -> f64 { log2(*self) } + /// Returns the base 10 logarithm of the number #[inline(always)] fn log10(&self) -> f64 { log10(*self) } } @@ -517,13 +519,13 @@ impl Real for f64 { #[inline(always)] fn log10_e() -> f64 { 0.434294481903251827651128918916605082 } - /// log(2.0) + /// ln(2.0) #[inline(always)] - fn log_2() -> f64 { 0.693147180559945309417232121458176568 } + fn ln_2() -> f64 { 0.693147180559945309417232121458176568 } - /// log(10.0) + /// ln(10.0) #[inline(always)] - fn log_10() -> f64 { 2.30258509299404568401799145468436421 } + fn ln_10() -> f64 { 2.30258509299404568401799145468436421 } /// Converts to degrees, assuming the number is in radians #[inline(always)] @@ -985,8 +987,8 @@ mod tests { assert_approx_eq!(Real::frac_1_sqrt2::(), 1f64 / 2f64.sqrt()); assert_approx_eq!(Real::log2_e::(), Real::e::().log2()); assert_approx_eq!(Real::log10_e::(), Real::e::().log10()); - assert_approx_eq!(Real::log_2::(), 2f64.log()); - assert_approx_eq!(Real::log_10::(), 10f64.log()); + assert_approx_eq!(Real::ln_2::(), 2f64.ln()); + assert_approx_eq!(Real::ln_10::(), 10f64.ln()); } #[test] diff --git a/src/libcore/num/float.rs b/src/libcore/num/float.rs index a54816532639..18c512b485fb 100644 --- a/src/libcore/num/float.rs +++ b/src/libcore/num/float.rs @@ -25,7 +25,6 @@ use num::{Zero, One, strconv}; use prelude::*; pub use f64::{add, sub, mul, div, rem, lt, le, eq, ne, ge, gt}; -pub use f64::logarithm; pub use f64::{acos, asin, atan2, cbrt, ceil, copysign, cosh, floor}; pub use f64::{erf, erfc, exp, expm1, exp2, abs_sub}; pub use f64::{mul_add, fmax, fmin, next_after, frexp, hypot, ldexp}; @@ -548,16 +547,25 @@ impl Exponential for float { (*self as f64).expm1() as float } + /// Returns the natural logarithm of the number #[inline(always)] - fn log(&self) -> float { - (*self as f64).log() as float + fn ln(&self) -> float { + (*self as f64).ln() as float } + /// Returns the logarithm of the number with respect to an arbitrary base + #[inline(always)] + fn log(&self, base: float) -> float { + (*self as f64).log(base as f64) as float + } + + /// Returns the base 2 logarithm of the number #[inline(always)] fn log2(&self) -> float { (*self as f64).log2() as float } + /// Returns the base 10 logarithm of the number #[inline(always)] fn log10(&self) -> float { (*self as f64).log10() as float @@ -642,13 +650,13 @@ impl Real for float { #[inline(always)] fn log10_e() -> float { 0.434294481903251827651128918916605082 } - /// log(2.0) + /// ln(2.0) #[inline(always)] - fn log_2() -> float { 0.693147180559945309417232121458176568 } + fn ln_2() -> float { 0.693147180559945309417232121458176568 } - /// log(10.0) + /// ln(10.0) #[inline(always)] - fn log_10() -> float { 2.30258509299404568401799145468436421 } + fn ln_10() -> float { 2.30258509299404568401799145468436421 } /// Converts to degrees, assuming the number is in radians #[inline(always)] @@ -949,8 +957,8 @@ mod tests { assert_approx_eq!(Real::frac_1_sqrt2::(), 1f / 2f.sqrt()); assert_approx_eq!(Real::log2_e::(), Real::e::().log2()); assert_approx_eq!(Real::log10_e::(), Real::e::().log10()); - assert_approx_eq!(Real::log_2::(), 2f.log()); - assert_approx_eq!(Real::log_10::(), 10f.log()); + assert_approx_eq!(Real::ln_2::(), 2f.ln()); + assert_approx_eq!(Real::ln_10::(), 10f.ln()); } #[test] diff --git a/src/libcore/num/num.rs b/src/libcore/num/num.rs index caa14ea802f6..7fcd16c199a7 100644 --- a/src/libcore/num/num.rs +++ b/src/libcore/num/num.rs @@ -122,7 +122,8 @@ pub trait Exponential { fn exp(&self) -> Self; fn exp2(&self) -> Self; fn expm1(&self) -> Self; - fn log(&self) -> Self; + fn ln(&self) -> Self; + fn log(&self, base: Self) -> Self; fn log2(&self) -> Self; fn log10(&self) -> Self; } @@ -158,8 +159,8 @@ pub trait Real: Signed fn e() -> Self; fn log2_e() -> Self; fn log10_e() -> Self; - fn log_2() -> Self; - fn log_10() -> Self; + fn ln_2() -> Self; + fn ln_10() -> Self; // Angular conversions fn to_degrees(&self) -> Self;