annotate Lib/test/test_math.py @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 33f4a91f871b
children 1ea8cf885f7b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Python test set -- math module
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 # XXXX Should not do tests around zero only
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 from test_support import *
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
4179
33f4a91f871b Subtle hack so the eps printed is not dependent on the formatting
Guido van Rossum <guido@python.org>
parents: 3950
diff changeset
6 seps='1e-05'
33f4a91f871b Subtle hack so the eps printed is not dependent on the formatting
Guido van Rossum <guido@python.org>
parents: 3950
diff changeset
7 eps = eval(seps)
33f4a91f871b Subtle hack so the eps printed is not dependent on the formatting
Guido van Rossum <guido@python.org>
parents: 3950
diff changeset
8 print 'math module, testing with eps', seps
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 import math
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 def testit(name, value, expected):
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 if abs(value-expected) > eps:
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 raise TestFailed, '%s returned %f, expected %f'%\
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 (name, value, expected)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 print 'constants'
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 testit('pi', math.pi, 3.1415926)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 testit('e', math.e, 2.7182818)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 print 'acos'
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 testit('acos(-1)', math.acos(-1), math.pi)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 testit('acos(0)', math.acos(0), math.pi/2)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 testit('acos(1)', math.acos(1), 0)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
24
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 print 'asin'
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 testit('asin(-1)', math.asin(-1), -math.pi/2)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 testit('asin(0)', math.asin(0), 0)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 testit('asin(1)', math.asin(1), math.pi/2)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 print 'atan'
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 testit('atan(-1)', math.atan(-1), -math.pi/4)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 testit('atan(0)', math.atan(0), 0)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 testit('atan(1)', math.atan(1), math.pi/4)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 print 'atan2'
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 testit('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 testit('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 testit('atan2(0, 1)', math.atan2(0, 1), 0)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 testit('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 testit('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 print 'ceil'
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 testit('ceil(0.5)', math.ceil(0.5), 1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 testit('ceil(1.0)', math.ceil(1.0), 1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 testit('ceil(1.5)', math.ceil(1.5), 2)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 testit('ceil(-0.5)', math.ceil(-0.5), 0)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 testit('ceil(-1.0)', math.ceil(-1.0), -1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 testit('ceil(-1.5)', math.ceil(-1.5), -1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 print 'cos'
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 testit('cos(-pi/2)', math.cos(-math.pi/2), 0)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 testit('cos(0)', math.cos(0), 1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 testit('cos(pi/2)', math.cos(math.pi/2), 0)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 testit('cos(pi)', math.cos(math.pi), -1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 print 'cosh'
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 testit('cosh(0)', math.cosh(0), 1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 testit('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 print 'exp'
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 testit('exp(-1)', math.exp(-1), 1/math.e)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 testit('exp(0)', math.exp(0), 1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 testit('exp(1)', math.exp(1), math.e)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
64
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 print 'fabs'
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 testit('fabs(-1)', math.fabs(-1), 1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 testit('fabs(0)', math.fabs(0), 0)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 testit('fabs(1)', math.fabs(1), 1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
69
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 print 'floor'
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 testit('floor(0.5)', math.floor(0.5), 0)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 testit('floor(1.0)', math.floor(1.0), 1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 testit('floor(1.5)', math.floor(1.5), 1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 testit('floor(-0.5)', math.floor(-0.5), -1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 testit('floor(-1.0)', math.floor(-1.0), -1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 testit('floor(-1.5)', math.floor(-1.5), -2)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
77
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 print 'fmod'
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 testit('fmod(10,1)', math.fmod(10,1), 0)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 testit('fmod(10,0.5)', math.fmod(10,0.5), 0)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 testit('fmod(10,1.5)', math.fmod(10,1.5), 1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 testit('fmod(-10,1)', math.fmod(-10,1), 0)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 testit('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 testit('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
85
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 print 'frexp'
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 def testfrexp(name, (mant, exp), (emant, eexp)):
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 if abs(mant-emant) > eps or exp <> eexp:
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 raise TestFailed, '%s returned %s, expected %s'%\
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 (name, `mant, exp`, `emant,eexp`)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
91
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 testfrexp('frexp(0)', math.frexp(0), (0, 0))
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
96
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 print 'hypot'
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 testit('hypot(0,0)', math.hypot(0,0), 0)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 testit('hypot(3,4)', math.hypot(3,4), 5)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
100
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 print 'ldexp'
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 testit('ldexp(0,1)', math.ldexp(0,1), 0)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 testit('ldexp(1,1)', math.ldexp(1,1), 2)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 testit('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 testit('ldexp(-1,1)', math.ldexp(-1,1), -2)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
106
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 print 'log'
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 testit('log(1/e)', math.log(1/math.e), -1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 testit('log(1)', math.log(1), 0)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 testit('log(e)', math.log(math.e), 1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
111
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 print 'log10'
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 testit('log10(0.1)', math.log10(0.1), -1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 testit('log10(1)', math.log10(1), 0)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 testit('log10(10)', math.log10(10), 1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
116
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 print 'modf'
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 def testmodf(name, (v1, v2), (e1, e2)):
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 if abs(v1-e1) > eps or abs(v2-e2):
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 raise TestFailed, '%s returned %s, expected %s'%\
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 (name, `v1,v2`, `e1,e2`)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
122
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
125
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 print 'pow'
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 testit('pow(0,1)', math.pow(0,1), 0)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 testit('pow(1,0)', math.pow(1,0), 1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 testit('pow(2,1)', math.pow(2,1), 2)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 testit('pow(2,-1)', math.pow(2,-1), 0.5)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
131
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 print 'sin'
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 testit('sin(0)', math.sin(0), 0)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 testit('sin(pi/2)', math.sin(math.pi/2), 1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 testit('sin(-pi/2)', math.sin(-math.pi/2), -1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
136
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 print 'sinh'
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 testit('sinh(0)', math.sinh(0), 0)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 testit('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 testit('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
141
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 print 'sqrt'
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 testit('sqrt(0)', math.sqrt(0), 0)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 testit('sqrt(1)', math.sqrt(1), 1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 testit('sqrt(4)', math.sqrt(4), 2)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
146
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 print 'tan'
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 testit('tan(0)', math.tan(0), 0)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 testit('tan(pi/4)', math.tan(math.pi/4), 1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 testit('tan(-pi/4)', math.tan(-math.pi/4), -1)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
151
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 print 'tanh'
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 testit('tanh(0)', math.tanh(0), 0)
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 testit('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)