algebra/cmp.rs
1//! Order and equivalence relations.
2
3// Copyright 2013-2014 The Algebra Developers.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17/// A type with an approximate equivalence relation.
18pub trait ApproxEq {
19 /// The epsilon type used measure an error.
20 type Eps: Sized;
21
22 /// The default epsilon value to use in `ApproxEq::approx_eq`.
23 fn default_epsilon() -> Self::Eps;
24
25 /// Compare `a` and `b` for approximate equality using the specified
26 /// epsilon value.
27 fn approx_eq_eps(&self, b: &Self, epsilon: &Self::Eps) -> bool;
28
29 /// Compare `a` and `b` for approximate equality using the default
30 /// epsilon value returned by `ApproxEq::default_epsilon`.
31 #[inline]
32 fn approx_eq(&self, b: &Self) -> bool {
33 Self::approx_eq_eps(self, b, &Self::default_epsilon())
34 }
35}
36
37impl_approx_eq!(0; u8, u16, u32, u64, i8, i16, i32, i64,);
38impl_approx_eq!(1.0e-6; f32, f64,);