Commit e72be1b9 authored by Damien George's avatar Damien George
Browse files

py: Fix smallint modulo with negative arguments.

Addresses issue #927.
parent 5fc42a6c
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#include "qstr.h" #include "qstr.h"
#include "obj.h" #include "obj.h"
#include "objstr.h" #include "objstr.h"
#include "smallint.h"
#include "runtime0.h" #include "runtime0.h"
#include "runtime.h" #include "runtime.h"
#include "builtin.h" #include "builtin.h"
...@@ -253,8 +254,8 @@ STATIC mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) { ...@@ -253,8 +254,8 @@ STATIC mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "division by zero")); nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "division by zero"));
} }
mp_obj_t args[2]; mp_obj_t args[2];
args[0] = MP_OBJ_NEW_SMALL_INT(i1 / i2); args[0] = MP_OBJ_NEW_SMALL_INT(mp_small_int_floor_divide(i1, i2));
args[1] = MP_OBJ_NEW_SMALL_INT(i1 % i2); args[1] = MP_OBJ_NEW_SMALL_INT(mp_small_int_modulo(i1, i2));
return mp_obj_new_tuple(2, args); return mp_obj_new_tuple(2, args);
#if MICROPY_PY_BUILTINS_FLOAT #if MICROPY_PY_BUILTINS_FLOAT
} else if (MP_OBJ_IS_TYPE(o1_in, &mp_type_float) || MP_OBJ_IS_TYPE(o2_in, &mp_type_float)) { } else if (MP_OBJ_IS_TYPE(o1_in, &mp_type_float) || MP_OBJ_IS_TYPE(o2_in, &mp_type_float)) {
......
...@@ -57,23 +57,23 @@ bool mp_small_int_mul_overflow(mp_int_t x, mp_int_t y) { ...@@ -57,23 +57,23 @@ bool mp_small_int_mul_overflow(mp_int_t x, mp_int_t y) {
} }
mp_int_t mp_small_int_modulo(mp_int_t dividend, mp_int_t divisor) { mp_int_t mp_small_int_modulo(mp_int_t dividend, mp_int_t divisor) {
mp_int_t lsign = (dividend >= 0) ? 1 :-1; // Python specs require that mod has same sign as second operand
mp_int_t rsign = (divisor >= 0) ? 1 :-1;
dividend %= divisor; dividend %= divisor;
if (lsign != rsign) { if ((dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0)) {
dividend += divisor; dividend += divisor;
} }
return dividend; return dividend;
} }
mp_int_t mp_small_int_floor_divide(mp_int_t num, mp_int_t denom) { mp_int_t mp_small_int_floor_divide(mp_int_t num, mp_int_t denom) {
mp_int_t lsign = num > 0 ? 1 : -1; if (num >= 0) {
mp_int_t rsign = denom > 0 ? 1 : -1; if (denom < 0) {
if (lsign == -1) {num *= -1;} num += -denom - 1;
if (rsign == -1) {denom *= -1;} }
if (lsign != rsign){
return - ( num + denom - 1) / denom;
} else { } else {
return num / denom; if (denom >= 0) {
num += -denom + 1;
}
} }
return num / denom;
} }
# test integer floor division and modulo
# test all combination of +/-/0 cases
for i in range(-2, 3):
for j in range(-4, 5):
if j != 0:
print(i, j, i // j, i % j, divmod(i, j))
# this tests compiler constant folding
print(123 // 7, 123 % 7)
print(-123 // 7, -123 % 7)
print(123 // -7, 123 % -7)
print(-123 // -7, -123 % -7)
# this tests bignum modulo
a = 987654321987987987987987987987
b = 19
print(a % b)
print(a % -b)
print(-a % b)
print(-a % -b)
# check modulo matches python definition
# this tests compiler constant folding
print(123 % 7)
print(-123 % 7)
print(123 % -7)
print(-123 % -7)
a = 321
b = 19
print(a % b)
print(a % -b)
print(-a % b)
print(-a % -b)
a = 987654321987987987987987987987
b = 19
print(a % b)
print(a % -b)
print(-a % b)
print(-a % -b)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment