Hi Dženan,

This really isn't anything to do numerically instability in Vec3f, but
with the standard float maths code that you are using in your code.

If you use acos in the way you are using it will be require a division
and if the divisor is 0 or near zero then you will always have
problems.  You just have to code defensively.  For instance you can
use atan2 instead it doesn't require you to do a division as you pass
in the two separate axis.

Robert.

2010/6/28 Dženan Zukić <[email protected]>:
> Hi,
>
> The following code occasionally leaves p with NaN value:
> [code]double cosFi=vec1*vec2/(vec1.length()*vec2.length());
> //cosine of angle between vectors
> p+=acos(cosFi)*...;[/code]
> Due to numerical round-off errors it would rarely occur that cosFi>1 
> (something like 1.00000002534), and acos would produce NaN, which would then 
> propagate further, causing a crash far away in the code.
>
> I have solved it like this:
> [code]if (cosFi>1)
>        cosFi=1;
> else if (cosFi<-1)
>        cosFi=-1;[/code]
> How common is this type of error and what is the usual way of dealing with it?
>
> Thank you!
>
> Cheers,
> Dženan
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=29431#29431
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> [email protected]
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to