pub trait Root<Exp = Self> {
type Output;
fn root(self, degree: Exp) -> Self::Output;
}
#[cfg(any(feature = "std", feature = "libm"))]
mod float_impls {
use super::Root;
#[inline]
fn powf_f32(base: f32, exp: f32) -> f32 {
#[cfg(feature = "libm")]
{
libm::powf(base, exp)
}
#[cfg(not(feature = "libm"))]
{
f32::powf(base, exp)
}
}
#[inline]
fn powf_f64(base: f64, exp: f64) -> f64 {
#[cfg(feature = "libm")]
{
libm::pow(base, exp)
}
#[cfg(not(feature = "libm"))]
{
f64::powf(base, exp)
}
}
macro_rules! impl_root_float_self {
($ty:ty, $powf:ident) => {
impl Root<$ty> for $ty {
type Output = $ty;
#[inline]
fn root(self, degree: $ty) -> Self::Output {
$powf(self, 1.0 / degree)
}
}
};
}
macro_rules! impl_root_float_int {
($ty:ty, $powf:ident) => {
impl Root<u32> for $ty {
type Output = $ty;
#[inline]
fn root(self, degree: u32) -> Self::Output {
let degree = degree as $ty;
$powf(self, 1.0 / degree)
}
}
};
}
impl_root_float_self!(f32, powf_f32);
impl_root_float_self!(f64, powf_f64);
impl_root_float_int!(f32, powf_f32);
impl_root_float_int!(f64, powf_f64);
}