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