LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-05-2009, 08:58 AM   #1
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
Question 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?
 
Old 02-05-2009, 09:08 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
What about this?
Code:
>>> 0.1
0.10000000000000001
Reason explained here: http://docs.python.org/tutorial/floatingpoint.html.
 
Old 02-05-2009, 09:12 AM   #3
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Original Poster
Rep: Reputation: 65
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?"
 
Old 02-05-2009, 09:16 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
 
Old 02-05-2009, 10:15 AM   #5
Randux
Senior Member
 
Registered: Feb 2006
Location: Siberia
Distribution: Slackware & Slamd64. What else is there?
Posts: 1,705

Rep: Reputation: 55
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!
 
Old 02-05-2009, 10:21 AM   #6
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Original Poster
Rep: Reputation: 65
It's a beautiful language.

Again, thank you for your responses colucix.
 
Old 02-05-2009, 07:04 PM   #7
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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

Last edited by jlinkels; 02-05-2009 at 07:06 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
No floating-point when cross-compiling Python for Mipsel Sytse Linux - Newbie 0 12-05-2008 11:56 AM
floating point exception srkbapanapalli Linux - Newbie 1 03-26-2008 11:38 AM
Floating point exception ramesh6056 Linux - Software 1 05-29-2007 01:14 AM
Floating Point in kernel 2.4.28 lucs Slackware 1 10-19-2005 08:33 AM
Floating point exception hemk76 Linux - Software 1 05-06-2005 11:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration