fraction: simplify variable names
parent
2e0b9de31c
commit
3270545953
|
@ -164,42 +164,42 @@ pub fn (f1 Fraction) divide(f2 Fraction) Fraction {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fraction negate method
|
// Fraction negate method
|
||||||
pub fn (f1 Fraction) negate() Fraction {
|
pub fn (f Fraction) negate() Fraction {
|
||||||
return Fraction{
|
return Fraction{
|
||||||
n: -f1.n
|
n: -f.n
|
||||||
d: f1.d
|
d: f.d
|
||||||
is_reduced: f1.is_reduced
|
is_reduced: f.is_reduced
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fraction reciprocal method
|
// Fraction reciprocal method
|
||||||
pub fn (f1 Fraction) reciprocal() Fraction {
|
pub fn (f Fraction) reciprocal() Fraction {
|
||||||
if f1.n == 0 {
|
if f.n == 0 {
|
||||||
panic('Denominator cannot be zero')
|
panic('Denominator cannot be zero')
|
||||||
}
|
}
|
||||||
return Fraction{
|
return Fraction{
|
||||||
n: f1.d
|
n: f.d
|
||||||
d: f1.n
|
d: f.n
|
||||||
is_reduced: f1.is_reduced
|
is_reduced: f.is_reduced
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fraction method which reduces the fraction
|
// Fraction method which reduces the fraction
|
||||||
pub fn (f1 Fraction) reduce() Fraction {
|
pub fn (f Fraction) reduce() Fraction {
|
||||||
if f1.is_reduced {
|
if f.is_reduced {
|
||||||
return f1
|
return f
|
||||||
}
|
}
|
||||||
cf := math.gcd(f1.n, f1.d)
|
cf := math.gcd(f.n, f.d)
|
||||||
return Fraction{
|
return Fraction{
|
||||||
n: f1.n / cf
|
n: f.n / cf
|
||||||
d: f1.d / cf
|
d: f.d / cf
|
||||||
is_reduced: true
|
is_reduced: true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// f64 converts the Fraction to 64-bit floating point
|
// f64 converts the Fraction to 64-bit floating point
|
||||||
pub fn (f1 Fraction) f64() f64 {
|
pub fn (f Fraction) f64() f64 {
|
||||||
return f64(f1.n) / f64(f1.d)
|
return f64(f.n) / f64(f.d)
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
Loading…
Reference in New Issue