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

round(x,y) doesn't behave as expected, round error #45906

Closed
shlomoa mannequin opened this issue Dec 7, 2007 · 4 comments
Closed

round(x,y) doesn't behave as expected, round error #45906

shlomoa mannequin opened this issue Dec 7, 2007 · 4 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@shlomoa
Copy link
Mannequin

shlomoa mannequin commented Dec 7, 2007

BPO 1565
Nosy @amauryfa

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 2007-12-07.09:46:37.727>
created_at = <Date 2007-12-07.09:30:33.111>
labels = ['interpreter-core', 'invalid']
title = "round(x,y) doesn't behave as expected, round error"
updated_at = <Date 2007-12-07.10:13:48.022>
user = 'https://bugs.python.org/shlomoa'

bugs.python.org fields:

activity = <Date 2007-12-07.10:13:48.022>
actor = 'amaury.forgeotdarc'
assignee = 'none'
closed = True
closed_date = <Date 2007-12-07.09:46:37.727>
closer = 'amaury.forgeotdarc'
components = ['Interpreter Core']
creation = <Date 2007-12-07.09:30:33.111>
creator = 'shlomoa'
dependencies = []
files = []
hgrepos = []
issue_num = 1565
keywords = []
message_count = 4.0
messages = ['58266', '58267', '58268', '58269']
nosy_count = 2.0
nosy_names = ['amaury.forgeotdarc', 'shlomoa']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue1565'
versions = ['Python 2.5']

@shlomoa
Copy link
Mannequin Author

shlomoa mannequin commented Dec 7, 2007

#Round is unexpectedly wrong
>>> z = complex(1,1)
>>> s=abs(z)
>>> round(s,2)
1.4099999999999999

@shlomoa shlomoa mannequin added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Dec 7, 2007
@amauryfa
Copy link
Member

amauryfa commented Dec 7, 2007

@shlomoa
Copy link
Mannequin Author

shlomoa mannequin commented Dec 7, 2007

>>> def my_round(n,i):
...     t = n * (10**i)
...     s = round(t)
...     r = s / (10**i)
...     return r
... 
>>> print my_round(s,2)
1.41

@amauryfa
Copy link
Member

amauryfa commented Dec 7, 2007

Your function is not better:

>>> print my_round(s,2)
1.41
>>> my_round(s,2)
1.4099999999999999

print uses str(), and restricts itself to 12 significant digits.
the direct call uses repr(), and display the most precise number.

The problem is not in the computation, but is inherent to the 'float'
type which cannot represent 1.41 exactly. Please carefully read the faq
entry above.

@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)
Projects
None yet
Development

No branches or pull requests

1 participant