Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 15th, 2008, 3:35 AM   #1
kurt
Programmer
 
Join Date: Oct 2005
Posts: 68
Rep Power: 3 kurt is on a distinguished road
How to get more precise floating-point arithmetic?

How do I make a floating point arithmetic more precise?

Let me show the code first, it probably can explain my question faster than in words.

int main()
{
   double n = 0.0;

   while (n < 2.5e-9) 
   {
      // perform some operation

      n += 5.0e-9;
   }   
}
Looking at the debugger, here are the values of n for each iteration:
0
5.0000000000000001e-009
1.0000000000000000e-008
1.5000000000000002e-008
2.0000000000000000e-008
2.4999999999999999e-008 (woah ! because of this, I get an extra iteration)
2.9999999999999997e-008

Now, basically I am trying to increment a float variable by 5e-9 on each loop, but it seems like the addition is not precise. I get an extra iteration because of this "imperfection".

And oh, I need to use floating point instead of integer in the while-loop statement, so "using integer instead" cannot be the solution. And the 2.5e-9 is actually a variable, therefore in different cases, it's a different value, thus doing something like:
while (n < Var - 0.000000001)
doesn't help too, because in some cases, I might lose an iteration instead.

Anyway to prevent the extra iteration?

Thanks in advance.
kurt is offline   Reply With Quote
Old Jun 15th, 2008, 5:15 AM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
Re: How to get more precise floating-point arithmetic?

Floating points are inherently inaccurate, so using build-in comparison operators doesn't work as well as you might expect.. You might see if your compiler has a long double type (not all compilers necessarily support this type; if they don't it will be equivalent to double), but otherwise the best bet would probably be to write comparison helpers that take an epsilon into account (let epsilon be a very small number).
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Jun 16th, 2008, 2:38 PM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,886
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: How to get more precise floating-point arithmetic?

You will want to keep an integer counter, i, and calculate the double n by multiplying i by 5.0e-9.

int i = 0;
double n = 0.0;

while(n < 2.5e-9) {
    n = (++i)*5.0e-9;
}

Or else you'll get an avalanche effect occuring on the decimal values.

And then you can rewrite the while condition in terms of i, instead of n.

Also, using an epsilon smaller than your increment is a good idea (see Jimbo's post).
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Floating point representation titaniumdecoy Coder's Corner Lounge 6 Jan 29th, 2008 10:21 PM
How Do I Represent/Convert Floating Point Numbers into IEEE-754 harryc Java 13 Sep 11th, 2007 2:26 PM
How Do I Represent/Convert Floating Point Numbers into IEEE-754 harryc C++ 5 Sep 3rd, 2007 2:53 PM
Floating point range kurt Python 4 Apr 10th, 2007 4:13 PM
how to print floating point numbers eax Assembly 5 Apr 17th, 2006 7:10 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:57 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC