Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit49f81d5

Browse files
committed
tests/float/math_constants.py: Test actual e and pi constant values.
The existing test for `math.e` and `math.pi` constants can fail on certaintargets if the functions `math.exp()` and/or `math.cos()` are not accurateenough (eg out by an LSB of float precision). For example this testcurrently fails on PYBD_SF6 which uses double precision floats (and that'sdue to the `lib/libm_dbl/exp.c` implementation not being exact).This commit changes this constant test so that it tests the actual constantvalue, not the evaluation of `exp()` and `cos()` functions.Signed-off-by: Damien George <damien@micropython.org>
1 parentdc1af38 commit49f81d5

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

‎tests/float/math_constants.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
11
# Tests various constants of the math module.
2+
3+
importsys
4+
25
try:
3-
importmath
4-
frommathimportexp,cos
6+
fromarrayimportarray
7+
frommathimporte,pi
58
exceptImportError:
69
print("SKIP")
710
raiseSystemExit
811

9-
print(math.e==exp(1.0))
12+
# Hexadecimal representations of e and pi constants.
13+
e_truth_single=0x402DF854
14+
pi_truth_single=0x40490FDB
15+
e_truth_double=0x4005BF0A8B145769
16+
pi_truth_double=0x400921FB54442D18
17+
18+
# Detect the floating-point precision of the system, to determine the exact values of
19+
# the constants (parsing the float from a decimal string can lead to inaccuracies).
20+
iffloat("1e300")==float("inf"):
21+
# Single precision floats.
22+
e_truth=array("f",e_truth_single.to_bytes(4,sys.byteorder))[0]
23+
pi_truth=array("f",pi_truth_single.to_bytes(4,sys.byteorder))[0]
24+
else:
25+
# Double precision floats.
26+
e_truth=array("d",e_truth_double.to_bytes(8,sys.byteorder))[0]
27+
pi_truth=array("d",pi_truth_double.to_bytes(8,sys.byteorder))[0]
1028

11-
print(cos(math.pi))
29+
print("e:",e==e_truthor (e,e_truth,e-e_truth))
30+
print("pi:",pi==pi_truthor (pi,pi_truth,pi-pi_truth))

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp