py: Partially fix float to int conversion.

This fixes conversion when float type has more mantissa bits than small int,
and float value has small exponent. This is for example the case of 32-bit
platform using doubles, and converting value of time.time(). Conversion of
floats with larg exponnet is still not handled correctly.
This commit is contained in:
Paul Sokolovsky 2014-12-30 00:22:10 +02:00
parent e3fa8278b4
commit 12033df511
5 changed files with 31 additions and 1 deletions

View File

@ -399,6 +399,7 @@ mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value);
mp_obj_t mp_obj_new_int_from_str_len(const char **str, mp_uint_t len, bool neg, mp_uint_t base);
mp_obj_t mp_obj_new_int_from_ll(long long val); // this must return a multi-precision integer object (or raise an overflow exception)
mp_obj_t mp_obj_new_int_from_ull(unsigned long long val); // this must return a multi-precision integer object (or raise an overflow exception)
mp_obj_t mp_obj_new_int_from_float(mp_float_t val);
mp_obj_t mp_obj_new_str(const char* data, mp_uint_t len, bool make_qstr_if_not_already);
mp_obj_t mp_obj_new_bytes(const byte* data, mp_uint_t len);
mp_obj_t mp_obj_new_bytearray(mp_uint_t n, void *items);

View File

@ -65,7 +65,7 @@ STATIC mp_obj_t mp_obj_int_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_
return mp_parse_num_integer(s, l, 0);
#if MICROPY_PY_BUILTINS_FLOAT
} else if (MP_OBJ_IS_TYPE(args[0], &mp_type_float)) {
return MP_OBJ_NEW_SMALL_INT((MICROPY_FLOAT_C_FUN(trunc)(mp_obj_float_get(args[0]))));
return mp_obj_new_int_from_float(mp_obj_float_get(args[0]));
#endif
} else {
// try to convert to small int (eg from bool)

View File

@ -40,6 +40,10 @@
#include "runtime0.h"
#include "runtime.h"
#if MICROPY_PY_BUILTINS_FLOAT
#include <math.h>
#endif
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
// Python3 no longer has "l" suffix for long ints. We allow to use it
@ -187,6 +191,14 @@ mp_obj_t mp_obj_new_int_from_ull(unsigned long long val) {
return o;
}
#if MICROPY_PY_BUILTINS_FLOAT
mp_obj_t mp_obj_new_int_from_float(mp_float_t val) {
// TODO raise an exception if the unsigned long long won't fit
long long i = MICROPY_FLOAT_C_FUN(trunc)(val);
return mp_obj_new_int_from_ll(i);
}
#endif
mp_obj_t mp_obj_new_int_from_str_len(const char **str, mp_uint_t len, bool neg, mp_uint_t base) {
// TODO this does not honor the given length of the string, but it all cases it should anyway be null terminated
// TODO check overflow

View File

@ -41,6 +41,10 @@
#include "runtime0.h"
#include "runtime.h"
#if MICROPY_PY_BUILTINS_FLOAT
#include <math.h>
#endif
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
#if MICROPY_PY_SYS_MAXSIZE
@ -298,6 +302,14 @@ mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
return mp_obj_new_int_from_ll(value);
}
#if MICROPY_PY_BUILTINS_FLOAT
mp_obj_t mp_obj_new_int_from_float(mp_float_t val) {
// TODO: This doesn't handle numbers with large exponent
long long i = MICROPY_FLOAT_C_FUN(trunc)(val);
return mp_obj_new_int_from_ll(i);
}
#endif
mp_obj_t mp_obj_new_int_from_str_len(const char **str, mp_uint_t len, bool neg, mp_uint_t base) {
mp_obj_int_t *o = mp_obj_int_new_mpz();
mp_uint_t n = mpz_set_from_str(&o->mpz, *str, len, neg, base);

5
tests/float/float2int.py Normal file
View File

@ -0,0 +1,5 @@
# This case occurs with time.time() values
print(int(1418774543.))
# TODO: General case with large exponent
#print(int(2.**100))