summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
Diffstat (limited to 'py')
-rw-r--r--py/objint.c14
1 files changed, 9 insertions, 5 deletions
diff --git a/py/objint.c b/py/objint.c
index 93cd2778e..4619fb575 100644
--- a/py/objint.c
+++ b/py/objint.c
@@ -135,11 +135,15 @@ STATIC mp_fp_as_int_class_t mp_classify_fp_as_int(mp_float_t val) {
#undef MP_FLOAT_EXP_SHIFT_I32
mp_obj_t mp_obj_new_int_from_float(mp_float_t val) {
- int cl = fpclassify(val);
- if (cl == FP_INFINITE) {
- mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("can't convert inf to int"));
- } else if (cl == FP_NAN) {
- mp_raise_ValueError(MP_ERROR_TEXT("can't convert NaN to int"));
+ mp_float_union_t u = {val};
+ // IEEE-754: if biased exponent is all 1 bits...
+ if (u.p.exp == ((1 << MP_FLOAT_EXP_BITS) - 1)) {
+ // ...then number is Inf (positive or negative) if fraction is 0, else NaN.
+ if (u.p.frc == 0) {
+ mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("can't convert inf to int"));
+ } else {
+ mp_raise_ValueError(MP_ERROR_TEXT("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) {