mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
math.fractions: refactor and add more tests
This commit is contained in:
@@ -1,153 +1,266 @@
|
||||
import math.fractions as fractions
|
||||
import math.fractions
|
||||
|
||||
// Results are verified using https://www.calculatorsoup.com/calculators/math/fractions.php
|
||||
|
||||
fn test_fraction_creation() {
|
||||
mut f1 := fractions.fraction(4,8)
|
||||
assert f1.f64() == 0.5
|
||||
assert f1.str().eq('4/8')
|
||||
f1 = fractions.fraction(10,5)
|
||||
assert f1.f64() == 2.0
|
||||
assert f1.str().eq('10/5')
|
||||
f1 = fractions.fraction(9,3)
|
||||
assert f1.f64() == 3.0
|
||||
assert f1.str().eq('9/3')
|
||||
// (Old) results are verified using https://www.calculatorsoup.com/calculators/math/fractions.php
|
||||
// Newer ones are contrived for corner cases or prepared by hand.
|
||||
fn test_4_by_8_f64_and_str() {
|
||||
f := fractions.fraction(4, 8)
|
||||
assert f.f64() == 0.5
|
||||
assert f.str() == '4/8'
|
||||
}
|
||||
|
||||
fn test_fraction_add() {
|
||||
mut f1 := fractions.fraction(4,8)
|
||||
mut f2 := fractions.fraction(5,10)
|
||||
mut sum := f1 + f2
|
||||
fn test_10_by_5_f64_and_str() {
|
||||
f := fractions.fraction(10, 5)
|
||||
assert f.f64() == 2.0
|
||||
assert f.str() == '10/5'
|
||||
}
|
||||
|
||||
fn test_9_by_3_f64_and_str() {
|
||||
f := fractions.fraction(9, 3)
|
||||
assert f.f64() == 3.0
|
||||
assert f.str() == '9/3'
|
||||
}
|
||||
|
||||
fn test_4_by_minus_5_f64_and_str() {
|
||||
f := fractions.fraction(4, -5)
|
||||
assert f.f64() == -0.8
|
||||
assert f.str() == '-4/5'
|
||||
}
|
||||
|
||||
fn test_minus_7_by_minus_92_str() {
|
||||
f := fractions.fraction(-7, -5)
|
||||
assert f.str() == '7/5'
|
||||
}
|
||||
|
||||
fn test_4_by_8_plus_5_by_10() {
|
||||
f1 := fractions.fraction(4, 8)
|
||||
f2 := fractions.fraction(5, 10)
|
||||
sum := f1 + f2
|
||||
assert sum.f64() == 1.0
|
||||
assert sum.str().eq('80/80')
|
||||
f1 = fractions.fraction(5,5)
|
||||
f2 = fractions.fraction(8,8)
|
||||
sum = f1 + f2
|
||||
assert sum.str() == '1/1'
|
||||
assert sum.equals(fractions.fraction(1, 1))
|
||||
}
|
||||
|
||||
fn test_5_by_5_plus_8_by_8() {
|
||||
f1 := fractions.fraction(5, 5)
|
||||
f2 := fractions.fraction(8, 8)
|
||||
sum := f1 + f2
|
||||
assert sum.f64() == 2.0
|
||||
assert sum.str().eq('80/40')
|
||||
f1 = fractions.fraction(9,3)
|
||||
f2 = fractions.fraction(1,3)
|
||||
sum = f1 + f2
|
||||
$if debug {
|
||||
println(sum.f64())
|
||||
}
|
||||
assert sum.str().eq('10/3')
|
||||
f1 = fractions.fraction(3,7)
|
||||
f2 = fractions.fraction(1,4)
|
||||
sum = f1 + f2
|
||||
$if debug {
|
||||
println(sum.f64())
|
||||
}
|
||||
assert sum.str().eq('19/28')
|
||||
assert sum.str() == '2/1'
|
||||
assert sum.equals(fractions.fraction(2, 1))
|
||||
}
|
||||
|
||||
fn test_fraction_subtract() {
|
||||
mut f1 := fractions.fraction(4,8)
|
||||
mut f2 := fractions.fraction(5,10)
|
||||
mut diff := f2 - f1
|
||||
assert diff.f64() == 0
|
||||
assert diff.str().eq('0/80')
|
||||
f1 = fractions.fraction(5,5)
|
||||
f2 = fractions.fraction(8,8)
|
||||
diff = f2 - f1
|
||||
assert diff.f64() == 0
|
||||
assert diff.str().eq('0/40')
|
||||
f1 = fractions.fraction(9,3)
|
||||
f2 = fractions.fraction(1,3)
|
||||
diff = f1 - f2
|
||||
$if debug {
|
||||
println(diff.f64())
|
||||
}
|
||||
assert diff.str().eq('8/3')
|
||||
f1 = fractions.fraction(3,7)
|
||||
f2 = fractions.fraction(1,4)
|
||||
diff = f1 - f2
|
||||
$if debug {
|
||||
println(diff.f64())
|
||||
}
|
||||
assert diff.str().eq('5/28')
|
||||
fn test_9_by_3_plus_1_by_3() {
|
||||
f1 := fractions.fraction(9, 3)
|
||||
f2 := fractions.fraction(1, 3)
|
||||
sum := f1 + f2
|
||||
assert sum.str() == '10/3'
|
||||
assert sum.equals(fractions.fraction(10, 3))
|
||||
}
|
||||
|
||||
fn test_fraction_multiply() {
|
||||
mut f1 := fractions.fraction(4,8)
|
||||
mut f2 := fractions.fraction(5,10)
|
||||
mut product := f1.multiply(f2)
|
||||
fn test_3_by_7_plus_1_by_4() {
|
||||
f1 := fractions.fraction(3, 7)
|
||||
f2 := fractions.fraction(1, 4)
|
||||
sum := f1 + f2
|
||||
assert sum.str() == '19/28'
|
||||
assert sum.equals(fractions.fraction(19, 28))
|
||||
}
|
||||
|
||||
fn test_36529_by_12409100000_plus_418754901_by_9174901000() {
|
||||
f1 := fractions.fraction(i64(36529), i64(12409100000))
|
||||
f2 := fractions.fraction(i64(418754901), i64(9174901000))
|
||||
sum := f1 + f2
|
||||
assert sum.str() == '5196706591957729/113852263999100000'
|
||||
}
|
||||
|
||||
fn test_4_by_8_plus_minus_5_by_10() {
|
||||
f1 := fractions.fraction(4, 8)
|
||||
f2 := fractions.fraction(-5, 10)
|
||||
diff := f2 + f1
|
||||
assert diff.f64() == 0
|
||||
assert diff.str() == '0/1'
|
||||
}
|
||||
|
||||
fn test_4_by_8_minus_5_by_10() {
|
||||
f1 := fractions.fraction(4, 8)
|
||||
f2 := fractions.fraction(5, 10)
|
||||
diff := f2 - f1
|
||||
assert diff.f64() == 0
|
||||
assert diff.str() == '0/1'
|
||||
}
|
||||
|
||||
fn test_5_by_5_minus_8_by_8() {
|
||||
f1 := fractions.fraction(5, 5)
|
||||
f2 := fractions.fraction(8, 8)
|
||||
diff := f2 - f1
|
||||
assert diff.f64() == 0
|
||||
assert diff.str() == '0/1'
|
||||
}
|
||||
|
||||
fn test_9_by_3_minus_1_by_3() {
|
||||
f1 := fractions.fraction(9, 3)
|
||||
f2 := fractions.fraction(1, 3)
|
||||
diff := f1 - f2
|
||||
assert diff.str() == '8/3'
|
||||
}
|
||||
|
||||
fn test_3_by_7_minus_1_by_4() {
|
||||
f1 := fractions.fraction(3, 7)
|
||||
f2 := fractions.fraction(1, 4)
|
||||
diff := f1 - f2
|
||||
assert diff.str() == '5/28'
|
||||
}
|
||||
|
||||
fn test_36529_by_12409100000_minus_418754901_by_9174901000() {
|
||||
f1 := fractions.fraction(i64(36529), i64(12409100000))
|
||||
f2 := fractions.fraction(i64(418754901), i64(9174901000))
|
||||
sum := f1 - f2
|
||||
assert sum.str() == '-5196036292040471/113852263999100000'
|
||||
}
|
||||
|
||||
fn test_4_by_8_times_5_by_10() {
|
||||
f1 := fractions.fraction(4, 8)
|
||||
f2 := fractions.fraction(5, 10)
|
||||
product := f1 * f2
|
||||
assert product.f64() == 0.25
|
||||
assert product.str().eq('20/80')
|
||||
f1 = fractions.fraction(5,5)
|
||||
f2 = fractions.fraction(8,8)
|
||||
product = f1.multiply(f2)
|
||||
assert product.str() == '1/4'
|
||||
}
|
||||
|
||||
fn test_5_by_5_times_8_by_8() {
|
||||
f1 := fractions.fraction(5, 5)
|
||||
f2 := fractions.fraction(8, 8)
|
||||
product := f1 * f2
|
||||
assert product.f64() == 1.0
|
||||
assert product.str().eq('40/40')
|
||||
f1 = fractions.fraction(9,3)
|
||||
f2 = fractions.fraction(1,3)
|
||||
product = f1.multiply(f2)
|
||||
assert product.str() == '1/1'
|
||||
}
|
||||
|
||||
fn test_9_by_3_times_1_by_3() {
|
||||
f1 := fractions.fraction(9, 3)
|
||||
f2 := fractions.fraction(1, 3)
|
||||
product := f1 * f2
|
||||
assert product.f64() == 1.0
|
||||
assert product.str().eq('9/9')
|
||||
f1 = fractions.fraction(3,7)
|
||||
f2 = fractions.fraction(1,4)
|
||||
product = f1.multiply(f2)
|
||||
$if debug {
|
||||
println(product.f64())
|
||||
}
|
||||
assert product.str().eq('3/28')
|
||||
assert product.str() == '1/1'
|
||||
}
|
||||
|
||||
fn test_fraction_divide() {
|
||||
mut f1 := fractions.fraction(4,8)
|
||||
mut f2 := fractions.fraction(5,10)
|
||||
mut re := f1.divide(f2)
|
||||
assert re.f64() == 1.0
|
||||
assert re.str().eq('40/40')
|
||||
f1 = fractions.fraction(5,5)
|
||||
f2 = fractions.fraction(8,8)
|
||||
re = f1.divide(f2)
|
||||
assert re.f64() == 1.0
|
||||
assert re.str().eq('40/40')
|
||||
f1 = fractions.fraction(9,3)
|
||||
f2 = fractions.fraction(1,3)
|
||||
re = f1.divide(f2)
|
||||
assert re.f64() == 9.0
|
||||
assert re.str().eq('27/3')
|
||||
f1 = fractions.fraction(3,7)
|
||||
f2 = fractions.fraction(1,4)
|
||||
re = f1.divide(f2)
|
||||
$if debug {
|
||||
println(re.f64())
|
||||
}
|
||||
assert re.str().eq('12/7')
|
||||
fn test_3_by_7_times_1_by_4() {
|
||||
f1 := fractions.fraction(3, 7)
|
||||
f2 := fractions.fraction(1, 4)
|
||||
product := f2 * f1
|
||||
assert product.f64() == (3.0 / 28.0)
|
||||
assert product.str() == '3/28'
|
||||
}
|
||||
|
||||
fn test_fraction_reciprocal() {
|
||||
mut f1 := fractions.fraction(4,8)
|
||||
assert f1.reciprocal().str().eq('8/4')
|
||||
f1 = fractions.fraction(5,10)
|
||||
assert f1.reciprocal().str().eq('10/5')
|
||||
f1 = fractions.fraction(5,5)
|
||||
assert f1.reciprocal().str().eq('5/5')
|
||||
f1 = fractions.fraction(8,8)
|
||||
assert f1.reciprocal().str().eq('8/8')
|
||||
f1 = fractions.fraction(9,3)
|
||||
assert f1.reciprocal().str().eq('3/9')
|
||||
f1 = fractions.fraction(1,3)
|
||||
assert f1.reciprocal().str().eq('3/1')
|
||||
f1 = fractions.fraction(3,7)
|
||||
assert f1.reciprocal().str().eq('7/3')
|
||||
f1 = fractions.fraction(1,4)
|
||||
assert f1.reciprocal().str().eq('4/1')
|
||||
fn test_4_by_8_over_5_by_10() {
|
||||
f1 := fractions.fraction(4, 8)
|
||||
f2 := fractions.fraction(5, 10)
|
||||
q := f1 / f2
|
||||
assert q.f64() == 1.0
|
||||
assert q.str() == '1/1'
|
||||
}
|
||||
|
||||
fn test_fraction_equals() {
|
||||
mut f1 := fractions.fraction(4,8)
|
||||
mut f2 := fractions.fraction(5,10)
|
||||
fn test_5_by_5_over_8_by_8() {
|
||||
f1 := fractions.fraction(5, 5)
|
||||
f2 := fractions.fraction(8, 8)
|
||||
q := f1 / f2
|
||||
assert q.f64() == 1.0
|
||||
assert q.str() == '1/1'
|
||||
}
|
||||
|
||||
fn test_9_by_3_over_1_by_3() {
|
||||
f1 := fractions.fraction(9, 3)
|
||||
f2 := fractions.fraction(1, 3)
|
||||
q := f1 / f2
|
||||
assert q.f64() == 9.0
|
||||
assert q.str() == '9/1'
|
||||
}
|
||||
|
||||
fn test_3_by_7_over_1_by_4() {
|
||||
f1 := fractions.fraction(3, 7)
|
||||
f2 := fractions.fraction(1, 4)
|
||||
q := f1 / f2
|
||||
assert q.str() == '12/7'
|
||||
}
|
||||
|
||||
fn test_reciprocal_4_by_8() {
|
||||
f := fractions.fraction(4, 8)
|
||||
assert f.reciprocal().str() == '8/4'
|
||||
}
|
||||
|
||||
fn test_reciprocal_5_by_10() {
|
||||
f := fractions.fraction(5, 10)
|
||||
assert f.reciprocal().str() == '10/5'
|
||||
}
|
||||
|
||||
fn test_reciprocal_5_by_5() {
|
||||
f := fractions.fraction(5, 5)
|
||||
assert f.reciprocal().str() == '5/5'
|
||||
}
|
||||
|
||||
fn test_reciprocal_8_by_8() {
|
||||
f := fractions.fraction(8, 8)
|
||||
assert f.reciprocal().str() == '8/8'
|
||||
}
|
||||
|
||||
fn test_reciprocal_9_by_3() {
|
||||
f := fractions.fraction(9, 3)
|
||||
assert f.reciprocal().str() == '3/9'
|
||||
}
|
||||
|
||||
fn test_reciprocal_1_by_3() {
|
||||
f := fractions.fraction(1, 3)
|
||||
assert f.reciprocal().str() == '3/1'
|
||||
}
|
||||
|
||||
fn test_reciprocal_7_by_3() {
|
||||
f := fractions.fraction(7, 3)
|
||||
assert f.reciprocal().str() == '3/7'
|
||||
}
|
||||
|
||||
fn test_reciprocal_1_by_4() {
|
||||
f := fractions.fraction(1, 4)
|
||||
assert f.reciprocal().str() == '4/1'
|
||||
}
|
||||
|
||||
fn test_4_by_8_equals_5_by_10() {
|
||||
f1 := fractions.fraction(4, 8)
|
||||
f2 := fractions.fraction(5, 10)
|
||||
assert f1.equals(f2)
|
||||
f1 = fractions.fraction(1,2)
|
||||
f2 = fractions.fraction(3,4)
|
||||
}
|
||||
|
||||
fn test_1_by_2_does_not_equal_3_by_4() {
|
||||
f1 := fractions.fraction(1, 2)
|
||||
f2 := fractions.fraction(3, 4)
|
||||
assert !f1.equals(f2)
|
||||
}
|
||||
|
||||
fn test_gcd_and_reduce(){
|
||||
fn test_reduce_3_by_9() {
|
||||
f := fractions.fraction(3, 9)
|
||||
assert f.gcd() == 3
|
||||
assert f.reduce().equals(fractions.fraction(1, 3))
|
||||
}
|
||||
|
||||
fn test_1_by_3_less_than_2_by_4() {
|
||||
f1 := fractions.fraction(1, 3)
|
||||
f2 := fractions.fraction(2, 4)
|
||||
assert f1.lt(f2)
|
||||
assert f1.le(f2)
|
||||
}
|
||||
|
||||
fn test_2_by_3_greater_than_2_by_4() {
|
||||
f1 := fractions.fraction(2, 3)
|
||||
f2 := fractions.fraction(2, 4)
|
||||
assert f1.gt(f2)
|
||||
assert f1.ge(f2)
|
||||
}
|
||||
|
||||
fn test_5_by_7_not_less_than_2_by_4() {
|
||||
f1 := fractions.fraction(5, 7)
|
||||
f2 := fractions.fraction(2, 4)
|
||||
assert !f1.lt(f2)
|
||||
assert !f1.le(f2)
|
||||
}
|
||||
|
||||
fn test_49_by_75_not_greater_than_2_by_3() {
|
||||
f1 := fractions.fraction(49, 75)
|
||||
f2 := fractions.fraction(2, 3)
|
||||
assert !f1.gt(f2)
|
||||
assert !f1.ge(f2)
|
||||
}
|
||||
Reference in New Issue
Block a user