blob: d86298d01bd80a4f6c51b6f06ab11a9a142c0f29 [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:
Nick Coghlanec2ce9b2007-07-27 10:36:30 +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' %
Georg Brandl2f037602006-10-28 13:51:49 +000020 (name, value, expected))
Guido van Rossumfcce6301996-08-08 18:26:25 +000021
Georg Brandl2f037602006-10-28 13:51:49 +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
Georg Brandl2f037602006-10-28 13:51:49 +000026 def testAcos(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000027 self.assertRaises(TypeError, math.acos)
Georg Brandl2f037602006-10-28 13:51:49 +000028 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
Georg Brandl2f037602006-10-28 13:51:49 +000032 def testAsin(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000033 self.assertRaises(TypeError, math.asin)
Georg Brandl2f037602006-10-28 13:51:49 +000034 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
Georg Brandl2f037602006-10-28 13:51:49 +000038 def testAtan(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000039 self.assertRaises(TypeError, math.atan)
Georg Brandl2f037602006-10-28 13:51:49 +000040 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
Georg Brandl2f037602006-10-28 13:51:49 +000044 def testAtan2(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000045 self.assertRaises(TypeError, math.atan2)
Georg Brandl2f037602006-10-28 13:51:49 +000046 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
Georg Brandl2f037602006-10-28 13:51:49 +000052 def testCeil(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000053 self.assertRaises(TypeError, math.ceil)
Georg Brandl2f037602006-10-28 13:51:49 +000054 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
Georg Brandl2f037602006-10-28 13:51:49 +000061 def testCos(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000062 self.assertRaises(TypeError, math.cos)
Georg Brandl2f037602006-10-28 13:51:49 +000063 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
Georg Brandl2f037602006-10-28 13:51:49 +000068 def testCosh(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000069 self.assertRaises(TypeError, math.cosh)
Georg Brandl2f037602006-10-28 13:51:49 +000070 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
Georg Brandl2f037602006-10-28 13:51:49 +000073 def testDegrees(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000074 self.assertRaises(TypeError, math.degrees)
Georg Brandl2f037602006-10-28 13:51:49 +000075 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
Georg Brandl2f037602006-10-28 13:51:49 +000079 def testExp(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000080 self.assertRaises(TypeError, math.exp)
Georg Brandl2f037602006-10-28 13:51:49 +000081 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
Georg Brandl2f037602006-10-28 13:51:49 +000085 def testFabs(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000086 self.assertRaises(TypeError, math.fabs)
Georg Brandl2f037602006-10-28 13:51:49 +000087 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
Georg Brandl2f037602006-10-28 13:51:49 +000091 def testFloor(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000092 self.assertRaises(TypeError, math.floor)
Georg Brandl2f037602006-10-28 13:51:49 +000093 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)
Nick Coghlan00f20292007-07-26 14:03:00 +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
Georg Brandl2f037602006-10-28 13:51:49 +0000104 def testFmod(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000105 self.assertRaises(TypeError, math.fmod)
Georg Brandl2f037602006-10-28 13:51:49 +0000106 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
Georg Brandl2f037602006-10-28 13:51:49 +0000113 def testFrexp(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000114 self.assertRaises(TypeError, math.frexp)
115
Georg Brandl2f037602006-10-28 13:51:49 +0000116 def testfrexp(name, (mant, exp), (emant, eexp)):
117 if abs(mant-emant) > eps or exp != eexp:
118 self.fail('%s returned %r, expected %r'%\
119 (name, (mant, exp), (emant,eexp)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000120
Georg Brandl2f037602006-10-28 13:51:49 +0000121 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
122 testfrexp('frexp(0)', math.frexp(0), (0, 0))
123 testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
124 testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000125
Georg Brandl2f037602006-10-28 13:51:49 +0000126 def testHypot(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000127 self.assertRaises(TypeError, math.hypot)
Georg Brandl2f037602006-10-28 13:51:49 +0000128 self.ftest('hypot(0,0)', math.hypot(0,0), 0)
129 self.ftest('hypot(3,4)', math.hypot(3,4), 5)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000130
Georg Brandl2f037602006-10-28 13:51:49 +0000131 def testLdexp(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000132 self.assertRaises(TypeError, math.ldexp)
Georg Brandl2f037602006-10-28 13:51:49 +0000133 self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
134 self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
135 self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
136 self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000137
Georg Brandl2f037602006-10-28 13:51:49 +0000138 def testLog(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000139 self.assertRaises(TypeError, math.log)
Georg Brandl2f037602006-10-28 13:51:49 +0000140 self.ftest('log(1/e)', math.log(1/math.e), -1)
141 self.ftest('log(1)', math.log(1), 0)
142 self.ftest('log(e)', math.log(math.e), 1)
143 self.ftest('log(32,2)', math.log(32,2), 5)
144 self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
145 self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000146
Georg Brandl2f037602006-10-28 13:51:49 +0000147 def testLog10(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000148 self.assertRaises(TypeError, math.log10)
Georg Brandl2f037602006-10-28 13:51:49 +0000149 self.ftest('log10(0.1)', math.log10(0.1), -1)
150 self.ftest('log10(1)', math.log10(1), 0)
151 self.ftest('log10(10)', math.log10(10), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000152
Georg Brandl2f037602006-10-28 13:51:49 +0000153 def testModf(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000154 self.assertRaises(TypeError, math.modf)
155
Georg Brandl2f037602006-10-28 13:51:49 +0000156 def testmodf(name, (v1, v2), (e1, e2)):
157 if abs(v1-e1) > eps or abs(v2-e2):
158 self.fail('%s returned %r, expected %r'%\
159 (name, (v1,v2), (e1,e2)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000160
Georg Brandl2f037602006-10-28 13:51:49 +0000161 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
162 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
Tim Petersabd8a332006-11-03 02:32:46 +0000163
Georg Brandl2f037602006-10-28 13:51:49 +0000164 def testPow(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000165 self.assertRaises(TypeError, math.pow)
Georg Brandl2f037602006-10-28 13:51:49 +0000166 self.ftest('pow(0,1)', math.pow(0,1), 0)
167 self.ftest('pow(1,0)', math.pow(1,0), 1)
168 self.ftest('pow(2,1)', math.pow(2,1), 2)
169 self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000170
Georg Brandl2f037602006-10-28 13:51:49 +0000171 def testRadians(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000172 self.assertRaises(TypeError, math.radians)
Georg Brandl2f037602006-10-28 13:51:49 +0000173 self.ftest('radians(180)', math.radians(180), math.pi)
174 self.ftest('radians(90)', math.radians(90), math.pi/2)
175 self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
Raymond Hettinger64108af2002-05-13 03:55:01 +0000176
Georg Brandl2f037602006-10-28 13:51:49 +0000177 def testSin(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000178 self.assertRaises(TypeError, math.sin)
Georg Brandl2f037602006-10-28 13:51:49 +0000179 self.ftest('sin(0)', math.sin(0), 0)
180 self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
181 self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000182
Georg Brandl2f037602006-10-28 13:51:49 +0000183 def testSinh(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000184 self.assertRaises(TypeError, math.sinh)
Georg Brandl2f037602006-10-28 13:51:49 +0000185 self.ftest('sinh(0)', math.sinh(0), 0)
186 self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
187 self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000188
Georg Brandl2f037602006-10-28 13:51:49 +0000189 def testSqrt(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000190 self.assertRaises(TypeError, math.sqrt)
Georg Brandl2f037602006-10-28 13:51:49 +0000191 self.ftest('sqrt(0)', math.sqrt(0), 0)
192 self.ftest('sqrt(1)', math.sqrt(1), 1)
193 self.ftest('sqrt(4)', math.sqrt(4), 2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000194
Georg Brandl2f037602006-10-28 13:51:49 +0000195 def testTan(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000196 self.assertRaises(TypeError, math.tan)
Georg Brandl2f037602006-10-28 13:51:49 +0000197 self.ftest('tan(0)', math.tan(0), 0)
198 self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
199 self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000200
Georg Brandl2f037602006-10-28 13:51:49 +0000201 def testTanh(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000202 self.assertRaises(TypeError, math.tanh)
Georg Brandl2f037602006-10-28 13:51:49 +0000203 self.ftest('tanh(0)', math.tanh(0), 0)
204 self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
Tim Peters1d120612000-10-12 06:10:25 +0000205
Georg Brandl2f037602006-10-28 13:51:49 +0000206 # RED_FLAG 16-Oct-2000 Tim
207 # While 2.0 is more consistent about exceptions than previous releases, it
208 # still fails this part of the test on some platforms. For now, we only
209 # *run* test_exceptions() in verbose mode, so that this isn't normally
210 # tested.
Tim Peters1d120612000-10-12 06:10:25 +0000211
Georg Brandl2f037602006-10-28 13:51:49 +0000212 if verbose:
213 def test_exceptions(self):
214 try:
215 x = math.exp(-1000000000)
216 except:
217 # mathmodule.c is failing to weed out underflows from libm, or
218 # we've got an fp format with huge dynamic range
219 self.fail("underflowing exp() should not have raised "
220 "an exception")
221 if x != 0:
222 self.fail("underflowing exp() should have returned 0")
Tim Peters1d120612000-10-12 06:10:25 +0000223
Georg Brandl2f037602006-10-28 13:51:49 +0000224 # If this fails, probably using a strict IEEE-754 conforming libm, and x
225 # is +Inf afterwards. But Python wants overflows detected by default.
226 try:
227 x = math.exp(1000000000)
228 except OverflowError:
229 pass
230 else:
231 self.fail("overflowing exp() didn't trigger OverflowError")
Tim Peters1d120612000-10-12 06:10:25 +0000232
Georg Brandl2f037602006-10-28 13:51:49 +0000233 # If this fails, it could be a puzzle. One odd possibility is that
234 # mathmodule.c's macros are getting confused while comparing
235 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
236 # as a result (and so raising OverflowError instead).
237 try:
238 x = math.sqrt(-1.0)
239 except ValueError:
240 pass
241 else:
242 self.fail("sqrt(-1) didn't raise ValueError")
Tim Peters98c81842000-10-16 17:35:13 +0000243
Georg Brandl2f037602006-10-28 13:51:49 +0000244
245def test_main():
246 run_unittest(MathTests)
247
248if __name__ == '__main__':
249 test_main()