blob: 699c621bcc80816cf6e5742b00c2686f8c233324 [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):
Walter Dörwald92911bf2006-10-29 22:06:28 +000023 self.assertRaises(TypeError, math.acos)
Georg Brandl2f037602006-10-28 13:51:49 +000024 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
Georg Brandl2f037602006-10-28 13:51:49 +000028 def testAsin(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000029 self.assertRaises(TypeError, math.asin)
Georg Brandl2f037602006-10-28 13:51:49 +000030 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
Georg Brandl2f037602006-10-28 13:51:49 +000034 def testAtan(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000035 self.assertRaises(TypeError, math.atan)
Georg Brandl2f037602006-10-28 13:51:49 +000036 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
Georg Brandl2f037602006-10-28 13:51:49 +000040 def testAtan2(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000041 self.assertRaises(TypeError, math.atan2)
Georg Brandl2f037602006-10-28 13:51:49 +000042 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
Georg Brandl2f037602006-10-28 13:51:49 +000048 def testCeil(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000049 self.assertRaises(TypeError, math.ceil)
Georg Brandl2f037602006-10-28 13:51:49 +000050 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
Georg Brandl2f037602006-10-28 13:51:49 +000057 def testCos(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000058 self.assertRaises(TypeError, math.cos)
Georg Brandl2f037602006-10-28 13:51:49 +000059 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
Georg Brandl2f037602006-10-28 13:51:49 +000064 def testCosh(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000065 self.assertRaises(TypeError, math.cosh)
Georg Brandl2f037602006-10-28 13:51:49 +000066 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
Georg Brandl2f037602006-10-28 13:51:49 +000069 def testDegrees(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000070 self.assertRaises(TypeError, math.degrees)
Georg Brandl2f037602006-10-28 13:51:49 +000071 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
Georg Brandl2f037602006-10-28 13:51:49 +000075 def testExp(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000076 self.assertRaises(TypeError, math.exp)
Georg Brandl2f037602006-10-28 13:51:49 +000077 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
Georg Brandl2f037602006-10-28 13:51:49 +000081 def testFabs(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000082 self.assertRaises(TypeError, math.fabs)
Georg Brandl2f037602006-10-28 13:51:49 +000083 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
Georg Brandl2f037602006-10-28 13:51:49 +000087 def testFloor(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000088 self.assertRaises(TypeError, math.floor)
Georg Brandl2f037602006-10-28 13:51:49 +000089 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)
Nick Coghlan00f20292007-07-26 14:03:00 +000095 # pow() relies on floor() to check for integers
96 # This fails on some platforms - so check it here
97 self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167)
98 self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167)
Guido van Rossumfcce6301996-08-08 18:26:25 +000099
Georg Brandl2f037602006-10-28 13:51:49 +0000100 def testFmod(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000101 self.assertRaises(TypeError, math.fmod)
Georg Brandl2f037602006-10-28 13:51:49 +0000102 self.ftest('fmod(10,1)', math.fmod(10,1), 0)
103 self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
104 self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
105 self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
106 self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
107 self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000108
Georg Brandl2f037602006-10-28 13:51:49 +0000109 def testFrexp(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000110 self.assertRaises(TypeError, math.frexp)
111
Georg Brandl2f037602006-10-28 13:51:49 +0000112 def testfrexp(name, (mant, exp), (emant, eexp)):
113 if abs(mant-emant) > eps or exp != eexp:
114 self.fail('%s returned %r, expected %r'%\
115 (name, (mant, exp), (emant,eexp)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000116
Georg Brandl2f037602006-10-28 13:51:49 +0000117 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
118 testfrexp('frexp(0)', math.frexp(0), (0, 0))
119 testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
120 testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000121
Georg Brandl2f037602006-10-28 13:51:49 +0000122 def testHypot(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000123 self.assertRaises(TypeError, math.hypot)
Georg Brandl2f037602006-10-28 13:51:49 +0000124 self.ftest('hypot(0,0)', math.hypot(0,0), 0)
125 self.ftest('hypot(3,4)', math.hypot(3,4), 5)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000126
Georg Brandl2f037602006-10-28 13:51:49 +0000127 def testLdexp(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000128 self.assertRaises(TypeError, math.ldexp)
Georg Brandl2f037602006-10-28 13:51:49 +0000129 self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
130 self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
131 self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
132 self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000133
Georg Brandl2f037602006-10-28 13:51:49 +0000134 def testLog(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000135 self.assertRaises(TypeError, math.log)
Georg Brandl2f037602006-10-28 13:51:49 +0000136 self.ftest('log(1/e)', math.log(1/math.e), -1)
137 self.ftest('log(1)', math.log(1), 0)
138 self.ftest('log(e)', math.log(math.e), 1)
139 self.ftest('log(32,2)', math.log(32,2), 5)
140 self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
141 self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000142
Georg Brandl2f037602006-10-28 13:51:49 +0000143 def testLog10(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000144 self.assertRaises(TypeError, math.log10)
Georg Brandl2f037602006-10-28 13:51:49 +0000145 self.ftest('log10(0.1)', math.log10(0.1), -1)
146 self.ftest('log10(1)', math.log10(1), 0)
147 self.ftest('log10(10)', math.log10(10), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000148
Georg Brandl2f037602006-10-28 13:51:49 +0000149 def testModf(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000150 self.assertRaises(TypeError, math.modf)
151
Georg Brandl2f037602006-10-28 13:51:49 +0000152 def testmodf(name, (v1, v2), (e1, e2)):
153 if abs(v1-e1) > eps or abs(v2-e2):
154 self.fail('%s returned %r, expected %r'%\
155 (name, (v1,v2), (e1,e2)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000156
Georg Brandl2f037602006-10-28 13:51:49 +0000157 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
158 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
Tim Petersabd8a332006-11-03 02:32:46 +0000159
Georg Brandl2f037602006-10-28 13:51:49 +0000160 def testPow(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000161 self.assertRaises(TypeError, math.pow)
Georg Brandl2f037602006-10-28 13:51:49 +0000162 self.ftest('pow(0,1)', math.pow(0,1), 0)
163 self.ftest('pow(1,0)', math.pow(1,0), 1)
164 self.ftest('pow(2,1)', math.pow(2,1), 2)
165 self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000166
Georg Brandl2f037602006-10-28 13:51:49 +0000167 def testRadians(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000168 self.assertRaises(TypeError, math.radians)
Georg Brandl2f037602006-10-28 13:51:49 +0000169 self.ftest('radians(180)', math.radians(180), math.pi)
170 self.ftest('radians(90)', math.radians(90), math.pi/2)
171 self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
Raymond Hettinger64108af2002-05-13 03:55:01 +0000172
Georg Brandl2f037602006-10-28 13:51:49 +0000173 def testSin(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000174 self.assertRaises(TypeError, math.sin)
Georg Brandl2f037602006-10-28 13:51:49 +0000175 self.ftest('sin(0)', math.sin(0), 0)
176 self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
177 self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000178
Georg Brandl2f037602006-10-28 13:51:49 +0000179 def testSinh(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000180 self.assertRaises(TypeError, math.sinh)
Georg Brandl2f037602006-10-28 13:51:49 +0000181 self.ftest('sinh(0)', math.sinh(0), 0)
182 self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
183 self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000184
Georg Brandl2f037602006-10-28 13:51:49 +0000185 def testSqrt(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000186 self.assertRaises(TypeError, math.sqrt)
Georg Brandl2f037602006-10-28 13:51:49 +0000187 self.ftest('sqrt(0)', math.sqrt(0), 0)
188 self.ftest('sqrt(1)', math.sqrt(1), 1)
189 self.ftest('sqrt(4)', math.sqrt(4), 2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000190
Georg Brandl2f037602006-10-28 13:51:49 +0000191 def testTan(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000192 self.assertRaises(TypeError, math.tan)
Georg Brandl2f037602006-10-28 13:51:49 +0000193 self.ftest('tan(0)', math.tan(0), 0)
194 self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
195 self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000196
Georg Brandl2f037602006-10-28 13:51:49 +0000197 def testTanh(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000198 self.assertRaises(TypeError, math.tanh)
Georg Brandl2f037602006-10-28 13:51:49 +0000199 self.ftest('tanh(0)', math.tanh(0), 0)
200 self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
Tim Peters1d120612000-10-12 06:10:25 +0000201
Georg Brandl2f037602006-10-28 13:51:49 +0000202 # RED_FLAG 16-Oct-2000 Tim
203 # While 2.0 is more consistent about exceptions than previous releases, it
204 # still fails this part of the test on some platforms. For now, we only
205 # *run* test_exceptions() in verbose mode, so that this isn't normally
206 # tested.
Tim Peters1d120612000-10-12 06:10:25 +0000207
Georg Brandl2f037602006-10-28 13:51:49 +0000208 if verbose:
209 def test_exceptions(self):
210 try:
211 x = math.exp(-1000000000)
212 except:
213 # mathmodule.c is failing to weed out underflows from libm, or
214 # we've got an fp format with huge dynamic range
215 self.fail("underflowing exp() should not have raised "
216 "an exception")
217 if x != 0:
218 self.fail("underflowing exp() should have returned 0")
Tim Peters1d120612000-10-12 06:10:25 +0000219
Georg Brandl2f037602006-10-28 13:51:49 +0000220 # If this fails, probably using a strict IEEE-754 conforming libm, and x
221 # is +Inf afterwards. But Python wants overflows detected by default.
222 try:
223 x = math.exp(1000000000)
224 except OverflowError:
225 pass
226 else:
227 self.fail("overflowing exp() didn't trigger OverflowError")
Tim Peters1d120612000-10-12 06:10:25 +0000228
Georg Brandl2f037602006-10-28 13:51:49 +0000229 # If this fails, it could be a puzzle. One odd possibility is that
230 # mathmodule.c's macros are getting confused while comparing
231 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
232 # as a result (and so raising OverflowError instead).
233 try:
234 x = math.sqrt(-1.0)
235 except ValueError:
236 pass
237 else:
238 self.fail("sqrt(-1) didn't raise ValueError")
Tim Peters98c81842000-10-16 17:35:13 +0000239
Georg Brandl2f037602006-10-28 13:51:49 +0000240
241def test_main():
242 run_unittest(MathTests)
243
244if __name__ == '__main__':
245 test_main()