[goffice] Math: add go_atan2pi.



commit 5e5fdbcb58c02ebb925151b37bd8614f1f35280b
Author: Morten Welinder <terra gnome org>
Date:   Wed Dec 18 13:46:29 2013 -0500

    Math: add go_atan2pi.

 ChangeLog                 |    1 +
 NEWS                      |    2 +-
 goffice/math/go-complex.c |   14 +-----------
 goffice/math/go-math.c    |   36 +++++++++++++++++++++++++++++++
 goffice/math/go-math.h    |    2 +
 tests/test-math.c         |   51 ++++++++++++++++++++++++++++++++++++++++++++-
 6 files changed, 91 insertions(+), 15 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 2ce95a4..aad4372 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,7 @@
 
        * goffice/math/go-complex.c (go_complex_from_polar_pi): Use
        go_sinpi and go_cospi.
+       (go_complex_angle_pi): Use go_atan2pi.
 
        * goffice/math/go-math.c (go_sinpi, go_cospi, go_tanpi): New
        functions.
diff --git a/NEWS b/NEWS
index acaf077..65f3726 100644
--- a/NEWS
+++ b/NEWS
@@ -8,7 +8,7 @@ Jean
 Morten:
        * Improve complex math, notably complex power.
        * Add quad precision log, atan2, hypot, asin, acos functions.
-       * Add go_sinpi, go_cospi, and go_tanpi.
+       * Add go_sinpi, go_cospi, go_tanpi, go_atan2pi.
 
 --------------------------------------------------------------------------
 goffice 0.10.9:
diff --git a/goffice/math/go-complex.c b/goffice/math/go-complex.c
index 99a2bf2..d897905 100644
--- a/goffice/math/go-complex.c
+++ b/goffice/math/go-complex.c
@@ -456,19 +456,7 @@ DOUBLE SUFFIX(go_complex_angle) (COMPLEX const *src)
 
 DOUBLE SUFFIX(go_complex_angle_pi) (COMPLEX const *src)
 {
-       /* This ignores loads of cases with -0. */
-
-       if (src->im == 0)
-               return (src->re >= 0 ? 0 : +1);
-
-       if (src->re == 0)
-               return (src->im >= 0 ? 0.5 : -0.5);
-
-       if (SUFFIX(fabs) (src->re) == SUFFIX(fabs) (src->im))
-               return (src->im > 0 ? +1 : -1) * (src->re > 0 ? 0.25 : 0.75);
-
-       /* Fallback.  */
-       return SUFFIX(go_complex_angle) (src) / M_PIgo;
+       return SUFFIX(go_atan2pi) (src->im, src->re);
 }
 
 /* ------------------------------------------------------------------------- */
diff --git a/goffice/math/go-math.c b/goffice/math/go-math.c
index ec9c14f..53a163a 100644
--- a/goffice/math/go-math.c
+++ b/goffice/math/go-math.c
@@ -969,6 +969,24 @@ go_tanpi (double x)
        return do_sin (x, k) / do_sin (x, k + 1);
 }
 
+double
+go_atan2pi (double y, double x)
+{
+       /* This ignores loads of cases with -0. */
+
+       if (y == 0)
+               return (x >= 0 ? 0 : +1);
+
+       if (x == 0)
+               return (y >= 0 ? 0.5 : -0.5);
+
+       if (fabs (x) == fabs (y))
+               return (y > 0 ? +1 : -1) * (x > 0 ? 0.25 : 0.75);
+
+       /* Fallback.  */
+       return atan2 (y, x) / M_PI;
+}
+
 #ifdef GOFFICE_WITH_LONG_DOUBLE
 
 #define M_PIgo    
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117L
@@ -1037,6 +1055,24 @@ go_tanpil (long double x)
        return do_sinl (x, k) / do_sinl (x, k + 1);
 }
 
+long double
+go_atan2pil (long double y, long double x)
+{
+       /* This ignores loads of cases with -0. */
+
+       if (y == 0)
+               return (x >= 0 ? 0 : +1);
+
+       if (x == 0)
+               return (y >= 0 ? 0.5 : -0.5);
+
+       if (fabs (x) == fabs (y))
+               return (y > 0 ? +1 : -1) * (x > 0 ? 0.25 : 0.75);
+
+       /* Fallback.  */
+       return atan2l (y, x) / M_PIgo;
+}
+
 #endif
 
 /* ------------------------------------------------------------------------- */
