Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
1e8ca3a3
Commit
1e8ca3a3
authored
Jul 14, 2015
by
Sebastian Plamauer
Committed by
Paul Sokolovsky
Jul 19, 2015
Browse files
modbuiltins: Implement round() to precision.
parent
ab14c304
Changes
3
Hide whitespace changes
Inline
Side-by-side
py/modbuiltins.c
View file @
1e8ca3a3
...
...
@@ -431,7 +431,6 @@ STATIC mp_obj_t mp_builtin_repr(mp_obj_t o_in) {
MP_DEFINE_CONST_FUN_OBJ_1
(
mp_builtin_repr_obj
,
mp_builtin_repr
);
STATIC
mp_obj_t
mp_builtin_round
(
mp_uint_t
n_args
,
const
mp_obj_t
*
args
)
{
// TODO really support second arg
mp_obj_t
o_in
=
args
[
0
];
if
(
MP_OBJ_IS_INT
(
o_in
))
{
return
o_in
;
...
...
@@ -440,9 +439,11 @@ STATIC mp_obj_t mp_builtin_round(mp_uint_t n_args, const mp_obj_t *args) {
mp_int_t
num_dig
=
0
;
if
(
n_args
>
1
)
{
num_dig
=
mp_obj_get_int
(
args
[
1
]);
if
(
num_dig
>
0
)
{
mp_not_implemented
(
"round(..., N>0)"
);
}
mp_float_t
val
=
mp_obj_get_float
(
o_in
);
mp_float_t
mult
=
MICROPY_FLOAT_C_FUN
(
pow
)(
10
,
num_dig
);
// TODO may lead to overflow
mp_float_t
rounded
=
MICROPY_FLOAT_C_FUN
(
round
)(
val
*
mult
)
/
mult
;
return
mp_obj_new_float
(
rounded
);
}
mp_float_t
val
=
mp_obj_get_float
(
o_in
);
mp_float_t
rounded
=
MICROPY_FLOAT_C_FUN
(
round
)(
val
);
...
...
tests/basics/builtin_round.py
View file @
1e8ca3a3
...
...
@@ -2,7 +2,7 @@
tests
=
[
False
,
True
,
0
,
1
,
-
1
,
10
,
0
,
1
,
-
1
,
10
]
for
t
in
tests
:
print
(
round
(
t
))
tests/float/builtin_float_round.py
View file @
1e8ca3a3
...
...
@@ -2,10 +2,11 @@
# check basic cases
tests
=
[
0.0
,
1.0
,
0.1
,
-
0.1
,
123.4
,
123.6
,
-
123.4
,
-
123.6
[
0.0
],
[
1.0
],
[
0.1
],
[
-
0.1
],
[
123.4
],
[
123.6
],
[
-
123.4
],
[
-
123.6
],
[
1.234567
,
5
],
[
1.23456
,
1
],
[
1.23456
,
0
],
[
1234.56
,
-
2
]
]
for
t
in
tests
:
print
(
round
(
t
))
print
(
round
(
*
t
))
# check .5 cases
for
i
in
range
(
11
):
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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