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: 10e667.__format__('+') should return 'inf'
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.1, Python 2.7
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: cdavid, cito, dino.viehland, eric.smith, mark.dickinson, stutzbach
Priority: normal Keywords: patch

Created on 2008-12-01 17:52 by dino.viehland, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
inf-test.patch stutzbach, 2009-03-30 16:54
inf.patch stutzbach, 2009-03-30 16:54
issue4482_tests.patch mark.dickinson, 2009-04-16 23:39
issue4482_tests-1.patch eric.smith, 2009-12-01 20:58
issue4482_tests-2.patch eric.smith, 2009-12-01 21:33
Messages (18)
msg76703 - (view) Author: Dino Viehland (dino.viehland) * (Python committer) Date: 2008-12-01 17:52
10e667.__format__('+')  returns '+1.0#INF'

vs:

10e667.__format__('') which returns 'inf'

The docs say 'inf' is right.
msg84581 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2009-03-30 16:54
Here's a patch to add tests for this and a few other problems with
formatting inf/-inf/nan, as well as a patch that fixes the problems.
msg85906 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-04-12 16:23
This is a duplicate of 4799, which I've closed.

I've resisted fixing this because differences in various platform
sprintf's have made it difficult. Now that Mark Dickinson and I are
close to removing the use of sprintf for float formatting, I'll have the
tools to deal with this better.

Not sure what this means for 2.7, yet. We're not planning on backporting
the new float formatting to 2.7.
msg85907 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-04-12 16:24
Actually this isn't quite a duplicate of 4799, but it's close. Any fix
to this issue will also address 4799's original report:

> On windows, with python 2.6,  s = '%s' % float('inf') is 'inf',
> but s ='%f' % float('inf') is equal to '1.#INF'.
msg85909 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2009-04-12 16:41
The patch I submitted adds a special-case for inf/-inf/NaN so that
sprintf is avoided for those values.  Should work on any platform, I
believe.

I'm not sure how it interacts with your 3.x plans.  Seems like it would
be a good patch for 2.7 at least, though.

(and the patch that adds tests should go into both 2.7 and 3.x)
msg85910 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-04-12 16:46
Agreed. That might be the strategy for 2.7.

But it depends on how much of the other float and int formatting code I
can re-use from 3.1. If I can hide the 2.7/3.1 differences in
PyOS_double_to_string, then the 2.7 and 3.1 code should be identical,
and the fix will be different in the 3.1 code base.

I'll decide in the next few weeks.
msg86050 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-04-16 23:29
A somewhat related comment about formatting of infs and nans:  I think 
that a formatted nan should never include a sign, even when it's 
explicitly asked for.  For example:

"%+e" % float('nan')

should give "nan" rather than "+nan" or "-nan".  (In contrast, infinities 
should, and currently do, get the requested sign.)
msg86051 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-04-16 23:39
Here are my proposed tests for this---they're for percent formatting 
rather than format() formatting.  They conflict slightly with Daniel's 
tests, in that I think

format(float('inf'), '+')

should produce '+inf' rather than plain 'inf'.
msg86077 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2009-04-17 16:19
I used 'inf' instead of "+inf' because the original bug report states
that the docs say it should be 'inf'.

Now that I actually look at the docs, I don't agree with the original
report's interpretation and agree that '+inf' is better.
msg86340 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-04-22 21:52
> A somewhat related comment about formatting of infs and nans:  I think 
> that a formatted nan should never include a sign, even when it's 
> explicitly asked for.

I'm not so sure about this any more.  Sometimes the role of an explicit 
sign is to act as a binary operator or visual separator, when there's 
something immediately preceding the float being formatted.  In this case 
one still wants the sign, even for nans.

For example:

>>> "{}{:+}".format(1, float('nan'))
'1+nan'

Also, the implementation is definitely easier this way. :-)
msg86341 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-04-22 22:05
I agree on the alignment issue. Maybe if you explicitly ask for the
"sign" of a NaN, it should always be a space.

And when requesting leading zeros, you also get leading spaces instead?
msg86344 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-04-22 22:46
> Maybe if you explicitly ask for the "sign" of a NaN, it should
> always be a space.

Actually, I think '+nan' for format(float('nan'), '+'), and ' nan'
for format(float('nan'), ' ').  Then nans and infs can be treated 
identically, except that the sign of a nan is always considered to be 
positive, regardless of what the _Py_dg_dtoa function returns.

> And when requesting leading zeros, you also get leading spaces
> instead?

Should do the same thing for nans and infs here.  But certainly leading 
zeros should be avoided.  Leading spaces sounds fine to me.
msg87093 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-05-04 01:00
With the new PyOS_double_to_string, this issue largely goes away. There
are some remaining issues with commas, 'n' and the like. I'll address those.
msg95874 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-12-01 20:58
This patch has tests that currently pass on trunk. I think this is the
desired behavior. I need to augment the tests for format() testing.
msg95875 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-12-01 21:18
Looks good to me, except that the second comment in the patch doesn't make 
a lot of sense any more.
msg95876 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-12-01 21:33
This version adds format() testing and fixes the comments (I hope).
msg95880 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-12-01 21:53
LGTM.
msg95914 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2009-12-02 18:03
Added tests to trunk in r76632 and py3k in r76634.

Note that the only thing that was changed are the tests. The actual
functionality was fixed earlier when the float formatting was reworked
as part of the short float repr changes.
History
Date User Action Args
2022-04-11 14:56:41adminsetgithub: 48732
2009-12-02 18:03:15eric.smithsetstatus: open -> closed
resolution: accepted
messages: + msg95914

stage: resolved
2009-12-01 21:53:17mark.dickinsonsetmessages: + msg95880
2009-12-01 21:33:58eric.smithsetfiles: + issue4482_tests-2.patch

messages: + msg95876
2009-12-01 21:18:46mark.dickinsonsetmessages: + msg95875
2009-12-01 20:58:59eric.smithsetfiles: + issue4482_tests-1.patch

messages: + msg95874
2009-05-04 01:00:47eric.smithsetmessages: + msg87093
2009-04-22 22:46:08mark.dickinsonsetmessages: + msg86344
2009-04-22 22:05:23eric.smithsetmessages: + msg86341
2009-04-22 21:52:16mark.dickinsonsetmessages: + msg86340
2009-04-17 16:19:28stutzbachsetmessages: + msg86077
2009-04-16 23:39:10mark.dickinsonsetfiles: + issue4482_tests.patch

messages: + msg86051
2009-04-16 23:29:21mark.dickinsonsetmessages: + msg86050
2009-04-12 16:46:25eric.smithsetmessages: + msg85910
2009-04-12 16:41:41stutzbachsetmessages: + msg85909
2009-04-12 16:24:40eric.smithsetmessages: + msg85907
2009-04-12 16:23:15eric.smithsetnosy: + mark.dickinson, cito, cdavid

messages: + msg85906
versions: - Python 2.6, Python 3.0
2009-03-30 16:55:02stutzbachsetfiles: + inf.patch
2009-03-30 16:54:25stutzbachsetfiles: + inf-test.patch
versions: + Python 3.1, Python 2.7
nosy: + stutzbach

messages: + msg84581

keywords: + patch
2008-12-01 18:16:30eric.smithsetassignee: eric.smith
nosy: + eric.smith
2008-12-01 17:53:49dino.viehlandsettype: behavior
components: + Interpreter Core
2008-12-01 17:52:24dino.viehlandcreate