@@ -84,9 +84,9 @@ impl Hash for Beat {
84
84
85
85
impl Beat {
86
86
pub fn reduce ( & mut self ) {
87
- let fraction = self . 1 . reduced ( ) ;
88
- self . 0 += fraction . trunc ( ) . numer ( ) ;
89
- self . 1 = fraction . fract ( ) ;
87
+ let value = Rational32 :: from_integer ( self . 0 ) + self . 1 ;
88
+ self . 0 = value . to_integer ( ) ;
89
+ self . 1 = value . fract ( ) ;
90
90
}
91
91
92
92
pub fn reduced ( & self ) -> Self {
@@ -207,31 +207,27 @@ impl Sub for Beat {
207
207
type Output = Self ;
208
208
209
209
fn sub ( self , rhs : Self ) -> Self :: Output {
210
- Self ( self . 0 - rhs. 0 , self . 1 - rhs. 1 , self . 2 - rhs. 2 )
210
+ Self ( self . 0 - rhs. 0 , self . 1 - rhs. 1 , self . 2 - rhs. 2 ) . reduced ( )
211
211
}
212
212
}
213
213
214
214
impl SubAssign for Beat {
215
215
fn sub_assign ( & mut self , rhs : Self ) {
216
- self . 0 -= rhs. 0 ;
217
- self . 1 -= rhs. 1 ;
218
- self . 2 -= rhs. 2 ;
216
+ * self = * self - rhs;
219
217
}
220
218
}
221
219
222
220
impl Add for Beat {
223
221
type Output = Self ;
224
222
225
223
fn add ( self , rhs : Self ) -> Self :: Output {
226
- Self ( self . 0 + rhs. 0 , self . 1 + rhs. 1 , self . 2 + rhs. 2 )
224
+ Self ( self . 0 + rhs. 0 , self . 1 + rhs. 1 , self . 2 + rhs. 2 ) . reduced ( )
227
225
}
228
226
}
229
227
230
228
impl AddAssign for Beat {
231
229
fn add_assign ( & mut self , rhs : Self ) {
232
- self . 0 += rhs. 0 ;
233
- self . 1 += rhs. 1 ;
234
- self . 2 += rhs. 2 ;
230
+ * self = * self + rhs
235
231
}
236
232
}
237
233
@@ -346,6 +342,7 @@ mod tests {
346
342
let mut beat = beat ! ( 1 , 3 , 2 ) ;
347
343
beat. reduce ( ) ;
348
344
assert_eq ! ( beat, beat!( 2 , 1 , 2 ) ) ;
345
+ // panic!();
349
346
}
350
347
351
348
#[ test]
@@ -355,6 +352,13 @@ mod tests {
355
352
assert_eq ! ( reduced, beat!( 2 , 1 , 2 ) ) ;
356
353
}
357
354
355
+ #[ test]
356
+ fn test_reduced_negative ( ) {
357
+ let beat = beat ! ( 1 , -1 , 2 ) ;
358
+ let reduced = beat. reduced ( ) ;
359
+ assert_eq ! ( reduced, beat!( 0 , 1 , 2 ) ) ;
360
+ }
361
+
358
362
#[ test]
359
363
fn test_from_f32 ( ) {
360
364
let beat: Beat = 1.5f32 . into ( ) ;
0 commit comments