This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Float formatting with no type code produces incorrect output for exponential notation
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.0, Python 3.1, Python 2.7, Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: eric.smith, mark.dickinson
Priority: normal Keywords:

Created on 2009-04-04 12:20 by eric.smith, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg85386 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-04-04 12:20
If no format specifier is supplied to a float, it's supposed to work
like 'g', but with at least one digit after the decimal. This works for
non-exponential notation:

>>> format(1., '')
'1.0'
>>> format(1., 'g')
'1'

But for exponential notation, it does not:
>>> format(1e200, '')
'1e+200'
>>> 
>>> format(1e200, 'g')
'1e+200'
msg85387 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-04-04 12:23
The documentation doesn't agree with the PEP. The docs at
http://docs.python.org/library/string.html#formatstrings just say '' is
the same as 'g'. It does not draw a distinction between exponential and
fixed output.

The PEP has the "at least one digit after the decimal" language. It also
does not draw a distinction between exponential and fixed output.
msg85391 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-04-04 13:14
I don't really see this as a bug in the behaviour.  I'd always understood 
the purpose of the "at least one digit after the decimal" to be to make it 
possible to visually distinguish floats and integers, in the same way that 
repr and str already do.

>>> repr(1e100)
'1e+100'
>>> repr(1e5)
'100000.0'

If an exponent's present then the string's already clearly not an integer, 
so there's no need for the '.0'.
msg85489 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-04-05 13:26
This isn't testing what I thought it was testing. I'd forgotten that
format(x, '') is exactly equivalent to str(x).

The better test is using a format string of '<':
>>> 1e200.__format__('<')
'1.0e+200'
>>> 1e200.__format__('<g')
'1e+200'

So this is a non-issue: the current behavior matches the PEP
description. The online docs could still be tweaked, though.
History
Date User Action Args
2022-04-11 14:56:47adminsetgithub: 49936
2009-04-05 13:26:36eric.smithsetstatus: open -> closed
resolution: not a bug
messages: + msg85489
2009-04-04 13:14:26mark.dickinsonsetmessages: + msg85391
2009-04-04 12:23:22eric.smithsetnosy: + mark.dickinson
messages: + msg85387
2009-04-04 12:20:39eric.smithsettype: behavior
2009-04-04 12:20:16eric.smithcreate