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