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 | |
Eric Smith | f24a0d9 | 2010-12-04 13:32:18 +0000 | [diff] [blame] | 4 | from test.support import run_unittest, verbose, requires_IEEE_754 |
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 |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 9 | import random |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 10 | import struct |
Victor Stinner | be3da38 | 2010-11-07 14:14:27 +0000 | [diff] [blame] | 11 | import sysconfig |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 12 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 13 | eps = 1E-05 |
| 14 | NAN = float('nan') |
| 15 | INF = float('inf') |
| 16 | NINF = float('-inf') |
| 17 | |
Mark Dickinson | 5c56708 | 2009-04-24 16:39:07 +0000 | [diff] [blame] | 18 | # detect evidence of double-rounding: fsum is not always correctly |
| 19 | # rounded on machines that suffer from double rounding. |
| 20 | x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer |
| 21 | HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4) |
| 22 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 23 | # locate file with test values |
| 24 | if __name__ == '__main__': |
| 25 | file = sys.argv[0] |
| 26 | else: |
| 27 | file = __file__ |
| 28 | test_dir = os.path.dirname(file) or os.curdir |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 29 | math_testcases = os.path.join(test_dir, 'math_testcases.txt') |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 30 | test_file = os.path.join(test_dir, 'cmath_testcases.txt') |
| 31 | |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 32 | def to_ulps(x): |
| 33 | """Convert a non-NaN float x to an integer, in such a way that |
| 34 | adjacent floats are converted to adjacent integers. Then |
| 35 | abs(ulps(x) - ulps(y)) gives the difference in ulps between two |
| 36 | floats. |
| 37 | |
| 38 | The results from this function will only make sense on platforms |
| 39 | where C doubles are represented in IEEE 754 binary64 format. |
| 40 | |
| 41 | """ |
Mark Dickinson | d412ab5 | 2009-10-17 07:10:00 +0000 | [diff] [blame] | 42 | n = struct.unpack('<q', struct.pack('<d', x))[0] |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 43 | if n < 0: |
| 44 | n = ~(n+2**63) |
| 45 | return n |
| 46 | |
Mark Dickinson | 05d2e08 | 2009-12-11 20:17:17 +0000 | [diff] [blame] | 47 | def ulps_check(expected, got, ulps=20): |
| 48 | """Given non-NaN floats `expected` and `got`, |
| 49 | check that they're equal to within the given number of ulps. |
| 50 | |
| 51 | Returns None on success and an error message on failure.""" |
| 52 | |
| 53 | ulps_error = to_ulps(got) - to_ulps(expected) |
| 54 | if abs(ulps_error) <= ulps: |
| 55 | return None |
| 56 | return "error = {} ulps; permitted error = {} ulps".format(ulps_error, |
| 57 | ulps) |
| 58 | |
Mark Dickinson | 4c8a9a2 | 2010-05-15 17:02:38 +0000 | [diff] [blame] | 59 | # Here's a pure Python version of the math.factorial algorithm, for |
| 60 | # documentation and comparison purposes. |
| 61 | # |
| 62 | # Formula: |
| 63 | # |
| 64 | # factorial(n) = factorial_odd_part(n) << (n - count_set_bits(n)) |
| 65 | # |
| 66 | # where |
| 67 | # |
| 68 | # factorial_odd_part(n) = product_{i >= 0} product_{0 < j <= n >> i; j odd} j |
| 69 | # |
| 70 | # The outer product above is an infinite product, but once i >= n.bit_length, |
| 71 | # (n >> i) < 1 and the corresponding term of the product is empty. So only the |
| 72 | # finitely many terms for 0 <= i < n.bit_length() contribute anything. |
| 73 | # |
| 74 | # We iterate downwards from i == n.bit_length() - 1 to i == 0. The inner |
| 75 | # product in the formula above starts at 1 for i == n.bit_length(); for each i |
| 76 | # < n.bit_length() we get the inner product for i from that for i + 1 by |
| 77 | # multiplying by all j in {n >> i+1 < j <= n >> i; j odd}. In Python terms, |
| 78 | # this set is range((n >> i+1) + 1 | 1, (n >> i) + 1 | 1, 2). |
| 79 | |
| 80 | def count_set_bits(n): |
| 81 | """Number of '1' bits in binary expansion of a nonnnegative integer.""" |
| 82 | return 1 + count_set_bits(n & n - 1) if n else 0 |
| 83 | |
| 84 | def partial_product(start, stop): |
| 85 | """Product of integers in range(start, stop, 2), computed recursively. |
| 86 | start and stop should both be odd, with start <= stop. |
| 87 | |
| 88 | """ |
| 89 | numfactors = (stop - start) >> 1 |
| 90 | if not numfactors: |
| 91 | return 1 |
| 92 | elif numfactors == 1: |
| 93 | return start |
| 94 | else: |
| 95 | mid = (start + numfactors) | 1 |
| 96 | return partial_product(start, mid) * partial_product(mid, stop) |
| 97 | |
| 98 | def py_factorial(n): |
| 99 | """Factorial of nonnegative integer n, via "Binary Split Factorial Formula" |
| 100 | described at http://www.luschny.de/math/factorial/binarysplitfact.html |
| 101 | |
| 102 | """ |
| 103 | inner = outer = 1 |
| 104 | for i in reversed(range(n.bit_length())): |
| 105 | inner *= partial_product((n >> i + 1) + 1 | 1, (n >> i) + 1 | 1) |
| 106 | outer *= inner |
| 107 | return outer << (n - count_set_bits(n)) |
| 108 | |
Mark Dickinson | 05d2e08 | 2009-12-11 20:17:17 +0000 | [diff] [blame] | 109 | def acc_check(expected, got, rel_err=2e-15, abs_err = 5e-323): |
| 110 | """Determine whether non-NaN floats a and b are equal to within a |
| 111 | (small) rounding error. The default values for rel_err and |
| 112 | abs_err are chosen to be suitable for platforms where a float is |
| 113 | represented by an IEEE 754 double. They allow an error of between |
| 114 | 9 and 19 ulps.""" |
| 115 | |
| 116 | # need to special case infinities, since inf - inf gives nan |
| 117 | if math.isinf(expected) and got == expected: |
| 118 | return None |
| 119 | |
| 120 | error = got - expected |
| 121 | |
| 122 | permitted_error = max(abs_err, rel_err * abs(expected)) |
| 123 | if abs(error) < permitted_error: |
| 124 | return None |
| 125 | return "error = {}; permitted error = {}".format(error, |
| 126 | permitted_error) |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 127 | |
| 128 | def parse_mtestfile(fname): |
| 129 | """Parse a file with test values |
| 130 | |
| 131 | -- starts a comment |
| 132 | blank lines, or lines containing only a comment, are ignored |
| 133 | other lines are expected to have the form |
| 134 | id fn arg -> expected [flag]* |
| 135 | |
| 136 | """ |
| 137 | with open(fname) as fp: |
| 138 | for line in fp: |
| 139 | # strip comments, and skip blank lines |
| 140 | if '--' in line: |
| 141 | line = line[:line.index('--')] |
| 142 | if not line.strip(): |
| 143 | continue |
| 144 | |
| 145 | lhs, rhs = line.split('->') |
| 146 | id, fn, arg = lhs.split() |
| 147 | rhs_pieces = rhs.split() |
| 148 | exp = rhs_pieces[0] |
| 149 | flags = rhs_pieces[1:] |
| 150 | |
| 151 | yield (id, fn, float(arg), float(exp), flags) |
| 152 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 153 | def parse_testfile(fname): |
| 154 | """Parse a file with test values |
| 155 | |
| 156 | Empty lines or lines starting with -- are ignored |
| 157 | yields id, fn, arg_real, arg_imag, exp_real, exp_imag |
| 158 | """ |
| 159 | with open(fname) as fp: |
| 160 | for line in fp: |
| 161 | # skip comment lines and blank lines |
| 162 | if line.startswith('--') or not line.strip(): |
| 163 | continue |
| 164 | |
| 165 | lhs, rhs = line.split('->') |
| 166 | id, fn, arg_real, arg_imag = lhs.split() |
| 167 | rhs_pieces = rhs.split() |
| 168 | exp_real, exp_imag = rhs_pieces[0], rhs_pieces[1] |
| 169 | flags = rhs_pieces[2:] |
| 170 | |
| 171 | yield (id, fn, |
| 172 | float(arg_real), float(arg_imag), |
| 173 | float(exp_real), float(exp_imag), |
| 174 | flags |
| 175 | ) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 176 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 177 | class MathTests(unittest.TestCase): |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 178 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 179 | def ftest(self, name, value, expected): |
| 180 | if abs(value-expected) > eps: |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 181 | # Use %r instead of %f so the error message |
| 182 | # displays full precision. Otherwise discrepancies |
| 183 | # in the last few bits will lead to very confusing |
| 184 | # error messages |
| 185 | self.fail('%s returned %r, expected %r' % |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 186 | (name, value, expected)) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 187 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 188 | def testConstants(self): |
| 189 | self.ftest('pi', math.pi, 3.1415926) |
| 190 | self.ftest('e', math.e, 2.7182818) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 191 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 192 | def testAcos(self): |
| 193 | self.assertRaises(TypeError, math.acos) |
| 194 | self.ftest('acos(-1)', math.acos(-1), math.pi) |
| 195 | self.ftest('acos(0)', math.acos(0), math.pi/2) |
| 196 | self.ftest('acos(1)', math.acos(1), 0) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 197 | self.assertRaises(ValueError, math.acos, INF) |
| 198 | self.assertRaises(ValueError, math.acos, NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 199 | self.assertTrue(math.isnan(math.acos(NAN))) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 200 | |
| 201 | def testAcosh(self): |
| 202 | self.assertRaises(TypeError, math.acosh) |
| 203 | self.ftest('acosh(1)', math.acosh(1), 0) |
| 204 | self.ftest('acosh(2)', math.acosh(2), 1.3169578969248168) |
| 205 | self.assertRaises(ValueError, math.acosh, 0) |
| 206 | self.assertRaises(ValueError, math.acosh, -1) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 207 | self.assertEqual(math.acosh(INF), INF) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 208 | self.assertRaises(ValueError, math.acosh, NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 209 | self.assertTrue(math.isnan(math.acosh(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 210 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 211 | def testAsin(self): |
| 212 | self.assertRaises(TypeError, math.asin) |
| 213 | self.ftest('asin(-1)', math.asin(-1), -math.pi/2) |
| 214 | self.ftest('asin(0)', math.asin(0), 0) |
| 215 | self.ftest('asin(1)', math.asin(1), math.pi/2) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 216 | self.assertRaises(ValueError, math.asin, INF) |
| 217 | self.assertRaises(ValueError, math.asin, NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 218 | self.assertTrue(math.isnan(math.asin(NAN))) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 219 | |
| 220 | def testAsinh(self): |
| 221 | self.assertRaises(TypeError, math.asinh) |
| 222 | self.ftest('asinh(0)', math.asinh(0), 0) |
| 223 | self.ftest('asinh(1)', math.asinh(1), 0.88137358701954305) |
| 224 | self.ftest('asinh(-1)', math.asinh(-1), -0.88137358701954305) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 225 | self.assertEqual(math.asinh(INF), INF) |
| 226 | self.assertEqual(math.asinh(NINF), NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 227 | self.assertTrue(math.isnan(math.asinh(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 228 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 229 | def testAtan(self): |
| 230 | self.assertRaises(TypeError, math.atan) |
| 231 | self.ftest('atan(-1)', math.atan(-1), -math.pi/4) |
| 232 | self.ftest('atan(0)', math.atan(0), 0) |
| 233 | self.ftest('atan(1)', math.atan(1), math.pi/4) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 234 | self.ftest('atan(inf)', math.atan(INF), math.pi/2) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 235 | self.ftest('atan(-inf)', math.atan(NINF), -math.pi/2) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 236 | self.assertTrue(math.isnan(math.atan(NAN))) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 237 | |
| 238 | def testAtanh(self): |
| 239 | self.assertRaises(TypeError, math.atan) |
| 240 | self.ftest('atanh(0)', math.atanh(0), 0) |
| 241 | self.ftest('atanh(0.5)', math.atanh(0.5), 0.54930614433405489) |
| 242 | self.ftest('atanh(-0.5)', math.atanh(-0.5), -0.54930614433405489) |
| 243 | self.assertRaises(ValueError, math.atanh, 1) |
| 244 | self.assertRaises(ValueError, math.atanh, -1) |
| 245 | self.assertRaises(ValueError, math.atanh, INF) |
| 246 | self.assertRaises(ValueError, math.atanh, NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 247 | self.assertTrue(math.isnan(math.atanh(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 248 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 249 | def testAtan2(self): |
| 250 | self.assertRaises(TypeError, math.atan2) |
| 251 | self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2) |
| 252 | self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4) |
| 253 | self.ftest('atan2(0, 1)', math.atan2(0, 1), 0) |
| 254 | self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4) |
| 255 | 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] | 256 | |
Christian Heimes | e57950f | 2008-04-21 13:08:03 +0000 | [diff] [blame] | 257 | # math.atan2(0, x) |
| 258 | self.ftest('atan2(0., -inf)', math.atan2(0., NINF), math.pi) |
| 259 | self.ftest('atan2(0., -2.3)', math.atan2(0., -2.3), math.pi) |
| 260 | self.ftest('atan2(0., -0.)', math.atan2(0., -0.), math.pi) |
| 261 | self.assertEqual(math.atan2(0., 0.), 0.) |
| 262 | self.assertEqual(math.atan2(0., 2.3), 0.) |
| 263 | self.assertEqual(math.atan2(0., INF), 0.) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 264 | self.assertTrue(math.isnan(math.atan2(0., NAN))) |
Christian Heimes | e57950f | 2008-04-21 13:08:03 +0000 | [diff] [blame] | 265 | # math.atan2(-0, x) |
| 266 | self.ftest('atan2(-0., -inf)', math.atan2(-0., NINF), -math.pi) |
| 267 | self.ftest('atan2(-0., -2.3)', math.atan2(-0., -2.3), -math.pi) |
| 268 | self.ftest('atan2(-0., -0.)', math.atan2(-0., -0.), -math.pi) |
| 269 | self.assertEqual(math.atan2(-0., 0.), -0.) |
| 270 | self.assertEqual(math.atan2(-0., 2.3), -0.) |
| 271 | self.assertEqual(math.atan2(-0., INF), -0.) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 272 | self.assertTrue(math.isnan(math.atan2(-0., NAN))) |
Christian Heimes | e57950f | 2008-04-21 13:08:03 +0000 | [diff] [blame] | 273 | # math.atan2(INF, x) |
| 274 | self.ftest('atan2(inf, -inf)', math.atan2(INF, NINF), math.pi*3/4) |
| 275 | self.ftest('atan2(inf, -2.3)', math.atan2(INF, -2.3), math.pi/2) |
| 276 | self.ftest('atan2(inf, -0.)', math.atan2(INF, -0.0), math.pi/2) |
| 277 | self.ftest('atan2(inf, 0.)', math.atan2(INF, 0.0), math.pi/2) |
| 278 | self.ftest('atan2(inf, 2.3)', math.atan2(INF, 2.3), math.pi/2) |
| 279 | self.ftest('atan2(inf, inf)', math.atan2(INF, INF), math.pi/4) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 280 | self.assertTrue(math.isnan(math.atan2(INF, NAN))) |
Christian Heimes | e57950f | 2008-04-21 13:08:03 +0000 | [diff] [blame] | 281 | # math.atan2(NINF, x) |
| 282 | self.ftest('atan2(-inf, -inf)', math.atan2(NINF, NINF), -math.pi*3/4) |
| 283 | self.ftest('atan2(-inf, -2.3)', math.atan2(NINF, -2.3), -math.pi/2) |
| 284 | self.ftest('atan2(-inf, -0.)', math.atan2(NINF, -0.0), -math.pi/2) |
| 285 | self.ftest('atan2(-inf, 0.)', math.atan2(NINF, 0.0), -math.pi/2) |
| 286 | self.ftest('atan2(-inf, 2.3)', math.atan2(NINF, 2.3), -math.pi/2) |
| 287 | self.ftest('atan2(-inf, inf)', math.atan2(NINF, INF), -math.pi/4) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 288 | self.assertTrue(math.isnan(math.atan2(NINF, NAN))) |
Christian Heimes | e57950f | 2008-04-21 13:08:03 +0000 | [diff] [blame] | 289 | # math.atan2(+finite, x) |
| 290 | self.ftest('atan2(2.3, -inf)', math.atan2(2.3, NINF), math.pi) |
| 291 | self.ftest('atan2(2.3, -0.)', math.atan2(2.3, -0.), math.pi/2) |
| 292 | self.ftest('atan2(2.3, 0.)', math.atan2(2.3, 0.), math.pi/2) |
| 293 | self.assertEqual(math.atan2(2.3, INF), 0.) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 294 | self.assertTrue(math.isnan(math.atan2(2.3, NAN))) |
Christian Heimes | e57950f | 2008-04-21 13:08:03 +0000 | [diff] [blame] | 295 | # math.atan2(-finite, x) |
| 296 | self.ftest('atan2(-2.3, -inf)', math.atan2(-2.3, NINF), -math.pi) |
| 297 | self.ftest('atan2(-2.3, -0.)', math.atan2(-2.3, -0.), -math.pi/2) |
| 298 | self.ftest('atan2(-2.3, 0.)', math.atan2(-2.3, 0.), -math.pi/2) |
| 299 | self.assertEqual(math.atan2(-2.3, INF), -0.) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 300 | self.assertTrue(math.isnan(math.atan2(-2.3, NAN))) |
Christian Heimes | e57950f | 2008-04-21 13:08:03 +0000 | [diff] [blame] | 301 | # math.atan2(NAN, x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 302 | self.assertTrue(math.isnan(math.atan2(NAN, NINF))) |
| 303 | self.assertTrue(math.isnan(math.atan2(NAN, -2.3))) |
| 304 | self.assertTrue(math.isnan(math.atan2(NAN, -0.))) |
| 305 | self.assertTrue(math.isnan(math.atan2(NAN, 0.))) |
| 306 | self.assertTrue(math.isnan(math.atan2(NAN, 2.3))) |
| 307 | self.assertTrue(math.isnan(math.atan2(NAN, INF))) |
| 308 | self.assertTrue(math.isnan(math.atan2(NAN, NAN))) |
Christian Heimes | e57950f | 2008-04-21 13:08:03 +0000 | [diff] [blame] | 309 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 310 | def testCeil(self): |
| 311 | self.assertRaises(TypeError, math.ceil) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 312 | self.assertEqual(int, type(math.ceil(0.5))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 313 | self.ftest('ceil(0.5)', math.ceil(0.5), 1) |
| 314 | self.ftest('ceil(1.0)', math.ceil(1.0), 1) |
| 315 | self.ftest('ceil(1.5)', math.ceil(1.5), 2) |
| 316 | self.ftest('ceil(-0.5)', math.ceil(-0.5), 0) |
| 317 | self.ftest('ceil(-1.0)', math.ceil(-1.0), -1) |
| 318 | self.ftest('ceil(-1.5)', math.ceil(-1.5), -1) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 319 | #self.assertEqual(math.ceil(INF), INF) |
| 320 | #self.assertEqual(math.ceil(NINF), NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 321 | #self.assertTrue(math.isnan(math.ceil(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 322 | |
Guido van Rossum | 13e05de | 2007-08-23 22:56:55 +0000 | [diff] [blame] | 323 | class TestCeil: |
| 324 | def __ceil__(self): |
| 325 | return 42 |
| 326 | class TestNoCeil: |
| 327 | pass |
| 328 | self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42) |
| 329 | self.assertRaises(TypeError, math.ceil, TestNoCeil()) |
| 330 | |
| 331 | t = TestNoCeil() |
| 332 | t.__ceil__ = lambda *args: args |
| 333 | self.assertRaises(TypeError, math.ceil, t) |
| 334 | self.assertRaises(TypeError, math.ceil, t, 0) |
| 335 | |
Mark Dickinson | 6356623 | 2009-09-18 21:04:19 +0000 | [diff] [blame] | 336 | @requires_IEEE_754 |
| 337 | def testCopysign(self): |
Mark Dickinson | 06b59e0 | 2010-02-06 23:16:50 +0000 | [diff] [blame] | 338 | self.assertEqual(math.copysign(1, 42), 1.0) |
| 339 | self.assertEqual(math.copysign(0., 42), 0.0) |
| 340 | self.assertEqual(math.copysign(1., -42), -1.0) |
| 341 | self.assertEqual(math.copysign(3, 0.), 3.0) |
| 342 | self.assertEqual(math.copysign(4., -0.), -4.0) |
| 343 | |
Mark Dickinson | 6356623 | 2009-09-18 21:04:19 +0000 | [diff] [blame] | 344 | self.assertRaises(TypeError, math.copysign) |
| 345 | # copysign should let us distinguish signs of zeros |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 346 | self.assertEqual(math.copysign(1., 0.), 1.) |
| 347 | self.assertEqual(math.copysign(1., -0.), -1.) |
| 348 | self.assertEqual(math.copysign(INF, 0.), INF) |
| 349 | self.assertEqual(math.copysign(INF, -0.), NINF) |
| 350 | self.assertEqual(math.copysign(NINF, 0.), INF) |
| 351 | self.assertEqual(math.copysign(NINF, -0.), NINF) |
Mark Dickinson | 6356623 | 2009-09-18 21:04:19 +0000 | [diff] [blame] | 352 | # and of infinities |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 353 | self.assertEqual(math.copysign(1., INF), 1.) |
| 354 | self.assertEqual(math.copysign(1., NINF), -1.) |
| 355 | self.assertEqual(math.copysign(INF, INF), INF) |
| 356 | self.assertEqual(math.copysign(INF, NINF), NINF) |
| 357 | self.assertEqual(math.copysign(NINF, INF), INF) |
| 358 | self.assertEqual(math.copysign(NINF, NINF), NINF) |
Mark Dickinson | 06b59e0 | 2010-02-06 23:16:50 +0000 | [diff] [blame] | 359 | self.assertTrue(math.isnan(math.copysign(NAN, 1.))) |
| 360 | self.assertTrue(math.isnan(math.copysign(NAN, INF))) |
| 361 | self.assertTrue(math.isnan(math.copysign(NAN, NINF))) |
| 362 | self.assertTrue(math.isnan(math.copysign(NAN, NAN))) |
Mark Dickinson | 6356623 | 2009-09-18 21:04:19 +0000 | [diff] [blame] | 363 | # copysign(INF, NAN) may be INF or it may be NINF, since |
| 364 | # we don't know whether the sign bit of NAN is set on any |
| 365 | # given platform. |
Mark Dickinson | 06b59e0 | 2010-02-06 23:16:50 +0000 | [diff] [blame] | 366 | self.assertTrue(math.isinf(math.copysign(INF, NAN))) |
Mark Dickinson | 6356623 | 2009-09-18 21:04:19 +0000 | [diff] [blame] | 367 | # similarly, copysign(2., NAN) could be 2. or -2. |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 368 | self.assertEqual(abs(math.copysign(2., NAN)), 2.) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 369 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 370 | def testCos(self): |
| 371 | self.assertRaises(TypeError, math.cos) |
| 372 | self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0) |
| 373 | self.ftest('cos(0)', math.cos(0), 1) |
| 374 | self.ftest('cos(pi/2)', math.cos(math.pi/2), 0) |
| 375 | self.ftest('cos(pi)', math.cos(math.pi), -1) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 376 | try: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 377 | self.assertTrue(math.isnan(math.cos(INF))) |
| 378 | self.assertTrue(math.isnan(math.cos(NINF))) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 379 | except ValueError: |
| 380 | self.assertRaises(ValueError, math.cos, INF) |
| 381 | self.assertRaises(ValueError, math.cos, NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 382 | self.assertTrue(math.isnan(math.cos(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 383 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 384 | def testCosh(self): |
| 385 | self.assertRaises(TypeError, math.cosh) |
| 386 | self.ftest('cosh(0)', math.cosh(0), 1) |
| 387 | self.ftest('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 388 | self.assertEqual(math.cosh(INF), INF) |
| 389 | self.assertEqual(math.cosh(NINF), INF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 390 | self.assertTrue(math.isnan(math.cosh(NAN))) |
Raymond Hettinger | 64108af | 2002-05-13 03:55:01 +0000 | [diff] [blame] | 391 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 392 | def testDegrees(self): |
| 393 | self.assertRaises(TypeError, math.degrees) |
| 394 | self.ftest('degrees(pi)', math.degrees(math.pi), 180.0) |
| 395 | self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0) |
| 396 | 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] | 397 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 398 | def testExp(self): |
| 399 | self.assertRaises(TypeError, math.exp) |
| 400 | self.ftest('exp(-1)', math.exp(-1), 1/math.e) |
| 401 | self.ftest('exp(0)', math.exp(0), 1) |
| 402 | self.ftest('exp(1)', math.exp(1), math.e) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 403 | self.assertEqual(math.exp(INF), INF) |
| 404 | self.assertEqual(math.exp(NINF), 0.) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 405 | self.assertTrue(math.isnan(math.exp(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 406 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 407 | def testFabs(self): |
| 408 | self.assertRaises(TypeError, math.fabs) |
| 409 | self.ftest('fabs(-1)', math.fabs(-1), 1) |
| 410 | self.ftest('fabs(0)', math.fabs(0), 0) |
| 411 | self.ftest('fabs(1)', math.fabs(1), 1) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 412 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 413 | def testFactorial(self): |
Mark Dickinson | 4c8a9a2 | 2010-05-15 17:02:38 +0000 | [diff] [blame] | 414 | self.assertEqual(math.factorial(0), 1) |
| 415 | self.assertEqual(math.factorial(0.0), 1) |
| 416 | total = 1 |
| 417 | for i in range(1, 1000): |
| 418 | total *= i |
| 419 | self.assertEqual(math.factorial(i), total) |
| 420 | self.assertEqual(math.factorial(float(i)), total) |
| 421 | self.assertEqual(math.factorial(i), py_factorial(i)) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 422 | self.assertRaises(ValueError, math.factorial, -1) |
Mark Dickinson | 4c8a9a2 | 2010-05-15 17:02:38 +0000 | [diff] [blame] | 423 | self.assertRaises(ValueError, math.factorial, -1.0) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 424 | self.assertRaises(ValueError, math.factorial, math.pi) |
Mark Dickinson | 4c8a9a2 | 2010-05-15 17:02:38 +0000 | [diff] [blame] | 425 | self.assertRaises(OverflowError, math.factorial, sys.maxsize+1) |
| 426 | self.assertRaises(OverflowError, math.factorial, 10e100) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 427 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 428 | def testFloor(self): |
| 429 | self.assertRaises(TypeError, math.floor) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 430 | self.assertEqual(int, type(math.floor(0.5))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 431 | self.ftest('floor(0.5)', math.floor(0.5), 0) |
| 432 | self.ftest('floor(1.0)', math.floor(1.0), 1) |
| 433 | self.ftest('floor(1.5)', math.floor(1.5), 1) |
| 434 | self.ftest('floor(-0.5)', math.floor(-0.5), -1) |
| 435 | self.ftest('floor(-1.0)', math.floor(-1.0), -1) |
| 436 | self.ftest('floor(-1.5)', math.floor(-1.5), -2) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 437 | # pow() relies on floor() to check for integers |
| 438 | # This fails on some platforms - so check it here |
| 439 | self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167) |
| 440 | self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 441 | #self.assertEqual(math.ceil(INF), INF) |
| 442 | #self.assertEqual(math.ceil(NINF), NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 443 | #self.assertTrue(math.isnan(math.floor(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 444 | |
Guido van Rossum | 13e05de | 2007-08-23 22:56:55 +0000 | [diff] [blame] | 445 | class TestFloor: |
| 446 | def __floor__(self): |
| 447 | return 42 |
| 448 | class TestNoFloor: |
| 449 | pass |
| 450 | self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42) |
| 451 | self.assertRaises(TypeError, math.floor, TestNoFloor()) |
| 452 | |
| 453 | t = TestNoFloor() |
| 454 | t.__floor__ = lambda *args: args |
| 455 | self.assertRaises(TypeError, math.floor, t) |
| 456 | self.assertRaises(TypeError, math.floor, t, 0) |
| 457 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 458 | def testFmod(self): |
| 459 | self.assertRaises(TypeError, math.fmod) |
| 460 | self.ftest('fmod(10,1)', math.fmod(10,1), 0) |
| 461 | self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0) |
| 462 | self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1) |
| 463 | self.ftest('fmod(-10,1)', math.fmod(-10,1), 0) |
| 464 | self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0) |
| 465 | self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 466 | self.assertTrue(math.isnan(math.fmod(NAN, 1.))) |
| 467 | self.assertTrue(math.isnan(math.fmod(1., NAN))) |
| 468 | self.assertTrue(math.isnan(math.fmod(NAN, NAN))) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 469 | self.assertRaises(ValueError, math.fmod, 1., 0.) |
| 470 | self.assertRaises(ValueError, math.fmod, INF, 1.) |
| 471 | self.assertRaises(ValueError, math.fmod, NINF, 1.) |
| 472 | self.assertRaises(ValueError, math.fmod, INF, 0.) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 473 | self.assertEqual(math.fmod(3.0, INF), 3.0) |
| 474 | self.assertEqual(math.fmod(-3.0, INF), -3.0) |
| 475 | self.assertEqual(math.fmod(3.0, NINF), 3.0) |
| 476 | self.assertEqual(math.fmod(-3.0, NINF), -3.0) |
| 477 | self.assertEqual(math.fmod(0.0, 3.0), 0.0) |
| 478 | self.assertEqual(math.fmod(0.0, NINF), 0.0) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 479 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 480 | def testFrexp(self): |
| 481 | self.assertRaises(TypeError, math.frexp) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 482 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 483 | def testfrexp(name, result, expected): |
| 484 | (mant, exp), (emant, eexp) = result, expected |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 485 | if abs(mant-emant) > eps or exp != eexp: |
| 486 | self.fail('%s returned %r, expected %r'%\ |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 487 | (name, result, expected)) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 488 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 489 | testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1)) |
| 490 | testfrexp('frexp(0)', math.frexp(0), (0, 0)) |
| 491 | testfrexp('frexp(1)', math.frexp(1), (0.5, 1)) |
| 492 | testfrexp('frexp(2)', math.frexp(2), (0.5, 2)) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 493 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 494 | self.assertEqual(math.frexp(INF)[0], INF) |
| 495 | self.assertEqual(math.frexp(NINF)[0], NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 496 | self.assertTrue(math.isnan(math.frexp(NAN)[0])) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 497 | |
Mark Dickinson | 6356623 | 2009-09-18 21:04:19 +0000 | [diff] [blame] | 498 | @requires_IEEE_754 |
Mark Dickinson | 5c56708 | 2009-04-24 16:39:07 +0000 | [diff] [blame] | 499 | @unittest.skipIf(HAVE_DOUBLE_ROUNDING, |
| 500 | "fsum is not exact on machines with double rounding") |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 501 | def testFsum(self): |
| 502 | # math.fsum relies on exact rounding for correct operation. |
| 503 | # There's a known problem with IA32 floating-point that causes |
| 504 | # inexact rounding in some situations, and will cause the |
| 505 | # math.fsum tests below to fail; see issue #2937. On non IEEE |
| 506 | # 754 platforms, and on IEEE 754 platforms that exhibit the |
| 507 | # problem described in issue #2937, we simply skip the whole |
| 508 | # test. |
| 509 | |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 510 | # Python version of math.fsum, for comparison. Uses a |
| 511 | # different algorithm based on frexp, ldexp and integer |
| 512 | # arithmetic. |
| 513 | from sys import float_info |
| 514 | mant_dig = float_info.mant_dig |
| 515 | etiny = float_info.min_exp - mant_dig |
| 516 | |
| 517 | def msum(iterable): |
| 518 | """Full precision summation. Compute sum(iterable) without any |
| 519 | intermediate accumulation of error. Based on the 'lsum' function |
| 520 | at http://code.activestate.com/recipes/393090/ |
| 521 | |
| 522 | """ |
| 523 | tmant, texp = 0, 0 |
| 524 | for x in iterable: |
| 525 | mant, exp = math.frexp(x) |
| 526 | mant, exp = int(math.ldexp(mant, mant_dig)), exp - mant_dig |
| 527 | if texp > exp: |
| 528 | tmant <<= texp-exp |
| 529 | texp = exp |
| 530 | else: |
| 531 | mant <<= exp-texp |
| 532 | tmant += mant |
| 533 | # Round tmant * 2**texp to a float. The original recipe |
| 534 | # used float(str(tmant)) * 2.0**texp for this, but that's |
| 535 | # a little unsafe because str -> float conversion can't be |
| 536 | # relied upon to do correct rounding on all platforms. |
| 537 | tail = max(len(bin(abs(tmant)))-2 - mant_dig, etiny - texp) |
| 538 | if tail > 0: |
| 539 | h = 1 << (tail-1) |
| 540 | tmant = tmant // (2*h) + bool(tmant & h and tmant & 3*h-1) |
| 541 | texp += tail |
| 542 | return math.ldexp(tmant, texp) |
| 543 | |
| 544 | test_values = [ |
| 545 | ([], 0.0), |
| 546 | ([0.0], 0.0), |
| 547 | ([1e100, 1.0, -1e100, 1e-100, 1e50, -1.0, -1e50], 1e-100), |
| 548 | ([2.0**53, -0.5, -2.0**-54], 2.0**53-1.0), |
| 549 | ([2.0**53, 1.0, 2.0**-100], 2.0**53+2.0), |
| 550 | ([2.0**53+10.0, 1.0, 2.0**-100], 2.0**53+12.0), |
| 551 | ([2.0**53-4.0, 0.5, 2.0**-54], 2.0**53-3.0), |
| 552 | ([1./n for n in range(1, 1001)], |
| 553 | float.fromhex('0x1.df11f45f4e61ap+2')), |
| 554 | ([(-1.)**n/n for n in range(1, 1001)], |
| 555 | float.fromhex('-0x1.62a2af1bd3624p-1')), |
| 556 | ([1.7**(i+1)-1.7**i for i in range(1000)] + [-1.7**1000], -1.0), |
| 557 | ([1e16, 1., 1e-16], 10000000000000002.0), |
| 558 | ([1e16-2., 1.-2.**-53, -(1e16-2.), -(1.-2.**-53)], 0.0), |
| 559 | # exercise code for resizing partials array |
| 560 | ([2.**n - 2.**(n+50) + 2.**(n+52) for n in range(-1074, 972, 2)] + |
| 561 | [-2.**1022], |
| 562 | float.fromhex('0x1.5555555555555p+970')), |
| 563 | ] |
| 564 | |
| 565 | for i, (vals, expected) in enumerate(test_values): |
| 566 | try: |
| 567 | actual = math.fsum(vals) |
| 568 | except OverflowError: |
| 569 | self.fail("test %d failed: got OverflowError, expected %r " |
| 570 | "for math.fsum(%.100r)" % (i, expected, vals)) |
| 571 | except ValueError: |
| 572 | self.fail("test %d failed: got ValueError, expected %r " |
| 573 | "for math.fsum(%.100r)" % (i, expected, vals)) |
| 574 | self.assertEqual(actual, expected) |
| 575 | |
| 576 | from random import random, gauss, shuffle |
| 577 | for j in range(1000): |
| 578 | vals = [7, 1e100, -7, -1e100, -9e-20, 8e-20] * 10 |
| 579 | s = 0 |
| 580 | for i in range(200): |
| 581 | v = gauss(0, random()) ** 7 - s |
| 582 | s += v |
| 583 | vals.append(v) |
| 584 | shuffle(vals) |
| 585 | |
| 586 | s = msum(vals) |
| 587 | self.assertEqual(msum(vals), math.fsum(vals)) |
| 588 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 589 | def testHypot(self): |
| 590 | self.assertRaises(TypeError, math.hypot) |
| 591 | self.ftest('hypot(0,0)', math.hypot(0,0), 0) |
| 592 | self.ftest('hypot(3,4)', math.hypot(3,4), 5) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 593 | self.assertEqual(math.hypot(NAN, INF), INF) |
| 594 | self.assertEqual(math.hypot(INF, NAN), INF) |
| 595 | self.assertEqual(math.hypot(NAN, NINF), INF) |
| 596 | self.assertEqual(math.hypot(NINF, NAN), INF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 597 | self.assertTrue(math.isnan(math.hypot(1.0, NAN))) |
| 598 | self.assertTrue(math.isnan(math.hypot(NAN, -2.0))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 599 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 600 | def testLdexp(self): |
| 601 | self.assertRaises(TypeError, math.ldexp) |
| 602 | self.ftest('ldexp(0,1)', math.ldexp(0,1), 0) |
| 603 | self.ftest('ldexp(1,1)', math.ldexp(1,1), 2) |
| 604 | self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5) |
| 605 | self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 606 | self.assertRaises(OverflowError, math.ldexp, 1., 1000000) |
| 607 | self.assertRaises(OverflowError, math.ldexp, -1., 1000000) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 608 | self.assertEqual(math.ldexp(1., -1000000), 0.) |
| 609 | self.assertEqual(math.ldexp(-1., -1000000), -0.) |
| 610 | self.assertEqual(math.ldexp(INF, 30), INF) |
| 611 | self.assertEqual(math.ldexp(NINF, -213), NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 612 | self.assertTrue(math.isnan(math.ldexp(NAN, 0))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 613 | |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 614 | # large second argument |
| 615 | for n in [10**5, 10**10, 10**20, 10**40]: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 616 | self.assertEqual(math.ldexp(INF, -n), INF) |
| 617 | self.assertEqual(math.ldexp(NINF, -n), NINF) |
| 618 | self.assertEqual(math.ldexp(1., -n), 0.) |
| 619 | self.assertEqual(math.ldexp(-1., -n), -0.) |
| 620 | self.assertEqual(math.ldexp(0., -n), 0.) |
| 621 | self.assertEqual(math.ldexp(-0., -n), -0.) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 622 | self.assertTrue(math.isnan(math.ldexp(NAN, -n))) |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 623 | |
| 624 | self.assertRaises(OverflowError, math.ldexp, 1., n) |
| 625 | self.assertRaises(OverflowError, math.ldexp, -1., n) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 626 | self.assertEqual(math.ldexp(0., n), 0.) |
| 627 | self.assertEqual(math.ldexp(-0., n), -0.) |
| 628 | self.assertEqual(math.ldexp(INF, n), INF) |
| 629 | self.assertEqual(math.ldexp(NINF, n), NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 630 | self.assertTrue(math.isnan(math.ldexp(NAN, n))) |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 631 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 632 | def testLog(self): |
| 633 | self.assertRaises(TypeError, math.log) |
| 634 | self.ftest('log(1/e)', math.log(1/math.e), -1) |
| 635 | self.ftest('log(1)', math.log(1), 0) |
| 636 | self.ftest('log(e)', math.log(math.e), 1) |
| 637 | self.ftest('log(32,2)', math.log(32,2), 5) |
| 638 | self.ftest('log(10**40, 10)', math.log(10**40, 10), 40) |
| 639 | self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2) |
Mark Dickinson | c603717 | 2010-09-29 19:06:36 +0000 | [diff] [blame] | 640 | self.ftest('log(10**1000)', math.log(10**1000), |
| 641 | 2302.5850929940457) |
| 642 | self.assertRaises(ValueError, math.log, -1.5) |
| 643 | self.assertRaises(ValueError, math.log, -10**1000) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 644 | self.assertRaises(ValueError, math.log, NINF) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 645 | self.assertEqual(math.log(INF), INF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 646 | self.assertTrue(math.isnan(math.log(NAN))) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 647 | |
| 648 | def testLog1p(self): |
| 649 | self.assertRaises(TypeError, math.log1p) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 650 | n= 2**90 |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 651 | self.assertAlmostEqual(math.log1p(n), math.log1p(float(n))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 652 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 653 | def testLog10(self): |
| 654 | self.assertRaises(TypeError, math.log10) |
| 655 | self.ftest('log10(0.1)', math.log10(0.1), -1) |
| 656 | self.ftest('log10(1)', math.log10(1), 0) |
| 657 | self.ftest('log10(10)', math.log10(10), 1) |
Mark Dickinson | c603717 | 2010-09-29 19:06:36 +0000 | [diff] [blame] | 658 | self.ftest('log10(10**1000)', math.log10(10**1000), 1000.0) |
| 659 | self.assertRaises(ValueError, math.log10, -1.5) |
| 660 | self.assertRaises(ValueError, math.log10, -10**1000) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 661 | self.assertRaises(ValueError, math.log10, NINF) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 662 | self.assertEqual(math.log(INF), INF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 663 | self.assertTrue(math.isnan(math.log10(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 664 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 665 | def testModf(self): |
| 666 | self.assertRaises(TypeError, math.modf) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 667 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 668 | def testmodf(name, result, expected): |
| 669 | (v1, v2), (e1, e2) = result, expected |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 670 | if abs(v1-e1) > eps or abs(v2-e2): |
| 671 | self.fail('%s returned %r, expected %r'%\ |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 672 | (name, result, expected)) |
Raymond Hettinger | 64108af | 2002-05-13 03:55:01 +0000 | [diff] [blame] | 673 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 674 | testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0)) |
| 675 | 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] | 676 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 677 | self.assertEqual(math.modf(INF), (0.0, INF)) |
| 678 | self.assertEqual(math.modf(NINF), (-0.0, NINF)) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 679 | |
| 680 | modf_nan = math.modf(NAN) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 681 | self.assertTrue(math.isnan(modf_nan[0])) |
| 682 | self.assertTrue(math.isnan(modf_nan[1])) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 683 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 684 | def testPow(self): |
| 685 | self.assertRaises(TypeError, math.pow) |
| 686 | self.ftest('pow(0,1)', math.pow(0,1), 0) |
| 687 | self.ftest('pow(1,0)', math.pow(1,0), 1) |
| 688 | self.ftest('pow(2,1)', math.pow(2,1), 2) |
| 689 | self.ftest('pow(2,-1)', math.pow(2,-1), 0.5) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 690 | self.assertEqual(math.pow(INF, 1), INF) |
| 691 | self.assertEqual(math.pow(NINF, 1), NINF) |
| 692 | self.assertEqual((math.pow(1, INF)), 1.) |
| 693 | self.assertEqual((math.pow(1, NINF)), 1.) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 694 | self.assertTrue(math.isnan(math.pow(NAN, 1))) |
| 695 | self.assertTrue(math.isnan(math.pow(2, NAN))) |
| 696 | self.assertTrue(math.isnan(math.pow(0, NAN))) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 697 | self.assertEqual(math.pow(1, NAN), 1) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 698 | |
| 699 | # pow(0., x) |
| 700 | self.assertEqual(math.pow(0., INF), 0.) |
| 701 | self.assertEqual(math.pow(0., 3.), 0.) |
| 702 | self.assertEqual(math.pow(0., 2.3), 0.) |
| 703 | self.assertEqual(math.pow(0., 2.), 0.) |
| 704 | self.assertEqual(math.pow(0., 0.), 1.) |
| 705 | self.assertEqual(math.pow(0., -0.), 1.) |
| 706 | self.assertRaises(ValueError, math.pow, 0., -2.) |
| 707 | self.assertRaises(ValueError, math.pow, 0., -2.3) |
| 708 | self.assertRaises(ValueError, math.pow, 0., -3.) |
| 709 | self.assertRaises(ValueError, math.pow, 0., NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 710 | self.assertTrue(math.isnan(math.pow(0., NAN))) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 711 | |
| 712 | # pow(INF, x) |
| 713 | self.assertEqual(math.pow(INF, INF), INF) |
| 714 | self.assertEqual(math.pow(INF, 3.), INF) |
| 715 | self.assertEqual(math.pow(INF, 2.3), INF) |
| 716 | self.assertEqual(math.pow(INF, 2.), INF) |
| 717 | self.assertEqual(math.pow(INF, 0.), 1.) |
| 718 | self.assertEqual(math.pow(INF, -0.), 1.) |
| 719 | self.assertEqual(math.pow(INF, -2.), 0.) |
| 720 | self.assertEqual(math.pow(INF, -2.3), 0.) |
| 721 | self.assertEqual(math.pow(INF, -3.), 0.) |
| 722 | self.assertEqual(math.pow(INF, NINF), 0.) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 723 | self.assertTrue(math.isnan(math.pow(INF, NAN))) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 724 | |
| 725 | # pow(-0., x) |
| 726 | self.assertEqual(math.pow(-0., INF), 0.) |
| 727 | self.assertEqual(math.pow(-0., 3.), -0.) |
| 728 | self.assertEqual(math.pow(-0., 2.3), 0.) |
| 729 | self.assertEqual(math.pow(-0., 2.), 0.) |
| 730 | self.assertEqual(math.pow(-0., 0.), 1.) |
| 731 | self.assertEqual(math.pow(-0., -0.), 1.) |
| 732 | self.assertRaises(ValueError, math.pow, -0., -2.) |
| 733 | self.assertRaises(ValueError, math.pow, -0., -2.3) |
| 734 | self.assertRaises(ValueError, math.pow, -0., -3.) |
| 735 | self.assertRaises(ValueError, math.pow, -0., NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 736 | self.assertTrue(math.isnan(math.pow(-0., NAN))) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 737 | |
| 738 | # pow(NINF, x) |
| 739 | self.assertEqual(math.pow(NINF, INF), INF) |
| 740 | self.assertEqual(math.pow(NINF, 3.), NINF) |
| 741 | self.assertEqual(math.pow(NINF, 2.3), INF) |
| 742 | self.assertEqual(math.pow(NINF, 2.), INF) |
| 743 | self.assertEqual(math.pow(NINF, 0.), 1.) |
| 744 | self.assertEqual(math.pow(NINF, -0.), 1.) |
| 745 | self.assertEqual(math.pow(NINF, -2.), 0.) |
| 746 | self.assertEqual(math.pow(NINF, -2.3), 0.) |
| 747 | self.assertEqual(math.pow(NINF, -3.), -0.) |
| 748 | self.assertEqual(math.pow(NINF, NINF), 0.) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 749 | self.assertTrue(math.isnan(math.pow(NINF, NAN))) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 750 | |
| 751 | # pow(-1, x) |
| 752 | self.assertEqual(math.pow(-1., INF), 1.) |
| 753 | self.assertEqual(math.pow(-1., 3.), -1.) |
| 754 | self.assertRaises(ValueError, math.pow, -1., 2.3) |
| 755 | self.assertEqual(math.pow(-1., 2.), 1.) |
| 756 | self.assertEqual(math.pow(-1., 0.), 1.) |
| 757 | self.assertEqual(math.pow(-1., -0.), 1.) |
| 758 | self.assertEqual(math.pow(-1., -2.), 1.) |
| 759 | self.assertRaises(ValueError, math.pow, -1., -2.3) |
| 760 | self.assertEqual(math.pow(-1., -3.), -1.) |
| 761 | self.assertEqual(math.pow(-1., NINF), 1.) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 762 | self.assertTrue(math.isnan(math.pow(-1., NAN))) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 763 | |
| 764 | # pow(1, x) |
| 765 | self.assertEqual(math.pow(1., INF), 1.) |
| 766 | self.assertEqual(math.pow(1., 3.), 1.) |
| 767 | self.assertEqual(math.pow(1., 2.3), 1.) |
| 768 | self.assertEqual(math.pow(1., 2.), 1.) |
| 769 | self.assertEqual(math.pow(1., 0.), 1.) |
| 770 | self.assertEqual(math.pow(1., -0.), 1.) |
| 771 | self.assertEqual(math.pow(1., -2.), 1.) |
| 772 | self.assertEqual(math.pow(1., -2.3), 1.) |
| 773 | self.assertEqual(math.pow(1., -3.), 1.) |
| 774 | self.assertEqual(math.pow(1., NINF), 1.) |
| 775 | self.assertEqual(math.pow(1., NAN), 1.) |
| 776 | |
| 777 | # pow(x, 0) should be 1 for any x |
| 778 | self.assertEqual(math.pow(2.3, 0.), 1.) |
| 779 | self.assertEqual(math.pow(-2.3, 0.), 1.) |
| 780 | self.assertEqual(math.pow(NAN, 0.), 1.) |
| 781 | self.assertEqual(math.pow(2.3, -0.), 1.) |
| 782 | self.assertEqual(math.pow(-2.3, -0.), 1.) |
| 783 | self.assertEqual(math.pow(NAN, -0.), 1.) |
| 784 | |
| 785 | # pow(x, y) is invalid if x is negative and y is not integral |
| 786 | self.assertRaises(ValueError, math.pow, -1., 2.3) |
| 787 | self.assertRaises(ValueError, math.pow, -15., -3.1) |
| 788 | |
| 789 | # pow(x, NINF) |
| 790 | self.assertEqual(math.pow(1.9, NINF), 0.) |
| 791 | self.assertEqual(math.pow(1.1, NINF), 0.) |
| 792 | self.assertEqual(math.pow(0.9, NINF), INF) |
| 793 | self.assertEqual(math.pow(0.1, NINF), INF) |
| 794 | self.assertEqual(math.pow(-0.1, NINF), INF) |
| 795 | self.assertEqual(math.pow(-0.9, NINF), INF) |
| 796 | self.assertEqual(math.pow(-1.1, NINF), 0.) |
| 797 | self.assertEqual(math.pow(-1.9, NINF), 0.) |
| 798 | |
| 799 | # pow(x, INF) |
| 800 | self.assertEqual(math.pow(1.9, INF), INF) |
| 801 | self.assertEqual(math.pow(1.1, INF), INF) |
| 802 | self.assertEqual(math.pow(0.9, INF), 0.) |
| 803 | self.assertEqual(math.pow(0.1, INF), 0.) |
| 804 | self.assertEqual(math.pow(-0.1, INF), 0.) |
| 805 | self.assertEqual(math.pow(-0.9, INF), 0.) |
| 806 | self.assertEqual(math.pow(-1.1, INF), INF) |
| 807 | self.assertEqual(math.pow(-1.9, INF), INF) |
| 808 | |
| 809 | # pow(x, y) should work for x negative, y an integer |
| 810 | self.ftest('(-2.)**3.', math.pow(-2.0, 3.0), -8.0) |
| 811 | self.ftest('(-2.)**2.', math.pow(-2.0, 2.0), 4.0) |
| 812 | self.ftest('(-2.)**1.', math.pow(-2.0, 1.0), -2.0) |
| 813 | self.ftest('(-2.)**0.', math.pow(-2.0, 0.0), 1.0) |
| 814 | self.ftest('(-2.)**-0.', math.pow(-2.0, -0.0), 1.0) |
| 815 | self.ftest('(-2.)**-1.', math.pow(-2.0, -1.0), -0.5) |
| 816 | self.ftest('(-2.)**-2.', math.pow(-2.0, -2.0), 0.25) |
| 817 | self.ftest('(-2.)**-3.', math.pow(-2.0, -3.0), -0.125) |
| 818 | self.assertRaises(ValueError, math.pow, -2.0, -0.5) |
| 819 | self.assertRaises(ValueError, math.pow, -2.0, 0.5) |
| 820 | |
| 821 | # the following tests have been commented out since they don't |
| 822 | # really belong here: the implementation of ** for floats is |
| 823 | # independent of the implemention of math.pow |
| 824 | #self.assertEqual(1**NAN, 1) |
| 825 | #self.assertEqual(1**INF, 1) |
| 826 | #self.assertEqual(1**NINF, 1) |
| 827 | #self.assertEqual(1**0, 1) |
| 828 | #self.assertEqual(1.**NAN, 1) |
| 829 | #self.assertEqual(1.**INF, 1) |
| 830 | #self.assertEqual(1.**NINF, 1) |
| 831 | #self.assertEqual(1.**0, 1) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 832 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 833 | def testRadians(self): |
| 834 | self.assertRaises(TypeError, math.radians) |
| 835 | self.ftest('radians(180)', math.radians(180), math.pi) |
| 836 | self.ftest('radians(90)', math.radians(90), math.pi/2) |
| 837 | self.ftest('radians(-45)', math.radians(-45), -math.pi/4) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 838 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 839 | def testSin(self): |
| 840 | self.assertRaises(TypeError, math.sin) |
| 841 | self.ftest('sin(0)', math.sin(0), 0) |
| 842 | self.ftest('sin(pi/2)', math.sin(math.pi/2), 1) |
| 843 | self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 844 | try: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 845 | self.assertTrue(math.isnan(math.sin(INF))) |
| 846 | self.assertTrue(math.isnan(math.sin(NINF))) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 847 | except ValueError: |
| 848 | self.assertRaises(ValueError, math.sin, INF) |
| 849 | self.assertRaises(ValueError, math.sin, NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 850 | self.assertTrue(math.isnan(math.sin(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 851 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 852 | def testSinh(self): |
| 853 | self.assertRaises(TypeError, math.sinh) |
| 854 | self.ftest('sinh(0)', math.sinh(0), 0) |
| 855 | self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1) |
| 856 | self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 857 | self.assertEqual(math.sinh(INF), INF) |
| 858 | self.assertEqual(math.sinh(NINF), NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 859 | self.assertTrue(math.isnan(math.sinh(NAN))) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 860 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 861 | def testSqrt(self): |
| 862 | self.assertRaises(TypeError, math.sqrt) |
| 863 | self.ftest('sqrt(0)', math.sqrt(0), 0) |
| 864 | self.ftest('sqrt(1)', math.sqrt(1), 1) |
| 865 | self.ftest('sqrt(4)', math.sqrt(4), 2) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 866 | self.assertEqual(math.sqrt(INF), INF) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 867 | self.assertRaises(ValueError, math.sqrt, NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 868 | self.assertTrue(math.isnan(math.sqrt(NAN))) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 869 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 870 | def testTan(self): |
| 871 | self.assertRaises(TypeError, math.tan) |
| 872 | self.ftest('tan(0)', math.tan(0), 0) |
| 873 | self.ftest('tan(pi/4)', math.tan(math.pi/4), 1) |
| 874 | self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 875 | try: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 876 | self.assertTrue(math.isnan(math.tan(INF))) |
| 877 | self.assertTrue(math.isnan(math.tan(NINF))) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 878 | except: |
| 879 | self.assertRaises(ValueError, math.tan, INF) |
| 880 | self.assertRaises(ValueError, math.tan, NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 881 | self.assertTrue(math.isnan(math.tan(NAN))) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 882 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 883 | def testTanh(self): |
| 884 | self.assertRaises(TypeError, math.tanh) |
| 885 | self.ftest('tanh(0)', math.tanh(0), 0) |
| 886 | 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] | 887 | self.ftest('tanh(inf)', math.tanh(INF), 1) |
| 888 | self.ftest('tanh(-inf)', math.tanh(NINF), -1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 889 | self.assertTrue(math.isnan(math.tanh(NAN))) |
Victor Stinner | be3da38 | 2010-11-07 14:14:27 +0000 | [diff] [blame] | 890 | |
| 891 | @requires_IEEE_754 |
| 892 | @unittest.skipIf(sysconfig.get_config_var('TANH_PRESERVES_ZERO_SIGN') == 0, |
| 893 | "system tanh() function doesn't copy the sign") |
| 894 | def testTanhSign(self): |
Christian Heimes | e57950f | 2008-04-21 13:08:03 +0000 | [diff] [blame] | 895 | # check that tanh(-0.) == -0. on IEEE 754 systems |
Victor Stinner | be3da38 | 2010-11-07 14:14:27 +0000 | [diff] [blame] | 896 | self.assertEqual(math.tanh(-0.), -0.) |
| 897 | self.assertEqual(math.copysign(1., math.tanh(-0.)), |
| 898 | math.copysign(1., -0.)) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 899 | |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 900 | def test_trunc(self): |
| 901 | self.assertEqual(math.trunc(1), 1) |
| 902 | self.assertEqual(math.trunc(-1), -1) |
| 903 | self.assertEqual(type(math.trunc(1)), int) |
| 904 | self.assertEqual(type(math.trunc(1.5)), int) |
| 905 | self.assertEqual(math.trunc(1.5), 1) |
| 906 | self.assertEqual(math.trunc(-1.5), -1) |
| 907 | self.assertEqual(math.trunc(1.999999), 1) |
| 908 | self.assertEqual(math.trunc(-1.999999), -1) |
| 909 | self.assertEqual(math.trunc(-0.999999), -0) |
| 910 | self.assertEqual(math.trunc(-100.999), -100) |
| 911 | |
| 912 | class TestTrunc(object): |
| 913 | def __trunc__(self): |
| 914 | return 23 |
| 915 | |
| 916 | class TestNoTrunc(object): |
| 917 | pass |
| 918 | |
| 919 | self.assertEqual(math.trunc(TestTrunc()), 23) |
| 920 | |
| 921 | self.assertRaises(TypeError, math.trunc) |
| 922 | self.assertRaises(TypeError, math.trunc, 1, 2) |
| 923 | self.assertRaises(TypeError, math.trunc, TestNoTrunc()) |
| 924 | |
Mark Dickinson | 8e0c996 | 2010-07-11 17:38:24 +0000 | [diff] [blame] | 925 | def testIsfinite(self): |
| 926 | self.assertTrue(math.isfinite(0.0)) |
| 927 | self.assertTrue(math.isfinite(-0.0)) |
| 928 | self.assertTrue(math.isfinite(1.0)) |
| 929 | self.assertTrue(math.isfinite(-1.0)) |
| 930 | self.assertFalse(math.isfinite(float("nan"))) |
| 931 | self.assertFalse(math.isfinite(float("inf"))) |
| 932 | self.assertFalse(math.isfinite(float("-inf"))) |
| 933 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 934 | def testIsnan(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 935 | self.assertTrue(math.isnan(float("nan"))) |
| 936 | self.assertTrue(math.isnan(float("inf")* 0.)) |
| 937 | self.assertFalse(math.isnan(float("inf"))) |
| 938 | self.assertFalse(math.isnan(0.)) |
| 939 | self.assertFalse(math.isnan(1.)) |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 940 | |
| 941 | def testIsinf(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 942 | self.assertTrue(math.isinf(float("inf"))) |
| 943 | self.assertTrue(math.isinf(float("-inf"))) |
| 944 | self.assertTrue(math.isinf(1E400)) |
| 945 | self.assertTrue(math.isinf(-1E400)) |
| 946 | self.assertFalse(math.isinf(float("nan"))) |
| 947 | self.assertFalse(math.isinf(0.)) |
| 948 | self.assertFalse(math.isinf(1.)) |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 949 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 950 | # RED_FLAG 16-Oct-2000 Tim |
| 951 | # While 2.0 is more consistent about exceptions than previous releases, it |
| 952 | # still fails this part of the test on some platforms. For now, we only |
| 953 | # *run* test_exceptions() in verbose mode, so that this isn't normally |
| 954 | # tested. |
Tim Peters | 98c8184 | 2000-10-16 17:35:13 +0000 | [diff] [blame] | 955 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 956 | if verbose: |
| 957 | def test_exceptions(self): |
| 958 | try: |
| 959 | x = math.exp(-1000000000) |
| 960 | except: |
| 961 | # mathmodule.c is failing to weed out underflows from libm, or |
| 962 | # we've got an fp format with huge dynamic range |
| 963 | self.fail("underflowing exp() should not have raised " |
| 964 | "an exception") |
| 965 | if x != 0: |
| 966 | self.fail("underflowing exp() should have returned 0") |
| 967 | |
| 968 | # If this fails, probably using a strict IEEE-754 conforming libm, and x |
| 969 | # is +Inf afterwards. But Python wants overflows detected by default. |
| 970 | try: |
| 971 | x = math.exp(1000000000) |
| 972 | except OverflowError: |
| 973 | pass |
| 974 | else: |
| 975 | self.fail("overflowing exp() didn't trigger OverflowError") |
| 976 | |
| 977 | # If this fails, it could be a puzzle. One odd possibility is that |
| 978 | # mathmodule.c's macros are getting confused while comparing |
| 979 | # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE |
| 980 | # as a result (and so raising OverflowError instead). |
| 981 | try: |
| 982 | x = math.sqrt(-1.0) |
| 983 | except ValueError: |
| 984 | pass |
| 985 | else: |
| 986 | self.fail("sqrt(-1) didn't raise ValueError") |
| 987 | |
Mark Dickinson | 6356623 | 2009-09-18 21:04:19 +0000 | [diff] [blame] | 988 | @requires_IEEE_754 |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 989 | def test_testfile(self): |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 990 | for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file): |
| 991 | # Skip if either the input or result is complex, or if |
| 992 | # flags is nonempty |
| 993 | if ai != 0. or ei != 0. or flags: |
| 994 | continue |
| 995 | if fn in ['rect', 'polar']: |
| 996 | # no real versions of rect, polar |
| 997 | continue |
| 998 | func = getattr(math, fn) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 999 | try: |
| 1000 | result = func(ar) |
Mark Dickinson | a0de26c | 2008-04-30 23:30:57 +0000 | [diff] [blame] | 1001 | except ValueError as exc: |
| 1002 | message = (("Unexpected ValueError: %s\n " + |
| 1003 | "in test %s:%s(%r)\n") % (exc.args[0], id, fn, ar)) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1004 | self.fail(message) |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 1005 | except OverflowError: |
| 1006 | message = ("Unexpected OverflowError in " + |
| 1007 | "test %s:%s(%r)\n" % (id, fn, ar)) |
| 1008 | self.fail(message) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1009 | self.ftest("%s:%s(%r)" % (id, fn, ar), result, er) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1010 | |
Victor Stinner | be3da38 | 2010-11-07 14:14:27 +0000 | [diff] [blame] | 1011 | @requires_IEEE_754 |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 1012 | def test_mtestfile(self): |
| 1013 | ALLOWED_ERROR = 20 # permitted error, in ulps |
| 1014 | fail_fmt = "{}:{}({!r}): expected {!r}, got {!r}" |
| 1015 | |
| 1016 | failures = [] |
| 1017 | for id, fn, arg, expected, flags in parse_mtestfile(math_testcases): |
| 1018 | func = getattr(math, fn) |
| 1019 | |
| 1020 | if 'invalid' in flags or 'divide-by-zero' in flags: |
| 1021 | expected = 'ValueError' |
| 1022 | elif 'overflow' in flags: |
| 1023 | expected = 'OverflowError' |
| 1024 | |
| 1025 | try: |
| 1026 | got = func(arg) |
| 1027 | except ValueError: |
| 1028 | got = 'ValueError' |
| 1029 | except OverflowError: |
| 1030 | got = 'OverflowError' |
| 1031 | |
Mark Dickinson | 05d2e08 | 2009-12-11 20:17:17 +0000 | [diff] [blame] | 1032 | accuracy_failure = None |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 1033 | if isinstance(got, float) and isinstance(expected, float): |
| 1034 | if math.isnan(expected) and math.isnan(got): |
| 1035 | continue |
| 1036 | if not math.isnan(expected) and not math.isnan(got): |
Mark Dickinson | 664b511 | 2009-12-16 20:23:42 +0000 | [diff] [blame] | 1037 | if fn == 'lgamma': |
| 1038 | # we use a weaker accuracy test for lgamma; |
| 1039 | # lgamma only achieves an absolute error of |
| 1040 | # a few multiples of the machine accuracy, in |
| 1041 | # general. |
Mark Dickinson | 05d2e08 | 2009-12-11 20:17:17 +0000 | [diff] [blame] | 1042 | accuracy_failure = acc_check(expected, got, |
| 1043 | rel_err = 5e-15, |
| 1044 | abs_err = 5e-15) |
Mark Dickinson | bcdf9da | 2010-06-13 10:52:38 +0000 | [diff] [blame] | 1045 | elif fn == 'erfc': |
| 1046 | # erfc has less-than-ideal accuracy for large |
| 1047 | # arguments (x ~ 25 or so), mainly due to the |
| 1048 | # error involved in computing exp(-x*x). |
| 1049 | # |
| 1050 | # XXX Would be better to weaken this test only |
| 1051 | # for large x, instead of for all x. |
| 1052 | accuracy_failure = ulps_check(expected, got, 2000) |
| 1053 | |
Mark Dickinson | 05d2e08 | 2009-12-11 20:17:17 +0000 | [diff] [blame] | 1054 | else: |
Mark Dickinson | 664b511 | 2009-12-16 20:23:42 +0000 | [diff] [blame] | 1055 | accuracy_failure = ulps_check(expected, got, 20) |
Mark Dickinson | 05d2e08 | 2009-12-11 20:17:17 +0000 | [diff] [blame] | 1056 | if accuracy_failure is None: |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 1057 | continue |
| 1058 | |
| 1059 | if isinstance(got, str) and isinstance(expected, str): |
| 1060 | if got == expected: |
| 1061 | continue |
| 1062 | |
| 1063 | fail_msg = fail_fmt.format(id, fn, arg, expected, got) |
Mark Dickinson | 05d2e08 | 2009-12-11 20:17:17 +0000 | [diff] [blame] | 1064 | if accuracy_failure is not None: |
| 1065 | fail_msg += ' ({})'.format(accuracy_failure) |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 1066 | failures.append(fail_msg) |
| 1067 | |
| 1068 | if failures: |
| 1069 | self.fail('Failures in test_mtestfile:\n ' + |
| 1070 | '\n '.join(failures)) |
| 1071 | |
| 1072 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1073 | def test_main(): |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1074 | from doctest import DocFileSuite |
| 1075 | suite = unittest.TestSuite() |
| 1076 | suite.addTest(unittest.makeSuite(MathTests)) |
| 1077 | suite.addTest(DocFileSuite("ieee754.txt")) |
| 1078 | run_unittest(suite) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1079 | |
| 1080 | if __name__ == '__main__': |
| 1081 | test_main() |