c++ double precision errors

Talk about your favorite PC games, Steam and building awesome custom rigs!

Moderator:Moderators

Post Reply
User avatar
grahamwheat
Posts:21
Joined:Wed Aug 29, 2007 9:54 pm
c++ double precision errors

Post by grahamwheat » Tue Mar 25, 2008 5:30 pm

I'm making a program in which the collision detection determines the distance that an object will move and orders any other object which would be overlapped by that move to move enough to get out of the way. For example, the player sitting on a block moving up at a speed of 1.0 would be ordered to move 1.0 pixels up in order to avoid an overlap.
The program is far too long to submit in its entirety, but this is the focus of the matter:

#include <iostream>

using namespace std;

int main (int argc, char *argv[])
{
double var = 0;
double var2 = 10;
for (int i = 0; i < 100; i++)
{
var += 0.003;
var2 += 0.003;
}
if (var2-var == 10.0000000) cout << "equal" << endl;
cout << var2-var << endl;
cout << "Press ENTER to continue..." << endl;
cin.get();
return 0;
}

In line 14, I found that the difference is less than .00000000000001. var2-var equals 10 at non-decimal values.

Why is this happening? I am using Dev-C++ 4.9.9.2 and the default c++ compiler.

vb_master
Moderator
Posts:4793
Joined:Tue Jun 08, 2004 9:52 pm

Re: c++ double precision errors

Post by vb_master » Tue Mar 25, 2008 7:51 pm

grahamwheat wrote:I'm making a program in which the collision detection determines the distance that an object will move and orders any other object which would be overlapped by that move to move enough to get out of the way. For example, the player sitting on a block moving up at a speed of 1.0 would be ordered to move 1.0 pixels up in order to avoid an overlap.
The program is far too long to submit in its entirety, but this is the focus of the matter:

#include <iostream>

using namespace std;

int main (int argc, char *argv[])
{
double var = 0;
double var2 = 10;
for (int i = 0; i < 100; i++)
{
var += 0.003;
var2 += 0.003;
}
if (var2-var == 10.0000000) cout << "equal" << endl;
cout << var2-var << endl;
cout << "Press ENTER to continue..." << endl;
cin.get();
return 0;
}

In line 14, I found that the difference is less than .00000000000001. var2-var equals 10 at non-decimal values.

Why is this happening? I am using Dev-C++ 4.9.9.2 and the default c++ compiler.
Use long double instead of double.

User avatar
grahamwheat
Posts:21
Joined:Wed Aug 29, 2007 9:54 pm

Post by grahamwheat » Tue Mar 25, 2008 11:18 pm

Thanks

I tried to implement it, but it doesn't seem to work after OpenGL has been enabled.

User avatar
Sharp Sapphire
Portablizer
Posts:415
Joined:Fri Feb 22, 2008 9:40 pm

Post by Sharp Sapphire » Sun May 11, 2008 5:07 pm

for (int i = 0; i < 100; i++)
{
var += 0.003;
var2 += 0.003;
}
Did you chop code out of that.. cause you could just do

var += 0.003*100;
var2+=0.003*100;

...
Things I need to purchase for my NES Laptop:
1X PSone screen
6X 1.2v 3500mAh sub-C's
1X 1/8'' to RCA stereo Adapater
1X Radioshack repair kit

..yeaaaaah...

APHawkes
Posts:179
Joined:Fri May 02, 2008 7:50 am

Post by APHawkes » Mon May 12, 2008 5:58 am

When it comes to floating-point values it's darn near impossible to get exact values. Finding if x=10.0000 is just as hard as trying to find when x=10.39740. Round numbers don't help, and are typically worse than weird decimal values.

You have a few options. First, you could use two integer/long variables to represent values similar to scientific notation. That will give precise numbers, but you'd have to override all the typical operators to make it smooth.
Second choice is to find a math library which already has types like this. It's already been done, I'm sure.

The third option is to allow for tolerances on your collisions. This is what most people do. If you want to know when x = 10.000000 you say:

Code: Select all

y= x - 10.0d;
if (y < 0.05 || y > -0.05) {
  /*your code here*/
}
thus giving a tolerance of +-0.05

User avatar
jdmlight
Posts:795
Joined:Thu Dec 27, 2007 11:17 pm
Steam ID:jdmlight
Location:A boring suburb of Chicago.

Re: c++ double precision errors

Post by jdmlight » Sun Apr 18, 2010 12:25 am

This thread is two years old. Why was this bumped???
--John (and please call me John, it's really weird to be called by my username)
Fight MS Paint abominations! If you don't have a camera, go here, and pick something 3 megapixels or higher.

Post Reply