I have a problem.
I am calculating position of some points for opengl rendering.
But calculations are wrong and it results with one pixel overlap on rendered image.
And its a serious problem because it looks nasty.
Strangest thing is that error occurs with simple addition.
I am just adding 0.02 for every row and column and there is a 0,009 from nowhere.
Any ideas where its coming from?
I have tried to change compiler from arm7 to arm and it does not help
code :
for (int i=0;i<elem;i++)
{
LOGE("ROW: %d",i);
startPosX = -(float)elem*vertSize/2.0f + vertSize/2.0f;
for (int j=0;j<elem;j++)
{
LOGE("Column: %d",j);
LOGE("x: %f y: %f vs:%f",startPosX,startPosY,vertSize);
Vvertices[((i*elem*3)+j*3+0)] = startPosX;
Vvertices[((i*elem*3)+j*3+1)] = startPosY;
Vvertices[((i*elem*3)+j*3+2)] = z;
positions[i*elem+j].Set( startPosX, startPosY, z );
if(randomType)
velocity[i*elem+j].Set(getRandom()/speed,getRandom()/speed,getRandom()/speed);
else
velocity[i*elem+j].Set(0.0f,0.1f/speed,0.0f);
startPosX+=vertSize;
}
startPosY +=vertSize;
}
Column: 85
x: 0.710000 y: 0.989999 vs:0.020000
Column: 86
x: 0.729999 y: 0.989999 vs:0.020000
When you need assured precision for mathematical calculations it can sometimes be a bad idea to use types such as float, because many numbers do not have a finite representation in binary, just like some fractions do not have a finite representation in decimal.
This problem has to do with the machine representation of the numbers and not with the programming language you are using itself. Many languages with the typical ways of handling float and double types have this issue.
In Java people use classes such as BigDecimal to deal with this kind of problem.