[comp.lang.c] atan2

tg1e+@andrew.cmu.edu (Timothy R. Gottschalk) (08/14/89)

     I need to calculate that arctan of y over x, where x,y are declared as
float.  Using the proper type-casting, I can get the atan() function to
work properly i.e.
     float result = (float)atan((double)(y/x);
However, if I use atan2() I get garbage values:
     float result = (float)atan2((double)y, (double)x);

     Has anyone used atan2() before?  Can anyone tell me if my type-casting
is incorrect?

Tim R. Gottschalk
Pittsburgh, PA

gwyn@smoke.BRL.MIL (Doug Gwyn) (08/15/89)

In article <oYtS4Cy00WB984HWR7@andrew.cmu.edu> tg1e+@andrew.cmu.edu (Timothy R. Gottschalk) writes:
>     I need to calculate that arctan of y over x, where x,y are declared as
>float.  Using the proper type-casting, I can get the atan() function to
>work properly i.e.
>     float result = (float)atan((double)(y/x);

Missing ")" in the above.

>However, if I use atan2() I get garbage values:
>     float result = (float)atan2((double)y, (double)x);

Be sure you've previously #included <math.h>.  The (float) cast is
unnecessary, but harmless.  In fact, so are the (double) casts.

Without more information about the system software you're using,
we cannot tell if your atan2() implementation is actually broken.

Generally, you should always use atan2() instead of atan().  I've
never seen a case where atan() was preferable.

Warning: many implementations consider atan2(0.0,0.0) to be an error.

thomas@uplog.se (Thomas Hameenaho) (08/25/89)

I presume you've included <math.h>

The man page for atan2 on our pyramid says that:
atan returns values in the range -pi/2 through pi/2 while
atan2 returns in the range -pi through pi.

Can this be your problem?
--
Real life:	Thomas Hameenaho		Email:	thomas@uplog.se
Snail mail:	TeleLOGIC Uppsala AB		Phone:	+46 18 189406
		Box 1218			Fax:	+46 18 132039
		S - 751 42 Uppsala, Sweden