Skip to content

Commit cd00f11

Browse files
committed
fixed Beat add & sub not reduced
1 parent a53c46f commit cd00f11

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

phichain-chart/src/beat.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ impl Hash for Beat {
8484

8585
impl Beat {
8686
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();
9090
}
9191

9292
pub fn reduced(&self) -> Self {
@@ -207,31 +207,27 @@ impl Sub for Beat {
207207
type Output = Self;
208208

209209
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()
211211
}
212212
}
213213

214214
impl SubAssign for Beat {
215215
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;
219217
}
220218
}
221219

222220
impl Add for Beat {
223221
type Output = Self;
224222

225223
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()
227225
}
228226
}
229227

230228
impl AddAssign for Beat {
231229
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
235231
}
236232
}
237233

@@ -346,6 +342,7 @@ mod tests {
346342
let mut beat = beat!(1, 3, 2);
347343
beat.reduce();
348344
assert_eq!(beat, beat!(2, 1, 2));
345+
// panic!();
349346
}
350347

351348
#[test]
@@ -355,6 +352,13 @@ mod tests {
355352
assert_eq!(reduced, beat!(2, 1, 2));
356353
}
357354

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+
358362
#[test]
359363
fn test_from_f32() {
360364
let beat: Beat = 1.5f32.into();

0 commit comments

Comments
 (0)