Simple addition

Piet van Oostrum piet at cs.uu.nl
Mon Feb 16 11:28:48 CET 2004


>>>>> Brian <pythonista_at_sbcglobal_dot_net at invalid.net> (B) wrote:

>>>> print str(0.1)
B> 0.1

B> It's important to realize that this is, in a real sense, an illusion: the
B> value in the machine is not exactly 1/10, you're simply rounding the
B> display of the true machine value."

On the other hand, python could have done better. There are algorithms to
print floating point numbers properly with a more pleasant output[1]:
in this particular case python could have given "0.1" also with "print 0.1".
Unfortunately most C libraries only use the stupid algorithm which often
gives some useless digits.

This is because ideally it should print the representation with the least
number of digits that when read back gives the same internal value as the
number printed. In this case that is obviously "0.1".

[1] Guy L. Steele, Jr. Jon L. White, How to print floating-point numbers
accurately, Proceedings of the ACM SIGPLAN 1990 conference on Programming
language design and implementation,  Pages: 112 - 126.
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.van.Oostrum at hccnet.nl



More information about the Python-list mailing list