Fix Inv trait, add Pow trait.

This commit is contained in:
Clar Charr 2018-02-19 15:00:36 -05:00
parent 5bdff3f0ff
commit c1f4118b4e
4 changed files with 104 additions and 20 deletions

View File

@ -34,6 +34,7 @@ pub use float::FloatConst;
// pub use real::{FloatCore, Real}; // NOTE: Don't do this, it breaks `use num_traits::*;`. // pub use real::{FloatCore, Real}; // NOTE: Don't do this, it breaks `use num_traits::*;`.
pub use identities::{Zero, One, zero, one}; pub use identities::{Zero, One, zero, one};
pub use ops::inv::Inv; pub use ops::inv::Inv;
pub use ops::pow::Pow;
pub use ops::checked::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv, CheckedShl, CheckedShr}; pub use ops::checked::{CheckedAdd, CheckedSub, CheckedMul, CheckedDiv, CheckedShl, CheckedShr};
pub use ops::wrapping::{WrappingAdd, WrappingMul, WrappingSub}; pub use ops::wrapping::{WrappingAdd, WrappingMul, WrappingSub};
pub use ops::saturating::Saturating; pub use ops::saturating::Saturating;

View File

@ -3,27 +3,38 @@ pub trait Inv {
/// The result after applying the operator. /// The result after applying the operator.
type Output; type Output;
/// Returns the multiplicative inverse of `Self`. /// Returns the multiplicative inverse of `self`.
///
/// # Examples
///
/// ```
/// use num_traits::{Inv, One};
///
/// let x = 7.0;
/// let y = -0.0;
/// assert_eq!(x.inv() * x, One::one());
/// assert_eq!(y.inv() * y, One::one());
/// ```
fn inv(self) -> Self::Output; fn inv(self) -> Self::Output;
} }
macro_rules! inv_impl { impl Inv for f32 {
($t:ty, $out:ty, $fn:expr) => { type Output = f32;
impl<'a> Inv for $t { #[inline]
type Output = $out; fn inv(self) -> f32 { 1.0 / self }
#[inline]
fn inv(self) -> $out {
($fn)(self)
}
}
}
} }
impl Inv for f64 {
#[cfg(feature = "std")] type Output = f64;
mod float_impls { #[inline]
inv_impl!(f32, f32, f32::recip); fn inv(self) -> f64 { 1.0 / self }
inv_impl!(f64, f64, f64::recip); }
inv_impl!(&'a f32, f32, f32::recip); impl<'a> Inv for &'a f32 {
inv_impl!(&'a f64, f64, f64::recip); type Output = f32;
#[inline]
fn inv(self) -> f32 { 1.0 / *self }
}
impl<'a> Inv for &'a f64 {
type Output = f64;
#[inline]
fn inv(self) -> f64 { 1.0 / *self }
} }

View File

@ -1,4 +1,5 @@
pub mod saturating; pub mod saturating;
pub mod checked; pub mod checked;
pub mod wrapping; pub mod wrapping;
pub mod new; pub mod inv;
pub mod pow;

71
src/ops/pow.rs Normal file
View File

@ -0,0 +1,71 @@
/// Binary operator for raising a value to a power.
pub trait Pow<RHS> {
/// The result after applying the operator.
type Output;
/// Returns `self` to the power `rhs`.
///
/// # Examples
///
/// ```
/// use num_traits::Pow;
/// assert_eq!(10.pow(2), 100);
/// ```
fn pow(self, rhs: RHS) -> Self::Output;
}
macro_rules! pow_impl {
($t:ty, $rhs:ty, $method:ident) => {
impl Pow<$rhs> for $t {
type Output = $t;
#[inline]
fn pow(self, rhs: $rhs) -> $t {
<$t>::$method(self, rhs)
}
}
impl<'a> Pow<&'a $rhs> for $t {
type Output = $t;
#[inline]
fn pow(self, rhs: &'a $rhs) -> $t {
<$t>::$method(self, *rhs)
}
}
impl<'a> Pow<$rhs> for &'a $t {
type Output = $t;
#[inline]
fn pow(self, rhs: $rhs) -> $t {
<$t>::$method(*self, rhs)
}
}
impl<'a, 'b> Pow<&'a $rhs> for &'b $t {
type Output = $t;
#[inline]
fn pow(self, rhs: &'a $rhs) -> $t {
<$t>::$method(*self, *rhs)
}
}
}
}
pow_impl!(u8, u32, pow);
pow_impl!(i8, u32, pow);
pow_impl!(u16, u32, pow);
pow_impl!(i16, u32, pow);
pow_impl!(u32, u32, pow);
pow_impl!(i32, u32, pow);
pow_impl!(u64, u32, pow);
pow_impl!(i64, u32, pow);
pow_impl!(usize, u32, pow);
pow_impl!(isize, u32, pow);
#[cfg(feature = "std")]
mod float_impls {
pow_impl!(f32, i32, powi);
pow_impl!(f64, i32, powi);
pow_impl!(f32, f32, powf);
pow_impl!(f64, f64, powf);
}