Higher precision -> wrong result ?

Im trying to implement a function to calculate a view matrix similar to gluLookAt, and it works fine if i define Mat4 as GLfloat[16] and Vec3 as GLfloat[3], but as soon as i use GLdouble or long double i get wrong results.

Until the call to S_Translate in GetViewMatrix all values are always the same, but then somehow m[13] gets calculated in a wrong way although x, y, z, m[1], m[5] and m[9] are always the same no matter if im using float or double.

Here is the relevant part of my program:
<div class=“ubbcode-block”><div class=“ubbcode-header”>Click to reveal… <input type=“button” class=“form-button” value=“Show me!” onclick=“toggle_spoiler(this, ‘Yikes, my eyes!’, ‘Show me!’)” />]<div style=“display: none;”>

void GetViewMatrix(Mat4& m, Vec3 camPos, Vec3 lookAt) {
	Vec3 dir, right, up;
	
	up[0] = 0.0;
	up[1] = 1.0;
	up[2] = 0.0;
	
	dir[0] = lookAt[0] - camPos[0];
	dir[1] = lookAt[1] - camPos[1];
	dir[2] = lookAt[2] - camPos[2];
	normalize(dir);
	
	crossProduct(right, dir, up);
	normalize(right);
	crossProduct(up, right, dir);
	normalize(up);
	
	m[0] = right[0];
	m[4] = right[1];
	m[8] = right[2];
	m[12] = 0.0;
	
	m[1] = up[0];
	m[5] = up[1];
	m[9] = up[2];
	m[13] = 0.0;
	
	m[2] = -dir[0];
	m[6] = -dir[1];
	m[10] = -dir[2];
	m[14] = 0.0;
	
	m[3] = m[7] = m[11] = 0.0;
	m[15] = 1.0;
	
	S_Translate(m, -camPos[0], -camPos[1], -camPos[2]);
}
void S_Translate(Mat4& m, D3Float x, D3Float y, D3Float z) {
	cout << "sizeof(D3Float): " << sizeof(D3Float) << " sizeof(Mat4): " << sizeof(Mat4) << endl;
	cout << "sizeof(GLfloat): " << sizeof(GLfloat) << " sizeof(GLdouble): " << sizeof(GLdouble) << endl;
	cout << "x: " << x << "  y: " << y << "  z: " << z << endl;
	cout << "m[1]: " << m[1] << "  m[5]: " << m[5] << "  m[9]: " << m[9] << endl;
	m[12] += x*m[0] + y*m[4] + z*m[8];
	m[13] += x*m[1] + y*m[5] + z*m[9];
	m[14] += x*m[2] + y*m[6] + z*m[10];
	cout << "Result: " << endl
		 << "	m[12]: " << m[12] << endl
		 << "	m[13]: " << m[13] << endl
		 << "	m[14]: " << m[14] << endl;
}

[/QUOTE]

Output (GLfloat):

sizeof(D3Float): 4 sizeof(Mat4): 64
sizeof(GLfloat): 4 sizeof(GLdouble): 8
x: -2  y: -3  z: -5
m[1]: -0.180743  m[5]: 0.873589  m[9]: -0.451856
Result: 
	m[12]: 0
	m[13]: 2.38419e-07
	m[14]: -6.16441

Output (GLdouble):

sizeof(D3Float): 8 sizeof(Mat4): 128
sizeof(GLfloat): 4 sizeof(GLdouble): 8
x: -2  y: -3  z: -5
m[1]: -0.180743  m[5]: 0.873589  m[9]: -0.451856
Result: 
	m[12]: 0
	m[13]: -4.44089e-16
	m[14]: -6.16441

I calculated the view matrix like it is described at http://pyopengl.sourceforge.net/documentation/manual/gluLookAt.3G.html

but as soon as i use GLdouble or long double i get wrong results.

What is “wrong” about this?

2.38419e-07 is basically 0. As is -4.44089e-16, which is even more like 0.

Floating-point math isn’t like integer math; you can’t expect perfect equality. There are rounding and precision issues. So if you want to check if they’re the same, you have to test if they’re equal within a certain range.

Doubles provide more precision than floats; that’s presumably why you’re using them. That means that doubles will not produce exactly the same results as floats; again, that’s why you’re using them. They will produce the same results within the error bounds for floating-point values.

You’re right of course… I thought that this difference in m[13] was the reason the cube my application draws does not appear when im using double and i didn’t notice that it actually didn’t matter.

The problem was that i did copy double values for the vertices into a buffer and i forgot to adjust the type parameter when calling glVertexAttribPointer - it was still GL_FLOAT

Thanks for your help :slight_smile:

HEllo…
I am new in this fourm… i want to discuss regarding algorithms…
actually my sir had given me a assignment to make a program on algorithm… but i am begginer in algo… so i dont know from where i should starts. Excluding the algorithms of sorting i have to choose any harder one. like from spaning tree or from greedy methods. so please can anyone help me out???

First of all your post has nothing to do with the topic; your just hijacking this thread - which you should not do.

Secondly you are not asking a specific question to which we can give a specific answer. You may as well ask “What’s the utimate solution to life, the universe and everything?”

Finally, and most importantly, we simply don’t do your home work or assignments. That’s for you to do and actually learn something - you know by reading and reasearching…

…and fourthly, a “plz send me the codez!” question does not generally meet with a warm reception. Anywhere.

If you were given an assignment on something you should either already know it or be in a good position to find it out. There are already enough bad programmers in the world. Here’s your opportunity to not be another.