@@ -14,24 +14,18 @@ pub struct ComplexExtended {
1414impl ComplexExtended {
1515 #[ inline]
1616 pub fn new ( mantissa : Complex < f64 > , exponent : i32 ) -> Self {
17- let mut temp = ComplexExtended {
17+ ComplexExtended {
1818 mantissa,
1919 exponent
20- } ;
21-
22- temp. reduce ( ) ;
23- temp
20+ }
2421 }
2522
2623 #[ inline]
2724 pub fn new2 ( re : f64 , im : f64 , exponent : i32 ) -> Self {
28- let mut temp = ComplexExtended {
25+ ComplexExtended {
2926 mantissa : Complex :: < f64 > :: new ( re, im) ,
3027 exponent
31- } ;
32-
33- temp. reduce ( ) ;
34- temp
28+ }
3529 }
3630
3731 #[ inline]
@@ -49,7 +43,7 @@ impl ComplexExtended {
4943
5044 #[ inline]
5145 pub fn to_float ( & self ) -> Complex < f64 > {
52- self . mantissa * 2 .0f64. powi ( self . exponent )
46+ self . mantissa * 1 .0f64. ldexp ( self . exponent )
5347 }
5448
5549 #[ inline]
@@ -71,36 +65,25 @@ impl ComplexExtended {
7165impl AddAssign for ComplexExtended {
7266 #[ inline]
7367 fn add_assign ( & mut self , other : Self ) {
74- if self . mantissa . re == 0.0 && self . mantissa . im == 0.0 {
75- self . mantissa = other. mantissa ;
76- self . exponent = other. exponent ;
77- } else if other. mantissa . re == 0.0 && other. mantissa . im == 0.0 {
78- return ;
68+ if self . exponent > other. exponent {
69+ self . mantissa += other. mantissa * 1.0f64 . ldexp ( other. exponent - self . exponent ) ;
7970 } else {
80- if self . exponent == other. exponent {
81- self . mantissa += other. mantissa ;
82- } else if self . exponent > other. exponent {
83- self . mantissa += other. mantissa / 2.0f64 . powi ( self . exponent - other. exponent ) ;
84- } else {
85- self . mantissa /= 2.0f64 . powi ( other. exponent - self . exponent ) ;
86- self . exponent = other. exponent ;
87- self . mantissa += other. mantissa ;
88- }
71+ self . mantissa *= 1.0f64 . ldexp ( self . exponent - other. exponent ) ;
72+ self . mantissa += other. mantissa ;
73+ self . exponent = other. exponent ;
8974 }
9075 }
9176}
9277
9378impl SubAssign for ComplexExtended {
9479 #[ inline]
9580 fn sub_assign ( & mut self , other : Self ) {
96- if self . exponent == other. exponent {
97- self . mantissa -= other. mantissa ;
98- } else if self . exponent > other. exponent {
99- self . mantissa -= other. mantissa / 2.0f64 . powi ( self . exponent - other. exponent ) ;
81+ if self . exponent > other. exponent {
82+ self . mantissa -= other. mantissa * 1.0f64 . ldexp ( other. exponent - self . exponent ) ;
10083 } else {
101- self . mantissa /= 2.0f64 . powi ( other. exponent - self . exponent ) ;
102- self . exponent = other. exponent ;
84+ self . mantissa *= 1.0f64 . ldexp ( self . exponent - other. exponent ) ;
10385 self . mantissa -= other. mantissa ;
86+ self . exponent = other. exponent ;
10487 }
10588 }
10689}
@@ -125,7 +108,6 @@ impl DivAssign for ComplexExtended {
125108 fn div_assign ( & mut self , other : Self ) {
126109 self . mantissa /= other. mantissa ;
127110 self . exponent -= other. exponent ;
128- self . reduce ( ) ;
129111 }
130112}
131113
@@ -134,19 +116,10 @@ impl Add<ComplexExtended> for ComplexExtended {
134116
135117 #[ inline]
136118 fn add ( self , other : Self ) -> Self :: Output {
137- if self . mantissa . re == 0.0 && self . mantissa . im == 0.0 {
138- other
139- } else if other. mantissa . re == 0.0 && other. mantissa . im == 0.0 {
140- self
119+ if self . exponent > other. exponent {
120+ ComplexExtended :: new ( self . mantissa + other. mantissa * 1.0f64 . ldexp ( other. exponent - self . exponent ) , self . exponent )
141121 } else {
142- let ( new_mantissa, new_exponent) = if self . exponent == other. exponent {
143- ( self . mantissa + other. mantissa , self . exponent )
144- } else if self . exponent > other. exponent {
145- ( self . mantissa + other. mantissa / 2.0f64 . powi ( self . exponent - other. exponent ) , self . exponent )
146- } else {
147- ( other. mantissa + self . mantissa / 2.0f64 . powi ( other. exponent - self . exponent ) , other. exponent )
148- } ;
149- ComplexExtended :: new ( new_mantissa, new_exponent)
122+ ComplexExtended :: new ( other. mantissa + self . mantissa * 1.0f64 . ldexp ( self . exponent - other. exponent ) , other. exponent )
150123 }
151124 }
152125}
@@ -156,19 +129,10 @@ impl Sub<ComplexExtended> for ComplexExtended {
156129
157130 #[ inline]
158131 fn sub ( self , other : Self ) -> Self :: Output {
159- if self . mantissa . re == 0.0 && self . mantissa . im == 0.0 {
160- ComplexExtended :: new ( -other. mantissa , other. exponent )
161- } else if other. mantissa . re == 0.0 && other. mantissa . im == 0.0 {
162- self
132+ if self . exponent > other. exponent {
133+ ComplexExtended :: new ( self . mantissa - other. mantissa * 1.0f64 . ldexp ( other. exponent - self . exponent ) , self . exponent )
163134 } else {
164- let ( new_mantissa, new_exponent) = if self . exponent == other. exponent {
165- ( self . mantissa - other. mantissa , self . exponent )
166- } else if self . exponent > other. exponent {
167- ( self . mantissa - other. mantissa / 2.0f64 . powi ( self . exponent - other. exponent ) , self . exponent )
168- } else {
169- ( -1.0 * other. mantissa + self . mantissa / 2.0f64 . powi ( other. exponent - self . exponent ) , other. exponent )
170- } ;
171- ComplexExtended :: new ( new_mantissa, new_exponent)
135+ ComplexExtended :: new ( other. mantissa - self . mantissa * 1.0f64 . ldexp ( self . exponent - other. exponent ) , other. exponent )
172136 }
173137 }
174138}
0 commit comments