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 |
Raymond Hettinger | 9c18b1a | 2018-07-31 00:45:49 -0700 | [diff] [blame] | 7 | import itertools |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 8 | import math |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 9 | import os |
Mark Dickinson | 8574654 | 2016-09-04 09:58:51 +0100 | [diff] [blame] | 10 | import platform |
Raymond Hettinger | 9c18b1a | 2018-07-31 00:45:49 -0700 | [diff] [blame] | 11 | import random |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 12 | import struct |
Mark Dickinson | 8574654 | 2016-09-04 09:58:51 +0100 | [diff] [blame] | 13 | import sys |
Victor Stinner | be3da38 | 2010-11-07 14:14:27 +0000 | [diff] [blame] | 14 | import sysconfig |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 15 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 16 | eps = 1E-05 |
| 17 | NAN = float('nan') |
| 18 | INF = float('inf') |
| 19 | NINF = float('-inf') |
Mark Dickinson | 31ba1c3 | 2016-09-04 12:29:14 +0100 | [diff] [blame] | 20 | FLOAT_MAX = sys.float_info.max |
Raymond Hettinger | c6dabe3 | 2018-07-28 07:48:04 -0700 | [diff] [blame] | 21 | FLOAT_MIN = sys.float_info.min |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 22 | |
Mark Dickinson | 5c56708 | 2009-04-24 16:39:07 +0000 | [diff] [blame] | 23 | # detect evidence of double-rounding: fsum is not always correctly |
| 24 | # rounded on machines that suffer from double rounding. |
| 25 | x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer |
| 26 | HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4) |
| 27 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 28 | # locate file with test values |
| 29 | if __name__ == '__main__': |
| 30 | file = sys.argv[0] |
| 31 | else: |
| 32 | file = __file__ |
| 33 | test_dir = os.path.dirname(file) or os.curdir |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 34 | math_testcases = os.path.join(test_dir, 'math_testcases.txt') |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 35 | test_file = os.path.join(test_dir, 'cmath_testcases.txt') |
| 36 | |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 37 | |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 38 | def to_ulps(x): |
| 39 | """Convert a non-NaN float x to an integer, in such a way that |
| 40 | adjacent floats are converted to adjacent integers. Then |
| 41 | abs(ulps(x) - ulps(y)) gives the difference in ulps between two |
| 42 | floats. |
| 43 | |
| 44 | The results from this function will only make sense on platforms |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 45 | where native doubles are represented in IEEE 754 binary64 format. |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 46 | |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 47 | Note: 0.0 and -0.0 are converted to 0 and -1, respectively. |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 48 | """ |
Mark Dickinson | d412ab5 | 2009-10-17 07:10:00 +0000 | [diff] [blame] | 49 | n = struct.unpack('<q', struct.pack('<d', x))[0] |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 50 | if n < 0: |
| 51 | n = ~(n+2**63) |
| 52 | return n |
| 53 | |
Mark Dickinson | 05d2e08 | 2009-12-11 20:17:17 +0000 | [diff] [blame] | 54 | |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 55 | def ulp(x): |
| 56 | """Return the value of the least significant bit of a |
| 57 | float x, such that the first float bigger than x is x+ulp(x). |
| 58 | Then, given an expected result x and a tolerance of n ulps, |
| 59 | the result y should be such that abs(y-x) <= n * ulp(x). |
| 60 | The results from this function will only make sense on platforms |
| 61 | where native doubles are represented in IEEE 754 binary64 format. |
| 62 | """ |
| 63 | x = abs(float(x)) |
| 64 | if math.isnan(x) or math.isinf(x): |
| 65 | return x |
Mark Dickinson | 05d2e08 | 2009-12-11 20:17:17 +0000 | [diff] [blame] | 66 | |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 67 | # Find next float up from x. |
| 68 | n = struct.unpack('<q', struct.pack('<d', x))[0] |
| 69 | x_next = struct.unpack('<d', struct.pack('<q', n + 1))[0] |
| 70 | if math.isinf(x_next): |
| 71 | # Corner case: x was the largest finite float. Then it's |
| 72 | # not an exact power of two, so we can take the difference |
| 73 | # between x and the previous float. |
| 74 | x_prev = struct.unpack('<d', struct.pack('<q', n - 1))[0] |
| 75 | return x - x_prev |
| 76 | else: |
| 77 | return x_next - x |
Mark Dickinson | 05d2e08 | 2009-12-11 20:17:17 +0000 | [diff] [blame] | 78 | |
Mark Dickinson | 4c8a9a2 | 2010-05-15 17:02:38 +0000 | [diff] [blame] | 79 | # Here's a pure Python version of the math.factorial algorithm, for |
| 80 | # documentation and comparison purposes. |
| 81 | # |
| 82 | # Formula: |
| 83 | # |
| 84 | # factorial(n) = factorial_odd_part(n) << (n - count_set_bits(n)) |
| 85 | # |
| 86 | # where |
| 87 | # |
| 88 | # factorial_odd_part(n) = product_{i >= 0} product_{0 < j <= n >> i; j odd} j |
| 89 | # |
| 90 | # The outer product above is an infinite product, but once i >= n.bit_length, |
| 91 | # (n >> i) < 1 and the corresponding term of the product is empty. So only the |
| 92 | # finitely many terms for 0 <= i < n.bit_length() contribute anything. |
| 93 | # |
| 94 | # We iterate downwards from i == n.bit_length() - 1 to i == 0. The inner |
| 95 | # product in the formula above starts at 1 for i == n.bit_length(); for each i |
| 96 | # < n.bit_length() we get the inner product for i from that for i + 1 by |
| 97 | # multiplying by all j in {n >> i+1 < j <= n >> i; j odd}. In Python terms, |
| 98 | # this set is range((n >> i+1) + 1 | 1, (n >> i) + 1 | 1, 2). |
| 99 | |
| 100 | def count_set_bits(n): |
| 101 | """Number of '1' bits in binary expansion of a nonnnegative integer.""" |
| 102 | return 1 + count_set_bits(n & n - 1) if n else 0 |
| 103 | |
| 104 | def partial_product(start, stop): |
| 105 | """Product of integers in range(start, stop, 2), computed recursively. |
| 106 | start and stop should both be odd, with start <= stop. |
| 107 | |
| 108 | """ |
| 109 | numfactors = (stop - start) >> 1 |
| 110 | if not numfactors: |
| 111 | return 1 |
| 112 | elif numfactors == 1: |
| 113 | return start |
| 114 | else: |
| 115 | mid = (start + numfactors) | 1 |
| 116 | return partial_product(start, mid) * partial_product(mid, stop) |
| 117 | |
| 118 | def py_factorial(n): |
| 119 | """Factorial of nonnegative integer n, via "Binary Split Factorial Formula" |
| 120 | described at http://www.luschny.de/math/factorial/binarysplitfact.html |
| 121 | |
| 122 | """ |
| 123 | inner = outer = 1 |
| 124 | for i in reversed(range(n.bit_length())): |
| 125 | inner *= partial_product((n >> i + 1) + 1 | 1, (n >> i) + 1 | 1) |
| 126 | outer *= inner |
| 127 | return outer << (n - count_set_bits(n)) |
| 128 | |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 129 | def ulp_abs_check(expected, got, ulp_tol, abs_tol): |
| 130 | """Given finite floats `expected` and `got`, check that they're |
| 131 | approximately equal to within the given number of ulps or the |
| 132 | given absolute tolerance, whichever is bigger. |
Mark Dickinson | 05d2e08 | 2009-12-11 20:17:17 +0000 | [diff] [blame] | 133 | |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 134 | Returns None on success and an error message on failure. |
| 135 | """ |
| 136 | ulp_error = abs(to_ulps(expected) - to_ulps(got)) |
| 137 | abs_error = abs(expected - got) |
| 138 | |
| 139 | # Succeed if either abs_error <= abs_tol or ulp_error <= ulp_tol. |
| 140 | if abs_error <= abs_tol or ulp_error <= ulp_tol: |
Mark Dickinson | 05d2e08 | 2009-12-11 20:17:17 +0000 | [diff] [blame] | 141 | return None |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 142 | else: |
| 143 | fmt = ("error = {:.3g} ({:d} ulps); " |
| 144 | "permitted error = {:.3g} or {:d} ulps") |
| 145 | return fmt.format(abs_error, ulp_error, abs_tol, ulp_tol) |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 146 | |
| 147 | def parse_mtestfile(fname): |
| 148 | """Parse a file with test values |
| 149 | |
| 150 | -- starts a comment |
| 151 | blank lines, or lines containing only a comment, are ignored |
| 152 | other lines are expected to have the form |
| 153 | id fn arg -> expected [flag]* |
| 154 | |
| 155 | """ |
| 156 | with open(fname) as fp: |
| 157 | for line in fp: |
| 158 | # strip comments, and skip blank lines |
| 159 | if '--' in line: |
| 160 | line = line[:line.index('--')] |
| 161 | if not line.strip(): |
| 162 | continue |
| 163 | |
| 164 | lhs, rhs = line.split('->') |
| 165 | id, fn, arg = lhs.split() |
| 166 | rhs_pieces = rhs.split() |
| 167 | exp = rhs_pieces[0] |
| 168 | flags = rhs_pieces[1:] |
| 169 | |
| 170 | yield (id, fn, float(arg), float(exp), flags) |
| 171 | |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 172 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 173 | def parse_testfile(fname): |
| 174 | """Parse a file with test values |
| 175 | |
| 176 | Empty lines or lines starting with -- are ignored |
| 177 | yields id, fn, arg_real, arg_imag, exp_real, exp_imag |
| 178 | """ |
| 179 | with open(fname) as fp: |
| 180 | for line in fp: |
| 181 | # skip comment lines and blank lines |
| 182 | if line.startswith('--') or not line.strip(): |
| 183 | continue |
| 184 | |
| 185 | lhs, rhs = line.split('->') |
| 186 | id, fn, arg_real, arg_imag = lhs.split() |
| 187 | rhs_pieces = rhs.split() |
| 188 | exp_real, exp_imag = rhs_pieces[0], rhs_pieces[1] |
| 189 | flags = rhs_pieces[2:] |
| 190 | |
| 191 | yield (id, fn, |
| 192 | float(arg_real), float(arg_imag), |
| 193 | float(exp_real), float(exp_imag), |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 194 | flags) |
| 195 | |
| 196 | |
| 197 | def result_check(expected, got, ulp_tol=5, abs_tol=0.0): |
| 198 | # Common logic of MathTests.(ftest, test_testcases, test_mtestcases) |
| 199 | """Compare arguments expected and got, as floats, if either |
| 200 | is a float, using a tolerance expressed in multiples of |
| 201 | ulp(expected) or absolutely (if given and greater). |
| 202 | |
| 203 | As a convenience, when neither argument is a float, and for |
| 204 | non-finite floats, exact equality is demanded. Also, nan==nan |
| 205 | as far as this function is concerned. |
| 206 | |
| 207 | Returns None on success and an error message on failure. |
| 208 | """ |
| 209 | |
| 210 | # Check exactly equal (applies also to strings representing exceptions) |
| 211 | if got == expected: |
| 212 | return None |
| 213 | |
| 214 | failure = "not equal" |
| 215 | |
| 216 | # Turn mixed float and int comparison (e.g. floor()) to all-float |
| 217 | if isinstance(expected, float) and isinstance(got, int): |
| 218 | got = float(got) |
| 219 | elif isinstance(got, float) and isinstance(expected, int): |
| 220 | expected = float(expected) |
| 221 | |
| 222 | if isinstance(expected, float) and isinstance(got, float): |
| 223 | if math.isnan(expected) and math.isnan(got): |
| 224 | # Pass, since both nan |
| 225 | failure = None |
| 226 | elif math.isinf(expected) or math.isinf(got): |
| 227 | # We already know they're not equal, drop through to failure |
| 228 | pass |
| 229 | else: |
| 230 | # Both are finite floats (now). Are they close enough? |
| 231 | failure = ulp_abs_check(expected, got, ulp_tol, abs_tol) |
| 232 | |
| 233 | # arguments are not equal, and if numeric, are too far apart |
| 234 | if failure is not None: |
| 235 | fail_fmt = "expected {!r}, got {!r}" |
| 236 | fail_msg = fail_fmt.format(expected, got) |
| 237 | fail_msg += ' ({})'.format(failure) |
| 238 | return fail_msg |
| 239 | else: |
| 240 | return None |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 241 | |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 242 | # Class providing an __index__ method. |
| 243 | class MyIndexable(object): |
| 244 | def __init__(self, value): |
| 245 | self.value = value |
| 246 | |
| 247 | def __index__(self): |
| 248 | return self.value |
| 249 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 250 | class MathTests(unittest.TestCase): |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 251 | |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 252 | def ftest(self, name, got, expected, ulp_tol=5, abs_tol=0.0): |
| 253 | """Compare arguments expected and got, as floats, if either |
| 254 | is a float, using a tolerance expressed in multiples of |
| 255 | ulp(expected) or absolutely, whichever is greater. |
| 256 | |
| 257 | As a convenience, when neither argument is a float, and for |
| 258 | non-finite floats, exact equality is demanded. Also, nan==nan |
| 259 | in this function. |
| 260 | """ |
| 261 | failure = result_check(expected, got, ulp_tol, abs_tol) |
| 262 | if failure is not None: |
| 263 | self.fail("{}: {}".format(name, failure)) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 264 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 265 | def testConstants(self): |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 266 | # Ref: Abramowitz & Stegun (Dover, 1965) |
| 267 | self.ftest('pi', math.pi, 3.141592653589793238462643) |
| 268 | self.ftest('e', math.e, 2.718281828459045235360287) |
Guido van Rossum | 0a891d7 | 2016-08-15 09:12:52 -0700 | [diff] [blame] | 269 | self.assertEqual(math.tau, 2*math.pi) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 270 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 271 | def testAcos(self): |
| 272 | self.assertRaises(TypeError, math.acos) |
| 273 | self.ftest('acos(-1)', math.acos(-1), math.pi) |
| 274 | self.ftest('acos(0)', math.acos(0), math.pi/2) |
| 275 | self.ftest('acos(1)', math.acos(1), 0) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 276 | self.assertRaises(ValueError, math.acos, INF) |
| 277 | self.assertRaises(ValueError, math.acos, NINF) |
Mark Dickinson | 31ba1c3 | 2016-09-04 12:29:14 +0100 | [diff] [blame] | 278 | self.assertRaises(ValueError, math.acos, 1 + eps) |
| 279 | self.assertRaises(ValueError, math.acos, -1 - eps) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 280 | self.assertTrue(math.isnan(math.acos(NAN))) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 281 | |
| 282 | def testAcosh(self): |
| 283 | self.assertRaises(TypeError, math.acosh) |
| 284 | self.ftest('acosh(1)', math.acosh(1), 0) |
| 285 | self.ftest('acosh(2)', math.acosh(2), 1.3169578969248168) |
| 286 | self.assertRaises(ValueError, math.acosh, 0) |
| 287 | self.assertRaises(ValueError, math.acosh, -1) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 288 | self.assertEqual(math.acosh(INF), INF) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 289 | self.assertRaises(ValueError, math.acosh, NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 290 | self.assertTrue(math.isnan(math.acosh(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 291 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 292 | def testAsin(self): |
| 293 | self.assertRaises(TypeError, math.asin) |
| 294 | self.ftest('asin(-1)', math.asin(-1), -math.pi/2) |
| 295 | self.ftest('asin(0)', math.asin(0), 0) |
| 296 | self.ftest('asin(1)', math.asin(1), math.pi/2) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 297 | self.assertRaises(ValueError, math.asin, INF) |
| 298 | self.assertRaises(ValueError, math.asin, NINF) |
Mark Dickinson | 31ba1c3 | 2016-09-04 12:29:14 +0100 | [diff] [blame] | 299 | self.assertRaises(ValueError, math.asin, 1 + eps) |
| 300 | self.assertRaises(ValueError, math.asin, -1 - eps) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 301 | self.assertTrue(math.isnan(math.asin(NAN))) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 302 | |
| 303 | def testAsinh(self): |
| 304 | self.assertRaises(TypeError, math.asinh) |
| 305 | self.ftest('asinh(0)', math.asinh(0), 0) |
| 306 | self.ftest('asinh(1)', math.asinh(1), 0.88137358701954305) |
| 307 | self.ftest('asinh(-1)', math.asinh(-1), -0.88137358701954305) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 308 | self.assertEqual(math.asinh(INF), INF) |
| 309 | self.assertEqual(math.asinh(NINF), NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 310 | self.assertTrue(math.isnan(math.asinh(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 311 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 312 | def testAtan(self): |
| 313 | self.assertRaises(TypeError, math.atan) |
| 314 | self.ftest('atan(-1)', math.atan(-1), -math.pi/4) |
| 315 | self.ftest('atan(0)', math.atan(0), 0) |
| 316 | self.ftest('atan(1)', math.atan(1), math.pi/4) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 317 | self.ftest('atan(inf)', math.atan(INF), math.pi/2) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 318 | self.ftest('atan(-inf)', math.atan(NINF), -math.pi/2) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 319 | self.assertTrue(math.isnan(math.atan(NAN))) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 320 | |
| 321 | def testAtanh(self): |
| 322 | self.assertRaises(TypeError, math.atan) |
| 323 | self.ftest('atanh(0)', math.atanh(0), 0) |
| 324 | self.ftest('atanh(0.5)', math.atanh(0.5), 0.54930614433405489) |
| 325 | self.ftest('atanh(-0.5)', math.atanh(-0.5), -0.54930614433405489) |
| 326 | self.assertRaises(ValueError, math.atanh, 1) |
| 327 | self.assertRaises(ValueError, math.atanh, -1) |
| 328 | self.assertRaises(ValueError, math.atanh, INF) |
| 329 | self.assertRaises(ValueError, math.atanh, NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 330 | self.assertTrue(math.isnan(math.atanh(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 331 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 332 | def testAtan2(self): |
| 333 | self.assertRaises(TypeError, math.atan2) |
| 334 | self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2) |
| 335 | self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4) |
| 336 | self.ftest('atan2(0, 1)', math.atan2(0, 1), 0) |
| 337 | self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4) |
| 338 | 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] | 339 | |
Christian Heimes | e57950f | 2008-04-21 13:08:03 +0000 | [diff] [blame] | 340 | # math.atan2(0, x) |
| 341 | self.ftest('atan2(0., -inf)', math.atan2(0., NINF), math.pi) |
| 342 | self.ftest('atan2(0., -2.3)', math.atan2(0., -2.3), math.pi) |
| 343 | self.ftest('atan2(0., -0.)', math.atan2(0., -0.), math.pi) |
| 344 | self.assertEqual(math.atan2(0., 0.), 0.) |
| 345 | self.assertEqual(math.atan2(0., 2.3), 0.) |
| 346 | self.assertEqual(math.atan2(0., INF), 0.) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 347 | self.assertTrue(math.isnan(math.atan2(0., NAN))) |
Christian Heimes | e57950f | 2008-04-21 13:08:03 +0000 | [diff] [blame] | 348 | # math.atan2(-0, x) |
| 349 | self.ftest('atan2(-0., -inf)', math.atan2(-0., NINF), -math.pi) |
| 350 | self.ftest('atan2(-0., -2.3)', math.atan2(-0., -2.3), -math.pi) |
| 351 | self.ftest('atan2(-0., -0.)', math.atan2(-0., -0.), -math.pi) |
| 352 | self.assertEqual(math.atan2(-0., 0.), -0.) |
| 353 | self.assertEqual(math.atan2(-0., 2.3), -0.) |
| 354 | self.assertEqual(math.atan2(-0., INF), -0.) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 355 | self.assertTrue(math.isnan(math.atan2(-0., NAN))) |
Christian Heimes | e57950f | 2008-04-21 13:08:03 +0000 | [diff] [blame] | 356 | # math.atan2(INF, x) |
| 357 | self.ftest('atan2(inf, -inf)', math.atan2(INF, NINF), math.pi*3/4) |
| 358 | self.ftest('atan2(inf, -2.3)', math.atan2(INF, -2.3), math.pi/2) |
| 359 | self.ftest('atan2(inf, -0.)', math.atan2(INF, -0.0), math.pi/2) |
| 360 | self.ftest('atan2(inf, 0.)', math.atan2(INF, 0.0), math.pi/2) |
| 361 | self.ftest('atan2(inf, 2.3)', math.atan2(INF, 2.3), math.pi/2) |
| 362 | self.ftest('atan2(inf, inf)', math.atan2(INF, INF), math.pi/4) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 363 | self.assertTrue(math.isnan(math.atan2(INF, NAN))) |
Christian Heimes | e57950f | 2008-04-21 13:08:03 +0000 | [diff] [blame] | 364 | # math.atan2(NINF, x) |
| 365 | self.ftest('atan2(-inf, -inf)', math.atan2(NINF, NINF), -math.pi*3/4) |
| 366 | self.ftest('atan2(-inf, -2.3)', math.atan2(NINF, -2.3), -math.pi/2) |
| 367 | self.ftest('atan2(-inf, -0.)', math.atan2(NINF, -0.0), -math.pi/2) |
| 368 | self.ftest('atan2(-inf, 0.)', math.atan2(NINF, 0.0), -math.pi/2) |
| 369 | self.ftest('atan2(-inf, 2.3)', math.atan2(NINF, 2.3), -math.pi/2) |
| 370 | self.ftest('atan2(-inf, inf)', math.atan2(NINF, INF), -math.pi/4) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 371 | self.assertTrue(math.isnan(math.atan2(NINF, NAN))) |
Christian Heimes | e57950f | 2008-04-21 13:08:03 +0000 | [diff] [blame] | 372 | # math.atan2(+finite, x) |
| 373 | self.ftest('atan2(2.3, -inf)', math.atan2(2.3, NINF), math.pi) |
| 374 | self.ftest('atan2(2.3, -0.)', math.atan2(2.3, -0.), math.pi/2) |
| 375 | self.ftest('atan2(2.3, 0.)', math.atan2(2.3, 0.), math.pi/2) |
| 376 | self.assertEqual(math.atan2(2.3, INF), 0.) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 377 | self.assertTrue(math.isnan(math.atan2(2.3, NAN))) |
Christian Heimes | e57950f | 2008-04-21 13:08:03 +0000 | [diff] [blame] | 378 | # math.atan2(-finite, x) |
| 379 | self.ftest('atan2(-2.3, -inf)', math.atan2(-2.3, NINF), -math.pi) |
| 380 | self.ftest('atan2(-2.3, -0.)', math.atan2(-2.3, -0.), -math.pi/2) |
| 381 | self.ftest('atan2(-2.3, 0.)', math.atan2(-2.3, 0.), -math.pi/2) |
| 382 | self.assertEqual(math.atan2(-2.3, INF), -0.) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 383 | self.assertTrue(math.isnan(math.atan2(-2.3, NAN))) |
Christian Heimes | e57950f | 2008-04-21 13:08:03 +0000 | [diff] [blame] | 384 | # math.atan2(NAN, x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 385 | self.assertTrue(math.isnan(math.atan2(NAN, NINF))) |
| 386 | self.assertTrue(math.isnan(math.atan2(NAN, -2.3))) |
| 387 | self.assertTrue(math.isnan(math.atan2(NAN, -0.))) |
| 388 | self.assertTrue(math.isnan(math.atan2(NAN, 0.))) |
| 389 | self.assertTrue(math.isnan(math.atan2(NAN, 2.3))) |
| 390 | self.assertTrue(math.isnan(math.atan2(NAN, INF))) |
| 391 | self.assertTrue(math.isnan(math.atan2(NAN, NAN))) |
Christian Heimes | e57950f | 2008-04-21 13:08:03 +0000 | [diff] [blame] | 392 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 393 | def testCeil(self): |
| 394 | self.assertRaises(TypeError, math.ceil) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 395 | self.assertEqual(int, type(math.ceil(0.5))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 396 | self.ftest('ceil(0.5)', math.ceil(0.5), 1) |
| 397 | self.ftest('ceil(1.0)', math.ceil(1.0), 1) |
| 398 | self.ftest('ceil(1.5)', math.ceil(1.5), 2) |
| 399 | self.ftest('ceil(-0.5)', math.ceil(-0.5), 0) |
| 400 | self.ftest('ceil(-1.0)', math.ceil(-1.0), -1) |
| 401 | self.ftest('ceil(-1.5)', math.ceil(-1.5), -1) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 402 | #self.assertEqual(math.ceil(INF), INF) |
| 403 | #self.assertEqual(math.ceil(NINF), NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 404 | #self.assertTrue(math.isnan(math.ceil(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 405 | |
Guido van Rossum | 13e05de | 2007-08-23 22:56:55 +0000 | [diff] [blame] | 406 | class TestCeil: |
| 407 | def __ceil__(self): |
| 408 | return 42 |
| 409 | class TestNoCeil: |
| 410 | pass |
| 411 | self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42) |
| 412 | self.assertRaises(TypeError, math.ceil, TestNoCeil()) |
| 413 | |
| 414 | t = TestNoCeil() |
| 415 | t.__ceil__ = lambda *args: args |
| 416 | self.assertRaises(TypeError, math.ceil, t) |
| 417 | self.assertRaises(TypeError, math.ceil, t, 0) |
| 418 | |
Mark Dickinson | 6356623 | 2009-09-18 21:04:19 +0000 | [diff] [blame] | 419 | @requires_IEEE_754 |
| 420 | def testCopysign(self): |
Mark Dickinson | 06b59e0 | 2010-02-06 23:16:50 +0000 | [diff] [blame] | 421 | self.assertEqual(math.copysign(1, 42), 1.0) |
| 422 | self.assertEqual(math.copysign(0., 42), 0.0) |
| 423 | self.assertEqual(math.copysign(1., -42), -1.0) |
| 424 | self.assertEqual(math.copysign(3, 0.), 3.0) |
| 425 | self.assertEqual(math.copysign(4., -0.), -4.0) |
| 426 | |
Mark Dickinson | 6356623 | 2009-09-18 21:04:19 +0000 | [diff] [blame] | 427 | self.assertRaises(TypeError, math.copysign) |
| 428 | # copysign should let us distinguish signs of zeros |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 429 | self.assertEqual(math.copysign(1., 0.), 1.) |
| 430 | self.assertEqual(math.copysign(1., -0.), -1.) |
| 431 | self.assertEqual(math.copysign(INF, 0.), INF) |
| 432 | self.assertEqual(math.copysign(INF, -0.), NINF) |
| 433 | self.assertEqual(math.copysign(NINF, 0.), INF) |
| 434 | self.assertEqual(math.copysign(NINF, -0.), NINF) |
Mark Dickinson | 6356623 | 2009-09-18 21:04:19 +0000 | [diff] [blame] | 435 | # and of infinities |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 436 | self.assertEqual(math.copysign(1., INF), 1.) |
| 437 | self.assertEqual(math.copysign(1., NINF), -1.) |
| 438 | self.assertEqual(math.copysign(INF, INF), INF) |
| 439 | self.assertEqual(math.copysign(INF, NINF), NINF) |
| 440 | self.assertEqual(math.copysign(NINF, INF), INF) |
| 441 | self.assertEqual(math.copysign(NINF, NINF), NINF) |
Mark Dickinson | 06b59e0 | 2010-02-06 23:16:50 +0000 | [diff] [blame] | 442 | self.assertTrue(math.isnan(math.copysign(NAN, 1.))) |
| 443 | self.assertTrue(math.isnan(math.copysign(NAN, INF))) |
| 444 | self.assertTrue(math.isnan(math.copysign(NAN, NINF))) |
| 445 | self.assertTrue(math.isnan(math.copysign(NAN, NAN))) |
Mark Dickinson | 6356623 | 2009-09-18 21:04:19 +0000 | [diff] [blame] | 446 | # copysign(INF, NAN) may be INF or it may be NINF, since |
| 447 | # we don't know whether the sign bit of NAN is set on any |
| 448 | # given platform. |
Mark Dickinson | 06b59e0 | 2010-02-06 23:16:50 +0000 | [diff] [blame] | 449 | self.assertTrue(math.isinf(math.copysign(INF, NAN))) |
Mark Dickinson | 6356623 | 2009-09-18 21:04:19 +0000 | [diff] [blame] | 450 | # similarly, copysign(2., NAN) could be 2. or -2. |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 451 | self.assertEqual(abs(math.copysign(2., NAN)), 2.) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 452 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 453 | def testCos(self): |
| 454 | self.assertRaises(TypeError, math.cos) |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 455 | self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0, abs_tol=ulp(1)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 456 | self.ftest('cos(0)', math.cos(0), 1) |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 457 | self.ftest('cos(pi/2)', math.cos(math.pi/2), 0, abs_tol=ulp(1)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 458 | self.ftest('cos(pi)', math.cos(math.pi), -1) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 459 | try: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 460 | self.assertTrue(math.isnan(math.cos(INF))) |
| 461 | self.assertTrue(math.isnan(math.cos(NINF))) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 462 | except ValueError: |
| 463 | self.assertRaises(ValueError, math.cos, INF) |
| 464 | self.assertRaises(ValueError, math.cos, NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 465 | self.assertTrue(math.isnan(math.cos(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 466 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 467 | def testCosh(self): |
| 468 | self.assertRaises(TypeError, math.cosh) |
| 469 | self.ftest('cosh(0)', math.cosh(0), 1) |
| 470 | 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] | 471 | self.assertEqual(math.cosh(INF), INF) |
| 472 | self.assertEqual(math.cosh(NINF), INF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 473 | self.assertTrue(math.isnan(math.cosh(NAN))) |
Raymond Hettinger | 64108af | 2002-05-13 03:55:01 +0000 | [diff] [blame] | 474 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 475 | def testDegrees(self): |
| 476 | self.assertRaises(TypeError, math.degrees) |
| 477 | self.ftest('degrees(pi)', math.degrees(math.pi), 180.0) |
| 478 | self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0) |
| 479 | self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0) |
Mark Dickinson | 31ba1c3 | 2016-09-04 12:29:14 +0100 | [diff] [blame] | 480 | self.ftest('degrees(0)', math.degrees(0), 0) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 481 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 482 | def testExp(self): |
| 483 | self.assertRaises(TypeError, math.exp) |
| 484 | self.ftest('exp(-1)', math.exp(-1), 1/math.e) |
| 485 | self.ftest('exp(0)', math.exp(0), 1) |
| 486 | self.ftest('exp(1)', math.exp(1), math.e) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 487 | self.assertEqual(math.exp(INF), INF) |
| 488 | self.assertEqual(math.exp(NINF), 0.) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 489 | self.assertTrue(math.isnan(math.exp(NAN))) |
Mark Dickinson | 31ba1c3 | 2016-09-04 12:29:14 +0100 | [diff] [blame] | 490 | self.assertRaises(OverflowError, math.exp, 1000000) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 491 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 492 | def testFabs(self): |
| 493 | self.assertRaises(TypeError, math.fabs) |
| 494 | self.ftest('fabs(-1)', math.fabs(-1), 1) |
| 495 | self.ftest('fabs(0)', math.fabs(0), 0) |
| 496 | self.ftest('fabs(1)', math.fabs(1), 1) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 497 | |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 498 | def testFactorial(self): |
Mark Dickinson | 4c8a9a2 | 2010-05-15 17:02:38 +0000 | [diff] [blame] | 499 | self.assertEqual(math.factorial(0), 1) |
| 500 | self.assertEqual(math.factorial(0.0), 1) |
| 501 | total = 1 |
| 502 | for i in range(1, 1000): |
| 503 | total *= i |
| 504 | self.assertEqual(math.factorial(i), total) |
| 505 | self.assertEqual(math.factorial(float(i)), total) |
| 506 | self.assertEqual(math.factorial(i), py_factorial(i)) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 507 | self.assertRaises(ValueError, math.factorial, -1) |
Mark Dickinson | 4c8a9a2 | 2010-05-15 17:02:38 +0000 | [diff] [blame] | 508 | self.assertRaises(ValueError, math.factorial, -1.0) |
Mark Dickinson | 5990d28 | 2014-04-10 09:29:39 -0400 | [diff] [blame] | 509 | self.assertRaises(ValueError, math.factorial, -10**100) |
| 510 | self.assertRaises(ValueError, math.factorial, -1e100) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 511 | self.assertRaises(ValueError, math.factorial, math.pi) |
Mark Dickinson | 5990d28 | 2014-04-10 09:29:39 -0400 | [diff] [blame] | 512 | |
| 513 | # Other implementations may place different upper bounds. |
| 514 | @support.cpython_only |
| 515 | def testFactorialHugeInputs(self): |
| 516 | # Currently raises ValueError for inputs that are too large |
| 517 | # to fit into a C long. |
| 518 | self.assertRaises(OverflowError, math.factorial, 10**100) |
| 519 | self.assertRaises(OverflowError, math.factorial, 1e100) |
Georg Brandl | c28e1fa | 2008-06-10 19:20:26 +0000 | [diff] [blame] | 520 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 521 | def testFloor(self): |
| 522 | self.assertRaises(TypeError, math.floor) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 523 | self.assertEqual(int, type(math.floor(0.5))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 524 | self.ftest('floor(0.5)', math.floor(0.5), 0) |
| 525 | self.ftest('floor(1.0)', math.floor(1.0), 1) |
| 526 | self.ftest('floor(1.5)', math.floor(1.5), 1) |
| 527 | self.ftest('floor(-0.5)', math.floor(-0.5), -1) |
| 528 | self.ftest('floor(-1.0)', math.floor(-1.0), -1) |
| 529 | self.ftest('floor(-1.5)', math.floor(-1.5), -2) |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 530 | # pow() relies on floor() to check for integers |
| 531 | # This fails on some platforms - so check it here |
| 532 | self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167) |
| 533 | self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 534 | #self.assertEqual(math.ceil(INF), INF) |
| 535 | #self.assertEqual(math.ceil(NINF), NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 536 | #self.assertTrue(math.isnan(math.floor(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 537 | |
Guido van Rossum | 13e05de | 2007-08-23 22:56:55 +0000 | [diff] [blame] | 538 | class TestFloor: |
| 539 | def __floor__(self): |
| 540 | return 42 |
| 541 | class TestNoFloor: |
| 542 | pass |
| 543 | self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42) |
| 544 | self.assertRaises(TypeError, math.floor, TestNoFloor()) |
| 545 | |
| 546 | t = TestNoFloor() |
| 547 | t.__floor__ = lambda *args: args |
| 548 | self.assertRaises(TypeError, math.floor, t) |
| 549 | self.assertRaises(TypeError, math.floor, t, 0) |
| 550 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 551 | def testFmod(self): |
| 552 | self.assertRaises(TypeError, math.fmod) |
Mark Dickinson | 5bc7a44 | 2011-05-03 21:13:40 +0100 | [diff] [blame] | 553 | self.ftest('fmod(10, 1)', math.fmod(10, 1), 0.0) |
| 554 | self.ftest('fmod(10, 0.5)', math.fmod(10, 0.5), 0.0) |
| 555 | self.ftest('fmod(10, 1.5)', math.fmod(10, 1.5), 1.0) |
| 556 | self.ftest('fmod(-10, 1)', math.fmod(-10, 1), -0.0) |
| 557 | self.ftest('fmod(-10, 0.5)', math.fmod(-10, 0.5), -0.0) |
| 558 | 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] | 559 | self.assertTrue(math.isnan(math.fmod(NAN, 1.))) |
| 560 | self.assertTrue(math.isnan(math.fmod(1., NAN))) |
| 561 | self.assertTrue(math.isnan(math.fmod(NAN, NAN))) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 562 | self.assertRaises(ValueError, math.fmod, 1., 0.) |
| 563 | self.assertRaises(ValueError, math.fmod, INF, 1.) |
| 564 | self.assertRaises(ValueError, math.fmod, NINF, 1.) |
| 565 | self.assertRaises(ValueError, math.fmod, INF, 0.) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 566 | self.assertEqual(math.fmod(3.0, INF), 3.0) |
| 567 | self.assertEqual(math.fmod(-3.0, INF), -3.0) |
| 568 | self.assertEqual(math.fmod(3.0, NINF), 3.0) |
| 569 | self.assertEqual(math.fmod(-3.0, NINF), -3.0) |
| 570 | self.assertEqual(math.fmod(0.0, 3.0), 0.0) |
| 571 | self.assertEqual(math.fmod(0.0, NINF), 0.0) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 572 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 573 | def testFrexp(self): |
| 574 | self.assertRaises(TypeError, math.frexp) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 575 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 576 | def testfrexp(name, result, expected): |
| 577 | (mant, exp), (emant, eexp) = result, expected |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 578 | if abs(mant-emant) > eps or exp != eexp: |
| 579 | self.fail('%s returned %r, expected %r'%\ |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 580 | (name, result, expected)) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 581 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 582 | testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1)) |
| 583 | testfrexp('frexp(0)', math.frexp(0), (0, 0)) |
| 584 | testfrexp('frexp(1)', math.frexp(1), (0.5, 1)) |
| 585 | testfrexp('frexp(2)', math.frexp(2), (0.5, 2)) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 586 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 587 | self.assertEqual(math.frexp(INF)[0], INF) |
| 588 | self.assertEqual(math.frexp(NINF)[0], NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 589 | self.assertTrue(math.isnan(math.frexp(NAN)[0])) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 590 | |
Mark Dickinson | 6356623 | 2009-09-18 21:04:19 +0000 | [diff] [blame] | 591 | @requires_IEEE_754 |
Mark Dickinson | 5c56708 | 2009-04-24 16:39:07 +0000 | [diff] [blame] | 592 | @unittest.skipIf(HAVE_DOUBLE_ROUNDING, |
| 593 | "fsum is not exact on machines with double rounding") |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 594 | def testFsum(self): |
| 595 | # math.fsum relies on exact rounding for correct operation. |
| 596 | # There's a known problem with IA32 floating-point that causes |
| 597 | # inexact rounding in some situations, and will cause the |
| 598 | # math.fsum tests below to fail; see issue #2937. On non IEEE |
| 599 | # 754 platforms, and on IEEE 754 platforms that exhibit the |
| 600 | # problem described in issue #2937, we simply skip the whole |
| 601 | # test. |
| 602 | |
Mark Dickinson | aa7633a | 2008-08-01 08:16:13 +0000 | [diff] [blame] | 603 | # Python version of math.fsum, for comparison. Uses a |
| 604 | # different algorithm based on frexp, ldexp and integer |
| 605 | # arithmetic. |
| 606 | from sys import float_info |
| 607 | mant_dig = float_info.mant_dig |
| 608 | etiny = float_info.min_exp - mant_dig |
| 609 | |
| 610 | def msum(iterable): |
| 611 | """Full precision summation. Compute sum(iterable) without any |
| 612 | intermediate accumulation of error. Based on the 'lsum' function |
| 613 | at http://code.activestate.com/recipes/393090/ |
| 614 | |
| 615 | """ |
| 616 | tmant, texp = 0, 0 |
| 617 | for x in iterable: |
| 618 | mant, exp = math.frexp(x) |
| 619 | mant, exp = int(math.ldexp(mant, mant_dig)), exp - mant_dig |
| 620 | if texp > exp: |
| 621 | tmant <<= texp-exp |
| 622 | texp = exp |
| 623 | else: |
| 624 | mant <<= exp-texp |
| 625 | tmant += mant |
| 626 | # Round tmant * 2**texp to a float. The original recipe |
| 627 | # used float(str(tmant)) * 2.0**texp for this, but that's |
| 628 | # a little unsafe because str -> float conversion can't be |
| 629 | # relied upon to do correct rounding on all platforms. |
| 630 | tail = max(len(bin(abs(tmant)))-2 - mant_dig, etiny - texp) |
| 631 | if tail > 0: |
| 632 | h = 1 << (tail-1) |
| 633 | tmant = tmant // (2*h) + bool(tmant & h and tmant & 3*h-1) |
| 634 | texp += tail |
| 635 | return math.ldexp(tmant, texp) |
| 636 | |
| 637 | test_values = [ |
| 638 | ([], 0.0), |
| 639 | ([0.0], 0.0), |
| 640 | ([1e100, 1.0, -1e100, 1e-100, 1e50, -1.0, -1e50], 1e-100), |
| 641 | ([2.0**53, -0.5, -2.0**-54], 2.0**53-1.0), |
| 642 | ([2.0**53, 1.0, 2.0**-100], 2.0**53+2.0), |
| 643 | ([2.0**53+10.0, 1.0, 2.0**-100], 2.0**53+12.0), |
| 644 | ([2.0**53-4.0, 0.5, 2.0**-54], 2.0**53-3.0), |
| 645 | ([1./n for n in range(1, 1001)], |
| 646 | float.fromhex('0x1.df11f45f4e61ap+2')), |
| 647 | ([(-1.)**n/n for n in range(1, 1001)], |
| 648 | float.fromhex('-0x1.62a2af1bd3624p-1')), |
| 649 | ([1.7**(i+1)-1.7**i for i in range(1000)] + [-1.7**1000], -1.0), |
| 650 | ([1e16, 1., 1e-16], 10000000000000002.0), |
| 651 | ([1e16-2., 1.-2.**-53, -(1e16-2.), -(1.-2.**-53)], 0.0), |
| 652 | # exercise code for resizing partials array |
| 653 | ([2.**n - 2.**(n+50) + 2.**(n+52) for n in range(-1074, 972, 2)] + |
| 654 | [-2.**1022], |
| 655 | float.fromhex('0x1.5555555555555p+970')), |
| 656 | ] |
| 657 | |
| 658 | for i, (vals, expected) in enumerate(test_values): |
| 659 | try: |
| 660 | actual = math.fsum(vals) |
| 661 | except OverflowError: |
| 662 | self.fail("test %d failed: got OverflowError, expected %r " |
| 663 | "for math.fsum(%.100r)" % (i, expected, vals)) |
| 664 | except ValueError: |
| 665 | self.fail("test %d failed: got ValueError, expected %r " |
| 666 | "for math.fsum(%.100r)" % (i, expected, vals)) |
| 667 | self.assertEqual(actual, expected) |
| 668 | |
| 669 | from random import random, gauss, shuffle |
| 670 | for j in range(1000): |
| 671 | vals = [7, 1e100, -7, -1e100, -9e-20, 8e-20] * 10 |
| 672 | s = 0 |
| 673 | for i in range(200): |
| 674 | v = gauss(0, random()) ** 7 - s |
| 675 | s += v |
| 676 | vals.append(v) |
| 677 | shuffle(vals) |
| 678 | |
| 679 | s = msum(vals) |
| 680 | self.assertEqual(msum(vals), math.fsum(vals)) |
| 681 | |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 682 | def testGcd(self): |
| 683 | gcd = math.gcd |
| 684 | self.assertEqual(gcd(0, 0), 0) |
| 685 | self.assertEqual(gcd(1, 0), 1) |
| 686 | self.assertEqual(gcd(-1, 0), 1) |
| 687 | self.assertEqual(gcd(0, 1), 1) |
| 688 | self.assertEqual(gcd(0, -1), 1) |
| 689 | self.assertEqual(gcd(7, 1), 1) |
| 690 | self.assertEqual(gcd(7, -1), 1) |
| 691 | self.assertEqual(gcd(-23, 15), 1) |
| 692 | self.assertEqual(gcd(120, 84), 12) |
| 693 | self.assertEqual(gcd(84, -120), 12) |
| 694 | self.assertEqual(gcd(1216342683557601535506311712, |
| 695 | 436522681849110124616458784), 32) |
| 696 | c = 652560 |
| 697 | x = 434610456570399902378880679233098819019853229470286994367836600566 |
| 698 | y = 1064502245825115327754847244914921553977 |
| 699 | a = x * c |
| 700 | b = y * c |
| 701 | self.assertEqual(gcd(a, b), c) |
| 702 | self.assertEqual(gcd(b, a), c) |
| 703 | self.assertEqual(gcd(-a, b), c) |
| 704 | self.assertEqual(gcd(b, -a), c) |
| 705 | self.assertEqual(gcd(a, -b), c) |
| 706 | self.assertEqual(gcd(-b, a), c) |
| 707 | self.assertEqual(gcd(-a, -b), c) |
| 708 | self.assertEqual(gcd(-b, -a), c) |
| 709 | c = 576559230871654959816130551884856912003141446781646602790216406874 |
| 710 | a = x * c |
| 711 | b = y * c |
| 712 | self.assertEqual(gcd(a, b), c) |
| 713 | self.assertEqual(gcd(b, a), c) |
| 714 | self.assertEqual(gcd(-a, b), c) |
| 715 | self.assertEqual(gcd(b, -a), c) |
| 716 | self.assertEqual(gcd(a, -b), c) |
| 717 | self.assertEqual(gcd(-b, a), c) |
| 718 | self.assertEqual(gcd(-a, -b), c) |
| 719 | self.assertEqual(gcd(-b, -a), c) |
| 720 | |
| 721 | self.assertRaises(TypeError, gcd, 120.0, 84) |
| 722 | self.assertRaises(TypeError, gcd, 120, 84.0) |
| 723 | self.assertEqual(gcd(MyIndexable(120), MyIndexable(84)), 12) |
| 724 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 725 | def testHypot(self): |
Raymond Hettinger | c6dabe3 | 2018-07-28 07:48:04 -0700 | [diff] [blame] | 726 | from decimal import Decimal |
| 727 | from fractions import Fraction |
| 728 | |
| 729 | hypot = math.hypot |
| 730 | |
| 731 | # Test different numbers of arguments (from zero to five) |
| 732 | # against a straightforward pure python implementation |
| 733 | args = math.e, math.pi, math.sqrt(2.0), math.gamma(3.5), math.sin(2.1) |
| 734 | for i in range(len(args)+1): |
| 735 | self.assertAlmostEqual( |
| 736 | hypot(*args[:i]), |
| 737 | math.sqrt(sum(s**2 for s in args[:i])) |
| 738 | ) |
| 739 | |
| 740 | # Test allowable types (those with __float__) |
| 741 | self.assertEqual(hypot(12.0, 5.0), 13.0) |
| 742 | self.assertEqual(hypot(12, 5), 13) |
| 743 | self.assertEqual(hypot(Decimal(12), Decimal(5)), 13) |
| 744 | self.assertEqual(hypot(Fraction(12, 32), Fraction(5, 32)), Fraction(13, 32)) |
| 745 | self.assertEqual(hypot(bool(1), bool(0), bool(1), bool(1)), math.sqrt(3)) |
| 746 | |
| 747 | # Test corner cases |
| 748 | self.assertEqual(hypot(0.0, 0.0), 0.0) # Max input is zero |
| 749 | self.assertEqual(hypot(-10.5), 10.5) # Negative input |
| 750 | self.assertEqual(hypot(), 0.0) # Negative input |
| 751 | self.assertEqual(1.0, |
| 752 | math.copysign(1.0, hypot(-0.0)) # Convert negative zero to positive zero |
| 753 | ) |
| 754 | |
| 755 | # Test handling of bad arguments |
| 756 | with self.assertRaises(TypeError): # Reject keyword args |
| 757 | hypot(x=1) |
| 758 | with self.assertRaises(TypeError): # Reject values without __float__ |
| 759 | hypot(1.1, 'string', 2.2) |
| 760 | |
| 761 | # Any infinity gives positive infinity. |
| 762 | self.assertEqual(hypot(INF), INF) |
| 763 | self.assertEqual(hypot(0, INF), INF) |
| 764 | self.assertEqual(hypot(10, INF), INF) |
| 765 | self.assertEqual(hypot(-10, INF), INF) |
| 766 | self.assertEqual(hypot(NAN, INF), INF) |
| 767 | self.assertEqual(hypot(INF, NAN), INF) |
| 768 | self.assertEqual(hypot(NINF, NAN), INF) |
| 769 | self.assertEqual(hypot(NAN, NINF), INF) |
| 770 | self.assertEqual(hypot(-INF, INF), INF) |
| 771 | self.assertEqual(hypot(-INF, -INF), INF) |
| 772 | self.assertEqual(hypot(10, -INF), INF) |
| 773 | |
| 774 | # If no infinity, any NaN gives a Nan. |
| 775 | self.assertTrue(math.isnan(hypot(NAN))) |
| 776 | self.assertTrue(math.isnan(hypot(0, NAN))) |
| 777 | self.assertTrue(math.isnan(hypot(NAN, 10))) |
| 778 | self.assertTrue(math.isnan(hypot(10, NAN))) |
| 779 | self.assertTrue(math.isnan(hypot(NAN, NAN))) |
| 780 | self.assertTrue(math.isnan(hypot(NAN))) |
| 781 | |
| 782 | # Verify scaling for extremely large values |
| 783 | fourthmax = FLOAT_MAX / 4.0 |
| 784 | for n in range(32): |
| 785 | self.assertEqual(hypot(*([fourthmax]*n)), fourthmax * math.sqrt(n)) |
| 786 | |
| 787 | # Verify scaling for extremely small values |
| 788 | for exp in range(32): |
| 789 | scale = FLOAT_MIN / 2.0 ** exp |
| 790 | self.assertEqual(math.hypot(4*scale, 3*scale), 5*scale) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 791 | |
Raymond Hettinger | 9c18b1a | 2018-07-31 00:45:49 -0700 | [diff] [blame] | 792 | def testDist(self): |
| 793 | from decimal import Decimal as D |
| 794 | from fractions import Fraction as F |
| 795 | |
| 796 | dist = math.dist |
| 797 | sqrt = math.sqrt |
| 798 | |
| 799 | # Simple exact case |
| 800 | self.assertEqual(dist((1, 2, 3), (4, 2, -1)), 5.0) |
| 801 | |
| 802 | # Test different numbers of arguments (from zero to nine) |
| 803 | # against a straightforward pure python implementation |
| 804 | for i in range(9): |
| 805 | for j in range(5): |
| 806 | p = tuple(random.uniform(-5, 5) for k in range(i)) |
| 807 | q = tuple(random.uniform(-5, 5) for k in range(i)) |
| 808 | self.assertAlmostEqual( |
| 809 | dist(p, q), |
| 810 | sqrt(sum((px - qx) ** 2.0 for px, qx in zip(p, q))) |
| 811 | ) |
| 812 | |
| 813 | # Test allowable types (those with __float__) |
| 814 | self.assertEqual(dist((14.0, 1.0), (2.0, -4.0)), 13.0) |
| 815 | self.assertEqual(dist((14, 1), (2, -4)), 13) |
| 816 | self.assertEqual(dist((D(14), D(1)), (D(2), D(-4))), D(13)) |
| 817 | self.assertEqual(dist((F(14, 32), F(1, 32)), (F(2, 32), F(-4, 32))), |
| 818 | F(13, 32)) |
| 819 | self.assertEqual(dist((True, True, False, True, False), |
| 820 | (True, False, True, True, False)), |
| 821 | sqrt(2.0)) |
| 822 | |
| 823 | # Test corner cases |
| 824 | self.assertEqual(dist((13.25, 12.5, -3.25), |
| 825 | (13.25, 12.5, -3.25)), |
| 826 | 0.0) # Distance with self is zero |
| 827 | self.assertEqual(dist((), ()), 0.0) # Zero-dimensional case |
| 828 | self.assertEqual(1.0, # Convert negative zero to positive zero |
| 829 | math.copysign(1.0, dist((-0.0,), (0.0,))) |
| 830 | ) |
| 831 | self.assertEqual(1.0, # Convert negative zero to positive zero |
| 832 | math.copysign(1.0, dist((0.0,), (-0.0,))) |
| 833 | ) |
| 834 | |
| 835 | # Verify tuple subclasses are allowed |
| 836 | class T(tuple): # tuple subclas |
| 837 | pass |
| 838 | self.assertEqual(dist(T((1, 2, 3)), ((4, 2, -1))), 5.0) |
| 839 | |
| 840 | # Test handling of bad arguments |
| 841 | with self.assertRaises(TypeError): # Reject keyword args |
| 842 | dist(p=(1, 2, 3), q=(4, 5, 6)) |
| 843 | with self.assertRaises(TypeError): # Too few args |
| 844 | dist((1, 2, 3)) |
| 845 | with self.assertRaises(TypeError): # Too many args |
| 846 | dist((1, 2, 3), (4, 5, 6), (7, 8, 9)) |
| 847 | with self.assertRaises(TypeError): # Scalars not allowed |
| 848 | dist(1, 2) |
| 849 | with self.assertRaises(TypeError): # Lists not allowed |
| 850 | dist([1, 2, 3], [4, 5, 6]) |
| 851 | with self.assertRaises(TypeError): # Reject values without __float__ |
| 852 | dist((1.1, 'string', 2.2), (1, 2, 3)) |
| 853 | with self.assertRaises(ValueError): # Check dimension agree |
| 854 | dist((1, 2, 3, 4), (5, 6, 7)) |
| 855 | with self.assertRaises(ValueError): # Check dimension agree |
| 856 | dist((1, 2, 3), (4, 5, 6, 7)) |
| 857 | |
| 858 | |
| 859 | # Verify that the one dimensional case equivalent to abs() |
| 860 | for i in range(20): |
| 861 | p, q = random.random(), random.random() |
| 862 | self.assertEqual(dist((p,), (q,)), abs(p - q)) |
| 863 | |
| 864 | # Test special values |
| 865 | values = [NINF, -10.5, -0.0, 0.0, 10.5, INF, NAN] |
| 866 | for p in itertools.product(values, repeat=3): |
| 867 | for q in itertools.product(values, repeat=3): |
| 868 | diffs = [px - qx for px, qx in zip(p, q)] |
| 869 | if any(map(math.isinf, diffs)): |
| 870 | # Any infinite difference gives positive infinity. |
| 871 | self.assertEqual(dist(p, q), INF) |
| 872 | elif any(map(math.isnan, diffs)): |
| 873 | # If no infinity, any NaN gives a Nan. |
| 874 | self.assertTrue(math.isnan(dist(p, q))) |
| 875 | |
| 876 | # Verify scaling for extremely large values |
| 877 | fourthmax = FLOAT_MAX / 4.0 |
| 878 | for n in range(32): |
| 879 | p = (fourthmax,) * n |
| 880 | q = (0.0,) * n |
| 881 | self.assertEqual(dist(p, q), fourthmax * math.sqrt(n)) |
| 882 | self.assertEqual(dist(q, p), fourthmax * math.sqrt(n)) |
| 883 | |
| 884 | # Verify scaling for extremely small values |
| 885 | for exp in range(32): |
| 886 | scale = FLOAT_MIN / 2.0 ** exp |
| 887 | p = (4*scale, 3*scale) |
| 888 | q = (0.0, 0.0) |
| 889 | self.assertEqual(math.dist(p, q), 5*scale) |
| 890 | self.assertEqual(math.dist(q, p), 5*scale) |
| 891 | |
| 892 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 893 | def testLdexp(self): |
| 894 | self.assertRaises(TypeError, math.ldexp) |
| 895 | self.ftest('ldexp(0,1)', math.ldexp(0,1), 0) |
| 896 | self.ftest('ldexp(1,1)', math.ldexp(1,1), 2) |
| 897 | self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5) |
| 898 | self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 899 | self.assertRaises(OverflowError, math.ldexp, 1., 1000000) |
| 900 | self.assertRaises(OverflowError, math.ldexp, -1., 1000000) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 901 | self.assertEqual(math.ldexp(1., -1000000), 0.) |
| 902 | self.assertEqual(math.ldexp(-1., -1000000), -0.) |
| 903 | self.assertEqual(math.ldexp(INF, 30), INF) |
| 904 | self.assertEqual(math.ldexp(NINF, -213), NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 905 | self.assertTrue(math.isnan(math.ldexp(NAN, 0))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 906 | |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 907 | # large second argument |
| 908 | for n in [10**5, 10**10, 10**20, 10**40]: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 909 | self.assertEqual(math.ldexp(INF, -n), INF) |
| 910 | self.assertEqual(math.ldexp(NINF, -n), NINF) |
| 911 | self.assertEqual(math.ldexp(1., -n), 0.) |
| 912 | self.assertEqual(math.ldexp(-1., -n), -0.) |
| 913 | self.assertEqual(math.ldexp(0., -n), 0.) |
| 914 | self.assertEqual(math.ldexp(-0., -n), -0.) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 915 | self.assertTrue(math.isnan(math.ldexp(NAN, -n))) |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 916 | |
| 917 | self.assertRaises(OverflowError, math.ldexp, 1., n) |
| 918 | self.assertRaises(OverflowError, math.ldexp, -1., n) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 919 | self.assertEqual(math.ldexp(0., n), 0.) |
| 920 | self.assertEqual(math.ldexp(-0., n), -0.) |
| 921 | self.assertEqual(math.ldexp(INF, n), INF) |
| 922 | self.assertEqual(math.ldexp(NINF, n), NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 923 | self.assertTrue(math.isnan(math.ldexp(NAN, n))) |
Alexandre Vassalotti | 6461e10 | 2008-05-15 22:09:29 +0000 | [diff] [blame] | 924 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 925 | def testLog(self): |
| 926 | self.assertRaises(TypeError, math.log) |
| 927 | self.ftest('log(1/e)', math.log(1/math.e), -1) |
| 928 | self.ftest('log(1)', math.log(1), 0) |
| 929 | self.ftest('log(e)', math.log(math.e), 1) |
| 930 | self.ftest('log(32,2)', math.log(32,2), 5) |
| 931 | self.ftest('log(10**40, 10)', math.log(10**40, 10), 40) |
| 932 | 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] | 933 | self.ftest('log(10**1000)', math.log(10**1000), |
| 934 | 2302.5850929940457) |
| 935 | self.assertRaises(ValueError, math.log, -1.5) |
| 936 | self.assertRaises(ValueError, math.log, -10**1000) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 937 | self.assertRaises(ValueError, math.log, NINF) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 938 | self.assertEqual(math.log(INF), INF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 939 | self.assertTrue(math.isnan(math.log(NAN))) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 940 | |
| 941 | def testLog1p(self): |
| 942 | self.assertRaises(TypeError, math.log1p) |
Mark Dickinson | 31ba1c3 | 2016-09-04 12:29:14 +0100 | [diff] [blame] | 943 | for n in [2, 2**90, 2**300]: |
| 944 | self.assertAlmostEqual(math.log1p(n), math.log1p(float(n))) |
| 945 | self.assertRaises(ValueError, math.log1p, -1) |
| 946 | self.assertEqual(math.log1p(INF), INF) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 947 | |
Victor Stinner | fa0e3d5 | 2011-05-09 01:01:09 +0200 | [diff] [blame] | 948 | @requires_IEEE_754 |
| 949 | def testLog2(self): |
| 950 | self.assertRaises(TypeError, math.log2) |
Victor Stinner | fa0e3d5 | 2011-05-09 01:01:09 +0200 | [diff] [blame] | 951 | |
| 952 | # Check some integer values |
| 953 | self.assertEqual(math.log2(1), 0.0) |
| 954 | self.assertEqual(math.log2(2), 1.0) |
| 955 | self.assertEqual(math.log2(4), 2.0) |
| 956 | |
| 957 | # Large integer values |
| 958 | self.assertEqual(math.log2(2**1023), 1023.0) |
| 959 | self.assertEqual(math.log2(2**1024), 1024.0) |
| 960 | self.assertEqual(math.log2(2**2000), 2000.0) |
| 961 | |
| 962 | self.assertRaises(ValueError, math.log2, -1.5) |
| 963 | self.assertRaises(ValueError, math.log2, NINF) |
| 964 | self.assertTrue(math.isnan(math.log2(NAN))) |
| 965 | |
Victor Stinner | cd9dd37 | 2011-05-10 23:40:17 +0200 | [diff] [blame] | 966 | @requires_IEEE_754 |
Victor Stinner | ebbbdaf | 2011-06-01 13:19:07 +0200 | [diff] [blame] | 967 | # log2() is not accurate enough on Mac OS X Tiger (10.4) |
| 968 | @support.requires_mac_ver(10, 5) |
Victor Stinner | cd9dd37 | 2011-05-10 23:40:17 +0200 | [diff] [blame] | 969 | def testLog2Exact(self): |
| 970 | # Check that we get exact equality for log2 of powers of 2. |
| 971 | actual = [math.log2(math.ldexp(1.0, n)) for n in range(-1074, 1024)] |
| 972 | expected = [float(n) for n in range(-1074, 1024)] |
| 973 | self.assertEqual(actual, expected) |
| 974 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 975 | def testLog10(self): |
| 976 | self.assertRaises(TypeError, math.log10) |
| 977 | self.ftest('log10(0.1)', math.log10(0.1), -1) |
| 978 | self.ftest('log10(1)', math.log10(1), 0) |
| 979 | self.ftest('log10(10)', math.log10(10), 1) |
Mark Dickinson | c603717 | 2010-09-29 19:06:36 +0000 | [diff] [blame] | 980 | self.ftest('log10(10**1000)', math.log10(10**1000), 1000.0) |
| 981 | self.assertRaises(ValueError, math.log10, -1.5) |
| 982 | self.assertRaises(ValueError, math.log10, -10**1000) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 983 | self.assertRaises(ValueError, math.log10, NINF) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 984 | self.assertEqual(math.log(INF), INF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 985 | self.assertTrue(math.isnan(math.log10(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 986 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 987 | def testModf(self): |
| 988 | self.assertRaises(TypeError, math.modf) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 989 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 990 | def testmodf(name, result, expected): |
| 991 | (v1, v2), (e1, e2) = result, expected |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 992 | if abs(v1-e1) > eps or abs(v2-e2): |
| 993 | self.fail('%s returned %r, expected %r'%\ |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 994 | (name, result, expected)) |
Raymond Hettinger | 64108af | 2002-05-13 03:55:01 +0000 | [diff] [blame] | 995 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 996 | testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0)) |
| 997 | 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] | 998 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 999 | self.assertEqual(math.modf(INF), (0.0, INF)) |
| 1000 | self.assertEqual(math.modf(NINF), (-0.0, NINF)) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1001 | |
| 1002 | modf_nan = math.modf(NAN) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1003 | self.assertTrue(math.isnan(modf_nan[0])) |
| 1004 | self.assertTrue(math.isnan(modf_nan[1])) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1005 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1006 | def testPow(self): |
| 1007 | self.assertRaises(TypeError, math.pow) |
| 1008 | self.ftest('pow(0,1)', math.pow(0,1), 0) |
| 1009 | self.ftest('pow(1,0)', math.pow(1,0), 1) |
| 1010 | self.ftest('pow(2,1)', math.pow(2,1), 2) |
| 1011 | self.ftest('pow(2,-1)', math.pow(2,-1), 0.5) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1012 | self.assertEqual(math.pow(INF, 1), INF) |
| 1013 | self.assertEqual(math.pow(NINF, 1), NINF) |
| 1014 | self.assertEqual((math.pow(1, INF)), 1.) |
| 1015 | self.assertEqual((math.pow(1, NINF)), 1.) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1016 | self.assertTrue(math.isnan(math.pow(NAN, 1))) |
| 1017 | self.assertTrue(math.isnan(math.pow(2, NAN))) |
| 1018 | self.assertTrue(math.isnan(math.pow(0, NAN))) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1019 | self.assertEqual(math.pow(1, NAN), 1) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1020 | |
| 1021 | # pow(0., x) |
| 1022 | self.assertEqual(math.pow(0., INF), 0.) |
| 1023 | self.assertEqual(math.pow(0., 3.), 0.) |
| 1024 | self.assertEqual(math.pow(0., 2.3), 0.) |
| 1025 | self.assertEqual(math.pow(0., 2.), 0.) |
| 1026 | self.assertEqual(math.pow(0., 0.), 1.) |
| 1027 | self.assertEqual(math.pow(0., -0.), 1.) |
| 1028 | self.assertRaises(ValueError, math.pow, 0., -2.) |
| 1029 | self.assertRaises(ValueError, math.pow, 0., -2.3) |
| 1030 | self.assertRaises(ValueError, math.pow, 0., -3.) |
| 1031 | self.assertRaises(ValueError, math.pow, 0., NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1032 | self.assertTrue(math.isnan(math.pow(0., NAN))) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1033 | |
| 1034 | # pow(INF, x) |
| 1035 | self.assertEqual(math.pow(INF, INF), INF) |
| 1036 | self.assertEqual(math.pow(INF, 3.), INF) |
| 1037 | self.assertEqual(math.pow(INF, 2.3), INF) |
| 1038 | self.assertEqual(math.pow(INF, 2.), INF) |
| 1039 | self.assertEqual(math.pow(INF, 0.), 1.) |
| 1040 | self.assertEqual(math.pow(INF, -0.), 1.) |
| 1041 | self.assertEqual(math.pow(INF, -2.), 0.) |
| 1042 | self.assertEqual(math.pow(INF, -2.3), 0.) |
| 1043 | self.assertEqual(math.pow(INF, -3.), 0.) |
| 1044 | self.assertEqual(math.pow(INF, NINF), 0.) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1045 | self.assertTrue(math.isnan(math.pow(INF, NAN))) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1046 | |
| 1047 | # pow(-0., x) |
| 1048 | self.assertEqual(math.pow(-0., INF), 0.) |
| 1049 | self.assertEqual(math.pow(-0., 3.), -0.) |
| 1050 | self.assertEqual(math.pow(-0., 2.3), 0.) |
| 1051 | self.assertEqual(math.pow(-0., 2.), 0.) |
| 1052 | self.assertEqual(math.pow(-0., 0.), 1.) |
| 1053 | self.assertEqual(math.pow(-0., -0.), 1.) |
| 1054 | self.assertRaises(ValueError, math.pow, -0., -2.) |
| 1055 | self.assertRaises(ValueError, math.pow, -0., -2.3) |
| 1056 | self.assertRaises(ValueError, math.pow, -0., -3.) |
| 1057 | self.assertRaises(ValueError, math.pow, -0., NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1058 | self.assertTrue(math.isnan(math.pow(-0., NAN))) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1059 | |
| 1060 | # pow(NINF, x) |
| 1061 | self.assertEqual(math.pow(NINF, INF), INF) |
| 1062 | self.assertEqual(math.pow(NINF, 3.), NINF) |
| 1063 | self.assertEqual(math.pow(NINF, 2.3), INF) |
| 1064 | self.assertEqual(math.pow(NINF, 2.), INF) |
| 1065 | self.assertEqual(math.pow(NINF, 0.), 1.) |
| 1066 | self.assertEqual(math.pow(NINF, -0.), 1.) |
| 1067 | self.assertEqual(math.pow(NINF, -2.), 0.) |
| 1068 | self.assertEqual(math.pow(NINF, -2.3), 0.) |
| 1069 | self.assertEqual(math.pow(NINF, -3.), -0.) |
| 1070 | self.assertEqual(math.pow(NINF, NINF), 0.) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1071 | self.assertTrue(math.isnan(math.pow(NINF, NAN))) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1072 | |
| 1073 | # pow(-1, x) |
| 1074 | self.assertEqual(math.pow(-1., INF), 1.) |
| 1075 | self.assertEqual(math.pow(-1., 3.), -1.) |
| 1076 | self.assertRaises(ValueError, math.pow, -1., 2.3) |
| 1077 | self.assertEqual(math.pow(-1., 2.), 1.) |
| 1078 | self.assertEqual(math.pow(-1., 0.), 1.) |
| 1079 | self.assertEqual(math.pow(-1., -0.), 1.) |
| 1080 | self.assertEqual(math.pow(-1., -2.), 1.) |
| 1081 | self.assertRaises(ValueError, math.pow, -1., -2.3) |
| 1082 | self.assertEqual(math.pow(-1., -3.), -1.) |
| 1083 | self.assertEqual(math.pow(-1., NINF), 1.) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1084 | self.assertTrue(math.isnan(math.pow(-1., NAN))) |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1085 | |
| 1086 | # pow(1, x) |
| 1087 | self.assertEqual(math.pow(1., INF), 1.) |
| 1088 | self.assertEqual(math.pow(1., 3.), 1.) |
| 1089 | self.assertEqual(math.pow(1., 2.3), 1.) |
| 1090 | self.assertEqual(math.pow(1., 2.), 1.) |
| 1091 | self.assertEqual(math.pow(1., 0.), 1.) |
| 1092 | self.assertEqual(math.pow(1., -0.), 1.) |
| 1093 | self.assertEqual(math.pow(1., -2.), 1.) |
| 1094 | self.assertEqual(math.pow(1., -2.3), 1.) |
| 1095 | self.assertEqual(math.pow(1., -3.), 1.) |
| 1096 | self.assertEqual(math.pow(1., NINF), 1.) |
| 1097 | self.assertEqual(math.pow(1., NAN), 1.) |
| 1098 | |
| 1099 | # pow(x, 0) should be 1 for any x |
| 1100 | self.assertEqual(math.pow(2.3, 0.), 1.) |
| 1101 | self.assertEqual(math.pow(-2.3, 0.), 1.) |
| 1102 | self.assertEqual(math.pow(NAN, 0.), 1.) |
| 1103 | self.assertEqual(math.pow(2.3, -0.), 1.) |
| 1104 | self.assertEqual(math.pow(-2.3, -0.), 1.) |
| 1105 | self.assertEqual(math.pow(NAN, -0.), 1.) |
| 1106 | |
| 1107 | # pow(x, y) is invalid if x is negative and y is not integral |
| 1108 | self.assertRaises(ValueError, math.pow, -1., 2.3) |
| 1109 | self.assertRaises(ValueError, math.pow, -15., -3.1) |
| 1110 | |
| 1111 | # pow(x, NINF) |
| 1112 | self.assertEqual(math.pow(1.9, NINF), 0.) |
| 1113 | self.assertEqual(math.pow(1.1, NINF), 0.) |
| 1114 | self.assertEqual(math.pow(0.9, NINF), INF) |
| 1115 | self.assertEqual(math.pow(0.1, NINF), INF) |
| 1116 | self.assertEqual(math.pow(-0.1, NINF), INF) |
| 1117 | self.assertEqual(math.pow(-0.9, NINF), INF) |
| 1118 | self.assertEqual(math.pow(-1.1, NINF), 0.) |
| 1119 | self.assertEqual(math.pow(-1.9, NINF), 0.) |
| 1120 | |
| 1121 | # pow(x, INF) |
| 1122 | self.assertEqual(math.pow(1.9, INF), INF) |
| 1123 | self.assertEqual(math.pow(1.1, INF), INF) |
| 1124 | self.assertEqual(math.pow(0.9, INF), 0.) |
| 1125 | self.assertEqual(math.pow(0.1, INF), 0.) |
| 1126 | self.assertEqual(math.pow(-0.1, INF), 0.) |
| 1127 | self.assertEqual(math.pow(-0.9, INF), 0.) |
| 1128 | self.assertEqual(math.pow(-1.1, INF), INF) |
| 1129 | self.assertEqual(math.pow(-1.9, INF), INF) |
| 1130 | |
| 1131 | # pow(x, y) should work for x negative, y an integer |
| 1132 | self.ftest('(-2.)**3.', math.pow(-2.0, 3.0), -8.0) |
| 1133 | self.ftest('(-2.)**2.', math.pow(-2.0, 2.0), 4.0) |
| 1134 | self.ftest('(-2.)**1.', math.pow(-2.0, 1.0), -2.0) |
| 1135 | self.ftest('(-2.)**0.', math.pow(-2.0, 0.0), 1.0) |
| 1136 | self.ftest('(-2.)**-0.', math.pow(-2.0, -0.0), 1.0) |
| 1137 | self.ftest('(-2.)**-1.', math.pow(-2.0, -1.0), -0.5) |
| 1138 | self.ftest('(-2.)**-2.', math.pow(-2.0, -2.0), 0.25) |
| 1139 | self.ftest('(-2.)**-3.', math.pow(-2.0, -3.0), -0.125) |
| 1140 | self.assertRaises(ValueError, math.pow, -2.0, -0.5) |
| 1141 | self.assertRaises(ValueError, math.pow, -2.0, 0.5) |
| 1142 | |
| 1143 | # the following tests have been commented out since they don't |
| 1144 | # really belong here: the implementation of ** for floats is |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 1145 | # independent of the implementation of math.pow |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1146 | #self.assertEqual(1**NAN, 1) |
| 1147 | #self.assertEqual(1**INF, 1) |
| 1148 | #self.assertEqual(1**NINF, 1) |
| 1149 | #self.assertEqual(1**0, 1) |
| 1150 | #self.assertEqual(1.**NAN, 1) |
| 1151 | #self.assertEqual(1.**INF, 1) |
| 1152 | #self.assertEqual(1.**NINF, 1) |
| 1153 | #self.assertEqual(1.**0, 1) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 1154 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1155 | def testRadians(self): |
| 1156 | self.assertRaises(TypeError, math.radians) |
| 1157 | self.ftest('radians(180)', math.radians(180), math.pi) |
| 1158 | self.ftest('radians(90)', math.radians(90), math.pi/2) |
| 1159 | self.ftest('radians(-45)', math.radians(-45), -math.pi/4) |
Mark Dickinson | 31ba1c3 | 2016-09-04 12:29:14 +0100 | [diff] [blame] | 1160 | self.ftest('radians(0)', math.radians(0), 0) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 1161 | |
Mark Dickinson | a0ce375 | 2017-04-05 18:34:27 +0100 | [diff] [blame] | 1162 | @requires_IEEE_754 |
| 1163 | def testRemainder(self): |
| 1164 | from fractions import Fraction |
| 1165 | |
| 1166 | def validate_spec(x, y, r): |
| 1167 | """ |
| 1168 | Check that r matches remainder(x, y) according to the IEEE 754 |
| 1169 | specification. Assumes that x, y and r are finite and y is nonzero. |
| 1170 | """ |
| 1171 | fx, fy, fr = Fraction(x), Fraction(y), Fraction(r) |
| 1172 | # r should not exceed y/2 in absolute value |
| 1173 | self.assertLessEqual(abs(fr), abs(fy/2)) |
| 1174 | # x - r should be an exact integer multiple of y |
| 1175 | n = (fx - fr) / fy |
| 1176 | self.assertEqual(n, int(n)) |
| 1177 | if abs(fr) == abs(fy/2): |
| 1178 | # If |r| == |y/2|, n should be even. |
| 1179 | self.assertEqual(n/2, int(n/2)) |
| 1180 | |
| 1181 | # triples (x, y, remainder(x, y)) in hexadecimal form. |
| 1182 | testcases = [ |
| 1183 | # Remainders modulo 1, showing the ties-to-even behaviour. |
| 1184 | '-4.0 1 -0.0', |
| 1185 | '-3.8 1 0.8', |
| 1186 | '-3.0 1 -0.0', |
| 1187 | '-2.8 1 -0.8', |
| 1188 | '-2.0 1 -0.0', |
| 1189 | '-1.8 1 0.8', |
| 1190 | '-1.0 1 -0.0', |
| 1191 | '-0.8 1 -0.8', |
| 1192 | '-0.0 1 -0.0', |
| 1193 | ' 0.0 1 0.0', |
| 1194 | ' 0.8 1 0.8', |
| 1195 | ' 1.0 1 0.0', |
| 1196 | ' 1.8 1 -0.8', |
| 1197 | ' 2.0 1 0.0', |
| 1198 | ' 2.8 1 0.8', |
| 1199 | ' 3.0 1 0.0', |
| 1200 | ' 3.8 1 -0.8', |
| 1201 | ' 4.0 1 0.0', |
| 1202 | |
| 1203 | # Reductions modulo 2*pi |
| 1204 | '0x0.0p+0 0x1.921fb54442d18p+2 0x0.0p+0', |
| 1205 | '0x1.921fb54442d18p+0 0x1.921fb54442d18p+2 0x1.921fb54442d18p+0', |
| 1206 | '0x1.921fb54442d17p+1 0x1.921fb54442d18p+2 0x1.921fb54442d17p+1', |
| 1207 | '0x1.921fb54442d18p+1 0x1.921fb54442d18p+2 0x1.921fb54442d18p+1', |
| 1208 | '0x1.921fb54442d19p+1 0x1.921fb54442d18p+2 -0x1.921fb54442d17p+1', |
| 1209 | '0x1.921fb54442d17p+2 0x1.921fb54442d18p+2 -0x0.0000000000001p+2', |
| 1210 | '0x1.921fb54442d18p+2 0x1.921fb54442d18p+2 0x0p0', |
| 1211 | '0x1.921fb54442d19p+2 0x1.921fb54442d18p+2 0x0.0000000000001p+2', |
| 1212 | '0x1.2d97c7f3321d1p+3 0x1.921fb54442d18p+2 0x1.921fb54442d14p+1', |
| 1213 | '0x1.2d97c7f3321d2p+3 0x1.921fb54442d18p+2 -0x1.921fb54442d18p+1', |
| 1214 | '0x1.2d97c7f3321d3p+3 0x1.921fb54442d18p+2 -0x1.921fb54442d14p+1', |
| 1215 | '0x1.921fb54442d17p+3 0x1.921fb54442d18p+2 -0x0.0000000000001p+3', |
| 1216 | '0x1.921fb54442d18p+3 0x1.921fb54442d18p+2 0x0p0', |
| 1217 | '0x1.921fb54442d19p+3 0x1.921fb54442d18p+2 0x0.0000000000001p+3', |
| 1218 | '0x1.f6a7a2955385dp+3 0x1.921fb54442d18p+2 0x1.921fb54442d14p+1', |
| 1219 | '0x1.f6a7a2955385ep+3 0x1.921fb54442d18p+2 0x1.921fb54442d18p+1', |
| 1220 | '0x1.f6a7a2955385fp+3 0x1.921fb54442d18p+2 -0x1.921fb54442d14p+1', |
| 1221 | '0x1.1475cc9eedf00p+5 0x1.921fb54442d18p+2 0x1.921fb54442d10p+1', |
| 1222 | '0x1.1475cc9eedf01p+5 0x1.921fb54442d18p+2 -0x1.921fb54442d10p+1', |
| 1223 | |
| 1224 | # Symmetry with respect to signs. |
| 1225 | ' 1 0.c 0.4', |
| 1226 | '-1 0.c -0.4', |
| 1227 | ' 1 -0.c 0.4', |
| 1228 | '-1 -0.c -0.4', |
| 1229 | ' 1.4 0.c -0.4', |
| 1230 | '-1.4 0.c 0.4', |
| 1231 | ' 1.4 -0.c -0.4', |
| 1232 | '-1.4 -0.c 0.4', |
| 1233 | |
| 1234 | # Huge modulus, to check that the underlying algorithm doesn't |
| 1235 | # rely on 2.0 * modulus being representable. |
| 1236 | '0x1.dp+1023 0x1.4p+1023 0x0.9p+1023', |
| 1237 | '0x1.ep+1023 0x1.4p+1023 -0x0.ap+1023', |
| 1238 | '0x1.fp+1023 0x1.4p+1023 -0x0.9p+1023', |
| 1239 | ] |
| 1240 | |
| 1241 | for case in testcases: |
| 1242 | with self.subTest(case=case): |
| 1243 | x_hex, y_hex, expected_hex = case.split() |
| 1244 | x = float.fromhex(x_hex) |
| 1245 | y = float.fromhex(y_hex) |
| 1246 | expected = float.fromhex(expected_hex) |
| 1247 | validate_spec(x, y, expected) |
| 1248 | actual = math.remainder(x, y) |
| 1249 | # Cheap way of checking that the floats are |
| 1250 | # as identical as we need them to be. |
| 1251 | self.assertEqual(actual.hex(), expected.hex()) |
| 1252 | |
| 1253 | # Test tiny subnormal modulus: there's potential for |
| 1254 | # getting the implementation wrong here (for example, |
| 1255 | # by assuming that modulus/2 is exactly representable). |
| 1256 | tiny = float.fromhex('1p-1074') # min +ve subnormal |
| 1257 | for n in range(-25, 25): |
| 1258 | if n == 0: |
| 1259 | continue |
| 1260 | y = n * tiny |
| 1261 | for m in range(100): |
| 1262 | x = m * tiny |
| 1263 | actual = math.remainder(x, y) |
| 1264 | validate_spec(x, y, actual) |
| 1265 | actual = math.remainder(-x, y) |
| 1266 | validate_spec(-x, y, actual) |
| 1267 | |
| 1268 | # Special values. |
| 1269 | # NaNs should propagate as usual. |
| 1270 | for value in [NAN, 0.0, -0.0, 2.0, -2.3, NINF, INF]: |
| 1271 | self.assertIsNaN(math.remainder(NAN, value)) |
| 1272 | self.assertIsNaN(math.remainder(value, NAN)) |
| 1273 | |
| 1274 | # remainder(x, inf) is x, for non-nan non-infinite x. |
| 1275 | for value in [-2.3, -0.0, 0.0, 2.3]: |
| 1276 | self.assertEqual(math.remainder(value, INF), value) |
| 1277 | self.assertEqual(math.remainder(value, NINF), value) |
| 1278 | |
| 1279 | # remainder(x, 0) and remainder(infinity, x) for non-NaN x are invalid |
| 1280 | # operations according to IEEE 754-2008 7.2(f), and should raise. |
| 1281 | for value in [NINF, -2.3, -0.0, 0.0, 2.3, INF]: |
| 1282 | with self.assertRaises(ValueError): |
| 1283 | math.remainder(INF, value) |
| 1284 | with self.assertRaises(ValueError): |
| 1285 | math.remainder(NINF, value) |
| 1286 | with self.assertRaises(ValueError): |
| 1287 | math.remainder(value, 0.0) |
| 1288 | with self.assertRaises(ValueError): |
| 1289 | math.remainder(value, -0.0) |
| 1290 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1291 | def testSin(self): |
| 1292 | self.assertRaises(TypeError, math.sin) |
| 1293 | self.ftest('sin(0)', math.sin(0), 0) |
| 1294 | self.ftest('sin(pi/2)', math.sin(math.pi/2), 1) |
| 1295 | self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1296 | try: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1297 | self.assertTrue(math.isnan(math.sin(INF))) |
| 1298 | self.assertTrue(math.isnan(math.sin(NINF))) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1299 | except ValueError: |
| 1300 | self.assertRaises(ValueError, math.sin, INF) |
| 1301 | self.assertRaises(ValueError, math.sin, NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1302 | self.assertTrue(math.isnan(math.sin(NAN))) |
Guido van Rossum | fcce630 | 1996-08-08 18:26:25 +0000 | [diff] [blame] | 1303 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1304 | def testSinh(self): |
| 1305 | self.assertRaises(TypeError, math.sinh) |
| 1306 | self.ftest('sinh(0)', math.sinh(0), 0) |
| 1307 | self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1) |
| 1308 | 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] | 1309 | self.assertEqual(math.sinh(INF), INF) |
| 1310 | self.assertEqual(math.sinh(NINF), NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1311 | self.assertTrue(math.isnan(math.sinh(NAN))) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 1312 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1313 | def testSqrt(self): |
| 1314 | self.assertRaises(TypeError, math.sqrt) |
| 1315 | self.ftest('sqrt(0)', math.sqrt(0), 0) |
| 1316 | self.ftest('sqrt(1)', math.sqrt(1), 1) |
| 1317 | self.ftest('sqrt(4)', math.sqrt(4), 2) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 1318 | self.assertEqual(math.sqrt(INF), INF) |
Mark Dickinson | 31ba1c3 | 2016-09-04 12:29:14 +0100 | [diff] [blame] | 1319 | self.assertRaises(ValueError, math.sqrt, -1) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1320 | self.assertRaises(ValueError, math.sqrt, NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1321 | self.assertTrue(math.isnan(math.sqrt(NAN))) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 1322 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1323 | def testTan(self): |
| 1324 | self.assertRaises(TypeError, math.tan) |
| 1325 | self.ftest('tan(0)', math.tan(0), 0) |
| 1326 | self.ftest('tan(pi/4)', math.tan(math.pi/4), 1) |
| 1327 | self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1328 | try: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1329 | self.assertTrue(math.isnan(math.tan(INF))) |
| 1330 | self.assertTrue(math.isnan(math.tan(NINF))) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1331 | except: |
| 1332 | self.assertRaises(ValueError, math.tan, INF) |
| 1333 | self.assertRaises(ValueError, math.tan, NINF) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1334 | self.assertTrue(math.isnan(math.tan(NAN))) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 1335 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1336 | def testTanh(self): |
| 1337 | self.assertRaises(TypeError, math.tanh) |
| 1338 | self.ftest('tanh(0)', math.tanh(0), 0) |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 1339 | self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0, |
| 1340 | abs_tol=ulp(1)) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1341 | self.ftest('tanh(inf)', math.tanh(INF), 1) |
| 1342 | self.ftest('tanh(-inf)', math.tanh(NINF), -1) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1343 | self.assertTrue(math.isnan(math.tanh(NAN))) |
Victor Stinner | be3da38 | 2010-11-07 14:14:27 +0000 | [diff] [blame] | 1344 | |
| 1345 | @requires_IEEE_754 |
| 1346 | @unittest.skipIf(sysconfig.get_config_var('TANH_PRESERVES_ZERO_SIGN') == 0, |
| 1347 | "system tanh() function doesn't copy the sign") |
| 1348 | def testTanhSign(self): |
Christian Heimes | e57950f | 2008-04-21 13:08:03 +0000 | [diff] [blame] | 1349 | # check that tanh(-0.) == -0. on IEEE 754 systems |
Victor Stinner | be3da38 | 2010-11-07 14:14:27 +0000 | [diff] [blame] | 1350 | self.assertEqual(math.tanh(-0.), -0.) |
| 1351 | self.assertEqual(math.copysign(1., math.tanh(-0.)), |
| 1352 | math.copysign(1., -0.)) |
Tim Peters | 1d12061 | 2000-10-12 06:10:25 +0000 | [diff] [blame] | 1353 | |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 1354 | def test_trunc(self): |
| 1355 | self.assertEqual(math.trunc(1), 1) |
| 1356 | self.assertEqual(math.trunc(-1), -1) |
| 1357 | self.assertEqual(type(math.trunc(1)), int) |
| 1358 | self.assertEqual(type(math.trunc(1.5)), int) |
| 1359 | self.assertEqual(math.trunc(1.5), 1) |
| 1360 | self.assertEqual(math.trunc(-1.5), -1) |
| 1361 | self.assertEqual(math.trunc(1.999999), 1) |
| 1362 | self.assertEqual(math.trunc(-1.999999), -1) |
| 1363 | self.assertEqual(math.trunc(-0.999999), -0) |
| 1364 | self.assertEqual(math.trunc(-100.999), -100) |
| 1365 | |
| 1366 | class TestTrunc(object): |
| 1367 | def __trunc__(self): |
| 1368 | return 23 |
| 1369 | |
| 1370 | class TestNoTrunc(object): |
| 1371 | pass |
| 1372 | |
| 1373 | self.assertEqual(math.trunc(TestTrunc()), 23) |
| 1374 | |
| 1375 | self.assertRaises(TypeError, math.trunc) |
| 1376 | self.assertRaises(TypeError, math.trunc, 1, 2) |
| 1377 | self.assertRaises(TypeError, math.trunc, TestNoTrunc()) |
| 1378 | |
Mark Dickinson | 8e0c996 | 2010-07-11 17:38:24 +0000 | [diff] [blame] | 1379 | def testIsfinite(self): |
| 1380 | self.assertTrue(math.isfinite(0.0)) |
| 1381 | self.assertTrue(math.isfinite(-0.0)) |
| 1382 | self.assertTrue(math.isfinite(1.0)) |
| 1383 | self.assertTrue(math.isfinite(-1.0)) |
| 1384 | self.assertFalse(math.isfinite(float("nan"))) |
| 1385 | self.assertFalse(math.isfinite(float("inf"))) |
| 1386 | self.assertFalse(math.isfinite(float("-inf"))) |
| 1387 | |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1388 | def testIsnan(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1389 | self.assertTrue(math.isnan(float("nan"))) |
Mark Dickinson | 31ba1c3 | 2016-09-04 12:29:14 +0100 | [diff] [blame] | 1390 | self.assertTrue(math.isnan(float("-nan"))) |
| 1391 | self.assertTrue(math.isnan(float("inf") * 0.)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1392 | self.assertFalse(math.isnan(float("inf"))) |
| 1393 | self.assertFalse(math.isnan(0.)) |
| 1394 | self.assertFalse(math.isnan(1.)) |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1395 | |
| 1396 | def testIsinf(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1397 | self.assertTrue(math.isinf(float("inf"))) |
| 1398 | self.assertTrue(math.isinf(float("-inf"))) |
| 1399 | self.assertTrue(math.isinf(1E400)) |
| 1400 | self.assertTrue(math.isinf(-1E400)) |
| 1401 | self.assertFalse(math.isinf(float("nan"))) |
| 1402 | self.assertFalse(math.isinf(0.)) |
| 1403 | self.assertFalse(math.isinf(1.)) |
Christian Heimes | 072c0f1 | 2008-01-03 23:01:04 +0000 | [diff] [blame] | 1404 | |
Mark Dickinson | a5d0c7c | 2015-01-11 11:55:29 +0000 | [diff] [blame] | 1405 | @requires_IEEE_754 |
| 1406 | def test_nan_constant(self): |
| 1407 | self.assertTrue(math.isnan(math.nan)) |
| 1408 | |
| 1409 | @requires_IEEE_754 |
| 1410 | def test_inf_constant(self): |
| 1411 | self.assertTrue(math.isinf(math.inf)) |
| 1412 | self.assertGreater(math.inf, 0.0) |
| 1413 | self.assertEqual(math.inf, float("inf")) |
| 1414 | self.assertEqual(-math.inf, float("-inf")) |
| 1415 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1416 | # RED_FLAG 16-Oct-2000 Tim |
| 1417 | # While 2.0 is more consistent about exceptions than previous releases, it |
| 1418 | # still fails this part of the test on some platforms. For now, we only |
| 1419 | # *run* test_exceptions() in verbose mode, so that this isn't normally |
| 1420 | # tested. |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 1421 | @unittest.skipUnless(verbose, 'requires verbose mode') |
| 1422 | def test_exceptions(self): |
| 1423 | try: |
| 1424 | x = math.exp(-1000000000) |
| 1425 | except: |
| 1426 | # mathmodule.c is failing to weed out underflows from libm, or |
| 1427 | # we've got an fp format with huge dynamic range |
| 1428 | self.fail("underflowing exp() should not have raised " |
| 1429 | "an exception") |
| 1430 | if x != 0: |
| 1431 | self.fail("underflowing exp() should have returned 0") |
Tim Peters | 98c8184 | 2000-10-16 17:35:13 +0000 | [diff] [blame] | 1432 | |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 1433 | # If this fails, probably using a strict IEEE-754 conforming libm, and x |
| 1434 | # is +Inf afterwards. But Python wants overflows detected by default. |
| 1435 | try: |
| 1436 | x = math.exp(1000000000) |
| 1437 | except OverflowError: |
| 1438 | pass |
| 1439 | else: |
| 1440 | self.fail("overflowing exp() didn't trigger OverflowError") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1441 | |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 1442 | # If this fails, it could be a puzzle. One odd possibility is that |
| 1443 | # mathmodule.c's macros are getting confused while comparing |
| 1444 | # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE |
| 1445 | # as a result (and so raising OverflowError instead). |
| 1446 | try: |
| 1447 | x = math.sqrt(-1.0) |
| 1448 | except ValueError: |
| 1449 | pass |
| 1450 | else: |
| 1451 | self.fail("sqrt(-1) didn't raise ValueError") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1452 | |
Mark Dickinson | 6356623 | 2009-09-18 21:04:19 +0000 | [diff] [blame] | 1453 | @requires_IEEE_754 |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1454 | def test_testfile(self): |
Mark Dickinson | 8574654 | 2016-09-04 09:58:51 +0100 | [diff] [blame] | 1455 | # Some tests need to be skipped on ancient OS X versions. |
| 1456 | # See issue #27953. |
| 1457 | SKIP_ON_TIGER = {'tan0064'} |
| 1458 | |
| 1459 | osx_version = None |
| 1460 | if sys.platform == 'darwin': |
| 1461 | version_txt = platform.mac_ver()[0] |
| 1462 | try: |
| 1463 | osx_version = tuple(map(int, version_txt.split('.'))) |
| 1464 | except ValueError: |
| 1465 | pass |
| 1466 | |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 1467 | fail_fmt = "{}: {}({!r}): {}" |
| 1468 | |
| 1469 | failures = [] |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1470 | for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file): |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 1471 | # Skip if either the input or result is complex |
| 1472 | if ai != 0.0 or ei != 0.0: |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1473 | continue |
| 1474 | if fn in ['rect', 'polar']: |
| 1475 | # no real versions of rect, polar |
| 1476 | continue |
Mark Dickinson | 8574654 | 2016-09-04 09:58:51 +0100 | [diff] [blame] | 1477 | # Skip certain tests on OS X 10.4. |
| 1478 | if osx_version is not None and osx_version < (10, 5): |
| 1479 | if id in SKIP_ON_TIGER: |
| 1480 | continue |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 1481 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1482 | func = getattr(math, fn) |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 1483 | |
| 1484 | if 'invalid' in flags or 'divide-by-zero' in flags: |
| 1485 | er = 'ValueError' |
| 1486 | elif 'overflow' in flags: |
| 1487 | er = 'OverflowError' |
| 1488 | |
Christian Heimes | a342c01 | 2008-04-20 21:01:16 +0000 | [diff] [blame] | 1489 | try: |
| 1490 | result = func(ar) |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 1491 | except ValueError: |
| 1492 | result = 'ValueError' |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 1493 | except OverflowError: |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 1494 | result = 'OverflowError' |
| 1495 | |
| 1496 | # Default tolerances |
| 1497 | ulp_tol, abs_tol = 5, 0.0 |
| 1498 | |
| 1499 | failure = result_check(er, result, ulp_tol, abs_tol) |
| 1500 | if failure is None: |
| 1501 | continue |
| 1502 | |
| 1503 | msg = fail_fmt.format(id, fn, ar, failure) |
| 1504 | failures.append(msg) |
| 1505 | |
| 1506 | if failures: |
| 1507 | self.fail('Failures in test_testfile:\n ' + |
| 1508 | '\n '.join(failures)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1509 | |
Victor Stinner | be3da38 | 2010-11-07 14:14:27 +0000 | [diff] [blame] | 1510 | @requires_IEEE_754 |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 1511 | def test_mtestfile(self): |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 1512 | fail_fmt = "{}: {}({!r}): {}" |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 1513 | |
| 1514 | failures = [] |
| 1515 | for id, fn, arg, expected, flags in parse_mtestfile(math_testcases): |
| 1516 | func = getattr(math, fn) |
| 1517 | |
| 1518 | if 'invalid' in flags or 'divide-by-zero' in flags: |
| 1519 | expected = 'ValueError' |
| 1520 | elif 'overflow' in flags: |
| 1521 | expected = 'OverflowError' |
| 1522 | |
| 1523 | try: |
| 1524 | got = func(arg) |
| 1525 | except ValueError: |
| 1526 | got = 'ValueError' |
| 1527 | except OverflowError: |
| 1528 | got = 'OverflowError' |
| 1529 | |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 1530 | # Default tolerances |
| 1531 | ulp_tol, abs_tol = 5, 0.0 |
Mark Dickinson | bcdf9da | 2010-06-13 10:52:38 +0000 | [diff] [blame] | 1532 | |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 1533 | # Exceptions to the defaults |
| 1534 | if fn == 'gamma': |
| 1535 | # Experimental results on one platform gave |
| 1536 | # an accuracy of <= 10 ulps across the entire float |
| 1537 | # domain. We weaken that to require 20 ulp accuracy. |
| 1538 | ulp_tol = 20 |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 1539 | |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 1540 | elif fn == 'lgamma': |
| 1541 | # we use a weaker accuracy test for lgamma; |
| 1542 | # lgamma only achieves an absolute error of |
| 1543 | # a few multiples of the machine accuracy, in |
| 1544 | # general. |
| 1545 | abs_tol = 1e-15 |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 1546 | |
Mark Dickinson | 96f774d | 2016-09-03 19:30:22 +0100 | [diff] [blame] | 1547 | elif fn == 'erfc' and arg >= 0.0: |
| 1548 | # erfc has less-than-ideal accuracy for large |
| 1549 | # arguments (x ~ 25 or so), mainly due to the |
| 1550 | # error involved in computing exp(-x*x). |
| 1551 | # |
| 1552 | # Observed between CPython and mpmath at 25 dp: |
| 1553 | # x < 0 : err <= 2 ulp |
| 1554 | # 0 <= x < 1 : err <= 10 ulp |
| 1555 | # 1 <= x < 10 : err <= 100 ulp |
| 1556 | # 10 <= x < 20 : err <= 300 ulp |
| 1557 | # 20 <= x : < 600 ulp |
| 1558 | # |
| 1559 | if arg < 1.0: |
| 1560 | ulp_tol = 10 |
| 1561 | elif arg < 10.0: |
| 1562 | ulp_tol = 100 |
| 1563 | else: |
| 1564 | ulp_tol = 1000 |
| 1565 | |
| 1566 | failure = result_check(expected, got, ulp_tol, abs_tol) |
| 1567 | if failure is None: |
| 1568 | continue |
| 1569 | |
| 1570 | msg = fail_fmt.format(id, fn, arg, failure) |
| 1571 | failures.append(msg) |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 1572 | |
| 1573 | if failures: |
| 1574 | self.fail('Failures in test_mtestfile:\n ' + |
| 1575 | '\n '.join(failures)) |
| 1576 | |
Mark Dickinson | a0ce375 | 2017-04-05 18:34:27 +0100 | [diff] [blame] | 1577 | # Custom assertions. |
| 1578 | |
| 1579 | def assertIsNaN(self, value): |
| 1580 | if not math.isnan(value): |
| 1581 | self.fail("Expected a NaN, got {!r}.".format(value)) |
| 1582 | |
Mark Dickinson | 12c4bdb | 2009-09-28 19:21:11 +0000 | [diff] [blame] | 1583 | |
Tal Einat | d5519ed | 2015-05-31 22:05:00 +0300 | [diff] [blame] | 1584 | class IsCloseTests(unittest.TestCase): |
Mike | 53f7a7c | 2017-12-14 14:04:53 +0300 | [diff] [blame] | 1585 | isclose = math.isclose # subclasses should override this |
Tal Einat | d5519ed | 2015-05-31 22:05:00 +0300 | [diff] [blame] | 1586 | |
| 1587 | def assertIsClose(self, a, b, *args, **kwargs): |
| 1588 | self.assertTrue(self.isclose(a, b, *args, **kwargs), |
| 1589 | msg="%s and %s should be close!" % (a, b)) |
| 1590 | |
| 1591 | def assertIsNotClose(self, a, b, *args, **kwargs): |
| 1592 | self.assertFalse(self.isclose(a, b, *args, **kwargs), |
| 1593 | msg="%s and %s should not be close!" % (a, b)) |
| 1594 | |
| 1595 | def assertAllClose(self, examples, *args, **kwargs): |
| 1596 | for a, b in examples: |
| 1597 | self.assertIsClose(a, b, *args, **kwargs) |
| 1598 | |
| 1599 | def assertAllNotClose(self, examples, *args, **kwargs): |
| 1600 | for a, b in examples: |
| 1601 | self.assertIsNotClose(a, b, *args, **kwargs) |
| 1602 | |
| 1603 | def test_negative_tolerances(self): |
| 1604 | # ValueError should be raised if either tolerance is less than zero |
| 1605 | with self.assertRaises(ValueError): |
| 1606 | self.assertIsClose(1, 1, rel_tol=-1e-100) |
| 1607 | with self.assertRaises(ValueError): |
| 1608 | self.assertIsClose(1, 1, rel_tol=1e-100, abs_tol=-1e10) |
| 1609 | |
| 1610 | def test_identical(self): |
| 1611 | # identical values must test as close |
| 1612 | identical_examples = [(2.0, 2.0), |
| 1613 | (0.1e200, 0.1e200), |
| 1614 | (1.123e-300, 1.123e-300), |
| 1615 | (12345, 12345.0), |
| 1616 | (0.0, -0.0), |
| 1617 | (345678, 345678)] |
| 1618 | self.assertAllClose(identical_examples, rel_tol=0.0, abs_tol=0.0) |
| 1619 | |
| 1620 | def test_eight_decimal_places(self): |
| 1621 | # examples that are close to 1e-8, but not 1e-9 |
| 1622 | eight_decimal_places_examples = [(1e8, 1e8 + 1), |
| 1623 | (-1e-8, -1.000000009e-8), |
| 1624 | (1.12345678, 1.12345679)] |
| 1625 | self.assertAllClose(eight_decimal_places_examples, rel_tol=1e-8) |
| 1626 | self.assertAllNotClose(eight_decimal_places_examples, rel_tol=1e-9) |
| 1627 | |
| 1628 | def test_near_zero(self): |
| 1629 | # values close to zero |
| 1630 | near_zero_examples = [(1e-9, 0.0), |
| 1631 | (-1e-9, 0.0), |
| 1632 | (-1e-150, 0.0)] |
| 1633 | # these should not be close to any rel_tol |
| 1634 | self.assertAllNotClose(near_zero_examples, rel_tol=0.9) |
| 1635 | # these should be close to abs_tol=1e-8 |
| 1636 | self.assertAllClose(near_zero_examples, abs_tol=1e-8) |
| 1637 | |
| 1638 | def test_identical_infinite(self): |
| 1639 | # these are close regardless of tolerance -- i.e. they are equal |
| 1640 | self.assertIsClose(INF, INF) |
| 1641 | self.assertIsClose(INF, INF, abs_tol=0.0) |
| 1642 | self.assertIsClose(NINF, NINF) |
| 1643 | self.assertIsClose(NINF, NINF, abs_tol=0.0) |
| 1644 | |
| 1645 | def test_inf_ninf_nan(self): |
| 1646 | # these should never be close (following IEEE 754 rules for equality) |
| 1647 | not_close_examples = [(NAN, NAN), |
| 1648 | (NAN, 1e-100), |
| 1649 | (1e-100, NAN), |
| 1650 | (INF, NAN), |
| 1651 | (NAN, INF), |
| 1652 | (INF, NINF), |
| 1653 | (INF, 1.0), |
| 1654 | (1.0, INF), |
| 1655 | (INF, 1e308), |
| 1656 | (1e308, INF)] |
| 1657 | # use largest reasonable tolerance |
| 1658 | self.assertAllNotClose(not_close_examples, abs_tol=0.999999999999999) |
| 1659 | |
| 1660 | def test_zero_tolerance(self): |
| 1661 | # test with zero tolerance |
| 1662 | zero_tolerance_close_examples = [(1.0, 1.0), |
| 1663 | (-3.4, -3.4), |
| 1664 | (-1e-300, -1e-300)] |
| 1665 | self.assertAllClose(zero_tolerance_close_examples, rel_tol=0.0) |
| 1666 | |
| 1667 | zero_tolerance_not_close_examples = [(1.0, 1.000000000000001), |
| 1668 | (0.99999999999999, 1.0), |
| 1669 | (1.0e200, .999999999999999e200)] |
| 1670 | self.assertAllNotClose(zero_tolerance_not_close_examples, rel_tol=0.0) |
| 1671 | |
Martin Panter | eb99570 | 2016-07-28 01:11:04 +0000 | [diff] [blame] | 1672 | def test_asymmetry(self): |
| 1673 | # test the asymmetry example from PEP 485 |
Tal Einat | d5519ed | 2015-05-31 22:05:00 +0300 | [diff] [blame] | 1674 | self.assertAllClose([(9, 10), (10, 9)], rel_tol=0.1) |
| 1675 | |
| 1676 | def test_integers(self): |
| 1677 | # test with integer values |
| 1678 | integer_examples = [(100000001, 100000000), |
| 1679 | (123456789, 123456788)] |
| 1680 | |
| 1681 | self.assertAllClose(integer_examples, rel_tol=1e-8) |
| 1682 | self.assertAllNotClose(integer_examples, rel_tol=1e-9) |
| 1683 | |
| 1684 | def test_decimals(self): |
| 1685 | # test with Decimal values |
| 1686 | from decimal import Decimal |
| 1687 | |
| 1688 | decimal_examples = [(Decimal('1.00000001'), Decimal('1.0')), |
| 1689 | (Decimal('1.00000001e-20'), Decimal('1.0e-20')), |
Mark Dickinson | 31ba1c3 | 2016-09-04 12:29:14 +0100 | [diff] [blame] | 1690 | (Decimal('1.00000001e-100'), Decimal('1.0e-100')), |
| 1691 | (Decimal('1.00000001e20'), Decimal('1.0e20'))] |
Tal Einat | d5519ed | 2015-05-31 22:05:00 +0300 | [diff] [blame] | 1692 | self.assertAllClose(decimal_examples, rel_tol=1e-8) |
| 1693 | self.assertAllNotClose(decimal_examples, rel_tol=1e-9) |
| 1694 | |
| 1695 | def test_fractions(self): |
| 1696 | # test with Fraction values |
| 1697 | from fractions import Fraction |
| 1698 | |
Mark Dickinson | 31ba1c3 | 2016-09-04 12:29:14 +0100 | [diff] [blame] | 1699 | fraction_examples = [ |
| 1700 | (Fraction(1, 100000000) + 1, Fraction(1)), |
| 1701 | (Fraction(100000001), Fraction(100000000)), |
| 1702 | (Fraction(10**8 + 1, 10**28), Fraction(1, 10**20))] |
Tal Einat | d5519ed | 2015-05-31 22:05:00 +0300 | [diff] [blame] | 1703 | self.assertAllClose(fraction_examples, rel_tol=1e-8) |
| 1704 | self.assertAllNotClose(fraction_examples, rel_tol=1e-9) |
| 1705 | |
| 1706 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1707 | def test_main(): |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1708 | from doctest import DocFileSuite |
| 1709 | suite = unittest.TestSuite() |
| 1710 | suite.addTest(unittest.makeSuite(MathTests)) |
Tal Einat | d5519ed | 2015-05-31 22:05:00 +0300 | [diff] [blame] | 1711 | suite.addTest(unittest.makeSuite(IsCloseTests)) |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 1712 | suite.addTest(DocFileSuite("ieee754.txt")) |
| 1713 | run_unittest(suite) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1714 | |
| 1715 | if __name__ == '__main__': |
| 1716 | test_main() |