annotate Lib/test/test_float.py @ 57303:4aa4c19dfaed legacy-trunk

#1757126: fix typo with the cyrillic_asian alias
author Philip Jenvey <pjenvey@underboss.org>
date Tue, 17 Nov 2009 02:42:26 +0000
parents f10104051339
children 92e06cdbeab9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
1
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
2 import unittest, struct
44262
24749a380409 Backport of r59456:59458 from py3k to trunk
Christian Heimes <christian@cheimes.de>
parents: 42704
diff changeset
3 import os
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
4 from test import test_support
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: 44482
diff changeset
5 import math
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
6 from math import isinf, isnan, copysign, ldexp
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: 44482
diff changeset
7 import operator
49054
e27cf7ee2968 #3796: A test class was not run in test_float.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48816
diff changeset
8 import random, fractions
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
9
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: 44482
diff changeset
10 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: 44482
diff changeset
11 NAN = float("nan")
44344
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
12
53619
5d9771d7cc46 Backport some of the float formatting tests from py3k.
Mark Dickinson <dickinsm@gmail.com>
parents: 53611
diff changeset
13 #locate file with float format test values
5d9771d7cc46 Backport some of the float formatting tests from py3k.
Mark Dickinson <dickinsm@gmail.com>
parents: 53611
diff changeset
14 test_dir = os.path.dirname(__file__) or os.curdir
5d9771d7cc46 Backport some of the float formatting tests from py3k.
Mark Dickinson <dickinsm@gmail.com>
parents: 53611
diff changeset
15 format_testfile = os.path.join(test_dir, 'formatfloat_testcases.txt')
5d9771d7cc46 Backport some of the float formatting tests from py3k.
Mark Dickinson <dickinsm@gmail.com>
parents: 53611
diff changeset
16
46649
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
17 class GeneralFloatCases(unittest.TestCase):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
18
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
19 def test_float(self):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
20 self.assertEqual(float(3.14), 3.14)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
21 self.assertEqual(float(314), 314.0)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
22 self.assertEqual(float(314L), 314.0)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
23 self.assertEqual(float(" 3.14 "), 3.14)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
24 self.assertRaises(ValueError, float, " 0x3.1 ")
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
25 self.assertRaises(ValueError, float, " -0x3.p-1 ")
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
26 self.assertRaises(ValueError, float, " +0x3.p-1 ")
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
27 self.assertRaises(ValueError, float, "++3.14")
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
28 self.assertRaises(ValueError, float, "+-3.14")
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
29 self.assertRaises(ValueError, float, "-+3.14")
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
30 self.assertRaises(ValueError, float, "--3.14")
49054
e27cf7ee2968 #3796: A test class was not run in test_float.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48816
diff changeset
31 if test_support.have_unicode:
46649
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
32 self.assertEqual(float(unicode(" 3.14 ")), 3.14)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
33 self.assertEqual(float(unicode(" \u0663.\u0661\u0664 ",'raw-unicode-escape')), 3.14)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
34
56913
f10104051339 Test long inputs to float
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
35 # extra long strings should no longer be a problem
f10104051339 Test long inputs to float
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
36 # (in 2.6, long unicode inputs to float raised ValueError)
f10104051339 Test long inputs to float
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
37 float('.' + '1'*1000)
f10104051339 Test long inputs to float
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
38 float(unicode('.' + '1'*1000))
f10104051339 Test long inputs to float
Mark Dickinson <dickinsm@gmail.com>
parents: 56766
diff changeset
39
46649
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
40 @test_support.run_with_locale('LC_NUMERIC', 'fr_FR', 'de_DE')
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
41 def test_float_with_comma(self):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
42 # set locale to something that doesn't use '.' for the decimal point
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
43 # float must not accept the locale specific decimal point but
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
44 # it still has to accept the normal python syntac
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
45 import locale
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
46 if not locale.localeconv()['decimal_point'] == ',':
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
47 return
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
48
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
49 self.assertEqual(float(" 3.14 "), 3.14)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
50 self.assertEqual(float("+3.14 "), 3.14)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
51 self.assertEqual(float("-3.14 "), -3.14)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
52 self.assertEqual(float(".14 "), .14)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
53 self.assertEqual(float("3. "), 3.0)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
54 self.assertEqual(float("3.e3 "), 3000.0)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
55 self.assertEqual(float("3.2e3 "), 3200.0)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
56 self.assertEqual(float("2.5e-1 "), 0.25)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
57 self.assertEqual(float("5e-1"), 0.5)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
58 self.assertRaises(ValueError, float, " 3,14 ")
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
59 self.assertRaises(ValueError, float, " +3,14 ")
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
60 self.assertRaises(ValueError, float, " -3,14 ")
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
61 self.assertRaises(ValueError, float, " 0x3.1 ")
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
62 self.assertRaises(ValueError, float, " -0x3.p-1 ")
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
63 self.assertRaises(ValueError, float, " +0x3.p-1 ")
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
64 self.assertEqual(float(" 25.e-1 "), 2.5)
49061
fdc0948c95cf fix missing module
Benjamin Peterson <benjamin@python.org>
parents: 49054
diff changeset
65 self.assertEqual(test_support.fcmp(float(" .25e-1 "), .025), 0)
46649
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
66
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
67 def test_floatconversion(self):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
68 # Make sure that calls to __float__() work properly
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
69 class Foo0:
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
70 def __float__(self):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
71 return 42.
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
72
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
73 class Foo1(object):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
74 def __float__(self):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
75 return 42.
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
76
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
77 class Foo2(float):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
78 def __float__(self):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
79 return 42.
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
80
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
81 class Foo3(float):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
82 def __new__(cls, value=0.):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
83 return float.__new__(cls, 2*value)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
84
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
85 def __float__(self):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
86 return self
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
87
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
88 class Foo4(float):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
89 def __float__(self):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
90 return 42
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
91
53162
6bcdc131f53c Merged revisions 71627 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49061
diff changeset
92 # Issue 5759: __float__ not called on str subclasses (though it is on
6bcdc131f53c Merged revisions 71627 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49061
diff changeset
93 # unicode subclasses).
6bcdc131f53c Merged revisions 71627 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49061
diff changeset
94 class FooStr(str):
6bcdc131f53c Merged revisions 71627 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49061
diff changeset
95 def __float__(self):
6bcdc131f53c Merged revisions 71627 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49061
diff changeset
96 return float(str(self)) + 1
6bcdc131f53c Merged revisions 71627 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49061
diff changeset
97
6bcdc131f53c Merged revisions 71627 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49061
diff changeset
98 class FooUnicode(unicode):
6bcdc131f53c Merged revisions 71627 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49061
diff changeset
99 def __float__(self):
6bcdc131f53c Merged revisions 71627 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49061
diff changeset
100 return float(unicode(self)) + 1
6bcdc131f53c Merged revisions 71627 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49061
diff changeset
101
46649
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
102 self.assertAlmostEqual(float(Foo0()), 42.)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
103 self.assertAlmostEqual(float(Foo1()), 42.)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
104 self.assertAlmostEqual(float(Foo2()), 42.)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
105 self.assertAlmostEqual(float(Foo3(21)), 42.)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
106 self.assertRaises(TypeError, float, Foo4(42))
53162
6bcdc131f53c Merged revisions 71627 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49061
diff changeset
107 self.assertAlmostEqual(float(FooUnicode('8')), 9.)
6bcdc131f53c Merged revisions 71627 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49061
diff changeset
108 self.assertAlmostEqual(float(FooStr('8')), 9.)
46649
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
109
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
110 def test_floatasratio(self):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
111 for f, ratio in [
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
112 (0.875, (7, 8)),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
113 (-0.875, (-7, 8)),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
114 (0.0, (0, 1)),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
115 (11.5, (23, 2)),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
116 ]:
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
117 self.assertEqual(f.as_integer_ratio(), ratio)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
118
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
119 for i in range(10000):
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
120 f = random.random()
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
121 f *= 10 ** random.randint(-100, 100)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
122 n, d = f.as_integer_ratio()
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
123 self.assertEqual(float(n).__truediv__(d), f)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
124
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
125 R = fractions.Fraction
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
126 self.assertEqual(R(0, 1),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
127 R(*float(0.0).as_integer_ratio()))
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
128 self.assertEqual(R(5, 2),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
129 R(*float(2.5).as_integer_ratio()))
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
130 self.assertEqual(R(1, 2),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
131 R(*float(0.5).as_integer_ratio()))
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
132 self.assertEqual(R(4728779608739021, 2251799813685248),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
133 R(*float(2.1).as_integer_ratio()))
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
134 self.assertEqual(R(-4728779608739021, 2251799813685248),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
135 R(*float(-2.1).as_integer_ratio()))
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
136 self.assertEqual(R(-2100, 1),
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
137 R(*float(-2100.0).as_integer_ratio()))
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
138
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
139 self.assertRaises(OverflowError, float('inf').as_integer_ratio)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
140 self.assertRaises(OverflowError, float('-inf').as_integer_ratio)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
141 self.assertRaises(ValueError, float('nan').as_integer_ratio)
83cc09682081 Moved testing of builtin types out of test_builtin and into type specific modules
Benjamin Peterson <benjamin@python.org>
parents: 46473
diff changeset
142
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
143 class FormatFunctionsTestCase(unittest.TestCase):
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
144
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
145 def setUp(self):
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
146 self.save_formats = {'double':float.__getformat__('double'),
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
147 'float':float.__getformat__('float')}
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
148
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
149 def tearDown(self):
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
150 float.__setformat__('double', self.save_formats['double'])
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
151 float.__setformat__('float', self.save_formats['float'])
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
152
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
153 def test_getformat(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54000
diff changeset
154 self.assertTrue(float.__getformat__('double') in
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
155 ['unknown', 'IEEE, big-endian', 'IEEE, little-endian'])
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54000
diff changeset
156 self.assertTrue(float.__getformat__('float') in
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
157 ['unknown', 'IEEE, big-endian', 'IEEE, little-endian'])
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
158 self.assertRaises(ValueError, float.__getformat__, 'chicken')
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
159 self.assertRaises(TypeError, float.__getformat__, 1)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
160
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
161 def test_setformat(self):
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
162 for t in 'double', 'float':
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
163 float.__setformat__(t, 'unknown')
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
164 if self.save_formats[t] == 'IEEE, big-endian':
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
165 self.assertRaises(ValueError, float.__setformat__,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
166 t, 'IEEE, little-endian')
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
167 elif self.save_formats[t] == 'IEEE, little-endian':
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
168 self.assertRaises(ValueError, float.__setformat__,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
169 t, 'IEEE, big-endian')
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
170 else:
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
171 self.assertRaises(ValueError, float.__setformat__,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
172 t, 'IEEE, big-endian')
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
173 self.assertRaises(ValueError, float.__setformat__,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
174 t, 'IEEE, little-endian')
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
175 self.assertRaises(ValueError, float.__setformat__,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
176 t, 'chicken')
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
177 self.assertRaises(ValueError, float.__setformat__,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
178 'chicken', 'unknown')
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
179
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
180 BE_DOUBLE_INF = '\x7f\xf0\x00\x00\x00\x00\x00\x00'
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
181 LE_DOUBLE_INF = ''.join(reversed(BE_DOUBLE_INF))
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
182 BE_DOUBLE_NAN = '\x7f\xf8\x00\x00\x00\x00\x00\x00'
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
183 LE_DOUBLE_NAN = ''.join(reversed(BE_DOUBLE_NAN))
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
184
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
185 BE_FLOAT_INF = '\x7f\x80\x00\x00'
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
186 LE_FLOAT_INF = ''.join(reversed(BE_FLOAT_INF))
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
187 BE_FLOAT_NAN = '\x7f\xc0\x00\x00'
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
188 LE_FLOAT_NAN = ''.join(reversed(BE_FLOAT_NAN))
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
189
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
190 # on non-IEEE platforms, attempting to unpack a bit pattern
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
191 # representing an infinity or a NaN should raise an exception.
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
192
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
193 class UnknownFormatTestCase(unittest.TestCase):
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
194 def setUp(self):
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
195 self.save_formats = {'double':float.__getformat__('double'),
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
196 'float':float.__getformat__('float')}
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
197 float.__setformat__('double', 'unknown')
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
198 float.__setformat__('float', 'unknown')
35014
275843630522 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 34972
diff changeset
199
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
200 def tearDown(self):
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
201 float.__setformat__('double', self.save_formats['double'])
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
202 float.__setformat__('float', self.save_formats['float'])
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
203
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
204 def test_double_specials_dont_unpack(self):
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
205 for fmt, data in [('>d', BE_DOUBLE_INF),
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
206 ('>d', BE_DOUBLE_NAN),
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
207 ('<d', LE_DOUBLE_INF),
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
208 ('<d', LE_DOUBLE_NAN)]:
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
209 self.assertRaises(ValueError, struct.unpack, fmt, data)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
210
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
211 def test_float_specials_dont_unpack(self):
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
212 for fmt, data in [('>f', BE_FLOAT_INF),
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
213 ('>f', BE_FLOAT_NAN),
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
214 ('<f', LE_FLOAT_INF),
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
215 ('<f', LE_FLOAT_NAN)]:
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
216 self.assertRaises(ValueError, struct.unpack, fmt, data)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
217
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
218
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
219 # on an IEEE platform, all we guarantee is that bit patterns
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
220 # representing infinities or NaNs do not raise an exception; all else
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
221 # is accident (today).
42704
092f02cf018f Fix compile.c so that it records 0.0 and -0.0 as separate constants in a code
Alex Martelli <aleaxit@gmail.com>
parents: 39642
diff changeset
222 # let's also try to guarantee that -0.0 and 0.0 don't get confused.
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
223
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
224 class IEEEFormatTestCase(unittest.TestCase):
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
225 if float.__getformat__("double").startswith("IEEE"):
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
226 def test_double_specials_do_unpack(self):
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
227 for fmt, data in [('>d', BE_DOUBLE_INF),
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
228 ('>d', BE_DOUBLE_NAN),
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
229 ('<d', LE_DOUBLE_INF),
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
230 ('<d', LE_DOUBLE_NAN)]:
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
231 struct.unpack(fmt, data)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
232
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
233 if float.__getformat__("float").startswith("IEEE"):
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
234 def test_float_specials_do_unpack(self):
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
235 for fmt, data in [('>f', BE_FLOAT_INF),
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
236 ('>f', BE_FLOAT_NAN),
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
237 ('<f', LE_FLOAT_INF),
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
238 ('<f', LE_FLOAT_NAN)]:
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
239 struct.unpack(fmt, data)
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
240
42704
092f02cf018f Fix compile.c so that it records 0.0 and -0.0 as separate constants in a code
Alex Martelli <aleaxit@gmail.com>
parents: 39642
diff changeset
241 if float.__getformat__("double").startswith("IEEE"):
092f02cf018f Fix compile.c so that it records 0.0 and -0.0 as separate constants in a code
Alex Martelli <aleaxit@gmail.com>
parents: 39642
diff changeset
242 def test_negative_zero(self):
092f02cf018f Fix compile.c so that it records 0.0 and -0.0 as separate constants in a code
Alex Martelli <aleaxit@gmail.com>
parents: 39642
diff changeset
243 import math
092f02cf018f Fix compile.c so that it records 0.0 and -0.0 as separate constants in a code
Alex Martelli <aleaxit@gmail.com>
parents: 39642
diff changeset
244 def pos_pos():
092f02cf018f Fix compile.c so that it records 0.0 and -0.0 as separate constants in a code
Alex Martelli <aleaxit@gmail.com>
parents: 39642
diff changeset
245 return 0.0, math.atan2(0.0, -1)
092f02cf018f Fix compile.c so that it records 0.0 and -0.0 as separate constants in a code
Alex Martelli <aleaxit@gmail.com>
parents: 39642
diff changeset
246 def pos_neg():
092f02cf018f Fix compile.c so that it records 0.0 and -0.0 as separate constants in a code
Alex Martelli <aleaxit@gmail.com>
parents: 39642
diff changeset
247 return 0.0, math.atan2(-0.0, -1)
092f02cf018f Fix compile.c so that it records 0.0 and -0.0 as separate constants in a code
Alex Martelli <aleaxit@gmail.com>
parents: 39642
diff changeset
248 def neg_pos():
092f02cf018f Fix compile.c so that it records 0.0 and -0.0 as separate constants in a code
Alex Martelli <aleaxit@gmail.com>
parents: 39642
diff changeset
249 return -0.0, math.atan2(0.0, -1)
092f02cf018f Fix compile.c so that it records 0.0 and -0.0 as separate constants in a code
Alex Martelli <aleaxit@gmail.com>
parents: 39642
diff changeset
250 def neg_neg():
092f02cf018f Fix compile.c so that it records 0.0 and -0.0 as separate constants in a code
Alex Martelli <aleaxit@gmail.com>
parents: 39642
diff changeset
251 return -0.0, math.atan2(-0.0, -1)
092f02cf018f Fix compile.c so that it records 0.0 and -0.0 as separate constants in a code
Alex Martelli <aleaxit@gmail.com>
parents: 39642
diff changeset
252 self.assertEquals(pos_pos(), neg_pos())
092f02cf018f Fix compile.c so that it records 0.0 and -0.0 as separate constants in a code
Alex Martelli <aleaxit@gmail.com>
parents: 39642
diff changeset
253 self.assertEquals(pos_neg(), neg_neg())
092f02cf018f Fix compile.c so that it records 0.0 and -0.0 as separate constants in a code
Alex Martelli <aleaxit@gmail.com>
parents: 39642
diff changeset
254
44482
3fcaf6cc306d Patch #1725 by Mark Dickinson, fixes incorrect conversion of -1e1000
Guido van Rossum <guido@python.org>
parents: 44344
diff changeset
255 if float.__getformat__("double").startswith("IEEE"):
3fcaf6cc306d Patch #1725 by Mark Dickinson, fixes incorrect conversion of -1e1000
Guido van Rossum <guido@python.org>
parents: 44344
diff changeset
256 def test_underflow_sign(self):
3fcaf6cc306d Patch #1725 by Mark Dickinson, fixes incorrect conversion of -1e1000
Guido van Rossum <guido@python.org>
parents: 44344
diff changeset
257 import math
3fcaf6cc306d Patch #1725 by Mark Dickinson, fixes incorrect conversion of -1e1000
Guido van Rossum <guido@python.org>
parents: 44344
diff changeset
258 # check that -1e-1000 gives -0.0, not 0.0
3fcaf6cc306d Patch #1725 by Mark Dickinson, fixes incorrect conversion of -1e1000
Guido van Rossum <guido@python.org>
parents: 44344
diff changeset
259 self.assertEquals(math.atan2(-1e-1000, -1), math.atan2(-0.0, -1))
3fcaf6cc306d Patch #1725 by Mark Dickinson, fixes incorrect conversion of -1e1000
Guido van Rossum <guido@python.org>
parents: 44344
diff changeset
260 self.assertEquals(math.atan2(float('-1e-1000'), -1),
3fcaf6cc306d Patch #1725 by Mark Dickinson, fixes incorrect conversion of -1e1000
Guido van Rossum <guido@python.org>
parents: 44344
diff changeset
261 math.atan2(-0.0, -1))
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
262
53802
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
263 def test_format(self):
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
264 # these should be rewritten to use both format(x, spec) and
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
265 # x.__format__(spec)
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
266
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
267 self.assertEqual(format(0.0, 'f'), '0.000000')
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
268
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
269 # the default is 'g', except for empty format spec
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
270 self.assertEqual(format(0.0, ''), '0.0')
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
271 self.assertEqual(format(0.01, ''), '0.01')
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
272 self.assertEqual(format(0.01, 'g'), '0.01')
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
273
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
274 # empty presentation type should format in the same way as str
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
275 # (issue 5920)
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
276 x = 100/7.
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
277 self.assertEqual(format(x, ''), str(x))
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
278 self.assertEqual(format(x, '-'), str(x))
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
279 self.assertEqual(format(x, '>'), str(x))
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
280 self.assertEqual(format(x, '2'), str(x))
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
281
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
282 self.assertEqual(format(1.0, 'f'), '1.000000')
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
283
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
284 self.assertEqual(format(-1.0, 'f'), '-1.000000')
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
285
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
286 self.assertEqual(format( 1.0, ' f'), ' 1.000000')
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
287 self.assertEqual(format(-1.0, ' f'), '-1.000000')
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
288 self.assertEqual(format( 1.0, '+f'), '+1.000000')
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
289 self.assertEqual(format(-1.0, '+f'), '-1.000000')
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
290
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
291 # % formatting
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
292 self.assertEqual(format(-1.0, '%'), '-100.000000%')
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
293
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
294 # conversion to string should fail
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
295 self.assertRaises(ValueError, format, 3.0, "s")
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
296
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
297 # other format specifiers shouldn't work on floats,
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
298 # in particular int specifiers
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
299 for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
300 [chr(x) for x in range(ord('A'), ord('Z')+1)]):
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
301 if not format_spec in 'eEfFgGn%':
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
302 self.assertRaises(ValueError, format, 0.0, format_spec)
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
303 self.assertRaises(ValueError, format, 1.0, format_spec)
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
304 self.assertRaises(ValueError, format, -1.0, format_spec)
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
305 self.assertRaises(ValueError, format, 1e100, format_spec)
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
306 self.assertRaises(ValueError, format, -1e100, format_spec)
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
307 self.assertRaises(ValueError, format, 1e-100, format_spec)
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
308 self.assertRaises(ValueError, format, -1e-100, format_spec)
1d8f24d40c2c Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents: 53619
diff changeset
309
53619
5d9771d7cc46 Backport some of the float formatting tests from py3k.
Mark Dickinson <dickinsm@gmail.com>
parents: 53611
diff changeset
310 @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
5d9771d7cc46 Backport some of the float formatting tests from py3k.
Mark Dickinson <dickinsm@gmail.com>
parents: 53611
diff changeset
311 "test requires IEEE 754 doubles")
5d9771d7cc46 Backport some of the float formatting tests from py3k.
Mark Dickinson <dickinsm@gmail.com>
parents: 53611
diff changeset
312 def test_format_testfile(self):
5d9771d7cc46 Backport some of the float formatting tests from py3k.
Mark Dickinson <dickinsm@gmail.com>
parents: 53611
diff changeset
313 for line in open(format_testfile):
5d9771d7cc46 Backport some of the float formatting tests from py3k.
Mark Dickinson <dickinsm@gmail.com>
parents: 53611
diff changeset
314 if line.startswith('--'):
5d9771d7cc46 Backport some of the float formatting tests from py3k.
Mark Dickinson <dickinsm@gmail.com>
parents: 53611
diff changeset
315 continue
5d9771d7cc46 Backport some of the float formatting tests from py3k.
Mark Dickinson <dickinsm@gmail.com>
parents: 53611
diff changeset
316 line = line.strip()
5d9771d7cc46 Backport some of the float formatting tests from py3k.
Mark Dickinson <dickinsm@gmail.com>
parents: 53611
diff changeset
317 if not line:
5d9771d7cc46 Backport some of the float formatting tests from py3k.
Mark Dickinson <dickinsm@gmail.com>
parents: 53611
diff changeset
318 continue
5d9771d7cc46 Backport some of the float formatting tests from py3k.
Mark Dickinson <dickinsm@gmail.com>
parents: 53611
diff changeset
319
5d9771d7cc46 Backport some of the float formatting tests from py3k.
Mark Dickinson <dickinsm@gmail.com>
parents: 53611
diff changeset
320 lhs, rhs = map(str.strip, line.split('->'))
5d9771d7cc46 Backport some of the float formatting tests from py3k.
Mark Dickinson <dickinsm@gmail.com>
parents: 53611
diff changeset
321 fmt, arg = lhs.split()
5d9771d7cc46 Backport some of the float formatting tests from py3k.
Mark Dickinson <dickinsm@gmail.com>
parents: 53611
diff changeset
322 self.assertEqual(fmt % float(arg), rhs)
5d9771d7cc46 Backport some of the float formatting tests from py3k.
Mark Dickinson <dickinsm@gmail.com>
parents: 53611
diff changeset
323 self.assertEqual(fmt % -float(arg), '-' + rhs)
5d9771d7cc46 Backport some of the float formatting tests from py3k.
Mark Dickinson <dickinsm@gmail.com>
parents: 53611
diff changeset
324
53611
a3fcccee8957 Issue #5864: format(1234.5, '.4') gives misleading result
Mark Dickinson <dickinsm@gmail.com>
parents: 53162
diff changeset
325 def test_issue5864(self):
a3fcccee8957 Issue #5864: format(1234.5, '.4') gives misleading result
Mark Dickinson <dickinsm@gmail.com>
parents: 53162
diff changeset
326 self.assertEquals(format(123.456, '.4'), '123.5')
a3fcccee8957 Issue #5864: format(1234.5, '.4') gives misleading result
Mark Dickinson <dickinsm@gmail.com>
parents: 53162
diff changeset
327 self.assertEquals(format(1234.56, '.4'), '1.235e+03')
a3fcccee8957 Issue #5864: format(1234.5, '.4') gives misleading result
Mark Dickinson <dickinsm@gmail.com>
parents: 53162
diff changeset
328 self.assertEquals(format(12345.6, '.4'), '1.235e+04')
a3fcccee8957 Issue #5864: format(1234.5, '.4') gives misleading result
Mark Dickinson <dickinsm@gmail.com>
parents: 53162
diff changeset
329
44262
24749a380409 Backport of r59456:59458 from py3k to trunk
Christian Heimes <christian@cheimes.de>
parents: 42704
diff changeset
330 class ReprTestCase(unittest.TestCase):
24749a380409 Backport of r59456:59458 from py3k to trunk
Christian Heimes <christian@cheimes.de>
parents: 42704
diff changeset
331 def test_repr(self):
24749a380409 Backport of r59456:59458 from py3k to trunk
Christian Heimes <christian@cheimes.de>
parents: 42704
diff changeset
332 floats_file = open(os.path.join(os.path.split(__file__)[0],
24749a380409 Backport of r59456:59458 from py3k to trunk
Christian Heimes <christian@cheimes.de>
parents: 42704
diff changeset
333 'floating_points.txt'))
24749a380409 Backport of r59456:59458 from py3k to trunk
Christian Heimes <christian@cheimes.de>
parents: 42704
diff changeset
334 for line in floats_file:
24749a380409 Backport of r59456:59458 from py3k to trunk
Christian Heimes <christian@cheimes.de>
parents: 42704
diff changeset
335 line = line.strip()
24749a380409 Backport of r59456:59458 from py3k to trunk
Christian Heimes <christian@cheimes.de>
parents: 42704
diff changeset
336 if not line or line.startswith('#'):
24749a380409 Backport of r59456:59458 from py3k to trunk
Christian Heimes <christian@cheimes.de>
parents: 42704
diff changeset
337 continue
24749a380409 Backport of r59456:59458 from py3k to trunk
Christian Heimes <christian@cheimes.de>
parents: 42704
diff changeset
338 v = eval(line)
24749a380409 Backport of r59456:59458 from py3k to trunk
Christian Heimes <christian@cheimes.de>
parents: 42704
diff changeset
339 self.assertEqual(v, eval(repr(v)))
24749a380409 Backport of r59456:59458 from py3k to trunk
Christian Heimes <christian@cheimes.de>
parents: 42704
diff changeset
340 floats_file.close()
24749a380409 Backport of r59456:59458 from py3k to trunk
Christian Heimes <christian@cheimes.de>
parents: 42704
diff changeset
341
44344
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
342 # Beginning with Python 2.6 float has cross platform compatible
47889
4bf84035cb5f Issue 3188: accept float('infinity') as well as float('inf'). This
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
343 # ways to create and represent inf and nan
44344
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
344 class InfNanTest(unittest.TestCase):
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
345 def test_inf_from_str(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54000
diff changeset
346 self.assertTrue(isinf(float("inf")))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54000
diff changeset
347 self.assertTrue(isinf(float("+inf")))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54000
diff changeset
348 self.assertTrue(isinf(float("-inf")))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54000
diff changeset
349 self.assertTrue(isinf(float("infinity")))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54000
diff changeset
350 self.assertTrue(isinf(float("+infinity")))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54000
diff changeset
351 self.assertTrue(isinf(float("-infinity")))
44344
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
352
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
353 self.assertEqual(repr(float("inf")), "inf")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
354 self.assertEqual(repr(float("+inf")), "inf")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
355 self.assertEqual(repr(float("-inf")), "-inf")
47889
4bf84035cb5f Issue 3188: accept float('infinity') as well as float('inf'). This
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
356 self.assertEqual(repr(float("infinity")), "inf")
4bf84035cb5f Issue 3188: accept float('infinity') as well as float('inf'). This
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
357 self.assertEqual(repr(float("+infinity")), "inf")
4bf84035cb5f Issue 3188: accept float('infinity') as well as float('inf'). This
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
358 self.assertEqual(repr(float("-infinity")), "-inf")
44344
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
359
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
360 self.assertEqual(repr(float("INF")), "inf")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
361 self.assertEqual(repr(float("+Inf")), "inf")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
362 self.assertEqual(repr(float("-iNF")), "-inf")
47889
4bf84035cb5f Issue 3188: accept float('infinity') as well as float('inf'). This
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
363 self.assertEqual(repr(float("Infinity")), "inf")
4bf84035cb5f Issue 3188: accept float('infinity') as well as float('inf'). This
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
364 self.assertEqual(repr(float("+iNfInItY")), "inf")
4bf84035cb5f Issue 3188: accept float('infinity') as well as float('inf'). This
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
365 self.assertEqual(repr(float("-INFINITY")), "-inf")
44344
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
366
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
367 self.assertEqual(str(float("inf")), "inf")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
368 self.assertEqual(str(float("+inf")), "inf")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
369 self.assertEqual(str(float("-inf")), "-inf")
47889
4bf84035cb5f Issue 3188: accept float('infinity') as well as float('inf'). This
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
370 self.assertEqual(str(float("infinity")), "inf")
4bf84035cb5f Issue 3188: accept float('infinity') as well as float('inf'). This
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
371 self.assertEqual(str(float("+infinity")), "inf")
4bf84035cb5f Issue 3188: accept float('infinity') as well as float('inf'). This
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
372 self.assertEqual(str(float("-infinity")), "-inf")
44344
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
373
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
374 self.assertRaises(ValueError, float, "info")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
375 self.assertRaises(ValueError, float, "+info")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
376 self.assertRaises(ValueError, float, "-info")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
377 self.assertRaises(ValueError, float, "in")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
378 self.assertRaises(ValueError, float, "+in")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
379 self.assertRaises(ValueError, float, "-in")
47889
4bf84035cb5f Issue 3188: accept float('infinity') as well as float('inf'). This
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
380 self.assertRaises(ValueError, float, "infinit")
4bf84035cb5f Issue 3188: accept float('infinity') as well as float('inf'). This
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
381 self.assertRaises(ValueError, float, "+Infin")
4bf84035cb5f Issue 3188: accept float('infinity') as well as float('inf'). This
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
382 self.assertRaises(ValueError, float, "-INFI")
4bf84035cb5f Issue 3188: accept float('infinity') as well as float('inf'). This
Mark Dickinson <dickinsm@gmail.com>
parents: 46649
diff changeset
383 self.assertRaises(ValueError, float, "infinitys")
44344
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
384
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
385 def test_inf_as_str(self):
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
386 self.assertEqual(repr(1e300 * 1e300), "inf")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
387 self.assertEqual(repr(-1e300 * 1e300), "-inf")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
388
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
389 self.assertEqual(str(1e300 * 1e300), "inf")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
390 self.assertEqual(str(-1e300 * 1e300), "-inf")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
391
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
392 def test_nan_from_str(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54000
diff changeset
393 self.assertTrue(isnan(float("nan")))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54000
diff changeset
394 self.assertTrue(isnan(float("+nan")))
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54000
diff changeset
395 self.assertTrue(isnan(float("-nan")))
44344
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
396
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
397 self.assertEqual(repr(float("nan")), "nan")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
398 self.assertEqual(repr(float("+nan")), "nan")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
399 self.assertEqual(repr(float("-nan")), "nan")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
400
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
401 self.assertEqual(repr(float("NAN")), "nan")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
402 self.assertEqual(repr(float("+NAn")), "nan")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
403 self.assertEqual(repr(float("-NaN")), "nan")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
404
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
405 self.assertEqual(str(float("nan")), "nan")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
406 self.assertEqual(str(float("+nan")), "nan")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
407 self.assertEqual(str(float("-nan")), "nan")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
408
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
409 self.assertRaises(ValueError, float, "nana")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
410 self.assertRaises(ValueError, float, "+nana")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
411 self.assertRaises(ValueError, float, "-nana")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
412 self.assertRaises(ValueError, float, "na")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
413 self.assertRaises(ValueError, float, "+na")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
414 self.assertRaises(ValueError, float, "-na")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
415
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
416 def test_nan_as_str(self):
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
417 self.assertEqual(repr(1e300 * 1e300 * 0), "nan")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
418 self.assertEqual(repr(-1e300 * 1e300 * 0), "nan")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
419
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
420 self.assertEqual(str(1e300 * 1e300 * 0), "nan")
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
421 self.assertEqual(str(-1e300 * 1e300 * 0), "nan")
44262
24749a380409 Backport of r59456:59458 from py3k to trunk
Christian Heimes <christian@cheimes.de>
parents: 42704
diff changeset
422
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: 44482
diff changeset
423 def notest_float_nan(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54000
diff changeset
424 self.assertTrue(NAN.is_nan())
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54000
diff changeset
425 self.assertFalse(INF.is_nan())
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54000
diff changeset
426 self.assertFalse((0.).is_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: 44482
diff changeset
427
5bc7b20dc04a I finally got the time to update and merge Mark's and my trunk-math branch. The patch is collaborated work of Mark Dickinson and me. It was mostly done a few months ago. The patch fixes a lot of loose ends and edge cases related to operations with NaN, INF, very small values and complex math.
Christian Heimes <christian@cheimes.de>
parents: 44482
diff changeset
428 def notest_float_inf(self):
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54000
diff changeset
429 self.assertTrue(INF.is_inf())
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54000
diff changeset
430 self.assertFalse(NAN.is_inf())
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 54000
diff changeset
431 self.assertFalse((0.).is_inf())
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: 44482
diff changeset
432
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
433 fromHex = float.fromhex
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
434 toHex = float.hex
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
435 class HexFloatTestCase(unittest.TestCase):
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
436 MAX = fromHex('0x.fffffffffffff8p+1024') # max normal
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
437 MIN = fromHex('0x1p-1022') # min normal
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
438 TINY = fromHex('0x0.0000000000001p-1022') # min subnormal
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
439 EPS = fromHex('0x0.0000000000001p0') # diff between 1.0 and next float up
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
440
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
441 def identical(self, x, y):
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
442 # check that floats x and y are identical, or that both
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
443 # are NaNs
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
444 if isnan(x) or isnan(y):
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
445 if isnan(x) == isnan(y):
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
446 return
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
447 elif x == y and (x != 0.0 or copysign(1.0, x) == copysign(1.0, y)):
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
448 return
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
449 self.fail('%r not identical to %r' % (x, y))
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
450
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
451 def test_ends(self):
47970
9cb28ad5a41c Fix float.from_hex tests. It appears that Linux/ia64 doesn't like
Mark Dickinson <dickinsm@gmail.com>
parents: 47966
diff changeset
452 self.identical(self.MIN, ldexp(1.0, -1022))
9cb28ad5a41c Fix float.from_hex tests. It appears that Linux/ia64 doesn't like
Mark Dickinson <dickinsm@gmail.com>
parents: 47966
diff changeset
453 self.identical(self.TINY, ldexp(1.0, -1074))
9cb28ad5a41c Fix float.from_hex tests. It appears that Linux/ia64 doesn't like
Mark Dickinson <dickinsm@gmail.com>
parents: 47966
diff changeset
454 self.identical(self.EPS, ldexp(1.0, -52))
9cb28ad5a41c Fix float.from_hex tests. It appears that Linux/ia64 doesn't like
Mark Dickinson <dickinsm@gmail.com>
parents: 47966
diff changeset
455 self.identical(self.MAX, 2.*(ldexp(1.0, 1023) - ldexp(1.0, 970)))
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
456
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
457 def test_invalid_inputs(self):
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
458 invalid_inputs = [
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
459 'infi', # misspelt infinities and nans
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
460 '-Infinit',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
461 '++inf',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
462 '-+Inf',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
463 '--nan',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
464 '+-NaN',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
465 'snan',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
466 'NaNs',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
467 'nna',
54000
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
468 'an',
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
469 'nf',
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
470 'nfinity',
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
471 'inity',
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
472 'iinity',
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
473 '0xnan',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
474 '',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
475 ' ',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
476 'x1.0p0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
477 '0xX1.0p0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
478 '+ 0x1.0p0', # internal whitespace
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
479 '- 0x1.0p0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
480 '0 x1.0p0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
481 '0x 1.0p0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
482 '0x1 2.0p0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
483 '+0x1 .0p0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
484 '0x1. 0p0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
485 '-0x1.0 1p0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
486 '-0x1.0 p0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
487 '+0x1.0p +0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
488 '0x1.0p -0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
489 '0x1.0p 0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
490 '+0x1.0p+ 0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
491 '-0x1.0p- 0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
492 '++0x1.0p-0', # double signs
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
493 '--0x1.0p0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
494 '+-0x1.0p+0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
495 '-+0x1.0p0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
496 '0x1.0p++0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
497 '+0x1.0p+-0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
498 '-0x1.0p-+0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
499 '0x1.0p--0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
500 '0x1.0.p0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
501 '0x.p0', # no hex digits before or after point
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
502 '0x1,p0', # wrong decimal point character
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
503 '0x1pa',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
504 u'0x1p\uff10', # fullwidth Unicode digits
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
505 u'\uff10x1p0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
506 u'0x\uff11p0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
507 u'0x1.\uff10p0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
508 '0x1p0 \n 0x2p0',
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
509 '0x1p0\0 0x1p0', # embedded null byte is not end of string
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
510 ]
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
511 for x in invalid_inputs:
48816
57a8735647cf Fix float.fromhex test to give additional information on failure. This
Mark Dickinson <dickinsm@gmail.com>
parents: 47970
diff changeset
512 try:
57a8735647cf Fix float.fromhex test to give additional information on failure. This
Mark Dickinson <dickinsm@gmail.com>
parents: 47970
diff changeset
513 result = fromHex(x)
57a8735647cf Fix float.fromhex test to give additional information on failure. This
Mark Dickinson <dickinsm@gmail.com>
parents: 47970
diff changeset
514 except ValueError:
57a8735647cf Fix float.fromhex test to give additional information on failure. This
Mark Dickinson <dickinsm@gmail.com>
parents: 47970
diff changeset
515 pass
57a8735647cf Fix float.fromhex test to give additional information on failure. This
Mark Dickinson <dickinsm@gmail.com>
parents: 47970
diff changeset
516 else:
57a8735647cf Fix float.fromhex test to give additional information on failure. This
Mark Dickinson <dickinsm@gmail.com>
parents: 47970
diff changeset
517 self.fail('Expected float.fromhex(%r) to raise ValueError; '
57a8735647cf Fix float.fromhex test to give additional information on failure. This
Mark Dickinson <dickinsm@gmail.com>
parents: 47970
diff changeset
518 'got %r instead' % (x, result))
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
519
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
520
54000
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
521 def test_whitespace(self):
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
522 value_pairs = [
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
523 ('inf', INF),
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
524 ('-Infinity', -INF),
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
525 ('nan', NAN),
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
526 ('1.0', 1.0),
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
527 ('-0x.2', -0.125),
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
528 ('-0.0', -0.0)
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
529 ]
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
530 whitespace = [
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
531 '',
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
532 ' ',
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
533 '\t',
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
534 '\n',
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
535 '\n \t',
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
536 '\f',
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
537 '\v',
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
538 '\r'
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
539 ]
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
540 for inp, expected in value_pairs:
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
541 for lead in whitespace:
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
542 for trail in whitespace:
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
543 got = fromHex(lead + inp + trail)
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
544 self.identical(got, expected)
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
545
119f4232af46 Issue #5981: Fix some float.fromhex bugs related to inf and nan handling.
Mark Dickinson <dickinsm@gmail.com>
parents: 53802
diff changeset
546
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
547 def test_from_hex(self):
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
548 MIN = self.MIN;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
549 MAX = self.MAX;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
550 TINY = self.TINY;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
551 EPS = self.EPS;
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
552
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
553 # two spellings of infinity, with optional signs; case-insensitive
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
554 self.identical(fromHex('inf'), INF)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
555 self.identical(fromHex('+Inf'), INF)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
556 self.identical(fromHex('-INF'), -INF)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
557 self.identical(fromHex('iNf'), INF)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
558 self.identical(fromHex('Infinity'), INF)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
559 self.identical(fromHex('+INFINITY'), INF)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
560 self.identical(fromHex('-infinity'), -INF)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
561 self.identical(fromHex('-iNFiNitY'), -INF)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
562
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
563 # nans with optional sign; case insensitive
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
564 self.identical(fromHex('nan'), NAN)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
565 self.identical(fromHex('+NaN'), NAN)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
566 self.identical(fromHex('-NaN'), NAN)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
567 self.identical(fromHex('-nAN'), NAN)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
568
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
569 # variations in input format
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
570 self.identical(fromHex('1'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
571 self.identical(fromHex('+1'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
572 self.identical(fromHex('1.'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
573 self.identical(fromHex('1.0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
574 self.identical(fromHex('1.0p0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
575 self.identical(fromHex('01'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
576 self.identical(fromHex('01.'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
577 self.identical(fromHex('0x1'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
578 self.identical(fromHex('0x1.'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
579 self.identical(fromHex('0x1.0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
580 self.identical(fromHex('+0x1.0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
581 self.identical(fromHex('0x1p0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
582 self.identical(fromHex('0X1p0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
583 self.identical(fromHex('0X1P0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
584 self.identical(fromHex('0x1P0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
585 self.identical(fromHex('0x1.p0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
586 self.identical(fromHex('0x1.0p0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
587 self.identical(fromHex('0x.1p4'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
588 self.identical(fromHex('0x.1p04'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
589 self.identical(fromHex('0x.1p004'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
590 self.identical(fromHex('0x1p+0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
591 self.identical(fromHex('0x1P-0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
592 self.identical(fromHex('+0x1p0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
593 self.identical(fromHex('0x01p0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
594 self.identical(fromHex('0x1p00'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
595 self.identical(fromHex(u'0x1p0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
596 self.identical(fromHex(' 0x1p0 '), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
597 self.identical(fromHex('\n 0x1p0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
598 self.identical(fromHex('0x1p0 \t'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
599 self.identical(fromHex('0xap0'), 10.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
600 self.identical(fromHex('0xAp0'), 10.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
601 self.identical(fromHex('0xaP0'), 10.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
602 self.identical(fromHex('0xAP0'), 10.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
603 self.identical(fromHex('0xbep0'), 190.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
604 self.identical(fromHex('0xBep0'), 190.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
605 self.identical(fromHex('0xbEp0'), 190.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
606 self.identical(fromHex('0XBE0P-4'), 190.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
607 self.identical(fromHex('0xBEp0'), 190.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
608 self.identical(fromHex('0xB.Ep4'), 190.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
609 self.identical(fromHex('0x.BEp8'), 190.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
610 self.identical(fromHex('0x.0BEp12'), 190.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
611
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
612 # moving the point around
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
613 pi = fromHex('0x1.921fb54442d18p1')
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
614 self.identical(fromHex('0x.006487ed5110b46p11'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
615 self.identical(fromHex('0x.00c90fdaa22168cp10'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
616 self.identical(fromHex('0x.01921fb54442d18p9'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
617 self.identical(fromHex('0x.03243f6a8885a3p8'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
618 self.identical(fromHex('0x.06487ed5110b46p7'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
619 self.identical(fromHex('0x.0c90fdaa22168cp6'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
620 self.identical(fromHex('0x.1921fb54442d18p5'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
621 self.identical(fromHex('0x.3243f6a8885a3p4'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
622 self.identical(fromHex('0x.6487ed5110b46p3'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
623 self.identical(fromHex('0x.c90fdaa22168cp2'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
624 self.identical(fromHex('0x1.921fb54442d18p1'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
625 self.identical(fromHex('0x3.243f6a8885a3p0'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
626 self.identical(fromHex('0x6.487ed5110b46p-1'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
627 self.identical(fromHex('0xc.90fdaa22168cp-2'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
628 self.identical(fromHex('0x19.21fb54442d18p-3'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
629 self.identical(fromHex('0x32.43f6a8885a3p-4'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
630 self.identical(fromHex('0x64.87ed5110b46p-5'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
631 self.identical(fromHex('0xc9.0fdaa22168cp-6'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
632 self.identical(fromHex('0x192.1fb54442d18p-7'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
633 self.identical(fromHex('0x324.3f6a8885a3p-8'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
634 self.identical(fromHex('0x648.7ed5110b46p-9'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
635 self.identical(fromHex('0xc90.fdaa22168cp-10'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
636 self.identical(fromHex('0x1921.fb54442d18p-11'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
637 # ...
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
638 self.identical(fromHex('0x1921fb54442d1.8p-47'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
639 self.identical(fromHex('0x3243f6a8885a3p-48'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
640 self.identical(fromHex('0x6487ed5110b46p-49'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
641 self.identical(fromHex('0xc90fdaa22168cp-50'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
642 self.identical(fromHex('0x1921fb54442d18p-51'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
643 self.identical(fromHex('0x3243f6a8885a30p-52'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
644 self.identical(fromHex('0x6487ed5110b460p-53'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
645 self.identical(fromHex('0xc90fdaa22168c0p-54'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
646 self.identical(fromHex('0x1921fb54442d180p-55'), pi)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
647
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
648
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
649 # results that should overflow...
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
650 self.assertRaises(OverflowError, fromHex, '-0x1p1024')
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
651 self.assertRaises(OverflowError, fromHex, '0x1p+1025')
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
652 self.assertRaises(OverflowError, fromHex, '+0X1p1030')
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
653 self.assertRaises(OverflowError, fromHex, '-0x1p+1100')
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
654 self.assertRaises(OverflowError, fromHex, '0X1p123456789123456789')
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
655 self.assertRaises(OverflowError, fromHex, '+0X.8p+1025')
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
656 self.assertRaises(OverflowError, fromHex, '+0x0.8p1025')
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
657 self.assertRaises(OverflowError, fromHex, '-0x0.4p1026')
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
658 self.assertRaises(OverflowError, fromHex, '0X2p+1023')
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
659 self.assertRaises(OverflowError, fromHex, '0x2.p1023')
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
660 self.assertRaises(OverflowError, fromHex, '-0x2.0p+1023')
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
661 self.assertRaises(OverflowError, fromHex, '+0X4p+1022')
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
662 self.assertRaises(OverflowError, fromHex, '0x1.ffffffffffffffp+1023')
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
663 self.assertRaises(OverflowError, fromHex, '-0X1.fffffffffffff9p1023')
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
664 self.assertRaises(OverflowError, fromHex, '0X1.fffffffffffff8p1023')
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
665 self.assertRaises(OverflowError, fromHex, '+0x3.fffffffffffffp1022')
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
666 self.assertRaises(OverflowError, fromHex, '0x3fffffffffffffp+970')
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
667 self.assertRaises(OverflowError, fromHex, '0x10000000000000000p960')
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
668 self.assertRaises(OverflowError, fromHex, '-0Xffffffffffffffffp960')
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
669
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
670 # ...and those that round to +-max float
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
671 self.identical(fromHex('+0x1.fffffffffffffp+1023'), MAX)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
672 self.identical(fromHex('-0X1.fffffffffffff7p1023'), -MAX)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
673 self.identical(fromHex('0X1.fffffffffffff7fffffffffffffp1023'), MAX)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
674
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
675 # zeros
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
676 self.identical(fromHex('0x0p0'), 0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
677 self.identical(fromHex('0x0p1000'), 0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
678 self.identical(fromHex('-0x0p1023'), -0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
679 self.identical(fromHex('0X0p1024'), 0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
680 self.identical(fromHex('-0x0p1025'), -0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
681 self.identical(fromHex('0X0p2000'), 0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
682 self.identical(fromHex('0x0p123456789123456789'), 0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
683 self.identical(fromHex('-0X0p-0'), -0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
684 self.identical(fromHex('-0X0p-1000'), -0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
685 self.identical(fromHex('0x0p-1023'), 0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
686 self.identical(fromHex('-0X0p-1024'), -0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
687 self.identical(fromHex('-0x0p-1025'), -0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
688 self.identical(fromHex('-0x0p-1072'), -0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
689 self.identical(fromHex('0X0p-1073'), 0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
690 self.identical(fromHex('-0x0p-1074'), -0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
691 self.identical(fromHex('0x0p-1075'), 0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
692 self.identical(fromHex('0X0p-1076'), 0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
693 self.identical(fromHex('-0X0p-2000'), -0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
694 self.identical(fromHex('-0x0p-123456789123456789'), -0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
695
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
696 # values that should underflow to 0
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
697 self.identical(fromHex('0X1p-1075'), 0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
698 self.identical(fromHex('-0X1p-1075'), -0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
699 self.identical(fromHex('-0x1p-123456789123456789'), -0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
700 self.identical(fromHex('0x1.00000000000000001p-1075'), TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
701 self.identical(fromHex('-0x1.1p-1075'), -TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
702 self.identical(fromHex('0x1.fffffffffffffffffp-1075'), TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
703
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
704 # check round-half-even is working correctly near 0 ...
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
705 self.identical(fromHex('0x1p-1076'), 0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
706 self.identical(fromHex('0X2p-1076'), 0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
707 self.identical(fromHex('0X3p-1076'), TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
708 self.identical(fromHex('0x4p-1076'), TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
709 self.identical(fromHex('0X5p-1076'), TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
710 self.identical(fromHex('0X6p-1076'), 2*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
711 self.identical(fromHex('0x7p-1076'), 2*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
712 self.identical(fromHex('0X8p-1076'), 2*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
713 self.identical(fromHex('0X9p-1076'), 2*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
714 self.identical(fromHex('0xap-1076'), 2*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
715 self.identical(fromHex('0Xbp-1076'), 3*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
716 self.identical(fromHex('0xcp-1076'), 3*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
717 self.identical(fromHex('0Xdp-1076'), 3*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
718 self.identical(fromHex('0Xep-1076'), 4*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
719 self.identical(fromHex('0xfp-1076'), 4*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
720 self.identical(fromHex('0x10p-1076'), 4*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
721 self.identical(fromHex('-0x1p-1076'), -0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
722 self.identical(fromHex('-0X2p-1076'), -0.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
723 self.identical(fromHex('-0x3p-1076'), -TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
724 self.identical(fromHex('-0X4p-1076'), -TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
725 self.identical(fromHex('-0x5p-1076'), -TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
726 self.identical(fromHex('-0x6p-1076'), -2*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
727 self.identical(fromHex('-0X7p-1076'), -2*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
728 self.identical(fromHex('-0X8p-1076'), -2*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
729 self.identical(fromHex('-0X9p-1076'), -2*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
730 self.identical(fromHex('-0Xap-1076'), -2*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
731 self.identical(fromHex('-0xbp-1076'), -3*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
732 self.identical(fromHex('-0xcp-1076'), -3*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
733 self.identical(fromHex('-0Xdp-1076'), -3*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
734 self.identical(fromHex('-0xep-1076'), -4*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
735 self.identical(fromHex('-0Xfp-1076'), -4*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
736 self.identical(fromHex('-0X10p-1076'), -4*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
737
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
738 # ... and near MIN ...
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
739 self.identical(fromHex('0x0.ffffffffffffd6p-1022'), MIN-3*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
740 self.identical(fromHex('0x0.ffffffffffffd8p-1022'), MIN-2*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
741 self.identical(fromHex('0x0.ffffffffffffdap-1022'), MIN-2*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
742 self.identical(fromHex('0x0.ffffffffffffdcp-1022'), MIN-2*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
743 self.identical(fromHex('0x0.ffffffffffffdep-1022'), MIN-2*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
744 self.identical(fromHex('0x0.ffffffffffffe0p-1022'), MIN-2*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
745 self.identical(fromHex('0x0.ffffffffffffe2p-1022'), MIN-2*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
746 self.identical(fromHex('0x0.ffffffffffffe4p-1022'), MIN-2*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
747 self.identical(fromHex('0x0.ffffffffffffe6p-1022'), MIN-2*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
748 self.identical(fromHex('0x0.ffffffffffffe8p-1022'), MIN-2*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
749 self.identical(fromHex('0x0.ffffffffffffeap-1022'), MIN-TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
750 self.identical(fromHex('0x0.ffffffffffffecp-1022'), MIN-TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
751 self.identical(fromHex('0x0.ffffffffffffeep-1022'), MIN-TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
752 self.identical(fromHex('0x0.fffffffffffff0p-1022'), MIN-TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
753 self.identical(fromHex('0x0.fffffffffffff2p-1022'), MIN-TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
754 self.identical(fromHex('0x0.fffffffffffff4p-1022'), MIN-TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
755 self.identical(fromHex('0x0.fffffffffffff6p-1022'), MIN-TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
756 self.identical(fromHex('0x0.fffffffffffff8p-1022'), MIN)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
757 self.identical(fromHex('0x0.fffffffffffffap-1022'), MIN)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
758 self.identical(fromHex('0x0.fffffffffffffcp-1022'), MIN)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
759 self.identical(fromHex('0x0.fffffffffffffep-1022'), MIN)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
760 self.identical(fromHex('0x1.00000000000000p-1022'), MIN)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
761 self.identical(fromHex('0x1.00000000000002p-1022'), MIN)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
762 self.identical(fromHex('0x1.00000000000004p-1022'), MIN)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
763 self.identical(fromHex('0x1.00000000000006p-1022'), MIN)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
764 self.identical(fromHex('0x1.00000000000008p-1022'), MIN)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
765 self.identical(fromHex('0x1.0000000000000ap-1022'), MIN+TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
766 self.identical(fromHex('0x1.0000000000000cp-1022'), MIN+TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
767 self.identical(fromHex('0x1.0000000000000ep-1022'), MIN+TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
768 self.identical(fromHex('0x1.00000000000010p-1022'), MIN+TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
769 self.identical(fromHex('0x1.00000000000012p-1022'), MIN+TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
770 self.identical(fromHex('0x1.00000000000014p-1022'), MIN+TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
771 self.identical(fromHex('0x1.00000000000016p-1022'), MIN+TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
772 self.identical(fromHex('0x1.00000000000018p-1022'), MIN+2*TINY)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
773
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
774 # ... and near 1.0.
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
775 self.identical(fromHex('0x0.fffffffffffff0p0'), 1.0-EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
776 self.identical(fromHex('0x0.fffffffffffff1p0'), 1.0-EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
777 self.identical(fromHex('0X0.fffffffffffff2p0'), 1.0-EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
778 self.identical(fromHex('0x0.fffffffffffff3p0'), 1.0-EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
779 self.identical(fromHex('0X0.fffffffffffff4p0'), 1.0-EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
780 self.identical(fromHex('0X0.fffffffffffff5p0'), 1.0-EPS/2)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
781 self.identical(fromHex('0X0.fffffffffffff6p0'), 1.0-EPS/2)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
782 self.identical(fromHex('0x0.fffffffffffff7p0'), 1.0-EPS/2)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
783 self.identical(fromHex('0x0.fffffffffffff8p0'), 1.0-EPS/2)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
784 self.identical(fromHex('0X0.fffffffffffff9p0'), 1.0-EPS/2)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
785 self.identical(fromHex('0X0.fffffffffffffap0'), 1.0-EPS/2)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
786 self.identical(fromHex('0x0.fffffffffffffbp0'), 1.0-EPS/2)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
787 self.identical(fromHex('0X0.fffffffffffffcp0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
788 self.identical(fromHex('0x0.fffffffffffffdp0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
789 self.identical(fromHex('0X0.fffffffffffffep0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
790 self.identical(fromHex('0x0.ffffffffffffffp0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
791 self.identical(fromHex('0X1.00000000000000p0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
792 self.identical(fromHex('0X1.00000000000001p0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
793 self.identical(fromHex('0x1.00000000000002p0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
794 self.identical(fromHex('0X1.00000000000003p0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
795 self.identical(fromHex('0x1.00000000000004p0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
796 self.identical(fromHex('0X1.00000000000005p0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
797 self.identical(fromHex('0X1.00000000000006p0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
798 self.identical(fromHex('0X1.00000000000007p0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
799 self.identical(fromHex('0x1.00000000000007ffffffffffffffffffffp0'),
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
800 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
801 self.identical(fromHex('0x1.00000000000008p0'), 1.0)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
802 self.identical(fromHex('0x1.00000000000008000000000000000001p0'),
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
803 1+EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
804 self.identical(fromHex('0X1.00000000000009p0'), 1.0+EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
805 self.identical(fromHex('0x1.0000000000000ap0'), 1.0+EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
806 self.identical(fromHex('0x1.0000000000000bp0'), 1.0+EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
807 self.identical(fromHex('0X1.0000000000000cp0'), 1.0+EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
808 self.identical(fromHex('0x1.0000000000000dp0'), 1.0+EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
809 self.identical(fromHex('0x1.0000000000000ep0'), 1.0+EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
810 self.identical(fromHex('0X1.0000000000000fp0'), 1.0+EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
811 self.identical(fromHex('0x1.00000000000010p0'), 1.0+EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
812 self.identical(fromHex('0X1.00000000000011p0'), 1.0+EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
813 self.identical(fromHex('0x1.00000000000012p0'), 1.0+EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
814 self.identical(fromHex('0X1.00000000000013p0'), 1.0+EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
815 self.identical(fromHex('0X1.00000000000014p0'), 1.0+EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
816 self.identical(fromHex('0x1.00000000000015p0'), 1.0+EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
817 self.identical(fromHex('0x1.00000000000016p0'), 1.0+EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
818 self.identical(fromHex('0X1.00000000000017p0'), 1.0+EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
819 self.identical(fromHex('0x1.00000000000017ffffffffffffffffffffp0'),
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
820 1.0+EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
821 self.identical(fromHex('0x1.00000000000018p0'), 1.0+2*EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
822 self.identical(fromHex('0X1.00000000000018000000000000000001p0'),
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
823 1.0+2*EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
824 self.identical(fromHex('0x1.00000000000019p0'), 1.0+2*EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
825 self.identical(fromHex('0X1.0000000000001ap0'), 1.0+2*EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
826 self.identical(fromHex('0X1.0000000000001bp0'), 1.0+2*EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
827 self.identical(fromHex('0x1.0000000000001cp0'), 1.0+2*EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
828 self.identical(fromHex('0x1.0000000000001dp0'), 1.0+2*EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
829 self.identical(fromHex('0x1.0000000000001ep0'), 1.0+2*EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
830 self.identical(fromHex('0X1.0000000000001fp0'), 1.0+2*EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
831 self.identical(fromHex('0x1.00000000000020p0'), 1.0+2*EPS)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
832
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
833 def test_roundtrip(self):
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
834 def roundtrip(x):
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
835 return fromHex(toHex(x))
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
836
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
837 for x in [NAN, INF, self.MAX, self.MIN, self.MIN-self.TINY, self.TINY, 0.0]:
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
838 self.identical(x, roundtrip(x))
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
839 self.identical(-x, roundtrip(-x))
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
840
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
841 # fromHex(toHex(x)) should exactly recover x, for any non-NaN float x.
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
842 import random
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
843 for i in xrange(10000):
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
844 e = random.randrange(-1200, 1200)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
845 m = random.random()
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
846 s = random.choice([1.0, -1.0])
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
847 try:
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
848 x = s*ldexp(m, e)
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
849 except OverflowError:
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
850 pass
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
851 else:
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
852 self.identical(x, fromHex(toHex(x)))
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
853
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: 44482
diff changeset
854
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
855 def test_main():
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
856 test_support.run_unittest(
49054
e27cf7ee2968 #3796: A test class was not run in test_float.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 48816
diff changeset
857 GeneralFloatCases,
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
858 FormatFunctionsTestCase,
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
859 UnknownFormatTestCase,
44262
24749a380409 Backport of r59456:59458 from py3k to trunk
Christian Heimes <christian@cheimes.de>
parents: 42704
diff changeset
860 IEEEFormatTestCase,
44344
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
861 ReprTestCase,
1d69aabf6f7f Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
Christian Heimes <christian@cheimes.de>
parents: 44267
diff changeset
862 InfNanTest,
47966
97ba936e8df8 Issue #3008: add instance method float.hex and class method float.fromhex
Mark Dickinson <dickinsm@gmail.com>
parents: 47889
diff changeset
863 HexFloatTestCase,
44267
f909872917b9 The new float repr causes too much trouble and pain. I'm disabling the feature until we have sorted out the issues on all machines. 64bit machines seem to have issues and Guido has reported even worse.
Christian Heimes <christian@cheimes.de>
parents: 44262
diff changeset
864 )
34972
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
865
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
866 if __name__ == '__main__':
1dd8a0d64432 This is my patch:
Michael W. Hudson <mwh@python.net>
parents:
diff changeset
867 test_main()