[ADMB Users] atan2 for dvariables gives wrong result

Johnoel Ancheta johnoel at hawaii.edu
Thu Mar 10 16:28:02 PST 2011


Hi Mirko Scholz,

Thank you for finding and reporting the problem and also providing the
test code.  A fix has been made and is now being tested.  It should appear 
in the next release...

Cheers,
Johnoel

----- Original Message -----
From: Mirko Scholz <mscholz5 at gwdg.de>
Date: Monday, March 7, 2011 9:57 am
Subject: [ADMB Users] atan2 for dvariables gives wrong result
To: users at admb-project.org

> Hello,
> 
> the following code gives different results for atan2 depending
> if it operates on dvariables or doubles.  The following versions
> of ADMB have this issue
> 
>  admb-10.0-linux-gcc4.4.5-64bit
>  admb-10.0-linux-gcc4.4.5-64bit
> 
> 
> #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
> 	cout<<"atan2(dvariable,dvariable)="<<atan2(ss[1],ss[2])<<endl;
> 
> 	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;
> //atan2(dvariable,dvariable)=1.35777
> //atan2(double,double)=-1.78382
> 
> 	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])
> 
> 
> Regards
> 
> Mirko Scholz
> _______________________________________________
> Users mailing list
> Users at admb-project.org
> http://lists.admb-project.org/mailman/listinfo/users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.admb-project.org/pipermail/users/attachments/20110310/f62bd570/attachment.html>


More information about the Users mailing list