//! Numerical traits. #![allow(missing_docs, clippy::useless_nonzero_new_unchecked)] use std::num::NonZeroU8; use std::ops::{Add, Div, Mul, Neg, Shl, Sub}; /// A type that has a "zero". pub trait Zero: PartialEq + Sized { const ZERO: Self; fn is_zero(&self) -> bool; } impl Zero for f32 { const ZERO: f32 = 0.; fn is_zero(&self) -> bool { self == &Self::ZERO } } impl Zero for f64 { const ZERO: f64 = 0.; fn is_zero(&self) -> bool { self == &Self::ZERO } } impl Zero for usize { const ZERO: usize = 0; fn is_zero(&self) -> bool { self == &Self::ZERO } } impl Zero for i32 { const ZERO: i32 = 0; fn is_zero(&self) -> bool { self == &Self::ZERO } } impl Zero for u8 { const ZERO: u8 = 0; fn is_zero(&self) -> bool { self == &Self::ZERO } } impl Zero for u16 { const ZERO: u16 = 0; fn is_zero(&self) -> bool { self == &Self::ZERO } } impl Zero for u32 { const ZERO: u32 = 0; fn is_zero(&self) -> bool { self == &Self::ZERO } } impl Zero for u64 { const ZERO: u64 = 0; fn is_zero(&self) -> bool { self == &Self::ZERO } } impl Zero for i64 { const ZERO: i64 = 0; fn is_zero(&self) -> bool { self == &Self::ZERO } } //////////////////////////////////////////////////////////////////////////////// /// A type that has a "one". pub trait One: Sized { const ONE: Self; fn is_one(&self) -> bool; } impl One for f32 { const ONE: f32 = 1.; fn is_one(&self) -> bool { self == &Self::ONE } } impl One for f64 { const ONE: f64 = 1.; fn is_one(&self) -> bool { self == &Self::ONE } } impl One for usize { const ONE: usize = 1; fn is_one(&self) -> bool { self == &Self::ONE } } impl One for u8 { const ONE: u8 = 1; fn is_one(&self) -> bool { self == &Self::ONE } } impl One for u32 { const ONE: u32 = 1; fn is_one(&self) -> bool { self == &Self::ONE } } impl One for u64 { const ONE: u64 = 1; fn is_one(&self) -> bool { self == &Self::ONE } } impl One for i32 { const ONE: i32 = 1; fn is_one(&self) -> bool { self == &Self::ONE } } impl One for NonZeroU8 { const ONE: NonZeroU8 = unsafe { NonZeroU8::new_unchecked(u8::ONE) }; fn is_one(&self) -> bool { self == &Self::ONE } } //////////////////////////////////////////////////////////////////////////////// /// A type that has a "two". pub trait Two { const TWO: Self; } impl Two for f32 { const TWO: f32 = 2.; } impl Two for f64 { const TWO: f64 = 2.; } impl Two for i32 { const TWO: i32 = 2; } //////////////////////////////////////////////////////////////////////////////// /// Trait for types with defined minimum and maximum bounds. pub trait Bounded { /// Minimum value for this type. const MIN: Self; /// Maximum value for this type. const MAX: Self; } impl Bounded for u8 { const MIN: Self = u8::MIN; const MAX: Self = u8::MAX; } impl Bounded for u16 { const MIN: Self = u16::MIN; const MAX: Self = u16::MAX; } impl Bounded for u32 { const MIN: Self = u32::MIN; const MAX: Self = u32::MAX; } impl Bounded for u64 { const MIN: Self = u64::MIN; const MAX: Self = u64::MAX; } impl Bounded for i8 { const MIN: Self = i8::MIN; const MAX: Self = i8::MAX; } impl Bounded for i16 { const MIN: Self = i16::MIN; const MAX: Self = i16::MAX; } impl Bounded for i32 { const MIN: Self = i32::MIN; const MAX: Self = i32::MAX; } impl Bounded for i64 { const MIN: Self = i64::MIN; const MAX: Self = i64::MAX; } impl Bounded for f64 { const MIN: Self = f64::MIN; const MAX: Self = f64::MAX; } impl Bounded for f32 { const MIN: Self = f32::MIN; const MAX: Self = f32::MAX; } impl Bounded for usize { const MIN: Self = usize::MIN; const MAX: Self = usize::MAX; } //////////////////////////////////////////////////////////////////////////////// /// Trait defining operations and properties required for integer types. pub trait Integer: Copy + Ord + One + Zero + Bounded + Add + Sub + Shl { /// Number of bits in this integer type. const BITS: u32; /// Converts a [`u64`] to this integer type. fn from_u64(other: u64) -> Self; /// Converts this integer to [`u64`]. fn as_u64(&self) -> u64; /// Wrapping add. fn wrapping_add(self, rhs: Self) -> Self; /// Wrapping sub. fn wrapping_sub(self, rhs: Self) -> Self; } impl Integer for u32 { const BITS: u32 = u32::BITS; fn from_u64(other: u64) -> Self { other as u32 } fn as_u64(&self) -> u64 { *self as u64 } fn wrapping_add(self, rhs: Self) -> Self { self.wrapping_add(rhs) } fn wrapping_sub(self, rhs: Self) -> Self { self.wrapping_sub(rhs) } } impl Integer for i32 { const BITS: u32 = i32::BITS; fn from_u64(other: u64) -> Self { other as u32 as i32 } fn as_u64(&self) -> u64 { *self as u32 as u64 } fn wrapping_add(self, rhs: Self) -> Self { self.wrapping_add(rhs) } fn wrapping_sub(self, rhs: Self) -> Self { self.wrapping_sub(rhs) } } impl Integer for u64 { const BITS: u32 = u64::BITS; fn from_u64(other: u64) -> Self { other } fn as_u64(&self) -> u64 { *self } fn wrapping_add(self, rhs: Self) -> Self { self.wrapping_add(rhs) } fn wrapping_sub(self, rhs: Self) -> Self { self.wrapping_sub(rhs) } } impl Integer for usize { const BITS: u32 = usize::BITS; fn from_u64(other: u64) -> Self { other as usize } fn as_u64(&self) -> u64 { *self as u64 } fn wrapping_add(self, rhs: Self) -> Self { self.wrapping_add(rhs) } fn wrapping_sub(self, rhs: Self) -> Self { self.wrapping_sub(rhs) } } //////////////////////////////////////////////////////////////////////////////// /// Floating point numbers. pub trait Float: Copy + PartialOrd + Zero + One + Two + Neg + Add + Sub + Div + Mul { /// Get the square root. fn sqrt(self) -> Self; } impl Float for f32 { fn sqrt(self) -> Self { f32::sqrt(self) } } impl Float for f64 { fn sqrt(self) -> Self { f64::sqrt(self) } } /// Adds convenience methods to `f32` and `f64`. pub trait FloatExt { /// Rounds to the nearest integer towards zero, /// unless the provided value is already an integer. /// /// It is to `floor` what `expand` is to `ceil`. /// /// # Examples /// /// ``` /// use rustik::math::traits::FloatExt; /// /// let f = 3.7_f64; /// let g = 3.0_f64; /// let h = -3.7_f64; /// let i = -5.9_f32; /// /// assert_eq!(f.truncate(), 3.0); /// assert_eq!(g.truncate(), 3.0); /// assert_eq!(h.truncate(), -3.0); /// assert_eq!(i.truncate(), -5.0); /// ``` fn truncate(&self) -> T; /// Rounds to the nearest integer away from zero, /// unless the provided value is already an integer. /// /// It is to `ceil` what `truncate` is to `floor`. /// /// # Examples /// /// ``` /// use rustik::math::traits::FloatExt; /// /// let f = 3.7_f64; /// let g = 3.0_f64; /// let h = -3.7_f64; /// let i = -5.1_f32; /// /// assert_eq!(f.expand(), 4.0); /// assert_eq!(g.expand(), 3.0); /// assert_eq!(h.expand(), -4.0); /// assert_eq!(i.expand(), -6.0); /// ``` fn expand(&self) -> T; } impl FloatExt for f64 { #[inline] fn expand(&self) -> f64 { self.abs().ceil().copysign(*self) } #[inline] fn truncate(&self) -> Self { self.abs().floor().copysign(*self) } } impl FloatExt for f32 { #[inline] fn expand(&self) -> f32 { self.abs().ceil().copysign(*self) } #[inline] fn truncate(&self) -> Self { self.abs().floor().copysign(*self) } } /// Approximate equality. pub trait ApproxEq { type Epsilon: Copy + PartialOrd; /// Checks if `|x - y| <= epsilon`. fn approx_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool; /// Checks if `|x - y| > epsilon`. fn approx_neq(&self, other: &Self, epsilon: Self::Epsilon) -> bool { !self.approx_eq(other, epsilon) } } impl ApproxEq for f32 { type Epsilon = f32; fn approx_eq(&self, other: &Self, epsilon: f32) -> bool { (self - other).abs() <= epsilon } } impl ApproxEq for f64 { type Epsilon = f64; fn approx_eq(&self, other: &Self, epsilon: f64) -> bool { (self - other).abs() <= epsilon } } #[cfg(test)] mod test { use super::*; use crate::math::macros::*; #[test] fn test_approx_eq() { assert_approx_eq!(1.0, 1.0, 0.0001); assert_approx_eq!(1.0, 1.001, 0.01); assert_approx_neq!(1.0, 1.001, 0.00000001); } #[test] fn test_integer_wrapping() { let max_u32 = u32::MAX; assert_eq!(max_u32.wrapping_add(1), 0); assert_eq!(0_u32.wrapping_sub(1), max_u32); let max_i32 = i32::MAX; assert_eq!(max_i32.wrapping_add(1), i32::MIN); assert_eq!(i32::MIN.wrapping_sub(1), i32::MAX); } #[test] fn test_integer_conversions() { assert_eq!(42_u32.as_u64(), 42_u64); assert_eq!(42_i32.as_u64(), 42_u64); assert_eq!(42_u64.as_u64(), 42_u64); assert_eq!(42_usize.as_u64(), 42_u64); } }