4 quadrant atan

Roel Schroeven rschroev_nospam_ml at fastmail.fm
Fri May 4 07:38:08 EDT 2007


Charles Sanders schreef:
> Roel Schroeven wrote:
>> I might be wrong of course, but can't you just use atan2? Only problem 
>> is that it returns negative angles for quadrants 3 and 4, but that is 
>> easily solved. In Python:
>>
>> from math import atan2, pi, fmod
>> def vectorAngle(x, y):
>>     return fmod(atan2(y, x) + 2*pi, 2*pi)
>>
>> No conditionals in sight.
>>
> 
> 	Yes, but there will be some conditionals for special
> cases in the implementation of atan2().

That's true, but the user of the atan2() doesn't need to concern himself 
with it; that's what I meant with 'in sight'.

> 
> 	I am pretty sure that in Python (and Perl) atan2() is
> the C atan2() from the C math library, also often used by
> Fortran.

I'm pretty sure too: the Python 2.4 documentation says "The math module 
consists mostly of thin wrappers around the platform C math library 
functions."

> 	This is normally written in assembler for the individual
> architecture by the hardware vendor or compiler writer, is usually
> heavily optimized, carefully handles all the corner cases, and
> often carries extra precision to ensure accuracy.

Yep, and that's why I prefer using that instead of trying to roll my 
own. And because it leverages the speed of the FPU on platforms where it 
is implemented in hardware.

I believe the FPU's of the i386 family have it in their instruction set. 
I even believe there are no instructions for asin, atan and acos; they 
are all calculated using FPATAN which calculates atan2.

-- 
If I have been able to see further, it was only because I stood
on the shoulders of giants.  -- Isaac Newton

Roel Schroeven



More information about the Python-list mailing list