summaryrefslogtreecommitdiffstatshomepage
path: root/py/objint_longlong.c
diff options
context:
space:
mode:
Diffstat (limited to 'py/objint_longlong.c')
-rw-r--r--py/objint_longlong.c18
1 files changed, 15 insertions, 3 deletions
diff --git a/py/objint_longlong.c b/py/objint_longlong.c
index 394ae111ad..83e1c67d6d 100644
--- a/py/objint_longlong.c
+++ b/py/objint_longlong.c
@@ -187,9 +187,21 @@ mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) {
#if MICROPY_PY_BUILTINS_FLOAT
mp_obj_t mp_obj_new_int_from_float(mp_float_t val) {
- // TODO raise an exception if the unsigned long long won't fit
- long long i = MICROPY_FLOAT_C_FUN(trunc)(val);
- return mp_obj_new_int_from_ll(i);
+ int cl = fpclassify(val);
+ if (cl == FP_INFINITE) {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OverflowError, "can't convert inf to int"));
+ } else if (cl == FP_NAN) {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "can't convert NaN to int"));
+ } else {
+ mp_fp_as_int_class_t icl = mp_classify_fp_as_int(val);
+ if (icl == MP_FP_CLASS_FIT_SMALLINT) {
+ return MP_OBJ_NEW_SMALL_INT((mp_int_t)val);
+ } else if (icl == MP_FP_CLASS_FIT_LONGINT) {
+ return mp_obj_new_int_from_ll((long long)val);
+ } else {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "float too big"));
+ }
+ }
}
#endif