[Numpy-discussion] Arcos returns nan

David Cournapeau david@ar.media.kyoto-u.ac...
Fri Mar 28 04:22:44 CDT 2008


João Quinta da Fonseca wrote:
> I have a function that returns the dot product of two unit vectors.  
> When I try to use arcos on the values returned I sometimes get the  
> warning:
> "Warning: invalid value encountered in arccos", and the angle  
> returned is nan. I found out that this happens for essentially co- 
> linear vectors, for which the dot product function returns 1.0.  This  
> looks like 1.0 no matter how I print it but if I do: >>N.dot(a,b)>1,  
> I get: >>True.
> Now I guess this arises because of the way computers store floats but  
> shouldn't numpy take care of this somehow? Is it a bug? I don't seem  
> to have this problem with Matlab or Fortran.
>   

I am not sure I understand which behaviour you would expect. In matlab:
 >> a = 1.; b = 1 + eps;
 >> acos(a)

ans =

     0

 >> acos(b)

ans =

        0 + 2.1073e-08i

So do you expect acos to handle values outside the [1;-1] range (cos 
considered as a 'generalized' complex function) ?

cheers,

David




More information about the Numpy-discussion mailing list