Skip to content

Commit c073519

Browse files
committed
py/objint: Handle special case of -0 when classifying fp as int.
Otherwise -0.0 is classified as a bigint, which for builds without bigints will lead unexpectedly to an overflow.
1 parent febeff4 commit c073519

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

py/objint.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ mp_fp_as_int_class_t mp_classify_fp_as_int(mp_float_t val) {
103103
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
104104
e |= u.i[MP_ENDIANNESS_BIG] != 0;
105105
#endif
106-
e += ((1 << MP_FLOAT_EXP_BITS) - 1) << MP_FLOAT_EXP_SHIFT_I32;
106+
if ((e & ~(1 << MP_FLOAT_SIGN_SHIFT_I32)) == 0) {
107+
// handle case of -0 (when sign is set but rest of bits are zero)
108+
e = 0;
109+
} else {
110+
e += ((1 << MP_FLOAT_EXP_BITS) - 1) << MP_FLOAT_EXP_SHIFT_I32;
111+
}
107112
} else {
108113
e &= ~((1 << MP_FLOAT_EXP_SHIFT_I32) - 1);
109114
}

0 commit comments

Comments
 (0)