Decimal place issues with floats and decimal.Decimal

IEEE floating point is binary, not decimal. There is no fixed length binary fraction that is exactly 0.1, or any multiple thereof. It is a repeating fraction, like 1/3 in decimal.

Please read What Every Computer Scientist Should Know About Floating-Point Arithmetic

Other options besides a Decimal class are

  • using Common Lisp or Python 2.6 or another language with exact rationals

  • converting the doubles to close rationals using, e.g., frap

Leave a Comment