py: Add explicit conversion from float to int via int().

This commit is contained in:
Damien George 2014-03-30 23:13:16 +01:00
parent 804760bfca
commit 6433bd927a
1 changed files with 8 additions and 0 deletions

View File

@ -10,6 +10,10 @@
#include "mpz.h"
#include "objint.h"
#if MICROPY_ENABLE_FLOAT
#include <math.h>
#endif
// This dispatcher function is expected to be independent of the implementation
// of long int
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_
uint l;
const char *s = mp_obj_str_get_data(args[0], &l);
return mp_parse_num_integer(s, l, 0);
#if MICROPY_ENABLE_FLOAT
} else if (MP_OBJ_IS_TYPE(args[0], &mp_type_float)) {
return MP_OBJ_NEW_SMALL_INT((machine_int_t)(MICROPY_FLOAT_C_FUN(trunc)(mp_obj_float_get(args[0]))));
#endif
} else {
return MP_OBJ_NEW_SMALL_INT(mp_obj_get_int(args[0]));
}