From 96ed213320cb627c1df7ea8550b86b0bba5cb559 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 31 Mar 2014 02:18:09 +0300 Subject: [PATCH] objfloat: Quick&dirty implementation of float floor division. TODO: Likely doesn't match Python semantics for negative numbers. --- py/objfloat.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/py/objfloat.c b/py/objfloat.c index d8ac96c0b4..3916c340b3 100644 --- a/py/objfloat.c +++ b/py/objfloat.c @@ -111,13 +111,15 @@ mp_obj_t mp_obj_float_binary_op(int op, mp_float_t lhs_val, mp_obj_t rhs_in) { case MP_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break; case MP_BINARY_OP_MULTIPLY: case MP_BINARY_OP_INPLACE_MULTIPLY: lhs_val *= rhs_val; break; - /* TODO floor(?) the value + // TODO: verify that C floor matches Python semantics case MP_BINARY_OP_FLOOR_DIVIDE: - case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE: val = lhs_val / rhs_val; break; - */ + case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE: + lhs_val = MICROPY_FLOAT_C_FUN(floor)(lhs_val / rhs_val); + goto check_zero_division; case MP_BINARY_OP_TRUE_DIVIDE: case MP_BINARY_OP_INPLACE_TRUE_DIVIDE: lhs_val /= rhs_val; +check_zero_division: if (isinf(lhs_val)){ // check for division by zero nlr_jump(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "float division by zero")); }