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