blob: e5c6ead80fb9c82a96b3652f3f52a7a9db6083e9 [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:
15 self.fail('%s returned %f, expected %f'%\
16 (name, value, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +000017
Thomas Wouters89f507f2006-12-13 04:49:30 +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
Thomas Wouters89f507f2006-12-13 04:49:30 +000022 def testAcos(self):
23 self.assertRaises(TypeError, math.acos)
24 self.ftest('acos(-1)', math.acos(-1), math.pi)
25 self.ftest('acos(0)', math.acos(0), math.pi/2)
26 self.ftest('acos(1)', math.acos(1), 0)
Guido van Rossumfcce6301996-08-08 18:26:25 +000027
Thomas Wouters89f507f2006-12-13 04:49:30 +000028 def testAsin(self):
29 self.assertRaises(TypeError, math.asin)
30 self.ftest('asin(-1)', math.asin(-1), -math.pi/2)
31 self.ftest('asin(0)', math.asin(0), 0)
32 self.ftest('asin(1)', math.asin(1), math.pi/2)
Guido van Rossumfcce6301996-08-08 18:26:25 +000033
Thomas Wouters89f507f2006-12-13 04:49:30 +000034 def testAtan(self):
35 self.assertRaises(TypeError, math.atan)
36 self.ftest('atan(-1)', math.atan(-1), -math.pi/4)
37 self.ftest('atan(0)', math.atan(0), 0)
38 self.ftest('atan(1)', math.atan(1), math.pi/4)
Guido van Rossumfcce6301996-08-08 18:26:25 +000039
Thomas Wouters89f507f2006-12-13 04:49:30 +000040 def testAtan2(self):
41 self.assertRaises(TypeError, math.atan2)
42 self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
43 self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
44 self.ftest('atan2(0, 1)', math.atan2(0, 1), 0)
45 self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
46 self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
Guido van Rossumfcce6301996-08-08 18:26:25 +000047
Thomas Wouters89f507f2006-12-13 04:49:30 +000048 def testCeil(self):
49 self.assertRaises(TypeError, math.ceil)
50 self.ftest('ceil(0.5)', math.ceil(0.5), 1)
51 self.ftest('ceil(1.0)', math.ceil(1.0), 1)
52 self.ftest('ceil(1.5)', math.ceil(1.5), 2)
53 self.ftest('ceil(-0.5)', math.ceil(-0.5), 0)
54 self.ftest('ceil(-1.0)', math.ceil(-1.0), -1)
55 self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +000056
Thomas Wouters89f507f2006-12-13 04:49:30 +000057 def testCos(self):
58 self.assertRaises(TypeError, math.cos)
59 self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0)
60 self.ftest('cos(0)', math.cos(0), 1)
61 self.ftest('cos(pi/2)', math.cos(math.pi/2), 0)
62 self.ftest('cos(pi)', math.cos(math.pi), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +000063
Thomas Wouters89f507f2006-12-13 04:49:30 +000064 def testCosh(self):
65 self.assertRaises(TypeError, math.cosh)
66 self.ftest('cosh(0)', math.cosh(0), 1)
67 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 +000068
Thomas Wouters89f507f2006-12-13 04:49:30 +000069 def testDegrees(self):
70 self.assertRaises(TypeError, math.degrees)
71 self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
72 self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
73 self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
Guido van Rossumfcce6301996-08-08 18:26:25 +000074
Thomas Wouters89f507f2006-12-13 04:49:30 +000075 def testExp(self):
76 self.assertRaises(TypeError, math.exp)
77 self.ftest('exp(-1)', math.exp(-1), 1/math.e)
78 self.ftest('exp(0)', math.exp(0), 1)
79 self.ftest('exp(1)', math.exp(1), math.e)
Guido van Rossumfcce6301996-08-08 18:26:25 +000080
Thomas Wouters89f507f2006-12-13 04:49:30 +000081 def testFabs(self):
82 self.assertRaises(TypeError, math.fabs)
83 self.ftest('fabs(-1)', math.fabs(-1), 1)
84 self.ftest('fabs(0)', math.fabs(0), 0)
85 self.ftest('fabs(1)', math.fabs(1), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +000086
Thomas Wouters89f507f2006-12-13 04:49:30 +000087 def testFloor(self):
88 self.assertRaises(TypeError, math.floor)
89 self.ftest('floor(0.5)', math.floor(0.5), 0)
90 self.ftest('floor(1.0)', math.floor(1.0), 1)
91 self.ftest('floor(1.5)', math.floor(1.5), 1)
92 self.ftest('floor(-0.5)', math.floor(-0.5), -1)
93 self.ftest('floor(-1.0)', math.floor(-1.0), -1)
94 self.ftest('floor(-1.5)', math.floor(-1.5), -2)
Guido van Rossumfcce6301996-08-08 18:26:25 +000095
Thomas Wouters89f507f2006-12-13 04:49:30 +000096 def testFmod(self):
97 self.assertRaises(TypeError, math.fmod)
98 self.ftest('fmod(10,1)', math.fmod(10,1), 0)
99 self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
100 self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
101 self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
102 self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
103 self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000104
Thomas Wouters89f507f2006-12-13 04:49:30 +0000105 def testFrexp(self):
106 self.assertRaises(TypeError, math.frexp)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000107
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000108 def testfrexp(name, result, expected):
109 (mant, exp), (emant, eexp) = result, expected
Thomas Wouters89f507f2006-12-13 04:49:30 +0000110 if abs(mant-emant) > eps or exp != eexp:
111 self.fail('%s returned %r, expected %r'%\
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000112 (name, result, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000113
Thomas Wouters89f507f2006-12-13 04:49:30 +0000114 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
115 testfrexp('frexp(0)', math.frexp(0), (0, 0))
116 testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
117 testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000118
Thomas Wouters89f507f2006-12-13 04:49:30 +0000119 def testHypot(self):
120 self.assertRaises(TypeError, math.hypot)
121 self.ftest('hypot(0,0)', math.hypot(0,0), 0)
122 self.ftest('hypot(3,4)', math.hypot(3,4), 5)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000123
Thomas Wouters89f507f2006-12-13 04:49:30 +0000124 def testLdexp(self):
125 self.assertRaises(TypeError, math.ldexp)
126 self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
127 self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
128 self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
129 self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000130
Thomas Wouters89f507f2006-12-13 04:49:30 +0000131 def testLog(self):
132 self.assertRaises(TypeError, math.log)
133 self.ftest('log(1/e)', math.log(1/math.e), -1)
134 self.ftest('log(1)', math.log(1), 0)
135 self.ftest('log(e)', math.log(math.e), 1)
136 self.ftest('log(32,2)', math.log(32,2), 5)
137 self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
138 self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000139
Thomas Wouters89f507f2006-12-13 04:49:30 +0000140 def testLog10(self):
141 self.assertRaises(TypeError, math.log10)
142 self.ftest('log10(0.1)', math.log10(0.1), -1)
143 self.ftest('log10(1)', math.log10(1), 0)
144 self.ftest('log10(10)', math.log10(10), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000145
Thomas Wouters89f507f2006-12-13 04:49:30 +0000146 def testModf(self):
147 self.assertRaises(TypeError, math.modf)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000148
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000149 def testmodf(name, result, expected):
150 (v1, v2), (e1, e2) = result, expected
Thomas Wouters89f507f2006-12-13 04:49:30 +0000151 if abs(v1-e1) > eps or abs(v2-e2):
152 self.fail('%s returned %r, expected %r'%\
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000153 (name, result, expected))
Raymond Hettinger64108af2002-05-13 03:55:01 +0000154
Thomas Wouters89f507f2006-12-13 04:49:30 +0000155 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
156 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000157
Thomas Wouters89f507f2006-12-13 04:49:30 +0000158 def testPow(self):
159 self.assertRaises(TypeError, math.pow)
160 self.ftest('pow(0,1)', math.pow(0,1), 0)
161 self.ftest('pow(1,0)', math.pow(1,0), 1)
162 self.ftest('pow(2,1)', math.pow(2,1), 2)
163 self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000164
Thomas Wouters89f507f2006-12-13 04:49:30 +0000165 def testRadians(self):
166 self.assertRaises(TypeError, math.radians)
167 self.ftest('radians(180)', math.radians(180), math.pi)
168 self.ftest('radians(90)', math.radians(90), math.pi/2)
169 self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000170
Thomas Wouters89f507f2006-12-13 04:49:30 +0000171 def testSin(self):
172 self.assertRaises(TypeError, math.sin)
173 self.ftest('sin(0)', math.sin(0), 0)
174 self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
175 self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000176
Thomas Wouters89f507f2006-12-13 04:49:30 +0000177 def testSinh(self):
178 self.assertRaises(TypeError, math.sinh)
179 self.ftest('sinh(0)', math.sinh(0), 0)
180 self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
181 self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
Tim Peters1d120612000-10-12 06:10:25 +0000182
Thomas Wouters89f507f2006-12-13 04:49:30 +0000183 def testSqrt(self):
184 self.assertRaises(TypeError, math.sqrt)
185 self.ftest('sqrt(0)', math.sqrt(0), 0)
186 self.ftest('sqrt(1)', math.sqrt(1), 1)
187 self.ftest('sqrt(4)', math.sqrt(4), 2)
Tim Peters1d120612000-10-12 06:10:25 +0000188
Thomas Wouters89f507f2006-12-13 04:49:30 +0000189 def testTan(self):
190 self.assertRaises(TypeError, math.tan)
191 self.ftest('tan(0)', math.tan(0), 0)
192 self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
193 self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
Tim Peters1d120612000-10-12 06:10:25 +0000194
Thomas Wouters89f507f2006-12-13 04:49:30 +0000195 def testTanh(self):
196 self.assertRaises(TypeError, math.tanh)
197 self.ftest('tanh(0)', math.tanh(0), 0)
198 self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
Tim Peters1d120612000-10-12 06:10:25 +0000199
Thomas Wouters89f507f2006-12-13 04:49:30 +0000200 # RED_FLAG 16-Oct-2000 Tim
201 # While 2.0 is more consistent about exceptions than previous releases, it
202 # still fails this part of the test on some platforms. For now, we only
203 # *run* test_exceptions() in verbose mode, so that this isn't normally
204 # tested.
Tim Peters98c81842000-10-16 17:35:13 +0000205
Thomas Wouters89f507f2006-12-13 04:49:30 +0000206 if verbose:
207 def test_exceptions(self):
208 try:
209 x = math.exp(-1000000000)
210 except:
211 # mathmodule.c is failing to weed out underflows from libm, or
212 # we've got an fp format with huge dynamic range
213 self.fail("underflowing exp() should not have raised "
214 "an exception")
215 if x != 0:
216 self.fail("underflowing exp() should have returned 0")
217
218 # If this fails, probably using a strict IEEE-754 conforming libm, and x
219 # is +Inf afterwards. But Python wants overflows detected by default.
220 try:
221 x = math.exp(1000000000)
222 except OverflowError:
223 pass
224 else:
225 self.fail("overflowing exp() didn't trigger OverflowError")
226
227 # If this fails, it could be a puzzle. One odd possibility is that
228 # mathmodule.c's macros are getting confused while comparing
229 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
230 # as a result (and so raising OverflowError instead).
231 try:
232 x = math.sqrt(-1.0)
233 except ValueError:
234 pass
235 else:
236 self.fail("sqrt(-1) didn't raise ValueError")
237
238
239def test_main():
240 run_unittest(MathTests)
241
242if __name__ == '__main__':
243 test_main()