Jump to content
  • Advertisement
Sign in to follow this  
ApLars

sin and cos floating point precision

This topic is 2561 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

Hello.
So I decided to implement some test routines for my math library and stumbled into something that I do not understand.
I'am comparing the results from my library against the counterparts in the Direct3D library and the code below actually signals that my matrix is not equal to the d3d one.



BOOST_AUTO_TEST_CASE(Matrix44_RotationX)
{
{
float angle = 0.0f;
for(unsigned int i = 0; i < 100; ++i)
{
angle = (float)math::Random(-100.0f, 100.0f);

D3DXMATRIX dxA;
D3DXMatrixRotationX(&dxA, angle);

math::Matrix44f A;
A.LoadRotateX(angle);


BOOST_CHECK(A==dxA);
}
}
}



The thing is that they are almost identical so it is a precision issue somewhere. My rotation Matrix looks like this:



template <typename T>
void Matrix44T<T>::LoadRotateX(const T& amountRad)
{
E[ 0] = (T)1.0; E[ 4] = (T)0.0; E[ 8] = (T)0.0; E[12] = (T)0.0;
E[ 1] = (T)0.0; E[ 5] = Cos(amountRad); E[ 9] = -Sin(amountRad); E[13] = (T)0.0;
E[ 2] = (T)0.0; E[ 6] = Sin(amountRad); E[10] = Cos(amountRad); E[14] = (T)0.0;
E[ 3] = (T)0.0; E[ 7] = (T)0.0; E[11] = (T)0.0; E[15] = (T)1.0;
}


[font="Arial"]So it must be something about cos and sin because the rest of the values are just 1 and 0.[/font]


What is going on here?
I can use a tolerance when comparing the matrices but I want to understand why this is happening first.

Share this post


Link to post
Share on other sites
Advertisement
It's the nature of floating point computations; unless you can replicate the exact operations in the exact same order, with the same processor settings, you should expect different results. It's just how they work. Decimal operations with limit precision are approximate, and different operations and settings results in varying approximations.

Share this post


Link to post
Share on other sites
This page

http://en.wikipedia.org/wiki/Floating_point#Minimizing_the_effect_of_accuracy_problems

will help you understand where some inaccuracies come from.

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!