blob: 85c93d98b0a1849425546a0ba85eb7c86ac6d585 [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
Georg Brandl2f037602006-10-28 13:51:49 +00004from test.test_support import run_unittest, verbose
5import unittest
6import math
Guido van Rossumfcce6301996-08-08 18:26:25 +00007
Guido van Rossum5ab007b1996-08-29 19:00:46 +00008seps='1e-05'
9eps = eval(seps)
Guido van Rossumfcce6301996-08-08 18:26:25 +000010
Georg Brandl2f037602006-10-28 13:51:49 +000011class MathTests(unittest.TestCase):
Guido van Rossumfcce6301996-08-08 18:26:25 +000012
Georg Brandl2f037602006-10-28 13:51:49 +000013 def ftest(self, name, value, expected):
14 if abs(value-expected) > eps:
15 self.fail('%s returned %f, expected %f'%\
16 (name, value, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +000017
Georg Brandl2f037602006-10-28 13:51:49 +000018 def testConstants(self):
19 self.ftest('pi', math.pi, 3.1415926)
20 self.ftest('e', math.e, 2.7182818)
Guido van Rossumfcce6301996-08-08 18:26:25 +000021
Georg Brandl2f037602006-10-28 13:51:49 +000022 def testAcos(self):
23 self.ftest('acos(-1)', math.acos(-1), math.pi)
24 self.ftest('acos(0)', math.acos(0), math.pi/2)
25 self.ftest('acos(1)', math.acos(1), 0)
Guido van Rossumfcce6301996-08-08 18:26:25 +000026
Georg Brandl2f037602006-10-28 13:51:49 +000027 def testAsin(self):
28 self.ftest('asin(-1)', math.asin(-1), -math.pi/2)
29 self.ftest('asin(0)', math.asin(0), 0)
30 self.ftest('asin(1)', math.asin(1), math.pi/2)
Guido van Rossumfcce6301996-08-08 18:26:25 +000031
Georg Brandl2f037602006-10-28 13:51:49 +000032 def testAtan(self):
33 self.ftest('atan(-1)', math.atan(-1), -math.pi/4)
34 self.ftest('atan(0)', math.atan(0), 0)
35 self.ftest('atan(1)', math.atan(1), math.pi/4)
Guido van Rossumfcce6301996-08-08 18:26:25 +000036
Georg Brandl2f037602006-10-28 13:51:49 +000037 def testAtan2(self):
38 self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
39 self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
40 self.ftest('atan2(0, 1)', math.atan2(0, 1), 0)
41 self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
42 self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
Guido van Rossumfcce6301996-08-08 18:26:25 +000043
Georg Brandl2f037602006-10-28 13:51:49 +000044 def testCeil(self):
45 self.ftest('ceil(0.5)', math.ceil(0.5), 1)
46 self.ftest('ceil(1.0)', math.ceil(1.0), 1)
47 self.ftest('ceil(1.5)', math.ceil(1.5), 2)
48 self.ftest('ceil(-0.5)', math.ceil(-0.5), 0)
49 self.ftest('ceil(-1.0)', math.ceil(-1.0), -1)
50 self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +000051
Georg Brandl2f037602006-10-28 13:51:49 +000052 def testCos(self):
53 self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0)
54 self.ftest('cos(0)', math.cos(0), 1)
55 self.ftest('cos(pi/2)', math.cos(math.pi/2), 0)
56 self.ftest('cos(pi)', math.cos(math.pi), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +000057
Georg Brandl2f037602006-10-28 13:51:49 +000058 def testCosh(self):
59 self.ftest('cosh(0)', math.cosh(0), 1)
60 self.ftest('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert
Raymond Hettinger64108af2002-05-13 03:55:01 +000061
Georg Brandl2f037602006-10-28 13:51:49 +000062 def testDegrees(self):
63 self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
64 self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
65 self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +000066
Georg Brandl2f037602006-10-28 13:51:49 +000067 def testExp(self):
68 self.ftest('exp(-1)', math.exp(-1), 1/math.e)
69 self.ftest('exp(0)', math.exp(0), 1)
70 self.ftest('exp(1)', math.exp(1), math.e)
Guido van Rossumfcce6301996-08-08 18:26:25 +000071
Georg Brandl2f037602006-10-28 13:51:49 +000072 def testFabs(self):
73 self.ftest('fabs(-1)', math.fabs(-1), 1)
74 self.ftest('fabs(0)', math.fabs(0), 0)
75 self.ftest('fabs(1)', math.fabs(1), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +000076
Georg Brandl2f037602006-10-28 13:51:49 +000077 def testFloor(self):
78 self.ftest('floor(0.5)', math.floor(0.5), 0)
79 self.ftest('floor(1.0)', math.floor(1.0), 1)
80 self.ftest('floor(1.5)', math.floor(1.5), 1)
81 self.ftest('floor(-0.5)', math.floor(-0.5), -1)
82 self.ftest('floor(-1.0)', math.floor(-1.0), -1)
83 self.ftest('floor(-1.5)', math.floor(-1.5), -2)
Guido van Rossumfcce6301996-08-08 18:26:25 +000084
Georg Brandl2f037602006-10-28 13:51:49 +000085 def testFmod(self):
86 self.ftest('fmod(10,1)', math.fmod(10,1), 0)
87 self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
88 self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
89 self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
90 self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
91 self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +000092
Georg Brandl2f037602006-10-28 13:51:49 +000093 def testFrexp(self):
94 def testfrexp(name, (mant, exp), (emant, eexp)):
95 if abs(mant-emant) > eps or exp != eexp:
96 self.fail('%s returned %r, expected %r'%\
97 (name, (mant, exp), (emant,eexp)))
Guido van Rossumfcce6301996-08-08 18:26:25 +000098
Georg Brandl2f037602006-10-28 13:51:49 +000099 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
100 testfrexp('frexp(0)', math.frexp(0), (0, 0))
101 testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
102 testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000103
Georg Brandl2f037602006-10-28 13:51:49 +0000104 def testHypot(self):
105 self.ftest('hypot(0,0)', math.hypot(0,0), 0)
106 self.ftest('hypot(3,4)', math.hypot(3,4), 5)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000107
Georg Brandl2f037602006-10-28 13:51:49 +0000108 def testLdexp(self):
109 self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
110 self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
111 self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
112 self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000113
Georg Brandl2f037602006-10-28 13:51:49 +0000114 def testLog(self):
115 self.ftest('log(1/e)', math.log(1/math.e), -1)
116 self.ftest('log(1)', math.log(1), 0)
117 self.ftest('log(e)', math.log(math.e), 1)
118 self.ftest('log(32,2)', math.log(32,2), 5)
119 self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
120 self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000121
Georg Brandl2f037602006-10-28 13:51:49 +0000122 def testLog10(self):
123 self.ftest('log10(0.1)', math.log10(0.1), -1)
124 self.ftest('log10(1)', math.log10(1), 0)
125 self.ftest('log10(10)', math.log10(10), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000126
Georg Brandl2f037602006-10-28 13:51:49 +0000127 def testModf(self):
128 def testmodf(name, (v1, v2), (e1, e2)):
129 if abs(v1-e1) > eps or abs(v2-e2):
130 self.fail('%s returned %r, expected %r'%\
131 (name, (v1,v2), (e1,e2)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000132
Georg Brandl2f037602006-10-28 13:51:49 +0000133 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
134 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
135
136 def testPow(self):
137 self.ftest('pow(0,1)', math.pow(0,1), 0)
138 self.ftest('pow(1,0)', math.pow(1,0), 1)
139 self.ftest('pow(2,1)', math.pow(2,1), 2)
140 self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000141
Georg Brandl2f037602006-10-28 13:51:49 +0000142 def testRadians(self):
143 self.ftest('radians(180)', math.radians(180), math.pi)
144 self.ftest('radians(90)', math.radians(90), math.pi/2)
145 self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
Raymond Hettinger64108af2002-05-13 03:55:01 +0000146
Georg Brandl2f037602006-10-28 13:51:49 +0000147 def testSin(self):
148 self.ftest('sin(0)', math.sin(0), 0)
149 self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
150 self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000151
Georg Brandl2f037602006-10-28 13:51:49 +0000152 def testSinh(self):
153 self.ftest('sinh(0)', math.sinh(0), 0)
154 self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
155 self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000156
Georg Brandl2f037602006-10-28 13:51:49 +0000157 def testSqrt(self):
158 self.ftest('sqrt(0)', math.sqrt(0), 0)
159 self.ftest('sqrt(1)', math.sqrt(1), 1)
160 self.ftest('sqrt(4)', math.sqrt(4), 2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000161
Georg Brandl2f037602006-10-28 13:51:49 +0000162 def testTan(self):
163 self.ftest('tan(0)', math.tan(0), 0)
164 self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
165 self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000166
Georg Brandl2f037602006-10-28 13:51:49 +0000167 def testTanh(self):
168 self.ftest('tanh(0)', math.tanh(0), 0)
169 self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
Tim Peters1d120612000-10-12 06:10:25 +0000170
Georg Brandl2f037602006-10-28 13:51:49 +0000171 # RED_FLAG 16-Oct-2000 Tim
172 # While 2.0 is more consistent about exceptions than previous releases, it
173 # still fails this part of the test on some platforms. For now, we only
174 # *run* test_exceptions() in verbose mode, so that this isn't normally
175 # tested.
Tim Peters1d120612000-10-12 06:10:25 +0000176
Georg Brandl2f037602006-10-28 13:51:49 +0000177 if verbose:
178 def test_exceptions(self):
179 try:
180 x = math.exp(-1000000000)
181 except:
182 # mathmodule.c is failing to weed out underflows from libm, or
183 # we've got an fp format with huge dynamic range
184 self.fail("underflowing exp() should not have raised "
185 "an exception")
186 if x != 0:
187 self.fail("underflowing exp() should have returned 0")
Tim Peters1d120612000-10-12 06:10:25 +0000188
Georg Brandl2f037602006-10-28 13:51:49 +0000189 # If this fails, probably using a strict IEEE-754 conforming libm, and x
190 # is +Inf afterwards. But Python wants overflows detected by default.
191 try:
192 x = math.exp(1000000000)
193 except OverflowError:
194 pass
195 else:
196 self.fail("overflowing exp() didn't trigger OverflowError")
Tim Peters1d120612000-10-12 06:10:25 +0000197
Georg Brandl2f037602006-10-28 13:51:49 +0000198 # If this fails, it could be a puzzle. One odd possibility is that
199 # mathmodule.c's macros are getting confused while comparing
200 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
201 # as a result (and so raising OverflowError instead).
202 try:
203 x = math.sqrt(-1.0)
204 except ValueError:
205 pass
206 else:
207 self.fail("sqrt(-1) didn't raise ValueError")
Tim Peters98c81842000-10-16 17:35:13 +0000208
Georg Brandl2f037602006-10-28 13:51:49 +0000209
210def test_main():
211 run_unittest(MathTests)
212
213if __name__ == '__main__':
214 test_main()