|
@@ -10,8 +10,15 @@ PartialEq {
|
|
/// Function returning the whole number in the requested type.
|
|
/// Function returning the whole number in the requested type.
|
|
fn whole(value: u8) -> Self;
|
|
fn whole(value: u8) -> Self;
|
|
|
|
|
|
|
|
+ /// Default tolerance for comparisons.
|
|
|
|
+ fn epsilon() -> Self;
|
|
|
|
+
|
|
/// Calculate the absolute value.
|
|
/// Calculate the absolute value.
|
|
fn abs(self) -> Self;
|
|
fn abs(self) -> Self;
|
|
|
|
+
|
|
|
|
+ /// Check for equality.
|
|
|
|
+ /// For integrals, the tolerance is ignored.
|
|
|
|
+ fn is_equal_to(self, other: &Self, tolerance: Self) -> bool;
|
|
}
|
|
}
|
|
|
|
|
|
impl Numeric for f32 {
|
|
impl Numeric for f32 {
|
|
@@ -19,9 +26,17 @@ impl Numeric for f32 {
|
|
value as Self
|
|
value as Self
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ fn epsilon() -> Self {
|
|
|
|
+ 1e-9
|
|
|
|
+ }
|
|
|
|
+
|
|
fn abs(self) -> Self {
|
|
fn abs(self) -> Self {
|
|
self.abs()
|
|
self.abs()
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ fn is_equal_to(self, other: &Self, tolerance: Self) -> bool {
|
|
|
|
+ (self - *other).abs() <= tolerance
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
impl Numeric for f64 {
|
|
impl Numeric for f64 {
|
|
@@ -29,9 +44,17 @@ impl Numeric for f64 {
|
|
value as Self
|
|
value as Self
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ fn epsilon() -> Self {
|
|
|
|
+ 1e-9
|
|
|
|
+ }
|
|
|
|
+
|
|
fn abs(self) -> Self {
|
|
fn abs(self) -> Self {
|
|
self.abs()
|
|
self.abs()
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ fn is_equal_to(self, other: &Self, tolerance: Self) -> bool {
|
|
|
|
+ (self - *other).abs() <= tolerance
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
impl Numeric for i8 {
|
|
impl Numeric for i8 {
|
|
@@ -39,9 +62,18 @@ impl Numeric for i8 {
|
|
value as Self
|
|
value as Self
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ fn epsilon() -> Self {
|
|
|
|
+ 0
|
|
|
|
+ }
|
|
|
|
+
|
|
fn abs(self) -> Self {
|
|
fn abs(self) -> Self {
|
|
self.abs()
|
|
self.abs()
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ fn is_equal_to(self, other: &Self, tolerance: Self) -> bool {
|
|
|
|
+ debug_assert_eq!(tolerance, 0);
|
|
|
|
+ self == *other
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
impl Numeric for i16 {
|
|
impl Numeric for i16 {
|
|
@@ -49,9 +81,18 @@ impl Numeric for i16 {
|
|
value as Self
|
|
value as Self
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ fn epsilon() -> Self {
|
|
|
|
+ 0
|
|
|
|
+ }
|
|
|
|
+
|
|
fn abs(self) -> Self {
|
|
fn abs(self) -> Self {
|
|
self.abs()
|
|
self.abs()
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ fn is_equal_to(self, other: &Self, tolerance: Self) -> bool {
|
|
|
|
+ debug_assert_eq!(tolerance, 0);
|
|
|
|
+ self == *other
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
impl Numeric for i32 {
|
|
impl Numeric for i32 {
|
|
@@ -59,9 +100,18 @@ impl Numeric for i32 {
|
|
value as Self
|
|
value as Self
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ fn epsilon() -> Self {
|
|
|
|
+ 0
|
|
|
|
+ }
|
|
|
|
+
|
|
fn abs(self) -> Self {
|
|
fn abs(self) -> Self {
|
|
self.abs()
|
|
self.abs()
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ fn is_equal_to(self, other: &Self, tolerance: Self) -> bool {
|
|
|
|
+ debug_assert_eq!(tolerance, 0);
|
|
|
|
+ self == *other
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
impl Numeric for i64 {
|
|
impl Numeric for i64 {
|
|
@@ -69,9 +119,18 @@ impl Numeric for i64 {
|
|
value as Self
|
|
value as Self
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ fn epsilon() -> Self {
|
|
|
|
+ 0
|
|
|
|
+ }
|
|
|
|
+
|
|
fn abs(self) -> Self {
|
|
fn abs(self) -> Self {
|
|
self.abs()
|
|
self.abs()
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ fn is_equal_to(self, other: &Self, tolerance: Self) -> bool {
|
|
|
|
+ debug_assert_eq!(tolerance, 0);
|
|
|
|
+ self == *other
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
impl Numeric for i128 {
|
|
impl Numeric for i128 {
|
|
@@ -79,9 +138,18 @@ impl Numeric for i128 {
|
|
value as Self
|
|
value as Self
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ fn epsilon() -> Self {
|
|
|
|
+ 0
|
|
|
|
+ }
|
|
|
|
+
|
|
fn abs(self) -> Self {
|
|
fn abs(self) -> Self {
|
|
self.abs()
|
|
self.abs()
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ fn is_equal_to(self, other: &Self, tolerance: Self) -> bool {
|
|
|
|
+ debug_assert_eq!(tolerance, 0);
|
|
|
|
+ self == *other
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
impl Numeric for isize {
|
|
impl Numeric for isize {
|
|
@@ -89,9 +157,18 @@ impl Numeric for isize {
|
|
value as Self
|
|
value as Self
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ fn epsilon() -> Self {
|
|
|
|
+ 0
|
|
|
|
+ }
|
|
|
|
+
|
|
fn abs(self) -> Self {
|
|
fn abs(self) -> Self {
|
|
self.abs()
|
|
self.abs()
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ fn is_equal_to(self, other: &Self, tolerance: Self) -> bool {
|
|
|
|
+ debug_assert_eq!(tolerance, 0);
|
|
|
|
+ self == *other
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
impl Numeric for u8 {
|
|
impl Numeric for u8 {
|
|
@@ -99,9 +176,18 @@ impl Numeric for u8 {
|
|
value as Self
|
|
value as Self
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ fn epsilon() -> Self {
|
|
|
|
+ 0
|
|
|
|
+ }
|
|
|
|
+
|
|
fn abs(self) -> Self {
|
|
fn abs(self) -> Self {
|
|
self
|
|
self
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ fn is_equal_to(self, other: &Self, tolerance: Self) -> bool {
|
|
|
|
+ debug_assert_eq!(tolerance, 0);
|
|
|
|
+ self == *other
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
impl Numeric for u16 {
|
|
impl Numeric for u16 {
|
|
@@ -109,9 +195,18 @@ impl Numeric for u16 {
|
|
value as Self
|
|
value as Self
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ fn epsilon() -> Self {
|
|
|
|
+ 0
|
|
|
|
+ }
|
|
|
|
+
|
|
fn abs(self) -> Self {
|
|
fn abs(self) -> Self {
|
|
self
|
|
self
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ fn is_equal_to(self, other: &Self, tolerance: Self) -> bool {
|
|
|
|
+ debug_assert_eq!(tolerance, 0);
|
|
|
|
+ self == *other
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
impl Numeric for u32 {
|
|
impl Numeric for u32 {
|
|
@@ -119,9 +214,18 @@ impl Numeric for u32 {
|
|
value as Self
|
|
value as Self
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ fn epsilon() -> Self {
|
|
|
|
+ 0
|
|
|
|
+ }
|
|
|
|
+
|
|
fn abs(self) -> Self {
|
|
fn abs(self) -> Self {
|
|
self
|
|
self
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ fn is_equal_to(self, other: &Self, tolerance: Self) -> bool {
|
|
|
|
+ debug_assert_eq!(tolerance, 0);
|
|
|
|
+ self == *other
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
impl Numeric for u64 {
|
|
impl Numeric for u64 {
|
|
@@ -129,9 +233,18 @@ impl Numeric for u64 {
|
|
value as Self
|
|
value as Self
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ fn epsilon() -> Self {
|
|
|
|
+ 0
|
|
|
|
+ }
|
|
|
|
+
|
|
fn abs(self) -> Self {
|
|
fn abs(self) -> Self {
|
|
self
|
|
self
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ fn is_equal_to(self, other: &Self, tolerance: Self) -> bool {
|
|
|
|
+ debug_assert_eq!(tolerance, 0);
|
|
|
|
+ self == *other
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
impl Numeric for u128 {
|
|
impl Numeric for u128 {
|
|
@@ -139,9 +252,18 @@ impl Numeric for u128 {
|
|
value as Self
|
|
value as Self
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ fn epsilon() -> Self {
|
|
|
|
+ 0
|
|
|
|
+ }
|
|
|
|
+
|
|
fn abs(self) -> Self {
|
|
fn abs(self) -> Self {
|
|
self
|
|
self
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ fn is_equal_to(self, other: &Self, tolerance: Self) -> bool {
|
|
|
|
+ debug_assert_eq!(tolerance, 0);
|
|
|
|
+ self == *other
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
impl Numeric for usize {
|
|
impl Numeric for usize {
|
|
@@ -149,9 +271,18 @@ impl Numeric for usize {
|
|
value as Self
|
|
value as Self
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ fn epsilon() -> Self {
|
|
|
|
+ 0
|
|
|
|
+ }
|
|
|
|
+
|
|
fn abs(self) -> Self {
|
|
fn abs(self) -> Self {
|
|
self
|
|
self
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ fn is_equal_to(self, other: &Self, tolerance: Self) -> bool {
|
|
|
|
+ debug_assert_eq!(tolerance, 0);
|
|
|
|
+ self == *other
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
#[cfg(test)]
|
|
#[cfg(test)]
|