Skip to content

Commit 8369559

Browse files
committed
py: Fix build error when float disabled; add test for divmod.
1 parent 8594ce2 commit 8369559

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

py/builtin.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,14 +247,16 @@ STATIC mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
247247
mp_int_t i1 = MP_OBJ_SMALL_INT_VALUE(o1_in);
248248
mp_int_t i2 = MP_OBJ_SMALL_INT_VALUE(o2_in);
249249
if (i2 == 0) {
250+
#if MICROPY_PY_BUILTINS_FLOAT
250251
zero_division_error:
252+
#endif
251253
nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "division by zero"));
252254
}
253255
mp_obj_t args[2];
254256
args[0] = MP_OBJ_NEW_SMALL_INT(i1 / i2);
255257
args[1] = MP_OBJ_NEW_SMALL_INT(i1 % i2);
256258
return mp_obj_new_tuple(2, args);
257-
#if MICROPY_PY_BUILTINS_FLOAT
259+
#if MICROPY_PY_BUILTINS_FLOAT
258260
} else if (MP_OBJ_IS_TYPE(o1_in, &mp_type_float) || MP_OBJ_IS_TYPE(o2_in, &mp_type_float)) {
259261
mp_float_t f1 = mp_obj_get_float(o1_in);
260262
mp_float_t f2 = mp_obj_get_float(o2_in);
@@ -267,7 +269,7 @@ STATIC mp_obj_t mp_builtin_divmod(mp_obj_t o1_in, mp_obj_t o2_in) {
267269
mp_obj_new_float(f2),
268270
};
269271
return mp_obj_new_tuple(2, tuple);
270-
#endif
272+
#endif
271273
} else {
272274
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "unsupported operand type(s) for divmod(): '%s' and '%s'", mp_obj_get_type_str(o1_in), mp_obj_get_type_str(o2_in)));
273275
}

tests/float/float_divmod.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# test floating point floor divide and modulus
2+
# it has some tricky corner cases
3+
4+
def test(x, y):
5+
div, mod = divmod(x, y)
6+
print('%.8f %.8f %.8f %.8f' % (x // y, x % y, div, mod))
7+
print(div == x // y, mod == x % y, abs(div * y + mod - x) < 1e-15)
8+
9+
test(1.23456, 0.7)
10+
test(-1.23456, 0.7)
11+
test(1.23456, -0.7)
12+
test(-1.23456, -0.7)
13+
14+
a = 1.23456
15+
b = 0.7
16+
test(a, b)
17+
test(a, -b)
18+
test(-a, b)
19+
test(-a, -b)
20+
21+
for i in range(25):
22+
x = (i - 12.5) / 6
23+
for j in range(25):
24+
y = (j - 12.5) / 6
25+
test(x, y)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# test floating point floor divide and modulus
2+
# it has some tricky corner cases
3+
4+
# pyboard has 32-bit floating point and gives different (but still
5+
# correct) answers for certain combinations of divmod arguments.
6+
7+
def test(x, y):
8+
div, mod = divmod(x, y)
9+
print(div == x // y, mod == x % y, abs(div * y + mod - x) < 1e-6)
10+
11+
test(1.23456, 0.7)
12+
test(-1.23456, 0.7)
13+
test(1.23456, -0.7)
14+
test(-1.23456, -0.7)
15+
16+
a = 1.23456
17+
b = 0.7
18+
test(a, b)
19+
test(a, -b)
20+
test(-a, b)
21+
test(-a, -b)
22+
23+
for i in range(25):
24+
x = (i - 12.5) / 6
25+
for j in range(25):
26+
y = (j - 12.5) / 6
27+
test(x, y)

0 commit comments

Comments
 (0)