aboutsummaryrefslogtreecommitdiff
path: root/py/objint_mpz.c
diff options
context:
space:
mode:
authorDavid Steinberg <david.steinberg.dev@gmail.com>2015-01-13 15:22:30 +0000
committerDamien George <damien.p.george@gmail.com>2015-01-24 20:54:28 +0000
commit0fb17f6ef46ade018a3fda8d4266f15638ddf5e3 (patch)
treeb2ba0a8b7c20d203577ddf3ca48b6c1f1a3eed24 /py/objint_mpz.c
parentca377b10de006b48f75383664f5f765b202412df (diff)
py: Use float-to-int classifications for mp_obj_new_int_from_float() functions
Diffstat (limited to 'py/objint_mpz.c')
-rw-r--r--py/objint_mpz.c14
1 files changed, 8 insertions, 6 deletions
diff --git a/py/objint_mpz.c b/py/objint_mpz.c
index 02b3ec0fe..c46692ec7 100644
--- a/py/objint_mpz.c
+++ b/py/objint_mpz.c
@@ -303,13 +303,15 @@ mp_obj_t mp_obj_new_int_from_float(mp_float_t val) {
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 if (MICROPY_FLOAT_C_FUN(fabs)(val) < 10000) {
- // temporary(?) fix for optimising case where int will be small int
- return MP_OBJ_NEW_SMALL_INT(MICROPY_FLOAT_C_FUN(trunc)(val));
} else {
- mp_obj_int_t *o = mp_obj_int_new_mpz();
- mpz_set_from_float(&o->mpz, val);
- return o;
+ 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 {
+ mp_obj_int_t *o = mp_obj_int_new_mpz();
+ mpz_set_from_float(&o->mpz, val);
+ return o;
+ }
}
}
#endif