Skip to content

Commit

Permalink
Add libm feature.
Browse files Browse the repository at this point in the history
Allows use of `Pow` and `Float` traits in no_std builds.

Fixes #164.
  • Loading branch information
mbrubeck committed Dec 19, 2024
1 parent d722d5f commit 14c9153
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ borsh = { version = "1.2.0", optional = true, default-features = false }
bytemuck = { version = "1.12.2", optional = true, default-features = false }
derive-visitor = { version = "0.4.0", optional = true }
num-cmp = { version = "0.1.0", optional = true }
num-traits = { version = "0.2.1", default-features = false }
num-traits = { version = "0.2.9", default-features = false }
proptest = { version = "1.0.0", optional = true }
rand = { version = "0.8.3", optional = true, default-features = false }
rkyv = { version = "0.7.41", optional = true, default-features = false, features = ["rend"] }
Expand All @@ -34,6 +34,7 @@ serde_test = "1.0"
[features]
default = ["std"]
std = ["num-traits/std"]
libm = ["num-traits/libm"]
serde = ["dep:serde", "rand?/serde1"]
randtest = ["rand/std", "rand/std_rng"]
rkyv = ["rkyv_32"]
Expand Down
36 changes: 18 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub use num_traits::float::FloatCore;
use num_traits::{
AsPrimitive, Bounded, FloatConst, FromPrimitive, Num, NumCast, One, Signed, ToPrimitive, Zero,
};
#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
pub use num_traits::{Float, Pow};

#[cfg(feature = "rand")]
Expand Down Expand Up @@ -571,7 +571,7 @@ impl_ordered_float_binop! {Rem, rem, RemAssign, rem_assign}

macro_rules! impl_ordered_float_pow {
($inner:ty, $rhs:ty) => {
#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl Pow<$rhs> for OrderedFloat<$inner> {
type Output = OrderedFloat<$inner>;
#[inline]
Expand All @@ -580,7 +580,7 @@ macro_rules! impl_ordered_float_pow {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<'a> Pow<&'a $rhs> for OrderedFloat<$inner> {
type Output = OrderedFloat<$inner>;
#[inline]
Expand All @@ -589,7 +589,7 @@ macro_rules! impl_ordered_float_pow {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<'a> Pow<$rhs> for &'a OrderedFloat<$inner> {
type Output = OrderedFloat<$inner>;
#[inline]
Expand All @@ -598,7 +598,7 @@ macro_rules! impl_ordered_float_pow {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<'a, 'b> Pow<&'a $rhs> for &'b OrderedFloat<$inner> {
type Output = OrderedFloat<$inner>;
#[inline]
Expand All @@ -625,7 +625,7 @@ impl_ordered_float_pow! {f64, f64}

macro_rules! impl_ordered_float_self_pow {
($base:ty, $exp:ty) => {
#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl Pow<OrderedFloat<$exp>> for OrderedFloat<$base> {
type Output = OrderedFloat<$base>;
#[inline]
Expand All @@ -634,7 +634,7 @@ macro_rules! impl_ordered_float_self_pow {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<'a> Pow<&'a OrderedFloat<$exp>> for OrderedFloat<$base> {
type Output = OrderedFloat<$base>;
#[inline]
Expand All @@ -643,7 +643,7 @@ macro_rules! impl_ordered_float_self_pow {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<'a> Pow<OrderedFloat<$exp>> for &'a OrderedFloat<$base> {
type Output = OrderedFloat<$base>;
#[inline]
Expand All @@ -652,7 +652,7 @@ macro_rules! impl_ordered_float_self_pow {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<'a, 'b> Pow<&'a OrderedFloat<$exp>> for &'b OrderedFloat<$base> {
type Output = OrderedFloat<$base>;
#[inline]
Expand Down Expand Up @@ -1037,7 +1037,7 @@ impl<T: FloatCore> FloatCore for OrderedFloat<T> {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<T: Float + FloatCore> Float for OrderedFloat<T> {
fn nan() -> Self {
OrderedFloat(<T as Float>::nan())
Expand Down Expand Up @@ -1663,7 +1663,7 @@ impl_not_nan_binop! {Rem, rem, RemAssign, rem_assign}
// Will panic if NaN value is return from the operation
macro_rules! impl_not_nan_pow {
($inner:ty, $rhs:ty) => {
#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl Pow<$rhs> for NotNan<$inner> {
type Output = NotNan<$inner>;
#[inline]
Expand All @@ -1672,7 +1672,7 @@ macro_rules! impl_not_nan_pow {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<'a> Pow<&'a $rhs> for NotNan<$inner> {
type Output = NotNan<$inner>;
#[inline]
Expand All @@ -1681,7 +1681,7 @@ macro_rules! impl_not_nan_pow {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<'a> Pow<$rhs> for &'a NotNan<$inner> {
type Output = NotNan<$inner>;
#[inline]
Expand All @@ -1690,7 +1690,7 @@ macro_rules! impl_not_nan_pow {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<'a, 'b> Pow<&'a $rhs> for &'b NotNan<$inner> {
type Output = NotNan<$inner>;
#[inline]
Expand Down Expand Up @@ -1718,7 +1718,7 @@ impl_not_nan_pow! {f64, f64}
// This also should panic on NaN
macro_rules! impl_not_nan_self_pow {
($base:ty, $exp:ty) => {
#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl Pow<NotNan<$exp>> for NotNan<$base> {
type Output = NotNan<$base>;
#[inline]
Expand All @@ -1727,7 +1727,7 @@ macro_rules! impl_not_nan_self_pow {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<'a> Pow<&'a NotNan<$exp>> for NotNan<$base> {
type Output = NotNan<$base>;
#[inline]
Expand All @@ -1736,7 +1736,7 @@ macro_rules! impl_not_nan_self_pow {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<'a> Pow<NotNan<$exp>> for &'a NotNan<$base> {
type Output = NotNan<$base>;
#[inline]
Expand All @@ -1745,7 +1745,7 @@ macro_rules! impl_not_nan_self_pow {
}
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
impl<'a, 'b> Pow<&'a NotNan<$exp>> for &'b NotNan<$base> {
type Output = NotNan<$base>;
#[inline]
Expand Down
10 changes: 5 additions & 5 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extern crate ordered_float;

pub use num_traits::float::FloatCore;
pub use num_traits::{Bounded, FloatConst, FromPrimitive, Num, One, Signed, ToPrimitive, Zero};
#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
pub use num_traits::{Float, Pow};
pub use ordered_float::*;

Expand Down Expand Up @@ -788,7 +788,7 @@ fn float_consts_equal_inner() {
test_float_const_methods!(NotNan<f32>);
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
macro_rules! test_pow_ord {
($type:ident < $inner:ident >) => {
assert_eq!($type::<$inner>::from(3.0).pow(2i8), OrderedFloat(9.0));
Expand All @@ -800,7 +800,7 @@ macro_rules! test_pow_ord {
};
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
macro_rules! test_pow_nn {
($type:ident < $inner:ident >) => {
assert_eq!(
Expand Down Expand Up @@ -830,7 +830,7 @@ macro_rules! test_pow_nn {
};
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
#[test]
fn test_pow_works() {
assert_eq!(OrderedFloat(3.0).pow(OrderedFloat(2.0)), OrderedFloat(9.0));
Expand All @@ -850,7 +850,7 @@ fn test_pow_works() {
);
}

#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "libm"))]
#[test]
#[should_panic]
fn test_pow_fails_on_nan() {
Expand Down

0 comments on commit 14c9153

Please sign in to comment.