blob: 1c6c4124c7debf9f7e47ff183160034c02ff57c7 [file] [log] [blame]
Michael W. Hudsonba283e22005-05-27 15:23:20 +00001
2import unittest, struct
Christian Heimes284d9272007-12-10 22:28:56 +00003import os
Michael W. Hudsonba283e22005-05-27 15:23:20 +00004from test import test_support
Christian Heimes6f341092008-04-18 23:13:07 +00005import math
Mark Dickinson7103aa42008-07-15 19:08:33 +00006from math import isinf, isnan, copysign, ldexp
Christian Heimes6f341092008-04-18 23:13:07 +00007import operator
Amaury Forgeot d'Arcfeb8cad2008-09-06 20:53:51 +00008import random, fractions
Michael W. Hudsonba283e22005-05-27 15:23:20 +00009
Christian Heimes6f341092008-04-18 23:13:07 +000010INF = float("inf")
11NAN = float("nan")
Christian Heimes0a8143f2007-12-18 23:22:54 +000012
Mark Dickinson61a0d052009-04-29 21:57:15 +000013#locate file with float format test values
14test_dir = os.path.dirname(__file__) or os.curdir
15format_testfile = os.path.join(test_dir, 'formatfloat_testcases.txt')
16
Benjamin Peterson979395b2008-05-03 21:35:18 +000017class GeneralFloatCases(unittest.TestCase):
18
19 def test_float(self):
20 self.assertEqual(float(3.14), 3.14)
21 self.assertEqual(float(314), 314.0)
22 self.assertEqual(float(314L), 314.0)
23 self.assertEqual(float(" 3.14 "), 3.14)
24 self.assertRaises(ValueError, float, " 0x3.1 ")
25 self.assertRaises(ValueError, float, " -0x3.p-1 ")
26 self.assertRaises(ValueError, float, " +0x3.p-1 ")
27 self.assertRaises(ValueError, float, "++3.14")
28 self.assertRaises(ValueError, float, "+-3.14")
29 self.assertRaises(ValueError, float, "-+3.14")
30 self.assertRaises(ValueError, float, "--3.14")
Amaury Forgeot d'Arcfeb8cad2008-09-06 20:53:51 +000031 if test_support.have_unicode:
Benjamin Peterson979395b2008-05-03 21:35:18 +000032 self.assertEqual(float(unicode(" 3.14 ")), 3.14)
33 self.assertEqual(float(unicode(" \u0663.\u0661\u0664 ",'raw-unicode-escape')), 3.14)
34 # Implementation limitation in PyFloat_FromString()
35 self.assertRaises(ValueError, float, unicode("1"*10000))
36
37 @test_support.run_with_locale('LC_NUMERIC', 'fr_FR', 'de_DE')
38 def test_float_with_comma(self):
39 # set locale to something that doesn't use '.' for the decimal point
40 # float must not accept the locale specific decimal point but
41 # it still has to accept the normal python syntac
42 import locale
43 if not locale.localeconv()['decimal_point'] == ',':
44 return
45
46 self.assertEqual(float(" 3.14 "), 3.14)
47 self.assertEqual(float("+3.14 "), 3.14)
48 self.assertEqual(float("-3.14 "), -3.14)
49 self.assertEqual(float(".14 "), .14)
50 self.assertEqual(float("3. "), 3.0)
51 self.assertEqual(float("3.e3 "), 3000.0)
52 self.assertEqual(float("3.2e3 "), 3200.0)
53 self.assertEqual(float("2.5e-1 "), 0.25)
54 self.assertEqual(float("5e-1"), 0.5)
55 self.assertRaises(ValueError, float, " 3,14 ")
56 self.assertRaises(ValueError, float, " +3,14 ")
57 self.assertRaises(ValueError, float, " -3,14 ")
58 self.assertRaises(ValueError, float, " 0x3.1 ")
59 self.assertRaises(ValueError, float, " -0x3.p-1 ")
60 self.assertRaises(ValueError, float, " +0x3.p-1 ")
61 self.assertEqual(float(" 25.e-1 "), 2.5)
Benjamin Petersona853a892008-09-06 23:19:15 +000062 self.assertEqual(test_support.fcmp(float(" .25e-1 "), .025), 0)
Benjamin Peterson979395b2008-05-03 21:35:18 +000063
64 def test_floatconversion(self):
65 # Make sure that calls to __float__() work properly
66 class Foo0:
67 def __float__(self):
68 return 42.
69
70 class Foo1(object):
71 def __float__(self):
72 return 42.
73
74 class Foo2(float):
75 def __float__(self):
76 return 42.
77
78 class Foo3(float):
79 def __new__(cls, value=0.):
80 return float.__new__(cls, 2*value)
81
82 def __float__(self):
83 return self
84
85 class Foo4(float):
86 def __float__(self):
87 return 42
88
Benjamin Peterson99d36f12009-04-15 21:26:36 +000089 # Issue 5759: __float__ not called on str subclasses (though it is on
90 # unicode subclasses).
91 class FooStr(str):
92 def __float__(self):
93 return float(str(self)) + 1
94
95 class FooUnicode(unicode):
96 def __float__(self):
97 return float(unicode(self)) + 1
98
Benjamin Peterson979395b2008-05-03 21:35:18 +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 Peterson99d36f12009-04-15 21:26:36 +0000104 self.assertAlmostEqual(float(FooUnicode('8')), 9.)
105 self.assertAlmostEqual(float(FooStr('8')), 9.)
Benjamin Peterson979395b2008-05-03 21:35:18 +0000106
107 def test_floatasratio(self):
108 for f, ratio in [
109 (0.875, (7, 8)),
110 (-0.875, (-7, 8)),
111 (0.0, (0, 1)),
112 (11.5, (23, 2)),
113 ]:
114 self.assertEqual(f.as_integer_ratio(), ratio)
115
116 for i in range(10000):
117 f = random.random()
118 f *= 10 ** random.randint(-100, 100)
119 n, d = f.as_integer_ratio()
120 self.assertEqual(float(n).__truediv__(d), f)
121
122 R = fractions.Fraction
123 self.assertEqual(R(0, 1),
124 R(*float(0.0).as_integer_ratio()))
125 self.assertEqual(R(5, 2),
126 R(*float(2.5).as_integer_ratio()))
127 self.assertEqual(R(1, 2),
128 R(*float(0.5).as_integer_ratio()))
129 self.assertEqual(R(4728779608739021, 2251799813685248),
130 R(*float(2.1).as_integer_ratio()))
131 self.assertEqual(R(-4728779608739021, 2251799813685248),
132 R(*float(-2.1).as_integer_ratio()))
133 self.assertEqual(R(-2100, 1),
134 R(*float(-2100.0).as_integer_ratio()))
135
136 self.assertRaises(OverflowError, float('inf').as_integer_ratio)
137 self.assertRaises(OverflowError, float('-inf').as_integer_ratio)
138 self.assertRaises(ValueError, float('nan').as_integer_ratio)
139
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000140class FormatFunctionsTestCase(unittest.TestCase):
141
142 def setUp(self):
143 self.save_formats = {'double':float.__getformat__('double'),
144 'float':float.__getformat__('float')}
145
146 def tearDown(self):
147 float.__setformat__('double', self.save_formats['double'])
148 float.__setformat__('float', self.save_formats['float'])
149
150 def test_getformat(self):
151 self.assert_(float.__getformat__('double') in
152 ['unknown', 'IEEE, big-endian', 'IEEE, little-endian'])
153 self.assert_(float.__getformat__('float') in
154 ['unknown', 'IEEE, big-endian', 'IEEE, little-endian'])
155 self.assertRaises(ValueError, float.__getformat__, 'chicken')
156 self.assertRaises(TypeError, float.__getformat__, 1)
157
158 def test_setformat(self):
159 for t in 'double', 'float':
160 float.__setformat__(t, 'unknown')
161 if self.save_formats[t] == 'IEEE, big-endian':
162 self.assertRaises(ValueError, float.__setformat__,
163 t, 'IEEE, little-endian')
164 elif self.save_formats[t] == 'IEEE, little-endian':
165 self.assertRaises(ValueError, float.__setformat__,
166 t, 'IEEE, big-endian')
167 else:
168 self.assertRaises(ValueError, float.__setformat__,
169 t, 'IEEE, big-endian')
170 self.assertRaises(ValueError, float.__setformat__,
171 t, 'IEEE, little-endian')
172 self.assertRaises(ValueError, float.__setformat__,
173 t, 'chicken')
174 self.assertRaises(ValueError, float.__setformat__,
175 'chicken', 'unknown')
176
177BE_DOUBLE_INF = '\x7f\xf0\x00\x00\x00\x00\x00\x00'
178LE_DOUBLE_INF = ''.join(reversed(BE_DOUBLE_INF))
179BE_DOUBLE_NAN = '\x7f\xf8\x00\x00\x00\x00\x00\x00'
180LE_DOUBLE_NAN = ''.join(reversed(BE_DOUBLE_NAN))
181
182BE_FLOAT_INF = '\x7f\x80\x00\x00'
183LE_FLOAT_INF = ''.join(reversed(BE_FLOAT_INF))
184BE_FLOAT_NAN = '\x7f\xc0\x00\x00'
185LE_FLOAT_NAN = ''.join(reversed(BE_FLOAT_NAN))
186
187# on non-IEEE platforms, attempting to unpack a bit pattern
188# representing an infinity or a NaN should raise an exception.
189
190class UnknownFormatTestCase(unittest.TestCase):
191 def setUp(self):
192 self.save_formats = {'double':float.__getformat__('double'),
193 'float':float.__getformat__('float')}
194 float.__setformat__('double', 'unknown')
195 float.__setformat__('float', 'unknown')
Tim Peters5d36a552005-06-03 22:40:27 +0000196
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000197 def tearDown(self):
198 float.__setformat__('double', self.save_formats['double'])
199 float.__setformat__('float', self.save_formats['float'])
200
201 def test_double_specials_dont_unpack(self):
202 for fmt, data in [('>d', BE_DOUBLE_INF),
203 ('>d', BE_DOUBLE_NAN),
204 ('<d', LE_DOUBLE_INF),
205 ('<d', LE_DOUBLE_NAN)]:
206 self.assertRaises(ValueError, struct.unpack, fmt, data)
207
208 def test_float_specials_dont_unpack(self):
209 for fmt, data in [('>f', BE_FLOAT_INF),
210 ('>f', BE_FLOAT_NAN),
211 ('<f', LE_FLOAT_INF),
212 ('<f', LE_FLOAT_NAN)]:
213 self.assertRaises(ValueError, struct.unpack, fmt, data)
214
215
216# on an IEEE platform, all we guarantee is that bit patterns
217# representing infinities or NaNs do not raise an exception; all else
218# is accident (today).
Alex Martellid8672aa2007-08-22 21:14:17 +0000219# let's also try to guarantee that -0.0 and 0.0 don't get confused.
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000220
221class IEEEFormatTestCase(unittest.TestCase):
222 if float.__getformat__("double").startswith("IEEE"):
223 def test_double_specials_do_unpack(self):
224 for fmt, data in [('>d', BE_DOUBLE_INF),
225 ('>d', BE_DOUBLE_NAN),
226 ('<d', LE_DOUBLE_INF),
227 ('<d', LE_DOUBLE_NAN)]:
228 struct.unpack(fmt, data)
229
230 if float.__getformat__("float").startswith("IEEE"):
231 def test_float_specials_do_unpack(self):
232 for fmt, data in [('>f', BE_FLOAT_INF),
233 ('>f', BE_FLOAT_NAN),
234 ('<f', LE_FLOAT_INF),
235 ('<f', LE_FLOAT_NAN)]:
236 struct.unpack(fmt, data)
237
Alex Martellid8672aa2007-08-22 21:14:17 +0000238 if float.__getformat__("double").startswith("IEEE"):
239 def test_negative_zero(self):
240 import math
241 def pos_pos():
242 return 0.0, math.atan2(0.0, -1)
243 def pos_neg():
244 return 0.0, math.atan2(-0.0, -1)
245 def neg_pos():
246 return -0.0, math.atan2(0.0, -1)
247 def neg_neg():
248 return -0.0, math.atan2(-0.0, -1)
249 self.assertEquals(pos_pos(), neg_pos())
250 self.assertEquals(pos_neg(), neg_neg())
251
Guido van Rossum3b835492008-01-05 00:59:59 +0000252 if float.__getformat__("double").startswith("IEEE"):
253 def test_underflow_sign(self):
254 import math
255 # check that -1e-1000 gives -0.0, not 0.0
256 self.assertEquals(math.atan2(-1e-1000, -1), math.atan2(-0.0, -1))
257 self.assertEquals(math.atan2(float('-1e-1000'), -1),
258 math.atan2(-0.0, -1))
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000259
Eric Smitha985a3a2009-05-05 18:26:08 +0000260 def test_format(self):
261 # these should be rewritten to use both format(x, spec) and
262 # x.__format__(spec)
263
264 self.assertEqual(format(0.0, 'f'), '0.000000')
265
266 # the default is 'g', except for empty format spec
267 self.assertEqual(format(0.0, ''), '0.0')
268 self.assertEqual(format(0.01, ''), '0.01')
269 self.assertEqual(format(0.01, 'g'), '0.01')
270
271 # empty presentation type should format in the same way as str
272 # (issue 5920)
273 x = 100/7.
274 self.assertEqual(format(x, ''), str(x))
275 self.assertEqual(format(x, '-'), str(x))
276 self.assertEqual(format(x, '>'), str(x))
277 self.assertEqual(format(x, '2'), str(x))
278
279 self.assertEqual(format(1.0, 'f'), '1.000000')
280
281 self.assertEqual(format(-1.0, 'f'), '-1.000000')
282
283 self.assertEqual(format( 1.0, ' f'), ' 1.000000')
284 self.assertEqual(format(-1.0, ' f'), '-1.000000')
285 self.assertEqual(format( 1.0, '+f'), '+1.000000')
286 self.assertEqual(format(-1.0, '+f'), '-1.000000')
287
288 # % formatting
289 self.assertEqual(format(-1.0, '%'), '-100.000000%')
290
291 # conversion to string should fail
292 self.assertRaises(ValueError, format, 3.0, "s")
293
294 # other format specifiers shouldn't work on floats,
295 # in particular int specifiers
296 for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
297 [chr(x) for x in range(ord('A'), ord('Z')+1)]):
298 if not format_spec in 'eEfFgGn%':
299 self.assertRaises(ValueError, format, 0.0, format_spec)
300 self.assertRaises(ValueError, format, 1.0, format_spec)
301 self.assertRaises(ValueError, format, -1.0, format_spec)
302 self.assertRaises(ValueError, format, 1e100, format_spec)
303 self.assertRaises(ValueError, format, -1e100, format_spec)
304 self.assertRaises(ValueError, format, 1e-100, format_spec)
305 self.assertRaises(ValueError, format, -1e-100, format_spec)
306
Mark Dickinson61a0d052009-04-29 21:57:15 +0000307 @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
308 "test requires IEEE 754 doubles")
309 def test_format_testfile(self):
310 for line in open(format_testfile):
311 if line.startswith('--'):
312 continue
313 line = line.strip()
314 if not line:
315 continue
316
317 lhs, rhs = map(str.strip, line.split('->'))
318 fmt, arg = lhs.split()
319 self.assertEqual(fmt % float(arg), rhs)
320 self.assertEqual(fmt % -float(arg), '-' + rhs)
321
Mark Dickinson92fcc9c2009-04-29 20:41:00 +0000322 def test_issue5864(self):
323 self.assertEquals(format(123.456, '.4'), '123.5')
324 self.assertEquals(format(1234.56, '.4'), '1.235e+03')
325 self.assertEquals(format(12345.6, '.4'), '1.235e+04')
326
Christian Heimes284d9272007-12-10 22:28:56 +0000327class ReprTestCase(unittest.TestCase):
328 def test_repr(self):
329 floats_file = open(os.path.join(os.path.split(__file__)[0],
330 'floating_points.txt'))
331 for line in floats_file:
332 line = line.strip()
333 if not line or line.startswith('#'):
334 continue
335 v = eval(line)
336 self.assertEqual(v, eval(repr(v)))
337 floats_file.close()
338
Christian Heimes0a8143f2007-12-18 23:22:54 +0000339# Beginning with Python 2.6 float has cross platform compatible
Mark Dickinsonbf9f4d82008-07-05 11:33:52 +0000340# ways to create and represent inf and nan
Christian Heimes0a8143f2007-12-18 23:22:54 +0000341class InfNanTest(unittest.TestCase):
342 def test_inf_from_str(self):
343 self.assert_(isinf(float("inf")))
344 self.assert_(isinf(float("+inf")))
345 self.assert_(isinf(float("-inf")))
Mark Dickinsonbf9f4d82008-07-05 11:33:52 +0000346 self.assert_(isinf(float("infinity")))
347 self.assert_(isinf(float("+infinity")))
348 self.assert_(isinf(float("-infinity")))
Christian Heimes0a8143f2007-12-18 23:22:54 +0000349
350 self.assertEqual(repr(float("inf")), "inf")
351 self.assertEqual(repr(float("+inf")), "inf")
352 self.assertEqual(repr(float("-inf")), "-inf")
Mark Dickinsonbf9f4d82008-07-05 11:33:52 +0000353 self.assertEqual(repr(float("infinity")), "inf")
354 self.assertEqual(repr(float("+infinity")), "inf")
355 self.assertEqual(repr(float("-infinity")), "-inf")
Christian Heimes0a8143f2007-12-18 23:22:54 +0000356
357 self.assertEqual(repr(float("INF")), "inf")
358 self.assertEqual(repr(float("+Inf")), "inf")
359 self.assertEqual(repr(float("-iNF")), "-inf")
Mark Dickinsonbf9f4d82008-07-05 11:33:52 +0000360 self.assertEqual(repr(float("Infinity")), "inf")
361 self.assertEqual(repr(float("+iNfInItY")), "inf")
362 self.assertEqual(repr(float("-INFINITY")), "-inf")
Christian Heimes0a8143f2007-12-18 23:22:54 +0000363
364 self.assertEqual(str(float("inf")), "inf")
365 self.assertEqual(str(float("+inf")), "inf")
366 self.assertEqual(str(float("-inf")), "-inf")
Mark Dickinsonbf9f4d82008-07-05 11:33:52 +0000367 self.assertEqual(str(float("infinity")), "inf")
368 self.assertEqual(str(float("+infinity")), "inf")
369 self.assertEqual(str(float("-infinity")), "-inf")
Christian Heimes0a8143f2007-12-18 23:22:54 +0000370
371 self.assertRaises(ValueError, float, "info")
372 self.assertRaises(ValueError, float, "+info")
373 self.assertRaises(ValueError, float, "-info")
374 self.assertRaises(ValueError, float, "in")
375 self.assertRaises(ValueError, float, "+in")
376 self.assertRaises(ValueError, float, "-in")
Mark Dickinsonbf9f4d82008-07-05 11:33:52 +0000377 self.assertRaises(ValueError, float, "infinit")
378 self.assertRaises(ValueError, float, "+Infin")
379 self.assertRaises(ValueError, float, "-INFI")
380 self.assertRaises(ValueError, float, "infinitys")
Christian Heimes0a8143f2007-12-18 23:22:54 +0000381
382 def test_inf_as_str(self):
383 self.assertEqual(repr(1e300 * 1e300), "inf")
384 self.assertEqual(repr(-1e300 * 1e300), "-inf")
385
386 self.assertEqual(str(1e300 * 1e300), "inf")
387 self.assertEqual(str(-1e300 * 1e300), "-inf")
388
389 def test_nan_from_str(self):
390 self.assert_(isnan(float("nan")))
391 self.assert_(isnan(float("+nan")))
392 self.assert_(isnan(float("-nan")))
393
394 self.assertEqual(repr(float("nan")), "nan")
395 self.assertEqual(repr(float("+nan")), "nan")
396 self.assertEqual(repr(float("-nan")), "nan")
397
398 self.assertEqual(repr(float("NAN")), "nan")
399 self.assertEqual(repr(float("+NAn")), "nan")
400 self.assertEqual(repr(float("-NaN")), "nan")
401
402 self.assertEqual(str(float("nan")), "nan")
403 self.assertEqual(str(float("+nan")), "nan")
404 self.assertEqual(str(float("-nan")), "nan")
405
406 self.assertRaises(ValueError, float, "nana")
407 self.assertRaises(ValueError, float, "+nana")
408 self.assertRaises(ValueError, float, "-nana")
409 self.assertRaises(ValueError, float, "na")
410 self.assertRaises(ValueError, float, "+na")
411 self.assertRaises(ValueError, float, "-na")
412
413 def test_nan_as_str(self):
414 self.assertEqual(repr(1e300 * 1e300 * 0), "nan")
415 self.assertEqual(repr(-1e300 * 1e300 * 0), "nan")
416
417 self.assertEqual(str(1e300 * 1e300 * 0), "nan")
418 self.assertEqual(str(-1e300 * 1e300 * 0), "nan")
Christian Heimes284d9272007-12-10 22:28:56 +0000419
Christian Heimes6f341092008-04-18 23:13:07 +0000420 def notest_float_nan(self):
421 self.assert_(NAN.is_nan())
422 self.failIf(INF.is_nan())
423 self.failIf((0.).is_nan())
424
425 def notest_float_inf(self):
426 self.assert_(INF.is_inf())
427 self.failIf(NAN.is_inf())
428 self.failIf((0.).is_inf())
429
Mark Dickinson7103aa42008-07-15 19:08:33 +0000430fromHex = float.fromhex
431toHex = float.hex
432class HexFloatTestCase(unittest.TestCase):
433 MAX = fromHex('0x.fffffffffffff8p+1024') # max normal
434 MIN = fromHex('0x1p-1022') # min normal
435 TINY = fromHex('0x0.0000000000001p-1022') # min subnormal
436 EPS = fromHex('0x0.0000000000001p0') # diff between 1.0 and next float up
437
438 def identical(self, x, y):
439 # check that floats x and y are identical, or that both
440 # are NaNs
441 if isnan(x) or isnan(y):
442 if isnan(x) == isnan(y):
443 return
444 elif x == y and (x != 0.0 or copysign(1.0, x) == copysign(1.0, y)):
445 return
446 self.fail('%r not identical to %r' % (x, y))
447
448 def test_ends(self):
Mark Dickinson62764562008-07-15 21:55:23 +0000449 self.identical(self.MIN, ldexp(1.0, -1022))
450 self.identical(self.TINY, ldexp(1.0, -1074))
451 self.identical(self.EPS, ldexp(1.0, -52))
452 self.identical(self.MAX, 2.*(ldexp(1.0, 1023) - ldexp(1.0, 970)))
Mark Dickinson7103aa42008-07-15 19:08:33 +0000453
454 def test_invalid_inputs(self):
455 invalid_inputs = [
456 'infi', # misspelt infinities and nans
457 '-Infinit',
458 '++inf',
459 '-+Inf',
460 '--nan',
461 '+-NaN',
462 'snan',
463 'NaNs',
464 'nna',
465 '0xnan',
466 '',
467 ' ',
468 'x1.0p0',
469 '0xX1.0p0',
470 '+ 0x1.0p0', # internal whitespace
471 '- 0x1.0p0',
472 '0 x1.0p0',
473 '0x 1.0p0',
474 '0x1 2.0p0',
475 '+0x1 .0p0',
476 '0x1. 0p0',
477 '-0x1.0 1p0',
478 '-0x1.0 p0',
479 '+0x1.0p +0',
480 '0x1.0p -0',
481 '0x1.0p 0',
482 '+0x1.0p+ 0',
483 '-0x1.0p- 0',
484 '++0x1.0p-0', # double signs
485 '--0x1.0p0',
486 '+-0x1.0p+0',
487 '-+0x1.0p0',
488 '0x1.0p++0',
489 '+0x1.0p+-0',
490 '-0x1.0p-+0',
491 '0x1.0p--0',
492 '0x1.0.p0',
493 '0x.p0', # no hex digits before or after point
494 '0x1,p0', # wrong decimal point character
495 '0x1pa',
496 u'0x1p\uff10', # fullwidth Unicode digits
497 u'\uff10x1p0',
498 u'0x\uff11p0',
499 u'0x1.\uff10p0',
500 '0x1p0 \n 0x2p0',
501 '0x1p0\0 0x1p0', # embedded null byte is not end of string
502 ]
503 for x in invalid_inputs:
Mark Dickinson892429b2008-08-21 20:02:24 +0000504 try:
505 result = fromHex(x)
506 except ValueError:
507 pass
508 else:
509 self.fail('Expected float.fromhex(%r) to raise ValueError; '
510 'got %r instead' % (x, result))
Mark Dickinson7103aa42008-07-15 19:08:33 +0000511
512
513 def test_from_hex(self):
514 MIN = self.MIN;
515 MAX = self.MAX;
516 TINY = self.TINY;
517 EPS = self.EPS;
518
519 # two spellings of infinity, with optional signs; case-insensitive
520 self.identical(fromHex('inf'), INF)
521 self.identical(fromHex('+Inf'), INF)
522 self.identical(fromHex('-INF'), -INF)
523 self.identical(fromHex('iNf'), INF)
524 self.identical(fromHex('Infinity'), INF)
525 self.identical(fromHex('+INFINITY'), INF)
526 self.identical(fromHex('-infinity'), -INF)
527 self.identical(fromHex('-iNFiNitY'), -INF)
528
529 # nans with optional sign; case insensitive
530 self.identical(fromHex('nan'), NAN)
531 self.identical(fromHex('+NaN'), NAN)
532 self.identical(fromHex('-NaN'), NAN)
533 self.identical(fromHex('-nAN'), NAN)
534
535 # variations in input format
536 self.identical(fromHex('1'), 1.0)
537 self.identical(fromHex('+1'), 1.0)
538 self.identical(fromHex('1.'), 1.0)
539 self.identical(fromHex('1.0'), 1.0)
540 self.identical(fromHex('1.0p0'), 1.0)
541 self.identical(fromHex('01'), 1.0)
542 self.identical(fromHex('01.'), 1.0)
543 self.identical(fromHex('0x1'), 1.0)
544 self.identical(fromHex('0x1.'), 1.0)
545 self.identical(fromHex('0x1.0'), 1.0)
546 self.identical(fromHex('+0x1.0'), 1.0)
547 self.identical(fromHex('0x1p0'), 1.0)
548 self.identical(fromHex('0X1p0'), 1.0)
549 self.identical(fromHex('0X1P0'), 1.0)
550 self.identical(fromHex('0x1P0'), 1.0)
551 self.identical(fromHex('0x1.p0'), 1.0)
552 self.identical(fromHex('0x1.0p0'), 1.0)
553 self.identical(fromHex('0x.1p4'), 1.0)
554 self.identical(fromHex('0x.1p04'), 1.0)
555 self.identical(fromHex('0x.1p004'), 1.0)
556 self.identical(fromHex('0x1p+0'), 1.0)
557 self.identical(fromHex('0x1P-0'), 1.0)
558 self.identical(fromHex('+0x1p0'), 1.0)
559 self.identical(fromHex('0x01p0'), 1.0)
560 self.identical(fromHex('0x1p00'), 1.0)
561 self.identical(fromHex(u'0x1p0'), 1.0)
562 self.identical(fromHex(' 0x1p0 '), 1.0)
563 self.identical(fromHex('\n 0x1p0'), 1.0)
564 self.identical(fromHex('0x1p0 \t'), 1.0)
565 self.identical(fromHex('0xap0'), 10.0)
566 self.identical(fromHex('0xAp0'), 10.0)
567 self.identical(fromHex('0xaP0'), 10.0)
568 self.identical(fromHex('0xAP0'), 10.0)
569 self.identical(fromHex('0xbep0'), 190.0)
570 self.identical(fromHex('0xBep0'), 190.0)
571 self.identical(fromHex('0xbEp0'), 190.0)
572 self.identical(fromHex('0XBE0P-4'), 190.0)
573 self.identical(fromHex('0xBEp0'), 190.0)
574 self.identical(fromHex('0xB.Ep4'), 190.0)
575 self.identical(fromHex('0x.BEp8'), 190.0)
576 self.identical(fromHex('0x.0BEp12'), 190.0)
577
578 # moving the point around
579 pi = fromHex('0x1.921fb54442d18p1')
580 self.identical(fromHex('0x.006487ed5110b46p11'), pi)
581 self.identical(fromHex('0x.00c90fdaa22168cp10'), pi)
582 self.identical(fromHex('0x.01921fb54442d18p9'), pi)
583 self.identical(fromHex('0x.03243f6a8885a3p8'), pi)
584 self.identical(fromHex('0x.06487ed5110b46p7'), pi)
585 self.identical(fromHex('0x.0c90fdaa22168cp6'), pi)
586 self.identical(fromHex('0x.1921fb54442d18p5'), pi)
587 self.identical(fromHex('0x.3243f6a8885a3p4'), pi)
588 self.identical(fromHex('0x.6487ed5110b46p3'), pi)
589 self.identical(fromHex('0x.c90fdaa22168cp2'), pi)
590 self.identical(fromHex('0x1.921fb54442d18p1'), pi)
591 self.identical(fromHex('0x3.243f6a8885a3p0'), pi)
592 self.identical(fromHex('0x6.487ed5110b46p-1'), pi)
593 self.identical(fromHex('0xc.90fdaa22168cp-2'), pi)
594 self.identical(fromHex('0x19.21fb54442d18p-3'), pi)
595 self.identical(fromHex('0x32.43f6a8885a3p-4'), pi)
596 self.identical(fromHex('0x64.87ed5110b46p-5'), pi)
597 self.identical(fromHex('0xc9.0fdaa22168cp-6'), pi)
598 self.identical(fromHex('0x192.1fb54442d18p-7'), pi)
599 self.identical(fromHex('0x324.3f6a8885a3p-8'), pi)
600 self.identical(fromHex('0x648.7ed5110b46p-9'), pi)
601 self.identical(fromHex('0xc90.fdaa22168cp-10'), pi)
602 self.identical(fromHex('0x1921.fb54442d18p-11'), pi)
603 # ...
604 self.identical(fromHex('0x1921fb54442d1.8p-47'), pi)
605 self.identical(fromHex('0x3243f6a8885a3p-48'), pi)
606 self.identical(fromHex('0x6487ed5110b46p-49'), pi)
607 self.identical(fromHex('0xc90fdaa22168cp-50'), pi)
608 self.identical(fromHex('0x1921fb54442d18p-51'), pi)
609 self.identical(fromHex('0x3243f6a8885a30p-52'), pi)
610 self.identical(fromHex('0x6487ed5110b460p-53'), pi)
611 self.identical(fromHex('0xc90fdaa22168c0p-54'), pi)
612 self.identical(fromHex('0x1921fb54442d180p-55'), pi)
613
614
615 # results that should overflow...
616 self.assertRaises(OverflowError, fromHex, '-0x1p1024')
617 self.assertRaises(OverflowError, fromHex, '0x1p+1025')
618 self.assertRaises(OverflowError, fromHex, '+0X1p1030')
619 self.assertRaises(OverflowError, fromHex, '-0x1p+1100')
620 self.assertRaises(OverflowError, fromHex, '0X1p123456789123456789')
621 self.assertRaises(OverflowError, fromHex, '+0X.8p+1025')
622 self.assertRaises(OverflowError, fromHex, '+0x0.8p1025')
623 self.assertRaises(OverflowError, fromHex, '-0x0.4p1026')
624 self.assertRaises(OverflowError, fromHex, '0X2p+1023')
625 self.assertRaises(OverflowError, fromHex, '0x2.p1023')
626 self.assertRaises(OverflowError, fromHex, '-0x2.0p+1023')
627 self.assertRaises(OverflowError, fromHex, '+0X4p+1022')
628 self.assertRaises(OverflowError, fromHex, '0x1.ffffffffffffffp+1023')
629 self.assertRaises(OverflowError, fromHex, '-0X1.fffffffffffff9p1023')
630 self.assertRaises(OverflowError, fromHex, '0X1.fffffffffffff8p1023')
631 self.assertRaises(OverflowError, fromHex, '+0x3.fffffffffffffp1022')
632 self.assertRaises(OverflowError, fromHex, '0x3fffffffffffffp+970')
633 self.assertRaises(OverflowError, fromHex, '0x10000000000000000p960')
634 self.assertRaises(OverflowError, fromHex, '-0Xffffffffffffffffp960')
635
636 # ...and those that round to +-max float
637 self.identical(fromHex('+0x1.fffffffffffffp+1023'), MAX)
638 self.identical(fromHex('-0X1.fffffffffffff7p1023'), -MAX)
639 self.identical(fromHex('0X1.fffffffffffff7fffffffffffffp1023'), MAX)
640
641 # zeros
642 self.identical(fromHex('0x0p0'), 0.0)
643 self.identical(fromHex('0x0p1000'), 0.0)
644 self.identical(fromHex('-0x0p1023'), -0.0)
645 self.identical(fromHex('0X0p1024'), 0.0)
646 self.identical(fromHex('-0x0p1025'), -0.0)
647 self.identical(fromHex('0X0p2000'), 0.0)
648 self.identical(fromHex('0x0p123456789123456789'), 0.0)
649 self.identical(fromHex('-0X0p-0'), -0.0)
650 self.identical(fromHex('-0X0p-1000'), -0.0)
651 self.identical(fromHex('0x0p-1023'), 0.0)
652 self.identical(fromHex('-0X0p-1024'), -0.0)
653 self.identical(fromHex('-0x0p-1025'), -0.0)
654 self.identical(fromHex('-0x0p-1072'), -0.0)
655 self.identical(fromHex('0X0p-1073'), 0.0)
656 self.identical(fromHex('-0x0p-1074'), -0.0)
657 self.identical(fromHex('0x0p-1075'), 0.0)
658 self.identical(fromHex('0X0p-1076'), 0.0)
659 self.identical(fromHex('-0X0p-2000'), -0.0)
660 self.identical(fromHex('-0x0p-123456789123456789'), -0.0)
661
662 # values that should underflow to 0
663 self.identical(fromHex('0X1p-1075'), 0.0)
664 self.identical(fromHex('-0X1p-1075'), -0.0)
665 self.identical(fromHex('-0x1p-123456789123456789'), -0.0)
666 self.identical(fromHex('0x1.00000000000000001p-1075'), TINY)
667 self.identical(fromHex('-0x1.1p-1075'), -TINY)
668 self.identical(fromHex('0x1.fffffffffffffffffp-1075'), TINY)
669
670 # check round-half-even is working correctly near 0 ...
671 self.identical(fromHex('0x1p-1076'), 0.0)
672 self.identical(fromHex('0X2p-1076'), 0.0)
673 self.identical(fromHex('0X3p-1076'), TINY)
674 self.identical(fromHex('0x4p-1076'), TINY)
675 self.identical(fromHex('0X5p-1076'), TINY)
676 self.identical(fromHex('0X6p-1076'), 2*TINY)
677 self.identical(fromHex('0x7p-1076'), 2*TINY)
678 self.identical(fromHex('0X8p-1076'), 2*TINY)
679 self.identical(fromHex('0X9p-1076'), 2*TINY)
680 self.identical(fromHex('0xap-1076'), 2*TINY)
681 self.identical(fromHex('0Xbp-1076'), 3*TINY)
682 self.identical(fromHex('0xcp-1076'), 3*TINY)
683 self.identical(fromHex('0Xdp-1076'), 3*TINY)
684 self.identical(fromHex('0Xep-1076'), 4*TINY)
685 self.identical(fromHex('0xfp-1076'), 4*TINY)
686 self.identical(fromHex('0x10p-1076'), 4*TINY)
687 self.identical(fromHex('-0x1p-1076'), -0.0)
688 self.identical(fromHex('-0X2p-1076'), -0.0)
689 self.identical(fromHex('-0x3p-1076'), -TINY)
690 self.identical(fromHex('-0X4p-1076'), -TINY)
691 self.identical(fromHex('-0x5p-1076'), -TINY)
692 self.identical(fromHex('-0x6p-1076'), -2*TINY)
693 self.identical(fromHex('-0X7p-1076'), -2*TINY)
694 self.identical(fromHex('-0X8p-1076'), -2*TINY)
695 self.identical(fromHex('-0X9p-1076'), -2*TINY)
696 self.identical(fromHex('-0Xap-1076'), -2*TINY)
697 self.identical(fromHex('-0xbp-1076'), -3*TINY)
698 self.identical(fromHex('-0xcp-1076'), -3*TINY)
699 self.identical(fromHex('-0Xdp-1076'), -3*TINY)
700 self.identical(fromHex('-0xep-1076'), -4*TINY)
701 self.identical(fromHex('-0Xfp-1076'), -4*TINY)
702 self.identical(fromHex('-0X10p-1076'), -4*TINY)
703
704 # ... and near MIN ...
705 self.identical(fromHex('0x0.ffffffffffffd6p-1022'), MIN-3*TINY)
706 self.identical(fromHex('0x0.ffffffffffffd8p-1022'), MIN-2*TINY)
707 self.identical(fromHex('0x0.ffffffffffffdap-1022'), MIN-2*TINY)
708 self.identical(fromHex('0x0.ffffffffffffdcp-1022'), MIN-2*TINY)
709 self.identical(fromHex('0x0.ffffffffffffdep-1022'), MIN-2*TINY)
710 self.identical(fromHex('0x0.ffffffffffffe0p-1022'), MIN-2*TINY)
711 self.identical(fromHex('0x0.ffffffffffffe2p-1022'), MIN-2*TINY)
712 self.identical(fromHex('0x0.ffffffffffffe4p-1022'), MIN-2*TINY)
713 self.identical(fromHex('0x0.ffffffffffffe6p-1022'), MIN-2*TINY)
714 self.identical(fromHex('0x0.ffffffffffffe8p-1022'), MIN-2*TINY)
715 self.identical(fromHex('0x0.ffffffffffffeap-1022'), MIN-TINY)
716 self.identical(fromHex('0x0.ffffffffffffecp-1022'), MIN-TINY)
717 self.identical(fromHex('0x0.ffffffffffffeep-1022'), MIN-TINY)
718 self.identical(fromHex('0x0.fffffffffffff0p-1022'), MIN-TINY)
719 self.identical(fromHex('0x0.fffffffffffff2p-1022'), MIN-TINY)
720 self.identical(fromHex('0x0.fffffffffffff4p-1022'), MIN-TINY)
721 self.identical(fromHex('0x0.fffffffffffff6p-1022'), MIN-TINY)
722 self.identical(fromHex('0x0.fffffffffffff8p-1022'), MIN)
723 self.identical(fromHex('0x0.fffffffffffffap-1022'), MIN)
724 self.identical(fromHex('0x0.fffffffffffffcp-1022'), MIN)
725 self.identical(fromHex('0x0.fffffffffffffep-1022'), MIN)
726 self.identical(fromHex('0x1.00000000000000p-1022'), MIN)
727 self.identical(fromHex('0x1.00000000000002p-1022'), MIN)
728 self.identical(fromHex('0x1.00000000000004p-1022'), MIN)
729 self.identical(fromHex('0x1.00000000000006p-1022'), MIN)
730 self.identical(fromHex('0x1.00000000000008p-1022'), MIN)
731 self.identical(fromHex('0x1.0000000000000ap-1022'), MIN+TINY)
732 self.identical(fromHex('0x1.0000000000000cp-1022'), MIN+TINY)
733 self.identical(fromHex('0x1.0000000000000ep-1022'), MIN+TINY)
734 self.identical(fromHex('0x1.00000000000010p-1022'), MIN+TINY)
735 self.identical(fromHex('0x1.00000000000012p-1022'), MIN+TINY)
736 self.identical(fromHex('0x1.00000000000014p-1022'), MIN+TINY)
737 self.identical(fromHex('0x1.00000000000016p-1022'), MIN+TINY)
738 self.identical(fromHex('0x1.00000000000018p-1022'), MIN+2*TINY)
739
740 # ... and near 1.0.
741 self.identical(fromHex('0x0.fffffffffffff0p0'), 1.0-EPS)
742 self.identical(fromHex('0x0.fffffffffffff1p0'), 1.0-EPS)
743 self.identical(fromHex('0X0.fffffffffffff2p0'), 1.0-EPS)
744 self.identical(fromHex('0x0.fffffffffffff3p0'), 1.0-EPS)
745 self.identical(fromHex('0X0.fffffffffffff4p0'), 1.0-EPS)
746 self.identical(fromHex('0X0.fffffffffffff5p0'), 1.0-EPS/2)
747 self.identical(fromHex('0X0.fffffffffffff6p0'), 1.0-EPS/2)
748 self.identical(fromHex('0x0.fffffffffffff7p0'), 1.0-EPS/2)
749 self.identical(fromHex('0x0.fffffffffffff8p0'), 1.0-EPS/2)
750 self.identical(fromHex('0X0.fffffffffffff9p0'), 1.0-EPS/2)
751 self.identical(fromHex('0X0.fffffffffffffap0'), 1.0-EPS/2)
752 self.identical(fromHex('0x0.fffffffffffffbp0'), 1.0-EPS/2)
753 self.identical(fromHex('0X0.fffffffffffffcp0'), 1.0)
754 self.identical(fromHex('0x0.fffffffffffffdp0'), 1.0)
755 self.identical(fromHex('0X0.fffffffffffffep0'), 1.0)
756 self.identical(fromHex('0x0.ffffffffffffffp0'), 1.0)
757 self.identical(fromHex('0X1.00000000000000p0'), 1.0)
758 self.identical(fromHex('0X1.00000000000001p0'), 1.0)
759 self.identical(fromHex('0x1.00000000000002p0'), 1.0)
760 self.identical(fromHex('0X1.00000000000003p0'), 1.0)
761 self.identical(fromHex('0x1.00000000000004p0'), 1.0)
762 self.identical(fromHex('0X1.00000000000005p0'), 1.0)
763 self.identical(fromHex('0X1.00000000000006p0'), 1.0)
764 self.identical(fromHex('0X1.00000000000007p0'), 1.0)
765 self.identical(fromHex('0x1.00000000000007ffffffffffffffffffffp0'),
766 1.0)
767 self.identical(fromHex('0x1.00000000000008p0'), 1.0)
768 self.identical(fromHex('0x1.00000000000008000000000000000001p0'),
769 1+EPS)
770 self.identical(fromHex('0X1.00000000000009p0'), 1.0+EPS)
771 self.identical(fromHex('0x1.0000000000000ap0'), 1.0+EPS)
772 self.identical(fromHex('0x1.0000000000000bp0'), 1.0+EPS)
773 self.identical(fromHex('0X1.0000000000000cp0'), 1.0+EPS)
774 self.identical(fromHex('0x1.0000000000000dp0'), 1.0+EPS)
775 self.identical(fromHex('0x1.0000000000000ep0'), 1.0+EPS)
776 self.identical(fromHex('0X1.0000000000000fp0'), 1.0+EPS)
777 self.identical(fromHex('0x1.00000000000010p0'), 1.0+EPS)
778 self.identical(fromHex('0X1.00000000000011p0'), 1.0+EPS)
779 self.identical(fromHex('0x1.00000000000012p0'), 1.0+EPS)
780 self.identical(fromHex('0X1.00000000000013p0'), 1.0+EPS)
781 self.identical(fromHex('0X1.00000000000014p0'), 1.0+EPS)
782 self.identical(fromHex('0x1.00000000000015p0'), 1.0+EPS)
783 self.identical(fromHex('0x1.00000000000016p0'), 1.0+EPS)
784 self.identical(fromHex('0X1.00000000000017p0'), 1.0+EPS)
785 self.identical(fromHex('0x1.00000000000017ffffffffffffffffffffp0'),
786 1.0+EPS)
787 self.identical(fromHex('0x1.00000000000018p0'), 1.0+2*EPS)
788 self.identical(fromHex('0X1.00000000000018000000000000000001p0'),
789 1.0+2*EPS)
790 self.identical(fromHex('0x1.00000000000019p0'), 1.0+2*EPS)
791 self.identical(fromHex('0X1.0000000000001ap0'), 1.0+2*EPS)
792 self.identical(fromHex('0X1.0000000000001bp0'), 1.0+2*EPS)
793 self.identical(fromHex('0x1.0000000000001cp0'), 1.0+2*EPS)
794 self.identical(fromHex('0x1.0000000000001dp0'), 1.0+2*EPS)
795 self.identical(fromHex('0x1.0000000000001ep0'), 1.0+2*EPS)
796 self.identical(fromHex('0X1.0000000000001fp0'), 1.0+2*EPS)
797 self.identical(fromHex('0x1.00000000000020p0'), 1.0+2*EPS)
798
799 def test_roundtrip(self):
800 def roundtrip(x):
801 return fromHex(toHex(x))
802
803 for x in [NAN, INF, self.MAX, self.MIN, self.MIN-self.TINY, self.TINY, 0.0]:
804 self.identical(x, roundtrip(x))
805 self.identical(-x, roundtrip(-x))
806
807 # fromHex(toHex(x)) should exactly recover x, for any non-NaN float x.
808 import random
809 for i in xrange(10000):
810 e = random.randrange(-1200, 1200)
811 m = random.random()
812 s = random.choice([1.0, -1.0])
813 try:
814 x = s*ldexp(m, e)
815 except OverflowError:
816 pass
817 else:
818 self.identical(x, fromHex(toHex(x)))
819
Christian Heimes6f341092008-04-18 23:13:07 +0000820
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000821def test_main():
822 test_support.run_unittest(
Amaury Forgeot d'Arcfeb8cad2008-09-06 20:53:51 +0000823 GeneralFloatCases,
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000824 FormatFunctionsTestCase,
825 UnknownFormatTestCase,
Christian Heimes284d9272007-12-10 22:28:56 +0000826 IEEEFormatTestCase,
Christian Heimes0a8143f2007-12-18 23:22:54 +0000827 ReprTestCase,
828 InfNanTest,
Mark Dickinson7103aa42008-07-15 19:08:33 +0000829 HexFloatTestCase,
Christian Heimesf15c66e2007-12-11 00:54:34 +0000830 )
Michael W. Hudsonba283e22005-05-27 15:23:20 +0000831
832if __name__ == '__main__':
833 test_main()