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
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
50e0a7b9
Commit
50e0a7b9
authored
9 years ago
by
Paul Sokolovsky
Browse files
Options
Downloads
Patches
Plain Diff
test/float2int_fp30: Variant of float2int for 30-bit stuffed float.
parent
fbb3c190
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
tests/float/float2int_fp30.py
+89
-0
89 additions, 0 deletions
tests/float/float2int_fp30.py
with
89 additions
and
0 deletions
tests/float/float2int_fp30.py
0 → 100644
+
89
−
0
View file @
50e0a7b9
# check cases converting float to int, relying only on single precision float
try
:
import
ustruct
as
struct
except
:
import
struct
# work out configuration values
is_64bit
=
struct
.
calcsize
(
"
P
"
)
==
8
# 0 = none, 1 = long long, 2 = mpz
try
:
dummy
=
0x7fffffffffffffff
try
:
if
(
0xffffffffffffffff
+
1
)
>
0
:
ll_type
=
2
else
:
ll_type
=
1
except
:
# in case the sum in the if statement above changes to raising an exception on overflow
ll_type
=
1
except
:
ll_type
=
0
# basic conversion
print
(
int
(
14187744.
))
print
(
"
%d
"
%
14187744.
)
if
ll_type
==
2
:
print
(
int
(
2.
**
100
))
print
(
"
%d
"
%
2.
**
100
)
testpass
=
True
p2_rng
=
((
30
,
63
,
127
),(
62
,
63
,
127
))[
is_64bit
][
ll_type
]
for
i
in
range
(
0
,
p2_rng
):
bitcnt
=
len
(
bin
(
int
(
2.
**
i
)))
-
3
;
if
i
!=
bitcnt
:
print
(
'
fail: 2.**%u was %u bits long
'
%
(
i
,
bitcnt
));
testpass
=
False
print
(
"
power of 2 test: %s
"
%
(
testpass
and
'
passed
'
or
'
failed
'
))
# TODO why does 10**12 fail this test for single precision float?
testpass
=
True
p10_rng
=
9
for
i
in
range
(
0
,
p10_rng
):
digcnt
=
len
(
str
(
int
(
10.
**
i
)))
-
1
;
if
i
!=
digcnt
:
print
(
'
fail: 10.**%u was %u digits long
'
%
(
i
,
digcnt
));
testpass
=
False
print
(
"
power of 10 test: %s
"
%
(
testpass
and
'
passed
'
or
'
failed
'
))
def
fp2int_test
(
num
,
name
,
should_fail
):
try
:
x
=
int
(
num
)
passed
=
~
should_fail
except
:
passed
=
should_fail
print
(
'
%s: %s
'
%
(
name
,
passed
and
'
passed
'
or
'
failed
'
))
if
ll_type
!=
2
:
if
ll_type
==
0
:
if
is_64bit
:
neg_bad_fp
=
-
1.00000005
*
2.
**
62.
pos_bad_fp
=
2.
**
62.
neg_good_fp
=
-
2.
**
62.
pos_good_fp
=
0.99999993
*
2.
**
62.
else
:
neg_bad_fp
=
-
1.00000005
*
2.
**
30.
pos_bad_fp
=
2.
**
30.
neg_good_fp
=
-
2.
**
30.
pos_good_fp
=
0.9999999499
*
2.
**
30.
else
:
neg_bad_fp
=
-
0.51
*
2.
**
64.
pos_bad_fp
=
2.
**
63.
neg_good_fp
=
-
2.
**
63.
pos_good_fp
=
1.9999998
*
2.
**
62.
fp2int_test
(
neg_bad_fp
,
'
neg bad
'
,
True
)
fp2int_test
(
pos_bad_fp
,
'
pos bad
'
,
True
)
fp2int_test
(
neg_good_fp
,
'
neg good
'
,
False
)
fp2int_test
(
pos_good_fp
,
'
pos good
'
,
False
)
else
:
fp2int_test
(
-
1.999999879
*
2.
**
126.
,
'
large neg
'
,
False
)
fp2int_test
(
1.999999879
*
2.
**
126.
,
'
large pos
'
,
False
)
fp2int_test
(
float
(
'
inf
'
),
'
inf test
'
,
True
)
fp2int_test
(
float
(
'
nan
'
),
'
NaN test
'
,
True
)
# test numbers < 1 (this used to fail; see issue #1044)
fp2int_test
(
0.0001
,
'
small num
'
,
False
)
struct
.
pack
(
'
I
'
,
int
(
1
/
2
))
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