blob: 0f984a5ca20c999009a2da6dda84ed00b923fda7 [file] [log] [blame]
Michael W. Hudsonba283e22005-05-27 15:23:20 +00001
2import unittest, struct
Christian Heimes827b35c2007-12-10 22:19:17 +00003import os
Eric Smith0923d1d2009-04-16 20:16:10 +00004import sys
Benjamin Petersonee8712c2008-05-20 21:35:26 +00005from test import support
Christian Heimes53876d92008-04-19 00:31:39 +00006import math
Mark Dickinson65fe25e2008-07-16 11:30:51 +00007from math import isinf, isnan, copysign, ldexp
Christian Heimes53876d92008-04-19 00:31:39 +00008import operator
Amaury Forgeot d'Arc7e958d12008-09-06 21:03:22 +00009import random, fractions
Michael W. Hudsonba283e22005-05-27 15:23:20 +000010
Christian Heimes53876d92008-04-19 00:31:39 +000011INF = float("inf")
12NAN = float("nan")
Christian Heimes99170a52007-12-19 02:07:34 +000013
Eric Smith0923d1d2009-04-16 20:16:10 +000014#locate file with float format test values
15test_dir = os.path.dirname(__file__) or os.curdir
16format_testfile = os.path.join(test_dir, 'formatfloat_testcases.txt')
17
Christian Heimes81ee3ef2008-05-04 22:42:01 +000018class GeneralFloatCases(unittest.TestCase):
19
20 def test_float(self):
21 self.assertEqual(float(3.14), 3.14)
22 self.assertEqual(float(314), 314.0)
23 self.assertEqual(float(" 3.14 "), 3.14)
Amaury Forgeot d'Arc7e958d12008-09-06 21:03:22 +000024 self.assertEqual(float(b" 3.14 "), 3.14)
Christian Heimes81ee3ef2008-05-04 22:42:01 +000025 self.assertRaises(ValueError, float, " 0x3.1 ")
26 self.assertRaises(ValueError, float, " -0x3.p-1 ")
27 self.assertRaises(ValueError, float, " +0x3.p-1 ")
28 self.assertRaises(ValueError, float, "++3.14")
29 self.assertRaises(ValueError, float, "+-3.14")
30 self.assertRaises(ValueError, float, "-+3.14")
31 self.assertRaises(ValueError, float, "--3.14")
Eric Smith0923d1d2009-04-16 20:16:10 +000032 self.assertRaises(ValueError, float, ".nan")
33 self.assertRaises(ValueError, float, "+.inf")
34 self.assertRaises(ValueError, float, ".")
35 self.assertRaises(ValueError, float, "-.")
Amaury Forgeot d'Arc7e958d12008-09-06 21:03:22 +000036 self.assertEqual(float(b" \u0663.\u0661\u0664 ".decode('raw-unicode-escape')), 3.14)
Mark Dickinsona9023be2009-10-27 22:12:20 +000037 # extra long strings should not be a problem
38 float(b'.' + b'1'*1000)
39 float('.' + '1'*1000)
Christian Heimes81ee3ef2008-05-04 22:42:01 +000040
Benjamin Petersonee8712c2008-05-20 21:35:26 +000041 @support.run_with_locale('LC_NUMERIC', 'fr_FR', 'de_DE')
Christian Heimes81ee3ef2008-05-04 22:42:01 +000042 def test_float_with_comma(self):
43 # set locale to something that doesn't use '.' for the decimal point
44 # float must not accept the locale specific decimal point but
45 # it still has to accept the normal python syntac
46 import locale
47 if not locale.localeconv()['decimal_point'] == ',':
48 return
49
50 self.assertEqual(float(" 3.14 "), 3.14)
51 self.assertEqual(float("+3.14 "), 3.14)
52 self.assertEqual(float("-3.14 "), -3.14)
53 self.assertEqual(float(".14 "), .14)
54 self.assertEqual(float("3. "), 3.0)
55 self.assertEqual(float("3.e3 "), 3000.0)
56 self.assertEqual(float("3.2e3 "), 3200.0)
57 self.assertEqual(float("2.5e-1 "), 0.25)
58 self.assertEqual(float("5e-1"), 0.5)
59 self.assertRaises(ValueError, float, " 3,14 ")
60 self.assertRaises(ValueError, float, " +3,14 ")
61 self.assertRaises(ValueError, float, " -3,14 ")
62 self.assertRaises(ValueError, float, " 0x3.1 ")
63 self.assertRaises(ValueError, float, " -0x3.p-1 ")
64 self.assertRaises(ValueError, float, " +0x3.p-1 ")
65 self.assertEqual(float(" 25.e-1 "), 2.5)
Benjamin Petersond43029b2008-09-06 23:33:21 +000066 self.assertEqual(support.fcmp(float(" .25e-1 "), .025), 0)
Christian Heimes81ee3ef2008-05-04 22:42:01 +000067
68 def test_floatconversion(self):
69 # Make sure that calls to __float__() work properly
70 class Foo0:
71 def __float__(self):
72 return 42.
73
74 class Foo1(object):
75 def __float__(self):
76 return 42.
77
78 class Foo2(float):
79 def __float__(self):
80 return 42.
81
82 class Foo3(float):
83 def __new__(cls, value=0.):
84 return float.__new__(cls, 2*value)
85
86 def __float__(self):
87 return self
88
89 class Foo4(float):
90 def __float__(self):
91 return 42
92
Benjamin Peterson2808d3c2009-04-15 21:34:27 +000093 # Issue 5759: __float__ not called on str subclasses (though it is on
94 # unicode subclasses).
95 class FooStr(str):
96 def __float__(self):
97 return float(str(self)) + 1
98
Christian Heimes81ee3ef2008-05-04 22:42:01 +000099 self.assertAlmostEqual(float(Foo0()), 42.)
100 self.assertAlmostEqual(float(Foo1()), 42.)
101 self.assertAlmostEqual(float(Foo2()), 42.)
102 self.assertAlmostEqual(float(Foo3(21)), 42.)
103 self.assertRaises(TypeError, float, Foo4(42))
Benjamin Peterson2808d3c2009-04-15 21:34:27 +0000104 self.assertAlmostEqual(float(FooStr('8')), 9.)
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000105
106 def test_floatasratio(self):
107 for f, ratio in [
108 (0.875, (7, 8)),
109 (-0.875, (-7, 8)),
110 (0.0, (0, 1)),
111 (11.5, (23, 2)),
112 ]:
113 self.assertEqual(f.as_integer_ratio(), ratio)
114
115 for i in range(10000):
116 f = random.random()
117 f *= 10 ** random.randint(-100, 100)
118 n, d = f.as_integer_ratio()
119 self.assertEqual(float(n).__truediv__(d), f)
120
121 R = fractions.Fraction
122 self.assertEqual(R(0, 1),
123 R(*float(0.0).as_integer_ratio()))
124 self.assertEqual(R(5, 2),
125 R(*float(2.5).as_integer_ratio()))
126 self.assertEqual(R(1, 2),
127 R(*float(0.5).as_integer_ratio()))
128 self.assertEqual(R(4728779608739021, 2251799813685248),
129 R(*float(2.1).as_integer_ratio()))
130 self.assertEqual(R(-4728779608739021, 2251799813685248),
131 R(*float(-2.1).as_integer_ratio()))
132 self.assertEqual(R(-2100, 1),
133 R(*float(-2100.0).as_integer_ratio()))
134
135 self.assertRaises(OverflowError, float('inf').as_integer_ratio)
136 self.assertRaises(OverflowError, float('-inf').as_integer_ratio)
137 self.assertRaises(ValueError, float('nan').as_integer_ratio)
138
Mark Dickinson4a1f5932008-11-12 23:23:36 +0000139 def test_float_containment(self):
140 floats = (INF, -INF, 0.0, 1.0, NAN)
141 for f in floats:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000142 self.assertTrue(f in [f], "'%r' not in []" % f)
143 self.assertTrue(f in (f,), "'%r' not in ()" % f)
144 self.assertTrue(f in {f}, "'%r' not in set()" % f)
145 self.assertTrue(f in {f: None}, "'%r' not in {}" % f)
Mark Dickinson4a1f5932008-11-12 23:23:36 +0000146 self.assertEqual([f].count(f), 1, "[].count('%r') != 1" % f)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000147 self.assertTrue(f in floats, "'%r' not in container" % f)
Mark Dickinson4a1f5932008-11-12 23:23:36 +0000148
149 for f in floats:
150 # nonidentical containers, same type, same contents
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000151 self.assertTrue([f] == [f], "[%r] != [%r]" % (f, f))
152 self.assertTrue((f,) == (f,), "(%r,) != (%r,)" % (f, f))
153 self.assertTrue({f} == {f}, "{%r} != {%r}" % (f, f))
154 self.assertTrue({f : None} == {f: None}, "{%r : None} != "
Mark Dickinson4a1f5932008-11-12 23:23:36 +0000155 "{%r : None}" % (f, f))
156
157 # identical containers
158 l, t, s, d = [f], (f,), {f}, {f: None}
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000159 self.assertTrue(l == l, "[%r] not equal to itself" % f)
160 self.assertTrue(t == t, "(%r,) not equal to itself" % f)
161 self.assertTrue(s == s, "{%r} not equal to itself" % f)
162 self.assertTrue(d == d, "{%r : None} not equal to itself" % f)
Mark Dickinson4a1f5932008-11-12 23:23:36 +0000163
164
165
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000166class FormatFunctionsTestCase(unittest.TestCase):
167
168 def setUp(self):
169 self.save_formats = {'double':float.__getformat__('double'),
170 'float':float.__getformat__('float')}
171
172 def tearDown(self):
173 float.__setformat__('double', self.save_formats['double'])
174 float.__setformat__('float', self.save_formats['float'])
175
176 def test_getformat(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000177 self.assertTrue(float.__getformat__('double') in
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000178 ['unknown', 'IEEE, big-endian', 'IEEE, little-endian'])
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000179 self.assertTrue(float.__getformat__('float') in
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000180 ['unknown', 'IEEE, big-endian', 'IEEE, little-endian'])
181 self.assertRaises(ValueError, float.__getformat__, 'chicken')
182 self.assertRaises(TypeError, float.__getformat__, 1)
183
184 def test_setformat(self):
185 for t in 'double', 'float':
186 float.__setformat__(t, 'unknown')
187 if self.save_formats[t] == 'IEEE, big-endian':
188 self.assertRaises(ValueError, float.__setformat__,
189 t, 'IEEE, little-endian')
190 elif self.save_formats[t] == 'IEEE, little-endian':
191 self.assertRaises(ValueError, float.__setformat__,
192 t, 'IEEE, big-endian')
193 else:
194 self.assertRaises(ValueError, float.__setformat__,
195 t, 'IEEE, big-endian')
196 self.assertRaises(ValueError, float.__setformat__,
197 t, 'IEEE, little-endian')
198 self.assertRaises(ValueError, float.__setformat__,
199 t, 'chicken')
200 self.assertRaises(ValueError, float.__setformat__,
201 'chicken', 'unknown')
202
Guido van Rossum2be161d2007-05-15 20:43:51 +0000203BE_DOUBLE_INF = b'\x7f\xf0\x00\x00\x00\x00\x00\x00'
Guido van Rossum254348e2007-11-21 19:29:53 +0000204LE_DOUBLE_INF = bytes(reversed(BE_DOUBLE_INF))
Guido van Rossum2be161d2007-05-15 20:43:51 +0000205BE_DOUBLE_NAN = b'\x7f\xf8\x00\x00\x00\x00\x00\x00'
Guido van Rossum254348e2007-11-21 19:29:53 +0000206LE_DOUBLE_NAN = bytes(reversed(BE_DOUBLE_NAN))
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000207
Guido van Rossum2be161d2007-05-15 20:43:51 +0000208BE_FLOAT_INF = b'\x7f\x80\x00\x00'
Guido van Rossum254348e2007-11-21 19:29:53 +0000209LE_FLOAT_INF = bytes(reversed(BE_FLOAT_INF))
Guido van Rossum2be161d2007-05-15 20:43:51 +0000210BE_FLOAT_NAN = b'\x7f\xc0\x00\x00'
Guido van Rossum254348e2007-11-21 19:29:53 +0000211LE_FLOAT_NAN = bytes(reversed(BE_FLOAT_NAN))
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000212
213# on non-IEEE platforms, attempting to unpack a bit pattern
214# representing an infinity or a NaN should raise an exception.
215
216class UnknownFormatTestCase(unittest.TestCase):
217 def setUp(self):
218 self.save_formats = {'double':float.__getformat__('double'),
219 'float':float.__getformat__('float')}
220 float.__setformat__('double', 'unknown')
221 float.__setformat__('float', 'unknown')
Tim Peters5d36a552005-06-03 22:40:27 +0000222
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000223 def tearDown(self):
224 float.__setformat__('double', self.save_formats['double'])
225 float.__setformat__('float', self.save_formats['float'])
226
227 def test_double_specials_dont_unpack(self):
228 for fmt, data in [('>d', BE_DOUBLE_INF),
229 ('>d', BE_DOUBLE_NAN),
230 ('<d', LE_DOUBLE_INF),
231 ('<d', LE_DOUBLE_NAN)]:
232 self.assertRaises(ValueError, struct.unpack, fmt, data)
233
234 def test_float_specials_dont_unpack(self):
235 for fmt, data in [('>f', BE_FLOAT_INF),
236 ('>f', BE_FLOAT_NAN),
237 ('<f', LE_FLOAT_INF),
238 ('<f', LE_FLOAT_NAN)]:
239 self.assertRaises(ValueError, struct.unpack, fmt, data)
240
241
242# on an IEEE platform, all we guarantee is that bit patterns
243# representing infinities or NaNs do not raise an exception; all else
244# is accident (today).
Guido van Rossum04110fb2007-08-24 16:32:05 +0000245# let's also try to guarantee that -0.0 and 0.0 don't get confused.
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000246
247class IEEEFormatTestCase(unittest.TestCase):
248 if float.__getformat__("double").startswith("IEEE"):
249 def test_double_specials_do_unpack(self):
250 for fmt, data in [('>d', BE_DOUBLE_INF),
251 ('>d', BE_DOUBLE_NAN),
252 ('<d', LE_DOUBLE_INF),
253 ('<d', LE_DOUBLE_NAN)]:
254 struct.unpack(fmt, data)
255
256 if float.__getformat__("float").startswith("IEEE"):
257 def test_float_specials_do_unpack(self):
258 for fmt, data in [('>f', BE_FLOAT_INF),
259 ('>f', BE_FLOAT_NAN),
260 ('<f', LE_FLOAT_INF),
261 ('<f', LE_FLOAT_NAN)]:
262 struct.unpack(fmt, data)
263
Guido van Rossum04110fb2007-08-24 16:32:05 +0000264 if float.__getformat__("double").startswith("IEEE"):
265 def test_negative_zero(self):
266 import math
267 def pos_pos():
268 return 0.0, math.atan2(0.0, -1)
269 def pos_neg():
270 return 0.0, math.atan2(-0.0, -1)
271 def neg_pos():
272 return -0.0, math.atan2(0.0, -1)
273 def neg_neg():
274 return -0.0, math.atan2(-0.0, -1)
275 self.assertEquals(pos_pos(), neg_pos())
276 self.assertEquals(pos_neg(), neg_neg())
277
Eric Smith8c663262007-08-25 02:26:07 +0000278class FormatTestCase(unittest.TestCase):
Eric Smith11fe3e02007-08-31 01:33:06 +0000279 def test_format(self):
Eric Smith8c663262007-08-25 02:26:07 +0000280 # these should be rewritten to use both format(x, spec) and
281 # x.__format__(spec)
282
283 self.assertEqual(format(0.0, 'f'), '0.000000')
284
285 # the default is 'g', except for empty format spec
286 self.assertEqual(format(0.0, ''), '0.0')
287 self.assertEqual(format(0.01, ''), '0.01')
288 self.assertEqual(format(0.01, 'g'), '0.01')
289
Eric Smith63376222009-05-05 14:04:18 +0000290 # empty presentation type should format in the same way as str
291 # (issue 5920)
292 x = 100/7.
293 self.assertEqual(format(x, ''), str(x))
294 self.assertEqual(format(x, '-'), str(x))
295 self.assertEqual(format(x, '>'), str(x))
296 self.assertEqual(format(x, '2'), str(x))
Eric Smith8c663262007-08-25 02:26:07 +0000297
298 self.assertEqual(format(1.0, 'f'), '1.000000')
Eric Smith8c663262007-08-25 02:26:07 +0000299
300 self.assertEqual(format(-1.0, 'f'), '-1.000000')
Eric Smith8c663262007-08-25 02:26:07 +0000301
302 self.assertEqual(format( 1.0, ' f'), ' 1.000000')
303 self.assertEqual(format(-1.0, ' f'), '-1.000000')
304 self.assertEqual(format( 1.0, '+f'), '+1.000000')
305 self.assertEqual(format(-1.0, '+f'), '-1.000000')
306
307 # % formatting
308 self.assertEqual(format(-1.0, '%'), '-100.000000%')
309
310 # conversion to string should fail
311 self.assertRaises(ValueError, format, 3.0, "s")
312
Eric Smith7b69c6c2008-01-27 21:07:59 +0000313 # other format specifiers shouldn't work on floats,
314 # in particular int specifiers
315 for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
316 [chr(x) for x in range(ord('A'), ord('Z')+1)]):
317 if not format_spec in 'eEfFgGn%':
318 self.assertRaises(ValueError, format, 0.0, format_spec)
319 self.assertRaises(ValueError, format, 1.0, format_spec)
320 self.assertRaises(ValueError, format, -1.0, format_spec)
321 self.assertRaises(ValueError, format, 1e100, format_spec)
322 self.assertRaises(ValueError, format, -1e100, format_spec)
323 self.assertRaises(ValueError, format, 1e-100, format_spec)
324 self.assertRaises(ValueError, format, -1e-100, format_spec)
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000325
Eric Smith741191f2009-05-06 13:08:15 +0000326 # issue 3382
327 self.assertEqual(format(NAN, 'f'), 'nan')
328 self.assertEqual(format(NAN, 'F'), 'NAN')
329 self.assertEqual(format(INF, 'f'), 'inf')
330 self.assertEqual(format(INF, 'F'), 'INF')
331
Eric Smith0923d1d2009-04-16 20:16:10 +0000332 @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
333 "test requires IEEE 754 doubles")
334 def test_format_testfile(self):
335 for line in open(format_testfile):
336 if line.startswith('--'):
337 continue
338 line = line.strip()
339 if not line:
340 continue
341
342 lhs, rhs = map(str.strip, line.split('->'))
343 fmt, arg = lhs.split()
344 self.assertEqual(fmt % float(arg), rhs)
345 self.assertEqual(fmt % -float(arg), '-' + rhs)
346
Mark Dickinsond3ca5572009-04-29 18:47:07 +0000347 def test_issue5864(self):
348 self.assertEquals(format(123.456, '.4'), '123.5')
349 self.assertEquals(format(1234.56, '.4'), '1.235e+03')
350 self.assertEquals(format(12345.6, '.4'), '1.235e+04')
351
Mark Dickinson7efad9e2009-04-17 20:59:58 +0000352class ReprTestCase(unittest.TestCase):
353 def test_repr(self):
354 floats_file = open(os.path.join(os.path.split(__file__)[0],
355 'floating_points.txt'))
356 for line in floats_file:
357 line = line.strip()
358 if not line or line.startswith('#'):
359 continue
360 v = eval(line)
361 self.assertEqual(v, eval(repr(v)))
362 floats_file.close()
363
Eric Smith0923d1d2009-04-16 20:16:10 +0000364 @unittest.skipUnless(getattr(sys, 'float_repr_style', '') == 'short',
365 "applies only when using short float repr style")
366 def test_short_repr(self):
367 # test short float repr introduced in Python 3.1. One aspect
368 # of this repr is that we get some degree of str -> float ->
369 # str roundtripping. In particular, for any numeric string
370 # containing 15 or fewer significant digits, those exact same
371 # digits (modulo trailing zeros) should appear in the output.
372 # No more repr(0.03) -> "0.029999999999999999"!
373
374 test_strings = [
375 # output always includes *either* a decimal point and at
376 # least one digit after that point, or an exponent.
377 '0.0',
378 '1.0',
379 '0.01',
380 '0.02',
381 '0.03',
382 '0.04',
383 '0.05',
384 '1.23456789',
385 '10.0',
386 '100.0',
387 # values >= 1e16 get an exponent...
388 '1000000000000000.0',
389 '9999999999999990.0',
390 '1e+16',
391 '1e+17',
392 # ... and so do values < 1e-4
393 '0.001',
394 '0.001001',
395 '0.00010000000000001',
396 '0.0001',
397 '9.999999999999e-05',
398 '1e-05',
399 # values designed to provoke failure if the FPU rounding
400 # precision isn't set correctly
401 '8.72293771110361e+25',
402 '7.47005307342313e+26',
403 '2.86438000439698e+28',
404 '8.89142905246179e+28',
405 '3.08578087079232e+35',
406 ]
407
408 for s in test_strings:
409 negs = '-'+s
410 self.assertEqual(s, repr(float(s)))
411 self.assertEqual(negs, repr(float(negs)))
412
Mark Dickinsone6a076d2009-04-18 11:48:33 +0000413class RoundTestCase(unittest.TestCase):
414 @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
415 "test requires IEEE 754 doubles")
416 def test_inf_nan(self):
417 self.assertRaises(OverflowError, round, INF)
418 self.assertRaises(OverflowError, round, -INF)
419 self.assertRaises(ValueError, round, NAN)
Mark Dickinson4ca33d12009-11-24 10:59:34 +0000420 self.assertRaises(TypeError, round, INF, 0.0)
421 self.assertRaises(TypeError, round, -INF, 1.0)
422 self.assertRaises(TypeError, round, NAN, "ceci n'est pas un integer")
423 self.assertRaises(TypeError, round, -0.0, 1j)
Mark Dickinsone6a076d2009-04-18 11:48:33 +0000424
425 @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
426 "test requires IEEE 754 doubles")
427 def test_large_n(self):
428 for n in [324, 325, 400, 2**31-1, 2**31, 2**32, 2**100]:
429 self.assertEqual(round(123.456, n), 123.456)
430 self.assertEqual(round(-123.456, n), -123.456)
431 self.assertEqual(round(1e300, n), 1e300)
432 self.assertEqual(round(1e-320, n), 1e-320)
433 self.assertEqual(round(1e150, 300), 1e150)
434 self.assertEqual(round(1e300, 307), 1e300)
435 self.assertEqual(round(-3.1415, 308), -3.1415)
436 self.assertEqual(round(1e150, 309), 1e150)
437 self.assertEqual(round(1.4e-315, 315), 1e-315)
438
439 @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
440 "test requires IEEE 754 doubles")
441 def test_small_n(self):
442 for n in [-308, -309, -400, 1-2**31, -2**31, -2**31-1, -2**100]:
443 self.assertEqual(round(123.456, n), 0.0)
444 self.assertEqual(round(-123.456, n), -0.0)
445 self.assertEqual(round(1e300, n), 0.0)
446 self.assertEqual(round(1e-320, n), 0.0)
447
448 @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
449 "test requires IEEE 754 doubles")
450 def test_overflow(self):
451 self.assertRaises(OverflowError, round, 1.6e308, -308)
452 self.assertRaises(OverflowError, round, -1.7e308, -308)
453
454 @unittest.skipUnless(getattr(sys, 'float_repr_style', '') == 'short',
455 "applies only when using short float repr style")
456 def test_previous_round_bugs(self):
457 # particular cases that have occurred in bug reports
458 self.assertEqual(round(562949953421312.5, 1),
459 562949953421312.5)
460 self.assertEqual(round(56294995342131.5, 3),
461 56294995342131.5)
462 # round-half-even
463 self.assertEqual(round(25.0, -1), 20.0)
464 self.assertEqual(round(35.0, -1), 40.0)
465 self.assertEqual(round(45.0, -1), 40.0)
466 self.assertEqual(round(55.0, -1), 60.0)
467 self.assertEqual(round(65.0, -1), 60.0)
468 self.assertEqual(round(75.0, -1), 80.0)
469 self.assertEqual(round(85.0, -1), 80.0)
470 self.assertEqual(round(95.0, -1), 100.0)
471
472 @unittest.skipUnless(getattr(sys, 'float_repr_style', '') == 'short',
473 "applies only when using short float repr style")
474 def test_matches_float_format(self):
475 # round should give the same results as float formatting
476 for i in range(500):
477 x = i/1000.
478 self.assertEqual(float(format(x, '.0f')), round(x, 0))
479 self.assertEqual(float(format(x, '.1f')), round(x, 1))
480 self.assertEqual(float(format(x, '.2f')), round(x, 2))
481 self.assertEqual(float(format(x, '.3f')), round(x, 3))
482
483 for i in range(5, 5000, 10):
484 x = i/1000.
485 self.assertEqual(float(format(x, '.0f')), round(x, 0))
486 self.assertEqual(float(format(x, '.1f')), round(x, 1))
487 self.assertEqual(float(format(x, '.2f')), round(x, 2))
488 self.assertEqual(float(format(x, '.3f')), round(x, 3))
489
490 for i in range(500):
491 x = random.random()
492 self.assertEqual(float(format(x, '.0f')), round(x, 0))
493 self.assertEqual(float(format(x, '.1f')), round(x, 1))
494 self.assertEqual(float(format(x, '.2f')), round(x, 2))
495 self.assertEqual(float(format(x, '.3f')), round(x, 3))
496
497
Eric Smith8a10ecc2009-12-02 17:58:24 +0000498 @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
499 "test requires IEEE 754 doubles")
500 def test_format_specials(self):
501 # Test formatting of nans and infs.
502
503 def test(fmt, value, expected):
504 # Test with both % and format().
505 self.assertEqual(fmt % value, expected, fmt)
506 if not '#' in fmt:
507 # Until issue 7094 is implemented, format() for floats doesn't
508 # support '#' formatting
509 fmt = fmt[1:] # strip off the %
510 self.assertEqual(format(value, fmt), expected, fmt)
511
512 for fmt in ['%e', '%f', '%g', '%.0e', '%.6f', '%.20g',
513 '%#e', '%#f', '%#g', '%#.20e', '%#.15f', '%#.3g']:
514 pfmt = '%+' + fmt[1:]
515 sfmt = '% ' + fmt[1:]
516 test(fmt, INF, 'inf')
517 test(fmt, -INF, '-inf')
518 test(fmt, NAN, 'nan')
519 test(fmt, -NAN, 'nan')
520 # When asking for a sign, it's always provided. nans are
521 # always positive.
522 test(pfmt, INF, '+inf')
523 test(pfmt, -INF, '-inf')
524 test(pfmt, NAN, '+nan')
525 test(pfmt, -NAN, '+nan')
526 # When using ' ' for a sign code, only infs can be negative.
527 # Others have a space.
528 test(sfmt, INF, ' inf')
529 test(sfmt, -INF, '-inf')
530 test(sfmt, NAN, ' nan')
531 test(sfmt, -NAN, ' nan')
532
Mark Dickinsone6a076d2009-04-18 11:48:33 +0000533
Christian Heimes99170a52007-12-19 02:07:34 +0000534# Beginning with Python 2.6 float has cross platform compatible
Georg Brandl2ee470f2008-07-16 12:55:28 +0000535# ways to create and represent inf and nan
Christian Heimes99170a52007-12-19 02:07:34 +0000536class InfNanTest(unittest.TestCase):
537 def test_inf_from_str(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000538 self.assertTrue(isinf(float("inf")))
539 self.assertTrue(isinf(float("+inf")))
540 self.assertTrue(isinf(float("-inf")))
541 self.assertTrue(isinf(float("infinity")))
542 self.assertTrue(isinf(float("+infinity")))
543 self.assertTrue(isinf(float("-infinity")))
Christian Heimes99170a52007-12-19 02:07:34 +0000544
545 self.assertEqual(repr(float("inf")), "inf")
546 self.assertEqual(repr(float("+inf")), "inf")
547 self.assertEqual(repr(float("-inf")), "-inf")
Georg Brandl2ee470f2008-07-16 12:55:28 +0000548 self.assertEqual(repr(float("infinity")), "inf")
549 self.assertEqual(repr(float("+infinity")), "inf")
550 self.assertEqual(repr(float("-infinity")), "-inf")
Christian Heimes99170a52007-12-19 02:07:34 +0000551
552 self.assertEqual(repr(float("INF")), "inf")
553 self.assertEqual(repr(float("+Inf")), "inf")
554 self.assertEqual(repr(float("-iNF")), "-inf")
Georg Brandl2ee470f2008-07-16 12:55:28 +0000555 self.assertEqual(repr(float("Infinity")), "inf")
556 self.assertEqual(repr(float("+iNfInItY")), "inf")
557 self.assertEqual(repr(float("-INFINITY")), "-inf")
Christian Heimes99170a52007-12-19 02:07:34 +0000558
559 self.assertEqual(str(float("inf")), "inf")
560 self.assertEqual(str(float("+inf")), "inf")
561 self.assertEqual(str(float("-inf")), "-inf")
Georg Brandl2ee470f2008-07-16 12:55:28 +0000562 self.assertEqual(str(float("infinity")), "inf")
563 self.assertEqual(str(float("+infinity")), "inf")
564 self.assertEqual(str(float("-infinity")), "-inf")
Christian Heimes99170a52007-12-19 02:07:34 +0000565
566 self.assertRaises(ValueError, float, "info")
567 self.assertRaises(ValueError, float, "+info")
568 self.assertRaises(ValueError, float, "-info")
569 self.assertRaises(ValueError, float, "in")
570 self.assertRaises(ValueError, float, "+in")
571 self.assertRaises(ValueError, float, "-in")
Georg Brandl2ee470f2008-07-16 12:55:28 +0000572 self.assertRaises(ValueError, float, "infinit")
573 self.assertRaises(ValueError, float, "+Infin")
574 self.assertRaises(ValueError, float, "-INFI")
575 self.assertRaises(ValueError, float, "infinitys")
Christian Heimes99170a52007-12-19 02:07:34 +0000576
Mark Dickinsonbd16edd2009-05-20 22:05:25 +0000577 self.assertRaises(ValueError, float, "++Inf")
578 self.assertRaises(ValueError, float, "-+inf")
579 self.assertRaises(ValueError, float, "+-infinity")
580 self.assertRaises(ValueError, float, "--Infinity")
581
Christian Heimes99170a52007-12-19 02:07:34 +0000582 def test_inf_as_str(self):
583 self.assertEqual(repr(1e300 * 1e300), "inf")
584 self.assertEqual(repr(-1e300 * 1e300), "-inf")
585
586 self.assertEqual(str(1e300 * 1e300), "inf")
587 self.assertEqual(str(-1e300 * 1e300), "-inf")
588
589 def test_nan_from_str(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000590 self.assertTrue(isnan(float("nan")))
591 self.assertTrue(isnan(float("+nan")))
592 self.assertTrue(isnan(float("-nan")))
Christian Heimes99170a52007-12-19 02:07:34 +0000593
594 self.assertEqual(repr(float("nan")), "nan")
595 self.assertEqual(repr(float("+nan")), "nan")
596 self.assertEqual(repr(float("-nan")), "nan")
597
598 self.assertEqual(repr(float("NAN")), "nan")
599 self.assertEqual(repr(float("+NAn")), "nan")
600 self.assertEqual(repr(float("-NaN")), "nan")
601
602 self.assertEqual(str(float("nan")), "nan")
603 self.assertEqual(str(float("+nan")), "nan")
604 self.assertEqual(str(float("-nan")), "nan")
605
606 self.assertRaises(ValueError, float, "nana")
607 self.assertRaises(ValueError, float, "+nana")
608 self.assertRaises(ValueError, float, "-nana")
609 self.assertRaises(ValueError, float, "na")
610 self.assertRaises(ValueError, float, "+na")
611 self.assertRaises(ValueError, float, "-na")
612
Mark Dickinsonbd16edd2009-05-20 22:05:25 +0000613 self.assertRaises(ValueError, float, "++nan")
614 self.assertRaises(ValueError, float, "-+NAN")
615 self.assertRaises(ValueError, float, "+-NaN")
616 self.assertRaises(ValueError, float, "--nAn")
617
Christian Heimes99170a52007-12-19 02:07:34 +0000618 def test_nan_as_str(self):
619 self.assertEqual(repr(1e300 * 1e300 * 0), "nan")
620 self.assertEqual(repr(-1e300 * 1e300 * 0), "nan")
621
622 self.assertEqual(str(1e300 * 1e300 * 0), "nan")
623 self.assertEqual(str(-1e300 * 1e300 * 0), "nan")
Christian Heimes827b35c2007-12-10 22:19:17 +0000624
Christian Heimes53876d92008-04-19 00:31:39 +0000625 def notest_float_nan(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000626 self.assertTrue(NAN.is_nan())
627 self.assertFalse(INF.is_nan())
628 self.assertFalse((0.).is_nan())
Christian Heimes53876d92008-04-19 00:31:39 +0000629
630 def notest_float_inf(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000631 self.assertTrue(INF.is_inf())
632 self.assertFalse(NAN.is_inf())
633 self.assertFalse((0.).is_inf())
Christian Heimes53876d92008-04-19 00:31:39 +0000634
Mark Dickinson65fe25e2008-07-16 11:30:51 +0000635fromHex = float.fromhex
636toHex = float.hex
637class HexFloatTestCase(unittest.TestCase):
638 MAX = fromHex('0x.fffffffffffff8p+1024') # max normal
639 MIN = fromHex('0x1p-1022') # min normal
640 TINY = fromHex('0x0.0000000000001p-1022') # min subnormal
641 EPS = fromHex('0x0.0000000000001p0') # diff between 1.0 and next float up
642
643 def identical(self, x, y):
644 # check that floats x and y are identical, or that both
645 # are NaNs
646 if isnan(x) or isnan(y):
647 if isnan(x) == isnan(y):
648 return
649 elif x == y and (x != 0.0 or copysign(1.0, x) == copysign(1.0, y)):
650 return
651 self.fail('%r not identical to %r' % (x, y))
652
653 def test_ends(self):
Mark Dickinson38bbc482008-07-16 11:32:23 +0000654 self.identical(self.MIN, ldexp(1.0, -1022))
655 self.identical(self.TINY, ldexp(1.0, -1074))
656 self.identical(self.EPS, ldexp(1.0, -52))
657 self.identical(self.MAX, 2.*(ldexp(1.0, 1023) - ldexp(1.0, 970)))
Mark Dickinson65fe25e2008-07-16 11:30:51 +0000658
659 def test_invalid_inputs(self):
660 invalid_inputs = [
661 'infi', # misspelt infinities and nans
662 '-Infinit',
663 '++inf',
664 '-+Inf',
665 '--nan',
666 '+-NaN',
667 'snan',
668 'NaNs',
669 'nna',
Mark Dickinsond1ec8b22009-05-11 15:45:15 +0000670 'an',
671 'nf',
672 'nfinity',
673 'inity',
674 'iinity',
Mark Dickinson65fe25e2008-07-16 11:30:51 +0000675 '0xnan',
676 '',
677 ' ',
678 'x1.0p0',
679 '0xX1.0p0',
680 '+ 0x1.0p0', # internal whitespace
681 '- 0x1.0p0',
682 '0 x1.0p0',
683 '0x 1.0p0',
684 '0x1 2.0p0',
685 '+0x1 .0p0',
686 '0x1. 0p0',
687 '-0x1.0 1p0',
688 '-0x1.0 p0',
689 '+0x1.0p +0',
690 '0x1.0p -0',
691 '0x1.0p 0',
692 '+0x1.0p+ 0',
693 '-0x1.0p- 0',
694 '++0x1.0p-0', # double signs
695 '--0x1.0p0',
696 '+-0x1.0p+0',
697 '-+0x1.0p0',
698 '0x1.0p++0',
699 '+0x1.0p+-0',
700 '-0x1.0p-+0',
701 '0x1.0p--0',
702 '0x1.0.p0',
703 '0x.p0', # no hex digits before or after point
704 '0x1,p0', # wrong decimal point character
705 '0x1pa',
706 '0x1p\uff10', # fullwidth Unicode digits
707 '\uff10x1p0',
708 '0x\uff11p0',
709 '0x1.\uff10p0',
710 '0x1p0 \n 0x2p0',
711 '0x1p0\0 0x1p0', # embedded null byte is not end of string
712 ]
713 for x in invalid_inputs:
Mark Dickinson589b7952008-08-21 20:05:56 +0000714 try:
715 result = fromHex(x)
716 except ValueError:
717 pass
718 else:
719 self.fail('Expected float.fromhex(%r) to raise ValueError; '
720 'got %r instead' % (x, result))
Mark Dickinson65fe25e2008-07-16 11:30:51 +0000721
722
Mark Dickinsond1ec8b22009-05-11 15:45:15 +0000723 def test_whitespace(self):
724 value_pairs = [
725 ('inf', INF),
726 ('-Infinity', -INF),
727 ('nan', NAN),
728 ('1.0', 1.0),
729 ('-0x.2', -0.125),
730 ('-0.0', -0.0)
731 ]
732 whitespace = [
733 '',
734 ' ',
735 '\t',
736 '\n',
737 '\n \t',
738 '\f',
739 '\v',
740 '\r'
741 ]
742 for inp, expected in value_pairs:
743 for lead in whitespace:
744 for trail in whitespace:
745 got = fromHex(lead + inp + trail)
746 self.identical(got, expected)
747
748
Mark Dickinson65fe25e2008-07-16 11:30:51 +0000749 def test_from_hex(self):
750 MIN = self.MIN;
751 MAX = self.MAX;
752 TINY = self.TINY;
753 EPS = self.EPS;
754
755 # two spellings of infinity, with optional signs; case-insensitive
756 self.identical(fromHex('inf'), INF)
757 self.identical(fromHex('+Inf'), INF)
758 self.identical(fromHex('-INF'), -INF)
759 self.identical(fromHex('iNf'), INF)
760 self.identical(fromHex('Infinity'), INF)
761 self.identical(fromHex('+INFINITY'), INF)
762 self.identical(fromHex('-infinity'), -INF)
763 self.identical(fromHex('-iNFiNitY'), -INF)
764
765 # nans with optional sign; case insensitive
766 self.identical(fromHex('nan'), NAN)
767 self.identical(fromHex('+NaN'), NAN)
768 self.identical(fromHex('-NaN'), NAN)
769 self.identical(fromHex('-nAN'), NAN)
770
771 # variations in input format
772 self.identical(fromHex('1'), 1.0)
773 self.identical(fromHex('+1'), 1.0)
774 self.identical(fromHex('1.'), 1.0)
775 self.identical(fromHex('1.0'), 1.0)
776 self.identical(fromHex('1.0p0'), 1.0)
777 self.identical(fromHex('01'), 1.0)
778 self.identical(fromHex('01.'), 1.0)
779 self.identical(fromHex('0x1'), 1.0)
780 self.identical(fromHex('0x1.'), 1.0)
781 self.identical(fromHex('0x1.0'), 1.0)
782 self.identical(fromHex('+0x1.0'), 1.0)
783 self.identical(fromHex('0x1p0'), 1.0)
784 self.identical(fromHex('0X1p0'), 1.0)
785 self.identical(fromHex('0X1P0'), 1.0)
786 self.identical(fromHex('0x1P0'), 1.0)
787 self.identical(fromHex('0x1.p0'), 1.0)
788 self.identical(fromHex('0x1.0p0'), 1.0)
789 self.identical(fromHex('0x.1p4'), 1.0)
790 self.identical(fromHex('0x.1p04'), 1.0)
791 self.identical(fromHex('0x.1p004'), 1.0)
792 self.identical(fromHex('0x1p+0'), 1.0)
793 self.identical(fromHex('0x1P-0'), 1.0)
794 self.identical(fromHex('+0x1p0'), 1.0)
795 self.identical(fromHex('0x01p0'), 1.0)
796 self.identical(fromHex('0x1p00'), 1.0)
797 self.identical(fromHex(' 0x1p0 '), 1.0)
798 self.identical(fromHex('\n 0x1p0'), 1.0)
799 self.identical(fromHex('0x1p0 \t'), 1.0)
800 self.identical(fromHex('0xap0'), 10.0)
801 self.identical(fromHex('0xAp0'), 10.0)
802 self.identical(fromHex('0xaP0'), 10.0)
803 self.identical(fromHex('0xAP0'), 10.0)
804 self.identical(fromHex('0xbep0'), 190.0)
805 self.identical(fromHex('0xBep0'), 190.0)
806 self.identical(fromHex('0xbEp0'), 190.0)
807 self.identical(fromHex('0XBE0P-4'), 190.0)
808 self.identical(fromHex('0xBEp0'), 190.0)
809 self.identical(fromHex('0xB.Ep4'), 190.0)
810 self.identical(fromHex('0x.BEp8'), 190.0)
811 self.identical(fromHex('0x.0BEp12'), 190.0)
812
813 # moving the point around
814 pi = fromHex('0x1.921fb54442d18p1')
815 self.identical(fromHex('0x.006487ed5110b46p11'), pi)
816 self.identical(fromHex('0x.00c90fdaa22168cp10'), pi)
817 self.identical(fromHex('0x.01921fb54442d18p9'), pi)
818 self.identical(fromHex('0x.03243f6a8885a3p8'), pi)
819 self.identical(fromHex('0x.06487ed5110b46p7'), pi)
820 self.identical(fromHex('0x.0c90fdaa22168cp6'), pi)
821 self.identical(fromHex('0x.1921fb54442d18p5'), pi)
822 self.identical(fromHex('0x.3243f6a8885a3p4'), pi)
823 self.identical(fromHex('0x.6487ed5110b46p3'), pi)
824 self.identical(fromHex('0x.c90fdaa22168cp2'), pi)
825 self.identical(fromHex('0x1.921fb54442d18p1'), pi)
826 self.identical(fromHex('0x3.243f6a8885a3p0'), pi)
827 self.identical(fromHex('0x6.487ed5110b46p-1'), pi)
828 self.identical(fromHex('0xc.90fdaa22168cp-2'), pi)
829 self.identical(fromHex('0x19.21fb54442d18p-3'), pi)
830 self.identical(fromHex('0x32.43f6a8885a3p-4'), pi)
831 self.identical(fromHex('0x64.87ed5110b46p-5'), pi)
832 self.identical(fromHex('0xc9.0fdaa22168cp-6'), pi)
833 self.identical(fromHex('0x192.1fb54442d18p-7'), pi)
834 self.identical(fromHex('0x324.3f6a8885a3p-8'), pi)
835 self.identical(fromHex('0x648.7ed5110b46p-9'), pi)
836 self.identical(fromHex('0xc90.fdaa22168cp-10'), pi)
837 self.identical(fromHex('0x1921.fb54442d18p-11'), pi)
838 # ...
839 self.identical(fromHex('0x1921fb54442d1.8p-47'), pi)
840 self.identical(fromHex('0x3243f6a8885a3p-48'), pi)
841 self.identical(fromHex('0x6487ed5110b46p-49'), pi)
842 self.identical(fromHex('0xc90fdaa22168cp-50'), pi)
843 self.identical(fromHex('0x1921fb54442d18p-51'), pi)
844 self.identical(fromHex('0x3243f6a8885a30p-52'), pi)
845 self.identical(fromHex('0x6487ed5110b460p-53'), pi)
846 self.identical(fromHex('0xc90fdaa22168c0p-54'), pi)
847 self.identical(fromHex('0x1921fb54442d180p-55'), pi)
848
849
850 # results that should overflow...
851 self.assertRaises(OverflowError, fromHex, '-0x1p1024')
852 self.assertRaises(OverflowError, fromHex, '0x1p+1025')
853 self.assertRaises(OverflowError, fromHex, '+0X1p1030')
854 self.assertRaises(OverflowError, fromHex, '-0x1p+1100')
855 self.assertRaises(OverflowError, fromHex, '0X1p123456789123456789')
856 self.assertRaises(OverflowError, fromHex, '+0X.8p+1025')
857 self.assertRaises(OverflowError, fromHex, '+0x0.8p1025')
858 self.assertRaises(OverflowError, fromHex, '-0x0.4p1026')
859 self.assertRaises(OverflowError, fromHex, '0X2p+1023')
860 self.assertRaises(OverflowError, fromHex, '0x2.p1023')
861 self.assertRaises(OverflowError, fromHex, '-0x2.0p+1023')
862 self.assertRaises(OverflowError, fromHex, '+0X4p+1022')
863 self.assertRaises(OverflowError, fromHex, '0x1.ffffffffffffffp+1023')
864 self.assertRaises(OverflowError, fromHex, '-0X1.fffffffffffff9p1023')
865 self.assertRaises(OverflowError, fromHex, '0X1.fffffffffffff8p1023')
866 self.assertRaises(OverflowError, fromHex, '+0x3.fffffffffffffp1022')
867 self.assertRaises(OverflowError, fromHex, '0x3fffffffffffffp+970')
868 self.assertRaises(OverflowError, fromHex, '0x10000000000000000p960')
869 self.assertRaises(OverflowError, fromHex, '-0Xffffffffffffffffp960')
870
871 # ...and those that round to +-max float
872 self.identical(fromHex('+0x1.fffffffffffffp+1023'), MAX)
873 self.identical(fromHex('-0X1.fffffffffffff7p1023'), -MAX)
874 self.identical(fromHex('0X1.fffffffffffff7fffffffffffffp1023'), MAX)
875
876 # zeros
877 self.identical(fromHex('0x0p0'), 0.0)
878 self.identical(fromHex('0x0p1000'), 0.0)
879 self.identical(fromHex('-0x0p1023'), -0.0)
880 self.identical(fromHex('0X0p1024'), 0.0)
881 self.identical(fromHex('-0x0p1025'), -0.0)
882 self.identical(fromHex('0X0p2000'), 0.0)
883 self.identical(fromHex('0x0p123456789123456789'), 0.0)
884 self.identical(fromHex('-0X0p-0'), -0.0)
885 self.identical(fromHex('-0X0p-1000'), -0.0)
886 self.identical(fromHex('0x0p-1023'), 0.0)
887 self.identical(fromHex('-0X0p-1024'), -0.0)
888 self.identical(fromHex('-0x0p-1025'), -0.0)
889 self.identical(fromHex('-0x0p-1072'), -0.0)
890 self.identical(fromHex('0X0p-1073'), 0.0)
891 self.identical(fromHex('-0x0p-1074'), -0.0)
892 self.identical(fromHex('0x0p-1075'), 0.0)
893 self.identical(fromHex('0X0p-1076'), 0.0)
894 self.identical(fromHex('-0X0p-2000'), -0.0)
895 self.identical(fromHex('-0x0p-123456789123456789'), -0.0)
896
897 # values that should underflow to 0
898 self.identical(fromHex('0X1p-1075'), 0.0)
899 self.identical(fromHex('-0X1p-1075'), -0.0)
900 self.identical(fromHex('-0x1p-123456789123456789'), -0.0)
901 self.identical(fromHex('0x1.00000000000000001p-1075'), TINY)
902 self.identical(fromHex('-0x1.1p-1075'), -TINY)
903 self.identical(fromHex('0x1.fffffffffffffffffp-1075'), TINY)
904
905 # check round-half-even is working correctly near 0 ...
906 self.identical(fromHex('0x1p-1076'), 0.0)
907 self.identical(fromHex('0X2p-1076'), 0.0)
908 self.identical(fromHex('0X3p-1076'), TINY)
909 self.identical(fromHex('0x4p-1076'), TINY)
910 self.identical(fromHex('0X5p-1076'), TINY)
911 self.identical(fromHex('0X6p-1076'), 2*TINY)
912 self.identical(fromHex('0x7p-1076'), 2*TINY)
913 self.identical(fromHex('0X8p-1076'), 2*TINY)
914 self.identical(fromHex('0X9p-1076'), 2*TINY)
915 self.identical(fromHex('0xap-1076'), 2*TINY)
916 self.identical(fromHex('0Xbp-1076'), 3*TINY)
917 self.identical(fromHex('0xcp-1076'), 3*TINY)
918 self.identical(fromHex('0Xdp-1076'), 3*TINY)
919 self.identical(fromHex('0Xep-1076'), 4*TINY)
920 self.identical(fromHex('0xfp-1076'), 4*TINY)
921 self.identical(fromHex('0x10p-1076'), 4*TINY)
922 self.identical(fromHex('-0x1p-1076'), -0.0)
923 self.identical(fromHex('-0X2p-1076'), -0.0)
924 self.identical(fromHex('-0x3p-1076'), -TINY)
925 self.identical(fromHex('-0X4p-1076'), -TINY)
926 self.identical(fromHex('-0x5p-1076'), -TINY)
927 self.identical(fromHex('-0x6p-1076'), -2*TINY)
928 self.identical(fromHex('-0X7p-1076'), -2*TINY)
929 self.identical(fromHex('-0X8p-1076'), -2*TINY)
930 self.identical(fromHex('-0X9p-1076'), -2*TINY)
931 self.identical(fromHex('-0Xap-1076'), -2*TINY)
932 self.identical(fromHex('-0xbp-1076'), -3*TINY)
933 self.identical(fromHex('-0xcp-1076'), -3*TINY)
934 self.identical(fromHex('-0Xdp-1076'), -3*TINY)
935 self.identical(fromHex('-0xep-1076'), -4*TINY)
936 self.identical(fromHex('-0Xfp-1076'), -4*TINY)
937 self.identical(fromHex('-0X10p-1076'), -4*TINY)
938
939 # ... and near MIN ...
940 self.identical(fromHex('0x0.ffffffffffffd6p-1022'), MIN-3*TINY)
941 self.identical(fromHex('0x0.ffffffffffffd8p-1022'), MIN-2*TINY)
942 self.identical(fromHex('0x0.ffffffffffffdap-1022'), MIN-2*TINY)
943 self.identical(fromHex('0x0.ffffffffffffdcp-1022'), MIN-2*TINY)
944 self.identical(fromHex('0x0.ffffffffffffdep-1022'), MIN-2*TINY)
945 self.identical(fromHex('0x0.ffffffffffffe0p-1022'), MIN-2*TINY)
946 self.identical(fromHex('0x0.ffffffffffffe2p-1022'), MIN-2*TINY)
947 self.identical(fromHex('0x0.ffffffffffffe4p-1022'), MIN-2*TINY)
948 self.identical(fromHex('0x0.ffffffffffffe6p-1022'), MIN-2*TINY)
949 self.identical(fromHex('0x0.ffffffffffffe8p-1022'), MIN-2*TINY)
950 self.identical(fromHex('0x0.ffffffffffffeap-1022'), MIN-TINY)
951 self.identical(fromHex('0x0.ffffffffffffecp-1022'), MIN-TINY)
952 self.identical(fromHex('0x0.ffffffffffffeep-1022'), MIN-TINY)
953 self.identical(fromHex('0x0.fffffffffffff0p-1022'), MIN-TINY)
954 self.identical(fromHex('0x0.fffffffffffff2p-1022'), MIN-TINY)
955 self.identical(fromHex('0x0.fffffffffffff4p-1022'), MIN-TINY)
956 self.identical(fromHex('0x0.fffffffffffff6p-1022'), MIN-TINY)
957 self.identical(fromHex('0x0.fffffffffffff8p-1022'), MIN)
958 self.identical(fromHex('0x0.fffffffffffffap-1022'), MIN)
959 self.identical(fromHex('0x0.fffffffffffffcp-1022'), MIN)
960 self.identical(fromHex('0x0.fffffffffffffep-1022'), MIN)
961 self.identical(fromHex('0x1.00000000000000p-1022'), MIN)
962 self.identical(fromHex('0x1.00000000000002p-1022'), MIN)
963 self.identical(fromHex('0x1.00000000000004p-1022'), MIN)
964 self.identical(fromHex('0x1.00000000000006p-1022'), MIN)
965 self.identical(fromHex('0x1.00000000000008p-1022'), MIN)
966 self.identical(fromHex('0x1.0000000000000ap-1022'), MIN+TINY)
967 self.identical(fromHex('0x1.0000000000000cp-1022'), MIN+TINY)
968 self.identical(fromHex('0x1.0000000000000ep-1022'), MIN+TINY)
969 self.identical(fromHex('0x1.00000000000010p-1022'), MIN+TINY)
970 self.identical(fromHex('0x1.00000000000012p-1022'), MIN+TINY)
971 self.identical(fromHex('0x1.00000000000014p-1022'), MIN+TINY)
972 self.identical(fromHex('0x1.00000000000016p-1022'), MIN+TINY)
973 self.identical(fromHex('0x1.00000000000018p-1022'), MIN+2*TINY)
974
975 # ... and near 1.0.
976 self.identical(fromHex('0x0.fffffffffffff0p0'), 1.0-EPS)
977 self.identical(fromHex('0x0.fffffffffffff1p0'), 1.0-EPS)
978 self.identical(fromHex('0X0.fffffffffffff2p0'), 1.0-EPS)
979 self.identical(fromHex('0x0.fffffffffffff3p0'), 1.0-EPS)
980 self.identical(fromHex('0X0.fffffffffffff4p0'), 1.0-EPS)
981 self.identical(fromHex('0X0.fffffffffffff5p0'), 1.0-EPS/2)
982 self.identical(fromHex('0X0.fffffffffffff6p0'), 1.0-EPS/2)
983 self.identical(fromHex('0x0.fffffffffffff7p0'), 1.0-EPS/2)
984 self.identical(fromHex('0x0.fffffffffffff8p0'), 1.0-EPS/2)
985 self.identical(fromHex('0X0.fffffffffffff9p0'), 1.0-EPS/2)
986 self.identical(fromHex('0X0.fffffffffffffap0'), 1.0-EPS/2)
987 self.identical(fromHex('0x0.fffffffffffffbp0'), 1.0-EPS/2)
988 self.identical(fromHex('0X0.fffffffffffffcp0'), 1.0)
989 self.identical(fromHex('0x0.fffffffffffffdp0'), 1.0)
990 self.identical(fromHex('0X0.fffffffffffffep0'), 1.0)
991 self.identical(fromHex('0x0.ffffffffffffffp0'), 1.0)
992 self.identical(fromHex('0X1.00000000000000p0'), 1.0)
993 self.identical(fromHex('0X1.00000000000001p0'), 1.0)
994 self.identical(fromHex('0x1.00000000000002p0'), 1.0)
995 self.identical(fromHex('0X1.00000000000003p0'), 1.0)
996 self.identical(fromHex('0x1.00000000000004p0'), 1.0)
997 self.identical(fromHex('0X1.00000000000005p0'), 1.0)
998 self.identical(fromHex('0X1.00000000000006p0'), 1.0)
999 self.identical(fromHex('0X1.00000000000007p0'), 1.0)
1000 self.identical(fromHex('0x1.00000000000007ffffffffffffffffffffp0'),
1001 1.0)
1002 self.identical(fromHex('0x1.00000000000008p0'), 1.0)
1003 self.identical(fromHex('0x1.00000000000008000000000000000001p0'),
1004 1+EPS)
1005 self.identical(fromHex('0X1.00000000000009p0'), 1.0+EPS)
1006 self.identical(fromHex('0x1.0000000000000ap0'), 1.0+EPS)
1007 self.identical(fromHex('0x1.0000000000000bp0'), 1.0+EPS)
1008 self.identical(fromHex('0X1.0000000000000cp0'), 1.0+EPS)
1009 self.identical(fromHex('0x1.0000000000000dp0'), 1.0+EPS)
1010 self.identical(fromHex('0x1.0000000000000ep0'), 1.0+EPS)
1011 self.identical(fromHex('0X1.0000000000000fp0'), 1.0+EPS)
1012 self.identical(fromHex('0x1.00000000000010p0'), 1.0+EPS)
1013 self.identical(fromHex('0X1.00000000000011p0'), 1.0+EPS)
1014 self.identical(fromHex('0x1.00000000000012p0'), 1.0+EPS)
1015 self.identical(fromHex('0X1.00000000000013p0'), 1.0+EPS)
1016 self.identical(fromHex('0X1.00000000000014p0'), 1.0+EPS)
1017 self.identical(fromHex('0x1.00000000000015p0'), 1.0+EPS)
1018 self.identical(fromHex('0x1.00000000000016p0'), 1.0+EPS)
1019 self.identical(fromHex('0X1.00000000000017p0'), 1.0+EPS)
1020 self.identical(fromHex('0x1.00000000000017ffffffffffffffffffffp0'),
1021 1.0+EPS)
1022 self.identical(fromHex('0x1.00000000000018p0'), 1.0+2*EPS)
1023 self.identical(fromHex('0X1.00000000000018000000000000000001p0'),
1024 1.0+2*EPS)
1025 self.identical(fromHex('0x1.00000000000019p0'), 1.0+2*EPS)
1026 self.identical(fromHex('0X1.0000000000001ap0'), 1.0+2*EPS)
1027 self.identical(fromHex('0X1.0000000000001bp0'), 1.0+2*EPS)
1028 self.identical(fromHex('0x1.0000000000001cp0'), 1.0+2*EPS)
1029 self.identical(fromHex('0x1.0000000000001dp0'), 1.0+2*EPS)
1030 self.identical(fromHex('0x1.0000000000001ep0'), 1.0+2*EPS)
1031 self.identical(fromHex('0X1.0000000000001fp0'), 1.0+2*EPS)
1032 self.identical(fromHex('0x1.00000000000020p0'), 1.0+2*EPS)
1033
1034 def test_roundtrip(self):
1035 def roundtrip(x):
1036 return fromHex(toHex(x))
1037
1038 for x in [NAN, INF, self.MAX, self.MIN, self.MIN-self.TINY, self.TINY, 0.0]:
1039 self.identical(x, roundtrip(x))
1040 self.identical(-x, roundtrip(-x))
1041
1042 # fromHex(toHex(x)) should exactly recover x, for any non-NaN float x.
1043 import random
1044 for i in range(10000):
1045 e = random.randrange(-1200, 1200)
1046 m = random.random()
1047 s = random.choice([1.0, -1.0])
1048 try:
1049 x = s*ldexp(m, e)
1050 except OverflowError:
1051 pass
1052 else:
1053 self.identical(x, fromHex(toHex(x)))
1054
Christian Heimes53876d92008-04-19 00:31:39 +00001055
Michael W. Hudsonba283e22005-05-27 15:23:20 +00001056def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001057 support.run_unittest(
Amaury Forgeot d'Arc7e958d12008-09-06 21:03:22 +00001058 GeneralFloatCases,
Michael W. Hudsonba283e22005-05-27 15:23:20 +00001059 FormatFunctionsTestCase,
1060 UnknownFormatTestCase,
Eric Smith8c663262007-08-25 02:26:07 +00001061 IEEEFormatTestCase,
Christian Heimes827b35c2007-12-10 22:19:17 +00001062 FormatTestCase,
Christian Heimes99170a52007-12-19 02:07:34 +00001063 ReprTestCase,
Mark Dickinsone6a076d2009-04-18 11:48:33 +00001064 RoundTestCase,
Christian Heimes99170a52007-12-19 02:07:34 +00001065 InfNanTest,
Mark Dickinson65fe25e2008-07-16 11:30:51 +00001066 HexFloatTestCase,
Christian Heimesb76922a2007-12-11 01:06:40 +00001067 )
Michael W. Hudsonba283e22005-05-27 15:23:20 +00001068
1069if __name__ == '__main__':
1070 test_main()