Skip to content

Commit f658b0d

Browse files
jeplerdpgeorge
authored andcommitted
py/objint: Fix converting float to int with OBJ_REPR_B.
The check for 'fits in a small int' is specific to the 31-bit int of other object representations. Signed-off-by: Jeff Epler <[email protected]>
1 parent e35ac67 commit f658b0d

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

py/objint.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,15 @@ static mp_fp_as_int_class_t mp_classify_fp_as_int(mp_float_t val) {
117117
}
118118
// 8 * sizeof(uintptr_t) counts the number of bits for a small int
119119
// TODO provide a way to configure this properly
120+
#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B
121+
if (e <= ((8 * sizeof(uintptr_t) + MP_FLOAT_EXP_BIAS - 4) << MP_FLOAT_EXP_SHIFT_I32)) {
122+
return MP_FP_CLASS_FIT_SMALLINT;
123+
}
124+
#else
120125
if (e <= ((8 * sizeof(uintptr_t) + MP_FLOAT_EXP_BIAS - 3) << MP_FLOAT_EXP_SHIFT_I32)) {
121126
return MP_FP_CLASS_FIT_SMALLINT;
122127
}
128+
#endif
123129
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
124130
if (e <= (((sizeof(long long) * MP_BITS_PER_BYTE) + MP_FLOAT_EXP_BIAS - 2) << MP_FLOAT_EXP_SHIFT_I32)) {
125131
return MP_FP_CLASS_FIT_LONGINT;

0 commit comments

Comments
 (0)