Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
1559a978
Commit
1559a978
authored
Oct 31, 2014
by
Damien George
Browse files
py: Add builtin round function.
Addresses issue #934.
parent
fa73c9cb
Changes
7
Show whitespace changes
Inline
Side-by-side
lib/libm/roundf.c
0 → 100644
View file @
1559a978
/*****************************************************************************/
/*****************************************************************************/
// 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
+
0x1
p23f
);
return
0
*
u
.
f
;
}
y
=
(
float
)(
x
+
0x1
p23f
)
-
0x1
p23f
-
x
;
if
(
y
>
0
.
5
f
)
y
=
y
+
x
-
1
;
else
if
(
y
<=
-
0
.
5
f
)
y
=
y
+
x
+
1
;
else
y
=
y
+
x
;
if
(
u
.
i
>>
31
)
y
=
-
y
;
return
y
;
}
py/builtin.c
View file @
1559a978
...
...
@@ -448,9 +448,30 @@ STATIC mp_obj_t mp_builtin_repr(mp_obj_t o_in) {
vstr_free
(
vstr
);
return
s
;
}
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
)
{
assert
(
1
<=
n_args
&&
n_args
<=
2
);
mp_obj_t
value
;
...
...
py/builtin.h
View file @
1559a978
...
...
@@ -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_print_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_sum_obj
);
...
...
py/builtintables.c
View file @
1559a978
...
...
@@ -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_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_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_sum
),
(
mp_obj_t
)
&
mp_builtin_sum_obj
},
...
...
py/qstrdefs.h
View file @
1559a978
...
...
@@ -201,6 +201,7 @@ Q(range)
Q
(
read
)
Q
(
repr
)
Q
(
reversed
)
Q
(
round
)
Q
(
sorted
)
Q
(
staticmethod
)
Q
(
sum
)
...
...
stmhal/Makefile
View file @
1559a978
...
...
@@ -68,6 +68,7 @@ SRC_LIB = $(addprefix lib/,\
libm/atanf.c
\
libm/atan2f.c
\
libm/fmodf.c
\
libm/roundf.c
\
)
SRC_C
=
\
...
...
tests/basics/builtin_round.py
0 → 100644
View file @
1559a978
# 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
))
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment