classification
Title: summing two numbers-strange answer
Type: Stage:
Components: Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, mhmtyozcu001, rhettinger, vstinner
Priority: normal Keywords:

Created on 2008-10-06 23:02 by mhmtyozcu001, last changed 2008-10-07 02:00 by benjamin.peterson. This issue is now closed.

Messages (7)
msg74408 - (view) Author: Mehmet Yozcu (mhmtyozcu001) Date: 2008-10-06 23:02
I don't know what's wrong but when I write on the IDLE 
2.3+6.3
it gives an answer like 8.59999999996 or similar
when I write on the IDLE
print 2.3+6.3
the answer is 8.6 as I expected
I tried these two commands on the DrPython IDE;the result was the same.

Below, I copy-past everything on the consol screen with detail of my
system configuration. And I ask if someone can help me with this?
Thank you in advance.


mehmet@mehmet-desktop:~$ python
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) 
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 2.3+6.3
8.5999999999999996
>>> print 2.3+6.3
8.6
>>>
msg74410 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-10-06 23:07
This is a result of poor floating-point precision:

>>> 2.3
2.2999999999999998
>>> 6.3
6.2999999999999998

See http://docs.python.org/tutorial/floatingpoint.html for more information.
msg74411 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2008-10-06 23:09
Use Decimal to get an exact result:
>>> from decimal import Decimal
>>> print Decimal('2.3') + Decimal('6.3')
8.6
msg74416 - (view) Author: Mehmet Yozcu (mhmtyozcu001) Date: 2008-10-07 01:43
Dear Victor Stinner;
Thank you very much for your answer. That was very helpful
Best Wishes

2008/10/7 STINNER Victor <report@bugs.python.org>:
>
> STINNER Victor <victor.stinner@haypocalc.com> added the comment:
>
> Use Decimal to get an exact result:
>>>> from decimal import Decimal
>>>> print Decimal('2.3') + Decimal('6.3')
> 8.6
>
> ----------
> nosy: +haypo
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue4061>
> _______________________________________
>
msg74417 - (view) Author: Mehmet Yozcu (mhmtyozcu001) Date: 2008-10-07 01:46
Dear Benjamin Peterson;
Thank you very much for your answer. That was very helpful
Best Wishes

2008/10/7 Benjamin Peterson <report@bugs.python.org>:
>
> Benjamin Peterson <musiccomposition@gmail.com> added the comment:
>
> This is a result of poor floating-point precision:
>
>>>> 2.3
> 2.2999999999999998
>>>> 6.3
> 6.2999999999999998
>
> See http://docs.python.org/tutorial/floatingpoint.html for more information.
>
> ----------
> nosy: +benjamin.peterson
> resolution:  -> invalid
> status: open -> closed
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue4061>
> _______________________________________
>
msg74418 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-10-07 01:54
FWIW, what you're seeing is related to "representation error" not "poor
precision".  The numbers 2.3, 6.3, and 8.6 cannot be exactly represented
in binary floating point.  When you think about it, this is no more
surprising than knowing the fractions 1/3 and 2/3 cannot be exactly
represented in either decimal or binary floating point.
msg74419 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-10-07 02:00
On Mon, Oct 6, 2008 at 8:54 PM, Raymond Hettinger
<report@bugs.python.org> wrote:
>
> Raymond Hettinger <rhettinger@users.sourceforge.net> added the comment:
>
> FWIW, what you're seeing is related to "representation error" not "poor
> precision".  The numbers 2.3, 6.3, and 8.6 cannot be exactly represented
> in binary floating point.  When you think about it, this is no more
> surprising than knowing the fractions 1/3 and 2/3 cannot be exactly
> represented in either decimal or binary floating point.

Please excuse my poor terminology!
History
Date User Action Args
2008-10-07 02:00:16benjamin.petersonsetmessages: + msg74419
2008-10-07 01:54:32rhettingersetnosy: + rhettinger
messages: + msg74418
2008-10-07 01:46:14mhmtyozcu001setmessages: + msg74417
2008-10-07 01:43:48mhmtyozcu001setmessages: + msg74416
2008-10-06 23:09:08vstinnersetnosy: + vstinner
messages: + msg74411
2008-10-06 23:07:12benjamin.petersonsetstatus: open -> closed
resolution: not a bug
messages: + msg74410
nosy: + benjamin.peterson
2008-10-06 23:02:12mhmtyozcu001create