Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 1 | # Python test set -- math module |
| 2 | # XXXX Should not do tests around zero only |
| 3 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 4 | from test.test_support import run_unittest, verbose |
| 5 | import unittest |
| 6 | import math |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 5ab007b | 1996-08-29 19:00:46 +0000 | [diff] [blame] | 8 | seps='1e-05' |
| 9 | eps = eval(seps) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 10 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 11 | class MathTests(unittest.TestCase): |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 12 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 13 | def ftest(self, name, value, expected): |
| 14 | if abs(value-expected) > eps: |
Nick Coghlan | ec2ce9b | 2007-07-27 10:36:30 +0000 | [diff] [blame] | 15 | # 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 Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 20 | (name, value, expected)) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 21 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 22 | def testConstants(self): |
| 23 | self.ftest('pi', math.pi, 3.1415926) |
| 24 | self.ftest('e', math.e, 2.7182818) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 25 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 26 | def testAcos(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 27 | self.assertRaises(TypeError, math.acos) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 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 Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 31 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 32 | def testAsin(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 33 | self.assertRaises(TypeError, math.asin) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 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 Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 37 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 38 | def testAtan(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 39 | self.assertRaises(TypeError, math.atan) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 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 Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 43 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 44 | def testAtan2(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 45 | self.assertRaises(TypeError, math.atan2) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 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 Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 51 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 52 | def testCeil(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 53 | self.assertRaises(TypeError, math.ceil) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 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 Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 60 | |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame^] | 61 | class TestCeil(object): |
| 62 | def __ceil__(self): |
| 63 | return 42 |
| 64 | class TestNoCeil(object): |
| 65 | pass |
| 66 | self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42) |
| 67 | self.assertRaises(TypeError, math.ceil, TestNoCeil()) |
| 68 | |
| 69 | t = TestNoCeil() |
| 70 | t.__ceil__ = lambda *args: args |
| 71 | self.assertRaises(TypeError, math.ceil, t) |
| 72 | self.assertRaises(TypeError, math.ceil, t, 0) |
| 73 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 74 | def testCos(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 75 | self.assertRaises(TypeError, math.cos) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 76 | self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0) |
| 77 | self.ftest('cos(0)', math.cos(0), 1) |
| 78 | self.ftest('cos(pi/2)', math.cos(math.pi/2), 0) |
| 79 | self.ftest('cos(pi)', math.cos(math.pi), -1) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 80 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 81 | def testCosh(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 82 | self.assertRaises(TypeError, math.cosh) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 83 | self.ftest('cosh(0)', math.cosh(0), 1) |
| 84 | self.ftest('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert |
Raymond Hettinger | 64108af | 2002-05-13 03:55:01 +0000 | [diff] [blame] | 85 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 86 | def testDegrees(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 87 | self.assertRaises(TypeError, math.degrees) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 88 | self.ftest('degrees(pi)', math.degrees(math.pi), 180.0) |
| 89 | self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0) |
| 90 | self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 91 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 92 | def testExp(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 93 | self.assertRaises(TypeError, math.exp) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 94 | self.ftest('exp(-1)', math.exp(-1), 1/math.e) |
| 95 | self.ftest('exp(0)', math.exp(0), 1) |
| 96 | self.ftest('exp(1)', math.exp(1), math.e) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 97 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 98 | def testFabs(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 99 | self.assertRaises(TypeError, math.fabs) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 100 | self.ftest('fabs(-1)', math.fabs(-1), 1) |
| 101 | self.ftest('fabs(0)', math.fabs(0), 0) |
| 102 | self.ftest('fabs(1)', math.fabs(1), 1) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 103 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 104 | def testFloor(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 105 | self.assertRaises(TypeError, math.floor) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 106 | self.ftest('floor(0.5)', math.floor(0.5), 0) |
| 107 | self.ftest('floor(1.0)', math.floor(1.0), 1) |
| 108 | self.ftest('floor(1.5)', math.floor(1.5), 1) |
| 109 | self.ftest('floor(-0.5)', math.floor(-0.5), -1) |
| 110 | self.ftest('floor(-1.0)', math.floor(-1.0), -1) |
| 111 | self.ftest('floor(-1.5)', math.floor(-1.5), -2) |
Nick Coghlan | 00f2029 | 2007-07-26 14:03:00 +0000 | [diff] [blame] | 112 | # pow() relies on floor() to check for integers |
| 113 | # This fails on some platforms - so check it here |
| 114 | self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167) |
| 115 | self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 116 | |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame^] | 117 | class TestFloor(object): |
| 118 | def __floor__(self): |
| 119 | return 42 |
| 120 | class TestNoFloor(object): |
| 121 | pass |
| 122 | self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42) |
| 123 | self.assertRaises(TypeError, math.floor, TestNoFloor()) |
| 124 | |
| 125 | t = TestNoFloor() |
| 126 | t.__floor__ = lambda *args: args |
| 127 | self.assertRaises(TypeError, math.floor, t) |
| 128 | self.assertRaises(TypeError, math.floor, t, 0) |
| 129 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 130 | def testFmod(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 131 | self.assertRaises(TypeError, math.fmod) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 132 | self.ftest('fmod(10,1)', math.fmod(10,1), 0) |
| 133 | self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0) |
| 134 | self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1) |
| 135 | self.ftest('fmod(-10,1)', math.fmod(-10,1), 0) |
| 136 | self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0) |
| 137 | self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 138 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 139 | def testFrexp(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 140 | self.assertRaises(TypeError, math.frexp) |
| 141 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 142 | def testfrexp(name, (mant, exp), (emant, eexp)): |
| 143 | if abs(mant-emant) > eps or exp != eexp: |
| 144 | self.fail('%s returned %r, expected %r'%\ |
| 145 | (name, (mant, exp), (emant,eexp))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 146 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 147 | testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1)) |
| 148 | testfrexp('frexp(0)', math.frexp(0), (0, 0)) |
| 149 | testfrexp('frexp(1)', math.frexp(1), (0.5, 1)) |
| 150 | testfrexp('frexp(2)', math.frexp(2), (0.5, 2)) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 151 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 152 | def testHypot(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 153 | self.assertRaises(TypeError, math.hypot) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 154 | self.ftest('hypot(0,0)', math.hypot(0,0), 0) |
| 155 | self.ftest('hypot(3,4)', math.hypot(3,4), 5) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 156 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 157 | def testLdexp(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 158 | self.assertRaises(TypeError, math.ldexp) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 159 | self.ftest('ldexp(0,1)', math.ldexp(0,1), 0) |
| 160 | self.ftest('ldexp(1,1)', math.ldexp(1,1), 2) |
| 161 | self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5) |
| 162 | self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 163 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 164 | def testLog(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 165 | self.assertRaises(TypeError, math.log) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 166 | self.ftest('log(1/e)', math.log(1/math.e), -1) |
| 167 | self.ftest('log(1)', math.log(1), 0) |
| 168 | self.ftest('log(e)', math.log(math.e), 1) |
| 169 | self.ftest('log(32,2)', math.log(32,2), 5) |
| 170 | self.ftest('log(10**40, 10)', math.log(10**40, 10), 40) |
| 171 | self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 172 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 173 | def testLog10(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 174 | self.assertRaises(TypeError, math.log10) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 175 | self.ftest('log10(0.1)', math.log10(0.1), -1) |
| 176 | self.ftest('log10(1)', math.log10(1), 0) |
| 177 | self.ftest('log10(10)', math.log10(10), 1) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 178 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 179 | def testModf(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 180 | self.assertRaises(TypeError, math.modf) |
| 181 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 182 | def testmodf(name, (v1, v2), (e1, e2)): |
| 183 | if abs(v1-e1) > eps or abs(v2-e2): |
| 184 | self.fail('%s returned %r, expected %r'%\ |
| 185 | (name, (v1,v2), (e1,e2))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 186 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 187 | testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0)) |
| 188 | testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0)) |
Tim Peters | abd8a33 | 2006-11-03 02:32:46 +0000 | [diff] [blame] | 189 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 190 | def testPow(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 191 | self.assertRaises(TypeError, math.pow) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 192 | self.ftest('pow(0,1)', math.pow(0,1), 0) |
| 193 | self.ftest('pow(1,0)', math.pow(1,0), 1) |
| 194 | self.ftest('pow(2,1)', math.pow(2,1), 2) |
| 195 | self.ftest('pow(2,-1)', math.pow(2,-1), 0.5) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 196 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 197 | def testRadians(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 198 | self.assertRaises(TypeError, math.radians) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 199 | self.ftest('radians(180)', math.radians(180), math.pi) |
| 200 | self.ftest('radians(90)', math.radians(90), math.pi/2) |
| 201 | self.ftest('radians(-45)', math.radians(-45), -math.pi/4) |
Raymond Hettinger | 64108af | 2002-05-13 03:55:01 +0000 | [diff] [blame] | 202 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 203 | def testSin(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 204 | self.assertRaises(TypeError, math.sin) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 205 | self.ftest('sin(0)', math.sin(0), 0) |
| 206 | self.ftest('sin(pi/2)', math.sin(math.pi/2), 1) |
| 207 | self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 208 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 209 | def testSinh(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 210 | self.assertRaises(TypeError, math.sinh) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 211 | self.ftest('sinh(0)', math.sinh(0), 0) |
| 212 | self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1) |
| 213 | self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 214 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 215 | def testSqrt(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 216 | self.assertRaises(TypeError, math.sqrt) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 217 | self.ftest('sqrt(0)', math.sqrt(0), 0) |
| 218 | self.ftest('sqrt(1)', math.sqrt(1), 1) |
| 219 | self.ftest('sqrt(4)', math.sqrt(4), 2) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 220 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 221 | def testTan(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 222 | self.assertRaises(TypeError, math.tan) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 223 | self.ftest('tan(0)', math.tan(0), 0) |
| 224 | self.ftest('tan(pi/4)', math.tan(math.pi/4), 1) |
| 225 | self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 226 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 227 | def testTanh(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 228 | self.assertRaises(TypeError, math.tanh) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 229 | self.ftest('tanh(0)', math.tanh(0), 0) |
| 230 | self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 231 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 232 | # RED_FLAG 16-Oct-2000 Tim |
| 233 | # While 2.0 is more consistent about exceptions than previous releases, it |
| 234 | # still fails this part of the test on some platforms. For now, we only |
| 235 | # *run* test_exceptions() in verbose mode, so that this isn't normally |
| 236 | # tested. |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 237 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 238 | if verbose: |
| 239 | def test_exceptions(self): |
| 240 | try: |
| 241 | x = math.exp(-1000000000) |
| 242 | except: |
| 243 | # mathmodule.c is failing to weed out underflows from libm, or |
| 244 | # we've got an fp format with huge dynamic range |
| 245 | self.fail("underflowing exp() should not have raised " |
| 246 | "an exception") |
| 247 | if x != 0: |
| 248 | self.fail("underflowing exp() should have returned 0") |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 249 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 250 | # If this fails, probably using a strict IEEE-754 conforming libm, and x |
| 251 | # is +Inf afterwards. But Python wants overflows detected by default. |
| 252 | try: |
| 253 | x = math.exp(1000000000) |
| 254 | except OverflowError: |
| 255 | pass |
| 256 | else: |
| 257 | self.fail("overflowing exp() didn't trigger OverflowError") |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 258 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 259 | # If this fails, it could be a puzzle. One odd possibility is that |
| 260 | # mathmodule.c's macros are getting confused while comparing |
| 261 | # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE |
| 262 | # as a result (and so raising OverflowError instead). |
| 263 | try: |
| 264 | x = math.sqrt(-1.0) |
| 265 | except ValueError: |
| 266 | pass |
| 267 | else: |
| 268 | self.fail("sqrt(-1) didn't raise ValueError") |
Tim Peters | 98c8184 | 2000-10-16 17:35:13 +0000 | [diff] [blame] | 269 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 270 | |
| 271 | def test_main(): |
| 272 | run_unittest(MathTests) |
| 273 | |
| 274 | if __name__ == '__main__': |
| 275 | test_main() |