Skip to content

Commit

Permalink
Add FloatConst::{LOG10_2, LOG2_10}
Browse files Browse the repository at this point in the history
These correspond to the `extra_log_consts` added in Rust 1.43.
  • Loading branch information
cuviper committed Jun 2, 2020
1 parent cf16338 commit e9fcdb0
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/float.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::mem;
use core::num::FpCategory;
use core::ops::{Add, Neg};
use core::ops::{Add, Div, Neg};

use core::f32;
use core::f64;
Expand Down Expand Up @@ -2252,6 +2252,16 @@ macro_rules! float_const_impl {
fn TAU() -> Self where Self: Sized + Add<Self, Output = Self> {
Self::PI() + Self::PI()
}
#[doc = "Return `log10(2.0)`."]
#[inline]
fn LOG10_2() -> Self where Self: Sized + Div<Self, Output = Self> {
Self::LN_2() / Self::LN_10()
}
#[doc = "Return `log2(10.0)`."]
#[inline]
fn LOG2_10() -> Self where Self: Sized + Div<Self, Output = Self> {
Self::LN_10() / Self::LN_2()
}
}
float_const_impl! { @float f32, $($constant,)+ }
float_const_impl! { @float f64, $($constant,)+ }
Expand All @@ -2261,6 +2271,8 @@ macro_rules! float_const_impl {
constant! {
$( $constant() -> $T::consts::$constant; )+
TAU() -> 6.28318530717958647692528676655900577;
LOG10_2() -> 0.301029995663981195213738894724493027;
LOG2_10() -> 3.32192809488736234787031942948939018;
}
}
);
Expand Down Expand Up @@ -2356,4 +2368,23 @@ mod tests {
57.2957795130823208767981548141051703
);
}

#[test]
#[cfg(any(feature = "std", feature = "libm"))]
fn extra_logs() {
use float::{Float, FloatConst};

fn check<F: Float + FloatConst>(diff: F) {
let _2 = F::from(2.0).unwrap();
assert!((F::LOG10_2() - F::log10(_2)).abs() < diff);
assert!((F::LOG10_2() - F::LN_2() / F::LN_10()).abs() < diff);

let _10 = F::from(10.0).unwrap();
assert!((F::LOG2_10() - F::log2(_10)).abs() < diff);
assert!((F::LOG2_10() - F::LN_10() / F::LN_2()).abs() < diff);
}

check::<f32>(1e-6);
check::<f64>(1e-12);
}
}

0 comments on commit e9fcdb0

Please sign in to comment.