CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Junior Member
Join Date
Mar 2009
Posts
20

Help with nan

Hi all

Yet another question from me and hope some one shoots the answer immediately.. very simple though

I have been tryin to do a rigid body simulation ... things work fine until i hit a nan ..

What I am trying to do is retrieve a angle from a quaternion for which i wrote this following function...

Code:
```
void get_AxisAngle_from_Quat(float &angle, Xlib::vec3d &axis)

{

float squareLength = get4d_X()*get4d_X() + get4d_Y()*get4d_Y() + get4d_Z()*get4d_Z();

if (squareLength>C_EPSILON)

{

angle = 2.0f * (float) acos(get4d_Z());
//assert (!XlibMath::is__nan(angle));

float inverseLength ;
inverseLength = 1.0f /  (float) pow(squareLength, 0.5f) ;

axis.x = get4d_X() * inverseLength;

axis.y = get4d_Y() * inverseLength;

axis.z = get4d_Z() * inverseLength;

}

else

{

angle = 0.0f;

axis.x = 1.0f;

axis.y = 0.0f;

axis.z = 0.0f;

}

}```
This is being refered in the code by this way
Code:
```        Xlib::vec3d axis(1,0,0);
float angle=0.0f;

RBody[0].q.get_AxisAngle_from_Quat(angle,axis);

Xlib::DevMsg("Quat  ",RBody[0].q);
Xlib::DevMsg("Angle ", 	angle);```
but This hits a nan and everytime it hits nan my object disappears from the screen.... (DevMsg is a overload for cout i use) When i try printing out the values at the point where i hit the nan this shows this

Code:
```DevMsg: Quat   1.442195 0.698514 1.700220 0.403262
Angle  nan```
I read online but it states nan is got by dividing by 0 .. but clearly i am not using any divisions at al...

can someone clarify me on where i went wrong ????

Cheers and regards

2. Senior Member
Join Date
May 2002
Posts
1,435

Re: Help with nan

You don't state where you are getting the NAN error, but your code indicates that it may be in the call to acos().

Note that the domain for arccosine is -1 to +1 so you need to determine why you are asking for the arccosine of a number outside of its domain. In other words, assert on the NAN but then back up from there to determine the cause.

3. Elite Member Power Poster
Join Date
Oct 2007
Location
Seattle, WA
Posts
10,895

Re: Help with nan

NaN isn't just division by 0---it may be used for any calculation which has an undefined result. Trying to take the acos of a vector of length 0 would do it.

4. Re: Help with nan

acos of 0 is well defined to be pi/2

5. Senior Member
Join Date
May 2002
Posts
1,435

Re: Help with nan

angle = 2.0f * (float) acos(get4d_Z());
//assert (!XlibMath::is__nan(angle));

You show the values for Xlib::vec3d axis but not for get4d_Z(). What is get4d_Z()? If it's not the Z-coordinate of a unit vector then that could be your problem.

6. Elite Member Power Poster
Join Date
Oct 2007
Location
Seattle, WA
Posts
10,895

Re: Help with nan

Originally Posted by NMTop40
acos of 0 is well defined to be pi/2
Oh, right. I was thinking of atan2.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•