diff options
author | Damien George <damien.p.george@gmail.com> | 2017-03-24 10:55:50 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2017-03-24 11:00:45 +1100 |
commit | c236ebfea705679078f906da484a3ed0bffd32e2 (patch) | |
tree | b90639b14e0ac8e6b64c28656c2b39799dbc7fa8 | |
parent | 125eae1ba3c59b882fc95c82c95dccd2d93ceaa1 (diff) | |
download | micropython-c236ebfea705679078f906da484a3ed0bffd32e2.tar.gz micropython-c236ebfea705679078f906da484a3ed0bffd32e2.zip |
py/modbuiltins: Allow round() to return a big int if necessary.
Previous to this patch, if the result of the round function overflowed a
small int, or was inf or nan, then a garbage value was returned. With
this patch the correct big-int is returned if necessary and exceptions are
raised for inf or nan.
-rw-r--r-- | py/modbuiltins.c | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/py/modbuiltins.c b/py/modbuiltins.c index 541e733e5a..bd325014d1 100644 --- a/py/modbuiltins.c +++ b/py/modbuiltins.c @@ -478,10 +478,11 @@ STATIC mp_obj_t mp_builtin_round(size_t n_args, const mp_obj_t *args) { } mp_float_t val = mp_obj_get_float(o_in); mp_float_t rounded = MICROPY_FLOAT_C_FUN(nearbyint)(val); + return mp_obj_new_int_from_float(rounded); #else mp_int_t r = mp_obj_get_int(o_in); -#endif return mp_obj_new_int(r); +#endif } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_round_obj, 1, 2, mp_builtin_round); |