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.
c++ double precision errors
Moderator:Moderators
Re: c++ double precision errors
Use long double instead of double.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.
- grahamwheat
- Posts:21
- Joined:Wed Aug 29, 2007 9:54 pm
- Sharp Sapphire
- Portablizer
- Posts:415
- Joined:Fri Feb 22, 2008 9:40 pm
Did you chop code out of that.. cause you could just dofor (int i = 0; i < 100; i++)
{
var += 0.003;
var2 += 0.003;
}
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...
1X PSone screen
6X 1.2v 3500mAh sub-C's
1X 1/8'' to RCA stereo Adapater
1X Radioshack repair kit
..yeaaaaah...
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:thus giving a tolerance of +-0.05
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*/
}
- 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
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.
Fight MS Paint abominations! If you don't have a camera, go here, and pick something 3 megapixels or higher.