reduce redundant code, add tests for reduce and gcd
parent
08866f1331
commit
7f4c3cda4d
|
@ -87,7 +87,7 @@ pub fn (f1 Fraction) gcd() i64 {
|
||||||
|
|
||||||
// Fraction method which reduces the fraction
|
// Fraction method which reduces the fraction
|
||||||
pub fn (f1 Fraction) reduce() Fraction {
|
pub fn (f1 Fraction) reduce() Fraction {
|
||||||
cf := gcd(f1.n, f1.d)
|
cf := f1.gcd()
|
||||||
return Fraction{f1.n / cf, f1.d / cf}
|
return Fraction{f1.n / cf, f1.d / cf}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -132,4 +132,10 @@ fn test_fraction_equals() {
|
||||||
f1 = math.fraction(1,2)
|
f1 = math.fraction(1,2)
|
||||||
f2 = math.fraction(3,4)
|
f2 = math.fraction(3,4)
|
||||||
assert !f1.equals(f2)
|
assert !f1.equals(f2)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_gcd_and_reduce(){
|
||||||
|
mut f := math.fraction(3, 9)
|
||||||
|
assert f.gcd() == 3
|
||||||
|
assert f.reduce().equals(math.fraction(1, 3))
|
||||||
}
|
}
|
Loading…
Reference in New Issue