1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

math.big: fix subtract_digit_array (#11451)

This commit is contained in:
Vincent Laisney 2021-09-09 13:56:20 +02:00 committed by GitHub
parent 1a2d5f65fb
commit c8d4a64512
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 2 deletions

View File

@ -69,7 +69,7 @@ fn add_digit_array(operand_a []u32, operand_b []u32, mut sum []u32) {
// Subtracts operand_b from operand_a and stores the difference in storage.
// It assumes operand_a contains the larger "integer" and that storage is
// the same size as operand_a
// the same size as operand_a and is 0
fn subtract_digit_array(operand_a []u32, operand_b []u32, mut storage []u32) {
// Zero length cases
if operand_a.len == 0 {
@ -104,7 +104,7 @@ fn subtract_digit_array(operand_a []u32, operand_b []u32, mut storage []u32) {
storage[index] = u32(a_digit - b_digit)
}
if storage.last() == 0 {
for storage.len > 0 && storage.last() == 0 {
storage.delete_last()
}
}

View File

@ -81,6 +81,14 @@ fn test_subtract_digit_array_03() {
assert c == [u32(0), 0, u32(-1), u32(-1), 0, 13]
}
fn test_subtract_digit_array_04() {
a := [u32(0x2), 0x4, 0x5, 0x3]
b := [u32(0x0), 0x0, 0x5, 0x3]
mut c := []u32{len: a.len}
subtract_digit_array(a, b, mut c)
assert c == [u32(0x2), 0x4]
}
fn test_multiply_digit_array_01() {
a := [u32(0), 0, 0, 1]
b := [u32(0), 0, 1]