1
2import unittest, struct
3import os
4from test import test_support
5import math
6from math import isinf, isnan, copysign, ldexp
7import operator
8import random
9import fractions
10import sys
11import time
12
13INF = float("inf")
14NAN = float("nan")
15
16have_getformat = hasattr(float, "__getformat__")
17requires_getformat = unittest.skipUnless(have_getformat,
18                                         "requires __getformat__")
19requires_setformat = unittest.skipUnless(hasattr(float, "__setformat__"),
20                                         "requires __setformat__")
21# decorator for skipping tests on non-IEEE 754 platforms
22requires_IEEE_754 = unittest.skipUnless(have_getformat and
23    float.__getformat__("double").startswith("IEEE"),
24    "test requires IEEE 754 doubles")
25
26#locate file with float format test values
27test_dir = os.path.dirname(__file__) or os.curdir
28format_testfile = os.path.join(test_dir, 'formatfloat_testcases.txt')
29
30class FloatSubclass(float):
31    pass
32
33class OtherFloatSubclass(float):
34    pass
35
36class GeneralFloatCases(unittest.TestCase):
37
38    def test_float(self):
39        self.assertEqual(float(3.14), 3.14)
40        self.assertEqual(float(314), 314.0)
41        self.assertEqual(float(314L), 314.0)
42        self.assertEqual(float("  3.14  "), 3.14)
43        self.assertRaises(ValueError, float, "  0x3.1  ")
44        self.assertRaises(ValueError, float, "  -0x3.p-1  ")
45        self.assertRaises(ValueError, float, "  +0x3.p-1  ")
46        self.assertRaises(ValueError, float, "++3.14")
47        self.assertRaises(ValueError, float, "+-3.14")
48        self.assertRaises(ValueError, float, "-+3.14")
49        self.assertRaises(ValueError, float, "--3.14")
50        # check that we don't accept alternate exponent markers
51        self.assertRaises(ValueError, float, "-1.7d29")
52        self.assertRaises(ValueError, float, "3D-14")
53        if test_support.have_unicode:
54            self.assertEqual(float(unicode("  3.14  ")), 3.14)
55            self.assertEqual(float(unicode("  \u0663.\u0661\u0664  ",'raw-unicode-escape')), 3.14)
56
57        # extra long strings should no longer be a problem
58        # (in 2.6, long unicode inputs to float raised ValueError)
59        float('.' + '1'*1000)
60        float(unicode('.' + '1'*1000))
61
62    def test_non_numeric_input_types(self):
63        # Test possible non-numeric types for the argument x, including
64        # subclasses of the explicitly documented accepted types.
65        class CustomStr(str): pass
66        class CustomByteArray(bytearray): pass
67        factories = [str, bytearray, CustomStr, CustomByteArray, buffer]
68
69        if test_support.have_unicode:
70            class CustomUnicode(unicode): pass
71            factories += [unicode, CustomUnicode]
72
73        for f in factories:
74            with test_support.check_py3k_warnings(quiet=True):
75                x = f(" 3.14  ")
76            msg = 'x has value %s and type %s' % (x, type(x).__name__)
77            try:
78                self.assertEqual(float(x), 3.14, msg=msg)
79            except TypeError, err:
80                raise AssertionError('For %s got TypeError: %s' %
81                                     (type(x).__name__, err))
82            errmsg = "could not convert"
83            with self.assertRaisesRegexp(ValueError, errmsg, msg=msg), \
84                 test_support.check_py3k_warnings(quiet=True):
85                float(f('A' * 0x10))
86
87    def test_float_buffer(self):
88        with test_support.check_py3k_warnings():
89            self.assertEqual(float(buffer('12.3', 1, 3)), 2.3)
90            self.assertEqual(float(buffer('12.3\x00', 1, 3)), 2.3)
91            self.assertEqual(float(buffer('12.3 ', 1, 3)), 2.3)
92            self.assertEqual(float(buffer('12.3A', 1, 3)), 2.3)
93            self.assertEqual(float(buffer('12.34', 1, 3)), 2.3)
94
95    def check_conversion_to_int(self, x):
96        """Check that int(x) has the correct value and type, for a float x."""
97        n = int(x)
98        if x >= 0.0:
99            # x >= 0 and n = int(x)  ==>  n <= x < n + 1
100            self.assertLessEqual(n, x)
101            self.assertLess(x, n + 1)
102        else:
103            # x < 0 and n = int(x)  ==>  n >= x > n - 1
104            self.assertGreaterEqual(n, x)
105            self.assertGreater(x, n - 1)
106
107        # Result should be an int if within range, else a long.
108        if -sys.maxint-1 <= n <= sys.maxint:
109            self.assertEqual(type(n), int)
110        else:
111            self.assertEqual(type(n), long)
112
113        # Double check.
114        self.assertEqual(type(int(n)), type(n))
115
116    def test_conversion_to_int(self):
117        # Check that floats within the range of an int convert to type
118        # int, not long.  (issue #11144.)
119        boundary = float(sys.maxint + 1)
120        epsilon = 2**-sys.float_info.mant_dig * boundary
121
122        # These 2 floats are either side of the positive int/long boundary on
123        # both 32-bit and 64-bit systems.
124        self.check_conversion_to_int(boundary - epsilon)
125        self.check_conversion_to_int(boundary)
126
127        # These floats are either side of the negative long/int boundary on
128        # 64-bit systems...
129        self.check_conversion_to_int(-boundary - 2*epsilon)
130        self.check_conversion_to_int(-boundary)
131
132        # ... and these ones are either side of the negative long/int
133        # boundary on 32-bit systems.
134        self.check_conversion_to_int(-boundary - 1.0)
135        self.check_conversion_to_int(-boundary - 1.0 + 2*epsilon)
136
137    @test_support.run_with_locale('LC_NUMERIC', 'fr_FR', 'de_DE')
138    def test_float_with_comma(self):
139        # set locale to something that doesn't use '.' for the decimal point
140        # float must not accept the locale specific decimal point but
141        # it still has to accept the normal python syntax
142        import locale
143        if not locale.localeconv()['decimal_point'] == ',':
144            self.skipTest('decimal_point is not ","')
145
146        self.assertEqual(float("  3.14  "), 3.14)
147        self.assertEqual(float("+3.14  "), 3.14)
148        self.assertEqual(float("-3.14  "), -3.14)
149        self.assertEqual(float(".14  "), .14)
150        self.assertEqual(float("3.  "), 3.0)
151        self.assertEqual(float("3.e3  "), 3000.0)
152        self.assertEqual(float("3.2e3  "), 3200.0)
153        self.assertEqual(float("2.5e-1  "), 0.25)
154        self.assertEqual(float("5e-1"), 0.5)
155        self.assertRaises(ValueError, float, "  3,14  ")
156        self.assertRaises(ValueError, float, "  +3,14  ")
157        self.assertRaises(ValueError, float, "  -3,14  ")
158        self.assertRaises(ValueError, float, "  0x3.1  ")
159        self.assertRaises(ValueError, float, "  -0x3.p-1  ")
160        self.assertRaises(ValueError, float, "  +0x3.p-1  ")
161        self.assertEqual(float("  25.e-1  "), 2.5)
162        self.assertEqual(test_support.fcmp(float("  .25e-1  "), .025), 0)
163
164    def test_floatconversion(self):
165        # Make sure that calls to __float__() work properly
166        class Foo0:
167            def __float__(self):
168                return 42.
169
170        class Foo1(object):
171            def __float__(self):
172                return 42.
173
174        class Foo2(float):
175            def __float__(self):
176                return 42.
177
178        class Foo3(float):
179            def __new__(cls, value=0.):
180                return float.__new__(cls, 2*value)
181
182            def __float__(self):
183                return self
184
185        class Foo4(float):
186            def __float__(self):
187                return 42
188
189        # Issue 5759: __float__ not called on str subclasses (though it is on
190        # unicode subclasses).
191        class FooStr(str):
192            def __float__(self):
193                return float(str(self)) + 1
194
195        class FooUnicode(unicode):
196            def __float__(self):
197                return float(unicode(self)) + 1
198
199        self.assertAlmostEqual(float(Foo0()), 42.)
200        self.assertAlmostEqual(float(Foo1()), 42.)
201        self.assertAlmostEqual(float(Foo2()), 42.)
202        self.assertAlmostEqual(float(Foo3(21)), 42.)
203        self.assertRaises(TypeError, float, Foo4(42))
204        self.assertAlmostEqual(float(FooUnicode('8')), 9.)
205        self.assertAlmostEqual(float(FooStr('8')), 9.)
206
207        class Foo5:
208            def __float__(self):
209                return ""
210        self.assertRaises(TypeError, time.sleep, Foo5())
211
212        # Issue #24731
213        class F:
214            def __float__(self):
215                return OtherFloatSubclass(42.)
216        self.assertAlmostEqual(float(F()), 42.)
217        self.assertIs(type(float(F())), OtherFloatSubclass)
218        self.assertAlmostEqual(FloatSubclass(F()), 42.)
219        self.assertIs(type(FloatSubclass(F())), FloatSubclass)
220
221    def test_is_integer(self):
222        self.assertFalse((1.1).is_integer())
223        self.assertTrue((1.).is_integer())
224        self.assertFalse(float("nan").is_integer())
225        self.assertFalse(float("inf").is_integer())
226
227    def test_floatasratio(self):
228        for f, ratio in [
229                (0.875, (7, 8)),
230                (-0.875, (-7, 8)),
231                (0.0, (0, 1)),
232                (11.5, (23, 2)),
233            ]:
234            self.assertEqual(f.as_integer_ratio(), ratio)
235
236        for i in range(10000):
237            f = random.random()
238            f *= 10 ** random.randint(-100, 100)
239            n, d = f.as_integer_ratio()
240            self.assertEqual(float(n).__truediv__(d), f)
241
242        R = fractions.Fraction
243        self.assertEqual(R(0, 1),
244                         R(*float(0.0).as_integer_ratio()))
245        self.assertEqual(R(5, 2),
246                         R(*float(2.5).as_integer_ratio()))
247        self.assertEqual(R(1, 2),
248                         R(*float(0.5).as_integer_ratio()))
249        self.assertEqual(R(4728779608739021, 2251799813685248),
250                         R(*float(2.1).as_integer_ratio()))
251        self.assertEqual(R(-4728779608739021, 2251799813685248),
252                         R(*float(-2.1).as_integer_ratio()))
253        self.assertEqual(R(-2100, 1),
254                         R(*float(-2100.0).as_integer_ratio()))
255
256        self.assertRaises(OverflowError, float('inf').as_integer_ratio)
257        self.assertRaises(OverflowError, float('-inf').as_integer_ratio)
258        self.assertRaises(ValueError, float('nan').as_integer_ratio)
259
260    def assertEqualAndEqualSign(self, a, b):
261        # fail unless a == b and a and b have the same sign bit;
262        # the only difference from assertEqual is that this test
263        # distinguishes -0.0 and 0.0.
264        self.assertEqual((a, copysign(1.0, a)), (b, copysign(1.0, b)))
265
266    @requires_IEEE_754
267    def test_float_mod(self):
268        # Check behaviour of % operator for IEEE 754 special cases.
269        # In particular, check signs of zeros.
270        mod = operator.mod
271
272        self.assertEqualAndEqualSign(mod(-1.0, 1.0), 0.0)
273        self.assertEqualAndEqualSign(mod(-1e-100, 1.0), 1.0)
274        self.assertEqualAndEqualSign(mod(-0.0, 1.0), 0.0)
275        self.assertEqualAndEqualSign(mod(0.0, 1.0), 0.0)
276        self.assertEqualAndEqualSign(mod(1e-100, 1.0), 1e-100)
277        self.assertEqualAndEqualSign(mod(1.0, 1.0), 0.0)
278
279        self.assertEqualAndEqualSign(mod(-1.0, -1.0), -0.0)
280        self.assertEqualAndEqualSign(mod(-1e-100, -1.0), -1e-100)
281        self.assertEqualAndEqualSign(mod(-0.0, -1.0), -0.0)
282        self.assertEqualAndEqualSign(mod(0.0, -1.0), -0.0)
283        self.assertEqualAndEqualSign(mod(1e-100, -1.0), -1.0)
284        self.assertEqualAndEqualSign(mod(1.0, -1.0), -0.0)
285
286    @requires_IEEE_754
287    def test_float_pow(self):
288        # test builtin pow and ** operator for IEEE 754 special cases.
289        # Special cases taken from section F.9.4.4 of the C99 specification
290
291        for pow_op in pow, operator.pow:
292            # x**NAN is NAN for any x except 1
293            self.assertTrue(isnan(pow_op(-INF, NAN)))
294            self.assertTrue(isnan(pow_op(-2.0, NAN)))
295            self.assertTrue(isnan(pow_op(-1.0, NAN)))
296            self.assertTrue(isnan(pow_op(-0.5, NAN)))
297            self.assertTrue(isnan(pow_op(-0.0, NAN)))
298            self.assertTrue(isnan(pow_op(0.0, NAN)))
299            self.assertTrue(isnan(pow_op(0.5, NAN)))
300            self.assertTrue(isnan(pow_op(2.0, NAN)))
301            self.assertTrue(isnan(pow_op(INF, NAN)))
302            self.assertTrue(isnan(pow_op(NAN, NAN)))
303
304            # NAN**y is NAN for any y except +-0
305            self.assertTrue(isnan(pow_op(NAN, -INF)))
306            self.assertTrue(isnan(pow_op(NAN, -2.0)))
307            self.assertTrue(isnan(pow_op(NAN, -1.0)))
308            self.assertTrue(isnan(pow_op(NAN, -0.5)))
309            self.assertTrue(isnan(pow_op(NAN, 0.5)))
310            self.assertTrue(isnan(pow_op(NAN, 1.0)))
311            self.assertTrue(isnan(pow_op(NAN, 2.0)))
312            self.assertTrue(isnan(pow_op(NAN, INF)))
313
314            # (+-0)**y raises ZeroDivisionError for y a negative odd integer
315            self.assertRaises(ZeroDivisionError, pow_op, -0.0, -1.0)
316            self.assertRaises(ZeroDivisionError, pow_op, 0.0, -1.0)
317
318            # (+-0)**y raises ZeroDivisionError for y finite and negative
319            # but not an odd integer
320            self.assertRaises(ZeroDivisionError, pow_op, -0.0, -2.0)
321            self.assertRaises(ZeroDivisionError, pow_op, -0.0, -0.5)
322            self.assertRaises(ZeroDivisionError, pow_op, 0.0, -2.0)
323            self.assertRaises(ZeroDivisionError, pow_op, 0.0, -0.5)
324
325            # (+-0)**y is +-0 for y a positive odd integer
326            self.assertEqualAndEqualSign(pow_op(-0.0, 1.0), -0.0)
327            self.assertEqualAndEqualSign(pow_op(0.0, 1.0), 0.0)
328
329            # (+-0)**y is 0 for y finite and positive but not an odd integer
330            self.assertEqualAndEqualSign(pow_op(-0.0, 0.5), 0.0)
331            self.assertEqualAndEqualSign(pow_op(-0.0, 2.0), 0.0)
332            self.assertEqualAndEqualSign(pow_op(0.0, 0.5), 0.0)
333            self.assertEqualAndEqualSign(pow_op(0.0, 2.0), 0.0)
334
335            # (-1)**+-inf is 1
336            self.assertEqualAndEqualSign(pow_op(-1.0, -INF), 1.0)
337            self.assertEqualAndEqualSign(pow_op(-1.0, INF), 1.0)
338
339            # 1**y is 1 for any y, even if y is an infinity or nan
340            self.assertEqualAndEqualSign(pow_op(1.0, -INF), 1.0)
341            self.assertEqualAndEqualSign(pow_op(1.0, -2.0), 1.0)
342            self.assertEqualAndEqualSign(pow_op(1.0, -1.0), 1.0)
343            self.assertEqualAndEqualSign(pow_op(1.0, -0.5), 1.0)
344            self.assertEqualAndEqualSign(pow_op(1.0, -0.0), 1.0)
345            self.assertEqualAndEqualSign(pow_op(1.0, 0.0), 1.0)
346            self.assertEqualAndEqualSign(pow_op(1.0, 0.5), 1.0)
347            self.assertEqualAndEqualSign(pow_op(1.0, 1.0), 1.0)
348            self.assertEqualAndEqualSign(pow_op(1.0, 2.0), 1.0)
349            self.assertEqualAndEqualSign(pow_op(1.0, INF), 1.0)
350            self.assertEqualAndEqualSign(pow_op(1.0, NAN), 1.0)
351
352            # x**+-0 is 1 for any x, even if x is a zero, infinity, or nan
353            self.assertEqualAndEqualSign(pow_op(-INF, 0.0), 1.0)
354            self.assertEqualAndEqualSign(pow_op(-2.0, 0.0), 1.0)
355            self.assertEqualAndEqualSign(pow_op(-1.0, 0.0), 1.0)
356            self.assertEqualAndEqualSign(pow_op(-0.5, 0.0), 1.0)
357            self.assertEqualAndEqualSign(pow_op(-0.0, 0.0), 1.0)
358            self.assertEqualAndEqualSign(pow_op(0.0, 0.0), 1.0)
359            self.assertEqualAndEqualSign(pow_op(0.5, 0.0), 1.0)
360            self.assertEqualAndEqualSign(pow_op(1.0, 0.0), 1.0)
361            self.assertEqualAndEqualSign(pow_op(2.0, 0.0), 1.0)
362            self.assertEqualAndEqualSign(pow_op(INF, 0.0), 1.0)
363            self.assertEqualAndEqualSign(pow_op(NAN, 0.0), 1.0)
364            self.assertEqualAndEqualSign(pow_op(-INF, -0.0), 1.0)
365            self.assertEqualAndEqualSign(pow_op(-2.0, -0.0), 1.0)
366            self.assertEqualAndEqualSign(pow_op(-1.0, -0.0), 1.0)
367            self.assertEqualAndEqualSign(pow_op(-0.5, -0.0), 1.0)
368            self.assertEqualAndEqualSign(pow_op(-0.0, -0.0), 1.0)
369            self.assertEqualAndEqualSign(pow_op(0.0, -0.0), 1.0)
370            self.assertEqualAndEqualSign(pow_op(0.5, -0.0), 1.0)
371            self.assertEqualAndEqualSign(pow_op(1.0, -0.0), 1.0)
372            self.assertEqualAndEqualSign(pow_op(2.0, -0.0), 1.0)
373            self.assertEqualAndEqualSign(pow_op(INF, -0.0), 1.0)
374            self.assertEqualAndEqualSign(pow_op(NAN, -0.0), 1.0)
375
376            # x**y raises ValueError for finite negative x and non-integral y
377            self.assertRaises(ValueError, pow_op, -2.0, -0.5)
378            self.assertRaises(ValueError, pow_op, -2.0, 0.5)
379            self.assertRaises(ValueError, pow_op, -1.0, -0.5)
380            self.assertRaises(ValueError, pow_op, -1.0, 0.5)
381            self.assertRaises(ValueError, pow_op, -0.5, -0.5)
382            self.assertRaises(ValueError, pow_op, -0.5, 0.5)
383
384            # x**-INF is INF for abs(x) < 1
385            self.assertEqualAndEqualSign(pow_op(-0.5, -INF), INF)
386            self.assertEqualAndEqualSign(pow_op(-0.0, -INF), INF)
387            self.assertEqualAndEqualSign(pow_op(0.0, -INF), INF)
388            self.assertEqualAndEqualSign(pow_op(0.5, -INF), INF)
389
390            # x**-INF is 0 for abs(x) > 1
391            self.assertEqualAndEqualSign(pow_op(-INF, -INF), 0.0)
392            self.assertEqualAndEqualSign(pow_op(-2.0, -INF), 0.0)
393            self.assertEqualAndEqualSign(pow_op(2.0, -INF), 0.0)
394            self.assertEqualAndEqualSign(pow_op(INF, -INF), 0.0)
395
396            # x**INF is 0 for abs(x) < 1
397            self.assertEqualAndEqualSign(pow_op(-0.5, INF), 0.0)
398            self.assertEqualAndEqualSign(pow_op(-0.0, INF), 0.0)
399            self.assertEqualAndEqualSign(pow_op(0.0, INF), 0.0)
400            self.assertEqualAndEqualSign(pow_op(0.5, INF), 0.0)
401
402            # x**INF is INF for abs(x) > 1
403            self.assertEqualAndEqualSign(pow_op(-INF, INF), INF)
404            self.assertEqualAndEqualSign(pow_op(-2.0, INF), INF)
405            self.assertEqualAndEqualSign(pow_op(2.0, INF), INF)
406            self.assertEqualAndEqualSign(pow_op(INF, INF), INF)
407
408            # (-INF)**y is -0.0 for y a negative odd integer
409            self.assertEqualAndEqualSign(pow_op(-INF, -1.0), -0.0)
410
411            # (-INF)**y is 0.0 for y negative but not an odd integer
412            self.assertEqualAndEqualSign(pow_op(-INF, -0.5), 0.0)
413            self.assertEqualAndEqualSign(pow_op(-INF, -2.0), 0.0)
414
415            # (-INF)**y is -INF for y a positive odd integer
416            self.assertEqualAndEqualSign(pow_op(-INF, 1.0), -INF)
417
418            # (-INF)**y is INF for y positive but not an odd integer
419            self.assertEqualAndEqualSign(pow_op(-INF, 0.5), INF)
420            self.assertEqualAndEqualSign(pow_op(-INF, 2.0), INF)
421
422            # INF**y is INF for y positive
423            self.assertEqualAndEqualSign(pow_op(INF, 0.5), INF)
424            self.assertEqualAndEqualSign(pow_op(INF, 1.0), INF)
425            self.assertEqualAndEqualSign(pow_op(INF, 2.0), INF)
426
427            # INF**y is 0.0 for y negative
428            self.assertEqualAndEqualSign(pow_op(INF, -2.0), 0.0)
429            self.assertEqualAndEqualSign(pow_op(INF, -1.0), 0.0)
430            self.assertEqualAndEqualSign(pow_op(INF, -0.5), 0.0)
431
432            # basic checks not covered by the special cases above
433            self.assertEqualAndEqualSign(pow_op(-2.0, -2.0), 0.25)
434            self.assertEqualAndEqualSign(pow_op(-2.0, -1.0), -0.5)
435            self.assertEqualAndEqualSign(pow_op(-2.0, -0.0), 1.0)
436            self.assertEqualAndEqualSign(pow_op(-2.0, 0.0), 1.0)
437            self.assertEqualAndEqualSign(pow_op(-2.0, 1.0), -2.0)
438            self.assertEqualAndEqualSign(pow_op(-2.0, 2.0), 4.0)
439            self.assertEqualAndEqualSign(pow_op(-1.0, -2.0), 1.0)
440            self.assertEqualAndEqualSign(pow_op(-1.0, -1.0), -1.0)
441            self.assertEqualAndEqualSign(pow_op(-1.0, -0.0), 1.0)
442            self.assertEqualAndEqualSign(pow_op(-1.0, 0.0), 1.0)
443            self.assertEqualAndEqualSign(pow_op(-1.0, 1.0), -1.0)
444            self.assertEqualAndEqualSign(pow_op(-1.0, 2.0), 1.0)
445            self.assertEqualAndEqualSign(pow_op(2.0, -2.0), 0.25)
446            self.assertEqualAndEqualSign(pow_op(2.0, -1.0), 0.5)
447            self.assertEqualAndEqualSign(pow_op(2.0, -0.0), 1.0)
448            self.assertEqualAndEqualSign(pow_op(2.0, 0.0), 1.0)
449            self.assertEqualAndEqualSign(pow_op(2.0, 1.0), 2.0)
450            self.assertEqualAndEqualSign(pow_op(2.0, 2.0), 4.0)
451
452            # 1 ** large and -1 ** large; some libms apparently
453            # have problems with these
454            self.assertEqualAndEqualSign(pow_op(1.0, -1e100), 1.0)
455            self.assertEqualAndEqualSign(pow_op(1.0, 1e100), 1.0)
456            self.assertEqualAndEqualSign(pow_op(-1.0, -1e100), 1.0)
457            self.assertEqualAndEqualSign(pow_op(-1.0, 1e100), 1.0)
458
459            # check sign for results that underflow to 0
460            self.assertEqualAndEqualSign(pow_op(-2.0, -2000.0), 0.0)
461            self.assertRaises(ValueError, pow_op, -2.0, -2000.5)
462            self.assertEqualAndEqualSign(pow_op(-2.0, -2001.0), -0.0)
463            self.assertEqualAndEqualSign(pow_op(2.0, -2000.0), 0.0)
464            self.assertEqualAndEqualSign(pow_op(2.0, -2000.5), 0.0)
465            self.assertEqualAndEqualSign(pow_op(2.0, -2001.0), 0.0)
466            self.assertEqualAndEqualSign(pow_op(-0.5, 2000.0), 0.0)
467            self.assertRaises(ValueError, pow_op, -0.5, 2000.5)
468            self.assertEqualAndEqualSign(pow_op(-0.5, 2001.0), -0.0)
469            self.assertEqualAndEqualSign(pow_op(0.5, 2000.0), 0.0)
470            self.assertEqualAndEqualSign(pow_op(0.5, 2000.5), 0.0)
471            self.assertEqualAndEqualSign(pow_op(0.5, 2001.0), 0.0)
472
473            # check we don't raise an exception for subnormal results,
474            # and validate signs.  Tests currently disabled, since
475            # they fail on systems where a subnormal result from pow
476            # is flushed to zero (e.g. Debian/ia64.)
477            #self.assertTrue(0.0 < pow_op(0.5, 1048) < 1e-315)
478            #self.assertTrue(0.0 < pow_op(-0.5, 1048) < 1e-315)
479            #self.assertTrue(0.0 < pow_op(0.5, 1047) < 1e-315)
480            #self.assertTrue(0.0 > pow_op(-0.5, 1047) > -1e-315)
481            #self.assertTrue(0.0 < pow_op(2.0, -1048) < 1e-315)
482            #self.assertTrue(0.0 < pow_op(-2.0, -1048) < 1e-315)
483            #self.assertTrue(0.0 < pow_op(2.0, -1047) < 1e-315)
484            #self.assertTrue(0.0 > pow_op(-2.0, -1047) > -1e-315)
485
486
487@requires_setformat
488class FormatFunctionsTestCase(unittest.TestCase):
489
490    def setUp(self):
491        self.save_formats = {'double':float.__getformat__('double'),
492                             'float':float.__getformat__('float')}
493
494    def tearDown(self):
495        float.__setformat__('double', self.save_formats['double'])
496        float.__setformat__('float', self.save_formats['float'])
497
498    def test_getformat(self):
499        self.assertIn(float.__getformat__('double'),
500                      ['unknown', 'IEEE, big-endian', 'IEEE, little-endian'])
501        self.assertIn(float.__getformat__('float'),
502                      ['unknown', 'IEEE, big-endian', 'IEEE, little-endian'])
503        self.assertRaises(ValueError, float.__getformat__, 'chicken')
504        self.assertRaises(TypeError, float.__getformat__, 1)
505
506    def test_setformat(self):
507        for t in 'double', 'float':
508            float.__setformat__(t, 'unknown')
509            if self.save_formats[t] == 'IEEE, big-endian':
510                self.assertRaises(ValueError, float.__setformat__,
511                                  t, 'IEEE, little-endian')
512            elif self.save_formats[t] == 'IEEE, little-endian':
513                self.assertRaises(ValueError, float.__setformat__,
514                                  t, 'IEEE, big-endian')
515            else:
516                self.assertRaises(ValueError, float.__setformat__,
517                                  t, 'IEEE, big-endian')
518                self.assertRaises(ValueError, float.__setformat__,
519                                  t, 'IEEE, little-endian')
520            self.assertRaises(ValueError, float.__setformat__,
521                              t, 'chicken')
522        self.assertRaises(ValueError, float.__setformat__,
523                          'chicken', 'unknown')
524
525BE_DOUBLE_INF = '\x7f\xf0\x00\x00\x00\x00\x00\x00'
526LE_DOUBLE_INF = ''.join(reversed(BE_DOUBLE_INF))
527BE_DOUBLE_NAN = '\x7f\xf8\x00\x00\x00\x00\x00\x00'
528LE_DOUBLE_NAN = ''.join(reversed(BE_DOUBLE_NAN))
529
530BE_FLOAT_INF = '\x7f\x80\x00\x00'
531LE_FLOAT_INF = ''.join(reversed(BE_FLOAT_INF))
532BE_FLOAT_NAN = '\x7f\xc0\x00\x00'
533LE_FLOAT_NAN = ''.join(reversed(BE_FLOAT_NAN))
534
535# on non-IEEE platforms, attempting to unpack a bit pattern
536# representing an infinity or a NaN should raise an exception.
537
538@requires_setformat
539class UnknownFormatTestCase(unittest.TestCase):
540    def setUp(self):
541        self.save_formats = {'double':float.__getformat__('double'),
542                             'float':float.__getformat__('float')}
543        float.__setformat__('double', 'unknown')
544        float.__setformat__('float', 'unknown')
545
546    def tearDown(self):
547        float.__setformat__('double', self.save_formats['double'])
548        float.__setformat__('float', self.save_formats['float'])
549
550    def test_double_specials_dont_unpack(self):
551        for fmt, data in [('>d', BE_DOUBLE_INF),
552                          ('>d', BE_DOUBLE_NAN),
553                          ('<d', LE_DOUBLE_INF),
554                          ('<d', LE_DOUBLE_NAN)]:
555            self.assertRaises(ValueError, struct.unpack, fmt, data)
556
557    def test_float_specials_dont_unpack(self):
558        for fmt, data in [('>f', BE_FLOAT_INF),
559                          ('>f', BE_FLOAT_NAN),
560                          ('<f', LE_FLOAT_INF),
561                          ('<f', LE_FLOAT_NAN)]:
562            self.assertRaises(ValueError, struct.unpack, fmt, data)
563
564
565# on an IEEE platform, all we guarantee is that bit patterns
566# representing infinities or NaNs do not raise an exception; all else
567# is accident (today).
568# let's also try to guarantee that -0.0 and 0.0 don't get confused.
569
570class IEEEFormatTestCase(unittest.TestCase):
571
572    @requires_IEEE_754
573    def test_double_specials_do_unpack(self):
574        for fmt, data in [('>d', BE_DOUBLE_INF),
575                          ('>d', BE_DOUBLE_NAN),
576                          ('<d', LE_DOUBLE_INF),
577                          ('<d', LE_DOUBLE_NAN)]:
578            struct.unpack(fmt, data)
579
580    @requires_IEEE_754
581    def test_float_specials_do_unpack(self):
582        for fmt, data in [('>f', BE_FLOAT_INF),
583                          ('>f', BE_FLOAT_NAN),
584                          ('<f', LE_FLOAT_INF),
585                          ('<f', LE_FLOAT_NAN)]:
586            struct.unpack(fmt, data)
587
588    @requires_IEEE_754
589    def test_negative_zero(self):
590        def pos_pos():
591            return 0.0, math.atan2(0.0, -1)
592        def pos_neg():
593            return 0.0, math.atan2(-0.0, -1)
594        def neg_pos():
595            return -0.0, math.atan2(0.0, -1)
596        def neg_neg():
597            return -0.0, math.atan2(-0.0, -1)
598        self.assertEqual(pos_pos(), neg_pos())
599        self.assertEqual(pos_neg(), neg_neg())
600
601    @requires_IEEE_754
602    def test_underflow_sign(self):
603        # check that -1e-1000 gives -0.0, not 0.0
604        self.assertEqual(math.atan2(-1e-1000, -1), math.atan2(-0.0, -1))
605        self.assertEqual(math.atan2(float('-1e-1000'), -1),
606                         math.atan2(-0.0, -1))
607
608    def test_format(self):
609        # these should be rewritten to use both format(x, spec) and
610        # x.__format__(spec)
611
612        self.assertEqual(format(0.0, 'f'), '0.000000')
613
614        # the default is 'g', except for empty format spec
615        self.assertEqual(format(0.0, ''), '0.0')
616        self.assertEqual(format(0.01, ''), '0.01')
617        self.assertEqual(format(0.01, 'g'), '0.01')
618
619        # empty presentation type should format in the same way as str
620        # (issue 5920)
621        x = 100/7.
622        self.assertEqual(format(x, ''), str(x))
623        self.assertEqual(format(x, '-'), str(x))
624        self.assertEqual(format(x, '>'), str(x))
625        self.assertEqual(format(x, '2'), str(x))
626
627        self.assertEqual(format(1.0, 'f'), '1.000000')
628
629        self.assertEqual(format(-1.0, 'f'), '-1.000000')
630
631        self.assertEqual(format( 1.0, ' f'), ' 1.000000')
632        self.assertEqual(format(-1.0, ' f'), '-1.000000')
633        self.assertEqual(format( 1.0, '+f'), '+1.000000')
634        self.assertEqual(format(-1.0, '+f'), '-1.000000')
635
636        # % formatting
637        self.assertEqual(format(-1.0, '%'), '-100.000000%')
638
639        # conversion to string should fail
640        self.assertRaises(ValueError, format, 3.0, "s")
641
642        # other format specifiers shouldn't work on floats,
643        #  in particular int specifiers
644        for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
645                            [chr(x) for x in range(ord('A'), ord('Z')+1)]):
646            if not format_spec in 'eEfFgGn%':
647                self.assertRaises(ValueError, format, 0.0, format_spec)
648                self.assertRaises(ValueError, format, 1.0, format_spec)
649                self.assertRaises(ValueError, format, -1.0, format_spec)
650                self.assertRaises(ValueError, format, 1e100, format_spec)
651                self.assertRaises(ValueError, format, -1e100, format_spec)
652                self.assertRaises(ValueError, format, 1e-100, format_spec)
653                self.assertRaises(ValueError, format, -1e-100, format_spec)
654
655        # issue 3382: 'f' and 'F' with inf's and nan's
656        self.assertEqual('{0:f}'.format(INF), 'inf')
657        self.assertEqual('{0:F}'.format(INF), 'INF')
658        self.assertEqual('{0:f}'.format(-INF), '-inf')
659        self.assertEqual('{0:F}'.format(-INF), '-INF')
660        self.assertEqual('{0:f}'.format(NAN), 'nan')
661        self.assertEqual('{0:F}'.format(NAN), 'NAN')
662
663    @requires_IEEE_754
664    def test_format_testfile(self):
665        with open(format_testfile) as testfile:
666            for line in open(format_testfile):
667                if line.startswith('--'):
668                    continue
669                line = line.strip()
670                if not line:
671                    continue
672
673                lhs, rhs = map(str.strip, line.split('->'))
674                fmt, arg = lhs.split()
675                arg = float(arg)
676                self.assertEqual(fmt % arg, rhs)
677                if not math.isnan(arg) and copysign(1.0, arg) > 0.0:
678                    self.assertEqual(fmt % -arg, '-' + rhs)
679
680    def test_issue5864(self):
681        self.assertEqual(format(123.456, '.4'), '123.5')
682        self.assertEqual(format(1234.56, '.4'), '1.235e+03')
683        self.assertEqual(format(12345.6, '.4'), '1.235e+04')
684
685class ReprTestCase(unittest.TestCase):
686    def test_repr(self):
687        floats_file = open(os.path.join(os.path.split(__file__)[0],
688                           'floating_points.txt'))
689        for line in floats_file:
690            line = line.strip()
691            if not line or line.startswith('#'):
692                continue
693            v = eval(line)
694            self.assertEqual(v, eval(repr(v)))
695        floats_file.close()
696
697    @unittest.skipUnless(getattr(sys, 'float_repr_style', '') == 'short',
698                         "applies only when using short float repr style")
699    def test_short_repr(self):
700        # test short float repr introduced in Python 3.1.  One aspect
701        # of this repr is that we get some degree of str -> float ->
702        # str roundtripping.  In particular, for any numeric string
703        # containing 15 or fewer significant digits, those exact same
704        # digits (modulo trailing zeros) should appear in the output.
705        # No more repr(0.03) -> "0.029999999999999999"!
706
707        test_strings = [
708            # output always includes *either* a decimal point and at
709            # least one digit after that point, or an exponent.
710            '0.0',
711            '1.0',
712            '0.01',
713            '0.02',
714            '0.03',
715            '0.04',
716            '0.05',
717            '1.23456789',
718            '10.0',
719            '100.0',
720            # values >= 1e16 get an exponent...
721            '1000000000000000.0',
722            '9999999999999990.0',
723            '1e+16',
724            '1e+17',
725            # ... and so do values < 1e-4
726            '0.001',
727            '0.001001',
728            '0.00010000000000001',
729            '0.0001',
730            '9.999999999999e-05',
731            '1e-05',
732            # values designed to provoke failure if the FPU rounding
733            # precision isn't set correctly
734            '8.72293771110361e+25',
735            '7.47005307342313e+26',
736            '2.86438000439698e+28',
737            '8.89142905246179e+28',
738            '3.08578087079232e+35',
739            ]
740
741        for s in test_strings:
742            negs = '-'+s
743            self.assertEqual(s, repr(float(s)))
744            self.assertEqual(negs, repr(float(negs)))
745
746
747@requires_IEEE_754
748class RoundTestCase(unittest.TestCase):
749    def test_second_argument_type(self):
750        # any type with an __index__ method should be permitted as
751        # a second argument
752        self.assertAlmostEqual(round(12.34, True), 12.3)
753
754        class MyIndex(object):
755            def __index__(self): return 4
756        self.assertAlmostEqual(round(-0.123456, MyIndex()), -0.1235)
757        # but floats should be illegal
758        self.assertRaises(TypeError, round, 3.14159, 2.0)
759
760    def test_inf_nan(self):
761        # rounding an infinity or nan returns the same number;
762        # (in py3k, rounding an infinity or nan raises an error,
763        #  since the result can't be represented as a long).
764        self.assertEqual(round(INF), INF)
765        self.assertEqual(round(-INF), -INF)
766        self.assertTrue(math.isnan(round(NAN)))
767        for n in range(-5, 5):
768            self.assertEqual(round(INF, n), INF)
769            self.assertEqual(round(-INF, n), -INF)
770            self.assertTrue(math.isnan(round(NAN, n)))
771
772        self.assertRaises(TypeError, round, INF, 0.0)
773        self.assertRaises(TypeError, round, -INF, 1.0)
774        self.assertRaises(TypeError, round, NAN, "ceci n'est pas un integer")
775        self.assertRaises(TypeError, round, -0.0, 1j)
776
777    def test_large_n(self):
778        for n in [324, 325, 400, 2**31-1, 2**31, 2**32, 2**100]:
779            self.assertEqual(round(123.456, n), 123.456)
780            self.assertEqual(round(-123.456, n), -123.456)
781            self.assertEqual(round(1e300, n), 1e300)
782            self.assertEqual(round(1e-320, n), 1e-320)
783        self.assertEqual(round(1e150, 300), 1e150)
784        self.assertEqual(round(1e300, 307), 1e300)
785        self.assertEqual(round(-3.1415, 308), -3.1415)
786        self.assertEqual(round(1e150, 309), 1e150)
787        self.assertEqual(round(1.4e-315, 315), 1e-315)
788
789    def test_small_n(self):
790        for n in [-308, -309, -400, 1-2**31, -2**31, -2**31-1, -2**100]:
791            self.assertEqual(round(123.456, n), 0.0)
792            self.assertEqual(round(-123.456, n), -0.0)
793            self.assertEqual(round(1e300, n), 0.0)
794            self.assertEqual(round(1e-320, n), 0.0)
795
796    def test_overflow(self):
797        self.assertRaises(OverflowError, round, 1.6e308, -308)
798        self.assertRaises(OverflowError, round, -1.7e308, -308)
799
800    @unittest.skipUnless(getattr(sys, 'float_repr_style', '') == 'short',
801                         "test applies only when using short float repr style")
802    def test_previous_round_bugs(self):
803        # particular cases that have occurred in bug reports
804        self.assertEqual(round(562949953421312.5, 1),
805                          562949953421312.5)
806        self.assertEqual(round(56294995342131.5, 3),
807                         56294995342131.5)
808
809    @unittest.skipUnless(getattr(sys, 'float_repr_style', '') == 'short',
810                         "test applies only when using short float repr style")
811    def test_halfway_cases(self):
812        # Halfway cases need special attention, since the current
813        # implementation has to deal with them specially.  Note that
814        # 2.x rounds halfway values up (i.e., away from zero) while
815        # 3.x does round-half-to-even.
816        self.assertAlmostEqual(round(0.125, 2), 0.13)
817        self.assertAlmostEqual(round(0.375, 2), 0.38)
818        self.assertAlmostEqual(round(0.625, 2), 0.63)
819        self.assertAlmostEqual(round(0.875, 2), 0.88)
820        self.assertAlmostEqual(round(-0.125, 2), -0.13)
821        self.assertAlmostEqual(round(-0.375, 2), -0.38)
822        self.assertAlmostEqual(round(-0.625, 2), -0.63)
823        self.assertAlmostEqual(round(-0.875, 2), -0.88)
824
825        self.assertAlmostEqual(round(0.25, 1), 0.3)
826        self.assertAlmostEqual(round(0.75, 1), 0.8)
827        self.assertAlmostEqual(round(-0.25, 1), -0.3)
828        self.assertAlmostEqual(round(-0.75, 1), -0.8)
829
830        self.assertEqual(round(-6.5, 0), -7.0)
831        self.assertEqual(round(-5.5, 0), -6.0)
832        self.assertEqual(round(-1.5, 0), -2.0)
833        self.assertEqual(round(-0.5, 0), -1.0)
834        self.assertEqual(round(0.5, 0), 1.0)
835        self.assertEqual(round(1.5, 0), 2.0)
836        self.assertEqual(round(2.5, 0), 3.0)
837        self.assertEqual(round(3.5, 0), 4.0)
838        self.assertEqual(round(4.5, 0), 5.0)
839        self.assertEqual(round(5.5, 0), 6.0)
840        self.assertEqual(round(6.5, 0), 7.0)
841
842        # same but without an explicit second argument; in 3.x these
843        # will give integers
844        self.assertEqual(round(-6.5), -7.0)
845        self.assertEqual(round(-5.5), -6.0)
846        self.assertEqual(round(-1.5), -2.0)
847        self.assertEqual(round(-0.5), -1.0)
848        self.assertEqual(round(0.5), 1.0)
849        self.assertEqual(round(1.5), 2.0)
850        self.assertEqual(round(2.5), 3.0)
851        self.assertEqual(round(3.5), 4.0)
852        self.assertEqual(round(4.5), 5.0)
853        self.assertEqual(round(5.5), 6.0)
854        self.assertEqual(round(6.5), 7.0)
855
856        self.assertEqual(round(-25.0, -1), -30.0)
857        self.assertEqual(round(-15.0, -1), -20.0)
858        self.assertEqual(round(-5.0, -1), -10.0)
859        self.assertEqual(round(5.0, -1), 10.0)
860        self.assertEqual(round(15.0, -1), 20.0)
861        self.assertEqual(round(25.0, -1), 30.0)
862        self.assertEqual(round(35.0, -1), 40.0)
863        self.assertEqual(round(45.0, -1), 50.0)
864        self.assertEqual(round(55.0, -1), 60.0)
865        self.assertEqual(round(65.0, -1), 70.0)
866        self.assertEqual(round(75.0, -1), 80.0)
867        self.assertEqual(round(85.0, -1), 90.0)
868        self.assertEqual(round(95.0, -1), 100.0)
869        self.assertEqual(round(12325.0, -1), 12330.0)
870
871        self.assertEqual(round(350.0, -2), 400.0)
872        self.assertEqual(round(450.0, -2), 500.0)
873
874        self.assertAlmostEqual(round(0.5e21, -21), 1e21)
875        self.assertAlmostEqual(round(1.5e21, -21), 2e21)
876        self.assertAlmostEqual(round(2.5e21, -21), 3e21)
877        self.assertAlmostEqual(round(5.5e21, -21), 6e21)
878        self.assertAlmostEqual(round(8.5e21, -21), 9e21)
879
880        self.assertAlmostEqual(round(-1.5e22, -22), -2e22)
881        self.assertAlmostEqual(round(-0.5e22, -22), -1e22)
882        self.assertAlmostEqual(round(0.5e22, -22), 1e22)
883        self.assertAlmostEqual(round(1.5e22, -22), 2e22)
884
885
886    @requires_IEEE_754
887    def test_format_specials(self):
888        # Test formatting of nans and infs.
889
890        def test(fmt, value, expected):
891            # Test with both % and format().
892            self.assertEqual(fmt % value, expected, fmt)
893            if not '#' in fmt:
894                # Until issue 7094 is implemented, format() for floats doesn't
895                #  support '#' formatting
896                fmt = fmt[1:] # strip off the %
897                self.assertEqual(format(value, fmt), expected, fmt)
898
899        for fmt in ['%e', '%f', '%g', '%.0e', '%.6f', '%.20g',
900                    '%#e', '%#f', '%#g', '%#.20e', '%#.15f', '%#.3g']:
901            pfmt = '%+' + fmt[1:]
902            sfmt = '% ' + fmt[1:]
903            test(fmt, INF, 'inf')
904            test(fmt, -INF, '-inf')
905            test(fmt, NAN, 'nan')
906            test(fmt, -NAN, 'nan')
907            # When asking for a sign, it's always provided. nans are
908            #  always positive.
909            test(pfmt, INF, '+inf')
910            test(pfmt, -INF, '-inf')
911            test(pfmt, NAN, '+nan')
912            test(pfmt, -NAN, '+nan')
913            # When using ' ' for a sign code, only infs can be negative.
914            #  Others have a space.
915            test(sfmt, INF, ' inf')
916            test(sfmt, -INF, '-inf')
917            test(sfmt, NAN, ' nan')
918            test(sfmt, -NAN, ' nan')
919
920
921# Beginning with Python 2.6 float has cross platform compatible
922# ways to create and represent inf and nan
923class InfNanTest(unittest.TestCase):
924    def test_inf_from_str(self):
925        self.assertTrue(isinf(float("inf")))
926        self.assertTrue(isinf(float("+inf")))
927        self.assertTrue(isinf(float("-inf")))
928        self.assertTrue(isinf(float("infinity")))
929        self.assertTrue(isinf(float("+infinity")))
930        self.assertTrue(isinf(float("-infinity")))
931
932        self.assertEqual(repr(float("inf")), "inf")
933        self.assertEqual(repr(float("+inf")), "inf")
934        self.assertEqual(repr(float("-inf")), "-inf")
935        self.assertEqual(repr(float("infinity")), "inf")
936        self.assertEqual(repr(float("+infinity")), "inf")
937        self.assertEqual(repr(float("-infinity")), "-inf")
938
939        self.assertEqual(repr(float("INF")), "inf")
940        self.assertEqual(repr(float("+Inf")), "inf")
941        self.assertEqual(repr(float("-iNF")), "-inf")
942        self.assertEqual(repr(float("Infinity")), "inf")
943        self.assertEqual(repr(float("+iNfInItY")), "inf")
944        self.assertEqual(repr(float("-INFINITY")), "-inf")
945
946        self.assertEqual(str(float("inf")), "inf")
947        self.assertEqual(str(float("+inf")), "inf")
948        self.assertEqual(str(float("-inf")), "-inf")
949        self.assertEqual(str(float("infinity")), "inf")
950        self.assertEqual(str(float("+infinity")), "inf")
951        self.assertEqual(str(float("-infinity")), "-inf")
952
953        self.assertRaises(ValueError, float, "info")
954        self.assertRaises(ValueError, float, "+info")
955        self.assertRaises(ValueError, float, "-info")
956        self.assertRaises(ValueError, float, "in")
957        self.assertRaises(ValueError, float, "+in")
958        self.assertRaises(ValueError, float, "-in")
959        self.assertRaises(ValueError, float, "infinit")
960        self.assertRaises(ValueError, float, "+Infin")
961        self.assertRaises(ValueError, float, "-INFI")
962        self.assertRaises(ValueError, float, "infinitys")
963
964    def test_inf_as_str(self):
965        self.assertEqual(repr(1e300 * 1e300), "inf")
966        self.assertEqual(repr(-1e300 * 1e300), "-inf")
967
968        self.assertEqual(str(1e300 * 1e300), "inf")
969        self.assertEqual(str(-1e300 * 1e300), "-inf")
970
971    def test_nan_from_str(self):
972        self.assertTrue(isnan(float("nan")))
973        self.assertTrue(isnan(float("+nan")))
974        self.assertTrue(isnan(float("-nan")))
975
976        self.assertEqual(repr(float("nan")), "nan")
977        self.assertEqual(repr(float("+nan")), "nan")
978        self.assertEqual(repr(float("-nan")), "nan")
979
980        self.assertEqual(repr(float("NAN")), "nan")
981        self.assertEqual(repr(float("+NAn")), "nan")
982        self.assertEqual(repr(float("-NaN")), "nan")
983
984        self.assertEqual(str(float("nan")), "nan")
985        self.assertEqual(str(float("+nan")), "nan")
986        self.assertEqual(str(float("-nan")), "nan")
987
988        self.assertRaises(ValueError, float, "nana")
989        self.assertRaises(ValueError, float, "+nana")
990        self.assertRaises(ValueError, float, "-nana")
991        self.assertRaises(ValueError, float, "na")
992        self.assertRaises(ValueError, float, "+na")
993        self.assertRaises(ValueError, float, "-na")
994
995    def test_nan_as_str(self):
996        self.assertEqual(repr(1e300 * 1e300 * 0), "nan")
997        self.assertEqual(repr(-1e300 * 1e300 * 0), "nan")
998
999        self.assertEqual(str(1e300 * 1e300 * 0), "nan")
1000        self.assertEqual(str(-1e300 * 1e300 * 0), "nan")
1001
1002    def notest_float_nan(self):
1003        self.assertTrue(NAN.is_nan())
1004        self.assertFalse(INF.is_nan())
1005        self.assertFalse((0.).is_nan())
1006
1007    def notest_float_inf(self):
1008        self.assertTrue(INF.is_inf())
1009        self.assertFalse(NAN.is_inf())
1010        self.assertFalse((0.).is_inf())
1011
1012    def test_hash_inf(self):
1013        # the actual values here should be regarded as an
1014        # implementation detail, but they need to be
1015        # identical to those used in the Decimal module.
1016        self.assertEqual(hash(float('inf')), 314159)
1017        self.assertEqual(hash(float('-inf')), -271828)
1018        self.assertEqual(hash(float('nan')), 0)
1019
1020
1021fromHex = float.fromhex
1022toHex = float.hex
1023class HexFloatTestCase(unittest.TestCase):
1024    MAX = fromHex('0x.fffffffffffff8p+1024')  # max normal
1025    MIN = fromHex('0x1p-1022')                # min normal
1026    TINY = fromHex('0x0.0000000000001p-1022') # min subnormal
1027    EPS = fromHex('0x0.0000000000001p0') # diff between 1.0 and next float up
1028
1029    def identical(self, x, y):
1030        # check that floats x and y are identical, or that both
1031        # are NaNs
1032        if isnan(x) or isnan(y):
1033            if isnan(x) == isnan(y):
1034                return
1035        elif x == y and (x != 0.0 or copysign(1.0, x) == copysign(1.0, y)):
1036            return
1037        self.fail('%r not identical to %r' % (x, y))
1038
1039    def test_ends(self):
1040        self.identical(self.MIN, ldexp(1.0, -1022))
1041        self.identical(self.TINY, ldexp(1.0, -1074))
1042        self.identical(self.EPS, ldexp(1.0, -52))
1043        self.identical(self.MAX, 2.*(ldexp(1.0, 1023) - ldexp(1.0, 970)))
1044
1045    def test_invalid_inputs(self):
1046        invalid_inputs = [
1047            'infi',   # misspelt infinities and nans
1048            '-Infinit',
1049            '++inf',
1050            '-+Inf',
1051            '--nan',
1052            '+-NaN',
1053            'snan',
1054            'NaNs',
1055            'nna',
1056            'an',
1057            'nf',
1058            'nfinity',
1059            'inity',
1060            'iinity',
1061            '0xnan',
1062            '',
1063            ' ',
1064            'x1.0p0',
1065            '0xX1.0p0',
1066            '+ 0x1.0p0', # internal whitespace
1067            '- 0x1.0p0',
1068            '0 x1.0p0',
1069            '0x 1.0p0',
1070            '0x1 2.0p0',
1071            '+0x1 .0p0',
1072            '0x1. 0p0',
1073            '-0x1.0 1p0',
1074            '-0x1.0 p0',
1075            '+0x1.0p +0',
1076            '0x1.0p -0',
1077            '0x1.0p 0',
1078            '+0x1.0p+ 0',
1079            '-0x1.0p- 0',
1080            '++0x1.0p-0', # double signs
1081            '--0x1.0p0',
1082            '+-0x1.0p+0',
1083            '-+0x1.0p0',
1084            '0x1.0p++0',
1085            '+0x1.0p+-0',
1086            '-0x1.0p-+0',
1087            '0x1.0p--0',
1088            '0x1.0.p0',
1089            '0x.p0', # no hex digits before or after point
1090            '0x1,p0', # wrong decimal point character
1091            '0x1pa',
1092            u'0x1p\uff10',  # fullwidth Unicode digits
1093            u'\uff10x1p0',
1094            u'0x\uff11p0',
1095            u'0x1.\uff10p0',
1096            '0x1p0 \n 0x2p0',
1097            '0x1p0\0 0x1p0',  # embedded null byte is not end of string
1098            ]
1099        for x in invalid_inputs:
1100            try:
1101                result = fromHex(x)
1102            except ValueError:
1103                pass
1104            else:
1105                self.fail('Expected float.fromhex(%r) to raise ValueError; '
1106                          'got %r instead' % (x, result))
1107
1108
1109    def test_whitespace(self):
1110        value_pairs = [
1111            ('inf', INF),
1112            ('-Infinity', -INF),
1113            ('nan', NAN),
1114            ('1.0', 1.0),
1115            ('-0x.2', -0.125),
1116            ('-0.0', -0.0)
1117            ]
1118        whitespace = [
1119            '',
1120            ' ',
1121            '\t',
1122            '\n',
1123            '\n \t',
1124            '\f',
1125            '\v',
1126            '\r'
1127            ]
1128        for inp, expected in value_pairs:
1129            for lead in whitespace:
1130                for trail in whitespace:
1131                    got = fromHex(lead + inp + trail)
1132                    self.identical(got, expected)
1133
1134
1135    def test_from_hex(self):
1136        MIN = self.MIN;
1137        MAX = self.MAX;
1138        TINY = self.TINY;
1139        EPS = self.EPS;
1140
1141        # two spellings of infinity, with optional signs; case-insensitive
1142        self.identical(fromHex('inf'), INF)
1143        self.identical(fromHex('+Inf'), INF)
1144        self.identical(fromHex('-INF'), -INF)
1145        self.identical(fromHex('iNf'), INF)
1146        self.identical(fromHex('Infinity'), INF)
1147        self.identical(fromHex('+INFINITY'), INF)
1148        self.identical(fromHex('-infinity'), -INF)
1149        self.identical(fromHex('-iNFiNitY'), -INF)
1150
1151        # nans with optional sign; case insensitive
1152        self.identical(fromHex('nan'), NAN)
1153        self.identical(fromHex('+NaN'), NAN)
1154        self.identical(fromHex('-NaN'), NAN)
1155        self.identical(fromHex('-nAN'), NAN)
1156
1157        # variations in input format
1158        self.identical(fromHex('1'), 1.0)
1159        self.identical(fromHex('+1'), 1.0)
1160        self.identical(fromHex('1.'), 1.0)
1161        self.identical(fromHex('1.0'), 1.0)
1162        self.identical(fromHex('1.0p0'), 1.0)
1163        self.identical(fromHex('01'), 1.0)
1164        self.identical(fromHex('01.'), 1.0)
1165        self.identical(fromHex('0x1'), 1.0)
1166        self.identical(fromHex('0x1.'), 1.0)
1167        self.identical(fromHex('0x1.0'), 1.0)
1168        self.identical(fromHex('+0x1.0'), 1.0)
1169        self.identical(fromHex('0x1p0'), 1.0)
1170        self.identical(fromHex('0X1p0'), 1.0)
1171        self.identical(fromHex('0X1P0'), 1.0)
1172        self.identical(fromHex('0x1P0'), 1.0)
1173        self.identical(fromHex('0x1.p0'), 1.0)
1174        self.identical(fromHex('0x1.0p0'), 1.0)
1175        self.identical(fromHex('0x.1p4'), 1.0)
1176        self.identical(fromHex('0x.1p04'), 1.0)
1177        self.identical(fromHex('0x.1p004'), 1.0)
1178        self.identical(fromHex('0x1p+0'), 1.0)
1179        self.identical(fromHex('0x1P-0'), 1.0)
1180        self.identical(fromHex('+0x1p0'), 1.0)
1181        self.identical(fromHex('0x01p0'), 1.0)
1182        self.identical(fromHex('0x1p00'), 1.0)
1183        self.identical(fromHex(u'0x1p0'), 1.0)
1184        self.identical(fromHex(' 0x1p0 '), 1.0)
1185        self.identical(fromHex('\n 0x1p0'), 1.0)
1186        self.identical(fromHex('0x1p0 \t'), 1.0)
1187        self.identical(fromHex('0xap0'), 10.0)
1188        self.identical(fromHex('0xAp0'), 10.0)
1189        self.identical(fromHex('0xaP0'), 10.0)
1190        self.identical(fromHex('0xAP0'), 10.0)
1191        self.identical(fromHex('0xbep0'), 190.0)
1192        self.identical(fromHex('0xBep0'), 190.0)
1193        self.identical(fromHex('0xbEp0'), 190.0)
1194        self.identical(fromHex('0XBE0P-4'), 190.0)
1195        self.identical(fromHex('0xBEp0'), 190.0)
1196        self.identical(fromHex('0xB.Ep4'), 190.0)
1197        self.identical(fromHex('0x.BEp8'), 190.0)
1198        self.identical(fromHex('0x.0BEp12'), 190.0)
1199
1200        # moving the point around
1201        pi = fromHex('0x1.921fb54442d18p1')
1202        self.identical(fromHex('0x.006487ed5110b46p11'), pi)
1203        self.identical(fromHex('0x.00c90fdaa22168cp10'), pi)
1204        self.identical(fromHex('0x.01921fb54442d18p9'), pi)
1205        self.identical(fromHex('0x.03243f6a8885a3p8'), pi)
1206        self.identical(fromHex('0x.06487ed5110b46p7'), pi)
1207        self.identical(fromHex('0x.0c90fdaa22168cp6'), pi)
1208        self.identical(fromHex('0x.1921fb54442d18p5'), pi)
1209        self.identical(fromHex('0x.3243f6a8885a3p4'), pi)
1210        self.identical(fromHex('0x.6487ed5110b46p3'), pi)
1211        self.identical(fromHex('0x.c90fdaa22168cp2'), pi)
1212        self.identical(fromHex('0x1.921fb54442d18p1'), pi)
1213        self.identical(fromHex('0x3.243f6a8885a3p0'), pi)
1214        self.identical(fromHex('0x6.487ed5110b46p-1'), pi)
1215        self.identical(fromHex('0xc.90fdaa22168cp-2'), pi)
1216        self.identical(fromHex('0x19.21fb54442d18p-3'), pi)
1217        self.identical(fromHex('0x32.43f6a8885a3p-4'), pi)
1218        self.identical(fromHex('0x64.87ed5110b46p-5'), pi)
1219        self.identical(fromHex('0xc9.0fdaa22168cp-6'), pi)
1220        self.identical(fromHex('0x192.1fb54442d18p-7'), pi)
1221        self.identical(fromHex('0x324.3f6a8885a3p-8'), pi)
1222        self.identical(fromHex('0x648.7ed5110b46p-9'), pi)
1223        self.identical(fromHex('0xc90.fdaa22168cp-10'), pi)
1224        self.identical(fromHex('0x1921.fb54442d18p-11'), pi)
1225        # ...
1226        self.identical(fromHex('0x1921fb54442d1.8p-47'), pi)
1227        self.identical(fromHex('0x3243f6a8885a3p-48'), pi)
1228        self.identical(fromHex('0x6487ed5110b46p-49'), pi)
1229        self.identical(fromHex('0xc90fdaa22168cp-50'), pi)
1230        self.identical(fromHex('0x1921fb54442d18p-51'), pi)
1231        self.identical(fromHex('0x3243f6a8885a30p-52'), pi)
1232        self.identical(fromHex('0x6487ed5110b460p-53'), pi)
1233        self.identical(fromHex('0xc90fdaa22168c0p-54'), pi)
1234        self.identical(fromHex('0x1921fb54442d180p-55'), pi)
1235
1236
1237        # results that should overflow...
1238        self.assertRaises(OverflowError, fromHex, '-0x1p1024')
1239        self.assertRaises(OverflowError, fromHex, '0x1p+1025')
1240        self.assertRaises(OverflowError, fromHex, '+0X1p1030')
1241        self.assertRaises(OverflowError, fromHex, '-0x1p+1100')
1242        self.assertRaises(OverflowError, fromHex, '0X1p123456789123456789')
1243        self.assertRaises(OverflowError, fromHex, '+0X.8p+1025')
1244        self.assertRaises(OverflowError, fromHex, '+0x0.8p1025')
1245        self.assertRaises(OverflowError, fromHex, '-0x0.4p1026')
1246        self.assertRaises(OverflowError, fromHex, '0X2p+1023')
1247        self.assertRaises(OverflowError, fromHex, '0x2.p1023')
1248        self.assertRaises(OverflowError, fromHex, '-0x2.0p+1023')
1249        self.assertRaises(OverflowError, fromHex, '+0X4p+1022')
1250        self.assertRaises(OverflowError, fromHex, '0x1.ffffffffffffffp+1023')
1251        self.assertRaises(OverflowError, fromHex, '-0X1.fffffffffffff9p1023')
1252        self.assertRaises(OverflowError, fromHex, '0X1.fffffffffffff8p1023')
1253        self.assertRaises(OverflowError, fromHex, '+0x3.fffffffffffffp1022')
1254        self.assertRaises(OverflowError, fromHex, '0x3fffffffffffffp+970')
1255        self.assertRaises(OverflowError, fromHex, '0x10000000000000000p960')
1256        self.assertRaises(OverflowError, fromHex, '-0Xffffffffffffffffp960')
1257
1258        # ...and those that round to +-max float
1259        self.identical(fromHex('+0x1.fffffffffffffp+1023'), MAX)
1260        self.identical(fromHex('-0X1.fffffffffffff7p1023'), -MAX)
1261        self.identical(fromHex('0X1.fffffffffffff7fffffffffffffp1023'), MAX)
1262
1263        # zeros
1264        self.identical(fromHex('0x0p0'), 0.0)
1265        self.identical(fromHex('0x0p1000'), 0.0)
1266        self.identical(fromHex('-0x0p1023'), -0.0)
1267        self.identical(fromHex('0X0p1024'), 0.0)
1268        self.identical(fromHex('-0x0p1025'), -0.0)
1269        self.identical(fromHex('0X0p2000'), 0.0)
1270        self.identical(fromHex('0x0p123456789123456789'), 0.0)
1271        self.identical(fromHex('-0X0p-0'), -0.0)
1272        self.identical(fromHex('-0X0p-1000'), -0.0)
1273        self.identical(fromHex('0x0p-1023'), 0.0)
1274        self.identical(fromHex('-0X0p-1024'), -0.0)
1275        self.identical(fromHex('-0x0p-1025'), -0.0)
1276        self.identical(fromHex('-0x0p-1072'), -0.0)
1277        self.identical(fromHex('0X0p-1073'), 0.0)
1278        self.identical(fromHex('-0x0p-1074'), -0.0)
1279        self.identical(fromHex('0x0p-1075'), 0.0)
1280        self.identical(fromHex('0X0p-1076'), 0.0)
1281        self.identical(fromHex('-0X0p-2000'), -0.0)
1282        self.identical(fromHex('-0x0p-123456789123456789'), -0.0)
1283
1284        # values that should underflow to 0
1285        self.identical(fromHex('0X1p-1075'), 0.0)
1286        self.identical(fromHex('-0X1p-1075'), -0.0)
1287        self.identical(fromHex('-0x1p-123456789123456789'), -0.0)
1288        self.identical(fromHex('0x1.00000000000000001p-1075'), TINY)
1289        self.identical(fromHex('-0x1.1p-1075'), -TINY)
1290        self.identical(fromHex('0x1.fffffffffffffffffp-1075'), TINY)
1291
1292        # check round-half-even is working correctly near 0 ...
1293        self.identical(fromHex('0x1p-1076'), 0.0)
1294        self.identical(fromHex('0X2p-1076'), 0.0)
1295        self.identical(fromHex('0X3p-1076'), TINY)
1296        self.identical(fromHex('0x4p-1076'), TINY)
1297        self.identical(fromHex('0X5p-1076'), TINY)
1298        self.identical(fromHex('0X6p-1076'), 2*TINY)
1299        self.identical(fromHex('0x7p-1076'), 2*TINY)
1300        self.identical(fromHex('0X8p-1076'), 2*TINY)
1301        self.identical(fromHex('0X9p-1076'), 2*TINY)
1302        self.identical(fromHex('0xap-1076'), 2*TINY)
1303        self.identical(fromHex('0Xbp-1076'), 3*TINY)
1304        self.identical(fromHex('0xcp-1076'), 3*TINY)
1305        self.identical(fromHex('0Xdp-1076'), 3*TINY)
1306        self.identical(fromHex('0Xep-1076'), 4*TINY)
1307        self.identical(fromHex('0xfp-1076'), 4*TINY)
1308        self.identical(fromHex('0x10p-1076'), 4*TINY)
1309        self.identical(fromHex('-0x1p-1076'), -0.0)
1310        self.identical(fromHex('-0X2p-1076'), -0.0)
1311        self.identical(fromHex('-0x3p-1076'), -TINY)
1312        self.identical(fromHex('-0X4p-1076'), -TINY)
1313        self.identical(fromHex('-0x5p-1076'), -TINY)
1314        self.identical(fromHex('-0x6p-1076'), -2*TINY)
1315        self.identical(fromHex('-0X7p-1076'), -2*TINY)
1316        self.identical(fromHex('-0X8p-1076'), -2*TINY)
1317        self.identical(fromHex('-0X9p-1076'), -2*TINY)
1318        self.identical(fromHex('-0Xap-1076'), -2*TINY)
1319        self.identical(fromHex('-0xbp-1076'), -3*TINY)
1320        self.identical(fromHex('-0xcp-1076'), -3*TINY)
1321        self.identical(fromHex('-0Xdp-1076'), -3*TINY)
1322        self.identical(fromHex('-0xep-1076'), -4*TINY)
1323        self.identical(fromHex('-0Xfp-1076'), -4*TINY)
1324        self.identical(fromHex('-0X10p-1076'), -4*TINY)
1325
1326        # ... and near MIN ...
1327        self.identical(fromHex('0x0.ffffffffffffd6p-1022'), MIN-3*TINY)
1328        self.identical(fromHex('0x0.ffffffffffffd8p-1022'), MIN-2*TINY)
1329        self.identical(fromHex('0x0.ffffffffffffdap-1022'), MIN-2*TINY)
1330        self.identical(fromHex('0x0.ffffffffffffdcp-1022'), MIN-2*TINY)
1331        self.identical(fromHex('0x0.ffffffffffffdep-1022'), MIN-2*TINY)
1332        self.identical(fromHex('0x0.ffffffffffffe0p-1022'), MIN-2*TINY)
1333        self.identical(fromHex('0x0.ffffffffffffe2p-1022'), MIN-2*TINY)
1334        self.identical(fromHex('0x0.ffffffffffffe4p-1022'), MIN-2*TINY)
1335        self.identical(fromHex('0x0.ffffffffffffe6p-1022'), MIN-2*TINY)
1336        self.identical(fromHex('0x0.ffffffffffffe8p-1022'), MIN-2*TINY)
1337        self.identical(fromHex('0x0.ffffffffffffeap-1022'), MIN-TINY)
1338        self.identical(fromHex('0x0.ffffffffffffecp-1022'), MIN-TINY)
1339        self.identical(fromHex('0x0.ffffffffffffeep-1022'), MIN-TINY)
1340        self.identical(fromHex('0x0.fffffffffffff0p-1022'), MIN-TINY)
1341        self.identical(fromHex('0x0.fffffffffffff2p-1022'), MIN-TINY)
1342        self.identical(fromHex('0x0.fffffffffffff4p-1022'), MIN-TINY)
1343        self.identical(fromHex('0x0.fffffffffffff6p-1022'), MIN-TINY)
1344        self.identical(fromHex('0x0.fffffffffffff8p-1022'), MIN)
1345        self.identical(fromHex('0x0.fffffffffffffap-1022'), MIN)
1346        self.identical(fromHex('0x0.fffffffffffffcp-1022'), MIN)
1347        self.identical(fromHex('0x0.fffffffffffffep-1022'), MIN)
1348        self.identical(fromHex('0x1.00000000000000p-1022'), MIN)
1349        self.identical(fromHex('0x1.00000000000002p-1022'), MIN)
1350        self.identical(fromHex('0x1.00000000000004p-1022'), MIN)
1351        self.identical(fromHex('0x1.00000000000006p-1022'), MIN)
1352        self.identical(fromHex('0x1.00000000000008p-1022'), MIN)
1353        self.identical(fromHex('0x1.0000000000000ap-1022'), MIN+TINY)
1354        self.identical(fromHex('0x1.0000000000000cp-1022'), MIN+TINY)
1355        self.identical(fromHex('0x1.0000000000000ep-1022'), MIN+TINY)
1356        self.identical(fromHex('0x1.00000000000010p-1022'), MIN+TINY)
1357        self.identical(fromHex('0x1.00000000000012p-1022'), MIN+TINY)
1358        self.identical(fromHex('0x1.00000000000014p-1022'), MIN+TINY)
1359        self.identical(fromHex('0x1.00000000000016p-1022'), MIN+TINY)
1360        self.identical(fromHex('0x1.00000000000018p-1022'), MIN+2*TINY)
1361
1362        # ... and near 1.0.
1363        self.identical(fromHex('0x0.fffffffffffff0p0'), 1.0-EPS)
1364        self.identical(fromHex('0x0.fffffffffffff1p0'), 1.0-EPS)
1365        self.identical(fromHex('0X0.fffffffffffff2p0'), 1.0-EPS)
1366        self.identical(fromHex('0x0.fffffffffffff3p0'), 1.0-EPS)
1367        self.identical(fromHex('0X0.fffffffffffff4p0'), 1.0-EPS)
1368        self.identical(fromHex('0X0.fffffffffffff5p0'), 1.0-EPS/2)
1369        self.identical(fromHex('0X0.fffffffffffff6p0'), 1.0-EPS/2)
1370        self.identical(fromHex('0x0.fffffffffffff7p0'), 1.0-EPS/2)
1371        self.identical(fromHex('0x0.fffffffffffff8p0'), 1.0-EPS/2)
1372        self.identical(fromHex('0X0.fffffffffffff9p0'), 1.0-EPS/2)
1373        self.identical(fromHex('0X0.fffffffffffffap0'), 1.0-EPS/2)
1374        self.identical(fromHex('0x0.fffffffffffffbp0'), 1.0-EPS/2)
1375        self.identical(fromHex('0X0.fffffffffffffcp0'), 1.0)
1376        self.identical(fromHex('0x0.fffffffffffffdp0'), 1.0)
1377        self.identical(fromHex('0X0.fffffffffffffep0'), 1.0)
1378        self.identical(fromHex('0x0.ffffffffffffffp0'), 1.0)
1379        self.identical(fromHex('0X1.00000000000000p0'), 1.0)
1380        self.identical(fromHex('0X1.00000000000001p0'), 1.0)
1381        self.identical(fromHex('0x1.00000000000002p0'), 1.0)
1382        self.identical(fromHex('0X1.00000000000003p0'), 1.0)
1383        self.identical(fromHex('0x1.00000000000004p0'), 1.0)
1384        self.identical(fromHex('0X1.00000000000005p0'), 1.0)
1385        self.identical(fromHex('0X1.00000000000006p0'), 1.0)
1386        self.identical(fromHex('0X1.00000000000007p0'), 1.0)
1387        self.identical(fromHex('0x1.00000000000007ffffffffffffffffffffp0'),
1388                       1.0)
1389        self.identical(fromHex('0x1.00000000000008p0'), 1.0)
1390        self.identical(fromHex('0x1.00000000000008000000000000000001p0'),
1391                       1+EPS)
1392        self.identical(fromHex('0X1.00000000000009p0'), 1.0+EPS)
1393        self.identical(fromHex('0x1.0000000000000ap0'), 1.0+EPS)
1394        self.identical(fromHex('0x1.0000000000000bp0'), 1.0+EPS)
1395        self.identical(fromHex('0X1.0000000000000cp0'), 1.0+EPS)
1396        self.identical(fromHex('0x1.0000000000000dp0'), 1.0+EPS)
1397        self.identical(fromHex('0x1.0000000000000ep0'), 1.0+EPS)
1398        self.identical(fromHex('0X1.0000000000000fp0'), 1.0+EPS)
1399        self.identical(fromHex('0x1.00000000000010p0'), 1.0+EPS)
1400        self.identical(fromHex('0X1.00000000000011p0'), 1.0+EPS)
1401        self.identical(fromHex('0x1.00000000000012p0'), 1.0+EPS)
1402        self.identical(fromHex('0X1.00000000000013p0'), 1.0+EPS)
1403        self.identical(fromHex('0X1.00000000000014p0'), 1.0+EPS)
1404        self.identical(fromHex('0x1.00000000000015p0'), 1.0+EPS)
1405        self.identical(fromHex('0x1.00000000000016p0'), 1.0+EPS)
1406        self.identical(fromHex('0X1.00000000000017p0'), 1.0+EPS)
1407        self.identical(fromHex('0x1.00000000000017ffffffffffffffffffffp0'),
1408                       1.0+EPS)
1409        self.identical(fromHex('0x1.00000000000018p0'), 1.0+2*EPS)
1410        self.identical(fromHex('0X1.00000000000018000000000000000001p0'),
1411                       1.0+2*EPS)
1412        self.identical(fromHex('0x1.00000000000019p0'), 1.0+2*EPS)
1413        self.identical(fromHex('0X1.0000000000001ap0'), 1.0+2*EPS)
1414        self.identical(fromHex('0X1.0000000000001bp0'), 1.0+2*EPS)
1415        self.identical(fromHex('0x1.0000000000001cp0'), 1.0+2*EPS)
1416        self.identical(fromHex('0x1.0000000000001dp0'), 1.0+2*EPS)
1417        self.identical(fromHex('0x1.0000000000001ep0'), 1.0+2*EPS)
1418        self.identical(fromHex('0X1.0000000000001fp0'), 1.0+2*EPS)
1419        self.identical(fromHex('0x1.00000000000020p0'), 1.0+2*EPS)
1420
1421    def test_roundtrip(self):
1422        def roundtrip(x):
1423            return fromHex(toHex(x))
1424
1425        for x in [NAN, INF, self.MAX, self.MIN, self.MIN-self.TINY, self.TINY, 0.0]:
1426            self.identical(x, roundtrip(x))
1427            self.identical(-x, roundtrip(-x))
1428
1429        # fromHex(toHex(x)) should exactly recover x, for any non-NaN float x.
1430        import random
1431        for i in xrange(10000):
1432            e = random.randrange(-1200, 1200)
1433            m = random.random()
1434            s = random.choice([1.0, -1.0])
1435            try:
1436                x = s*ldexp(m, e)
1437            except OverflowError:
1438                pass
1439            else:
1440                self.identical(x, fromHex(toHex(x)))
1441
1442
1443def test_main():
1444    test_support.run_unittest(
1445        GeneralFloatCases,
1446        FormatFunctionsTestCase,
1447        UnknownFormatTestCase,
1448        IEEEFormatTestCase,
1449        ReprTestCase,
1450        RoundTestCase,
1451        InfNanTest,
1452        HexFloatTestCase,
1453        )
1454
1455if __name__ == '__main__':
1456    test_main()
1457