math.fractions: use operator overloading and deprecate old functions (#19547)

This commit is contained in:
Swastik Baranwal 2023-10-11 05:46:11 +05:30 committed by GitHub
parent c360e03780
commit 491b5f7614
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 15 deletions

View File

@ -233,27 +233,43 @@ fn cmp(f1 Fraction, f2 Fraction) int {
// +-----------------------------+
// | Public comparison functions |
// +-----------------------------+
// equals returns true if both the Fractions are equal
[deprecated: 'use f1 == f2 instead']
pub fn (f1 Fraction) equals(f2 Fraction) bool {
return cmp(f1, f2) == 0
}
// return true if f1 == f2
pub fn (f1 Fraction) == (f2 Fraction) bool {
return cmp(f1, f2) == 0
}
// ge returns true if f1 >= f2
[deprecated: 'use f1 >= f2 instead']
pub fn (f1 Fraction) ge(f2 Fraction) bool {
return cmp(f1, f2) >= 0
}
// gt returns true if f1 > f2
[deprecated: 'use f1 > f2 instead']
pub fn (f1 Fraction) gt(f2 Fraction) bool {
return cmp(f1, f2) > 0
}
// le returns true if f1 <= f2
[deprecated: 'use f1 <= f2 instead']
pub fn (f1 Fraction) le(f2 Fraction) bool {
return cmp(f1, f2) <= 0
}
// lt returns true if f1 < f2
[deprecated: 'use f1 < f2 instead']
pub fn (f1 Fraction) lt(f2 Fraction) bool {
return cmp(f1, f2) < 0
}
// return true if f1 < f2
pub fn (f1 Fraction) < (f2 Fraction) bool {
return cmp(f1, f2) < 0
}

View File

@ -40,7 +40,7 @@ fn test_4_by_8_plus_5_by_10() {
sum := f1 + f2
assert sum.f64() == 1.0
assert sum.str() == '1/1'
assert sum.equals(fractions.fraction(1, 1))
assert sum == fractions.fraction(1, 1)
}
fn test_5_by_5_plus_8_by_8() {
@ -49,7 +49,7 @@ fn test_5_by_5_plus_8_by_8() {
sum := f1 + f2
assert sum.f64() == 2.0
assert sum.str() == '2/1'
assert sum.equals(fractions.fraction(2, 1))
assert sum == fractions.fraction(2, 1)
}
fn test_9_by_3_plus_1_by_3() {
@ -57,7 +57,7 @@ fn test_9_by_3_plus_1_by_3() {
f2 := fractions.fraction(1, 3)
sum := f1 + f2
assert sum.str() == '10/3'
assert sum.equals(fractions.fraction(10, 3))
assert sum == fractions.fraction(10, 3)
}
fn test_3_by_7_plus_1_by_4() {
@ -65,7 +65,7 @@ fn test_3_by_7_plus_1_by_4() {
f2 := fractions.fraction(1, 4)
sum := f1 + f2
assert sum.str() == '19/28'
assert sum.equals(fractions.fraction(19, 28))
assert sum == fractions.fraction(19, 28)
}
fn test_36529_by_12409100000_plus_418754901_by_9174901000() {
@ -226,44 +226,44 @@ fn test_reciprocal_1_by_4() {
fn test_4_by_8_equals_5_by_10() {
f1 := fractions.fraction(4, 8)
f2 := fractions.fraction(5, 10)
assert f1.equals(f2)
assert f1 == f2
}
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)
assert f1 != f2
}
fn test_reduce_3_by_9() {
f := fractions.fraction(3, 9)
assert f.reduce().equals(fractions.fraction(1, 3))
assert f.reduce() == 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)
assert f1 < f2
assert f1 <= 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)
assert f1 > f2
assert f1 >= 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)
assert !(f1 < f2)
assert !(f1 <= 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)
assert !(f1 > f2)
assert !(f1 >= f2)
}