diff --git a/goffice/math/go-math.h b/goffice/math/go-math.h
index 3d88085..07bd147 100644
--- a/goffice/math/go-math.h
+++ b/goffice/math/go-math.h
@@ -42,6 +42,7 @@ double go_ascii_strtod (const char *s, char **end);
 double go_sinpi (double x);
 double go_cospi (double x);
 double go_tanpi (double x);
+double go_atan2pi (double y, double x);
 
 /*
  * We provide working versions of these functions for doubles.
@@ -87,6 +88,7 @@ long double go_ascii_strtold (const char *s, char **end);
 long double go_sinpil (long double x);
 long double go_cospil (long double x);
 long double go_tanpil (long double x);
+long double go_atan2pil (long double y, long double x);
 
 /*
  * We provide working versions of these functions for long doubles.
diff --git a/tests/test-math.c b/tests/test-math.c
index 0e93829..bc49bbb 100644
--- a/tests/test-math.c
+++ b/tests/test-math.c
@@ -13,12 +13,25 @@ do {                                                        \
                g_assert (fabs (r - fa) / r < 1e-14);   \
 } while (0)
 
+#define REFTEST2(a_,b_,f_,r_, txt_)                    \
+do {                                                   \
+       double a = (a_);                                \
+       double b = (b_);                                \
+       double r = (r_);                                \
+       double fab = f_(a, b);                          \
+       g_printerr ("%s(%g,%g) = %g  [%g]\n",           \
+                   txt_, a, b, fab, r);                \
+       if (r == floor (r))                             \
+               g_assert (r == fab);                    \
+       else                                            \
+               g_assert (fabs (r - fab) / r < 1e-14);  \
+} while (0)
 
 /* ------------------------------------------------------------------------- */
 
 static double fake_sinpi (double x) { return x == floor (x) ? 0 : sin (x * M_PI); }
 static double fake_cospi (double x) { return fake_sinpi (x + 0.5); }
-
+static double fake_atan2pi (double y, double x) { return atan2 (y,x) / M_PI; }
 
 
 #define TEST1(a_) do {                                 \
@@ -27,6 +40,10 @@ static double fake_cospi (double x) { return fake_sinpi (x + 0.5); }
        REFTEST(a_,go_tanpi,(fake_sinpi(a_)/fake_cospi(a_)),"tanpi");   \
 } while (0)
 
+#define TEST2(a_,b_) do {                                              \
+               REFTEST2(a_,b_,go_atan2pi,fake_atan2pi(a_,b_),"atan2pi"); \
+} while (0)
+
 static void
 trig_tests (void)
 {
@@ -36,9 +53,41 @@ trig_tests (void)
                TEST1(d);
                TEST1(-d);
        }
+
+       TEST2 (0, +2);
+       TEST2 (0, -2);
+       TEST2 (3, 0);
+       TEST2 (-3, 0);
+       TEST2 (0, 0);
+       TEST2 (+1, +1);
+       TEST2 (-1, +1);
+       TEST2 (+1, -1);
+       TEST2 (-1, -1);
+       TEST2 (+2.3, +1.2);
+       TEST2 (+2.3, -1.2);
+       TEST2 (+2.3, +0.2);
+       TEST2 (+2.3, -0.2);
+       TEST2 (+0.2, +2.3);
+       TEST2 (+0.2, -2.3);
+       TEST2 (+2.3, +3);
+       TEST2 (+2.3, -3);
+       TEST2 (-2.3, +3);
+       TEST2 (-2.3, -3);
+       TEST2 (+2.3, +4);
+       TEST2 (+2.3, -4);
+       TEST2 (-2.3, +4);
+       TEST2 (-2.3, -4);
+       TEST2 (2, 100);
+       TEST2 (2, -100);
+       TEST2 (1.0/256, 1.0/1024);
+       TEST2 (exp(1), 1);
+       TEST2 (exp(1), -1);
+       TEST2 (exp(1), log(2));
+       TEST2 (exp(1), -log(2));
 }
 
 #undef TEST1
+#undef TEST2
 
 /* ------------------------------------------------------------------------- */
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]