Hi, I'm using python with zope. I used _.round and it seems like it is look unstable. For example if i have one number, 1130.975 and rounded it to 2 decimal places, it will be 1130.97 and not 1130.98. But for certain numbers it will rounded correctly if the last digit is 5. I even used fixedpoint.py but still i've got the same problem. This applies only to numbers that have digit 5 after the decimal places that needs to be rounded. Any idea why? TIA
On Monday 31 May 2004 06:08 pm, Norlin Abd Rahim wrote:
Hi,
I'm using python with zope. I used _.round and it seems like it is look unstable. For example if i have one number, 1130.975 and rounded it to 2 decimal places, it will be 1130.97 and not 1130.98. But for certain numbers it will rounded correctly if the last digit is 5. I even used fixedpoint.py but still i've got the same problem. This applies only to numbers that have digit 5 after the decimal places that needs to be rounded.
Any idea why?
TIA
Perhaps the last digit is 5 only because the printing rounded ...4999... or some such up to 5. Unless you KNOW that the number is exactly 1130.975 (in its internal representation) then you must consider this as a possibility. Gary Herron
Hi,
I'm using python with zope. I used _.round and it seems like it is look unstable. For example if i have one number, 1130.975 and rounded it to 2 decimal places, it will be 1130.97 and not 1130.98. But for certain numbers it will rounded correctly if the last digit is 5. I even used fixedpoint
the number actually comes from multiplication between 237.50 and 4.762 which will get 1130.975. So it suppose to be 1130.98 isnt it if it'll get rounded to 2 decimal places. -------Original Message------- From: gherron@islandtraining.com Date: Tuesday, June 01, 2004 08:38:32 To: Norlin Abd Rahim; python-dev@python.org Subject: Re: [Python-Dev] rounding problem On Monday 31 May 2004 06:08 pm, Norlin Abd Rahim wrote: py
but still i've got the same problem. This applies only to numbers that have digit 5 after the decimal places that needs to be rounded.
Any idea why?
TIA
Perhaps the last digit is 5 only because the printing rounded ....4999... or some such up to 5. Unless you KNOW that the number is exactly 1130.975 (in its internal representation) then you must consider this as a possibility. Gary Herron .
On Monday 31 May 2004 06:22 pm, Norlin Abd Rahim wrote:
the number actually comes from multiplication between 237.50 and 4.762 which will get 1130.975. So it suppose to be 1130.98 isnt it if it'll get rounded to 2 decimal places.
But... Due to the wonders and inaccuracies of binary floating point representations on digital computers, 4.762 can not be represented EXACTLY. The closest it can get is 4.7619999999999996, and that's where you're inaccuracy comes from. In the following interactive python session, the values printed out are printed out to their full precision (that's a often misunderstood feature of the python interactive mode). You can see where the floating point inaccuracies are introduced.
237.50 237.5 4.762 4.7619999999999996 237.50 * 4.762 1130.9749999999999
Just for clarification .. This is NOT a problem with Python, but is a feature of the binary representation of floating point numbers and can be found on all computers and in all programming languages. Gary Herron
[Norlin Abd Rahim]
the number actually comes from multiplication between 237.50 and 4.762 which will get 1130.975.
Please read the Appendix on floating point issues in the Python Tutorial: http://docs.python.org/tut/node15.html The actual fp product of those two numbers is strictly less than 1130.975. You shouldn't have any such problem with FixedPoint if you're using it correctly, but python-dev isn't the right place for this (so if you want more on this, please use the newsgroup comp.lang.python).
Norlin> the number actually comes from multiplication between 237.50 and Norlin> 4.762 which will get 1130.975. So it suppose to be 1130.98 isnt Norlin> it if it'll get rounded to 2 decimal places. Nope. 1130.975 is not exactly representable. In fact, it's internal representation is slightly less that 1130.975, so rounding to two decimal places is correctly 1130.97. % python Python 2.4a0 (#25, May 22 2004, 15:16:21) [GCC 3.3 20030304 (Apple Computer, Inc. build 1493)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> 1130.975 1130.9749999999999 Skip
participants (4)
-
Gary Herron
-
Norlin Abd Rahim
-
Skip Montanaro
-
Tim Peters