[ADMB Users] atan2 for dvariables gives wrong result

Mirko Scholz mscholz5 at gwdg.de
Sun Mar 6 04:38:20 PST 2011


the following code gives different results for atan2 depending
if it operates on dvariables or doubles.  The following versions
of ADMB have this issue


#include <fvar.hpp>

int main(void)
	independent_variables s(1,2);
	s[1] = -0.977396;
	s[2] = -0.211418;

	gradient_structure gs;
	dvar_vector ss(1,2);
	ss[1] = s[1];
	ss[2] = s[2];
	// wrong value

	dvector st(1,2);
	st[1] = value(ss[1]);
	st[2] = value(ss[2]);
	// correct value
	cout<<"atan2(double,double)="<<atan2(st[1], st[2])<<endl;


	return 0;

I feel not able to fix it directly since my understanding of ADMB
is rather limited currently, but the following workaround fixed 
the issue for me

ph = atan(ss[1]/ss[2]);
if (ss[1]>0 && ss[2]>0) ph = ph; 
else if (ss[1]>0 && ss[2]<0) ph = M_PI+ph;
else if (ss[1]<0 && ss[2]<0) ph = ph-M_PI; 
else if (ss[1]<0 && ss[2]>0) ph = ph; 
// ph = atan2(ss[1], ss[2])


Mirko Scholz

More information about the Users mailing list