]> git.lizzy.rs Git - rust.git/commitdiff
Switch to using 'ln' for the natural logarithm and 'log' for arbitrary base logarithms
authorBrendan Zabarauskas <bjzaba@yahoo.com.au>
Mon, 6 May 2013 17:09:09 +0000 (03:09 +1000)
committerBrendan Zabarauskas <bjzaba@yahoo.com.au>
Tue, 7 May 2013 09:16:02 +0000 (19:16 +1000)
src/libcore/num/f32.rs
src/libcore/num/f64.rs
src/libcore/num/float.rs
src/libcore/num/num.rs

index 7c13f136a80f27ef4c761eb0f1975b2d7ca0ce69..8a0b6567898e0eac5ef3f90226825008350dd618 100644 (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 @@ fn exp2(&self) -> f32 { exp2(*self) }
     #[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 @@ fn log2_e() -> f32 { 1.44269504088896340735992468100189214 }
     #[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 @@ fn test_real_consts() {
         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]
index e5f10c23ecd8755e66f61cf203d1cd6fb332532e..6d2fd3d1b81b6a7d8660595baa499925476d931a 100644 (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 @@ fn exp2(&self) -> f64 { exp2(*self) }
     #[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 @@ fn log2_e() -> f64 { 1.44269504088896340735992468100189214 }
     #[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 @@ fn test_real_consts() {
         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]
index a54816532639666faa931c32fa4ab7b0928bc26c..18c512b485fb1897187082f2f880aba286942943 100644 (file)
@@ -25,7 +25,6 @@
 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 @@ fn expm1(&self) -> 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 @@ fn log2_e() -> float { 1.44269504088896340735992468100189214 }
     #[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 @@ fn test_real_consts() {
         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]
index caa14ea802f6cdc6c821e10fecbb058f2a4e988b..7fcd16c199a705a1b83e50d79c02689e0c141f69 100644 (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;