Index: Lib/test/test_float.py =================================================================== --- Lib/test/test_float.py (revision 76596) +++ Lib/test/test_float.py (working copy) @@ -490,7 +490,30 @@ self.assertAlmostEqual(round(0.5e22, -22), 1e22) self.assertAlmostEqual(round(1.5e22, -22), 2e22) + @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"), + "test requires IEEE 754 doubles") + def test_format_specials(self): + # test formatting of nans and infs + nan = float('nan') + inf = float('inf') + for fmt in ['%e', '%f', '%g', '%.0e', '%.6f', '%.20g', + '%#e', '%#f', '%#g', '%#.20e', '%#.15f', '%#.3g']: + pfmt = '%+' + fmt[1:] + sfmt = '% ' + fmt[1:] + self.assertEqual(fmt % inf, 'inf', fmt) + self.assertEqual(fmt % -inf, '-inf', fmt) + self.assertEqual(fmt % nan, 'nan', fmt) + self.assertEqual(fmt % -nan, 'nan', fmt) + # when a sign is asked for, it should be included + # for infinities, but not for nans + self.assertEqual(pfmt % inf, '+inf', pfmt) + self.assertEqual(pfmt % -inf, '-inf', pfmt) + self.assertEqual(pfmt % nan, '+nan', pfmt) + self.assertEqual(sfmt % inf, ' inf', sfmt) + self.assertEqual(sfmt % -inf, '-inf', sfmt) + self.assertEqual(sfmt % -nan, ' nan', sfmt) + # Beginning with Python 2.6 float has cross platform compatible # ways to create and represent inf and nan class InfNanTest(unittest.TestCase):