Jump to content

  • Log In with Google      Sign In   
  • Create Account

directx9 D3DXMATRIX math problem,not equal


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 tracegame   Members   -  Reputation: 134

Like
0Likes
Like

Posted 06 February 2013 - 09:24 AM

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


Sponsor:

#2 C0lumbo   Crossbones+   -  Reputation: 2495

Like
0Likes
Like

Posted 06 February 2013 - 10:15 AM

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, 06 February 2013 - 10:16 AM.


#3 tracegame   Members   -  Reputation: 134

Like
0Likes
Like

Posted 08 February 2013 - 08:31 PM

thank you,and i should read some c language book,i really don't know %1.8e format which did confuse me.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS