20.5 * 0.2 = 4.10000000000000005
No it ruddy doesn't it is 4.1
20.5 / 5 = 4.0999999999999996
Java is just as bad at this so its not a Python fault (although with Java at least its consistently bad, Python is inconsitently bad as it just defaults to the C implementation on the box).
Technorati Tags: SOA, Service Architecture
3 comments:
python:
>>> from decimal import Decimal
>>> Decimal("20.5")/5
Decimal("4.1")
>>> print Decimal("20.5")/5
4.1
This is not a Java problem. It is a binary arithmetic problem, common to all language environments. If you need accuracy, avoid using float and double. The answers you're seeing are "precise" but not "accurate."
"Most terminating decimal fractions cannot be exactly represented as terminating binary fractions."
-- Joe Darcy, Java Floating-Point Czar
http://mindprod.com/jglossfloatingpoint.html has an article on floating point.
"[0.1 is a repeater fraction in binary.] It is like the repeater fraction 1/3 = 0.33333 in base 10. When you add 0.333333... to 0.666666... why are you not astonished to get 0.999999... rather than 1.0, even though you just added 1/3 + 2/3 to get 1? Yet, with Java floating point you are astonished when you add 0.1 + 0.1 and get something other than 0.2."
Additional references on the issue of using binary floating point arithmetic and expecting accuracy:
http://www2.hursley.ibm.com/decimal/
http://docs.sun.com/source/806-3568/ncg_goldberg.html
http://en.wikipedia.org/wiki/Floating_point
"...naive use of floating point arithmetic can lead to many problems... Floating point arithmetic is at its best when it is simply being used to measure real-world quantities over a wide range of scales (such as the orbital period of Io or the mass of the proton), and at its worst when it is expected to model quantities expressed as decimal strings that are expected to be exact. An example of the latter case is financial calculations. For this reason, financial software tends not to use a binary floating-point number representation."
Post a Comment