Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rounding when converting float tuple to str #51259

Closed
scientist47 mannequin opened this issue Sep 28, 2009 · 6 comments
Closed

Rounding when converting float tuple to str #51259

scientist47 mannequin opened this issue Sep 28, 2009 · 6 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@scientist47
Copy link
Mannequin

scientist47 mannequin commented Sep 28, 2009

BPO 7010
Nosy @rhettinger, @mdickinson, @ericvsmith

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2009-09-28.09:14:34.492>
created_at = <Date 2009-09-28.07:43:56.917>
labels = ['interpreter-core', 'type-bug', 'invalid']
title = 'Rounding when converting float tuple to str'
updated_at = <Date 2009-09-28.18:14:07.067>
user = 'https://bugs.python.org/scientist47'

bugs.python.org fields:

activity = <Date 2009-09-28.18:14:07.067>
actor = 'rhettinger'
assignee = 'none'
closed = True
closed_date = <Date 2009-09-28.09:14:34.492>
closer = 'mark.dickinson'
components = ['Interpreter Core']
creation = <Date 2009-09-28.07:43:56.917>
creator = 'scientist47'
dependencies = []
files = []
hgrepos = []
issue_num = 7010
keywords = []
message_count = 6.0
messages = ['93193', '93195', '93196', '93197', '93202', '93222']
nosy_count = 4.0
nosy_names = ['rhettinger', 'mark.dickinson', 'eric.smith', 'scientist47']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue7010'
versions = ['Python 2.6', 'Python 3.0']

@scientist47
Copy link
Mannequin Author

scientist47 mannequin commented Sep 28, 2009

When a floating point value is stored, the actual value stored is
something at most some small number eps larger or smaller than the
original value. Python knows this, so if it stores 0.1, and then prints
the stored value, it rounds off decimals less significant than eps.
For some reason, if it prints a tuple with the same value, it doesn't do
the rounding properly. This behavior is incorrect, and very annoying for
instance when printing manually entered constants in tuples.

'''Shows that floats in tuples are not rounded like floats.
>>> print(.1)
0.1
>>> print((.1,))
(0.10000000000000001,)
'''
import doctest
doctest.testmod(verbose=True)

@scientist47 scientist47 mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Sep 28, 2009
@ericvsmith
Copy link
Member

What OS, processor, and Python version are you running this code on?

From your example, it's Python 3.x. 3.1 has a completely rewritten
float->decimal conversion system, and I get different results.

>>> print(.1)
0.1
>>> print((.1,))
(0.1,)
>>> 

For 2.6 and 3.0, you'll get the old behavior, and this won't change. Are
you really using Python 3.0 (which is marked in the versions)? If so,
switch to 3.1 and see what you get.

@mdickinson
Copy link
Member

This is expected (and correct) behaviour for Python 2.x and 3.0. Note
that the first example calls .1.__str__, while the second calls .1.__repr__.

Python 2.6.2 (r262:71600, Aug 26 2009, 09:40:44)
[GCC 4.2.1 (SUSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> str(.1)
'0.1'
>>> repr(.1)
'0.10000000000000001'
>>> str((.1,))
'(0.10000000000000001,)'
>>> tuple(str(x) for x in (.1,))
('0.1',)

@mdickinson
Copy link
Member

And as Eric says, you shouldn't see this behaviour in Python 3.1, since
there str(0.1) == repr(0.1) == '0.1'. If you are seeing it in 3.1, then
something interesting's happening: please reopen in that case. :)

@scientist47
Copy link
Mannequin Author

scientist47 mannequin commented Sep 28, 2009

I tried Python 3.1, and it does indeed not have this issue. Thanks for
swift response!

@rhettinger
Copy link
Contributor

You're seeing the difference between str(.1) and repr(.1). When
printing the tuple, you're getting the str of the tuple but just the
repr of its contents ('%.17g'). When printing the number directly,
you're just getting the str of the number which is rounded to fewer
decimal places for display ('%.12g').

This is easily seen with a number like pi:

>>> from math import *
>>> print(pi)
3.14159265359
>>> print((pi,))
(3.141592653589793,)
>>> repr(pi)
'3.141592653589793'
>>> str(pi)
'3.14159265359'

In Py3.1, the repr computation was changed to show the shortest string
the where float(x)==x. In the case of .1, the repr and str value happen
to be the same (that is why Eric sees no difference in 3.1).

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants