inv.rs 931 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. /// Returns the multiplicative inverse of `self`.
  6. ///
  7. /// # Examples
  8. ///
  9. /// ```
  10. /// use std::f64::INFINITY;
  11. /// use num_traits::Inv;
  12. ///
  13. /// assert_eq!(7.0.inv() * 7.0, 1.0);
  14. /// assert_eq!((-0.0).inv(), -INFINITY);
  15. /// ```
  16. fn inv(self) -> Self::Output;
  17. }
  18. impl Inv for f32 {
  19. type Output = f32;
  20. #[inline]
  21. fn inv(self) -> f32 {
  22. 1.0 / self
  23. }
  24. }
  25. impl Inv for f64 {
  26. type Output = f64;
  27. #[inline]
  28. fn inv(self) -> f64 {
  29. 1.0 / self
  30. }
  31. }
  32. impl<'a> Inv for &'a f32 {
  33. type Output = f32;
  34. #[inline]
  35. fn inv(self) -> f32 {
  36. 1.0 / *self
  37. }
  38. }
  39. impl<'a> Inv for &'a f64 {
  40. type Output = f64;
  41. #[inline]
  42. fn inv(self) -> f64 {
  43. 1.0 / *self
  44. }
  45. }