   Accurate angle between 2 vectors?
  I need to calculated the angle between two normalised vectors. This is very easy to do right? just take the inverse cos of the dot product of the two vectors. Right! But i''ve got a problem, for very small angles, the answer i get is 0 even when using double precision floating point numbers. The reason is that the dot product is giving values of over 1 (e.g. 1.0000001192092896). And before i take the acos of it, i need to set the number equal to 1 if it is above 1.So anyway, I was wondering if anybody knows of a more accurate method for calculating the angle between 2 vectors (needs to be for 3D vectors). 
  Have you tried (2D) asin(a.x*b.y - b.x*a.y) ? Or (3D) the arc-sine of the magnitude of the cross-product ?“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”    — Brian W. Kernighan (C programming language co-inventor) [edited by - Fruny on February 7, 2004 10:03:42 PM];  http://www.gamedev.net/community/forums/topic.asp?topic_id=185968 ;quote:Original post by Fruny  Have you tried (2D) asin(a.x*b.y - b.x*a.y) ? Or (3D) the arc-sine of the magnitude of the cross-product ?Ah lol, thanks man... I completely forgot that you could get the angle from the length of the cross product... Its solved my problem perfectly, i owe you one! ;quote:Original post by SpaceDude  Its solved my problem perfectly, i owe you one!   I always collect my dues. MUHAHAHAHAHAHAHAHAHAHAHAHA!Today, SpaceDude, tomorrow, the world ! ;  Its funny cause in the thread Anon Poster linked to, I actually replied saying how there where 2 ways to calculate the angle. I guess i just wasn''t thinking straight today... ;  best way  is to use something likeatan2(s,c); (instead of atan(s/c) or asin(s) or acos(c))where c is a dot product and s is a magnitude of cross product.atan2 is a reverse of sincos function thet returns both sine and cosine of given angle :-).The problem with asin or acos,or any other function,that at point where derivative of sin or cos are zero,precision are small because derivative of asin or acos are very big.asin is wrong for >90 degrees angles.It will return 180-actualangle...it solves problem with small angles but introduces worse one for big If you haven't something like atan2 predefined,you may use //l:=sqrt(c*c+s*s);if l=0 then break;c:=c/l; s:=s/l;//remove code above if (c,s) are already normalized// use most accurate function,asin or acos.if abs(c)<1/sqrt(2) then begin angle:=acos(c); if s<0 then angle:=-angle; // remove line above if s are always positive;end else begin angle:=asin(s); if c<0 then if s>0 then angle:=pi-angle else angle:=-pi-angle;end;it's assumed that asin returns angles in ranges -pi/2..pi/2,and acos returns angles in range 0..piangle returned by function are in range -pi .. +pi(code in subset of pascal)Regards,Dmytry Lavrov.edit:there was typo in function.   edit2:again typo,i'm are also dude.note:my function atan2 could return negative angle for 2d case,say,5 degrees clockwise are positive and counterclockwise are negative.Main idea that if |c|<1/sqrt(2) acos(c) are more accurate,else,asin(c) are more accurate.And also we need to flip sign.   [edited by - Dmytry on February 10, 2004 7:52:16 AM];  fair point there i''ve changed it to use atan2 now... even though it was working before, because my angles where never above 90 degrees... but now at least i''m safe 