Skip to content

Commit 96ed213

Browse files
committed
objfloat: Quick&dirty implementation of float floor division.
TODO: Likely doesn't match Python semantics for negative numbers.
1 parent 96eec4f commit 96ed213

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

py/objfloat.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,15 @@ mp_obj_t mp_obj_float_binary_op(int op, mp_float_t lhs_val, mp_obj_t rhs_in) {
111111
case MP_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
112112
case MP_BINARY_OP_MULTIPLY:
113113
case MP_BINARY_OP_INPLACE_MULTIPLY: lhs_val *= rhs_val; break;
114-
/* TODO floor(?) the value
114+
// TODO: verify that C floor matches Python semantics
115115
case MP_BINARY_OP_FLOOR_DIVIDE:
116-
case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE: val = lhs_val / rhs_val; break;
117-
*/
116+
case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
117+
lhs_val = MICROPY_FLOAT_C_FUN(floor)(lhs_val / rhs_val);
118+
goto check_zero_division;
118119
case MP_BINARY_OP_TRUE_DIVIDE:
119120
case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
120121
lhs_val /= rhs_val;
122+
check_zero_division:
121123
if (isinf(lhs_val)){ // check for division by zero
122124
nlr_jump(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "float division by zero"));
123125
}

0 commit comments

Comments
 (0)