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 |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 7 | import os |
| 8 | import sys |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 9 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 10 | eps = 1E-05 |
| 11 | NAN = float('nan') |
| 12 | INF = float('inf') |
| 13 | NINF = float('-inf') |
| 14 | |
| 15 | # locate file with test values |
| 16 | if __name__ == '__main__': |
| 17 | file = sys.argv[0] |
| 18 | else: |
| 19 | file = __file__ |
| 20 | test_dir = os.path.dirname(file) or os.curdir |
| 21 | test_file = os.path.join(test_dir, 'cmath_testcases.txt') |
| 22 | |
| 23 | def parse_testfile(fname): |
| 24 | """Parse a file with test values |
| 25 | |
| 26 | Empty lines or lines starting with -- are ignored |
| 27 | yields id, fn, arg_real, arg_imag, exp_real, exp_imag |
| 28 | """ |
| 29 | with open(fname) as fp: |
| 30 | for line in fp: |
| 31 | # skip comment lines and blank lines |
| 32 | if line.startswith('--') or not line.strip(): |
| 33 | continue |
| 34 | |
| 35 | lhs, rhs = line.split('->') |
| 36 | id, fn, arg_real, arg_imag = lhs.split() |
| 37 | rhs_pieces = rhs.split() |
| 38 | exp_real, exp_imag = rhs_pieces[0], rhs_pieces[1] |
| 39 | flags = rhs_pieces[2:] |
| 40 | |
| 41 | yield (id, fn, |
| 42 | float(arg_real), float(arg_imag), |
| 43 | float(exp_real), float(exp_imag), |
| 44 | flags |
| 45 | ) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 46 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 47 | class MathTests(unittest.TestCase): |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 48 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 49 | def ftest(self, name, value, expected): |
| 50 | if abs(value-expected) > eps: |
Nick Coghlan | ec2ce9b | 2007-07-27 10:36:30 +0000 | [diff] [blame] | 51 | # Use %r instead of %f so the error message |
| 52 | # displays full precision. Otherwise discrepancies |
| 53 | # in the last few bits will lead to very confusing |
| 54 | # error messages |
| 55 | self.fail('%s returned %r, expected %r' % |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 56 | (name, value, expected)) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 57 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 58 | def testConstants(self): |
| 59 | self.ftest('pi', math.pi, 3.1415926) |
| 60 | self.ftest('e', math.e, 2.7182818) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 61 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 62 | def testAcos(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 63 | self.assertRaises(TypeError, math.acos) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 64 | self.ftest('acos(-1)', math.acos(-1), math.pi) |
| 65 | self.ftest('acos(0)', math.acos(0), math.pi/2) |
| 66 | self.ftest('acos(1)', math.acos(1), 0) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 67 | self.assertRaises(ValueError, math.acos, INF) |
| 68 | self.assertRaises(ValueError, math.acos, NINF) |
| 69 | self.assert_(math.isnan(math.acos(NAN))) |
| 70 | |
| 71 | def testAcosh(self): |
| 72 | self.assertRaises(TypeError, math.acosh) |
| 73 | self.ftest('acosh(1)', math.acosh(1), 0) |
| 74 | self.ftest('acosh(2)', math.acosh(2), 1.3169578969248168) |
| 75 | self.assertRaises(ValueError, math.acosh, 0) |
| 76 | self.assertRaises(ValueError, math.acosh, -1) |
| 77 | self.assertEquals(math.acosh(INF), INF) |
| 78 | self.assertRaises(ValueError, math.acosh, NINF) |
| 79 | self.assert_(math.isnan(math.acosh(NAN))) |
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 testAsin(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 82 | self.assertRaises(TypeError, math.asin) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 83 | self.ftest('asin(-1)', math.asin(-1), -math.pi/2) |
| 84 | self.ftest('asin(0)', math.asin(0), 0) |
| 85 | self.ftest('asin(1)', math.asin(1), math.pi/2) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 86 | self.assertRaises(ValueError, math.asin, INF) |
| 87 | self.assertRaises(ValueError, math.asin, NINF) |
| 88 | self.assert_(math.isnan(math.asin(NAN))) |
| 89 | |
| 90 | def testAsinh(self): |
| 91 | self.assertRaises(TypeError, math.asinh) |
| 92 | self.ftest('asinh(0)', math.asinh(0), 0) |
| 93 | self.ftest('asinh(1)', math.asinh(1), 0.88137358701954305) |
| 94 | self.ftest('asinh(-1)', math.asinh(-1), -0.88137358701954305) |
| 95 | self.assertEquals(math.asinh(INF), INF) |
| 96 | self.assertEquals(math.asinh(NINF), NINF) |
| 97 | self.assert_(math.isnan(math.asinh(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 98 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 99 | def testAtan(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 100 | self.assertRaises(TypeError, math.atan) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 101 | self.ftest('atan(-1)', math.atan(-1), -math.pi/4) |
| 102 | self.ftest('atan(0)', math.atan(0), 0) |
| 103 | self.ftest('atan(1)', math.atan(1), math.pi/4) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 104 | self.ftest('atan(inf)', math.atan(INF), math.pi/2) |
Mark Dickinson | e941d97 | 2008-04-19 18:51:48 +0000 | [diff] [blame] | 105 | self.ftest('atan(-inf)', math.atan(NINF), -math.pi/2) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 106 | self.assert_(math.isnan(math.atan(NAN))) |
| 107 | |
| 108 | def testAtanh(self): |
| 109 | self.assertRaises(TypeError, math.atan) |
| 110 | self.ftest('atanh(0)', math.atanh(0), 0) |
| 111 | self.ftest('atanh(0.5)', math.atanh(0.5), 0.54930614433405489) |
| 112 | self.ftest('atanh(-0.5)', math.atanh(-0.5), -0.54930614433405489) |
| 113 | self.assertRaises(ValueError, math.atanh, 1) |
| 114 | self.assertRaises(ValueError, math.atanh, -1) |
| 115 | self.assertRaises(ValueError, math.atanh, INF) |
| 116 | self.assertRaises(ValueError, math.atanh, NINF) |
| 117 | self.assert_(math.isnan(math.atanh(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 118 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 119 | def testAtan2(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 120 | self.assertRaises(TypeError, math.atan2) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 121 | self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2) |
| 122 | self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4) |
| 123 | self.ftest('atan2(0, 1)', math.atan2(0, 1), 0) |
| 124 | self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4) |
| 125 | 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] | 126 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 127 | def testCeil(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 128 | self.assertRaises(TypeError, math.ceil) |
Jeffrey Yasskin | 737c73f | 2008-01-04 08:01:23 +0000 | [diff] [blame] | 129 | # These types will be int in py3k. |
| 130 | self.assertEquals(float, type(math.ceil(1))) |
| 131 | self.assertEquals(float, type(math.ceil(1L))) |
| 132 | self.assertEquals(float, type(math.ceil(1.0))) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 133 | self.ftest('ceil(0.5)', math.ceil(0.5), 1) |
| 134 | self.ftest('ceil(1.0)', math.ceil(1.0), 1) |
| 135 | self.ftest('ceil(1.5)', math.ceil(1.5), 2) |
| 136 | self.ftest('ceil(-0.5)', math.ceil(-0.5), 0) |
| 137 | self.ftest('ceil(-1.0)', math.ceil(-1.0), -1) |
| 138 | self.ftest('ceil(-1.5)', math.ceil(-1.5), -1) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 139 | self.assertEquals(math.ceil(INF), INF) |
| 140 | self.assertEquals(math.ceil(NINF), NINF) |
| 141 | self.assert_(math.isnan(math.ceil(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 142 | |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 143 | class TestCeil(object): |
Jeffrey Yasskin | 9871d8f | 2008-01-05 08:47:13 +0000 | [diff] [blame] | 144 | def __float__(self): |
| 145 | return 41.3 |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 146 | class TestNoCeil(object): |
| 147 | pass |
| 148 | self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42) |
| 149 | self.assertRaises(TypeError, math.ceil, TestNoCeil()) |
| 150 | |
| 151 | t = TestNoCeil() |
| 152 | t.__ceil__ = lambda *args: args |
| 153 | self.assertRaises(TypeError, math.ceil, t) |
| 154 | self.assertRaises(TypeError, math.ceil, t, 0) |
| 155 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 156 | if float.__getformat__("double").startswith("IEEE"): |
| 157 | def testCopysign(self): |
| 158 | self.assertRaises(TypeError, math.copysign) |
| 159 | # copysign should let us distinguish signs of zeros |
| 160 | self.assertEquals(copysign(1., 0.), 1.) |
| 161 | self.assertEquals(copysign(1., -0.), -1.) |
| 162 | self.assertEquals(copysign(INF, 0.), INF) |
| 163 | self.assertEquals(copysign(INF, -0.), NINF) |
| 164 | self.assertEquals(copysign(NINF, 0.), INF) |
| 165 | self.assertEquals(copysign(NINF, -0.), NINF) |
| 166 | # and of infinities |
| 167 | self.assertEquals(copysign(1., INF), 1.) |
| 168 | self.assertEquals(copysign(1., NINF), -1.) |
| 169 | self.assertEquals(copysign(INF, INF), INF) |
| 170 | self.assertEquals(copysign(INF, NINF), NINF) |
| 171 | self.assertEquals(copysign(NINF, INF), INF) |
| 172 | self.assertEquals(copysign(NINF, NINF), NINF) |
| 173 | self.assert_(math.isnan(copysign(NAN, 1.))) |
| 174 | self.assert_(math.isnan(copysign(NAN, INF))) |
| 175 | self.assert_(math.isnan(copysign(NAN, NINF))) |
| 176 | self.assert_(math.isnan(copysign(NAN, NAN))) |
| 177 | # copysign(INF, NAN) may be INF or it may be NINF, since |
| 178 | # we don't know whether the sign bit of NAN is set on any |
| 179 | # given platform. |
| 180 | self.assert_(math.isinf(copysign(INF, NAN))) |
| 181 | # similarly, copysign(2., NAN) could be 2. or -2. |
| 182 | self.assertEquals(abs(copysign(2., NAN)), 2.) |
| 183 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 184 | def testCos(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 185 | self.assertRaises(TypeError, math.cos) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 186 | self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0) |
| 187 | self.ftest('cos(0)', math.cos(0), 1) |
| 188 | self.ftest('cos(pi/2)', math.cos(math.pi/2), 0) |
| 189 | self.ftest('cos(pi)', math.cos(math.pi), -1) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 190 | try: |
| 191 | self.assert_(math.isnan(math.cos(INF))) |
| 192 | self.assert_(math.isnan(math.cos(NINF))) |
| 193 | except ValueError: |
| 194 | self.assertRaises(ValueError, math.cos, INF) |
| 195 | self.assertRaises(ValueError, math.cos, NINF) |
| 196 | self.assert_(math.isnan(math.cos(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 197 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 198 | def testCosh(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 199 | self.assertRaises(TypeError, math.cosh) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 200 | self.ftest('cosh(0)', math.cosh(0), 1) |
| 201 | self.ftest('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 202 | self.assertEquals(math.cosh(INF), INF) |
| 203 | self.assertEquals(math.cosh(NINF), INF) |
| 204 | self.assert_(math.isnan(math.cosh(NAN))) |
Raymond Hettinger | 64108af | 2002-05-13 03:55:01 +0000 | [diff] [blame] | 205 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 206 | def testDegrees(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 207 | self.assertRaises(TypeError, math.degrees) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 208 | self.ftest('degrees(pi)', math.degrees(math.pi), 180.0) |
| 209 | self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0) |
| 210 | 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] | 211 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 212 | def testExp(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 213 | self.assertRaises(TypeError, math.exp) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 214 | self.ftest('exp(-1)', math.exp(-1), 1/math.e) |
| 215 | self.ftest('exp(0)', math.exp(0), 1) |
| 216 | self.ftest('exp(1)', math.exp(1), math.e) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 217 | self.assertEquals(math.exp(INF), INF) |
| 218 | self.assertEquals(math.exp(NINF), 0.) |
| 219 | self.assert_(math.isnan(math.exp(NAN))) |
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 testFabs(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 222 | self.assertRaises(TypeError, math.fabs) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 223 | self.ftest('fabs(-1)', math.fabs(-1), 1) |
| 224 | self.ftest('fabs(0)', math.fabs(0), 0) |
| 225 | self.ftest('fabs(1)', math.fabs(1), 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 testFloor(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 228 | self.assertRaises(TypeError, math.floor) |
Jeffrey Yasskin | 737c73f | 2008-01-04 08:01:23 +0000 | [diff] [blame] | 229 | # These types will be int in py3k. |
| 230 | self.assertEquals(float, type(math.floor(1))) |
| 231 | self.assertEquals(float, type(math.floor(1L))) |
| 232 | self.assertEquals(float, type(math.floor(1.0))) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 233 | self.ftest('floor(0.5)', math.floor(0.5), 0) |
| 234 | self.ftest('floor(1.0)', math.floor(1.0), 1) |
| 235 | self.ftest('floor(1.5)', math.floor(1.5), 1) |
| 236 | self.ftest('floor(-0.5)', math.floor(-0.5), -1) |
| 237 | self.ftest('floor(-1.0)', math.floor(-1.0), -1) |
| 238 | self.ftest('floor(-1.5)', math.floor(-1.5), -2) |
Nick Coghlan | 00f2029 | 2007-07-26 14:03:00 +0000 | [diff] [blame] | 239 | # pow() relies on floor() to check for integers |
| 240 | # This fails on some platforms - so check it here |
| 241 | self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167) |
| 242 | self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 243 | self.assertEquals(math.ceil(INF), INF) |
| 244 | self.assertEquals(math.ceil(NINF), NINF) |
| 245 | self.assert_(math.isnan(math.floor(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 246 | |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 247 | class TestFloor(object): |
Jeffrey Yasskin | 9871d8f | 2008-01-05 08:47:13 +0000 | [diff] [blame] | 248 | def __float__(self): |
| 249 | return 42.3 |
Jeffrey Yasskin | 2f3c16b | 2008-01-03 02:21:52 +0000 | [diff] [blame] | 250 | class TestNoFloor(object): |
| 251 | pass |
| 252 | self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42) |
| 253 | self.assertRaises(TypeError, math.floor, TestNoFloor()) |
| 254 | |
| 255 | t = TestNoFloor() |
| 256 | t.__floor__ = lambda *args: args |
| 257 | self.assertRaises(TypeError, math.floor, t) |
| 258 | self.assertRaises(TypeError, math.floor, t, 0) |
| 259 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 260 | def testFmod(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 261 | self.assertRaises(TypeError, math.fmod) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 262 | self.ftest('fmod(10,1)', math.fmod(10,1), 0) |
| 263 | self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0) |
| 264 | self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1) |
| 265 | self.ftest('fmod(-10,1)', math.fmod(-10,1), 0) |
| 266 | self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0) |
| 267 | self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 268 | self.assert_(math.isnan(math.fmod(NAN, 1.))) |
| 269 | self.assert_(math.isnan(math.fmod(1., NAN))) |
| 270 | self.assert_(math.isnan(math.fmod(NAN, NAN))) |
| 271 | self.assertRaises(ValueError, math.fmod, 1., 0.) |
| 272 | self.assertRaises(ValueError, math.fmod, INF, 1.) |
| 273 | self.assertRaises(ValueError, math.fmod, NINF, 1.) |
| 274 | self.assertRaises(ValueError, math.fmod, INF, 0.) |
| 275 | self.assertEquals(math.fmod(3.0, INF), 3.0) |
| 276 | self.assertEquals(math.fmod(-3.0, INF), -3.0) |
| 277 | self.assertEquals(math.fmod(3.0, NINF), 3.0) |
| 278 | self.assertEquals(math.fmod(-3.0, NINF), -3.0) |
| 279 | self.assertEquals(math.fmod(0.0, 3.0), 0.0) |
| 280 | self.assertEquals(math.fmod(0.0, NINF), 0.0) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 281 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 282 | def testFrexp(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 283 | self.assertRaises(TypeError, math.frexp) |
| 284 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 285 | def testfrexp(name, (mant, exp), (emant, eexp)): |
| 286 | if abs(mant-emant) > eps or exp != eexp: |
| 287 | self.fail('%s returned %r, expected %r'%\ |
| 288 | (name, (mant, exp), (emant,eexp))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 289 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 290 | testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1)) |
| 291 | testfrexp('frexp(0)', math.frexp(0), (0, 0)) |
| 292 | testfrexp('frexp(1)', math.frexp(1), (0.5, 1)) |
| 293 | testfrexp('frexp(2)', math.frexp(2), (0.5, 2)) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 294 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 295 | self.assertEquals(math.frexp(INF)[0], INF) |
| 296 | self.assertEquals(math.frexp(NINF)[0], NINF) |
| 297 | self.assert_(math.isnan(math.frexp(NAN)[0])) |
| 298 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 299 | def testHypot(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 300 | self.assertRaises(TypeError, math.hypot) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 301 | self.ftest('hypot(0,0)', math.hypot(0,0), 0) |
| 302 | self.ftest('hypot(3,4)', math.hypot(3,4), 5) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 303 | self.assertEqual(math.hypot(NAN, INF), INF) |
| 304 | self.assertEqual(math.hypot(INF, NAN), INF) |
| 305 | self.assertEqual(math.hypot(NAN, NINF), INF) |
| 306 | self.assertEqual(math.hypot(NINF, NAN), INF) |
| 307 | self.assert_(math.isnan(math.hypot(1.0, NAN))) |
| 308 | self.assert_(math.isnan(math.hypot(NAN, -2.0))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 309 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 310 | def testLdexp(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 311 | self.assertRaises(TypeError, math.ldexp) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 312 | self.ftest('ldexp(0,1)', math.ldexp(0,1), 0) |
| 313 | self.ftest('ldexp(1,1)', math.ldexp(1,1), 2) |
| 314 | self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5) |
| 315 | self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 316 | self.assertRaises(OverflowError, math.ldexp, 1., 1000000) |
| 317 | self.assertRaises(OverflowError, math.ldexp, -1., 1000000) |
| 318 | self.assertEquals(math.ldexp(1., -1000000), 0.) |
| 319 | self.assertEquals(math.ldexp(-1., -1000000), -0.) |
| 320 | self.assertEquals(math.ldexp(INF, 30), INF) |
| 321 | self.assertEquals(math.ldexp(NINF, -213), NINF) |
| 322 | self.assert_(math.isnan(math.ldexp(NAN, 0))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 323 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 324 | def testLog(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 325 | self.assertRaises(TypeError, math.log) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 326 | self.ftest('log(1/e)', math.log(1/math.e), -1) |
| 327 | self.ftest('log(1)', math.log(1), 0) |
| 328 | self.ftest('log(e)', math.log(math.e), 1) |
| 329 | self.ftest('log(32,2)', math.log(32,2), 5) |
| 330 | self.ftest('log(10**40, 10)', math.log(10**40, 10), 40) |
| 331 | self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 332 | self.assertEquals(math.log(INF), INF) |
| 333 | self.assertRaises(ValueError, math.log, NINF) |
| 334 | self.assert_(math.isnan(math.log(NAN))) |
| 335 | |
| 336 | def testLog1p(self): |
| 337 | self.assertRaises(TypeError, math.log1p) |
| 338 | self.ftest('log1p(1/e -1)', math.log1p(1/math.e-1), -1) |
| 339 | self.ftest('log1p(0)', math.log1p(0), 0) |
| 340 | self.ftest('log1p(e-1)', math.log1p(math.e-1), 1) |
| 341 | self.ftest('log1p(1)', math.log1p(1), math.log(2)) |
| 342 | self.assertEquals(math.log1p(INF), INF) |
| 343 | self.assertRaises(ValueError, math.log1p, NINF) |
| 344 | self.assert_(math.isnan(math.log1p(NAN))) |
| 345 | n= 2**90 |
| 346 | self.assertAlmostEquals(math.log1p(n), 62.383246250395075) |
| 347 | self.assertAlmostEquals(math.log1p(n), math.log1p(float(n))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 348 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 349 | def testLog10(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 350 | self.assertRaises(TypeError, math.log10) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 351 | self.ftest('log10(0.1)', math.log10(0.1), -1) |
| 352 | self.ftest('log10(1)', math.log10(1), 0) |
| 353 | self.ftest('log10(10)', math.log10(10), 1) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 354 | self.assertEquals(math.log(INF), INF) |
| 355 | self.assertRaises(ValueError, math.log10, NINF) |
| 356 | self.assert_(math.isnan(math.log10(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 357 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 358 | def testModf(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 359 | self.assertRaises(TypeError, math.modf) |
| 360 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 361 | def testmodf(name, (v1, v2), (e1, e2)): |
| 362 | if abs(v1-e1) > eps or abs(v2-e2): |
| 363 | self.fail('%s returned %r, expected %r'%\ |
| 364 | (name, (v1,v2), (e1,e2))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 365 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 366 | testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0)) |
| 367 | testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0)) |
Tim Peters | abd8a33 | 2006-11-03 02:32:46 +0000 | [diff] [blame] | 368 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 369 | self.assertEquals(math.modf(INF), (0.0, INF)) |
| 370 | self.assertEquals(math.modf(NINF), (-0.0, NINF)) |
| 371 | |
| 372 | modf_nan = math.modf(NAN) |
| 373 | self.assert_(math.isnan(modf_nan[0])) |
| 374 | self.assert_(math.isnan(modf_nan[1])) |
| 375 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 376 | def testPow(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 377 | self.assertRaises(TypeError, math.pow) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 378 | self.ftest('pow(0,1)', math.pow(0,1), 0) |
| 379 | self.ftest('pow(1,0)', math.pow(1,0), 1) |
| 380 | self.ftest('pow(2,1)', math.pow(2,1), 2) |
| 381 | self.ftest('pow(2,-1)', math.pow(2,-1), 0.5) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 382 | self.assertEqual(math.pow(INF, 1), INF) |
| 383 | self.assertEqual(math.pow(NINF, 1), NINF) |
| 384 | self.assertEqual((math.pow(1, INF)), 1.) |
| 385 | self.assertEqual((math.pow(1, NINF)), 1.) |
| 386 | self.assert_(math.isnan(math.pow(NAN, 1))) |
| 387 | self.assert_(math.isnan(math.pow(2, NAN))) |
| 388 | self.assert_(math.isnan(math.pow(0, NAN))) |
| 389 | self.assertEqual(math.pow(1, NAN), 1) |
Mark Dickinson | e941d97 | 2008-04-19 18:51:48 +0000 | [diff] [blame] | 390 | |
| 391 | # pow(0., x) |
| 392 | self.assertEqual(math.pow(0., INF), 0.) |
| 393 | self.assertEqual(math.pow(0., 3.), 0.) |
| 394 | self.assertEqual(math.pow(0., 2.3), 0.) |
| 395 | self.assertEqual(math.pow(0., 2.), 0.) |
| 396 | self.assertEqual(math.pow(0., 0.), 1.) |
| 397 | self.assertEqual(math.pow(0., -0.), 1.) |
| 398 | self.assertRaises(ValueError, math.pow, 0., -2.) |
| 399 | self.assertRaises(ValueError, math.pow, 0., -2.3) |
| 400 | self.assertRaises(ValueError, math.pow, 0., -3.) |
| 401 | self.assertRaises(ValueError, math.pow, 0., NINF) |
| 402 | self.assert_(math.isnan(math.pow(0., NAN))) |
| 403 | |
| 404 | # pow(INF, x) |
| 405 | self.assertEqual(math.pow(INF, INF), INF) |
| 406 | self.assertEqual(math.pow(INF, 3.), INF) |
| 407 | self.assertEqual(math.pow(INF, 2.3), INF) |
| 408 | self.assertEqual(math.pow(INF, 2.), INF) |
| 409 | self.assertEqual(math.pow(INF, 0.), 1.) |
| 410 | self.assertEqual(math.pow(INF, -0.), 1.) |
| 411 | self.assertEqual(math.pow(INF, -2.), 0.) |
| 412 | self.assertEqual(math.pow(INF, -2.3), 0.) |
| 413 | self.assertEqual(math.pow(INF, -3.), 0.) |
| 414 | self.assertEqual(math.pow(INF, NINF), 0.) |
| 415 | self.assert_(math.isnan(math.pow(INF, NAN))) |
| 416 | |
| 417 | # pow(-0., x) |
| 418 | self.assertEqual(math.pow(-0., INF), 0.) |
| 419 | self.assertEqual(math.pow(-0., 3.), -0.) |
| 420 | self.assertEqual(math.pow(-0., 2.3), 0.) |
| 421 | self.assertEqual(math.pow(-0., 2.), 0.) |
| 422 | self.assertEqual(math.pow(-0., 0.), 1.) |
| 423 | self.assertEqual(math.pow(-0., -0.), 1.) |
| 424 | self.assertRaises(ValueError, math.pow, -0., -2.) |
| 425 | self.assertRaises(ValueError, math.pow, -0., -2.3) |
| 426 | self.assertRaises(ValueError, math.pow, -0., -3.) |
| 427 | self.assertRaises(ValueError, math.pow, -0., NINF) |
| 428 | self.assert_(math.isnan(math.pow(-0., NAN))) |
| 429 | |
| 430 | # pow(NINF, x) |
| 431 | self.assertEqual(math.pow(NINF, INF), INF) |
| 432 | self.assertEqual(math.pow(NINF, 3.), NINF) |
| 433 | self.assertEqual(math.pow(NINF, 2.3), INF) |
| 434 | self.assertEqual(math.pow(NINF, 2.), INF) |
| 435 | self.assertEqual(math.pow(NINF, 0.), 1.) |
| 436 | self.assertEqual(math.pow(NINF, -0.), 1.) |
| 437 | self.assertEqual(math.pow(NINF, -2.), 0.) |
| 438 | self.assertEqual(math.pow(NINF, -2.3), 0.) |
| 439 | self.assertEqual(math.pow(NINF, -3.), -0.) |
| 440 | self.assertEqual(math.pow(NINF, NINF), 0.) |
| 441 | self.assert_(math.isnan(math.pow(NINF, NAN))) |
| 442 | |
| 443 | # pow(-1, x) |
| 444 | self.assertEqual(math.pow(-1., INF), 1.) |
| 445 | self.assertEqual(math.pow(-1., 3.), -1.) |
| 446 | self.assertRaises(ValueError, math.pow, -1., 2.3) |
| 447 | self.assertEqual(math.pow(-1., 2.), 1.) |
| 448 | self.assertEqual(math.pow(-1., 0.), 1.) |
| 449 | self.assertEqual(math.pow(-1., -0.), 1.) |
| 450 | self.assertEqual(math.pow(-1., -2.), 1.) |
| 451 | self.assertRaises(ValueError, math.pow, -1., -2.3) |
| 452 | self.assertEqual(math.pow(-1., -3.), -1.) |
| 453 | self.assertEqual(math.pow(-1., NINF), 1.) |
| 454 | self.assert_(math.isnan(math.pow(-1., NAN))) |
| 455 | |
| 456 | # pow(1, x) |
| 457 | self.assertEqual(math.pow(1., INF), 1.) |
| 458 | self.assertEqual(math.pow(1., 3.), 1.) |
| 459 | self.assertEqual(math.pow(1., 2.3), 1.) |
| 460 | self.assertEqual(math.pow(1., 2.), 1.) |
| 461 | self.assertEqual(math.pow(1., 0.), 1.) |
| 462 | self.assertEqual(math.pow(1., -0.), 1.) |
| 463 | self.assertEqual(math.pow(1., -2.), 1.) |
| 464 | self.assertEqual(math.pow(1., -2.3), 1.) |
| 465 | self.assertEqual(math.pow(1., -3.), 1.) |
| 466 | self.assertEqual(math.pow(1., NINF), 1.) |
| 467 | self.assertEqual(math.pow(1., NAN), 1.) |
| 468 | |
| 469 | # pow(x, 0) should be 1 for any x |
| 470 | self.assertEqual(math.pow(2.3, 0.), 1.) |
| 471 | self.assertEqual(math.pow(-2.3, 0.), 1.) |
| 472 | self.assertEqual(math.pow(NAN, 0.), 1.) |
| 473 | self.assertEqual(math.pow(2.3, -0.), 1.) |
| 474 | self.assertEqual(math.pow(-2.3, -0.), 1.) |
| 475 | self.assertEqual(math.pow(NAN, -0.), 1.) |
| 476 | |
| 477 | # pow(x, y) is invalid if x is negative and y is not integral |
| 478 | self.assertRaises(ValueError, math.pow, -1., 2.3) |
| 479 | self.assertRaises(ValueError, math.pow, -15., -3.1) |
| 480 | |
| 481 | # pow(x, NINF) |
| 482 | self.assertEqual(math.pow(1.9, NINF), 0.) |
| 483 | self.assertEqual(math.pow(1.1, NINF), 0.) |
| 484 | self.assertEqual(math.pow(0.9, NINF), INF) |
| 485 | self.assertEqual(math.pow(0.1, NINF), INF) |
| 486 | self.assertEqual(math.pow(-0.1, NINF), INF) |
| 487 | self.assertEqual(math.pow(-0.9, NINF), INF) |
| 488 | self.assertEqual(math.pow(-1.1, NINF), 0.) |
| 489 | self.assertEqual(math.pow(-1.9, NINF), 0.) |
| 490 | |
| 491 | # pow(x, INF) |
| 492 | self.assertEqual(math.pow(1.9, INF), INF) |
| 493 | self.assertEqual(math.pow(1.1, INF), INF) |
| 494 | self.assertEqual(math.pow(0.9, INF), 0.) |
| 495 | self.assertEqual(math.pow(0.1, INF), 0.) |
| 496 | self.assertEqual(math.pow(-0.1, INF), 0.) |
| 497 | self.assertEqual(math.pow(-0.9, INF), 0.) |
| 498 | self.assertEqual(math.pow(-1.1, INF), INF) |
| 499 | self.assertEqual(math.pow(-1.9, INF), INF) |
| 500 | |
| 501 | # the following tests have been commented out since they don't |
| 502 | # really belong here: the implementation of ** for floats is |
| 503 | # independent of the implemention of math.pow |
| 504 | #self.assertEqual(1**NAN, 1) |
| 505 | #self.assertEqual(1**INF, 1) |
| 506 | #self.assertEqual(1**NINF, 1) |
| 507 | #self.assertEqual(1**0, 1) |
| 508 | #self.assertEqual(1.**NAN, 1) |
| 509 | #self.assertEqual(1.**INF, 1) |
| 510 | #self.assertEqual(1.**NINF, 1) |
| 511 | #self.assertEqual(1.**0, 1) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 512 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 513 | def testRadians(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 514 | self.assertRaises(TypeError, math.radians) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 515 | self.ftest('radians(180)', math.radians(180), math.pi) |
| 516 | self.ftest('radians(90)', math.radians(90), math.pi/2) |
| 517 | self.ftest('radians(-45)', math.radians(-45), -math.pi/4) |
Raymond Hettinger | 64108af | 2002-05-13 03:55:01 +0000 | [diff] [blame] | 518 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 519 | def testSin(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 520 | self.assertRaises(TypeError, math.sin) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 521 | self.ftest('sin(0)', math.sin(0), 0) |
| 522 | self.ftest('sin(pi/2)', math.sin(math.pi/2), 1) |
| 523 | self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 524 | try: |
| 525 | self.assert_(math.isnan(math.sin(INF))) |
| 526 | self.assert_(math.isnan(math.sin(NINF))) |
| 527 | except ValueError: |
| 528 | self.assertRaises(ValueError, math.sin, INF) |
| 529 | self.assertRaises(ValueError, math.sin, NINF) |
| 530 | self.assert_(math.isnan(math.sin(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 531 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 532 | def testSinh(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 533 | self.assertRaises(TypeError, math.sinh) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 534 | self.ftest('sinh(0)', math.sinh(0), 0) |
| 535 | self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1) |
| 536 | self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 537 | self.assertEquals(math.sinh(INF), INF) |
Mark Dickinson | e941d97 | 2008-04-19 18:51:48 +0000 | [diff] [blame] | 538 | self.assertEquals(math.sinh(NINF), NINF) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 539 | self.assert_(math.isnan(math.sinh(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 540 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 541 | def testSqrt(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 542 | self.assertRaises(TypeError, math.sqrt) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 543 | self.ftest('sqrt(0)', math.sqrt(0), 0) |
| 544 | self.ftest('sqrt(1)', math.sqrt(1), 1) |
| 545 | self.ftest('sqrt(4)', math.sqrt(4), 2) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 546 | self.assertEquals(math.sqrt(INF), INF) |
| 547 | self.assertRaises(ValueError, math.sqrt, NINF) |
| 548 | self.assert_(math.isnan(math.sqrt(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 549 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 550 | def testTan(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 551 | self.assertRaises(TypeError, math.tan) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 552 | self.ftest('tan(0)', math.tan(0), 0) |
| 553 | self.ftest('tan(pi/4)', math.tan(math.pi/4), 1) |
| 554 | self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 555 | try: |
| 556 | self.assert_(math.isnan(math.tan(INF))) |
| 557 | self.assert_(math.isnan(math.tan(NINF))) |
| 558 | except: |
| 559 | self.assertRaises(ValueError, math.tan, INF) |
| 560 | self.assertRaises(ValueError, math.tan, NINF) |
| 561 | self.assert_(math.isnan(math.tan(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 562 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 563 | def testTanh(self): |
Walter Dörwald | 92911bf | 2006-10-29 22:06:28 +0000 | [diff] [blame] | 564 | self.assertRaises(TypeError, math.tanh) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 565 | self.ftest('tanh(0)', math.tanh(0), 0) |
| 566 | self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 567 | self.ftest('tanh(inf)', math.tanh(INF), 1) |
| 568 | self.ftest('tanh(-inf)', math.tanh(NINF), -1) |
| 569 | self.assert_(math.isnan(math.tanh(NAN))) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 570 | |
Jeffrey Yasskin | ca2b69f | 2008-02-01 06:22:46 +0000 | [diff] [blame] | 571 | def test_trunc(self): |
| 572 | self.assertEqual(math.trunc(1), 1) |
| 573 | self.assertEqual(math.trunc(-1), -1) |
| 574 | self.assertEqual(type(math.trunc(1)), int) |
| 575 | self.assertEqual(type(math.trunc(1.5)), int) |
| 576 | self.assertEqual(math.trunc(1.5), 1) |
| 577 | self.assertEqual(math.trunc(-1.5), -1) |
| 578 | self.assertEqual(math.trunc(1.999999), 1) |
| 579 | self.assertEqual(math.trunc(-1.999999), -1) |
| 580 | self.assertEqual(math.trunc(-0.999999), -0) |
| 581 | self.assertEqual(math.trunc(-100.999), -100) |
| 582 | |
| 583 | class TestTrunc(object): |
| 584 | def __trunc__(self): |
| 585 | return 23 |
| 586 | |
| 587 | class TestNoTrunc(object): |
| 588 | pass |
| 589 | |
| 590 | self.assertEqual(math.trunc(TestTrunc()), 23) |
| 591 | |
| 592 | self.assertRaises(TypeError, math.trunc) |
| 593 | self.assertRaises(TypeError, math.trunc, 1, 2) |
| 594 | # XXX: This is not ideal, but see the comment in math_trunc(). |
| 595 | self.assertRaises(AttributeError, math.trunc, TestNoTrunc()) |
| 596 | |
| 597 | t = TestNoTrunc() |
| 598 | t.__trunc__ = lambda *args: args |
| 599 | self.assertEquals((), math.trunc(t)) |
| 600 | self.assertRaises(TypeError, math.trunc, t, 0) |
| 601 | |
Christian Heimes | eebb79c | 2008-01-03 22:32:26 +0000 | [diff] [blame] | 602 | def testCopysign(self): |
| 603 | self.assertEqual(math.copysign(1, 42), 1.0) |
| 604 | self.assertEqual(math.copysign(0., 42), 0.0) |
| 605 | self.assertEqual(math.copysign(1., -42), -1.0) |
| 606 | self.assertEqual(math.copysign(3, 0.), 3.0) |
| 607 | self.assertEqual(math.copysign(4., -0.), -4.0) |
| 608 | |
Christian Heimes | e2ca424 | 2008-01-03 20:23:15 +0000 | [diff] [blame] | 609 | def testIsnan(self): |
| 610 | self.assert_(math.isnan(float("nan"))) |
| 611 | self.assert_(math.isnan(float("inf")* 0.)) |
| 612 | self.failIf(math.isnan(float("inf"))) |
| 613 | self.failIf(math.isnan(0.)) |
| 614 | self.failIf(math.isnan(1.)) |
| 615 | |
| 616 | def testIsinf(self): |
| 617 | self.assert_(math.isinf(float("inf"))) |
| 618 | self.assert_(math.isinf(float("-inf"))) |
| 619 | self.assert_(math.isinf(1E400)) |
| 620 | self.assert_(math.isinf(-1E400)) |
| 621 | self.failIf(math.isinf(float("nan"))) |
| 622 | self.failIf(math.isinf(0.)) |
| 623 | self.failIf(math.isinf(1.)) |
| 624 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 625 | # RED_FLAG 16-Oct-2000 Tim |
| 626 | # While 2.0 is more consistent about exceptions than previous releases, it |
| 627 | # still fails this part of the test on some platforms. For now, we only |
| 628 | # *run* test_exceptions() in verbose mode, so that this isn't normally |
| 629 | # tested. |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 630 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 631 | if verbose: |
| 632 | def test_exceptions(self): |
| 633 | try: |
| 634 | x = math.exp(-1000000000) |
| 635 | except: |
| 636 | # mathmodule.c is failing to weed out underflows from libm, or |
| 637 | # we've got an fp format with huge dynamic range |
| 638 | self.fail("underflowing exp() should not have raised " |
| 639 | "an exception") |
| 640 | if x != 0: |
| 641 | self.fail("underflowing exp() should have returned 0") |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 642 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 643 | # If this fails, probably using a strict IEEE-754 conforming libm, and x |
| 644 | # is +Inf afterwards. But Python wants overflows detected by default. |
| 645 | try: |
| 646 | x = math.exp(1000000000) |
| 647 | except OverflowError: |
| 648 | pass |
| 649 | else: |
| 650 | self.fail("overflowing exp() didn't trigger OverflowError") |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 651 | |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 652 | # If this fails, it could be a puzzle. One odd possibility is that |
| 653 | # mathmodule.c's macros are getting confused while comparing |
| 654 | # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE |
| 655 | # as a result (and so raising OverflowError instead). |
| 656 | try: |
| 657 | x = math.sqrt(-1.0) |
| 658 | except ValueError: |
| 659 | pass |
| 660 | else: |
| 661 | self.fail("sqrt(-1) didn't raise ValueError") |
Tim Peters | 98c8184 | 2000-10-16 17:35:13 +0000 | [diff] [blame] | 662 | |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 663 | def test_testfile(self): |
| 664 | if not float.__getformat__("double").startswith("IEEE"): |
| 665 | return |
| 666 | for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file): |
| 667 | # Skip if either the input or result is complex, or if |
| 668 | # flags is nonempty |
| 669 | if ai != 0. or ei != 0. or flags: |
| 670 | continue |
| 671 | if fn in ['rect', 'polar']: |
| 672 | # no real versions of rect, polar |
| 673 | continue |
| 674 | func = getattr(math, fn) |
Mark Dickinson | 9f99d70 | 2008-04-20 01:22:30 +0000 | [diff] [blame] | 675 | try: |
| 676 | result = func(ar) |
| 677 | except ValueError: |
| 678 | message = ("Unexpected ValueError in " + |
| 679 | "test %s:%s(%r)\n" % (id, fn, ar)) |
| 680 | self.fail(message) |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 681 | self.ftest("%s:%s(%r)" % (id, fn, ar), result, er) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 682 | |
| 683 | def test_main(): |
Christian Heimes | 6f34109 | 2008-04-18 23:13:07 +0000 | [diff] [blame] | 684 | from doctest import DocFileSuite |
| 685 | suite = unittest.TestSuite() |
| 686 | suite.addTest(unittest.makeSuite(MathTests)) |
| 687 | suite.addTest(DocFileSuite("ieee754.txt")) |
| 688 | run_unittest(suite) |
Georg Brandl | 2f03760 | 2006-10-28 13:51:49 +0000 | [diff] [blame] | 689 | |
| 690 | if __name__ == '__main__': |
| 691 | test_main() |