Comment #5 on issue 22 by
avrbegin...@googlemail.com: atan2 incorrect
http://code.google.com/p/libfixmath/issues/detail?id=22
I'm also having difficulties with atan2. Here is the code I use to compare
atan2 for double and Fix16. I cannot expect Fix16 to be as accurate as
double, but there seems to be an offset of about 0.1 pi for some values.
Here is the code I use:
#include <cmath>
#include <fstream>
#include <iostream>
#include <fix16.h>
#include <fix16.hpp>
int main()
{
unsigned int n = 1000;
std::ofstream file("out.dat");
std::ostream& out(file);//(std::cout);
for(unsigned int i = 0; i <= n; i++)
{
double d = M_PI/2.*i/1000;
double s = sin(d);
double c = cos(d);
double a = atan2(s,c);
Fix16 df(d);
Fix16 sf(df.sin());
Fix16 cf(df.cos());
Fix16 af(sf.atan2(cf));
out << d
<< " " << a
<< " " << (double)af
<< std::endl;
}
return 0;
}
I used gnuplot for plotting the results, here is the command:
plot "out.dat" u 1:2 w l title 'double', "out.dat" u 1:3 w l title 'Fix16'
I have attached the plot.
Attachments:
doubleVsFix16.png 24.9 KB