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