| 1 |
/// Unary operator for retrieving the multiplicative inverse, or reciprocal, of a value. |
| 2 |
pub trait Inv { |
| 3 |
/// The result after applying the operator. |
| 4 |
type Output; |
| 5 |
|
| 6 |
/// Returns the multiplicative inverse of `self`. |
| 7 |
/// |
| 8 |
/// # Examples |
| 9 |
/// |
| 10 |
/// ``` |
| 11 |
/// use std::f64::INFINITY; |
| 12 |
/// use num_traits::Inv; |
| 13 |
/// |
| 14 |
/// assert_eq!(7.0.inv() * 7.0, 1.0); |
| 15 |
/// assert_eq!((-0.0).inv(), -INFINITY); |
| 16 |
/// ``` |
| 17 |
fn inv(self) -> Self::Output; |
| 18 |
} |
| 19 |
|
| 20 |
impl Inv for f32 { |
| 21 |
type Output = f32; |
| 22 |
#[inline] |
| 23 |
fn inv(self) -> f32 { |
| 24 |
1.0 / self |
| 25 |
} |
| 26 |
} |
| 27 |
impl Inv for f64 { |
| 28 |
type Output = f64; |
| 29 |
#[inline] |
| 30 |
fn inv(self) -> f64 { |
| 31 |
1.0 / self |
| 32 |
} |
| 33 |
} |
| 34 |
impl<'a> Inv for &'a f32 { |
| 35 |
type Output = f32; |
| 36 |
#[inline] |
| 37 |
fn inv(self) -> f32 { |
| 38 |
1.0 / *self |
| 39 |
} |
| 40 |
} |
| 41 |
impl<'a> Inv for &'a f64 { |
| 42 |
type Output = f64; |
| 43 |
#[inline] |
| 44 |
fn inv(self) -> f64 { |
| 45 |
1.0 / *self |
| 46 |
} |
| 47 |
} |