1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
use std::ops; use std::fmt; use super::zprimitive::Z; pub struct I(pub f64); impl ops::Add for I { type Output = I; fn add(self, rhs: I) -> I { I(self.0 + rhs.0) } } impl ops::Add<f64> for I { type Output = Z; fn add(self, rhs: f64) -> Z { Z(rhs, self.0) } } impl ops::Add<I> for f64 { type Output = Z; fn add(self, rhs: I) -> Z { Z(self, rhs.0) } } impl ops::Sub for I { type Output = I; fn sub(self, rhs: I) -> I { I(self.0 - rhs.0) } } impl ops::Sub<f64> for I{ type Output = Z; fn sub(self, rhs: f64) -> Z { Z(-rhs, self.0) } } impl ops::Sub<I> for f64 { type Output = Z; fn sub(self, rhs: I) -> Z { Z(self, -rhs.0) } } impl ops::Mul for I { type Output = f64; fn mul(self, rhs: I) -> f64 { -(self.0 * rhs.0) } } impl ops::Mul<f64> for I { type Output = I; fn mul(self, rhs: f64) -> I { I(self.0 * rhs) } } impl ops::Mul<I> for f64 { type Output = I; fn mul(self, rhs: I) -> I { I(self * rhs.0) } } impl ops::Div for I { type Output = f64; fn div(self, rhs: I) -> f64 { self.0 / rhs.0 } } impl ops::Div<f64> for I { type Output = I; fn div(self, rhs: f64) -> I { I(self.0 / rhs) } } impl ops::Div<I> for f64 { type Output = I; fn div(self, rhs: I) -> I { I(self / rhs.0) } } impl fmt::Display for I { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", if self.0 != 0.0 {format!("{}i", self.0)} else {"".to_string()}) } }