Mailing List Archive

[issue5118] '%.2f' % 2.545 doesn't round correctly
New submission from Ultrasick <python@ontheserver.de>:

print '%.2f' % 2.544 // returns 2.54
print '%.2f' % 2.545 // returns 2.54 but should return 2.55
print '%.2f' % 2.546 // returns 2.55

----------
messages: 80868
nosy: Ultrasick
severity: normal
status: open
title: '%.2f' % 2.545 doesn't round correctly
type: behavior
versions: Python 2.6

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue5118>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue5118] '%.2f' % 2.545 doesn't round correctly [ In reply to ]
Mark Dickinson <dickinsm@gmail.com> added the comment:

This is not a bug; it's a consequence of the finite accuracy of floating-
point arithmetic. If you look at the actual value that's stored for
'2.545', you'll see that it's actually slightly less than 2.545, so
rounding it down is the correct thing to do.

>>> 2.545
2.5449999999999999

----------
nosy: +marketdickinson
resolution: -> invalid

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue5118>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue5118] '%.2f' % 2.545 doesn't round correctly [ In reply to ]
Changes by Mark Dickinson <dickinsm@gmail.com>:


----------
status: open -> closed

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue5118>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue5118] '%.2f' % 2.545 doesn't round correctly [ In reply to ]
Ultrasick <python@ontheserver.de> added the comment:

print round(2.545, 2) // returns 2.55

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue5118>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue5118] '%.2f' % 2.545 doesn't round correctly [ In reply to ]
Mark Dickinson <dickinsm@gmail.com> added the comment:

> print round(2.545, 2) // returns 2.55

Aha! Yes, that one *is* a bug (see issue #1869), though it's not one that
I regard as terribly serious, and not one that can be easily solved in all
cases.

Here's why I don't see it as particularly serious: you're rounding a value
that's just on the boundary: 2.545+tiny_error should round up, while
2.545-tiny_error should round down. But tiny (or not-so-tiny) errors are
an almost unavoidable part of working with binary floating-point
arithmetic. Additionally, whether the binary approximation stored for
2.545 is less than or greater than the true value depends on many things
(format of a C double, system C library function used for string-to-double
conversion, etc.), so in a sense either 2.55 *or* 2.54 can be defended as
a valid result, and a good numeric programmer won't write code that
depends on getting one or the other.

Having said that, if you're interested in providing a patch for issue
#1869 I'd certainly take a look.

If you care about *exact* representations of numbers with a finite number
of places after the decimal point, you may be interested in Python's
'decimal' module.

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue5118>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue5118] '%.2f' % 2.545 doesn't round correctly [ In reply to ]
Ultrasick <python@ontheserver.de> added the comment:

I am sorry but I am not a C programmer. I cannot provide any patches.

As far as I understood this issue and issue #1869 have a common problem
but this issue wouldn't be solved if issue #1869 is solved. "print
'%.2f' % 2.545" doesn't seam to use the built in round() function.
Otherwise the result would be 2.55 already as the result of round(2.545,
2) is.

So you might want to reopen the bug. But either way I don't consider
this bug as really serious either.

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue5118>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue5118] '%.2f' % 2.545 doesn't round correctly [ In reply to ]
Mark Dickinson <dickinsm@gmail.com> added the comment:

> So you might want to reopen the bug. But either way I don't consider
> this bug as really serious either.

I don't understand. As far as I can see '%.2f' % 2.545 is returning
the correct result: there is no bug here, so no need to reopen. '%.2f'
should *not* return 2.55; it should return 2.54, which is exactly what it
does. round(2.545, 2) should also return 2.54, but returns 2.55 instead;
issue 1869 is already open for this.

You're correct that the float formatting doesn't use round: it does
whatever the platform C library's sprintf does.

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue5118>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue5118] '%.2f' % 2.545 doesn't round correctly [ In reply to ]
Ultrasick <python@ontheserver.de> added the comment:

Well that's not what I have learned how rounding works. I think that's
the more common way:

0.4 -> 0
0.5 -> 1
0.6 -> 1

I hope you don't try to spread the misbehavoir of pythons way of rounding

print '%.2f' % 2.545 // returns 2.54

to the built in round() function. So that round() would also return 2.54.

The result of rounding 2.545 is 2.55 no matter how python temporarly
stores "2.545" and independent of how python does the rounding. The
result is 2.55 and not 2.54. If python doesn't deliver "2.55" as the
result of it's rounding algorithm then it's doing it wrong. And if
python does stuff wrong then it has a bug.

in my opinion

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue5118>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue5118] '%.2f' % 2.545 doesn't round correctly [ In reply to ]
Mark Dickinson <dickinsm@gmail.com> added the comment:

> result is 2.55 and not 2.54. If python doesn't deliver "2.55" as the
> result of it's rounding algorithm then it's doing it wrong. And if

Sorry, but that's just not true. I suggest that you (a) read the
section on floating-point[1] in the Python tutorial, and/or (b) ask
about this on comp.lang.python if you feel inclined---there are plenty
of people there who would be glad to explain what's going on here.

[1] http://docs.python.org/tutorial/floatingpoint.html

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue5118>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com