oxiphysics_core/interval/
interval_traits.rs1#[allow(unused_imports)]
17use super::functions::*;
18use std::ops::{Add, Div, Mul, Neg, Sub};
19
20use super::types::Interval;
21
22impl Neg for Interval {
23 type Output = Self;
24 fn neg(self) -> Self {
25 Self {
26 lo: -self.hi,
27 hi: -self.lo,
28 }
29 }
30}
31
32impl Add for Interval {
33 type Output = Self;
34 fn add(self, rhs: Self) -> Self {
35 Self {
36 lo: self.lo + rhs.lo,
37 hi: self.hi + rhs.hi,
38 }
39 }
40}
41
42impl Sub for Interval {
43 type Output = Self;
44 fn sub(self, rhs: Self) -> Self {
45 Self {
46 lo: self.lo - rhs.hi,
47 hi: self.hi - rhs.lo,
48 }
49 }
50}
51
52impl Mul for Interval {
53 type Output = Self;
54 fn mul(self, rhs: Self) -> Self {
56 let c1 = self.lo * rhs.lo;
57 let c2 = self.lo * rhs.hi;
58 let c3 = self.hi * rhs.lo;
59 let c4 = self.hi * rhs.hi;
60 Self {
61 lo: c1.min(c2).min(c3).min(c4),
62 hi: c1.max(c2).max(c3).max(c4),
63 }
64 }
65}
66
67impl Div for Interval {
68 type Output = Self;
69 #[allow(clippy::suspicious_arithmetic_impl)]
71 fn div(self, rhs: Self) -> Self {
72 self * rhs.reciprocal()
73 }
74}
75
76impl Mul<f64> for Interval {
78 type Output = Interval;
79 fn mul(self, s: f64) -> Interval {
80 if s >= 0.0 {
81 Interval {
82 lo: self.lo * s,
83 hi: self.hi * s,
84 }
85 } else {
86 Interval {
87 lo: self.hi * s,
88 hi: self.lo * s,
89 }
90 }
91 }
92}