openfile(): Go back to opening the files in text mode. This undoes
[python/dscho.git] / Lib / test / test_math.py
blob2d9b55b2f506a2b1b88f9b56451527b9994b367b
1 # Python test set -- math module
2 # XXXX Should not do tests around zero only
4 from test.test_support import TestFailed, verbose
6 seps='1e-05'
7 eps = eval(seps)
8 print 'math module, testing with eps', seps
9 import math
11 def testit(name, value, expected):
12 if abs(value-expected) > eps:
13 raise TestFailed, '%s returned %f, expected %f'%\
14 (name, value, expected)
16 print 'constants'
17 testit('pi', math.pi, 3.1415926)
18 testit('e', math.e, 2.7182818)
20 print 'acos'
21 testit('acos(-1)', math.acos(-1), math.pi)
22 testit('acos(0)', math.acos(0), math.pi/2)
23 testit('acos(1)', math.acos(1), 0)
25 print 'asin'
26 testit('asin(-1)', math.asin(-1), -math.pi/2)
27 testit('asin(0)', math.asin(0), 0)
28 testit('asin(1)', math.asin(1), math.pi/2)
30 print 'atan'
31 testit('atan(-1)', math.atan(-1), -math.pi/4)
32 testit('atan(0)', math.atan(0), 0)
33 testit('atan(1)', math.atan(1), math.pi/4)
35 print 'atan2'
36 testit('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
37 testit('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
38 testit('atan2(0, 1)', math.atan2(0, 1), 0)
39 testit('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
40 testit('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
42 print 'ceil'
43 testit('ceil(0.5)', math.ceil(0.5), 1)
44 testit('ceil(1.0)', math.ceil(1.0), 1)
45 testit('ceil(1.5)', math.ceil(1.5), 2)
46 testit('ceil(-0.5)', math.ceil(-0.5), 0)
47 testit('ceil(-1.0)', math.ceil(-1.0), -1)
48 testit('ceil(-1.5)', math.ceil(-1.5), -1)
50 print 'cos'
51 testit('cos(-pi/2)', math.cos(-math.pi/2), 0)
52 testit('cos(0)', math.cos(0), 1)
53 testit('cos(pi/2)', math.cos(math.pi/2), 0)
54 testit('cos(pi)', math.cos(math.pi), -1)
56 print 'cosh'
57 testit('cosh(0)', math.cosh(0), 1)
58 testit('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert
60 print 'degrees'
61 testit('degrees(pi)', math.degrees(math.pi), 180.0)
62 testit('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
63 testit('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
65 print 'exp'
66 testit('exp(-1)', math.exp(-1), 1/math.e)
67 testit('exp(0)', math.exp(0), 1)
68 testit('exp(1)', math.exp(1), math.e)
70 print 'fabs'
71 testit('fabs(-1)', math.fabs(-1), 1)
72 testit('fabs(0)', math.fabs(0), 0)
73 testit('fabs(1)', math.fabs(1), 1)
75 print 'floor'
76 testit('floor(0.5)', math.floor(0.5), 0)
77 testit('floor(1.0)', math.floor(1.0), 1)
78 testit('floor(1.5)', math.floor(1.5), 1)
79 testit('floor(-0.5)', math.floor(-0.5), -1)
80 testit('floor(-1.0)', math.floor(-1.0), -1)
81 testit('floor(-1.5)', math.floor(-1.5), -2)
83 print 'fmod'
84 testit('fmod(10,1)', math.fmod(10,1), 0)
85 testit('fmod(10,0.5)', math.fmod(10,0.5), 0)
86 testit('fmod(10,1.5)', math.fmod(10,1.5), 1)
87 testit('fmod(-10,1)', math.fmod(-10,1), 0)
88 testit('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
89 testit('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
91 print 'frexp'
92 def testfrexp(name, (mant, exp), (emant, eexp)):
93 if abs(mant-emant) > eps or exp != eexp:
94 raise TestFailed, '%s returned %s, expected %s'%\
95 (name, `mant, exp`, `emant,eexp`)
97 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
98 testfrexp('frexp(0)', math.frexp(0), (0, 0))
99 testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
100 testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
102 print 'hypot'
103 testit('hypot(0,0)', math.hypot(0,0), 0)
104 testit('hypot(3,4)', math.hypot(3,4), 5)
106 print 'ldexp'
107 testit('ldexp(0,1)', math.ldexp(0,1), 0)
108 testit('ldexp(1,1)', math.ldexp(1,1), 2)
109 testit('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
110 testit('ldexp(-1,1)', math.ldexp(-1,1), -2)
112 print 'log'
113 testit('log(1/e)', math.log(1/math.e), -1)
114 testit('log(1)', math.log(1), 0)
115 testit('log(e)', math.log(math.e), 1)
117 print 'log10'
118 testit('log10(0.1)', math.log10(0.1), -1)
119 testit('log10(1)', math.log10(1), 0)
120 testit('log10(10)', math.log10(10), 1)
122 print 'modf'
123 def testmodf(name, (v1, v2), (e1, e2)):
124 if abs(v1-e1) > eps or abs(v2-e2):
125 raise TestFailed, '%s returned %s, expected %s'%\
126 (name, `v1,v2`, `e1,e2`)
128 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
129 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
131 print 'pow'
132 testit('pow(0,1)', math.pow(0,1), 0)
133 testit('pow(1,0)', math.pow(1,0), 1)
134 testit('pow(2,1)', math.pow(2,1), 2)
135 testit('pow(2,-1)', math.pow(2,-1), 0.5)
137 print 'radians'
138 testit('radians(180)', math.radians(180), math.pi)
139 testit('radians(90)', math.radians(90), math.pi/2)
140 testit('radians(-45)', math.radians(-45), -math.pi/4)
142 print 'sin'
143 testit('sin(0)', math.sin(0), 0)
144 testit('sin(pi/2)', math.sin(math.pi/2), 1)
145 testit('sin(-pi/2)', math.sin(-math.pi/2), -1)
147 print 'sinh'
148 testit('sinh(0)', math.sinh(0), 0)
149 testit('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
150 testit('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
152 print 'sqrt'
153 testit('sqrt(0)', math.sqrt(0), 0)
154 testit('sqrt(1)', math.sqrt(1), 1)
155 testit('sqrt(4)', math.sqrt(4), 2)
157 print 'tan'
158 testit('tan(0)', math.tan(0), 0)
159 testit('tan(pi/4)', math.tan(math.pi/4), 1)
160 testit('tan(-pi/4)', math.tan(-math.pi/4), -1)
162 print 'tanh'
163 testit('tanh(0)', math.tanh(0), 0)
164 testit('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
166 # RED_FLAG 16-Oct-2000 Tim
167 # While 2.0 is more consistent about exceptions than previous releases, it
168 # still fails this part of the test on some platforms. For now, we only
169 # *run* test_exceptions() in verbose mode, so that this isn't normally
170 # tested.
172 def test_exceptions():
173 print 'exceptions'
174 try:
175 x = math.exp(-1000000000)
176 except:
177 # mathmodule.c is failing to weed out underflows from libm, or
178 # we've got an fp format with huge dynamic range
179 raise TestFailed("underflowing exp() should not have raised "
180 "an exception")
181 if x != 0:
182 raise TestFailed("underflowing exp() should have returned 0")
184 # If this fails, probably using a strict IEEE-754 conforming libm, and x
185 # is +Inf afterwards. But Python wants overflows detected by default.
186 try:
187 x = math.exp(1000000000)
188 except OverflowError:
189 pass
190 else:
191 raise TestFailed("overflowing exp() didn't trigger OverflowError")
193 # If this fails, it could be a puzzle. One odd possibility is that
194 # mathmodule.c's macros are getting confused while comparing
195 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
196 # as a result (and so raising OverflowError instead).
197 try:
198 x = math.sqrt(-1.0)
199 except ValueError:
200 pass
201 else:
202 raise TestFailed("sqrt(-1) didn't raise ValueError")
204 if verbose:
205 test_exceptions()