Linux 平台下 Atan2 导致core dump 问题
What?
经测试,在linux + RT_Preempt 平台下,<math.h>中的atan2()函数会导致系统core dump, 且随机。
How?
借鉴OpenCV库中的FastAtan2()函数,采用近似公式代替系统函数,精度可以控制在0.0002rad以内
#include <math.h>
#include <limits.h>
#include <cfloat>
#include <stdlib.h>
#define M_PI 3.14159265358979323846
static const float atan2_p1 = 0.9997878412794807f;
static const float atan2_p3 = -0.3258083974640975f;
static const float atan2_p5 = 0.1555786518463281f;
static const float atan2_p7 = -0.04432655554792128f;
// 输入值 x,y 单位为rad
float fastAtan2(float y, float x)
{
float ax = fabs(x), ay = fabs(y);//首先不分象限,求得一个锐角角度
float a, c, c2;
if (ax >= ay)
{
c = ay / (ax + (float)DBL_EPSILON);
c2 = c*c;
a = (((atan2_p7*c2 + atan2_p5)*c2 + atan2_p3)*c2 + atan2_p1)*c;
}
else
{
c = ax / (ay + (float)DBL_EPSILON);
c2 = c*c;
a = M_PI/2 - (((atan2_p7*c2 + atan2_p5)*c2 + atan2_p3)*c2 + atan2_p1)*c;
}
if (x < 0)
a = M_PI - a;
if (y < 0)
a = - a;
return a;
}