Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FloatConst::{LOG10_2, LOG2_10} #171

Merged
merged 1 commit into from
Jun 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}