blob: c28902b94f34ff73414d3515bfcd65d12708a5de [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
Thomas Wouters89f507f2006-12-13 04:49:30 +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
Thomas Wouters89f507f2006-12-13 04:49:30 +000011class MathTests(unittest.TestCase):
Guido van Rossumfcce6301996-08-08 18:26:25 +000012
Thomas Wouters89f507f2006-12-13 04:49:30 +000013 def ftest(self, name, value, expected):
14 if abs(value-expected) > eps:
Guido van Rossum806c2462007-08-06 23:33:07 +000015 # Use %r instead of %f so the error message
16 # displays full precision. Otherwise discrepancies
17 # in the last few bits will lead to very confusing
18 # error messages
19 self.fail('%s returned %r, expected %r' %
Thomas Wouters89f507f2006-12-13 04:49:30 +000020 (name, value, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +000021
Thomas Wouters89f507f2006-12-13 04:49:30 +000022 def testConstants(self):
23 self.ftest('pi', math.pi, 3.1415926)
24 self.ftest('e', math.e, 2.7182818)
Guido van Rossumfcce6301996-08-08 18:26:25 +000025
Thomas Wouters89f507f2006-12-13 04:49:30 +000026 def testAcos(self):
27 self.assertRaises(TypeError, math.acos)
28 self.ftest('acos(-1)', math.acos(-1), math.pi)
29 self.ftest('acos(0)', math.acos(0), math.pi/2)
30 self.ftest('acos(1)', math.acos(1), 0)
Guido van Rossumfcce6301996-08-08 18:26:25 +000031
Thomas Wouters89f507f2006-12-13 04:49:30 +000032 def testAsin(self):
33 self.assertRaises(TypeError, math.asin)
34 self.ftest('asin(-1)', math.asin(-1), -math.pi/2)
35 self.ftest('asin(0)', math.asin(0), 0)
36 self.ftest('asin(1)', math.asin(1), math.pi/2)
Guido van Rossumfcce6301996-08-08 18:26:25 +000037
Thomas Wouters89f507f2006-12-13 04:49:30 +000038 def testAtan(self):
39 self.assertRaises(TypeError, math.atan)
40 self.ftest('atan(-1)', math.atan(-1), -math.pi/4)
41 self.ftest('atan(0)', math.atan(0), 0)
42 self.ftest('atan(1)', math.atan(1), math.pi/4)
Guido van Rossumfcce6301996-08-08 18:26:25 +000043
Thomas Wouters89f507f2006-12-13 04:49:30 +000044 def testAtan2(self):
45 self.assertRaises(TypeError, math.atan2)
46 self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
47 self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
48 self.ftest('atan2(0, 1)', math.atan2(0, 1), 0)
49 self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
50 self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
Guido van Rossumfcce6301996-08-08 18:26:25 +000051
Thomas Wouters89f507f2006-12-13 04:49:30 +000052 def testCeil(self):
53 self.assertRaises(TypeError, math.ceil)
54 self.ftest('ceil(0.5)', math.ceil(0.5), 1)
55 self.ftest('ceil(1.0)', math.ceil(1.0), 1)
56 self.ftest('ceil(1.5)', math.ceil(1.5), 2)
57 self.ftest('ceil(-0.5)', math.ceil(-0.5), 0)
58 self.ftest('ceil(-1.0)', math.ceil(-1.0), -1)
59 self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +000060
Thomas Wouters89f507f2006-12-13 04:49:30 +000061 def testCos(self):
62 self.assertRaises(TypeError, math.cos)
63 self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0)
64 self.ftest('cos(0)', math.cos(0), 1)
65 self.ftest('cos(pi/2)', math.cos(math.pi/2), 0)
66 self.ftest('cos(pi)', math.cos(math.pi), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +000067
Thomas Wouters89f507f2006-12-13 04:49:30 +000068 def testCosh(self):
69 self.assertRaises(TypeError, math.cosh)
70 self.ftest('cosh(0)', math.cosh(0), 1)
71 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 +000072
Thomas Wouters89f507f2006-12-13 04:49:30 +000073 def testDegrees(self):
74 self.assertRaises(TypeError, math.degrees)
75 self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
76 self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
77 self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +000078
Thomas Wouters89f507f2006-12-13 04:49:30 +000079 def testExp(self):
80 self.assertRaises(TypeError, math.exp)
81 self.ftest('exp(-1)', math.exp(-1), 1/math.e)
82 self.ftest('exp(0)', math.exp(0), 1)
83 self.ftest('exp(1)', math.exp(1), math.e)
Guido van Rossumfcce6301996-08-08 18:26:25 +000084
Thomas Wouters89f507f2006-12-13 04:49:30 +000085 def testFabs(self):
86 self.assertRaises(TypeError, math.fabs)
87 self.ftest('fabs(-1)', math.fabs(-1), 1)
88 self.ftest('fabs(0)', math.fabs(0), 0)
89 self.ftest('fabs(1)', math.fabs(1), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +000090
Thomas Wouters89f507f2006-12-13 04:49:30 +000091 def testFloor(self):
92 self.assertRaises(TypeError, math.floor)
93 self.ftest('floor(0.5)', math.floor(0.5), 0)
94 self.ftest('floor(1.0)', math.floor(1.0), 1)
95 self.ftest('floor(1.5)', math.floor(1.5), 1)
96 self.ftest('floor(-0.5)', math.floor(-0.5), -1)
97 self.ftest('floor(-1.0)', math.floor(-1.0), -1)
98 self.ftest('floor(-1.5)', math.floor(-1.5), -2)
Guido van Rossum806c2462007-08-06 23:33:07 +000099 # pow() relies on floor() to check for integers
100 # This fails on some platforms - so check it here
101 self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167)
102 self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000103
Thomas Wouters89f507f2006-12-13 04:49:30 +0000104 def testFmod(self):
105 self.assertRaises(TypeError, math.fmod)
106 self.ftest('fmod(10,1)', math.fmod(10,1), 0)
107 self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
108 self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
109 self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
110 self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
111 self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000112
Thomas Wouters89f507f2006-12-13 04:49:30 +0000113 def testFrexp(self):
114 self.assertRaises(TypeError, math.frexp)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000115
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000116 def testfrexp(name, result, expected):
117 (mant, exp), (emant, eexp) = result, expected
Thomas Wouters89f507f2006-12-13 04:49:30 +0000118 if abs(mant-emant) > eps or exp != eexp:
119 self.fail('%s returned %r, expected %r'%\
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000120 (name, result, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000121
Thomas Wouters89f507f2006-12-13 04:49:30 +0000122 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
123 testfrexp('frexp(0)', math.frexp(0), (0, 0))
124 testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
125 testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000126
Thomas Wouters89f507f2006-12-13 04:49:30 +0000127 def testHypot(self):
128 self.assertRaises(TypeError, math.hypot)
129 self.ftest('hypot(0,0)', math.hypot(0,0), 0)
130 self.ftest('hypot(3,4)', math.hypot(3,4), 5)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000131
Thomas Wouters89f507f2006-12-13 04:49:30 +0000132 def testLdexp(self):
133 self.assertRaises(TypeError, math.ldexp)
134 self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
135 self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
136 self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
137 self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000138
Thomas Wouters89f507f2006-12-13 04:49:30 +0000139 def testLog(self):
140 self.assertRaises(TypeError, math.log)
141 self.ftest('log(1/e)', math.log(1/math.e), -1)
142 self.ftest('log(1)', math.log(1), 0)
143 self.ftest('log(e)', math.log(math.e), 1)
144 self.ftest('log(32,2)', math.log(32,2), 5)
145 self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
146 self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000147
Thomas Wouters89f507f2006-12-13 04:49:30 +0000148 def testLog10(self):
149 self.assertRaises(TypeError, math.log10)
150 self.ftest('log10(0.1)', math.log10(0.1), -1)
151 self.ftest('log10(1)', math.log10(1), 0)
152 self.ftest('log10(10)', math.log10(10), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000153
Thomas Wouters89f507f2006-12-13 04:49:30 +0000154 def testModf(self):
155 self.assertRaises(TypeError, math.modf)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000156
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000157 def testmodf(name, result, expected):
158 (v1, v2), (e1, e2) = result, expected
Thomas Wouters89f507f2006-12-13 04:49:30 +0000159 if abs(v1-e1) > eps or abs(v2-e2):
160 self.fail('%s returned %r, expected %r'%\
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000161 (name, result, expected))
Raymond Hettinger64108af2002-05-13 03:55:01 +0000162
Thomas Wouters89f507f2006-12-13 04:49:30 +0000163 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
164 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000165
Thomas Wouters89f507f2006-12-13 04:49:30 +0000166 def testPow(self):
167 self.assertRaises(TypeError, math.pow)
168 self.ftest('pow(0,1)', math.pow(0,1), 0)
169 self.ftest('pow(1,0)', math.pow(1,0), 1)
170 self.ftest('pow(2,1)', math.pow(2,1), 2)
171 self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000172
Thomas Wouters89f507f2006-12-13 04:49:30 +0000173 def testRadians(self):
174 self.assertRaises(TypeError, math.radians)
175 self.ftest('radians(180)', math.radians(180), math.pi)
176 self.ftest('radians(90)', math.radians(90), math.pi/2)
177 self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000178
Thomas Wouters89f507f2006-12-13 04:49:30 +0000179 def testSin(self):
180 self.assertRaises(TypeError, math.sin)
181 self.ftest('sin(0)', math.sin(0), 0)
182 self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
183 self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000184
Thomas Wouters89f507f2006-12-13 04:49:30 +0000185 def testSinh(self):
186 self.assertRaises(TypeError, math.sinh)
187 self.ftest('sinh(0)', math.sinh(0), 0)
188 self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
189 self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
Tim Peters1d120612000-10-12 06:10:25 +0000190
Thomas Wouters89f507f2006-12-13 04:49:30 +0000191 def testSqrt(self):
192 self.assertRaises(TypeError, math.sqrt)
193 self.ftest('sqrt(0)', math.sqrt(0), 0)
194 self.ftest('sqrt(1)', math.sqrt(1), 1)
195 self.ftest('sqrt(4)', math.sqrt(4), 2)
Tim Peters1d120612000-10-12 06:10:25 +0000196
Thomas Wouters89f507f2006-12-13 04:49:30 +0000197 def testTan(self):
198 self.assertRaises(TypeError, math.tan)
199 self.ftest('tan(0)', math.tan(0), 0)
200 self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
201 self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
Tim Peters1d120612000-10-12 06:10:25 +0000202
Thomas Wouters89f507f2006-12-13 04:49:30 +0000203 def testTanh(self):
204 self.assertRaises(TypeError, math.tanh)
205 self.ftest('tanh(0)', math.tanh(0), 0)
206 self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
Tim Peters1d120612000-10-12 06:10:25 +0000207
Thomas Wouters89f507f2006-12-13 04:49:30 +0000208 # RED_FLAG 16-Oct-2000 Tim
209 # While 2.0 is more consistent about exceptions than previous releases, it
210 # still fails this part of the test on some platforms. For now, we only
211 # *run* test_exceptions() in verbose mode, so that this isn't normally
212 # tested.
Tim Peters98c81842000-10-16 17:35:13 +0000213
Thomas Wouters89f507f2006-12-13 04:49:30 +0000214 if verbose:
215 def test_exceptions(self):
216 try:
217 x = math.exp(-1000000000)
218 except:
219 # mathmodule.c is failing to weed out underflows from libm, or
220 # we've got an fp format with huge dynamic range
221 self.fail("underflowing exp() should not have raised "
222 "an exception")
223 if x != 0:
224 self.fail("underflowing exp() should have returned 0")
225
226 # If this fails, probably using a strict IEEE-754 conforming libm, and x
227 # is +Inf afterwards. But Python wants overflows detected by default.
228 try:
229 x = math.exp(1000000000)
230 except OverflowError:
231 pass
232 else:
233 self.fail("overflowing exp() didn't trigger OverflowError")
234
235 # If this fails, it could be a puzzle. One odd possibility is that
236 # mathmodule.c's macros are getting confused while comparing
237 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
238 # as a result (and so raising OverflowError instead).
239 try:
240 x = math.sqrt(-1.0)
241 except ValueError:
242 pass
243 else:
244 self.fail("sqrt(-1) didn't raise ValueError")
245
246
247def test_main():
248 run_unittest(MathTests)
249
250if __name__ == '__main__':
251 test_main()