Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
6fd4b36b
Commit
6fd4b36b
authored
Jan 2, 2015
by
Damien George
Browse files
Options
Downloads
Patches
Plain Diff
py: Raise exception if trying to convert inf/nan to int.
parent
6e0b6d02
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
py/mpz.c
+2
-9
2 additions, 9 deletions
py/mpz.c
py/objint_mpz.c
+10
-3
10 additions, 3 deletions
py/objint_mpz.c
tests/float/float2int.py
+12
-0
12 additions, 0 deletions
tests/float/float2int.py
with
24 additions
and
12 deletions
py/mpz.c
+
2
−
9
View file @
6fd4b36b
...
@@ -711,16 +711,9 @@ typedef uint32_t mp_float_int_t;
...
@@ -711,16 +711,9 @@ typedef uint32_t mp_float_int_t;
// value == 0 || value < 1
// value == 0 || value < 1
mpz_init_zero
(
z
);
mpz_init_zero
(
z
);
}
else
if
(
u
.
p
.
exp
==
((
1
<<
EXP_SZ
)
-
1
))
{
}
else
if
(
u
.
p
.
exp
==
((
1
<<
EXP_SZ
)
-
1
))
{
// inf or NaN
// u.p.frc == 0 indicates inf, else NaN
#if 0
// should be handled by caller
// TODO: this probably isn't the right place to throw an exception
if(u.p.frc == 0)
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OverflowError, "cannot convert float infinity to integer"));
else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "cannot convert float NaN to integer"));
#else
mpz_init_zero
(
z
);
mpz_init_zero
(
z
);
#endif
}
else
{
}
else
{
const
int
adj_exp
=
(
int
)
u
.
p
.
exp
-
((
1
<<
(
EXP_SZ
-
1
))
-
1
);
const
int
adj_exp
=
(
int
)
u
.
p
.
exp
-
((
1
<<
(
EXP_SZ
-
1
))
-
1
);
if
(
adj_exp
<
0
)
{
if
(
adj_exp
<
0
)
{
...
...
This diff is collapsed.
Click to expand it.
py/objint_mpz.c
+
10
−
3
View file @
6fd4b36b
...
@@ -298,10 +298,17 @@ mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
...
@@ -298,10 +298,17 @@ mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value) {
#if MICROPY_PY_BUILTINS_FLOAT
#if MICROPY_PY_BUILTINS_FLOAT
mp_obj_t
mp_obj_new_int_from_float
(
mp_float_t
val
)
{
mp_obj_t
mp_obj_new_int_from_float
(
mp_float_t
val
)
{
int
cl
=
fpclassify
(
val
);
if
(
cl
==
FP_INFINITE
)
{
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_OverflowError
,
"can't convert inf to int"
));
}
else
if
(
cl
==
FP_NAN
)
{
nlr_raise
(
mp_obj_new_exception_msg_varg
(
&
mp_type_ValueError
,
"can't convert NaN to int"
));
}
else
{
mp_obj_int_t
*
o
=
mp_obj_int_new_mpz
();
mp_obj_int_t
*
o
=
mp_obj_int_new_mpz
();
mpz_set_from_float
(
&
o
->
mpz
,
val
);
mpz_set_from_float
(
&
o
->
mpz
,
val
);
return
o
;
return
o
;
}
}
}
#endif
#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_t
mp_obj_new_int_from_str_len
(
const
char
**
str
,
mp_uint_t
len
,
bool
neg
,
mp_uint_t
base
)
{
...
...
This diff is collapsed.
Click to expand it.
tests/float/float2int.py
+
12
−
0
View file @
6fd4b36b
...
@@ -22,3 +22,15 @@ for i in range(0,23):
...
@@ -22,3 +22,15 @@ for i in range(0,23):
print
(
'
fail: 10**%u was %u digits long
'
%
(
i
,
digcnt
));
print
(
'
fail: 10**%u was %u digits long
'
%
(
i
,
digcnt
));
testpass
=
False
testpass
=
False
print
(
"
power of 10 test: %s
"
%
(
testpass
and
'
passed
'
or
'
failed
'
))
print
(
"
power of 10 test: %s
"
%
(
testpass
and
'
passed
'
or
'
failed
'
))
# test inf conversion
try
:
int
(
float
(
'
inf
'
))
except
OverflowError
:
print
(
"
OverflowError
"
)
# test nan conversion
try
:
int
(
float
(
'
nan
'
))
except
ValueError
:
print
(
"
ValueError
"
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment