annotate Lib/test/test_math.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents 55144cae02ba
children 34a686fbffeb
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
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
16 # 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
17 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
18 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
19 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
20 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
21 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
22 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
23
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 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
25 """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
26
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 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
28 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
29 """
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 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
31 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
32 # 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
33 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
34 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
35
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 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
37 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
38 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
39 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
40 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
41
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 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
43 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
44 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
45 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
46 )
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
47
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
48 class MathTests(unittest.TestCase):
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
50 def ftest(self, name, value, expected):
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
51 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
52 # 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
53 # 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
54 # 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
55 # error messages
0546c115a9cf Make test_math error messages more meaningful for small discrepancies in results
Nick Coghlan <ncoghlan@gmail.com>
parents: 42370
diff changeset
56 self.fail('%s returned %r, expected %r' %
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
57 (name, value, expected))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
58
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
59 def testConstants(self):
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
60 self.ftest('pi', math.pi, 3.1415926)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
61 self.ftest('e', math.e, 2.7182818)
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
62
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
63 def testAcos(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
64 self.assertRaises(TypeError, math.acos)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
65 self.ftest('acos(-1)', math.acos(-1), math.pi)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
66 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
67 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
68 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
69 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
70 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
71
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
72 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
73 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
74 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
75 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
76 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
77 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
78 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
79 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
80 self.assert_(math.isnan(math.acosh(NAN)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
82 def testAsin(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
83 self.assertRaises(TypeError, math.asin)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
84 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
85 self.ftest('asin(0)', math.asin(0), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
86 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
87 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
88 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
89 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
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
91 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
92 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
93 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
94 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
95 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
96 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
97 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
98 self.assert_(math.isnan(math.asinh(NAN)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
99
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
100 def testAtan(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
101 self.assertRaises(TypeError, math.atan)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
102 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
103 self.ftest('atan(0)', math.atan(0), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
104 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
105 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
106 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
107 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
108
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
109 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
110 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
111 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
112 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
113 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
114 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
115 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
116 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
117 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
118 self.assert_(math.isnan(math.atanh(NAN)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
119
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
120 def testAtan2(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
121 self.assertRaises(TypeError, math.atan2)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
122 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
123 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
124 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
125 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
126 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
127
46504
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
128 # math.atan2(0, x)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
129 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
130 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
131 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
132 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
133 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
134 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
135 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
136 # math.atan2(-0, x)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
137 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
138 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
139 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
140 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
141 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
142 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
143 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
144 # math.atan2(INF, x)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
145 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
146 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
147 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
148 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
149 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
150 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
151 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
152 # math.atan2(NINF, x)
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, -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
154 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
155 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
156 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
157 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
158 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
159 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
160 # math.atan2(+finite, x)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
161 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
162 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
163 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
164 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
165 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
166 # math.atan2(-finite, x)
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, -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
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.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
170 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
171 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
172 # math.atan2(NAN, x)
60f4456fa338 Add test for tanh(-0.) == -0. on IEEE 754 systems
Mark Dickinson <dickinsm@gmail.com>
parents: 46500
diff changeset
173 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
174 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
175 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
176 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
177 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
178 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
179 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
180
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
181 def testCeil(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
182 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
183 # 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
184 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
185 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
186 self.assertEquals(float, type(math.ceil(1.0)))
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
187 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
188 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
189 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
190 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
191 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
192 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
193 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
194 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
195 self.assert_(math.isnan(math.ceil(NAN)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
196
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
197 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
198 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
199 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
200 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
201 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
202 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
203 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
204
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 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
206 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
207 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
208 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
209
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
210 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
211 def testCopysign(self):
58768
55144cae02ba Merged revisions 78041 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57837
diff changeset
212 self.assertEqual(math.copysign(1, 42), 1.0)
55144cae02ba Merged revisions 78041 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57837
diff changeset
213 self.assertEqual(math.copysign(0., 42), 0.0)
55144cae02ba Merged revisions 78041 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57837
diff changeset
214 self.assertEqual(math.copysign(1., -42), -1.0)
55144cae02ba Merged revisions 78041 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57837
diff changeset
215 self.assertEqual(math.copysign(3, 0.), 3.0)
55144cae02ba Merged revisions 78041 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57837
diff changeset
216 self.assertEqual(math.copysign(4., -0.), -4.0)
55144cae02ba Merged revisions 78041 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57837
diff changeset
217
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
218 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
219 # copysign should let us distinguish signs of zeros
58768
55144cae02ba Merged revisions 78041 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57837
diff changeset
220 self.assertEquals(math.copysign(1., 0.), 1.)
55144cae02ba Merged revisions 78041 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57837
diff changeset
221 self.assertEquals(math.copysign(1., -0.), -1.)
55144cae02ba Merged revisions 78041 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57837
diff changeset
222 self.assertEquals(math.copysign(INF, 0.), INF)
55144cae02ba Merged revisions 78041 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57837
diff changeset
223 self.assertEquals(math.copysign(INF, -0.), NINF)
55144cae02ba Merged revisions 78041 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57837
diff changeset
224 self.assertEquals(math.copysign(NINF, 0.), INF)
55144cae02ba Merged revisions 78041 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57837
diff changeset
225 self.assertEquals(math.copysign(NINF, -0.), 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
226 # and of infinities
58768
55144cae02ba Merged revisions 78041 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57837
diff changeset
227 self.assertEquals(math.copysign(1., INF), 1.)
55144cae02ba Merged revisions 78041 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57837
diff changeset
228 self.assertEquals(math.copysign(1., NINF), -1.)
55144cae02ba Merged revisions 78041 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57837
diff changeset
229 self.assertEquals(math.copysign(INF, INF), INF)
55144cae02ba Merged revisions 78041 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57837
diff changeset
230 self.assertEquals(math.copysign(INF, NINF), NINF)
55144cae02ba Merged revisions 78041 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57837
diff changeset
231 self.assertEquals(math.copysign(NINF, INF), INF)
55144cae02ba Merged revisions 78041 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57837
diff changeset
232 self.assertEquals(math.copysign(NINF, NINF), NINF)
55144cae02ba Merged revisions 78041 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57837
diff changeset
233 self.assertTrue(math.isnan(math.copysign(NAN, 1.)))
55144cae02ba Merged revisions 78041 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57837
diff changeset
234 self.assertTrue(math.isnan(math.copysign(NAN, INF)))
55144cae02ba Merged revisions 78041 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57837
diff changeset
235 self.assertTrue(math.isnan(math.copysign(NAN, NINF)))
55144cae02ba Merged revisions 78041 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57837
diff changeset
236 self.assertTrue(math.isnan(math.copysign(NAN, NAN)))
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
237 # 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
238 # 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
239 # given platform.
58768
55144cae02ba Merged revisions 78041 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57837
diff changeset
240 self.assertTrue(math.isinf(math.copysign(INF, NAN)))
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
241 # similarly, copysign(2., NAN) could be 2. or -2.
58768
55144cae02ba Merged revisions 78041 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 57837
diff changeset
242 self.assertEquals(abs(math.copysign(2., NAN)), 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
243
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
244 def testCos(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
245 self.assertRaises(TypeError, math.cos)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
246 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
247 self.ftest('cos(0)', math.cos(0), 1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
248 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
249 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
250 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
251 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
252 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
253 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
254 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
255 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
256 self.assert_(math.isnan(math.cos(NAN)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
257
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
258 def testCosh(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
259 self.assertRaises(TypeError, math.cosh)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
260 self.ftest('cosh(0)', math.cosh(0), 1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
261 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
262 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
263 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
264 self.assert_(math.isnan(math.cosh(NAN)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
265
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
266 def testDegrees(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
267 self.assertRaises(TypeError, math.degrees)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
268 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
269 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
270 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
271
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
272 def testExp(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
273 self.assertRaises(TypeError, math.exp)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
274 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
275 self.ftest('exp(0)', math.exp(0), 1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
276 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
277 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
278 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
279 self.assert_(math.isnan(math.exp(NAN)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
280
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
281 def testFabs(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
282 self.assertRaises(TypeError, math.fabs)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
283 self.ftest('fabs(-1)', math.fabs(-1), 1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
284 self.ftest('fabs(0)', math.fabs(0), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
285 self.ftest('fabs(1)', math.fabs(1), 1)
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
286
47529
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
287 def testFactorial(self):
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
288 def fact(n):
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
289 result = 1
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
290 for i in range(1, int(n)+1):
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
291 result *= i
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
292 return result
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
293 values = range(10) + [50, 100, 500]
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
294 random.shuffle(values)
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
295 for x in range(10):
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
296 for cast in (int, long, float):
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
297 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
298 self.assertRaises(ValueError, math.factorial, -1)
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
299 self.assertRaises(ValueError, math.factorial, math.pi)
6428c4f1ae09 Issue #2138: Add math.factorial().
Raymond Hettinger <python@rcn.com>
parents: 47382
diff changeset
300
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
301 def testFloor(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
302 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
303 # 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
304 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
305 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
306 self.assertEquals(float, type(math.floor(1.0)))
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
307 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
308 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
309 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
310 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
311 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
312 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
313 # 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
314 # 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
315 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
316 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
317 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
318 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
319 self.assert_(math.isnan(math.floor(NAN)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
320
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
321 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
322 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
323 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
324 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
325 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
326 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
327 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
328
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 = 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
330 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
331 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
332 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
333
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
334 def testFmod(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
335 self.assertRaises(TypeError, math.fmod)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
336 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
337 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
338 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
339 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
340 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
341 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
342 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
343 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
344 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
345 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
346 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
347 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
348 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
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, 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
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(-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
353 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
354 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
355
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
356 def testFrexp(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
357 self.assertRaises(TypeError, math.frexp)
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
358
57837
bf2eff60eae2 Merged revisions 76945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 48110
diff changeset
359 def testfrexp(name, result, expected):
bf2eff60eae2 Merged revisions 76945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 48110
diff changeset
360 (mant, exp), (emant, eexp) = result, expected
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
361 if abs(mant-emant) > eps or exp != eexp:
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
362 self.fail('%s returned %r, expected %r'%\
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
363 (name, (mant, exp), (emant,eexp)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
364
40219
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(0)', math.frexp(0), (0, 0))
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
367 testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
368 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
369
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
370 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
371 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
372 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
373
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
374 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
375 # 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
376 # 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
377 # 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
378 # 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
379 # 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
380 # 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
381 # 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
382
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 if not float.__getformat__("double").startswith("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
384 return
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
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 # on IEEE 754 compliant machines, both of the expressions
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 # below should round to 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
388 if 1e16+2.0 != 1e16+2.9999:
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 return
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
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 # 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
392 # 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
393 # 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
394 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
395 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
396 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
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 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
399 """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
400 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
401 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
402
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 """
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 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
405 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
406 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
407 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
408 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
409 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
410 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
411 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
412 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
413 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
414 # 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
415 # 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
416 # 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
417 # 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
418 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
419 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
420 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
421 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
422 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
423 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
424
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 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
426 ([], 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
427 ([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
428 ([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
429 ([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
430 ([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
431 ([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
432 ([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
433 ([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
434 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
435 ([(-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
436 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
437 ([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
438 ([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
439 ([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
440 # 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
441 ([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
442 [-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
443 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
444 ]
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
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 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
447 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
448 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
449 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
450 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
451 "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
452 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
453 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
454 "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
455 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
456
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 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
458 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
459 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
460 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
461 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
462 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
463 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
464 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
465 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
466
347603f794d1 Rename testSum to testFsum and move it to proper place in test_math.py
Mark Dickinson <dickinsm@gmail.com>
parents: 48101
diff changeset
467 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
468 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
469
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
470 def testHypot(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
471 self.assertRaises(TypeError, math.hypot)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
472 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
473 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
474 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
475 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
476 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
477 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
478 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
479 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
480
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
481 def testLdexp(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
482 self.assertRaises(TypeError, math.ldexp)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
483 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
484 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
485 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
486 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
487 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
488 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
489 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
490 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
491 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
492 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
493 self.assert_(math.isnan(math.ldexp(NAN, 0)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
494
46797
fd0b894bc90c Issue #2487. math.ldexp(x, n) raised OverflowError when n was large and
Mark Dickinson <dickinsm@gmail.com>
parents: 46504
diff changeset
495 # 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
496 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
497 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
498 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
499 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
500 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
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.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
504
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.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
506 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
507 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
508 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
509 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
510 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
511 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
512
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
513 def testLog(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
514 self.assertRaises(TypeError, math.log)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
515 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
516 self.ftest('log(1)', math.log(1), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
517 self.ftest('log(e)', math.log(math.e), 1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
518 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
519 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
520 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
521 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
522 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
523 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
524
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 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
526 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
527 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
528 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
529 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
530 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
531 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
532 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
533 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
534 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
535 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
536 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
537
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
538 def testLog10(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
539 self.assertRaises(TypeError, math.log10)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
540 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
541 self.ftest('log10(1)', math.log10(1), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
542 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
543 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
544 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
545 self.assert_(math.isnan(math.log10(NAN)))
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
546
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
547 def testModf(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
548 self.assertRaises(TypeError, math.modf)
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
549
57837
bf2eff60eae2 Merged revisions 76945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 48110
diff changeset
550 def testmodf(name, result, expected):
bf2eff60eae2 Merged revisions 76945 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 48110
diff changeset
551 (v1, v2), (e1, e2) = result, expected
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
552 if abs(v1-e1) > eps or abs(v2-e2):
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
553 self.fail('%s returned %r, expected %r'%\
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
554 (name, (v1,v2), (e1,e2)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
555
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
556 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
557 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
558
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
559 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
560 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
561
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
562 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
563 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
564 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
565
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
566 def testPow(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
567 self.assertRaises(TypeError, math.pow)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
568 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
569 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
570 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
571 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
572 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
573 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
574 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
575 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
576 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
577 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
578 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
579 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
580
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
581 # pow(0., x)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
582 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
583 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
584 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
585 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
586 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
587 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
588 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
589 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
590 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
591 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
592 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
593
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
594 # pow(INF, x)
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, INF), INF)
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.), INF)
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, 2.3), INF)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
598 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
599 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
600 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
601 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
602 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
603 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
604 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
605 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
606
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
607 # pow(-0., x)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
608 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
609 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
610 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
611 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
612 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
613 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
614 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
615 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
616 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
617 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
618 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
619
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
620 # pow(NINF, x)
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, INF), INF)
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.), NINF)
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, 2.3), INF)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
624 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
625 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
626 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
627 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
628 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
629 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
630 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
631 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
632
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
633 # pow(-1, x)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
634 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
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.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
637 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
638 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
639 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
640 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
641 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
642 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
643 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
644 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
645
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
646 # pow(1, x)
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., INF), 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., 2.3), 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., 2.), 1.)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
651 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
652 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
653 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
654 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
655 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
656 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
657 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
658
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
659 # 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
660 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
661 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
662 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
663 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
664 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
665 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
666
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
667 # 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
668 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
669 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
670
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
671 # pow(x, NINF)
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 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
674 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
675 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
676 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
677 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
678 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
679 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
680
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
681 # pow(x, 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 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
684 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
685 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
686 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
687 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
688 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
689 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
690
46500
a20def0101de Yet more explicit special case handling to make
Mark Dickinson <dickinsm@gmail.com>
parents: 46498
diff changeset
691 # 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
692 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
693 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
694 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
695 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
696 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
697 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
698 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
699 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
700 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
701 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
702
46486
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
703 # 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
704 # 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
705 # 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
706 #self.assertEqual(1**NAN, 1)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
707 #self.assertEqual(1**INF, 1)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
708 #self.assertEqual(1**NINF, 1)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
709 #self.assertEqual(1**0, 1)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
710 #self.assertEqual(1.**NAN, 1)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
711 #self.assertEqual(1.**INF, 1)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
712 #self.assertEqual(1.**NINF, 1)
82d85a05923b Additional tests for math.pow, and extra special-case
Mark Dickinson <dickinsm@gmail.com>
parents: 46473
diff changeset
713 #self.assertEqual(1.**0, 1)
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
714
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
715 def testRadians(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
716 self.assertRaises(TypeError, math.radians)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
717 self.ftest('radians(180)', math.radians(180), math.pi)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
718 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
719 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
720
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
721 def testSin(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
722 self.assertRaises(TypeError, math.sin)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
723 self.ftest('sin(0)', math.sin(0), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
724 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
725 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
726 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
727 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
728 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
729 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
730 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
731 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
732 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
733
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
734 def testSinh(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
735 self.assertRaises(TypeError, math.sinh)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
736 self.ftest('sinh(0)', math.sinh(0), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
737 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
738 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
739 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
740 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
741 self.assert_(math.isnan(math.sinh(NAN)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
742
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
743 def testSqrt(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
744 self.assertRaises(TypeError, math.sqrt)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
745 self.ftest('sqrt(0)', math.sqrt(0), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
746 self.ftest('sqrt(1)', math.sqrt(1), 1)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
747 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
748 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
749 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
750 self.assert_(math.isnan(math.sqrt(NAN)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
751
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
752 def testTan(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
753 self.assertRaises(TypeError, math.tan)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
754 self.ftest('tan(0)', math.tan(0), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
755 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
756 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
757 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
758 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
759 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
760 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
761 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
762 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
763 self.assert_(math.isnan(math.tan(NAN)))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
764
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
765 def testTanh(self):
40270
1715e6a0aee9 Add tests for basic argument errors.
Walter Dörwald <walter@livinglogic.de>
parents: 40219
diff changeset
766 self.assertRaises(TypeError, math.tanh)
40219
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
767 self.ftest('tanh(0)', math.tanh(0), 0)
69728a7d2312 Convert test_math to unittest.
Georg Brandl <georg@python.org>
parents: 31515
diff changeset
768 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
769 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
770 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
771 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
772 # 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
773 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
774 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
775 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
776 math.copysign(1., -0.))
3950
75d016c97d1c Added test for math module
Guido van Rossum <guido@python.org>
parents:
diff changeset
777
45079
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
778 def test_trunc(self):
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
779 self.assertEqual(math.trunc(1), 1)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
780 self.assertEqual(math.trunc(-1), -1)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
781 self.assertEqual(type(math.trunc(1)), int)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
782 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
783 self.assertEqual(math.trunc(1.5), 1)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
784 self.assertEqual(math.trunc(-1.5), -1)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
785 self.assertEqual(math.trunc(1.999999), 1)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
786 self.assertEqual(math.trunc(-1.999999), -1)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
787 self.assertEqual(math.trunc(-0.999999), -0)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
788 self.assertEqual(math.trunc(-100.999), -100)
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 class TestTrunc(object):
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
791 def __trunc__(self):
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
792 return 23
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
793
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
794 class TestNoTrunc(object):
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
795 pass
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 self.assertEqual(math.trunc(TestTrunc()), 23)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
798
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
799 self.assertRaises(TypeError, math.trunc)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
800 self.assertRaises(TypeError, math.trunc, 1, 2)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
801 # 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
802 self.assertRaises(AttributeError, math.trunc, TestNoTrunc())
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
803
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
804 t = TestNoTrunc()
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
805 t.__trunc__ = lambda *args: args
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
806 self.assertEquals((), math.trunc(t))
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
diff changeset
807 self.assertRaises(TypeError, math.trunc, t, 0)
492b302aded6 Move __builtins__.trunc() to math.trunc() per
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 44488
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()