annotate Lib/test/test_math.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 32226776f866
children 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Python test set -- math module
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 # XXXX Should not do tests around zero only
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
3
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
4 from test.test_support import run_unittest, verbose
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
5 import unittest
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
6 import math
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
7 import os
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
8 import sys
47529
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
9 import random
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
11 eps = 1E-05
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
12 NAN = float('nan')
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
13 INF = float('inf')
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
14 NINF = float('-inf')
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
15
53350
133c66027f33 Issue #5593: Use more robust test for double-rounding in test_fsum.
Mark Dickinson <dickinsm@gmail.com>
parents: 48110
diff changeset
16 # detect evidence of double-rounding: fsum is not always correctly
133c66027f33 Issue #5593: Use more robust test for double-rounding in test_fsum.
Mark Dickinson <dickinsm@gmail.com>
parents: 48110
diff changeset
17 # rounded on machines that suffer from double rounding.
133c66027f33 Issue #5593: Use more robust test for double-rounding in test_fsum.
Mark Dickinson <dickinsm@gmail.com>
parents: 48110
diff changeset
18 x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer
133c66027f33 Issue #5593: Use more robust test for double-rounding in test_fsum.
Mark Dickinson <dickinsm@gmail.com>
parents: 48110
diff changeset
19 HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4)
133c66027f33 Issue #5593: Use more robust test for double-rounding in test_fsum.
Mark Dickinson <dickinsm@gmail.com>
parents: 48110
diff changeset
20
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
21 # locate file with test values
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
22 if __name__ == '__main__':
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
23 file = sys.argv[0]
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
24 else:
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
25 file = __file__
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
26 test_dir = os.path.dirname(file) or os.curdir
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
27 test_file = os.path.join(test_dir, 'cmath_testcases.txt')
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
28
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
29 def parse_testfile(fname):
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
30 """Parse a file with test values
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
31
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
32 Empty lines or lines starting with -- are ignored
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
33 yields id, fn, arg_real, arg_imag, exp_real, exp_imag
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
34 """
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
35 with open(fname) as fp:
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
36 for line in fp:
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
37 # skip comment lines and blank lines
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
38 if line.startswith('--') or not line.strip():
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
39 continue
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
40
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
41 lhs, rhs = line.split('->')
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
42 id, fn, arg_real, arg_imag = lhs.split()
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
43 rhs_pieces = rhs.split()
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
44 exp_real, exp_imag = rhs_pieces[0], rhs_pieces[1]
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
45 flags = rhs_pieces[2:]
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
46
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
47 yield (id, fn,
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
48 float(arg_real), float(arg_imag),
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
49 float(exp_real), float(exp_imag),
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
50 flags
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
51 )
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
52
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
53 class MathTests(unittest.TestCase):
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
55 def ftest(self, name, value, expected):
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
56 if abs(value-expected) > eps:
42380
0546c115a9cf Make test_math error messages more meaningful for small discrepancies in results
Nick Coghlan <ncoghlan@gmail.com>
parents: 42370
diff changeset
57 # Use %r instead of %f so the error message
0546c115a9cf Make test_math error messages more meaningful for small discrepancies in results
Nick Coghlan <ncoghlan@gmail.com>
parents: 42370
diff changeset
58 # displays full precision. Otherwise discrepancies
0546c115a9cf Make test_math error messages more meaningful for small discrepancies in results
Nick Coghlan <ncoghlan@gmail.com>
parents: 42370
diff changeset
59 # in the last few bits will lead to very confusing
0546c115a9cf Make test_math error messages more meaningful for small discrepancies in results
Nick Coghlan <ncoghlan@gmail.com>
parents: 42370
diff changeset
60 # error messages
0546c115a9cf Make test_math error messages more meaningful for small discrepancies in results
Nick Coghlan <ncoghlan@gmail.com>
parents: 42370
diff changeset
61 self.fail('%s returned %r, expected %r' %
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
62 (name, value, expected))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
64 def testConstants(self):
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
65 self.ftest('pi', math.pi, 3.1415926)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
66 self.ftest('e', math.e, 2.7182818)
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
68 def testAcos(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
69 self.assertRaises(TypeError, math.acos)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
70 self.ftest('acos(-1)', math.acos(-1), math.pi)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
71 self.ftest('acos(0)', math.acos(0), math.pi/2)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
72 self.ftest('acos(1)', math.acos(1), 0)
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
73 self.assertRaises(ValueError, math.acos, INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
74 self.assertRaises(ValueError, math.acos, NINF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
75 self.assert_(math.isnan(math.acos(NAN)))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
76
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
77 def testAcosh(self):
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
78 self.assertRaises(TypeError, math.acosh)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
79 self.ftest('acosh(1)', math.acosh(1), 0)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
80 self.ftest('acosh(2)', math.acosh(2), 1.3169578969248168)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
81 self.assertRaises(ValueError, math.acosh, 0)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
82 self.assertRaises(ValueError, math.acosh, -1)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
83 self.assertEquals(math.acosh(INF), INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
84 self.assertRaises(ValueError, math.acosh, NINF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
85 self.assert_(math.isnan(math.acosh(NAN)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
87 def testAsin(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
88 self.assertRaises(TypeError, math.asin)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
89 self.ftest('asin(-1)', math.asin(-1), -math.pi/2)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
90 self.ftest('asin(0)', math.asin(0), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
91 self.ftest('asin(1)', math.asin(1), math.pi/2)
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
92 self.assertRaises(ValueError, math.asin, INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
93 self.assertRaises(ValueError, math.asin, NINF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
94 self.assert_(math.isnan(math.asin(NAN)))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
95
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
96 def testAsinh(self):
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
97 self.assertRaises(TypeError, math.asinh)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
98 self.ftest('asinh(0)', math.asinh(0), 0)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
99 self.ftest('asinh(1)', math.asinh(1), 0.88137358701954305)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
100 self.ftest('asinh(-1)', math.asinh(-1), -0.88137358701954305)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
101 self.assertEquals(math.asinh(INF), INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
102 self.assertEquals(math.asinh(NINF), NINF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
103 self.assert_(math.isnan(math.asinh(NAN)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
104
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
105 def testAtan(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
106 self.assertRaises(TypeError, math.atan)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
107 self.ftest('atan(-1)', math.atan(-1), -math.pi/4)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
108 self.ftest('atan(0)', math.atan(0), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
109 self.ftest('atan(1)', math.atan(1), math.pi/4)
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
110 self.ftest('atan(inf)', math.atan(INF), math.pi/2)
46486
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
111 self.ftest('atan(-inf)', math.atan(NINF), -math.pi/2)
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
112 self.assert_(math.isnan(math.atan(NAN)))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
113
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
114 def testAtanh(self):
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
115 self.assertRaises(TypeError, math.atan)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
116 self.ftest('atanh(0)', math.atanh(0), 0)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
117 self.ftest('atanh(0.5)', math.atanh(0.5), 0.54930614433405489)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
118 self.ftest('atanh(-0.5)', math.atanh(-0.5), -0.54930614433405489)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
119 self.assertRaises(ValueError, math.atanh, 1)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
120 self.assertRaises(ValueError, math.atanh, -1)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
121 self.assertRaises(ValueError, math.atanh, INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
122 self.assertRaises(ValueError, math.atanh, NINF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
123 self.assert_(math.isnan(math.atanh(NAN)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
124
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
125 def testAtan2(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
126 self.assertRaises(TypeError, math.atan2)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
127 self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
128 self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
129 self.ftest('atan2(0, 1)', math.atan2(0, 1), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
130 self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
131 self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
132
46504
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
133 # math.atan2(0, x)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
134 self.ftest('atan2(0., -inf)', math.atan2(0., NINF), math.pi)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
135 self.ftest('atan2(0., -2.3)', math.atan2(0., -2.3), math.pi)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
136 self.ftest('atan2(0., -0.)', math.atan2(0., -0.), math.pi)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
137 self.assertEqual(math.atan2(0., 0.), 0.)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
138 self.assertEqual(math.atan2(0., 2.3), 0.)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
139 self.assertEqual(math.atan2(0., INF), 0.)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
140 self.assert_(math.isnan(math.atan2(0., NAN)))
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
141 # math.atan2(-0, x)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
142 self.ftest('atan2(-0., -inf)', math.atan2(-0., NINF), -math.pi)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
143 self.ftest('atan2(-0., -2.3)', math.atan2(-0., -2.3), -math.pi)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
144 self.ftest('atan2(-0., -0.)', math.atan2(-0., -0.), -math.pi)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
145 self.assertEqual(math.atan2(-0., 0.), -0.)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
146 self.assertEqual(math.atan2(-0., 2.3), -0.)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
147 self.assertEqual(math.atan2(-0., INF), -0.)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
148 self.assert_(math.isnan(math.atan2(-0., NAN)))
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
149 # math.atan2(INF, x)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
150 self.ftest('atan2(inf, -inf)', math.atan2(INF, NINF), math.pi*3/4)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
151 self.ftest('atan2(inf, -2.3)', math.atan2(INF, -2.3), math.pi/2)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
152 self.ftest('atan2(inf, -0.)', math.atan2(INF, -0.0), math.pi/2)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
153 self.ftest('atan2(inf, 0.)', math.atan2(INF, 0.0), math.pi/2)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
154 self.ftest('atan2(inf, 2.3)', math.atan2(INF, 2.3), math.pi/2)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
155 self.ftest('atan2(inf, inf)', math.atan2(INF, INF), math.pi/4)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
156 self.assert_(math.isnan(math.atan2(INF, NAN)))
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
157 # math.atan2(NINF, x)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
158 self.ftest('atan2(-inf, -inf)', math.atan2(NINF, NINF), -math.pi*3/4)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
159 self.ftest('atan2(-inf, -2.3)', math.atan2(NINF, -2.3), -math.pi/2)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
160 self.ftest('atan2(-inf, -0.)', math.atan2(NINF, -0.0), -math.pi/2)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
161 self.ftest('atan2(-inf, 0.)', math.atan2(NINF, 0.0), -math.pi/2)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
162 self.ftest('atan2(-inf, 2.3)', math.atan2(NINF, 2.3), -math.pi/2)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
163 self.ftest('atan2(-inf, inf)', math.atan2(NINF, INF), -math.pi/4)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
164 self.assert_(math.isnan(math.atan2(NINF, NAN)))
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
165 # math.atan2(+finite, x)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
166 self.ftest('atan2(2.3, -inf)', math.atan2(2.3, NINF), math.pi)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
167 self.ftest('atan2(2.3, -0.)', math.atan2(2.3, -0.), math.pi/2)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
168 self.ftest('atan2(2.3, 0.)', math.atan2(2.3, 0.), math.pi/2)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
169 self.assertEqual(math.atan2(2.3, INF), 0.)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
170 self.assert_(math.isnan(math.atan2(2.3, NAN)))
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
171 # math.atan2(-finite, x)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
172 self.ftest('atan2(-2.3, -inf)', math.atan2(-2.3, NINF), -math.pi)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
173 self.ftest('atan2(-2.3, -0.)', math.atan2(-2.3, -0.), -math.pi/2)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
174 self.ftest('atan2(-2.3, 0.)', math.atan2(-2.3, 0.), -math.pi/2)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
175 self.assertEqual(math.atan2(-2.3, INF), -0.)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
176 self.assert_(math.isnan(math.atan2(-2.3, NAN)))
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
177 # math.atan2(NAN, x)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
178 self.assert_(math.isnan(math.atan2(NAN, NINF)))
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
179 self.assert_(math.isnan(math.atan2(NAN, -2.3)))
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
180 self.assert_(math.isnan(math.atan2(NAN, -0.)))
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
181 self.assert_(math.isnan(math.atan2(NAN, 0.)))
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
182 self.assert_(math.isnan(math.atan2(NAN, 2.3)))
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
183 self.assert_(math.isnan(math.atan2(NAN, INF)))
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
184 self.assert_(math.isnan(math.atan2(NAN, NAN)))
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
185
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
186 def testCeil(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
187 self.assertRaises(TypeError, math.ceil)
44465
77c37e32fe35 Make math.{floor,ceil}({int,long}) return float again for backwards
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44440
diff changeset
188 # These types will be int in py3k.
77c37e32fe35 Make math.{floor,ceil}({int,long}) return float again for backwards
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44440
diff changeset
189 self.assertEquals(float, type(math.ceil(1)))
77c37e32fe35 Make math.{floor,ceil}({int,long}) return float again for backwards
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44440
diff changeset
190 self.assertEquals(float, type(math.ceil(1L)))
77c37e32fe35 Make math.{floor,ceil}({int,long}) return float again for backwards
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44440
diff changeset
191 self.assertEquals(float, type(math.ceil(1.0)))
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
192 self.ftest('ceil(0.5)', math.ceil(0.5), 1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
193 self.ftest('ceil(1.0)', math.ceil(1.0), 1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
194 self.ftest('ceil(1.5)', math.ceil(1.5), 2)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
195 self.ftest('ceil(-0.5)', math.ceil(-0.5), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
196 self.ftest('ceil(-1.0)', math.ceil(-1.0), -1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
197 self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
198 self.assertEquals(math.ceil(INF), INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
199 self.assertEquals(math.ceil(NINF), NINF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
200 self.assert_(math.isnan(math.ceil(NAN)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
201
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 42380
diff changeset
202 class TestCeil(object):
44488
94eeaeeb3ce9 Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44465
diff changeset
203 def __float__(self):
94eeaeeb3ce9 Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44465
diff changeset
204 return 41.3
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 42380
diff changeset
205 class TestNoCeil(object):
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 42380
diff changeset
206 pass
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 42380
diff changeset
207 self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42)
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 42380
diff changeset
208 self.assertRaises(TypeError, math.ceil, TestNoCeil())
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 42380
diff changeset
209
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 42380
diff changeset
210 t = TestNoCeil()
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 42380
diff changeset
211 t.__ceil__ = lambda *args: args
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 42380
diff changeset
212 self.assertRaises(TypeError, math.ceil, t)
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 42380
diff changeset
213 self.assertRaises(TypeError, math.ceil, t, 0)
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 42380
diff changeset
214
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
215 if float.__getformat__("double").startswith("IEEE"):
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
216 def testCopysign(self):
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
217 self.assertRaises(TypeError, math.copysign)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
218 # copysign should let us distinguish signs of zeros
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
219 self.assertEquals(copysign(1., 0.), 1.)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
220 self.assertEquals(copysign(1., -0.), -1.)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
221 self.assertEquals(copysign(INF, 0.), INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
222 self.assertEquals(copysign(INF, -0.), NINF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
223 self.assertEquals(copysign(NINF, 0.), INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
224 self.assertEquals(copysign(NINF, -0.), NINF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
225 # and of infinities
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
226 self.assertEquals(copysign(1., INF), 1.)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
227 self.assertEquals(copysign(1., NINF), -1.)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
228 self.assertEquals(copysign(INF, INF), INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
229 self.assertEquals(copysign(INF, NINF), NINF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
230 self.assertEquals(copysign(NINF, INF), INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
231 self.assertEquals(copysign(NINF, NINF), NINF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
232 self.assert_(math.isnan(copysign(NAN, 1.)))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
233 self.assert_(math.isnan(copysign(NAN, INF)))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
234 self.assert_(math.isnan(copysign(NAN, NINF)))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
235 self.assert_(math.isnan(copysign(NAN, NAN)))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
236 # copysign(INF, NAN) may be INF or it may be NINF, since
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
237 # we don't know whether the sign bit of NAN is set on any
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
238 # given platform.
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
239 self.assert_(math.isinf(copysign(INF, NAN)))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
240 # similarly, copysign(2., NAN) could be 2. or -2.
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
241 self.assertEquals(abs(copysign(2., NAN)), 2.)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
242
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
243 def testCos(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
244 self.assertRaises(TypeError, math.cos)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
245 self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
246 self.ftest('cos(0)', math.cos(0), 1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
247 self.ftest('cos(pi/2)', math.cos(math.pi/2), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
248 self.ftest('cos(pi)', math.cos(math.pi), -1)
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
249 try:
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
250 self.assert_(math.isnan(math.cos(INF)))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
251 self.assert_(math.isnan(math.cos(NINF)))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
252 except ValueError:
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
253 self.assertRaises(ValueError, math.cos, INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
254 self.assertRaises(ValueError, math.cos, NINF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
255 self.assert_(math.isnan(math.cos(NAN)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
256
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
257 def testCosh(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
258 self.assertRaises(TypeError, math.cosh)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
259 self.ftest('cosh(0)', math.cosh(0), 1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
260 self.ftest('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
261 self.assertEquals(math.cosh(INF), INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
262 self.assertEquals(math.cosh(NINF), INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
263 self.assert_(math.isnan(math.cosh(NAN)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
264
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
265 def testDegrees(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
266 self.assertRaises(TypeError, math.degrees)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
267 self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
268 self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
269 self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
23426
1157281f00a0 Adds tests for degrees() and radians() functions added to mathmodule.
Raymond Hettinger <python@rcn.com>
parents: 19750
diff changeset
270
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
271 def testExp(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
272 self.assertRaises(TypeError, math.exp)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
273 self.ftest('exp(-1)', math.exp(-1), 1/math.e)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
274 self.ftest('exp(0)', math.exp(0), 1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
275 self.ftest('exp(1)', math.exp(1), math.e)
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
276 self.assertEquals(math.exp(INF), INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
277 self.assertEquals(math.exp(NINF), 0.)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
278 self.assert_(math.isnan(math.exp(NAN)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
279
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
280 def testFabs(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
281 self.assertRaises(TypeError, math.fabs)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
282 self.ftest('fabs(-1)', math.fabs(-1), 1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
283 self.ftest('fabs(0)', math.fabs(0), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
284 self.ftest('fabs(1)', math.fabs(1), 1)
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
285
47529
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
286 def testFactorial(self):
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
287 def fact(n):
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
288 result = 1
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
289 for i in range(1, int(n)+1):
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
290 result *= i
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
291 return result
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
292 values = range(10) + [50, 100, 500]
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
293 random.shuffle(values)
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
294 for x in range(10):
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
295 for cast in (int, long, float):
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
296 self.assertEqual(math.factorial(cast(x)), fact(x), (x, fact(x), math.factorial(x)))
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
297 self.assertRaises(ValueError, math.factorial, -1)
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
298 self.assertRaises(ValueError, math.factorial, math.pi)
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
299
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
300 def testFloor(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
301 self.assertRaises(TypeError, math.floor)
44465
77c37e32fe35 Make math.{floor,ceil}({int,long}) return float again for backwards
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44440
diff changeset
302 # These types will be int in py3k.
77c37e32fe35 Make math.{floor,ceil}({int,long}) return float again for backwards
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44440
diff changeset
303 self.assertEquals(float, type(math.floor(1)))
77c37e32fe35 Make math.{floor,ceil}({int,long}) return float again for backwards
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44440
diff changeset
304 self.assertEquals(float, type(math.floor(1L)))
77c37e32fe35 Make math.{floor,ceil}({int,long}) return float again for backwards
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44440
diff changeset
305 self.assertEquals(float, type(math.floor(1.0)))
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
306 self.ftest('floor(0.5)', math.floor(0.5), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
307 self.ftest('floor(1.0)', math.floor(1.0), 1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
308 self.ftest('floor(1.5)', math.floor(1.5), 1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
309 self.ftest('floor(-0.5)', math.floor(-0.5), -1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
310 self.ftest('floor(-1.0)', math.floor(-1.0), -1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
311 self.ftest('floor(-1.5)', math.floor(-1.5), -2)
42370
64903ff76ede Add explicit test for a misbehaving math.floor
Nick Coghlan <ncoghlan@gmail.com>
parents: 40284
diff changeset
312 # pow() relies on floor() to check for integers
64903ff76ede Add explicit test for a misbehaving math.floor
Nick Coghlan <ncoghlan@gmail.com>
parents: 40284
diff changeset
313 # This fails on some platforms - so check it here
64903ff76ede Add explicit test for a misbehaving math.floor
Nick Coghlan <ncoghlan@gmail.com>
parents: 40284
diff changeset
314 self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167)
64903ff76ede Add explicit test for a misbehaving math.floor
Nick Coghlan <ncoghlan@gmail.com>
parents: 40284
diff changeset
315 self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167)
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
316 self.assertEquals(math.ceil(INF), INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
317 self.assertEquals(math.ceil(NINF), NINF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
318 self.assert_(math.isnan(math.floor(NAN)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
319
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 42380
diff changeset
320 class TestFloor(object):
44488
94eeaeeb3ce9 Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44465
diff changeset
321 def __float__(self):
94eeaeeb3ce9 Continue rolling back pep-3141 changes that changed behavior from 2.5. This
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44465
diff changeset
322 return 42.3
44432
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 42380
diff changeset
323 class TestNoFloor(object):
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 42380
diff changeset
324 pass
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 42380
diff changeset
325 self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42)
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 42380
diff changeset
326 self.assertRaises(TypeError, math.floor, TestNoFloor())
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 42380
diff changeset
327
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 42380
diff changeset
328 t = TestNoFloor()
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 42380
diff changeset
329 t.__floor__ = lambda *args: args
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 42380
diff changeset
330 self.assertRaises(TypeError, math.floor, t)
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 42380
diff changeset
331 self.assertRaises(TypeError, math.floor, t, 0)
4f9723d9ca32 Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 42380
diff changeset
332
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
333 def testFmod(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
334 self.assertRaises(TypeError, math.fmod)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
335 self.ftest('fmod(10,1)', math.fmod(10,1), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
336 self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
337 self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
338 self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
339 self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
340 self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
341 self.assert_(math.isnan(math.fmod(NAN, 1.)))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
342 self.assert_(math.isnan(math.fmod(1., NAN)))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
343 self.assert_(math.isnan(math.fmod(NAN, NAN)))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
344 self.assertRaises(ValueError, math.fmod, 1., 0.)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
345 self.assertRaises(ValueError, math.fmod, INF, 1.)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
346 self.assertRaises(ValueError, math.fmod, NINF, 1.)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
347 self.assertRaises(ValueError, math.fmod, INF, 0.)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
348 self.assertEquals(math.fmod(3.0, INF), 3.0)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
349 self.assertEquals(math.fmod(-3.0, INF), -3.0)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
350 self.assertEquals(math.fmod(3.0, NINF), 3.0)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
351 self.assertEquals(math.fmod(-3.0, NINF), -3.0)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
352 self.assertEquals(math.fmod(0.0, 3.0), 0.0)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
353 self.assertEquals(math.fmod(0.0, NINF), 0.0)
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
354
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
355 def testFrexp(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
356 self.assertRaises(TypeError, math.frexp)
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
357
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
358 def testfrexp(name, (mant, exp), (emant, eexp)):
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
359 if abs(mant-emant) > eps or exp != eexp:
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
360 self.fail('%s returned %r, expected %r'%\
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
361 (name, (mant, exp), (emant,eexp)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
362
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
363 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
364 testfrexp('frexp(0)', math.frexp(0), (0, 0))
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
365 testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
366 testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
367
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
368 self.assertEquals(math.frexp(INF)[0], INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
369 self.assertEquals(math.frexp(NINF)[0], NINF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
370 self.assert_(math.isnan(math.frexp(NAN)[0]))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
371
53350
133c66027f33 Issue #5593: Use more robust test for double-rounding in test_fsum.
Mark Dickinson <dickinsm@gmail.com>
parents: 48110
diff changeset
372 @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
133c66027f33 Issue #5593: Use more robust test for double-rounding in test_fsum.
Mark Dickinson <dickinsm@gmail.com>
parents: 48110
diff changeset
373 "test requires IEEE 754 doubles")
53351
32226776f866 Remove unnecessary double negative
Mark Dickinson <dickinsm@gmail.com>
parents: 53350
diff changeset
374 @unittest.skipIf(HAVE_DOUBLE_ROUNDING,
53350
133c66027f33 Issue #5593: Use more robust test for double-rounding in test_fsum.
Mark Dickinson <dickinsm@gmail.com>
parents: 48110
diff changeset
375 "fsum is not exact on machines with double rounding")
48110
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
376 def testFsum(self):
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
377 # math.fsum relies on exact rounding for correct operation.
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
378 # There's a known problem with IA32 floating-point that causes
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
379 # inexact rounding in some situations, and will cause the
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
380 # math.fsum tests below to fail; see issue #2937. On non IEEE
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
381 # 754 platforms, and on IEEE 754 platforms that exhibit the
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
382 # problem described in issue #2937, we simply skip the whole
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
383 # test.
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
384
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
385 # Python version of math.fsum, for comparison. Uses a
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
386 # different algorithm based on frexp, ldexp and integer
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
387 # arithmetic.
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
388 from sys import float_info
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
389 mant_dig = float_info.mant_dig
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
390 etiny = float_info.min_exp - mant_dig
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
391
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
392 def msum(iterable):
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
393 """Full precision summation. Compute sum(iterable) without any
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
394 intermediate accumulation of error. Based on the 'lsum' function
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
395 at http://code.activestate.com/recipes/393090/
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
396
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
397 """
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
398 tmant, texp = 0, 0
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
399 for x in iterable:
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
400 mant, exp = math.frexp(x)
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
401 mant, exp = int(math.ldexp(mant, mant_dig)), exp - mant_dig
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
402 if texp > exp:
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
403 tmant <<= texp-exp
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
404 texp = exp
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
405 else:
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
406 mant <<= exp-texp
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
407 tmant += mant
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
408 # Round tmant * 2**texp to a float. The original recipe
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
409 # used float(str(tmant)) * 2.0**texp for this, but that's
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
410 # a little unsafe because str -> float conversion can't be
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
411 # relied upon to do correct rounding on all platforms.
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
412 tail = max(len(bin(abs(tmant)))-2 - mant_dig, etiny - texp)
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
413 if tail > 0:
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
414 h = 1 << (tail-1)
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
415 tmant = tmant // (2*h) + bool(tmant & h and tmant & 3*h-1)
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
416 texp += tail
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
417 return math.ldexp(tmant, texp)
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
418
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
419 test_values = [
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
420 ([], 0.0),
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
421 ([0.0], 0.0),
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
422 ([1e100, 1.0, -1e100, 1e-100, 1e50, -1.0, -1e50], 1e-100),
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
423 ([2.0**53, -0.5, -2.0**-54], 2.0**53-1.0),
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
424 ([2.0**53, 1.0, 2.0**-100], 2.0**53+2.0),
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
425 ([2.0**53+10.0, 1.0, 2.0**-100], 2.0**53+12.0),
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
426 ([2.0**53-4.0, 0.5, 2.0**-54], 2.0**53-3.0),
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
427 ([1./n for n in range(1, 1001)],
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
428 float.fromhex('0x1.df11f45f4e61ap+2')),
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
429 ([(-1.)**n/n for n in range(1, 1001)],
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
430 float.fromhex('-0x1.62a2af1bd3624p-1')),
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
431 ([1.7**(i+1)-1.7**i for i in range(1000)] + [-1.7**1000], -1.0),
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
432 ([1e16, 1., 1e-16], 10000000000000002.0),
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
433 ([1e16-2., 1.-2.**-53, -(1e16-2.), -(1.-2.**-53)], 0.0),
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
434 # exercise code for resizing partials array
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
435 ([2.**n - 2.**(n+50) + 2.**(n+52) for n in range(-1074, 972, 2)] +
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
436 [-2.**1022],
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
437 float.fromhex('0x1.5555555555555p+970')),
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
438 ]
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
439
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
440 for i, (vals, expected) in enumerate(test_values):
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
441 try:
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
442 actual = math.fsum(vals)
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
443 except OverflowError:
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
444 self.fail("test %d failed: got OverflowError, expected %r "
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
445 "for math.fsum(%.100r)" % (i, expected, vals))
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
446 except ValueError:
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
447 self.fail("test %d failed: got ValueError, expected %r "
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
448 "for math.fsum(%.100r)" % (i, expected, vals))
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
449 self.assertEqual(actual, expected)
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
450
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
451 from random import random, gauss, shuffle
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
452 for j in xrange(1000):
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
453 vals = [7, 1e100, -7, -1e100, -9e-20, 8e-20] * 10
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
454 s = 0
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
455 for i in xrange(200):
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
456 v = gauss(0, random()) ** 7 - s
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
457 s += v
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
458 vals.append(v)
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
459 shuffle(vals)
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
460
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
461 s = msum(vals)
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
462 self.assertEqual(msum(vals), math.fsum(vals))
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
463
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
464 def testHypot(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
465 self.assertRaises(TypeError, math.hypot)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
466 self.ftest('hypot(0,0)', math.hypot(0,0), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
467 self.ftest('hypot(3,4)', math.hypot(3,4), 5)
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
468 self.assertEqual(math.hypot(NAN, INF), INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
469 self.assertEqual(math.hypot(INF, NAN), INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
470 self.assertEqual(math.hypot(NAN, NINF), INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
471 self.assertEqual(math.hypot(NINF, NAN), INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
472 self.assert_(math.isnan(math.hypot(1.0, NAN)))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
473 self.assert_(math.isnan(math.hypot(NAN, -2.0)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
474
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
475 def testLdexp(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
476 self.assertRaises(TypeError, math.ldexp)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
477 self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
478 self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
479 self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
480 self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
481 self.assertRaises(OverflowError, math.ldexp, 1., 1000000)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
482 self.assertRaises(OverflowError, math.ldexp, -1., 1000000)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
483 self.assertEquals(math.ldexp(1., -1000000), 0.)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
484 self.assertEquals(math.ldexp(-1., -1000000), -0.)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
485 self.assertEquals(math.ldexp(INF, 30), INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
486 self.assertEquals(math.ldexp(NINF, -213), NINF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
487 self.assert_(math.isnan(math.ldexp(NAN, 0)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
488
46797
fd0b894bc90c Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents: 46504
diff changeset
489 # large second argument
fd0b894bc90c Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents: 46504
diff changeset
490 for n in [10**5, 10L**5, 10**10, 10L**10, 10**20, 10**40]:
fd0b894bc90c Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents: 46504
diff changeset
491 self.assertEquals(math.ldexp(INF, -n), INF)
fd0b894bc90c Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents: 46504
diff changeset
492 self.assertEquals(math.ldexp(NINF, -n), NINF)
fd0b894bc90c Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents: 46504
diff changeset
493 self.assertEquals(math.ldexp(1., -n), 0.)
fd0b894bc90c Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents: 46504
diff changeset
494 self.assertEquals(math.ldexp(-1., -n), -0.)
fd0b894bc90c Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents: 46504
diff changeset
495 self.assertEquals(math.ldexp(0., -n), 0.)
fd0b894bc90c Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents: 46504
diff changeset
496 self.assertEquals(math.ldexp(-0., -n), -0.)
fd0b894bc90c Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents: 46504
diff changeset
497 self.assert_(math.isnan(math.ldexp(NAN, -n)))
fd0b894bc90c Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents: 46504
diff changeset
498
fd0b894bc90c Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents: 46504
diff changeset
499 self.assertRaises(OverflowError, math.ldexp, 1., n)
fd0b894bc90c Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents: 46504
diff changeset
500 self.assertRaises(OverflowError, math.ldexp, -1., n)
fd0b894bc90c Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents: 46504
diff changeset
501 self.assertEquals(math.ldexp(0., n), 0.)
fd0b894bc90c Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents: 46504
diff changeset
502 self.assertEquals(math.ldexp(-0., n), -0.)
fd0b894bc90c Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents: 46504
diff changeset
503 self.assertEquals(math.ldexp(INF, n), INF)
fd0b894bc90c Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents: 46504
diff changeset
504 self.assertEquals(math.ldexp(NINF, n), NINF)
fd0b894bc90c Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents: 46504
diff changeset
505 self.assert_(math.isnan(math.ldexp(NAN, n)))
fd0b894bc90c Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents: 46504
diff changeset
506
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
507 def testLog(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
508 self.assertRaises(TypeError, math.log)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
509 self.ftest('log(1/e)', math.log(1/math.e), -1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
510 self.ftest('log(1)', math.log(1), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
511 self.ftest('log(e)', math.log(math.e), 1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
512 self.ftest('log(32,2)', math.log(32,2), 5)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
513 self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
514 self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
515 self.assertEquals(math.log(INF), INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
516 self.assertRaises(ValueError, math.log, NINF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
517 self.assert_(math.isnan(math.log(NAN)))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
518
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
519 def testLog1p(self):
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
520 self.assertRaises(TypeError, math.log1p)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
521 self.ftest('log1p(1/e -1)', math.log1p(1/math.e-1), -1)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
522 self.ftest('log1p(0)', math.log1p(0), 0)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
523 self.ftest('log1p(e-1)', math.log1p(math.e-1), 1)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
524 self.ftest('log1p(1)', math.log1p(1), math.log(2))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
525 self.assertEquals(math.log1p(INF), INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
526 self.assertRaises(ValueError, math.log1p, NINF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
527 self.assert_(math.isnan(math.log1p(NAN)))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
528 n= 2**90
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
529 self.assertAlmostEquals(math.log1p(n), 62.383246250395075)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
530 self.assertAlmostEquals(math.log1p(n), math.log1p(float(n)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
531
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
532 def testLog10(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
533 self.assertRaises(TypeError, math.log10)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
534 self.ftest('log10(0.1)', math.log10(0.1), -1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
535 self.ftest('log10(1)', math.log10(1), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
536 self.ftest('log10(10)', math.log10(10), 1)
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
537 self.assertEquals(math.log(INF), INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
538 self.assertRaises(ValueError, math.log10, NINF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
539 self.assert_(math.isnan(math.log10(NAN)))
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
540
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
541 def testModf(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
542 self.assertRaises(TypeError, math.modf)
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
543
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
544 def testmodf(name, (v1, v2), (e1, e2)):
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
545 if abs(v1-e1) > eps or abs(v2-e2):
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
546 self.fail('%s returned %r, expected %r'%\
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
547 (name, (v1,v2), (e1,e2)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
548
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
549 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
550 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
40284
ff678669aa6e Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 40270
diff changeset
551
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
552 self.assertEquals(math.modf(INF), (0.0, INF))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
553 self.assertEquals(math.modf(NINF), (-0.0, NINF))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
554
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
555 modf_nan = math.modf(NAN)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
556 self.assert_(math.isnan(modf_nan[0]))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
557 self.assert_(math.isnan(modf_nan[1]))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
558
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
559 def testPow(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
560 self.assertRaises(TypeError, math.pow)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
561 self.ftest('pow(0,1)', math.pow(0,1), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
562 self.ftest('pow(1,0)', math.pow(1,0), 1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
563 self.ftest('pow(2,1)', math.pow(2,1), 2)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
564 self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
565 self.assertEqual(math.pow(INF, 1), INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
566 self.assertEqual(math.pow(NINF, 1), NINF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
567 self.assertEqual((math.pow(1, INF)), 1.)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
568 self.assertEqual((math.pow(1, NINF)), 1.)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
569 self.assert_(math.isnan(math.pow(NAN, 1)))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
570 self.assert_(math.isnan(math.pow(2, NAN)))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
571 self.assert_(math.isnan(math.pow(0, NAN)))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
572 self.assertEqual(math.pow(1, NAN), 1)
46486
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
573
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
574 # pow(0., x)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
575 self.assertEqual(math.pow(0., INF), 0.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
576 self.assertEqual(math.pow(0., 3.), 0.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
577 self.assertEqual(math.pow(0., 2.3), 0.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
578 self.assertEqual(math.pow(0., 2.), 0.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
579 self.assertEqual(math.pow(0., 0.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
580 self.assertEqual(math.pow(0., -0.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
581 self.assertRaises(ValueError, math.pow, 0., -2.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
582 self.assertRaises(ValueError, math.pow, 0., -2.3)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
583 self.assertRaises(ValueError, math.pow, 0., -3.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
584 self.assertRaises(ValueError, math.pow, 0., NINF)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
585 self.assert_(math.isnan(math.pow(0., NAN)))
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
586
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
587 # pow(INF, x)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
588 self.assertEqual(math.pow(INF, INF), INF)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
589 self.assertEqual(math.pow(INF, 3.), INF)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
590 self.assertEqual(math.pow(INF, 2.3), INF)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
591 self.assertEqual(math.pow(INF, 2.), INF)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
592 self.assertEqual(math.pow(INF, 0.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
593 self.assertEqual(math.pow(INF, -0.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
594 self.assertEqual(math.pow(INF, -2.), 0.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
595 self.assertEqual(math.pow(INF, -2.3), 0.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
596 self.assertEqual(math.pow(INF, -3.), 0.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
597 self.assertEqual(math.pow(INF, NINF), 0.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
598 self.assert_(math.isnan(math.pow(INF, NAN)))
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
599
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
600 # pow(-0., x)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
601 self.assertEqual(math.pow(-0., INF), 0.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
602 self.assertEqual(math.pow(-0., 3.), -0.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
603 self.assertEqual(math.pow(-0., 2.3), 0.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
604 self.assertEqual(math.pow(-0., 2.), 0.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
605 self.assertEqual(math.pow(-0., 0.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
606 self.assertEqual(math.pow(-0., -0.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
607 self.assertRaises(ValueError, math.pow, -0., -2.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
608 self.assertRaises(ValueError, math.pow, -0., -2.3)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
609 self.assertRaises(ValueError, math.pow, -0., -3.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
610 self.assertRaises(ValueError, math.pow, -0., NINF)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
611 self.assert_(math.isnan(math.pow(-0., NAN)))
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
612
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
613 # pow(NINF, x)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
614 self.assertEqual(math.pow(NINF, INF), INF)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
615 self.assertEqual(math.pow(NINF, 3.), NINF)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
616 self.assertEqual(math.pow(NINF, 2.3), INF)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
617 self.assertEqual(math.pow(NINF, 2.), INF)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
618 self.assertEqual(math.pow(NINF, 0.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
619 self.assertEqual(math.pow(NINF, -0.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
620 self.assertEqual(math.pow(NINF, -2.), 0.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
621 self.assertEqual(math.pow(NINF, -2.3), 0.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
622 self.assertEqual(math.pow(NINF, -3.), -0.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
623 self.assertEqual(math.pow(NINF, NINF), 0.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
624 self.assert_(math.isnan(math.pow(NINF, NAN)))
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
625
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
626 # pow(-1, x)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
627 self.assertEqual(math.pow(-1., INF), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
628 self.assertEqual(math.pow(-1., 3.), -1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
629 self.assertRaises(ValueError, math.pow, -1., 2.3)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
630 self.assertEqual(math.pow(-1., 2.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
631 self.assertEqual(math.pow(-1., 0.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
632 self.assertEqual(math.pow(-1., -0.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
633 self.assertEqual(math.pow(-1., -2.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
634 self.assertRaises(ValueError, math.pow, -1., -2.3)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
635 self.assertEqual(math.pow(-1., -3.), -1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
636 self.assertEqual(math.pow(-1., NINF), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
637 self.assert_(math.isnan(math.pow(-1., NAN)))
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
638
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
639 # pow(1, x)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
640 self.assertEqual(math.pow(1., INF), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
641 self.assertEqual(math.pow(1., 3.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
642 self.assertEqual(math.pow(1., 2.3), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
643 self.assertEqual(math.pow(1., 2.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
644 self.assertEqual(math.pow(1., 0.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
645 self.assertEqual(math.pow(1., -0.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
646 self.assertEqual(math.pow(1., -2.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
647 self.assertEqual(math.pow(1., -2.3), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
648 self.assertEqual(math.pow(1., -3.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
649 self.assertEqual(math.pow(1., NINF), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
650 self.assertEqual(math.pow(1., NAN), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
651
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
652 # pow(x, 0) should be 1 for any x
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
653 self.assertEqual(math.pow(2.3, 0.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
654 self.assertEqual(math.pow(-2.3, 0.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
655 self.assertEqual(math.pow(NAN, 0.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
656 self.assertEqual(math.pow(2.3, -0.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
657 self.assertEqual(math.pow(-2.3, -0.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
658 self.assertEqual(math.pow(NAN, -0.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
659
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
660 # pow(x, y) is invalid if x is negative and y is not integral
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
661 self.assertRaises(ValueError, math.pow, -1., 2.3)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
662 self.assertRaises(ValueError, math.pow, -15., -3.1)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
663
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
664 # pow(x, NINF)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
665 self.assertEqual(math.pow(1.9, NINF), 0.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
666 self.assertEqual(math.pow(1.1, NINF), 0.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
667 self.assertEqual(math.pow(0.9, NINF), INF)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
668 self.assertEqual(math.pow(0.1, NINF), INF)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
669 self.assertEqual(math.pow(-0.1, NINF), INF)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
670 self.assertEqual(math.pow(-0.9, NINF), INF)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
671 self.assertEqual(math.pow(-1.1, NINF), 0.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
672 self.assertEqual(math.pow(-1.9, NINF), 0.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
673
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
674 # pow(x, INF)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
675 self.assertEqual(math.pow(1.9, INF), INF)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
676 self.assertEqual(math.pow(1.1, INF), INF)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
677 self.assertEqual(math.pow(0.9, INF), 0.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
678 self.assertEqual(math.pow(0.1, INF), 0.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
679 self.assertEqual(math.pow(-0.1, INF), 0.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
680 self.assertEqual(math.pow(-0.9, INF), 0.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
681 self.assertEqual(math.pow(-1.1, INF), INF)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
682 self.assertEqual(math.pow(-1.9, INF), INF)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
683
46500
a20def0101de Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents: 46498
diff changeset
684 # pow(x, y) should work for x negative, y an integer
a20def0101de Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents: 46498
diff changeset
685 self.ftest('(-2.)**3.', math.pow(-2.0, 3.0), -8.0)
a20def0101de Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents: 46498
diff changeset
686 self.ftest('(-2.)**2.', math.pow(-2.0, 2.0), 4.0)
a20def0101de Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents: 46498
diff changeset
687 self.ftest('(-2.)**1.', math.pow(-2.0, 1.0), -2.0)
a20def0101de Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents: 46498
diff changeset
688 self.ftest('(-2.)**0.', math.pow(-2.0, 0.0), 1.0)
a20def0101de Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents: 46498
diff changeset
689 self.ftest('(-2.)**-0.', math.pow(-2.0, -0.0), 1.0)
a20def0101de Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents: 46498
diff changeset
690 self.ftest('(-2.)**-1.', math.pow(-2.0, -1.0), -0.5)
a20def0101de Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents: 46498
diff changeset
691 self.ftest('(-2.)**-2.', math.pow(-2.0, -2.0), 0.25)
a20def0101de Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents: 46498
diff changeset
692 self.ftest('(-2.)**-3.', math.pow(-2.0, -3.0), -0.125)
a20def0101de Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents: 46498
diff changeset
693 self.assertRaises(ValueError, math.pow, -2.0, -0.5)
a20def0101de Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents: 46498
diff changeset
694 self.assertRaises(ValueError, math.pow, -2.0, 0.5)
a20def0101de Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents: 46498
diff changeset
695
46486
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
696 # the following tests have been commented out since they don't
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
697 # really belong here: the implementation of ** for floats is
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
698 # independent of the implemention of math.pow
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
699 #self.assertEqual(1**NAN, 1)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
700 #self.assertEqual(1**INF, 1)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
701 #self.assertEqual(1**NINF, 1)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
702 #self.assertEqual(1**0, 1)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
703 #self.assertEqual(1.**NAN, 1)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
704 #self.assertEqual(1.**INF, 1)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
705 #self.assertEqual(1.**NINF, 1)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
706 #self.assertEqual(1.**0, 1)
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
707
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
708 def testRadians(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
709 self.assertRaises(TypeError, math.radians)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
710 self.ftest('radians(180)', math.radians(180), math.pi)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
711 self.ftest('radians(90)', math.radians(90), math.pi/2)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
712 self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
713
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
714 def testSin(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
715 self.assertRaises(TypeError, math.sin)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
716 self.ftest('sin(0)', math.sin(0), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
717 self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
718 self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
719 try:
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
720 self.assert_(math.isnan(math.sin(INF)))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
721 self.assert_(math.isnan(math.sin(NINF)))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
722 except ValueError:
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
723 self.assertRaises(ValueError, math.sin, INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
724 self.assertRaises(ValueError, math.sin, NINF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
725 self.assert_(math.isnan(math.sin(NAN)))
23426
1157281f00a0 Adds tests for degrees() and radians() functions added to mathmodule.
Raymond Hettinger <python@rcn.com>
parents: 19750
diff changeset
726
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
727 def testSinh(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
728 self.assertRaises(TypeError, math.sinh)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
729 self.ftest('sinh(0)', math.sinh(0), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
730 self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
731 self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
732 self.assertEquals(math.sinh(INF), INF)
46486
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
733 self.assertEquals(math.sinh(NINF), NINF)
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
734 self.assert_(math.isnan(math.sinh(NAN)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
735
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
736 def testSqrt(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
737 self.assertRaises(TypeError, math.sqrt)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
738 self.ftest('sqrt(0)', math.sqrt(0), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
739 self.ftest('sqrt(1)', math.sqrt(1), 1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
740 self.ftest('sqrt(4)', math.sqrt(4), 2)
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
741 self.assertEquals(math.sqrt(INF), INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
742 self.assertRaises(ValueError, math.sqrt, NINF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
743 self.assert_(math.isnan(math.sqrt(NAN)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
744
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
745 def testTan(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
746 self.assertRaises(TypeError, math.tan)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
747 self.ftest('tan(0)', math.tan(0), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
748 self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
749 self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
750 try:
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
751 self.assert_(math.isnan(math.tan(INF)))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
752 self.assert_(math.isnan(math.tan(NINF)))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
753 except:
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
754 self.assertRaises(ValueError, math.tan, INF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
755 self.assertRaises(ValueError, math.tan, NINF)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
756 self.assert_(math.isnan(math.tan(NAN)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
757
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
758 def testTanh(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
759 self.assertRaises(TypeError, math.tanh)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
760 self.ftest('tanh(0)', math.tanh(0), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
761 self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
762 self.ftest('tanh(inf)', math.tanh(INF), 1)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
763 self.ftest('tanh(-inf)', math.tanh(NINF), -1)
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
764 self.assert_(math.isnan(math.tanh(NAN)))
46504
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
765 # check that tanh(-0.) == -0. on IEEE 754 systems
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
766 if float.__getformat__("double").startswith("IEEE"):
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
767 self.assertEqual(math.tanh(-0.), -0.)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
768 self.assertEqual(math.copysign(1., math.tanh(-0.)),
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
769 math.copysign(1., -0.))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
770
45079
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
771 def test_trunc(self):
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
772 self.assertEqual(math.trunc(1), 1)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
773 self.assertEqual(math.trunc(-1), -1)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
774 self.assertEqual(type(math.trunc(1)), int)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
775 self.assertEqual(type(math.trunc(1.5)), int)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
776 self.assertEqual(math.trunc(1.5), 1)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
777 self.assertEqual(math.trunc(-1.5), -1)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
778 self.assertEqual(math.trunc(1.999999), 1)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
779 self.assertEqual(math.trunc(-1.999999), -1)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
780 self.assertEqual(math.trunc(-0.999999), -0)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
781 self.assertEqual(math.trunc(-100.999), -100)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
782
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
783 class TestTrunc(object):
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
784 def __trunc__(self):
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
785 return 23
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
786
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
787 class TestNoTrunc(object):
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
788 pass
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
789
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
790 self.assertEqual(math.trunc(TestTrunc()), 23)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
791
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
792 self.assertRaises(TypeError, math.trunc)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
793 self.assertRaises(TypeError, math.trunc, 1, 2)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
794 # XXX: This is not ideal, but see the comment in math_trunc().
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
795 self.assertRaises(AttributeError, math.trunc, TestNoTrunc())
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
796
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
797 t = TestNoTrunc()
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
798 t.__trunc__ = lambda *args: args
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
799 self.assertEquals((), math.trunc(t))
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
800 self.assertRaises(TypeError, math.trunc, t, 0)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
801
44440
6208b17d0dbe Added copysign(x, y) function to the math module
Christian Heimes <christian@cheimes.de>
parents: 44437
diff changeset
802 def testCopysign(self):
6208b17d0dbe Added copysign(x, y) function to the math module
Christian Heimes <christian@cheimes.de>
parents: 44437
diff changeset
803 self.assertEqual(math.copysign(1, 42), 1.0)
6208b17d0dbe Added copysign(x, y) function to the math module
Christian Heimes <christian@cheimes.de>
parents: 44437
diff changeset
804 self.assertEqual(math.copysign(0., 42), 0.0)
6208b17d0dbe Added copysign(x, y) function to the math module
Christian Heimes <christian@cheimes.de>
parents: 44437
diff changeset
805 self.assertEqual(math.copysign(1., -42), -1.0)
6208b17d0dbe Added copysign(x, y) function to the math module
Christian Heimes <christian@cheimes.de>
parents: 44437
diff changeset
806 self.assertEqual(math.copysign(3, 0.), 3.0)
6208b17d0dbe Added copysign(x, y) function to the math module
Christian Heimes <christian@cheimes.de>
parents: 44437
diff changeset
807 self.assertEqual(math.copysign(4., -0.), -4.0)
6208b17d0dbe Added copysign(x, y) function to the math module
Christian Heimes <christian@cheimes.de>
parents: 44437
diff changeset
808
44437
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
809 def testIsnan(self):
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
810 self.assert_(math.isnan(float("nan")))
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
811 self.assert_(math.isnan(float("inf")* 0.))
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
812 self.failIf(math.isnan(float("inf")))
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
813 self.failIf(math.isnan(0.))
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
814 self.failIf(math.isnan(1.))
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
815
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
816 def testIsinf(self):
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
817 self.assert_(math.isinf(float("inf")))
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
818 self.assert_(math.isinf(float("-inf")))
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
819 self.assert_(math.isinf(1E400))
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
820 self.assert_(math.isinf(-1E400))
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
821 self.failIf(math.isinf(float("nan")))
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
822 self.failIf(math.isinf(0.))
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
823 self.failIf(math.isinf(1.))
968bda81e6c8 Added math.isinf() and math.isnan()
Christian Heimes <christian@cheimes.de>
parents: 44432
diff changeset
824
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
825 # RED_FLAG 16-Oct-2000 Tim
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
826 # While 2.0 is more consistent about exceptions than previous releases, it
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
827 # still fails this part of the test on some platforms. For now, we only
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
828 # *run* test_exceptions() in verbose mode, so that this isn't normally
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
829 # tested.
15170
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 13912
diff changeset
830
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
831 if verbose:
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
832 def test_exceptions(self):
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
833 try:
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
834 x = math.exp(-1000000000)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
835 except:
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
836 # mathmodule.c is failing to weed out underflows from libm, or
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
837 # we've got an fp format with huge dynamic range
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
838 self.fail("underflowing exp() should not have raised "
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
839 "an exception")
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
840 if x != 0:
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
841 self.fail("underflowing exp() should have returned 0")
15170
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 13912
diff changeset
842
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
843 # If this fails, probably using a strict IEEE-754 conforming libm, and x
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
844 # is +Inf afterwards. But Python wants overflows detected by default.
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
845 try:
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
846 x = math.exp(1000000000)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
847 except OverflowError:
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
848 pass
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
849 else:
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
850 self.fail("overflowing exp() didn't trigger OverflowError")
15170
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 13912
diff changeset
851
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
852 # If this fails, it could be a puzzle. One odd possibility is that
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
853 # mathmodule.c's macros are getting confused while comparing
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
854 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
855 # as a result (and so raising OverflowError instead).
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
856 try:
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
857 x = math.sqrt(-1.0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
858 except ValueError:
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
859 pass
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
860 else:
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
861 self.fail("sqrt(-1) didn't raise ValueError")
15170
09c7ac188ce2 Stop raising OverflowError on underflows reported by libm (errno==ERANGE and
Tim Peters <tim.peters@gmail.com>
parents: 13912
diff changeset
862
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
863 def test_testfile(self):
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
864 if not float.__getformat__("double").startswith("IEEE"):
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
865 return
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
866 for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file):
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
867 # Skip if either the input or result is complex, or if
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
868 # flags is nonempty
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
869 if ai != 0. or ei != 0. or flags:
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
870 continue
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
871 if fn in ['rect', 'polar']:
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
872 # no real versions of rect, polar
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
873 continue
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
874 func = getattr(math, fn)
46498
31ff59d984bd Report additional diagnostic information in
Mark Dickinson <dickinsm@gmail.com>
parents: 46486
diff changeset
875 try:
31ff59d984bd Report additional diagnostic information in
Mark Dickinson <dickinsm@gmail.com>
parents: 46486
diff changeset
876 result = func(ar)
31ff59d984bd Report additional diagnostic information in
Mark Dickinson <dickinsm@gmail.com>
parents: 46486
diff changeset
877 except ValueError:
31ff59d984bd Report additional diagnostic information in
Mark Dickinson <dickinsm@gmail.com>
parents: 46486
diff changeset
878 message = ("Unexpected ValueError in " +
31ff59d984bd Report additional diagnostic information in
Mark Dickinson <dickinsm@gmail.com>
parents: 46486
diff changeset
879 "test %s:%s(%r)\n" % (id, fn, ar))
31ff59d984bd Report additional diagnostic information in
Mark Dickinson <dickinsm@gmail.com>
parents: 46486
diff changeset
880 self.fail(message)
47218
5a29552412fa Better error reporting in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 47217
diff changeset
881 except OverflowError:
5a29552412fa Better error reporting in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 47217
diff changeset
882 message = ("Unexpected OverflowError in " +
5a29552412fa Better error reporting in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 47217
diff changeset
883 "test %s:%s(%r)\n" % (id, fn, ar))
5a29552412fa Better error reporting in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 47217
diff changeset
884 self.fail(message)
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
885 self.ftest("%s:%s(%r)" % (id, fn, ar), result, er)
15247
dd0d199674f8 Test for math.* exceptional behavior only in verbose mode, so that the
Tim Peters <tim.peters@gmail.com>
parents: 15171
diff changeset
886
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
887 def test_main():
46473
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
888 from doctest import DocFileSuite
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
889 suite = unittest.TestSuite()
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
890 suite.addTest(unittest.makeSuite(MathTests))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
891 suite.addTest(DocFileSuite("ieee754.txt"))
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 45079
diff changeset
892 run_unittest(suite)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
893
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
894 if __name__ == '__main__':
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
895 test_main()