mirror of
https://github.com/python/cpython.git
synced 2025-07-31 23:23:11 +00:00
Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
This commit is contained in:
parent
929ab93489
commit
a985a3aee4
9 changed files with 110 additions and 27 deletions
|
@ -257,6 +257,53 @@ class IEEEFormatTestCase(unittest.TestCase):
|
|||
self.assertEquals(math.atan2(float('-1e-1000'), -1),
|
||||
math.atan2(-0.0, -1))
|
||||
|
||||
def test_format(self):
|
||||
# these should be rewritten to use both format(x, spec) and
|
||||
# x.__format__(spec)
|
||||
|
||||
self.assertEqual(format(0.0, 'f'), '0.000000')
|
||||
|
||||
# the default is 'g', except for empty format spec
|
||||
self.assertEqual(format(0.0, ''), '0.0')
|
||||
self.assertEqual(format(0.01, ''), '0.01')
|
||||
self.assertEqual(format(0.01, 'g'), '0.01')
|
||||
|
||||
# empty presentation type should format in the same way as str
|
||||
# (issue 5920)
|
||||
x = 100/7.
|
||||
self.assertEqual(format(x, ''), str(x))
|
||||
self.assertEqual(format(x, '-'), str(x))
|
||||
self.assertEqual(format(x, '>'), str(x))
|
||||
self.assertEqual(format(x, '2'), str(x))
|
||||
|
||||
self.assertEqual(format(1.0, 'f'), '1.000000')
|
||||
|
||||
self.assertEqual(format(-1.0, 'f'), '-1.000000')
|
||||
|
||||
self.assertEqual(format( 1.0, ' f'), ' 1.000000')
|
||||
self.assertEqual(format(-1.0, ' f'), '-1.000000')
|
||||
self.assertEqual(format( 1.0, '+f'), '+1.000000')
|
||||
self.assertEqual(format(-1.0, '+f'), '-1.000000')
|
||||
|
||||
# % formatting
|
||||
self.assertEqual(format(-1.0, '%'), '-100.000000%')
|
||||
|
||||
# conversion to string should fail
|
||||
self.assertRaises(ValueError, format, 3.0, "s")
|
||||
|
||||
# other format specifiers shouldn't work on floats,
|
||||
# in particular int specifiers
|
||||
for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
|
||||
[chr(x) for x in range(ord('A'), ord('Z')+1)]):
|
||||
if not format_spec in 'eEfFgGn%':
|
||||
self.assertRaises(ValueError, format, 0.0, format_spec)
|
||||
self.assertRaises(ValueError, format, 1.0, format_spec)
|
||||
self.assertRaises(ValueError, format, -1.0, format_spec)
|
||||
self.assertRaises(ValueError, format, 1e100, format_spec)
|
||||
self.assertRaises(ValueError, format, -1e100, format_spec)
|
||||
self.assertRaises(ValueError, format, 1e-100, format_spec)
|
||||
self.assertRaises(ValueError, format, -1e-100, format_spec)
|
||||
|
||||
@unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
|
||||
"test requires IEEE 754 doubles")
|
||||
def test_format_testfile(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue