Skip to content

Commit 6433bd9

Browse files
committed
py: Add explicit conversion from float to int via int().
1 parent 804760b commit 6433bd9

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

py/objint.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
#include "mpz.h"
1111
#include "objint.h"
1212

13+
#if MICROPY_ENABLE_FLOAT
14+
#include <math.h>
15+
#endif
16+
1317
// This dispatcher function is expected to be independent of the implementation
1418
// of long int
1519
STATIC mp_obj_t int_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
@@ -25,6 +29,10 @@ STATIC mp_obj_t int_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_
2529
uint l;
2630
const char *s = mp_obj_str_get_data(args[0], &l);
2731
return mp_parse_num_integer(s, l, 0);
32+
#if MICROPY_ENABLE_FLOAT
33+
} else if (MP_OBJ_IS_TYPE(args[0], &mp_type_float)) {
34+
return MP_OBJ_NEW_SMALL_INT((machine_int_t)(MICROPY_FLOAT_C_FUN(trunc)(mp_obj_float_get(args[0]))));
35+
#endif
2836
} else {
2937
return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
3038
}

0 commit comments

Comments
 (0)