LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Strange floating-point behaviour in Python (https://www.linuxquestions.org/questions/programming-9/strange-floating-point-behaviour-in-python-702491/)

indienick 02-05-2009 08:58 AM

Strange floating-point behaviour in Python
 
At work, I - sadly - am limited to Windows for my 3D mechanical design work.

Every now and then, I have to do some basic math with a calculator but sometimes it is much easier to do things in one, long mathematical expression and have it evaluated (with proper bracketing).

I installed Python on my Window XP workstation, and I noticed something VERY strange when I did a simple calculation (let's just say this boils down to laziness):
Code:

>>> (11.175 - 3) / 2
4.0875000000000004

WTF?! Why so many zeros and that trailing "4"?

I got somewhat upset and installed "Lisp in a Box" (oh darn, eh?) and did the exact same calculation:
Code:

CL-USER> (/ (- 11.175 3) 2)
4.0875

Beautiful! Why the difference; why does Python give me a messed up value, and CLISP not?

colucix 02-05-2009 09:08 AM

What about this?
Code:

>>> 0.1
0.10000000000000001

Reason explained here: http://docs.python.org/tutorial/floatingpoint.html.

indienick 02-05-2009 09:12 AM

Thanks for the reply, and the link, colucix! :)

However, I am still left with a feeling of, "Why isn't this fixable? Why is it CLISP doesn't do that, and Python does?"

colucix 02-05-2009 09:16 AM

Maybe because of the different method to convert numbers to string upon displaying on the terminal. The following should give the expected number, since the str function consider only 12 significant digits
Code:

>>> print str((11.175 - 3) / 2)
4.0875


Randux 02-05-2009 10:15 AM

If you think that was cool try running a program to calculate factorials. I couldn't believe how fast common lisp could get the result in 100s of digits!

Note to self: learn Lisp!

indienick 02-05-2009 10:21 AM

:) It's a beautiful language.

Again, thank you for your responses colucix.

jlinkels 02-05-2009 07:04 PM

Interesting. It is well known that certain rational figures (are they called like this in English as well) cannot be represented in binary as they are represented in decimal, with a finite number of bits.

However, can someone explain this?

Code:

donald_pc:/tmp$ echo "64 k 11.175 3 - 2 / p" | dc
4.0875000000000000000000000000000000000000000000000000000000000000

DC is an aribitrary precision RPN calculator.

It is really not cheating!

Code:

donald_pc:/tmp$ echo "64 k 11.175000000000000000000000000000000000000000000000001 3 - 2 / p" | dc
4.0875000000000000000000000000000000000000000000000005000000000000

I expected some less accurate results, that is normal floating point errors.

The most amazing is, I can invert the operation with the result I got, and then the outcome is exactly the number I started with. Is there something really smart going on?

jlinkels


All times are GMT -5. The time now is 05:04 PM.