blob: 2d9b55b2f506a2b1b88f9b56451527b9994b367b [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
Barry Warsaw408b6d32002-07-30 23:27:12 +00004from test.test_support import TestFailed, verbose
Guido van Rossumfcce6301996-08-08 18:26:25 +00005
Guido van Rossum5ab007b1996-08-29 19:00:46 +00006seps='1e-05'
7eps = eval(seps)
8print 'math module, testing with eps', seps
Guido van Rossumfcce6301996-08-08 18:26:25 +00009import math
10
11def testit(name, value, expected):
Fred Drake004d5e62000-10-23 17:22:08 +000012 if abs(value-expected) > eps:
13 raise TestFailed, '%s returned %f, expected %f'%\
14 (name, value, expected)
Guido van Rossumfcce6301996-08-08 18:26:25 +000015
16print 'constants'
17testit('pi', math.pi, 3.1415926)
18testit('e', math.e, 2.7182818)
19
20print 'acos'
21testit('acos(-1)', math.acos(-1), math.pi)
22testit('acos(0)', math.acos(0), math.pi/2)
23testit('acos(1)', math.acos(1), 0)
24
25print 'asin'
26testit('asin(-1)', math.asin(-1), -math.pi/2)
27testit('asin(0)', math.asin(0), 0)
28testit('asin(1)', math.asin(1), math.pi/2)
29
30print 'atan'
31testit('atan(-1)', math.atan(-1), -math.pi/4)
32testit('atan(0)', math.atan(0), 0)
33testit('atan(1)', math.atan(1), math.pi/4)
34
35print 'atan2'
36testit('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
37testit('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
Tim Peters4642cb92001-04-12 00:24:41 +000038testit('atan2(0, 1)', math.atan2(0, 1), 0)
Guido van Rossumfcce6301996-08-08 18:26:25 +000039testit('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
40testit('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
41
42print 'ceil'
43testit('ceil(0.5)', math.ceil(0.5), 1)
44testit('ceil(1.0)', math.ceil(1.0), 1)
45testit('ceil(1.5)', math.ceil(1.5), 2)
46testit('ceil(-0.5)', math.ceil(-0.5), 0)
47testit('ceil(-1.0)', math.ceil(-1.0), -1)
48testit('ceil(-1.5)', math.ceil(-1.5), -1)
49
50print 'cos'
51testit('cos(-pi/2)', math.cos(-math.pi/2), 0)
52testit('cos(0)', math.cos(0), 1)
53testit('cos(pi/2)', math.cos(math.pi/2), 0)
54testit('cos(pi)', math.cos(math.pi), -1)
55
56print 'cosh'
57testit('cosh(0)', math.cosh(0), 1)
58testit('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert
59
Raymond Hettinger64108af2002-05-13 03:55:01 +000060print 'degrees'
61testit('degrees(pi)', math.degrees(math.pi), 180.0)
62testit('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
63testit('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
64
Guido van Rossumfcce6301996-08-08 18:26:25 +000065print 'exp'
66testit('exp(-1)', math.exp(-1), 1/math.e)
67testit('exp(0)', math.exp(0), 1)
68testit('exp(1)', math.exp(1), math.e)
69
70print 'fabs'
71testit('fabs(-1)', math.fabs(-1), 1)
72testit('fabs(0)', math.fabs(0), 0)
73testit('fabs(1)', math.fabs(1), 1)
74
75print 'floor'
76testit('floor(0.5)', math.floor(0.5), 0)
77testit('floor(1.0)', math.floor(1.0), 1)
78testit('floor(1.5)', math.floor(1.5), 1)
79testit('floor(-0.5)', math.floor(-0.5), -1)
80testit('floor(-1.0)', math.floor(-1.0), -1)
81testit('floor(-1.5)', math.floor(-1.5), -2)
82
83print 'fmod'
84testit('fmod(10,1)', math.fmod(10,1), 0)
85testit('fmod(10,0.5)', math.fmod(10,0.5), 0)
86testit('fmod(10,1.5)', math.fmod(10,1.5), 1)
87testit('fmod(-10,1)', math.fmod(-10,1), 0)
88testit('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
89testit('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
90
91print 'frexp'
92def testfrexp(name, (mant, exp), (emant, eexp)):
Fred Drake132dce22000-12-12 23:11:42 +000093 if abs(mant-emant) > eps or exp != eexp:
Fred Drake004d5e62000-10-23 17:22:08 +000094 raise TestFailed, '%s returned %s, expected %s'%\
95 (name, `mant, exp`, `emant,eexp`)
Guido van Rossumfcce6301996-08-08 18:26:25 +000096
97testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
98testfrexp('frexp(0)', math.frexp(0), (0, 0))
99testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
100testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
101
102print 'hypot'
103testit('hypot(0,0)', math.hypot(0,0), 0)
104testit('hypot(3,4)', math.hypot(3,4), 5)
105
106print 'ldexp'
107testit('ldexp(0,1)', math.ldexp(0,1), 0)
108testit('ldexp(1,1)', math.ldexp(1,1), 2)
109testit('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
110testit('ldexp(-1,1)', math.ldexp(-1,1), -2)
111
112print 'log'
113testit('log(1/e)', math.log(1/math.e), -1)
114testit('log(1)', math.log(1), 0)
115testit('log(e)', math.log(math.e), 1)
116
117print 'log10'
118testit('log10(0.1)', math.log10(0.1), -1)
119testit('log10(1)', math.log10(1), 0)
120testit('log10(10)', math.log10(10), 1)
121
122print 'modf'
123def testmodf(name, (v1, v2), (e1, e2)):
Fred Drake004d5e62000-10-23 17:22:08 +0000124 if abs(v1-e1) > eps or abs(v2-e2):
125 raise TestFailed, '%s returned %s, expected %s'%\
126 (name, `v1,v2`, `e1,e2`)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000127
128testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
129testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
130
131print 'pow'
132testit('pow(0,1)', math.pow(0,1), 0)
133testit('pow(1,0)', math.pow(1,0), 1)
134testit('pow(2,1)', math.pow(2,1), 2)
135testit('pow(2,-1)', math.pow(2,-1), 0.5)
136
Raymond Hettinger64108af2002-05-13 03:55:01 +0000137print 'radians'
138testit('radians(180)', math.radians(180), math.pi)
139testit('radians(90)', math.radians(90), math.pi/2)
Tim Peters8ac14952002-05-23 15:15:30 +0000140testit('radians(-45)', math.radians(-45), -math.pi/4)
Raymond Hettinger64108af2002-05-13 03:55:01 +0000141
Guido van Rossumfcce6301996-08-08 18:26:25 +0000142print 'sin'
143testit('sin(0)', math.sin(0), 0)
144testit('sin(pi/2)', math.sin(math.pi/2), 1)
145testit('sin(-pi/2)', math.sin(-math.pi/2), -1)
146
147print 'sinh'
148testit('sinh(0)', math.sinh(0), 0)
149testit('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
150testit('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
151
152print 'sqrt'
153testit('sqrt(0)', math.sqrt(0), 0)
154testit('sqrt(1)', math.sqrt(1), 1)
155testit('sqrt(4)', math.sqrt(4), 2)
156
157print 'tan'
158testit('tan(0)', math.tan(0), 0)
159testit('tan(pi/4)', math.tan(math.pi/4), 1)
160testit('tan(-pi/4)', math.tan(-math.pi/4), -1)
161
162print 'tanh'
163testit('tanh(0)', math.tanh(0), 0)
164testit('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
Tim Peters1d120612000-10-12 06:10:25 +0000165
Tim Peters98c81842000-10-16 17:35:13 +0000166# 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.
Tim Peters1d120612000-10-12 06:10:25 +0000171
Tim Peters98c81842000-10-16 17:35:13 +0000172def 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")
Tim Peters1d120612000-10-12 06:10:25 +0000183
Tim Peters98c81842000-10-16 17:35:13 +0000184 # 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")
Tim Peters1d120612000-10-12 06:10:25 +0000192
Tim Peters98c81842000-10-16 17:35:13 +0000193 # If this fails, it could be a puzzle. One odd possibility is that
Tim Petersa40c7932001-09-05 22:36:56 +0000194 # mathmodule.c's macros are getting confused while comparing
Tim Peters98c81842000-10-16 17:35:13 +0000195 # 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")
203
204if verbose:
205 test_exceptions()