blob: 6f742bf6d50016d513ebda1299e63f1110726f9a [file] [log] [blame]
Guido van Rossumfcce6301996-08-08 18:26:25 +00001# Python test set -- math module
2# XXXX Should not do tests around zero only
3
Guido van Rossum2242f2f2001-04-11 20:58:20 +00004import sys
Guido van Rossumfcce6301996-08-08 18:26:25 +00005from test_support import *
6
Guido van Rossum5ab007b1996-08-29 19:00:46 +00007seps='1e-05'
8eps = eval(seps)
9print 'math module, testing with eps', seps
Guido van Rossumfcce6301996-08-08 18:26:25 +000010import math
11
12def testit(name, value, expected):
Fred Drake004d5e62000-10-23 17:22:08 +000013 if abs(value-expected) > eps:
14 raise TestFailed, '%s returned %f, expected %f'%\
15 (name, value, expected)
Guido van Rossumfcce6301996-08-08 18:26:25 +000016
17print 'constants'
18testit('pi', math.pi, 3.1415926)
19testit('e', math.e, 2.7182818)
20
21print 'acos'
22testit('acos(-1)', math.acos(-1), math.pi)
23testit('acos(0)', math.acos(0), math.pi/2)
24testit('acos(1)', math.acos(1), 0)
25
26print 'asin'
27testit('asin(-1)', math.asin(-1), -math.pi/2)
28testit('asin(0)', math.asin(0), 0)
29testit('asin(1)', math.asin(1), math.pi/2)
30
31print 'atan'
32testit('atan(-1)', math.atan(-1), -math.pi/4)
33testit('atan(0)', math.atan(0), 0)
34testit('atan(1)', math.atan(1), math.pi/4)
35
36print 'atan2'
37testit('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
38testit('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
Guido van Rossum2242f2f2001-04-11 20:58:20 +000039if sys.platform in ['unixware7']:
40 testit('atan2(0, 1)', math.atan2(0, 1), math.pi)
41else:
42 testit('atan2(0, 1)', math.atan2(0, 1), 0)
Guido van Rossumfcce6301996-08-08 18:26:25 +000043testit('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
44testit('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
45
46print 'ceil'
47testit('ceil(0.5)', math.ceil(0.5), 1)
48testit('ceil(1.0)', math.ceil(1.0), 1)
49testit('ceil(1.5)', math.ceil(1.5), 2)
50testit('ceil(-0.5)', math.ceil(-0.5), 0)
51testit('ceil(-1.0)', math.ceil(-1.0), -1)
52testit('ceil(-1.5)', math.ceil(-1.5), -1)
53
54print 'cos'
55testit('cos(-pi/2)', math.cos(-math.pi/2), 0)
56testit('cos(0)', math.cos(0), 1)
57testit('cos(pi/2)', math.cos(math.pi/2), 0)
58testit('cos(pi)', math.cos(math.pi), -1)
59
60print 'cosh'
61testit('cosh(0)', math.cosh(0), 1)
62testit('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert
63
64print 'exp'
65testit('exp(-1)', math.exp(-1), 1/math.e)
66testit('exp(0)', math.exp(0), 1)
67testit('exp(1)', math.exp(1), math.e)
68
69print 'fabs'
70testit('fabs(-1)', math.fabs(-1), 1)
71testit('fabs(0)', math.fabs(0), 0)
72testit('fabs(1)', math.fabs(1), 1)
73
74print 'floor'
75testit('floor(0.5)', math.floor(0.5), 0)
76testit('floor(1.0)', math.floor(1.0), 1)
77testit('floor(1.5)', math.floor(1.5), 1)
78testit('floor(-0.5)', math.floor(-0.5), -1)
79testit('floor(-1.0)', math.floor(-1.0), -1)
80testit('floor(-1.5)', math.floor(-1.5), -2)
81
82print 'fmod'
83testit('fmod(10,1)', math.fmod(10,1), 0)
84testit('fmod(10,0.5)', math.fmod(10,0.5), 0)
85testit('fmod(10,1.5)', math.fmod(10,1.5), 1)
86testit('fmod(-10,1)', math.fmod(-10,1), 0)
87testit('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
88testit('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
89
90print 'frexp'
91def testfrexp(name, (mant, exp), (emant, eexp)):
Fred Drake132dce22000-12-12 23:11:42 +000092 if abs(mant-emant) > eps or exp != eexp:
Fred Drake004d5e62000-10-23 17:22:08 +000093 raise TestFailed, '%s returned %s, expected %s'%\
94 (name, `mant, exp`, `emant,eexp`)
Guido van Rossumfcce6301996-08-08 18:26:25 +000095
96testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
97testfrexp('frexp(0)', math.frexp(0), (0, 0))
98testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
99testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
100
101print 'hypot'
102testit('hypot(0,0)', math.hypot(0,0), 0)
103testit('hypot(3,4)', math.hypot(3,4), 5)
104
105print 'ldexp'
106testit('ldexp(0,1)', math.ldexp(0,1), 0)
107testit('ldexp(1,1)', math.ldexp(1,1), 2)
108testit('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
109testit('ldexp(-1,1)', math.ldexp(-1,1), -2)
110
111print 'log'
112testit('log(1/e)', math.log(1/math.e), -1)
113testit('log(1)', math.log(1), 0)
114testit('log(e)', math.log(math.e), 1)
115
116print 'log10'
117testit('log10(0.1)', math.log10(0.1), -1)
118testit('log10(1)', math.log10(1), 0)
119testit('log10(10)', math.log10(10), 1)
120
121print 'modf'
122def testmodf(name, (v1, v2), (e1, e2)):
Fred Drake004d5e62000-10-23 17:22:08 +0000123 if abs(v1-e1) > eps or abs(v2-e2):
124 raise TestFailed, '%s returned %s, expected %s'%\
125 (name, `v1,v2`, `e1,e2`)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000126
127testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
128testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
129
130print 'pow'
131testit('pow(0,1)', math.pow(0,1), 0)
132testit('pow(1,0)', math.pow(1,0), 1)
133testit('pow(2,1)', math.pow(2,1), 2)
134testit('pow(2,-1)', math.pow(2,-1), 0.5)
135
Guido van Rossumfcce6301996-08-08 18:26:25 +0000136print 'sin'
137testit('sin(0)', math.sin(0), 0)
138testit('sin(pi/2)', math.sin(math.pi/2), 1)
139testit('sin(-pi/2)', math.sin(-math.pi/2), -1)
140
141print 'sinh'
142testit('sinh(0)', math.sinh(0), 0)
143testit('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
144testit('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
145
146print 'sqrt'
147testit('sqrt(0)', math.sqrt(0), 0)
148testit('sqrt(1)', math.sqrt(1), 1)
149testit('sqrt(4)', math.sqrt(4), 2)
150
151print 'tan'
152testit('tan(0)', math.tan(0), 0)
153testit('tan(pi/4)', math.tan(math.pi/4), 1)
154testit('tan(-pi/4)', math.tan(-math.pi/4), -1)
155
156print 'tanh'
157testit('tanh(0)', math.tanh(0), 0)
158testit('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
Tim Peters1d120612000-10-12 06:10:25 +0000159
Tim Peters98c81842000-10-16 17:35:13 +0000160# RED_FLAG 16-Oct-2000 Tim
161# While 2.0 is more consistent about exceptions than previous releases, it
162# still fails this part of the test on some platforms. For now, we only
163# *run* test_exceptions() in verbose mode, so that this isn't normally
164# tested.
Tim Peters1d120612000-10-12 06:10:25 +0000165
Tim Peters98c81842000-10-16 17:35:13 +0000166def test_exceptions():
167 print 'exceptions'
168 try:
169 x = math.exp(-1000000000)
170 except:
171 # mathmodule.c is failing to weed out underflows from libm, or
172 # we've got an fp format with huge dynamic range
173 raise TestFailed("underflowing exp() should not have raised "
174 "an exception")
175 if x != 0:
176 raise TestFailed("underflowing exp() should have returned 0")
Tim Peters1d120612000-10-12 06:10:25 +0000177
Tim Peters98c81842000-10-16 17:35:13 +0000178 # If this fails, probably using a strict IEEE-754 conforming libm, and x
179 # is +Inf afterwards. But Python wants overflows detected by default.
180 try:
181 x = math.exp(1000000000)
182 except OverflowError:
183 pass
184 else:
185 raise TestFailed("overflowing exp() didn't trigger OverflowError")
Tim Peters1d120612000-10-12 06:10:25 +0000186
Tim Peters98c81842000-10-16 17:35:13 +0000187 # If this fails, it could be a puzzle. One odd possibility is that
188 # mathmodule.c's CHECK() macro is getting confused while comparing
189 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
190 # as a result (and so raising OverflowError instead).
191 try:
192 x = math.sqrt(-1.0)
193 except ValueError:
194 pass
195 else:
196 raise TestFailed("sqrt(-1) didn't raise ValueError")
197
198if verbose:
199 test_exceptions()