Switch to using 'ln' for the natural logarithm and 'log' for arbitrary base logarithms

This commit is contained in:
Brendan Zabarauskas 2013-05-07 03:09:09 +10:00
parent 7b2020f2c3
commit 9b09dce3e1
4 changed files with 49 additions and 36 deletions

View file

@ -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::<f32>(), 1f32 / 2f32.sqrt());
assert_approx_eq!(Real::log2_e::<f32>(), Real::e::<f32>().log2());
assert_approx_eq!(Real::log10_e::<f32>(), Real::e::<f32>().log10());
assert_approx_eq!(Real::log_2::<f32>(), 2f32.log());
assert_approx_eq!(Real::log_10::<f32>(), 10f32.log());
assert_approx_eq!(Real::ln_2::<f32>(), 2f32.ln());
assert_approx_eq!(Real::ln_10::<f32>(), 10f32.ln());
}
#[test]

View file

@ -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::<f64>(), 1f64 / 2f64.sqrt());
assert_approx_eq!(Real::log2_e::<f64>(), Real::e::<f64>().log2());
assert_approx_eq!(Real::log10_e::<f64>(), Real::e::<f64>().log10());
assert_approx_eq!(Real::log_2::<f64>(), 2f64.log());
assert_approx_eq!(Real::log_10::<f64>(), 10f64.log());
assert_approx_eq!(Real::ln_2::<f64>(), 2f64.ln());
assert_approx_eq!(Real::ln_10::<f64>(), 10f64.ln());
}
#[test]

View file

@ -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::<float>(), 1f / 2f.sqrt());
assert_approx_eq!(Real::log2_e::<float>(), Real::e::<float>().log2());
assert_approx_eq!(Real::log10_e::<float>(), Real::e::<float>().log10());
assert_approx_eq!(Real::log_2::<float>(), 2f.log());
assert_approx_eq!(Real::log_10::<float>(), 10f.log());
assert_approx_eq!(Real::ln_2::<float>(), 2f.ln());
assert_approx_eq!(Real::ln_10::<float>(), 10f.ln());
}
#[test]

View file

@ -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;