py: Fix smallint modulo with negative arguments.

Addresses issue #927.
This commit is contained in:
Damien George 2014-10-22 23:05:50 +01:00
parent 5fc42a6c97
commit e72be1b999
4 changed files with 34 additions and 35 deletions

View File

@ -33,6 +33,7 @@
#include "qstr.h"
#include "obj.h"
#include "objstr.h"
#include "smallint.h"
#include "runtime0.h"
#include "runtime.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) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "division by zero"));
}
mp_obj_t args[2];
args[0] = MP_OBJ_NEW_SMALL_INT(i1 / i2);
args[1] = 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(mp_small_int_modulo(i1, i2));
return mp_obj_new_tuple(2, args);
#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)) {

View File

@ -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 lsign = (dividend >= 0) ? 1 :-1;
mp_int_t rsign = (divisor >= 0) ? 1 :-1;
// Python specs require that mod has same sign as second operand
dividend %= divisor;
if (lsign != rsign) {
if ((dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0)) {
dividend += divisor;
}
return dividend;
}
mp_int_t mp_small_int_floor_divide(mp_int_t num, mp_int_t denom) {
mp_int_t lsign = num > 0 ? 1 : -1;
mp_int_t rsign = denom > 0 ? 1 : -1;
if (lsign == -1) {num *= -1;}
if (rsign == -1) {denom *= -1;}
if (lsign != rsign){
return - ( num + denom - 1) / denom;
} else {
return num / denom;
if (num >= 0) {
if (denom < 0) {
num += -denom - 1;
}
} else {
if (denom >= 0) {
num += -denom + 1;
}
}
return num / denom;
}

View File

@ -0,0 +1,21 @@
# 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)

View File

@ -1,23 +0,0 @@
# 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)