Skip to content

Commit 6fda7b2

Browse files
committed
Implement ApproxEq for gradient::Range
1 parent 04a1548 commit 6fda7b2

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

src/gradient.rs

+52
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use num::{Float, One, Zero, NumCast};
44
use std::cmp::max;
5+
use approx::ApproxEq;
56

67
use flt;
78

@@ -276,6 +277,57 @@ impl<T: Float> From<::std::ops::RangeFull> for Range<T> {
276277
}
277278
}
278279

280+
impl<T> ApproxEq for Range<T> where
281+
T: ApproxEq + Float,
282+
T::Epsilon: Copy,
283+
{
284+
type Epsilon = T::Epsilon;
285+
286+
fn default_epsilon() -> Self::Epsilon {
287+
T::default_epsilon()
288+
}
289+
290+
fn default_max_relative() -> Self::Epsilon {
291+
T::default_max_relative()
292+
}
293+
294+
fn default_max_ulps() -> u32 {
295+
T::default_max_ulps()
296+
}
297+
298+
fn relative_eq(&self, other: &Range<T>, epsilon: Self::Epsilon, max_relative: Self::Epsilon) -> bool {
299+
let from = match (self.from, other.from) {
300+
(Some(s), Some(o)) => s.relative_eq(&o, epsilon, max_relative),
301+
(None, None) => true,
302+
_ => false,
303+
};
304+
305+
let to = match (self.to, other.to) {
306+
(Some(s), Some(o)) => s.relative_eq(&o, epsilon, max_relative),
307+
(None, None) => true,
308+
_ => false,
309+
};
310+
311+
from && to
312+
}
313+
314+
fn ulps_eq(&self, other: &Range<T>, epsilon: Self::Epsilon, max_ulps: u32) -> bool{
315+
let from = match (self.from, other.from) {
316+
(Some(s), Some(o)) => s.ulps_eq(&o, epsilon, max_ulps),
317+
(None, None) => true,
318+
_ => false,
319+
};
320+
321+
let to = match (self.to, other.to) {
322+
(Some(s), Some(o)) => s.ulps_eq(&o, epsilon, max_ulps),
323+
(None, None) => true,
324+
_ => false,
325+
};
326+
327+
from && to
328+
}
329+
}
330+
279331
#[derive(Clone)]
280332
enum MaybeSlice<'a, C: Mix + Clone + 'a> {
281333
NotSlice(&'a Gradient<C>),

0 commit comments

Comments
 (0)