blob: 1458c82f8c3b86454a7b1e66891086cd5eeaa99d [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)
Guido van Rossumfcce6301996-08-08 18:26:25 +000095
Georg Brandl2f037602006-10-28 13:51:49 +000096 def testFmod(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +000097 self.assertRaises(TypeError, math.fmod)
Georg Brandl2f037602006-10-28 13:51:49 +000098 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
Georg Brandl2f037602006-10-28 13:51:49 +0000105 def testFrexp(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000106 self.assertRaises(TypeError, math.frexp)
107
Georg Brandl2f037602006-10-28 13:51:49 +0000108 def testfrexp(name, (mant, exp), (emant, eexp)):
109 if abs(mant-emant) > eps or exp != eexp:
110 self.fail('%s returned %r, expected %r'%\
111 (name, (mant, exp), (emant,eexp)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000112
Georg Brandl2f037602006-10-28 13:51:49 +0000113 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
114 testfrexp('frexp(0)', math.frexp(0), (0, 0))
115 testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
116 testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000117
Georg Brandl2f037602006-10-28 13:51:49 +0000118 def testHypot(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000119 self.assertRaises(TypeError, math.hypot)
Georg Brandl2f037602006-10-28 13:51:49 +0000120 self.ftest('hypot(0,0)', math.hypot(0,0), 0)
121 self.ftest('hypot(3,4)', math.hypot(3,4), 5)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000122
Georg Brandl2f037602006-10-28 13:51:49 +0000123 def testLdexp(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000124 self.assertRaises(TypeError, math.ldexp)
Georg Brandl2f037602006-10-28 13:51:49 +0000125 self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
126 self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
127 self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
128 self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000129
Georg Brandl2f037602006-10-28 13:51:49 +0000130 def testLog(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000131 self.assertRaises(TypeError, math.log)
Georg Brandl2f037602006-10-28 13:51:49 +0000132 self.ftest('log(1/e)', math.log(1/math.e), -1)
133 self.ftest('log(1)', math.log(1), 0)
134 self.ftest('log(e)', math.log(math.e), 1)
135 self.ftest('log(32,2)', math.log(32,2), 5)
136 self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
137 self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000138
Georg Brandl2f037602006-10-28 13:51:49 +0000139 def testLog10(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000140 self.assertRaises(TypeError, math.log10)
Georg Brandl2f037602006-10-28 13:51:49 +0000141 self.ftest('log10(0.1)', math.log10(0.1), -1)
142 self.ftest('log10(1)', math.log10(1), 0)
143 self.ftest('log10(10)', math.log10(10), 1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000144
Georg Brandl2f037602006-10-28 13:51:49 +0000145 def testModf(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000146 self.assertRaises(TypeError, math.modf)
147
Georg Brandl2f037602006-10-28 13:51:49 +0000148 def testmodf(name, (v1, v2), (e1, e2)):
149 if abs(v1-e1) > eps or abs(v2-e2):
150 self.fail('%s returned %r, expected %r'%\
151 (name, (v1,v2), (e1,e2)))
Guido van Rossumfcce6301996-08-08 18:26:25 +0000152
Georg Brandl2f037602006-10-28 13:51:49 +0000153 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
154 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
155
156 def testPow(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000157 self.assertRaises(TypeError, math.pow)
Georg Brandl2f037602006-10-28 13:51:49 +0000158 self.ftest('pow(0,1)', math.pow(0,1), 0)
159 self.ftest('pow(1,0)', math.pow(1,0), 1)
160 self.ftest('pow(2,1)', math.pow(2,1), 2)
161 self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000162
Georg Brandl2f037602006-10-28 13:51:49 +0000163 def testRadians(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000164 self.assertRaises(TypeError, math.radians)
Georg Brandl2f037602006-10-28 13:51:49 +0000165 self.ftest('radians(180)', math.radians(180), math.pi)
166 self.ftest('radians(90)', math.radians(90), math.pi/2)
167 self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
Raymond Hettinger64108af2002-05-13 03:55:01 +0000168
Georg Brandl2f037602006-10-28 13:51:49 +0000169 def testSin(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000170 self.assertRaises(TypeError, math.sin)
Georg Brandl2f037602006-10-28 13:51:49 +0000171 self.ftest('sin(0)', math.sin(0), 0)
172 self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
173 self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000174
Georg Brandl2f037602006-10-28 13:51:49 +0000175 def testSinh(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000176 self.assertRaises(TypeError, math.sinh)
Georg Brandl2f037602006-10-28 13:51:49 +0000177 self.ftest('sinh(0)', math.sinh(0), 0)
178 self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
179 self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000180
Georg Brandl2f037602006-10-28 13:51:49 +0000181 def testSqrt(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000182 self.assertRaises(TypeError, math.sqrt)
Georg Brandl2f037602006-10-28 13:51:49 +0000183 self.ftest('sqrt(0)', math.sqrt(0), 0)
184 self.ftest('sqrt(1)', math.sqrt(1), 1)
185 self.ftest('sqrt(4)', math.sqrt(4), 2)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000186
Georg Brandl2f037602006-10-28 13:51:49 +0000187 def testTan(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000188 self.assertRaises(TypeError, math.tan)
Georg Brandl2f037602006-10-28 13:51:49 +0000189 self.ftest('tan(0)', math.tan(0), 0)
190 self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
191 self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
Guido van Rossumfcce6301996-08-08 18:26:25 +0000192
Georg Brandl2f037602006-10-28 13:51:49 +0000193 def testTanh(self):
Walter Dörwald92911bf2006-10-29 22:06:28 +0000194 self.assertRaises(TypeError, math.tanh)
Georg Brandl2f037602006-10-28 13:51:49 +0000195 self.ftest('tanh(0)', math.tanh(0), 0)
196 self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
Tim Peters1d120612000-10-12 06:10:25 +0000197
Georg Brandl2f037602006-10-28 13:51:49 +0000198 # RED_FLAG 16-Oct-2000 Tim
199 # While 2.0 is more consistent about exceptions than previous releases, it
200 # still fails this part of the test on some platforms. For now, we only
201 # *run* test_exceptions() in verbose mode, so that this isn't normally
202 # tested.
Tim Peters1d120612000-10-12 06:10:25 +0000203
Georg Brandl2f037602006-10-28 13:51:49 +0000204 if verbose:
205 def test_exceptions(self):
206 try:
207 x = math.exp(-1000000000)
208 except:
209 # mathmodule.c is failing to weed out underflows from libm, or
210 # we've got an fp format with huge dynamic range
211 self.fail("underflowing exp() should not have raised "
212 "an exception")
213 if x != 0:
214 self.fail("underflowing exp() should have returned 0")
Tim Peters1d120612000-10-12 06:10:25 +0000215
Georg Brandl2f037602006-10-28 13:51:49 +0000216 # If this fails, probably using a strict IEEE-754 conforming libm, and x
217 # is +Inf afterwards. But Python wants overflows detected by default.
218 try:
219 x = math.exp(1000000000)
220 except OverflowError:
221 pass
222 else:
223 self.fail("overflowing exp() didn't trigger OverflowError")
Tim Peters1d120612000-10-12 06:10:25 +0000224
Georg Brandl2f037602006-10-28 13:51:49 +0000225 # If this fails, it could be a puzzle. One odd possibility is that
226 # mathmodule.c's macros are getting confused while comparing
227 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
228 # as a result (and so raising OverflowError instead).
229 try:
230 x = math.sqrt(-1.0)
231 except ValueError:
232 pass
233 else:
234 self.fail("sqrt(-1) didn't raise ValueError")
Tim Peters98c81842000-10-16 17:35:13 +0000235
Georg Brandl2f037602006-10-28 13:51:49 +0000236
237def test_main():
238 run_unittest(MathTests)
239
240if __name__ == '__main__':
241 test_main()