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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 629 | def testTan(self): |
| 630 | self.assertRaises(TypeError, math.tan) |
| 631 | self.ftest('tan(0)', math.tan(0), 0) |
| 632 | self.ftest('tan(pi/4)', math.tan(math.pi/4), 1) |
| 633 | self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 634 | try: |
| 635 | self.assert_(math.isnan(math.tan(INF))) |
| 636 | self.assert_(math.isnan(math.tan(NINF))) |
| 637 | except: |
| 638 | self.assertRaises(ValueError, math.tan, INF) |
| 639 | self.assertRaises(ValueError, math.tan, NINF) |
| 640 | self.assert_(math.isnan(math.tan(NAN))) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 641 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 642 | def testTanh(self): |
| 643 | self.assertRaises(TypeError, math.tanh) |
| 644 | self.ftest('tanh(0)', math.tanh(0), 0) |
| 645 | 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] | 646 | self.ftest('tanh(inf)', math.tanh(INF), 1) |
| 647 | self.ftest('tanh(-inf)', math.tanh(NINF), -1) |
| 648 | self.assert_(math.isnan(math.tanh(NAN))) |
Christian Heimes | e57950f | 2008-04-21 13:08:03 +0000 | [diff] [blame] | 649 | # check that tanh(-0.) == -0. on IEEE 754 systems |
| 650 | if float.__getformat__("double").startswith("IEEE"): |
| 651 | self.assertEqual(math.tanh(-0.), -0.) |
| 652 | self.assertEqual(math.copysign(1., math.tanh(-0.)), |
| 653 | math.copysign(1., -0.)) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 654 | |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 655 | def test_trunc(self): |
| 656 | self.assertEqual(math.trunc(1), 1) |
| 657 | self.assertEqual(math.trunc(-1), -1) |
| 658 | self.assertEqual(type(math.trunc(1)), int) |
| 659 | self.assertEqual(type(math.trunc(1.5)), int) |
| 660 | self.assertEqual(math.trunc(1.5), 1) |
| 661 | self.assertEqual(math.trunc(-1.5), -1) |
| 662 | self.assertEqual(math.trunc(1.999999), 1) |
| 663 | self.assertEqual(math.trunc(-1.999999), -1) |
| 664 | self.assertEqual(math.trunc(-0.999999), -0) |
| 665 | self.assertEqual(math.trunc(-100.999), -100) |
| 666 | |
| 667 | class TestTrunc(object): |
| 668 | def __trunc__(self): |
| 669 | return 23 |
| 670 | |
| 671 | class TestNoTrunc(object): |
| 672 | pass |
| 673 | |
| 674 | self.assertEqual(math.trunc(TestTrunc()), 23) |
| 675 | |
| 676 | self.assertRaises(TypeError, math.trunc) |
| 677 | self.assertRaises(TypeError, math.trunc, 1, 2) |
| 678 | self.assertRaises(TypeError, math.trunc, TestNoTrunc()) |
| 679 | |
| 680 | # XXX Doesn't work because the method is looked up on |
| 681 | # the type only. |
| 682 | #t = TestNoTrunc() |
| 683 | #t.__trunc__ = lambda *args: args |
| 684 | #self.assertEquals((), math.trunc(t)) |
| 685 | #self.assertRaises(TypeError, math.trunc, t, 0) |
| 686 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 687 | def testCopysign(self): |
| 688 | self.assertEqual(math.copysign(1, 42), 1.0) |
| 689 | self.assertEqual(math.copysign(0., 42), 0.0) |
| 690 | self.assertEqual(math.copysign(1., -42), -1.0) |
| 691 | self.assertEqual(math.copysign(3, 0.), 3.0) |
| 692 | self.assertEqual(math.copysign(4., -0.), -4.0) |
| 693 | |
| 694 | def testIsnan(self): |
| 695 | self.assert_(math.isnan(float("nan"))) |
| 696 | self.assert_(math.isnan(float("inf")* 0.)) |
| 697 | self.failIf(math.isnan(float("inf"))) |
| 698 | self.failIf(math.isnan(0.)) |
| 699 | self.failIf(math.isnan(1.)) |
| 700 | |
| 701 | def testIsinf(self): |
| 702 | self.assert_(math.isinf(float("inf"))) |
| 703 | self.assert_(math.isinf(float("-inf"))) |
| 704 | self.assert_(math.isinf(1E400)) |
| 705 | self.assert_(math.isinf(-1E400)) |
| 706 | self.failIf(math.isinf(float("nan"))) |
| 707 | self.failIf(math.isinf(0.)) |
| 708 | self.failIf(math.isinf(1.)) |
| 709 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 710 | # RED_FLAG 16-Oct-2000 Tim |
| 711 | # While 2.0 is more consistent about exceptions than previous releases, it |
| 712 | # still fails this part of the test on some platforms. For now, we only |
| 713 | # *run* test_exceptions() in verbose mode, so that this isn't normally |
| 714 | # tested. |
Tim Peters | 98c8184 | 2000-10-16 17:35:13 +0000 | [diff] [blame] | 715 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 716 | if verbose: |
| 717 | def test_exceptions(self): |
| 718 | try: |
| 719 | x = math.exp(-1000000000) |
| 720 | except: |
| 721 | # mathmodule.c is failing to weed out underflows from libm, or |
| 722 | # we've got an fp format with huge dynamic range |
| 723 | self.fail("underflowing exp() should not have raised " |
| 724 | "an exception") |
| 725 | if x != 0: |
| 726 | self.fail("underflowing exp() should have returned 0") |
| 727 | |
| 728 | # If this fails, probably using a strict IEEE-754 conforming libm, and x |
| 729 | # is +Inf afterwards. But Python wants overflows detected by default. |
| 730 | try: |
| 731 | x = math.exp(1000000000) |
| 732 | except OverflowError: |
| 733 | pass |
| 734 | else: |
| 735 | self.fail("overflowing exp() didn't trigger OverflowError") |
| 736 | |
| 737 | # If this fails, it could be a puzzle. One odd possibility is that |
| 738 | # mathmodule.c's macros are getting confused while comparing |
| 739 | # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE |
| 740 | # as a result (and so raising OverflowError instead). |
| 741 | try: |
| 742 | x = math.sqrt(-1.0) |
| 743 | except ValueError: |
| 744 | pass |
| 745 | else: |
| 746 | self.fail("sqrt(-1) didn't raise ValueError") |
| 747 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 748 | def test_testfile(self): |
| 749 | if not float.__getformat__("double").startswith("IEEE"): |
| 750 | return |
| 751 | for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file): |
| 752 | # Skip if either the input or result is complex, or if |
| 753 | # flags is nonempty |
| 754 | if ai != 0. or ei != 0. or flags: |
| 755 | continue |
| 756 | if fn in ['rect', 'polar']: |
| 757 | # no real versions of rect, polar |
| 758 | continue |
| 759 | func = getattr(math, fn) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 760 | try: |
| 761 | result = func(ar) |
Mark Dickinson | a0de26c | 2008-04-30 23:30:57 +0000 | [diff] [blame] | 762 | except ValueError as exc: |
| 763 | message = (("Unexpected ValueError: %s\n " + |
| 764 | "in test %s:%s(%r)\n") % (exc.args[0], id, fn, ar)) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 765 | self.fail(message) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 766 | self.ftest("%s:%s(%r)" % (id, fn, ar), result, er) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 767 | |
| 768 | def test_main(): |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 769 | from doctest import DocFileSuite |
| 770 | suite = unittest.TestSuite() |
| 771 | suite.addTest(unittest.makeSuite(MathTests)) |
| 772 | suite.addTest(DocFileSuite("ieee754.txt")) |
| 773 | run_unittest(suite) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 774 | |
| 775 | if __name__ == '__main__': |
| 776 | test_main() |