[goffice] Complex: minor improvement for polar coordinates.



commit 9104e4e602d08a65f528d786d537d95a92d4b699
Author: Morten Welinder <terra gnome org>
Date:   Tue Dec 17 08:35:20 2013 -0500

    Complex: minor improvement for polar coordinates.
    
    We now special-case the diagonals too.

 ChangeLog                 |    5 +++++
 goffice/math/go-complex.c |   18 +++++++++++++++---
 2 files changed, 20 insertions(+), 3 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 4c90f66..654ab94 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2013-12-17  Morten Welinder  <terra gnome org>
+
+       * goffice/math/go-complex.c (go_complex_from_polar_pi)
+       (go_complex_angle_pi): Handle diagonals too.
+
 2013-12-16  Morten Welinder  <terra gnome org>
 
        * tests/test-quad.c (main): Test hypot.
diff --git a/goffice/math/go-complex.c b/goffice/math/go-complex.c
index b1cedf7..aff1051 100644
--- a/goffice/math/go-complex.c
+++ b/goffice/math/go-complex.c
@@ -200,6 +200,7 @@ static  void
 SUFFIX(go_complex_from_polar_pi) (COMPLEX *dst, DOUBLE mod, DOUBLE angle)
 {
        DOUBLE s, c;
+       gboolean neg;
 
        if (SUFFIX(fabs) (angle) >= 1) {
                angle = SUFFIX(fmod) (angle, 2);
@@ -209,19 +210,27 @@ SUFFIX(go_complex_from_polar_pi) (COMPLEX *dst, DOUBLE mod, DOUBLE angle)
                        angle += 2;             
        }
 
+       neg = (angle < 0);  /* Ignores -0 */
+       angle = SUFFIX(fabs) (angle);
+
        if (angle == 0)
                s = 0, c = 1;
+       else if (angle == 0.25)
+               s = c = SUFFIX(sqrt) (0.5);
        else if (angle == 0.5)
                s = 1, c = 0;
+       else if (angle == 0.75)
+               s = SUFFIX(sqrt) (0.5), c = -s;
        else if (angle == 1)
                s = 0, c = -1;
-       else if (angle == -0.5)
-               s = -1, c = 0;
        else {
                s = SUFFIX(sin) (angle * M_PIgo);
                c = SUFFIX(cos) (angle * M_PIgo);
        }
 
+       if (neg)
+               s = -s;
+
        SUFFIX(go_complex_init) (dst, mod * c, mod * s);
 }
 
@@ -480,13 +489,16 @@ 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);
 
-       /* We could do quarters too */
+       if (SUFFIX(fabs) (src->re) == SUFFIX(fabs) (src->im))
+               return (src->im > 0 ? +1 : -1) * (src->im > 0 ? 0.25 : 0.75);
 
        /* Fallback.  */
        return SUFFIX(go_complex_angle) (src) / M_PIgo;


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