Jump to content
  • Advertisement
Sign in to follow this  
tracegame

directx9 D3DXMATRIX math problem,not equal

This topic is 2077 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!