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 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test.support import run_unittest, verbose |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5 | import unittest |
| 6 | import math |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +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 | 53876d9 | 2008-04-19 00:31:39 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 47 | class MathTests(unittest.TestCase): |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 48 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 49 | def ftest(self, name, value, expected): |
| 50 | if abs(value-expected) > eps: |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +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' % |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 56 | (name, value, expected)) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 57 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 62 | def testAcos(self): |
| 63 | self.assertRaises(TypeError, math.acos) |
| 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 | 53876d9 | 2008-04-19 00:31:39 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 81 | def testAsin(self): |
| 82 | self.assertRaises(TypeError, math.asin) |
| 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 | 53876d9 | 2008-04-19 00:31:39 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 99 | def testAtan(self): |
| 100 | self.assertRaises(TypeError, math.atan) |
| 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 | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 104 | self.ftest('atan(inf)', math.atan(INF), math.pi/2) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 105 | self.ftest('atan(-inf)', math.atan(NINF), -math.pi/2) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 119 | def testAtan2(self): |
| 120 | self.assertRaises(TypeError, math.atan2) |
| 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 | |
Christian Heimes | e57950f | 2008-04-21 13:08:03 +0000 | [diff] [blame] | 127 | # math.atan2(0, x) |
| 128 | self.ftest('atan2(0., -inf)', math.atan2(0., NINF), math.pi) |
| 129 | self.ftest('atan2(0., -2.3)', math.atan2(0., -2.3), math.pi) |
| 130 | self.ftest('atan2(0., -0.)', math.atan2(0., -0.), math.pi) |
| 131 | self.assertEqual(math.atan2(0., 0.), 0.) |
| 132 | self.assertEqual(math.atan2(0., 2.3), 0.) |
| 133 | self.assertEqual(math.atan2(0., INF), 0.) |
| 134 | self.assert_(math.isnan(math.atan2(0., NAN))) |
| 135 | # math.atan2(-0, x) |
| 136 | self.ftest('atan2(-0., -inf)', math.atan2(-0., NINF), -math.pi) |
| 137 | self.ftest('atan2(-0., -2.3)', math.atan2(-0., -2.3), -math.pi) |
| 138 | self.ftest('atan2(-0., -0.)', math.atan2(-0., -0.), -math.pi) |
| 139 | self.assertEqual(math.atan2(-0., 0.), -0.) |
| 140 | self.assertEqual(math.atan2(-0., 2.3), -0.) |
| 141 | self.assertEqual(math.atan2(-0., INF), -0.) |
| 142 | self.assert_(math.isnan(math.atan2(-0., NAN))) |
| 143 | # math.atan2(INF, x) |
| 144 | self.ftest('atan2(inf, -inf)', math.atan2(INF, NINF), math.pi*3/4) |
| 145 | self.ftest('atan2(inf, -2.3)', math.atan2(INF, -2.3), math.pi/2) |
| 146 | self.ftest('atan2(inf, -0.)', math.atan2(INF, -0.0), math.pi/2) |
| 147 | self.ftest('atan2(inf, 0.)', math.atan2(INF, 0.0), math.pi/2) |
| 148 | self.ftest('atan2(inf, 2.3)', math.atan2(INF, 2.3), math.pi/2) |
| 149 | self.ftest('atan2(inf, inf)', math.atan2(INF, INF), math.pi/4) |
| 150 | self.assert_(math.isnan(math.atan2(INF, NAN))) |
| 151 | # math.atan2(NINF, x) |
| 152 | self.ftest('atan2(-inf, -inf)', math.atan2(NINF, NINF), -math.pi*3/4) |
| 153 | self.ftest('atan2(-inf, -2.3)', math.atan2(NINF, -2.3), -math.pi/2) |
| 154 | self.ftest('atan2(-inf, -0.)', math.atan2(NINF, -0.0), -math.pi/2) |
| 155 | self.ftest('atan2(-inf, 0.)', math.atan2(NINF, 0.0), -math.pi/2) |
| 156 | self.ftest('atan2(-inf, 2.3)', math.atan2(NINF, 2.3), -math.pi/2) |
| 157 | self.ftest('atan2(-inf, inf)', math.atan2(NINF, INF), -math.pi/4) |
| 158 | self.assert_(math.isnan(math.atan2(NINF, NAN))) |
| 159 | # math.atan2(+finite, x) |
| 160 | self.ftest('atan2(2.3, -inf)', math.atan2(2.3, NINF), math.pi) |
| 161 | self.ftest('atan2(2.3, -0.)', math.atan2(2.3, -0.), math.pi/2) |
| 162 | self.ftest('atan2(2.3, 0.)', math.atan2(2.3, 0.), math.pi/2) |
| 163 | self.assertEqual(math.atan2(2.3, INF), 0.) |
| 164 | self.assert_(math.isnan(math.atan2(2.3, NAN))) |
| 165 | # math.atan2(-finite, x) |
| 166 | self.ftest('atan2(-2.3, -inf)', math.atan2(-2.3, NINF), -math.pi) |
| 167 | self.ftest('atan2(-2.3, -0.)', math.atan2(-2.3, -0.), -math.pi/2) |
| 168 | self.ftest('atan2(-2.3, 0.)', math.atan2(-2.3, 0.), -math.pi/2) |
| 169 | self.assertEqual(math.atan2(-2.3, INF), -0.) |
| 170 | self.assert_(math.isnan(math.atan2(-2.3, NAN))) |
| 171 | # math.atan2(NAN, x) |
| 172 | self.assert_(math.isnan(math.atan2(NAN, NINF))) |
| 173 | self.assert_(math.isnan(math.atan2(NAN, -2.3))) |
| 174 | self.assert_(math.isnan(math.atan2(NAN, -0.))) |
| 175 | self.assert_(math.isnan(math.atan2(NAN, 0.))) |
| 176 | self.assert_(math.isnan(math.atan2(NAN, 2.3))) |
| 177 | self.assert_(math.isnan(math.atan2(NAN, INF))) |
| 178 | self.assert_(math.isnan(math.atan2(NAN, NAN))) |
| 179 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 180 | def testCeil(self): |
| 181 | self.assertRaises(TypeError, math.ceil) |
Jeffrey Yasskin | c215583 | 2008-01-05 20:03:11 +0000 | [diff] [blame] | 182 | self.assertEquals(int, type(math.ceil(0.5))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 183 | self.ftest('ceil(0.5)', math.ceil(0.5), 1) |
| 184 | self.ftest('ceil(1.0)', math.ceil(1.0), 1) |
| 185 | self.ftest('ceil(1.5)', math.ceil(1.5), 2) |
| 186 | self.ftest('ceil(-0.5)', math.ceil(-0.5), 0) |
| 187 | self.ftest('ceil(-1.0)', math.ceil(-1.0), -1) |
| 188 | self.ftest('ceil(-1.5)', math.ceil(-1.5), -1) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 189 | #self.assertEquals(math.ceil(INF), INF) |
| 190 | #self.assertEquals(math.ceil(NINF), NINF) |
| 191 | #self.assert_(math.isnan(math.ceil(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 192 | |
Guido van Rossum | 13e05de | 2007-08-23 22:56:55 +0000 | [diff] [blame] | 193 | class TestCeil: |
| 194 | def __ceil__(self): |
| 195 | return 42 |
| 196 | class TestNoCeil: |
| 197 | pass |
| 198 | self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42) |
| 199 | self.assertRaises(TypeError, math.ceil, TestNoCeil()) |
| 200 | |
| 201 | t = TestNoCeil() |
| 202 | t.__ceil__ = lambda *args: args |
| 203 | self.assertRaises(TypeError, math.ceil, t) |
| 204 | self.assertRaises(TypeError, math.ceil, t, 0) |
| 205 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 206 | if float.__getformat__("double").startswith("IEEE"): |
| 207 | def testCopysign(self): |
| 208 | self.assertRaises(TypeError, math.copysign) |
| 209 | # copysign should let us distinguish signs of zeros |
| 210 | self.assertEquals(copysign(1., 0.), 1.) |
| 211 | self.assertEquals(copysign(1., -0.), -1.) |
| 212 | self.assertEquals(copysign(INF, 0.), INF) |
| 213 | self.assertEquals(copysign(INF, -0.), NINF) |
| 214 | self.assertEquals(copysign(NINF, 0.), INF) |
| 215 | self.assertEquals(copysign(NINF, -0.), NINF) |
| 216 | # and of infinities |
| 217 | self.assertEquals(copysign(1., INF), 1.) |
| 218 | self.assertEquals(copysign(1., NINF), -1.) |
| 219 | self.assertEquals(copysign(INF, INF), INF) |
| 220 | self.assertEquals(copysign(INF, NINF), NINF) |
| 221 | self.assertEquals(copysign(NINF, INF), INF) |
| 222 | self.assertEquals(copysign(NINF, NINF), NINF) |
| 223 | self.assert_(math.isnan(copysign(NAN, 1.))) |
| 224 | self.assert_(math.isnan(copysign(NAN, INF))) |
| 225 | self.assert_(math.isnan(copysign(NAN, NINF))) |
| 226 | self.assert_(math.isnan(copysign(NAN, NAN))) |
| 227 | # copysign(INF, NAN) may be INF or it may be NINF, since |
| 228 | # we don't know whether the sign bit of NAN is set on any |
| 229 | # given platform. |
| 230 | self.assert_(math.isinf(copysign(INF, NAN))) |
| 231 | # similarly, copysign(2., NAN) could be 2. or -2. |
| 232 | self.assertEquals(abs(copysign(2., NAN)), 2.) |
| 233 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 234 | def testCos(self): |
| 235 | self.assertRaises(TypeError, math.cos) |
| 236 | self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0) |
| 237 | self.ftest('cos(0)', math.cos(0), 1) |
| 238 | self.ftest('cos(pi/2)', math.cos(math.pi/2), 0) |
| 239 | self.ftest('cos(pi)', math.cos(math.pi), -1) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 240 | try: |
| 241 | self.assert_(math.isnan(math.cos(INF))) |
| 242 | self.assert_(math.isnan(math.cos(NINF))) |
| 243 | except ValueError: |
| 244 | self.assertRaises(ValueError, math.cos, INF) |
| 245 | self.assertRaises(ValueError, math.cos, NINF) |
| 246 | self.assert_(math.isnan(math.cos(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 247 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 248 | def testCosh(self): |
| 249 | self.assertRaises(TypeError, math.cosh) |
| 250 | self.ftest('cosh(0)', math.cosh(0), 1) |
| 251 | self.ftest('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 252 | self.assertEquals(math.cosh(INF), INF) |
| 253 | self.assertEquals(math.cosh(NINF), INF) |
| 254 | self.assert_(math.isnan(math.cosh(NAN))) |
Raymond Hettinger | 64108af | 2002-05-13 03:55:01 +0000 | [diff] [blame] | 255 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 256 | def testDegrees(self): |
| 257 | self.assertRaises(TypeError, math.degrees) |
| 258 | self.ftest('degrees(pi)', math.degrees(math.pi), 180.0) |
| 259 | self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0) |
| 260 | 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] | 261 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 262 | def testExp(self): |
| 263 | self.assertRaises(TypeError, math.exp) |
| 264 | self.ftest('exp(-1)', math.exp(-1), 1/math.e) |
| 265 | self.ftest('exp(0)', math.exp(0), 1) |
| 266 | self.ftest('exp(1)', math.exp(1), math.e) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 267 | self.assertEquals(math.exp(INF), INF) |
| 268 | self.assertEquals(math.exp(NINF), 0.) |
| 269 | self.assert_(math.isnan(math.exp(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 270 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 271 | def testFabs(self): |
| 272 | self.assertRaises(TypeError, math.fabs) |
| 273 | self.ftest('fabs(-1)', math.fabs(-1), 1) |
| 274 | self.ftest('fabs(0)', math.fabs(0), 0) |
| 275 | self.ftest('fabs(1)', math.fabs(1), 1) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 276 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 277 | def testFloor(self): |
| 278 | self.assertRaises(TypeError, math.floor) |
Jeffrey Yasskin | c215583 | 2008-01-05 20:03:11 +0000 | [diff] [blame] | 279 | self.assertEquals(int, type(math.floor(0.5))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 280 | self.ftest('floor(0.5)', math.floor(0.5), 0) |
| 281 | self.ftest('floor(1.0)', math.floor(1.0), 1) |
| 282 | self.ftest('floor(1.5)', math.floor(1.5), 1) |
| 283 | self.ftest('floor(-0.5)', math.floor(-0.5), -1) |
| 284 | self.ftest('floor(-1.0)', math.floor(-1.0), -1) |
| 285 | self.ftest('floor(-1.5)', math.floor(-1.5), -2) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 286 | # pow() relies on floor() to check for integers |
| 287 | # This fails on some platforms - so check it here |
| 288 | self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167) |
| 289 | self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 290 | #self.assertEquals(math.ceil(INF), INF) |
| 291 | #self.assertEquals(math.ceil(NINF), NINF) |
| 292 | #self.assert_(math.isnan(math.floor(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 293 | |
Guido van Rossum | 13e05de | 2007-08-23 22:56:55 +0000 | [diff] [blame] | 294 | class TestFloor: |
| 295 | def __floor__(self): |
| 296 | return 42 |
| 297 | class TestNoFloor: |
| 298 | pass |
| 299 | self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42) |
| 300 | self.assertRaises(TypeError, math.floor, TestNoFloor()) |
| 301 | |
| 302 | t = TestNoFloor() |
| 303 | t.__floor__ = lambda *args: args |
| 304 | self.assertRaises(TypeError, math.floor, t) |
| 305 | self.assertRaises(TypeError, math.floor, t, 0) |
| 306 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 307 | def testFmod(self): |
| 308 | self.assertRaises(TypeError, math.fmod) |
| 309 | self.ftest('fmod(10,1)', math.fmod(10,1), 0) |
| 310 | self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0) |
| 311 | self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1) |
| 312 | self.ftest('fmod(-10,1)', math.fmod(-10,1), 0) |
| 313 | self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0) |
| 314 | self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 315 | self.assert_(math.isnan(math.fmod(NAN, 1.))) |
| 316 | self.assert_(math.isnan(math.fmod(1., NAN))) |
| 317 | self.assert_(math.isnan(math.fmod(NAN, NAN))) |
| 318 | self.assertRaises(ValueError, math.fmod, 1., 0.) |
| 319 | self.assertRaises(ValueError, math.fmod, INF, 1.) |
| 320 | self.assertRaises(ValueError, math.fmod, NINF, 1.) |
| 321 | self.assertRaises(ValueError, math.fmod, INF, 0.) |
| 322 | self.assertEquals(math.fmod(3.0, INF), 3.0) |
| 323 | self.assertEquals(math.fmod(-3.0, INF), -3.0) |
| 324 | self.assertEquals(math.fmod(3.0, NINF), 3.0) |
| 325 | self.assertEquals(math.fmod(-3.0, NINF), -3.0) |
| 326 | self.assertEquals(math.fmod(0.0, 3.0), 0.0) |
| 327 | self.assertEquals(math.fmod(0.0, NINF), 0.0) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 328 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 329 | def testFrexp(self): |
| 330 | self.assertRaises(TypeError, math.frexp) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 331 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 332 | def testfrexp(name, result, expected): |
| 333 | (mant, exp), (emant, eexp) = result, expected |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 334 | if abs(mant-emant) > eps or exp != eexp: |
| 335 | self.fail('%s returned %r, expected %r'%\ |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 336 | (name, result, expected)) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 337 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 338 | testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1)) |
| 339 | testfrexp('frexp(0)', math.frexp(0), (0, 0)) |
| 340 | testfrexp('frexp(1)', math.frexp(1), (0.5, 1)) |
| 341 | testfrexp('frexp(2)', math.frexp(2), (0.5, 2)) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 342 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 343 | self.assertEquals(math.frexp(INF)[0], INF) |
| 344 | self.assertEquals(math.frexp(NINF)[0], NINF) |
| 345 | self.assert_(math.isnan(math.frexp(NAN)[0])) |
| 346 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 347 | def testHypot(self): |
| 348 | self.assertRaises(TypeError, math.hypot) |
| 349 | self.ftest('hypot(0,0)', math.hypot(0,0), 0) |
| 350 | self.ftest('hypot(3,4)', math.hypot(3,4), 5) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 351 | self.assertEqual(math.hypot(NAN, INF), INF) |
| 352 | self.assertEqual(math.hypot(INF, NAN), INF) |
| 353 | self.assertEqual(math.hypot(NAN, NINF), INF) |
| 354 | self.assertEqual(math.hypot(NINF, NAN), INF) |
| 355 | self.assert_(math.isnan(math.hypot(1.0, NAN))) |
| 356 | self.assert_(math.isnan(math.hypot(NAN, -2.0))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 357 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 358 | def testLdexp(self): |
| 359 | self.assertRaises(TypeError, math.ldexp) |
| 360 | self.ftest('ldexp(0,1)', math.ldexp(0,1), 0) |
| 361 | self.ftest('ldexp(1,1)', math.ldexp(1,1), 2) |
| 362 | self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5) |
| 363 | self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 364 | self.assertRaises(OverflowError, math.ldexp, 1., 1000000) |
| 365 | self.assertRaises(OverflowError, math.ldexp, -1., 1000000) |
| 366 | self.assertEquals(math.ldexp(1., -1000000), 0.) |
| 367 | self.assertEquals(math.ldexp(-1., -1000000), -0.) |
| 368 | self.assertEquals(math.ldexp(INF, 30), INF) |
| 369 | self.assertEquals(math.ldexp(NINF, -213), NINF) |
| 370 | self.assert_(math.isnan(math.ldexp(NAN, 0))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 371 | |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 372 | # large second argument |
| 373 | for n in [10**5, 10**10, 10**20, 10**40]: |
| 374 | self.assertEquals(math.ldexp(INF, -n), INF) |
| 375 | self.assertEquals(math.ldexp(NINF, -n), NINF) |
| 376 | self.assertEquals(math.ldexp(1., -n), 0.) |
| 377 | self.assertEquals(math.ldexp(-1., -n), -0.) |
| 378 | self.assertEquals(math.ldexp(0., -n), 0.) |
| 379 | self.assertEquals(math.ldexp(-0., -n), -0.) |
| 380 | self.assert_(math.isnan(math.ldexp(NAN, -n))) |
| 381 | |
| 382 | self.assertRaises(OverflowError, math.ldexp, 1., n) |
| 383 | self.assertRaises(OverflowError, math.ldexp, -1., n) |
| 384 | self.assertEquals(math.ldexp(0., n), 0.) |
| 385 | self.assertEquals(math.ldexp(-0., n), -0.) |
| 386 | self.assertEquals(math.ldexp(INF, n), INF) |
| 387 | self.assertEquals(math.ldexp(NINF, n), NINF) |
| 388 | self.assert_(math.isnan(math.ldexp(NAN, n))) |
| 389 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 390 | def testLog(self): |
| 391 | self.assertRaises(TypeError, math.log) |
| 392 | self.ftest('log(1/e)', math.log(1/math.e), -1) |
| 393 | self.ftest('log(1)', math.log(1), 0) |
| 394 | self.ftest('log(e)', math.log(math.e), 1) |
| 395 | self.ftest('log(32,2)', math.log(32,2), 5) |
| 396 | self.ftest('log(10**40, 10)', math.log(10**40, 10), 40) |
| 397 | self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 398 | self.assertEquals(math.log(INF), INF) |
| 399 | self.assertRaises(ValueError, math.log, NINF) |
| 400 | self.assert_(math.isnan(math.log(NAN))) |
| 401 | |
| 402 | def testLog1p(self): |
| 403 | self.assertRaises(TypeError, math.log1p) |
| 404 | self.ftest('log1p(1/e -1)', math.log1p(1/math.e-1), -1) |
| 405 | self.ftest('log1p(0)', math.log1p(0), 0) |
| 406 | self.ftest('log1p(e-1)', math.log1p(math.e-1), 1) |
| 407 | self.ftest('log1p(1)', math.log1p(1), math.log(2)) |
| 408 | self.assertEquals(math.log1p(INF), INF) |
| 409 | self.assertRaises(ValueError, math.log1p, NINF) |
| 410 | self.assert_(math.isnan(math.log1p(NAN))) |
| 411 | n= 2**90 |
| 412 | self.assertAlmostEquals(math.log1p(n), 62.383246250395075) |
| 413 | self.assertAlmostEquals(math.log1p(n), math.log1p(float(n))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 414 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 415 | def testLog10(self): |
| 416 | self.assertRaises(TypeError, math.log10) |
| 417 | self.ftest('log10(0.1)', math.log10(0.1), -1) |
| 418 | self.ftest('log10(1)', math.log10(1), 0) |
| 419 | self.ftest('log10(10)', math.log10(10), 1) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 420 | self.assertEquals(math.log(INF), INF) |
| 421 | self.assertRaises(ValueError, math.log10, NINF) |
| 422 | self.assert_(math.isnan(math.log10(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 423 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 424 | def testModf(self): |
| 425 | self.assertRaises(TypeError, math.modf) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 426 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 427 | def testmodf(name, result, expected): |
| 428 | (v1, v2), (e1, e2) = result, expected |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 429 | if abs(v1-e1) > eps or abs(v2-e2): |
| 430 | self.fail('%s returned %r, expected %r'%\ |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 431 | (name, result, expected)) |
Raymond Hettinger | 64108af | 2002-05-13 03:55:01 +0000 | [diff] [blame] | 432 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 433 | testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0)) |
| 434 | testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0)) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 435 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 436 | self.assertEquals(math.modf(INF), (0.0, INF)) |
| 437 | self.assertEquals(math.modf(NINF), (-0.0, NINF)) |
| 438 | |
| 439 | modf_nan = math.modf(NAN) |
| 440 | self.assert_(math.isnan(modf_nan[0])) |
| 441 | self.assert_(math.isnan(modf_nan[1])) |
| 442 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 443 | def testPow(self): |
| 444 | self.assertRaises(TypeError, math.pow) |
| 445 | self.ftest('pow(0,1)', math.pow(0,1), 0) |
| 446 | self.ftest('pow(1,0)', math.pow(1,0), 1) |
| 447 | self.ftest('pow(2,1)', math.pow(2,1), 2) |
| 448 | self.ftest('pow(2,-1)', math.pow(2,-1), 0.5) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 449 | self.assertEqual(math.pow(INF, 1), INF) |
| 450 | self.assertEqual(math.pow(NINF, 1), NINF) |
| 451 | self.assertEqual((math.pow(1, INF)), 1.) |
| 452 | self.assertEqual((math.pow(1, NINF)), 1.) |
| 453 | self.assert_(math.isnan(math.pow(NAN, 1))) |
| 454 | self.assert_(math.isnan(math.pow(2, NAN))) |
| 455 | self.assert_(math.isnan(math.pow(0, NAN))) |
| 456 | self.assertEqual(math.pow(1, NAN), 1) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 457 | |
| 458 | # pow(0., x) |
| 459 | self.assertEqual(math.pow(0., INF), 0.) |
| 460 | self.assertEqual(math.pow(0., 3.), 0.) |
| 461 | self.assertEqual(math.pow(0., 2.3), 0.) |
| 462 | self.assertEqual(math.pow(0., 2.), 0.) |
| 463 | self.assertEqual(math.pow(0., 0.), 1.) |
| 464 | self.assertEqual(math.pow(0., -0.), 1.) |
| 465 | self.assertRaises(ValueError, math.pow, 0., -2.) |
| 466 | self.assertRaises(ValueError, math.pow, 0., -2.3) |
| 467 | self.assertRaises(ValueError, math.pow, 0., -3.) |
| 468 | self.assertRaises(ValueError, math.pow, 0., NINF) |
| 469 | self.assert_(math.isnan(math.pow(0., NAN))) |
| 470 | |
| 471 | # pow(INF, x) |
| 472 | self.assertEqual(math.pow(INF, INF), INF) |
| 473 | self.assertEqual(math.pow(INF, 3.), INF) |
| 474 | self.assertEqual(math.pow(INF, 2.3), INF) |
| 475 | self.assertEqual(math.pow(INF, 2.), INF) |
| 476 | self.assertEqual(math.pow(INF, 0.), 1.) |
| 477 | self.assertEqual(math.pow(INF, -0.), 1.) |
| 478 | self.assertEqual(math.pow(INF, -2.), 0.) |
| 479 | self.assertEqual(math.pow(INF, -2.3), 0.) |
| 480 | self.assertEqual(math.pow(INF, -3.), 0.) |
| 481 | self.assertEqual(math.pow(INF, NINF), 0.) |
| 482 | self.assert_(math.isnan(math.pow(INF, NAN))) |
| 483 | |
| 484 | # pow(-0., x) |
| 485 | self.assertEqual(math.pow(-0., INF), 0.) |
| 486 | self.assertEqual(math.pow(-0., 3.), -0.) |
| 487 | self.assertEqual(math.pow(-0., 2.3), 0.) |
| 488 | self.assertEqual(math.pow(-0., 2.), 0.) |
| 489 | self.assertEqual(math.pow(-0., 0.), 1.) |
| 490 | self.assertEqual(math.pow(-0., -0.), 1.) |
| 491 | self.assertRaises(ValueError, math.pow, -0., -2.) |
| 492 | self.assertRaises(ValueError, math.pow, -0., -2.3) |
| 493 | self.assertRaises(ValueError, math.pow, -0., -3.) |
| 494 | self.assertRaises(ValueError, math.pow, -0., NINF) |
| 495 | self.assert_(math.isnan(math.pow(-0., NAN))) |
| 496 | |
| 497 | # pow(NINF, x) |
| 498 | self.assertEqual(math.pow(NINF, INF), INF) |
| 499 | self.assertEqual(math.pow(NINF, 3.), NINF) |
| 500 | self.assertEqual(math.pow(NINF, 2.3), INF) |
| 501 | self.assertEqual(math.pow(NINF, 2.), INF) |
| 502 | self.assertEqual(math.pow(NINF, 0.), 1.) |
| 503 | self.assertEqual(math.pow(NINF, -0.), 1.) |
| 504 | self.assertEqual(math.pow(NINF, -2.), 0.) |
| 505 | self.assertEqual(math.pow(NINF, -2.3), 0.) |
| 506 | self.assertEqual(math.pow(NINF, -3.), -0.) |
| 507 | self.assertEqual(math.pow(NINF, NINF), 0.) |
| 508 | self.assert_(math.isnan(math.pow(NINF, NAN))) |
| 509 | |
| 510 | # pow(-1, x) |
| 511 | self.assertEqual(math.pow(-1., INF), 1.) |
| 512 | self.assertEqual(math.pow(-1., 3.), -1.) |
| 513 | self.assertRaises(ValueError, math.pow, -1., 2.3) |
| 514 | self.assertEqual(math.pow(-1., 2.), 1.) |
| 515 | self.assertEqual(math.pow(-1., 0.), 1.) |
| 516 | self.assertEqual(math.pow(-1., -0.), 1.) |
| 517 | self.assertEqual(math.pow(-1., -2.), 1.) |
| 518 | self.assertRaises(ValueError, math.pow, -1., -2.3) |
| 519 | self.assertEqual(math.pow(-1., -3.), -1.) |
| 520 | self.assertEqual(math.pow(-1., NINF), 1.) |
| 521 | self.assert_(math.isnan(math.pow(-1., NAN))) |
| 522 | |
| 523 | # pow(1, x) |
| 524 | self.assertEqual(math.pow(1., INF), 1.) |
| 525 | self.assertEqual(math.pow(1., 3.), 1.) |
| 526 | self.assertEqual(math.pow(1., 2.3), 1.) |
| 527 | self.assertEqual(math.pow(1., 2.), 1.) |
| 528 | self.assertEqual(math.pow(1., 0.), 1.) |
| 529 | self.assertEqual(math.pow(1., -0.), 1.) |
| 530 | self.assertEqual(math.pow(1., -2.), 1.) |
| 531 | self.assertEqual(math.pow(1., -2.3), 1.) |
| 532 | self.assertEqual(math.pow(1., -3.), 1.) |
| 533 | self.assertEqual(math.pow(1., NINF), 1.) |
| 534 | self.assertEqual(math.pow(1., NAN), 1.) |
| 535 | |
| 536 | # pow(x, 0) should be 1 for any x |
| 537 | self.assertEqual(math.pow(2.3, 0.), 1.) |
| 538 | self.assertEqual(math.pow(-2.3, 0.), 1.) |
| 539 | self.assertEqual(math.pow(NAN, 0.), 1.) |
| 540 | self.assertEqual(math.pow(2.3, -0.), 1.) |
| 541 | self.assertEqual(math.pow(-2.3, -0.), 1.) |
| 542 | self.assertEqual(math.pow(NAN, -0.), 1.) |
| 543 | |
| 544 | # pow(x, y) is invalid if x is negative and y is not integral |
| 545 | self.assertRaises(ValueError, math.pow, -1., 2.3) |
| 546 | self.assertRaises(ValueError, math.pow, -15., -3.1) |
| 547 | |
| 548 | # pow(x, NINF) |
| 549 | self.assertEqual(math.pow(1.9, NINF), 0.) |
| 550 | self.assertEqual(math.pow(1.1, NINF), 0.) |
| 551 | self.assertEqual(math.pow(0.9, NINF), INF) |
| 552 | self.assertEqual(math.pow(0.1, NINF), INF) |
| 553 | self.assertEqual(math.pow(-0.1, NINF), INF) |
| 554 | self.assertEqual(math.pow(-0.9, NINF), INF) |
| 555 | self.assertEqual(math.pow(-1.1, NINF), 0.) |
| 556 | self.assertEqual(math.pow(-1.9, NINF), 0.) |
| 557 | |
| 558 | # pow(x, INF) |
| 559 | self.assertEqual(math.pow(1.9, INF), INF) |
| 560 | self.assertEqual(math.pow(1.1, INF), INF) |
| 561 | self.assertEqual(math.pow(0.9, INF), 0.) |
| 562 | self.assertEqual(math.pow(0.1, INF), 0.) |
| 563 | self.assertEqual(math.pow(-0.1, INF), 0.) |
| 564 | self.assertEqual(math.pow(-0.9, INF), 0.) |
| 565 | self.assertEqual(math.pow(-1.1, INF), INF) |
| 566 | self.assertEqual(math.pow(-1.9, INF), INF) |
| 567 | |
| 568 | # pow(x, y) should work for x negative, y an integer |
| 569 | self.ftest('(-2.)**3.', math.pow(-2.0, 3.0), -8.0) |
| 570 | self.ftest('(-2.)**2.', math.pow(-2.0, 2.0), 4.0) |
| 571 | self.ftest('(-2.)**1.', math.pow(-2.0, 1.0), -2.0) |
| 572 | self.ftest('(-2.)**0.', math.pow(-2.0, 0.0), 1.0) |
| 573 | self.ftest('(-2.)**-0.', math.pow(-2.0, -0.0), 1.0) |
| 574 | self.ftest('(-2.)**-1.', math.pow(-2.0, -1.0), -0.5) |
| 575 | self.ftest('(-2.)**-2.', math.pow(-2.0, -2.0), 0.25) |
| 576 | self.ftest('(-2.)**-3.', math.pow(-2.0, -3.0), -0.125) |
| 577 | self.assertRaises(ValueError, math.pow, -2.0, -0.5) |
| 578 | self.assertRaises(ValueError, math.pow, -2.0, 0.5) |
| 579 | |
| 580 | # the following tests have been commented out since they don't |
| 581 | # really belong here: the implementation of ** for floats is |
| 582 | # independent of the implemention of math.pow |
| 583 | #self.assertEqual(1**NAN, 1) |
| 584 | #self.assertEqual(1**INF, 1) |
| 585 | #self.assertEqual(1**NINF, 1) |
| 586 | #self.assertEqual(1**0, 1) |
| 587 | #self.assertEqual(1.**NAN, 1) |
| 588 | #self.assertEqual(1.**INF, 1) |
| 589 | #self.assertEqual(1.**NINF, 1) |
| 590 | #self.assertEqual(1.**0, 1) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 591 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 592 | def testRadians(self): |
| 593 | self.assertRaises(TypeError, math.radians) |
| 594 | self.ftest('radians(180)', math.radians(180), math.pi) |
| 595 | self.ftest('radians(90)', math.radians(90), math.pi/2) |
| 596 | self.ftest('radians(-45)', math.radians(-45), -math.pi/4) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 597 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 598 | def testSin(self): |
| 599 | self.assertRaises(TypeError, math.sin) |
| 600 | self.ftest('sin(0)', math.sin(0), 0) |
| 601 | self.ftest('sin(pi/2)', math.sin(math.pi/2), 1) |
| 602 | self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 603 | try: |
| 604 | self.assert_(math.isnan(math.sin(INF))) |
| 605 | self.assert_(math.isnan(math.sin(NINF))) |
| 606 | except ValueError: |
| 607 | self.assertRaises(ValueError, math.sin, INF) |
| 608 | self.assertRaises(ValueError, math.sin, NINF) |
| 609 | self.assert_(math.isnan(math.sin(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 610 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 611 | def testSinh(self): |
| 612 | self.assertRaises(TypeError, math.sinh) |
| 613 | self.ftest('sinh(0)', math.sinh(0), 0) |
| 614 | self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1) |
| 615 | self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 616 | self.assertEquals(math.sinh(INF), INF) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 617 | self.assertEquals(math.sinh(NINF), NINF) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 618 | self.assert_(math.isnan(math.sinh(NAN))) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 619 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 620 | def testSqrt(self): |
| 621 | self.assertRaises(TypeError, math.sqrt) |
| 622 | self.ftest('sqrt(0)', math.sqrt(0), 0) |
| 623 | self.ftest('sqrt(1)', math.sqrt(1), 1) |
| 624 | self.ftest('sqrt(4)', math.sqrt(4), 2) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 625 | self.assertEquals(math.sqrt(INF), INF) |
| 626 | self.assertRaises(ValueError, math.sqrt, NINF) |
| 627 | self.assert_(math.isnan(math.sqrt(NAN))) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 628 | |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 629 | def testSum(self): |
| 630 | # math.sum relies on exact rounding for correct operation. |
| 631 | # There's a known problem with IA32 floating-point that causes |
| 632 | # inexact rounding in some situations, and will cause the |
| 633 | # math.sum tests below to fail; see issue #2937. On non IEEE |
| 634 | # 754 platforms, and on IEEE 754 platforms that exhibit the |
| 635 | # problem described in issue #2937, we simply skip the whole |
| 636 | # test. |
| 637 | |
| 638 | if not float.__getformat__("double").startswith("IEEE"): |
| 639 | return |
| 640 | |
| 641 | # on IEEE 754 compliant machines, both of the expressions |
| 642 | # below should round to 10000000000000002.0. |
| 643 | if 1e16+2.999 != 1e16+2.9999: |
| 644 | return |
| 645 | |
| 646 | # Python version of math.sum algorithm, for comparison |
| 647 | def msum(iterable): |
| 648 | """Full precision sum of values in iterable. Returns the value of |
| 649 | the sum, rounded to the nearest representable floating-point number |
| 650 | using the round-half-to-even rule. |
| 651 | |
| 652 | """ |
| 653 | # Stage 1: accumulate partials |
| 654 | partials = [] |
| 655 | for x in iterable: |
| 656 | i = 0 |
| 657 | for y in partials: |
| 658 | if abs(x) < abs(y): |
| 659 | x, y = y, x |
| 660 | hi = x + y |
| 661 | lo = y - (hi - x) |
| 662 | if lo: |
| 663 | partials[i] = lo |
| 664 | i += 1 |
| 665 | x = hi |
| 666 | partials[i:] = [x] if x else [] |
| 667 | |
| 668 | # Stage 2: sum partials |
| 669 | if not partials: |
| 670 | return 0.0 |
| 671 | |
| 672 | # sum from the top, stopping as soon as the sum is inexact. |
| 673 | total = partials.pop() |
| 674 | while partials: |
| 675 | x = partials.pop() |
| 676 | old_total, total = total, total + x |
| 677 | error = x - (total - old_total) |
| 678 | if error != 0.0: |
| 679 | # adjust for correct rounding if necessary |
| 680 | if partials and (partials[-1] > 0.0) == (error > 0.0) and \ |
| 681 | total + 2*error - total == 2*error: |
| 682 | total += 2*error |
| 683 | break |
| 684 | return total |
| 685 | |
| 686 | from sys import float_info |
| 687 | maxfloat = float_info.max |
| 688 | twopow = 2.**(float_info.max_exp - 1) |
| 689 | |
| 690 | test_values = [ |
| 691 | ([], 0.0), |
| 692 | ([0.0], 0.0), |
| 693 | ([1e100, 1.0, -1e100, 1e-100, 1e50, -1.0, -1e50], 1e-100), |
| 694 | ([1e308, 1e308, -1e308], OverflowError), |
| 695 | ([-1e308, 1e308, 1e308], 1e308), |
| 696 | ([1e308, -1e308, 1e308], 1e308), |
| 697 | ([2.0**1023, 2.0**1023, -2.0**1000], OverflowError), |
| 698 | ([twopow, twopow, twopow, twopow, -twopow, -twopow, -twopow], |
| 699 | OverflowError), |
| 700 | ([2.0**53, -0.5, -2.0**-54], 2.0**53-1.0), |
| 701 | ([2.0**53, 1.0, 2.0**-100], 2.0**53+2.0), |
| 702 | ([2.0**53+10.0, 1.0, 2.0**-100], 2.0**53+12.0), |
| 703 | |
| 704 | ([2.0**53-4.0, 0.5, 2.0**-54], 2.0**53-3.0), |
| 705 | ([2.0**1023-2.0**970, -1.0, 2.0**1023], OverflowError), |
| 706 | ([maxfloat, maxfloat*2.**-54], maxfloat), |
| 707 | ([maxfloat, maxfloat*2.**-53], OverflowError), |
| 708 | ([1./n for n in range(1, 1001)], 7.4854708605503451), |
| 709 | ([(-1.)**n/n for n in range(1, 1001)], -0.69264743055982025), |
| 710 | ([1.7**(i+1)-1.7**i for i in range(1000)] + [-1.7**1000], -1.0), |
| 711 | ([INF, -INF, NAN], ValueError), |
| 712 | ([NAN, INF, -INF], ValueError), |
| 713 | ([INF, NAN, INF], ValueError), |
| 714 | |
| 715 | ([INF, INF], OverflowError), |
| 716 | ([INF, -INF], ValueError), |
| 717 | ([-INF, 1e308, 1e308, -INF], OverflowError), |
| 718 | ([2.0**1023-2.0**970, 0.0, 2.0**1023], OverflowError), |
| 719 | ([2.0**1023-2.0**970, 1.0, 2.0**1023], OverflowError), |
| 720 | ([2.0**1023, 2.0**1023], OverflowError), |
| 721 | ([2.0**1023, 2.0**1023, -1.0], OverflowError), |
| 722 | ([twopow, twopow, twopow, twopow, -twopow, -twopow], |
| 723 | OverflowError), |
| 724 | ([twopow, twopow, twopow, twopow, -twopow, twopow], OverflowError), |
| 725 | ([-twopow, -twopow, -twopow, -twopow], OverflowError), |
| 726 | |
| 727 | ([2.**1023, 2.**1023, -2.**971], OverflowError), |
| 728 | ([2.**1023, 2.**1023, -2.**970], OverflowError), |
| 729 | ([-2.**970, 2.**1023, 2.**1023, -2.**-1074], OverflowError), |
| 730 | ([ 2.**1023, 2.**1023, -2.**970, 2.**-1074], OverflowError), |
| 731 | ([-2.**1023, 2.**971, -2.**1023], -maxfloat), |
| 732 | ([-2.**1023, -2.**1023, 2.**970], OverflowError), |
| 733 | ([-2.**1023, -2.**1023, 2.**970, 2.**-1074], OverflowError), |
| 734 | ([-2.**-1074, -2.**1023, -2.**1023, 2.**970], OverflowError), |
| 735 | ([2.**930, -2.**980, 2.**1023, 2.**1023, twopow, -twopow], |
| 736 | OverflowError), |
| 737 | ([2.**1023, 2.**1023, -1e307], OverflowError), |
| 738 | ([1e16, 1., 1e-16], 10000000000000002.0), |
| 739 | ([1e16-2., 1.-2.**53, -(1e16-2.), -(1.-2.**53)], 0.0), |
| 740 | ] |
| 741 | |
| 742 | for i, (vals, s) in enumerate(test_values): |
| 743 | if isinstance(s, type) and issubclass(s, Exception): |
| 744 | try: |
| 745 | m = math.sum(vals) |
| 746 | except s: |
| 747 | pass |
| 748 | else: |
| 749 | self.fail("test %d failed: got %r, expected %r " |
| 750 | "for math.sum(%.100r)" % |
| 751 | (i, m, s.__name__, vals)) |
| 752 | else: |
| 753 | try: |
| 754 | self.assertEqual(math.sum(vals), s) |
| 755 | except OverflowError: |
| 756 | self.fail("test %d failed: got OverflowError, expected %r " |
| 757 | "for math.sum(%.100r)" % (i, s, vals)) |
| 758 | except ValueError: |
| 759 | self.fail("test %d failed: got ValueError, expected %r " |
| 760 | "for math.sum(%.100r)" % (i, s, vals)) |
| 761 | |
| 762 | # compare with output of msum above, but only when |
| 763 | # result isn't an IEEE special or an exception |
| 764 | if not math.isinf(s) and not math.isnan(s): |
| 765 | self.assertEqual(msum(vals), s) |
| 766 | |
| 767 | from random import random, gauss, shuffle |
| 768 | for j in range(1000): |
| 769 | vals = [7, 1e100, -7, -1e100, -9e-20, 8e-20] * 10 |
| 770 | s = 0 |
| 771 | for i in range(200): |
| 772 | v = gauss(0, random()) ** 7 - s |
| 773 | s += v |
| 774 | vals.append(v) |
| 775 | shuffle(vals) |
| 776 | |
| 777 | s = msum(vals) |
| 778 | self.assertEqual(msum(vals), math.sum(vals)) |
| 779 | |
| 780 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 781 | def testTan(self): |
| 782 | self.assertRaises(TypeError, math.tan) |
| 783 | self.ftest('tan(0)', math.tan(0), 0) |
| 784 | self.ftest('tan(pi/4)', math.tan(math.pi/4), 1) |
| 785 | self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 786 | try: |
| 787 | self.assert_(math.isnan(math.tan(INF))) |
| 788 | self.assert_(math.isnan(math.tan(NINF))) |
| 789 | except: |
| 790 | self.assertRaises(ValueError, math.tan, INF) |
| 791 | self.assertRaises(ValueError, math.tan, NINF) |
| 792 | self.assert_(math.isnan(math.tan(NAN))) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 793 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 794 | def testTanh(self): |
| 795 | self.assertRaises(TypeError, math.tanh) |
| 796 | self.ftest('tanh(0)', math.tanh(0), 0) |
| 797 | self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 798 | self.ftest('tanh(inf)', math.tanh(INF), 1) |
| 799 | self.ftest('tanh(-inf)', math.tanh(NINF), -1) |
| 800 | self.assert_(math.isnan(math.tanh(NAN))) |
Christian Heimes | e57950f | 2008-04-21 13:08:03 +0000 | [diff] [blame] | 801 | # check that tanh(-0.) == -0. on IEEE 754 systems |
| 802 | if float.__getformat__("double").startswith("IEEE"): |
| 803 | self.assertEqual(math.tanh(-0.), -0.) |
| 804 | self.assertEqual(math.copysign(1., math.tanh(-0.)), |
| 805 | math.copysign(1., -0.)) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 806 | |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 807 | def test_trunc(self): |
| 808 | self.assertEqual(math.trunc(1), 1) |
| 809 | self.assertEqual(math.trunc(-1), -1) |
| 810 | self.assertEqual(type(math.trunc(1)), int) |
| 811 | self.assertEqual(type(math.trunc(1.5)), int) |
| 812 | self.assertEqual(math.trunc(1.5), 1) |
| 813 | self.assertEqual(math.trunc(-1.5), -1) |
| 814 | self.assertEqual(math.trunc(1.999999), 1) |
| 815 | self.assertEqual(math.trunc(-1.999999), -1) |
| 816 | self.assertEqual(math.trunc(-0.999999), -0) |
| 817 | self.assertEqual(math.trunc(-100.999), -100) |
| 818 | |
| 819 | class TestTrunc(object): |
| 820 | def __trunc__(self): |
| 821 | return 23 |
| 822 | |
| 823 | class TestNoTrunc(object): |
| 824 | pass |
| 825 | |
| 826 | self.assertEqual(math.trunc(TestTrunc()), 23) |
| 827 | |
| 828 | self.assertRaises(TypeError, math.trunc) |
| 829 | self.assertRaises(TypeError, math.trunc, 1, 2) |
| 830 | self.assertRaises(TypeError, math.trunc, TestNoTrunc()) |
| 831 | |
| 832 | # XXX Doesn't work because the method is looked up on |
| 833 | # the type only. |
| 834 | #t = TestNoTrunc() |
| 835 | #t.__trunc__ = lambda *args: args |
| 836 | #self.assertEquals((), math.trunc(t)) |
| 837 | #self.assertRaises(TypeError, math.trunc, t, 0) |
| 838 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 839 | def testCopysign(self): |
| 840 | self.assertEqual(math.copysign(1, 42), 1.0) |
| 841 | self.assertEqual(math.copysign(0., 42), 0.0) |
| 842 | self.assertEqual(math.copysign(1., -42), -1.0) |
| 843 | self.assertEqual(math.copysign(3, 0.), 3.0) |
| 844 | self.assertEqual(math.copysign(4., -0.), -4.0) |
| 845 | |
| 846 | def testIsnan(self): |
| 847 | self.assert_(math.isnan(float("nan"))) |
| 848 | self.assert_(math.isnan(float("inf")* 0.)) |
| 849 | self.failIf(math.isnan(float("inf"))) |
| 850 | self.failIf(math.isnan(0.)) |
| 851 | self.failIf(math.isnan(1.)) |
| 852 | |
| 853 | def testIsinf(self): |
| 854 | self.assert_(math.isinf(float("inf"))) |
| 855 | self.assert_(math.isinf(float("-inf"))) |
| 856 | self.assert_(math.isinf(1E400)) |
| 857 | self.assert_(math.isinf(-1E400)) |
| 858 | self.failIf(math.isinf(float("nan"))) |
| 859 | self.failIf(math.isinf(0.)) |
| 860 | self.failIf(math.isinf(1.)) |
| 861 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 862 | # RED_FLAG 16-Oct-2000 Tim |
| 863 | # While 2.0 is more consistent about exceptions than previous releases, it |
| 864 | # still fails this part of the test on some platforms. For now, we only |
| 865 | # *run* test_exceptions() in verbose mode, so that this isn't normally |
| 866 | # tested. |
Tim Peters | 98c8184 | 2000-10-16 17:35:13 +0000 | [diff] [blame] | 867 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 868 | if verbose: |
| 869 | def test_exceptions(self): |
| 870 | try: |
| 871 | x = math.exp(-1000000000) |
| 872 | except: |
| 873 | # mathmodule.c is failing to weed out underflows from libm, or |
| 874 | # we've got an fp format with huge dynamic range |
| 875 | self.fail("underflowing exp() should not have raised " |
| 876 | "an exception") |
| 877 | if x != 0: |
| 878 | self.fail("underflowing exp() should have returned 0") |
| 879 | |
| 880 | # If this fails, probably using a strict IEEE-754 conforming libm, and x |
| 881 | # is +Inf afterwards. But Python wants overflows detected by default. |
| 882 | try: |
| 883 | x = math.exp(1000000000) |
| 884 | except OverflowError: |
| 885 | pass |
| 886 | else: |
| 887 | self.fail("overflowing exp() didn't trigger OverflowError") |
| 888 | |
| 889 | # If this fails, it could be a puzzle. One odd possibility is that |
| 890 | # mathmodule.c's macros are getting confused while comparing |
| 891 | # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE |
| 892 | # as a result (and so raising OverflowError instead). |
| 893 | try: |
| 894 | x = math.sqrt(-1.0) |
| 895 | except ValueError: |
| 896 | pass |
| 897 | else: |
| 898 | self.fail("sqrt(-1) didn't raise ValueError") |
| 899 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 900 | def test_testfile(self): |
| 901 | if not float.__getformat__("double").startswith("IEEE"): |
| 902 | return |
| 903 | for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file): |
| 904 | # Skip if either the input or result is complex, or if |
| 905 | # flags is nonempty |
| 906 | if ai != 0. or ei != 0. or flags: |
| 907 | continue |
| 908 | if fn in ['rect', 'polar']: |
| 909 | # no real versions of rect, polar |
| 910 | continue |
| 911 | func = getattr(math, fn) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 912 | try: |
| 913 | result = func(ar) |
Mark Dickinson | a0de26c | 2008-04-30 23:30:57 +0000 | [diff] [blame] | 914 | except ValueError as exc: |
| 915 | message = (("Unexpected ValueError: %s\n " + |
| 916 | "in test %s:%s(%r)\n") % (exc.args[0], id, fn, ar)) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 917 | self.fail(message) |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 918 | except OverflowError: |
| 919 | message = ("Unexpected OverflowError in " + |
| 920 | "test %s:%s(%r)\n" % (id, fn, ar)) |
| 921 | self.fail(message) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 922 | self.ftest("%s:%s(%r)" % (id, fn, ar), result, er) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 923 | |
| 924 | def test_main(): |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 925 | from doctest import DocFileSuite |
| 926 | suite = unittest.TestSuite() |
| 927 | suite.addTest(unittest.makeSuite(MathTests)) |
| 928 | suite.addTest(DocFileSuite("ieee754.txt")) |
| 929 | run_unittest(suite) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 930 | |
| 931 | if __name__ == '__main__': |
| 932 | test_main() |