math.big: fix comparison and add tests (#11449)

pull/11450/head
Subhomoy Haldar 2021-09-08 23:00:20 +05:30 committed by GitHub
parent 4a2728e1bf
commit 96d4a0777f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View File

@ -149,6 +149,24 @@ fn test_divmod() {
assert h.str() == '2900204736088469'
}
fn test_comparison() {
values := [-3, 13, 52, 6, 41]
for value in values {
x := big.integer_from_int(value)
assert x == x
assert x <= x
assert x >= x
}
a := big.integer_from_int(-45)
b := big.integer_from_int(35)
assert a < b
assert a <= b
assert b > a
assert b >= a
}
fn test_conversion() {
ten := big.integer_from_int(10)

View File

@ -454,7 +454,7 @@ pub fn (a Integer) < (b Integer) bool {
return false
}
// If they are negative, the one with the larger absolute value is smaller
cmp := a.abs_cmp()
cmp := a.abs_cmp(b)
return if signum < 0 { cmp > 0 } else { cmp < 0 }
}