diff --git a/vlib/math/big/array_ops.v b/vlib/math/big/array_ops.v index 3124d23093..bff4c5d450 100644 --- a/vlib/math/big/array_ops.v +++ b/vlib/math/big/array_ops.v @@ -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() } } diff --git a/vlib/math/big/array_ops_test.v b/vlib/math/big/array_ops_test.v index 1841e1431d..813fe31e9c 100644 --- a/vlib/math/big/array_ops_test.v +++ b/vlib/math/big/array_ops_test.v @@ -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]