44
55use std:: {
66 cmp:: Ordering ,
7- convert:: TryInto ,
87 fmt,
98 ops:: { Add , AddAssign , Div , DivAssign , Mul , MulAssign , Sub , SubAssign } ,
109 str:: FromStr ,
@@ -90,9 +89,9 @@ impl Add for Fraction {
9089 return other;
9190 }
9291
93- let ( self_mul, other_mul, den) = if self . den % other. den == 0 {
92+ let ( self_mul, other_mul, den) = if self . den . is_multiple_of ( other. den ) {
9493 ( 1 , self . den / other. den , self . den )
95- } else if other. den % self . den == 0 {
94+ } else if other. den . is_multiple_of ( self . den ) {
9695 ( other. den / self . den , 1 , other. den )
9796 } else {
9897 ( other. den , self . den , self . den * other. den )
@@ -117,9 +116,9 @@ impl Sub for Fraction {
117116 type Output = Fraction ;
118117
119118 fn sub ( self , other : Fraction ) -> Self :: Output {
120- let ( self_mul, other_mul, den) = if self . den % other. den == 0 {
119+ let ( self_mul, other_mul, den) = if self . den . is_multiple_of ( other. den ) {
121120 ( 1 , self . den / other. den , self . den )
122- } else if other. den % self . den == 0 {
121+ } else if other. den . is_multiple_of ( self . den ) {
123122 ( other. den / self . den , 1 , other. den )
124123 } else {
125124 ( other. den , self . den , self . den * other. den )
@@ -200,41 +199,41 @@ impl fmt::Display for Fraction {
200199}
201200
202201pub trait IsZero {
203- fn is_zero ( self ) -> bool ;
202+ fn is_zero ( & self ) -> bool ;
204203}
205204
206205impl IsZero for u8 {
207- fn is_zero ( self ) -> bool {
208- self == 0
206+ fn is_zero ( & self ) -> bool {
207+ * self == 0
209208 }
210209}
211210
212211impl IsZero for u16 {
213- fn is_zero ( self ) -> bool {
214- self == 0
212+ fn is_zero ( & self ) -> bool {
213+ * self == 0
215214 }
216215}
217216
218217impl IsZero for u32 {
219- fn is_zero ( self ) -> bool {
220- self == 0
218+ fn is_zero ( & self ) -> bool {
219+ * self == 0
221220 }
222221}
223222
224223impl IsZero for u64 {
225- fn is_zero ( self ) -> bool {
226- self == 0
224+ fn is_zero ( & self ) -> bool {
225+ * self == 0
227226 }
228227}
229228
230229impl IsZero for u128 {
231- fn is_zero ( self ) -> bool {
232- self == 0
230+ fn is_zero ( & self ) -> bool {
231+ * self == 0
233232 }
234233}
235234
236235impl IsZero for Fraction {
237- fn is_zero ( self ) -> bool {
236+ fn is_zero ( & self ) -> bool {
238237 self . num == 0 && self . den != 0
239238 }
240239}
@@ -353,6 +352,6 @@ mod tests {
353352 assert ! ( Fraction :: new( 50 , 25 ) > Fraction :: new( 99 , 50 ) ) ;
354353 assert ! ( Fraction :: new( 3 , 4 ) > Fraction :: new( 1 , 2 ) ) ;
355354 assert ! ( Fraction :: new( 1 , 3 ) > Fraction :: new( 1 , 4 ) ) ;
356- assert_eq ! ( false , Fraction :: new( 0 , 3 ) > Fraction :: new( 0 , 4 ) ) ;
355+ assert ! ( Fraction :: new( 0 , 3 ) <= Fraction :: new( 0 , 4 ) ) ;
357356 }
358357}
0 commit comments