Sign in to follow this  
tracegame

directx9 D3DXMATRIX math problem,not equal

Recommended Posts

this is a pretty strange result.

the number displayed seems to be ok.

but with equal operation,it returns false.

i don't know what is going on here.need some help.

 

#include <stdio.h>
#include <d3dx9.h>

int main()
{
	D3DXMATRIX m1,m2,m3,m4;
	D3DXMatrixRotationY(&m1,0.1f);
	D3DXMatrixRotationY(&m2,0.1f);
	m3=m1*m2;
	D3DXMatrixRotationY(&m4,0.2f);

	for(int i=0; i<4; ++i)
	{
		for(int j = 0; j<4; ++j)
		{
			if(m3(i, j) == m4(i, j))
			{
				printf("(%d,%d) equal\n",i,j);
			}
			else
			{
				printf("(%d,%d) not equal!!!!\n",i,j);
				printf("(m3 = %f, m4 = %f) not equal!!!!\n",m3(i,j),m4(i,j));
			}
		}
	}

	return 0;
}

console result

 

(0,0) equal
(0,1) equal
(0,2) not equal!!!!
(m3 = -0.198669, m4 = -0.198669) not equal!!!!
(0,3) equal
(1,0) equal
(1,1) equal
(1,2) equal
(1,3) equal
(2,0) not equal!!!!
(m3 = 0.198669, m4 = 0.198669) not equal!!!!
(2,1) equal
(2,2) equal
(2,3) equal
(3,0) equal
(3,1) equal
(3,2) equal
(3,3) equal

Share this post


Link to post
Share on other sites

If you do any calculations using floats, you should generally expect precision errors to creep in, and you should (almost) never compare results of float calculations with ==, but use an epsilon to compare if results are approximately equal.

 

I strongly recommend (to everyone) reading Bruce Dawson's articles about floating point numbers on AltDevBlogADay, they're excellent (http://www.altdevblogaday.com/author/bruce-dawson/)

 

Your point about floating point numbers looking identical in your printf is covered, they can even wrongly look identical in the Visual Studio debugger. According to Bruce Dawson in this article (http://randomascii.wordpress.com/2012/02/11/they-sure-look-equal/) you need 9 digits to reliably disambiguate floating point numbers, so using "%1.8e" instead of "%f" would show it up.

Edited by C0lumbo

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this