mirror of
https://github.com/apache/nuttx.git
synced 2026-05-31 23:40:19 +08:00
Correct atan2 implementations from Denis Arnst
This commit is contained in:
+17
-31
@@ -41,46 +41,32 @@
|
|||||||
#ifdef CONFIG_HAVE_DOUBLE
|
#ifdef CONFIG_HAVE_DOUBLE
|
||||||
double atan2(double y, double x)
|
double atan2(double y, double x)
|
||||||
{
|
{
|
||||||
if (y == 0.0)
|
if (x > 0)
|
||||||
{
|
{
|
||||||
if (x >= 0.0)
|
return atan(y / x);
|
||||||
|
}
|
||||||
|
else if (y >= 0 && x < 0)
|
||||||
|
{
|
||||||
|
return atan(y / x) + M_PI;
|
||||||
|
}
|
||||||
|
else if (y < 0)
|
||||||
|
{
|
||||||
|
if (x == 0)
|
||||||
{
|
{
|
||||||
return 0.0;
|
return -M_PI_2;
|
||||||
}
|
}
|
||||||
else
|
else /* Can only be x < 0 */
|
||||||
{
|
{
|
||||||
return M_PI;
|
return atan(y / x) - M_PI;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (y > 0.0)
|
else if (y > 0 && x == 0)
|
||||||
{
|
{
|
||||||
if (x == 0.0)
|
return M_PI_2;
|
||||||
{
|
|
||||||
return M_PI_2;
|
|
||||||
}
|
|
||||||
else if (x > 0.0)
|
|
||||||
{
|
|
||||||
return atan(y / x);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return M_PI - atan(y / x);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else if (y == 0 && x == 0) /* Undefined but returns normally 0 */
|
||||||
{
|
{
|
||||||
if (x == 0.0)
|
return 0;
|
||||||
{
|
|
||||||
return M_PI + M_PI_2;
|
|
||||||
}
|
|
||||||
else if (x > 0.0)
|
|
||||||
{
|
|
||||||
return 2 * M_PI - atan(y / x);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return M_PI + atan(y / x);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+17
-31
@@ -37,45 +37,31 @@
|
|||||||
|
|
||||||
float atan2f(float y, float x)
|
float atan2f(float y, float x)
|
||||||
{
|
{
|
||||||
if (y == 0.0)
|
if (x > 0)
|
||||||
{
|
{
|
||||||
if (x >= 0.0)
|
return atanf(y / x);
|
||||||
|
}
|
||||||
|
else if (y >= 0 && x < 0)
|
||||||
|
{
|
||||||
|
return atanf(y / x) + M_PI;
|
||||||
|
}
|
||||||
|
else if (y < 0)
|
||||||
|
{
|
||||||
|
if (x == 0)
|
||||||
{
|
{
|
||||||
return 0.0;
|
return -M_PI_2;
|
||||||
}
|
}
|
||||||
else
|
else /* Can only be x < 0 */
|
||||||
{
|
{
|
||||||
return M_PI;
|
return atanf(y / x) - M_PI;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (y > 0.0)
|
else if (y > 0 && x == 0)
|
||||||
{
|
{
|
||||||
if (x == 0.0)
|
return M_PI_2;
|
||||||
{
|
|
||||||
return M_PI_2;
|
|
||||||
}
|
|
||||||
else if (x > 0.0)
|
|
||||||
{
|
|
||||||
return atanf(y / x);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return M_PI - atanf(y / x);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else if (y == 0 && x == 0) /* Undefined but returns normally 0 */
|
||||||
{
|
{
|
||||||
if (x == 0.0)
|
return 0;
|
||||||
{
|
|
||||||
return M_PI + M_PI_2;
|
|
||||||
}
|
|
||||||
else if (x > 0.0)
|
|
||||||
{
|
|
||||||
return 2 * M_PI - atanf(y / x);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return M_PI + atanf(y / x);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-32
@@ -41,47 +41,32 @@
|
|||||||
#ifdef CONFIG_HAVE_LONG_DOUBLE
|
#ifdef CONFIG_HAVE_LONG_DOUBLE
|
||||||
long double atan2l(long double y, long double x)
|
long double atan2l(long double y, long double x)
|
||||||
{
|
{
|
||||||
|
if (x > 0)
|
||||||
if (y == 0.0)
|
|
||||||
{
|
{
|
||||||
if (x >= 0.0)
|
return atanl(y / x);
|
||||||
|
}
|
||||||
|
else if (y >= 0 && x < 0)
|
||||||
|
{
|
||||||
|
return atanl(y / x) + M_PI;
|
||||||
|
}
|
||||||
|
else if (y < 0)
|
||||||
|
{
|
||||||
|
if (x == 0)
|
||||||
{
|
{
|
||||||
return 0.0;
|
return -M_PI_2;
|
||||||
}
|
}
|
||||||
else
|
else /* Can only be x < 0 */
|
||||||
{
|
{
|
||||||
return M_PI;
|
return atanl(y / x) - M_PI;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (y > 0.0)
|
else if (y > 0 && x == 0)
|
||||||
{
|
{
|
||||||
if (x == 0.0)
|
return M_PI_2;
|
||||||
{
|
|
||||||
return M_PI_2;
|
|
||||||
}
|
|
||||||
else if (x > 0.0)
|
|
||||||
{
|
|
||||||
return atanl(y / x);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return M_PI - atanl(y / x);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else if (y == 0 && x == 0) /* Undefined but returns normally 0 */
|
||||||
{
|
{
|
||||||
if (x == 0.0)
|
return 0;
|
||||||
{
|
|
||||||
return M_PI + M_PI_2;
|
|
||||||
}
|
|
||||||
else if (x > 0.0)
|
|
||||||
{
|
|
||||||
return 2 * M_PI - atanl(y / x);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return M_PI + atanl(y / x);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user