summaryrefslogtreecommitdiff
path: root/nuttx
diff options
context:
space:
mode:
authorGregory Nutt <gnutt@nuttx.org>2014-09-08 07:55:09 -0600
committerGregory Nutt <gnutt@nuttx.org>2014-09-08 07:55:09 -0600
commite6d64b939a78f3d18a34add5668e9364bcea83c4 (patch)
treea5f3e06f62d80a4746357cd56a99747e64ffbd18 /nuttx
parentb6b8c83b162653d5c50d4cff14dd6d52ef5e6dea (diff)
downloadpx4-nuttx-e6d64b939a78f3d18a34add5668e9364bcea83c4.tar.gz
px4-nuttx-e6d64b939a78f3d18a34add5668e9364bcea83c4.tar.bz2
px4-nuttx-e6d64b939a78f3d18a34add5668e9364bcea83c4.zip
Correct atan2 implementations from Denis Arnst
Diffstat (limited to 'nuttx')
-rw-r--r--nuttx/libc/math/lib_atan2.c48
-rw-r--r--nuttx/libc/math/lib_atan2f.c48
-rw-r--r--nuttx/libc/math/lib_atan2l.c49
3 files changed, 51 insertions, 94 deletions
diff --git a/nuttx/libc/math/lib_atan2.c b/nuttx/libc/math/lib_atan2.c
index f78484d39..5f4a6bff0 100644
--- a/nuttx/libc/math/lib_atan2.c
+++ b/nuttx/libc/math/lib_atan2.c
@@ -41,46 +41,32 @@
#ifdef CONFIG_HAVE_DOUBLE
double atan2(double y, double x)
{
- if (y == 0.0)
+ if (x > 0)
{
- if (x >= 0.0)
- {
- return 0.0;
- }
- else
- {
- return M_PI;
- }
+ return atan(y / x);
}
- else if (y > 0.0)
+ else if (y >= 0 && x < 0)
{
- if (x == 0.0)
- {
- return M_PI_2;
- }
- else if (x > 0.0)
- {
- return atan(y / x);
- }
- else
- {
- return M_PI - atan(y / x);
- }
+ return atan(y / x) + M_PI;
}
- else
+ else if (y < 0)
{
- if (x == 0.0)
- {
- return M_PI + M_PI_2;
- }
- else if (x > 0.0)
+ if (x == 0)
{
- return 2 * M_PI - atan(y / x);
+ return -M_PI_2;
}
- else
+ else /* Can only be x < 0 */
{
- return M_PI + atan(y / x);
+ return atan(y / x) - M_PI;
}
}
+ else if (y > 0 && x == 0)
+ {
+ return M_PI_2;
+ }
+ else if (y == 0 && x == 0) /* Undefined but returns normally 0 */
+ {
+ return 0;
+ }
}
#endif
diff --git a/nuttx/libc/math/lib_atan2f.c b/nuttx/libc/math/lib_atan2f.c
index f3a92ff53..999edd6ee 100644
--- a/nuttx/libc/math/lib_atan2f.c
+++ b/nuttx/libc/math/lib_atan2f.c
@@ -37,45 +37,31 @@
float atan2f(float y, float x)
{
- if (y == 0.0)
+ if (x > 0)
{
- if (x >= 0.0)
- {
- return 0.0;
- }
- else
- {
- return M_PI;
- }
+ return atanf(y / x);
}
- else if (y > 0.0)
+ else if (y >= 0 && x < 0)
{
- if (x == 0.0)
- {
- return M_PI_2;
- }
- else if (x > 0.0)
- {
- return atanf(y / x);
- }
- else
- {
- return M_PI - atanf(y / x);
- }
+ return atanf(y / x) + M_PI;
}
- else
+ else if (y < 0)
{
- if (x == 0.0)
- {
- return M_PI + M_PI_2;
- }
- else if (x > 0.0)
+ if (x == 0)
{
- return 2 * M_PI - atanf(y / x);
+ return -M_PI_2;
}
- else
+ else /* Can only be x < 0 */
{
- return M_PI + atanf(y / x);
+ return atanf(y / x) - M_PI;
}
}
+ else if (y > 0 && x == 0)
+ {
+ return M_PI_2;
+ }
+ else if (y == 0 && x == 0) /* Undefined but returns normally 0 */
+ {
+ return 0;
+ }
}
diff --git a/nuttx/libc/math/lib_atan2l.c b/nuttx/libc/math/lib_atan2l.c
index e2a576e58..1946a3f04 100644
--- a/nuttx/libc/math/lib_atan2l.c
+++ b/nuttx/libc/math/lib_atan2l.c
@@ -41,47 +41,32 @@
#ifdef CONFIG_HAVE_LONG_DOUBLE
long double atan2l(long double y, long double x)
{
-
- if (y == 0.0)
+ if (x > 0)
{
- if (x >= 0.0)
- {
- return 0.0;
- }
- else
- {
- return M_PI;
- }
+ return atanl(y / x);
}
- else if (y > 0.0)
+ else if (y >= 0 && x < 0)
{
- if (x == 0.0)
- {
- return M_PI_2;
- }
- else if (x > 0.0)
- {
- return atanl(y / x);
- }
- else
- {
- return M_PI - atanl(y / x);
- }
+ return atanl(y / x) + M_PI;
}
- else
+ else if (y < 0)
{
- if (x == 0.0)
- {
- return M_PI + M_PI_2;
- }
- else if (x > 0.0)
+ if (x == 0)
{
- return 2 * M_PI - atanl(y / x);
+ return -M_PI_2;
}
- else
+ else /* Can only be x < 0 */
{
- return M_PI + atanl(y / x);
+ return atanl(y / x) - M_PI;
}
}
+ else if (y > 0 && x == 0)
+ {
+ return M_PI_2;
+ }
+ else if (y == 0 && x == 0) /* Undefined but returns normally 0 */
+ {
+ return 0;
+ }
}
#endif