math: moved fraction to math/fraction
parent
62e6e03492
commit
d58174e77f
|
@ -1,141 +1,141 @@
|
||||||
import math
|
import math.fractions as fractions
|
||||||
|
|
||||||
// Results are verified using https://www.calculatorsoup.com/calculators/math/fractions.php
|
// Results are verified using https://www.calculatorsoup.com/calculators/math/fractions.php
|
||||||
|
|
||||||
fn test_fraction_creation() {
|
fn test_fraction_creation() {
|
||||||
mut f1 := math.fraction(4,8)
|
mut f1 := fractions.fraction(4,8)
|
||||||
assert f1.f64() == 0.5
|
assert f1.f64() == 0.5
|
||||||
assert f1.str().eq('4/8')
|
assert f1.str().eq('4/8')
|
||||||
f1 = math.fraction(10,5)
|
f1 = fractions.fraction(10,5)
|
||||||
assert f1.f64() == 2.0
|
assert f1.f64() == 2.0
|
||||||
assert f1.str().eq('10/5')
|
assert f1.str().eq('10/5')
|
||||||
f1 = math.fraction(9,3)
|
f1 = fractions.fraction(9,3)
|
||||||
assert f1.f64() == 3.0
|
assert f1.f64() == 3.0
|
||||||
assert f1.str().eq('9/3')
|
assert f1.str().eq('9/3')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_fraction_add() {
|
fn test_fraction_add() {
|
||||||
mut f1 := math.fraction(4,8)
|
mut f1 := fractions.fraction(4,8)
|
||||||
mut f2 := math.fraction(5,10)
|
mut f2 := fractions.fraction(5,10)
|
||||||
mut sum := f1 + f2
|
mut sum := f1 + f2
|
||||||
assert sum.f64() == 1.0
|
assert sum.f64() == 1.0
|
||||||
assert sum.str().eq('80/80')
|
assert sum.str().eq('80/80')
|
||||||
f1 = math.fraction(5,5)
|
f1 = fractions.fraction(5,5)
|
||||||
f2 = math.fraction(8,8)
|
f2 = fractions.fraction(8,8)
|
||||||
sum = f1 + f2
|
sum = f1 + f2
|
||||||
assert sum.f64() == 2.0
|
assert sum.f64() == 2.0
|
||||||
assert sum.str().eq('80/40')
|
assert sum.str().eq('80/40')
|
||||||
f1 = math.fraction(9,3)
|
f1 = fractions.fraction(9,3)
|
||||||
f2 = math.fraction(1,3)
|
f2 = fractions.fraction(1,3)
|
||||||
sum = f1 + f2
|
sum = f1 + f2
|
||||||
println(sum.f64())
|
println(sum.f64())
|
||||||
assert sum.str().eq('10/3')
|
assert sum.str().eq('10/3')
|
||||||
f1 = math.fraction(3,7)
|
f1 = fractions.fraction(3,7)
|
||||||
f2 = math.fraction(1,4)
|
f2 = fractions.fraction(1,4)
|
||||||
sum = f1 + f2
|
sum = f1 + f2
|
||||||
println(sum.f64())
|
println(sum.f64())
|
||||||
assert sum.str().eq('19/28')
|
assert sum.str().eq('19/28')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_fraction_subtract() {
|
fn test_fraction_subtract() {
|
||||||
mut f1 := math.fraction(4,8)
|
mut f1 := fractions.fraction(4,8)
|
||||||
mut f2 := math.fraction(5,10)
|
mut f2 := fractions.fraction(5,10)
|
||||||
mut diff := f2 - f1
|
mut diff := f2 - f1
|
||||||
assert diff.f64() == 0
|
assert diff.f64() == 0
|
||||||
assert diff.str().eq('0/80')
|
assert diff.str().eq('0/80')
|
||||||
f1 = math.fraction(5,5)
|
f1 = fractions.fraction(5,5)
|
||||||
f2 = math.fraction(8,8)
|
f2 = fractions.fraction(8,8)
|
||||||
diff = f2 - f1
|
diff = f2 - f1
|
||||||
assert diff.f64() == 0
|
assert diff.f64() == 0
|
||||||
assert diff.str().eq('0/40')
|
assert diff.str().eq('0/40')
|
||||||
f1 = math.fraction(9,3)
|
f1 = fractions.fraction(9,3)
|
||||||
f2 = math.fraction(1,3)
|
f2 = fractions.fraction(1,3)
|
||||||
diff = f1 - f2
|
diff = f1 - f2
|
||||||
println(diff.f64())
|
println(diff.f64())
|
||||||
assert diff.str().eq('8/3')
|
assert diff.str().eq('8/3')
|
||||||
f1 = math.fraction(3,7)
|
f1 = fractions.fraction(3,7)
|
||||||
f2 = math.fraction(1,4)
|
f2 = fractions.fraction(1,4)
|
||||||
diff = f1 - f2
|
diff = f1 - f2
|
||||||
println(diff.f64())
|
println(diff.f64())
|
||||||
assert diff.str().eq('5/28')
|
assert diff.str().eq('5/28')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_fraction_multiply() {
|
fn test_fraction_multiply() {
|
||||||
mut f1 := math.fraction(4,8)
|
mut f1 := fractions.fraction(4,8)
|
||||||
mut f2 := math.fraction(5,10)
|
mut f2 := fractions.fraction(5,10)
|
||||||
mut product := f1.multiply(f2)
|
mut product := f1.multiply(f2)
|
||||||
assert product.f64() == 0.25
|
assert product.f64() == 0.25
|
||||||
assert product.str().eq('20/80')
|
assert product.str().eq('20/80')
|
||||||
f1 = math.fraction(5,5)
|
f1 = fractions.fraction(5,5)
|
||||||
f2 = math.fraction(8,8)
|
f2 = fractions.fraction(8,8)
|
||||||
product = f1.multiply(f2)
|
product = f1.multiply(f2)
|
||||||
assert product.f64() == 1.0
|
assert product.f64() == 1.0
|
||||||
assert product.str().eq('40/40')
|
assert product.str().eq('40/40')
|
||||||
f1 = math.fraction(9,3)
|
f1 = fractions.fraction(9,3)
|
||||||
f2 = math.fraction(1,3)
|
f2 = fractions.fraction(1,3)
|
||||||
product = f1.multiply(f2)
|
product = f1.multiply(f2)
|
||||||
assert product.f64() == 1.0
|
assert product.f64() == 1.0
|
||||||
assert product.str().eq('9/9')
|
assert product.str().eq('9/9')
|
||||||
f1 = math.fraction(3,7)
|
f1 = fractions.fraction(3,7)
|
||||||
f2 = math.fraction(1,4)
|
f2 = fractions.fraction(1,4)
|
||||||
product = f1.multiply(f2)
|
product = f1.multiply(f2)
|
||||||
println(product.f64())
|
println(product.f64())
|
||||||
assert product.str().eq('3/28')
|
assert product.str().eq('3/28')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_fraction_divide() {
|
fn test_fraction_divide() {
|
||||||
mut f1 := math.fraction(4,8)
|
mut f1 := fractions.fraction(4,8)
|
||||||
mut f2 := math.fraction(5,10)
|
mut f2 := fractions.fraction(5,10)
|
||||||
mut re := f1.divide(f2)
|
mut re := f1.divide(f2)
|
||||||
assert re.f64() == 1.0
|
assert re.f64() == 1.0
|
||||||
assert re.str().eq('40/40')
|
assert re.str().eq('40/40')
|
||||||
f1 = math.fraction(5,5)
|
f1 = fractions.fraction(5,5)
|
||||||
f2 = math.fraction(8,8)
|
f2 = fractions.fraction(8,8)
|
||||||
re = f1.divide(f2)
|
re = f1.divide(f2)
|
||||||
assert re.f64() == 1.0
|
assert re.f64() == 1.0
|
||||||
assert re.str().eq('40/40')
|
assert re.str().eq('40/40')
|
||||||
f1 = math.fraction(9,3)
|
f1 = fractions.fraction(9,3)
|
||||||
f2 = math.fraction(1,3)
|
f2 = fractions.fraction(1,3)
|
||||||
re = f1.divide(f2)
|
re = f1.divide(f2)
|
||||||
assert re.f64() == 9.0
|
assert re.f64() == 9.0
|
||||||
assert re.str().eq('27/3')
|
assert re.str().eq('27/3')
|
||||||
f1 = math.fraction(3,7)
|
f1 = fractions.fraction(3,7)
|
||||||
f2 = math.fraction(1,4)
|
f2 = fractions.fraction(1,4)
|
||||||
re = f1.divide(f2)
|
re = f1.divide(f2)
|
||||||
println(re.f64())
|
println(re.f64())
|
||||||
assert re.str().eq('12/7')
|
assert re.str().eq('12/7')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_fraction_reciprocal() {
|
fn test_fraction_reciprocal() {
|
||||||
mut f1 := math.fraction(4,8)
|
mut f1 := fractions.fraction(4,8)
|
||||||
assert f1.reciprocal().str().eq('8/4')
|
assert f1.reciprocal().str().eq('8/4')
|
||||||
f1 = math.fraction(5,10)
|
f1 = fractions.fraction(5,10)
|
||||||
assert f1.reciprocal().str().eq('10/5')
|
assert f1.reciprocal().str().eq('10/5')
|
||||||
f1 = math.fraction(5,5)
|
f1 = fractions.fraction(5,5)
|
||||||
assert f1.reciprocal().str().eq('5/5')
|
assert f1.reciprocal().str().eq('5/5')
|
||||||
f1 = math.fraction(8,8)
|
f1 = fractions.fraction(8,8)
|
||||||
assert f1.reciprocal().str().eq('8/8')
|
assert f1.reciprocal().str().eq('8/8')
|
||||||
f1 = math.fraction(9,3)
|
f1 = fractions.fraction(9,3)
|
||||||
assert f1.reciprocal().str().eq('3/9')
|
assert f1.reciprocal().str().eq('3/9')
|
||||||
f1 = math.fraction(1,3)
|
f1 = fractions.fraction(1,3)
|
||||||
assert f1.reciprocal().str().eq('3/1')
|
assert f1.reciprocal().str().eq('3/1')
|
||||||
f1 = math.fraction(3,7)
|
f1 = fractions.fraction(3,7)
|
||||||
assert f1.reciprocal().str().eq('7/3')
|
assert f1.reciprocal().str().eq('7/3')
|
||||||
f1 = math.fraction(1,4)
|
f1 = fractions.fraction(1,4)
|
||||||
assert f1.reciprocal().str().eq('4/1')
|
assert f1.reciprocal().str().eq('4/1')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_fraction_equals() {
|
fn test_fraction_equals() {
|
||||||
mut f1 := math.fraction(4,8)
|
mut f1 := fractions.fraction(4,8)
|
||||||
mut f2 := math.fraction(5,10)
|
mut f2 := fractions.fraction(5,10)
|
||||||
assert f1.equals(f2)
|
assert f1.equals(f2)
|
||||||
f1 = math.fraction(1,2)
|
f1 = fractions.fraction(1,2)
|
||||||
f2 = math.fraction(3,4)
|
f2 = fractions.fraction(3,4)
|
||||||
assert !f1.equals(f2)
|
assert !f1.equals(f2)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_gcd_and_reduce(){
|
fn test_gcd_and_reduce(){
|
||||||
mut f := math.fraction(3, 9)
|
mut f := fractions.fraction(3, 9)
|
||||||
assert f.gcd() == 3
|
assert f.gcd() == 3
|
||||||
assert f.reduce().equals(math.fraction(1, 3))
|
assert f.reduce().equals(fractions.fraction(1, 3))
|
||||||
}
|
}
|
|
@ -2,7 +2,9 @@
|
||||||
// Use of this source code is governed by an MIT license
|
// Use of this source code is governed by an MIT license
|
||||||
// that can be found in the LICENSE file.
|
// that can be found in the LICENSE file.
|
||||||
|
|
||||||
module math
|
module fractions
|
||||||
|
|
||||||
|
import math
|
||||||
|
|
||||||
// Fraction Struct
|
// Fraction Struct
|
||||||
struct Fraction {
|
struct Fraction {
|
||||||
|
@ -82,7 +84,7 @@ pub fn (f1 Fraction) reciprocal() Fraction {
|
||||||
|
|
||||||
// Fraction method which gives greatest common divisor of numerator and denominator
|
// Fraction method which gives greatest common divisor of numerator and denominator
|
||||||
pub fn (f1 Fraction) gcd() i64 {
|
pub fn (f1 Fraction) gcd() i64 {
|
||||||
return gcd(f1.n, f1.d)
|
return math.gcd(f1.n, f1.d)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fraction method which reduces the fraction
|
// Fraction method which reduces the fraction
|
Loading…
Reference in New Issue