parent
fa73c9cb25
commit
1559a97810
|
@ -0,0 +1,33 @@
|
||||||
|
/*****************************************************************************/
|
||||||
|
/*****************************************************************************/
|
||||||
|
// roundf from musl-0.9.15
|
||||||
|
/*****************************************************************************/
|
||||||
|
/*****************************************************************************/
|
||||||
|
|
||||||
|
#include "libm.h"
|
||||||
|
|
||||||
|
float roundf(float x)
|
||||||
|
{
|
||||||
|
union {float f; uint32_t i;} u = {x};
|
||||||
|
int e = u.i >> 23 & 0xff;
|
||||||
|
float_t y;
|
||||||
|
|
||||||
|
if (e >= 0x7f+23)
|
||||||
|
return x;
|
||||||
|
if (u.i >> 31)
|
||||||
|
x = -x;
|
||||||
|
if (e < 0x7f-1) {
|
||||||
|
FORCE_EVAL(x + 0x1p23f);
|
||||||
|
return 0*u.f;
|
||||||
|
}
|
||||||
|
y = (float)(x + 0x1p23f) - 0x1p23f - x;
|
||||||
|
if (y > 0.5f)
|
||||||
|
y = y + x - 1;
|
||||||
|
else if (y <= -0.5f)
|
||||||
|
y = y + x + 1;
|
||||||
|
else
|
||||||
|
y = y + x;
|
||||||
|
if (u.i >> 31)
|
||||||
|
y = -y;
|
||||||
|
return y;
|
||||||
|
}
|
23
py/builtin.c
23
py/builtin.c
|
@ -448,9 +448,30 @@ STATIC mp_obj_t mp_builtin_repr(mp_obj_t o_in) {
|
||||||
vstr_free(vstr);
|
vstr_free(vstr);
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_repr_obj, mp_builtin_repr);
|
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_repr_obj, mp_builtin_repr);
|
||||||
|
|
||||||
|
STATIC mp_obj_t mp_builtin_round(mp_obj_t o_in) {
|
||||||
|
// TODO support second arg
|
||||||
|
if (MP_OBJ_IS_INT(o_in)) {
|
||||||
|
return o_in;
|
||||||
|
}
|
||||||
|
#if MICROPY_PY_BUILTINS_FLOAT
|
||||||
|
mp_float_t val = mp_obj_get_float(o_in);
|
||||||
|
mp_float_t rounded = MICROPY_FLOAT_C_FUN(round)(val);
|
||||||
|
mp_int_t r = rounded;
|
||||||
|
// make rounded value even if it was halfway between ints
|
||||||
|
if (val - rounded == 0.5) {
|
||||||
|
r = (r + 1) & (~1);
|
||||||
|
} else if (val - rounded == -0.5) {
|
||||||
|
r &= ~1;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
mp_int_t r = mp_obj_get_int(o_in);
|
||||||
|
#endif
|
||||||
|
return mp_obj_new_int(r);
|
||||||
|
}
|
||||||
|
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_round_obj, mp_builtin_round);
|
||||||
|
|
||||||
STATIC mp_obj_t mp_builtin_sum(mp_uint_t n_args, const mp_obj_t *args) {
|
STATIC mp_obj_t mp_builtin_sum(mp_uint_t n_args, const mp_obj_t *args) {
|
||||||
assert(1 <= n_args && n_args <= 2);
|
assert(1 <= n_args && n_args <= 2);
|
||||||
mp_obj_t value;
|
mp_obj_t value;
|
||||||
|
|
|
@ -61,6 +61,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_builtin_ord_obj);
|
||||||
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_pow_obj);
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_pow_obj);
|
||||||
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_print_obj);
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_print_obj);
|
||||||
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_repr_obj);
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_repr_obj);
|
||||||
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_round_obj);
|
||||||
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sorted_obj);
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sorted_obj);
|
||||||
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sum_obj);
|
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_sum_obj);
|
||||||
|
|
||||||
|
|
|
@ -118,6 +118,7 @@ STATIC const mp_map_elem_t mp_builtin_object_table[] = {
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_pow), (mp_obj_t)&mp_builtin_pow_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_pow), (mp_obj_t)&mp_builtin_pow_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_print), (mp_obj_t)&mp_builtin_print_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_print), (mp_obj_t)&mp_builtin_print_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_repr), (mp_obj_t)&mp_builtin_repr_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_repr), (mp_obj_t)&mp_builtin_repr_obj },
|
||||||
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_round), (mp_obj_t)&mp_builtin_round_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_sorted), (mp_obj_t)&mp_builtin_sorted_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_sorted), (mp_obj_t)&mp_builtin_sorted_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_sum), (mp_obj_t)&mp_builtin_sum_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_sum), (mp_obj_t)&mp_builtin_sum_obj },
|
||||||
|
|
||||||
|
|
|
@ -201,6 +201,7 @@ Q(range)
|
||||||
Q(read)
|
Q(read)
|
||||||
Q(repr)
|
Q(repr)
|
||||||
Q(reversed)
|
Q(reversed)
|
||||||
|
Q(round)
|
||||||
Q(sorted)
|
Q(sorted)
|
||||||
Q(staticmethod)
|
Q(staticmethod)
|
||||||
Q(sum)
|
Q(sum)
|
||||||
|
|
|
@ -68,6 +68,7 @@ SRC_LIB = $(addprefix lib/,\
|
||||||
libm/atanf.c \
|
libm/atanf.c \
|
||||||
libm/atan2f.c \
|
libm/atan2f.c \
|
||||||
libm/fmodf.c \
|
libm/fmodf.c \
|
||||||
|
libm/roundf.c \
|
||||||
)
|
)
|
||||||
|
|
||||||
SRC_C = \
|
SRC_C = \
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
# test round()
|
||||||
|
|
||||||
|
# check basic cases
|
||||||
|
tests = [
|
||||||
|
False, True,
|
||||||
|
0, 1, -1, 10,
|
||||||
|
0.0, 1.0, 0.1, -0.1, 123.4, 123.6, -123.4, -123.6
|
||||||
|
]
|
||||||
|
for t in tests:
|
||||||
|
print(round(t))
|
||||||
|
|
||||||
|
# check .5 cases
|
||||||
|
for i in range(11):
|
||||||
|
print(round((i - 5) / 2))
|
Loading…
Reference in New Issue