The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Origin C
 Floating number precision
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

zeron

Germany
Posts

Posted - 07/26/2007 :  05:27:44 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Version (Select Help-->About Origin): 7.5885
Operating System: Windows XP

Hello,
I recently discovered a problem with calculating simple differences of floating numbers. For simplification I reduced the code to finally just one line:


void test()
{
float d= 1.2-0.3;
}


which I checked with the debugger and there it says d= 0.8999999761581421. Same code but with double precision: d= 0.8999999999999999.
Why doesn't it calculate the correct number of 0.9?
The problem became apparent when I tried to calculate something like this:

int x= (int)(1.2-0.3)/0.1+1;

where I wanted to use just typecasting to convert into integer, because I always know the result should be some integer. But in the shown case the result is 9 instead of 10, because it calculates to 9.999... and the truncates. So I have to always use round to get the right result. For sure, I can live with that, but I was really wondering about that. Is it really meant this way? Shouldn't it give the exact result? Or is there some compiler switch or something like that? Thanks in advance.

Mike Buess

USA
3037 Posts

Posted - 07/26/2007 :  08:38:17 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
See http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems and other sections of that Floating Point topic.

Mike Buess
Origin WebRing Member
Go to Top of Page

zeron

Germany
Posts

Posted - 07/26/2007 :  08:47:44 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hmm. Interesting. I've never been aware of these inaccuracies. But I already made some workaround. Thanks again.
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000