summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-03-30 23:13:16 +0100
committerDamien George <damien.p.george@gmail.com>2014-03-30 23:13:16 +0100
commit6433bd927a0dbcc136eecd2593f44d0ee3f4ae25 (patch)
treea7f11eebf98bf225e8e524207eede6ee0d85c428 /py
parent804760bfca9bb8d49fe57e107375336eac9d3f79 (diff)
downloadmicropython-6433bd927a0dbcc136eecd2593f44d0ee3f4ae25.tar.gz
micropython-6433bd927a0dbcc136eecd2593f44d0ee3f4ae25.zip
py: Add explicit conversion from float to int via int().
Diffstat (limited to 'py')
-rw-r--r--py/objint.c8
1 files changed, 8 insertions, 0 deletions
diff --git a/py/objint.c b/py/objint.c
index 70f20e3cdb..27834b81fd 100644
--- a/py/objint.c
+++ b/py/objint.c
@@ -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]));
}