[goffice] Code simplification.



commit 6fa9a2aa786711b485d275dc3ed95f6de7b953d3
Author: Morten Welinder <terra gnome org>
Date:   Wed Mar 17 18:20:29 2021 -0400

    Code simplification.

 ChangeLog                        |  6 ++++++
 NEWS                             |  3 +++
 plugins/reg_linear/gog-lin-reg.c | 27 +++++++++++++++++----------
 3 files changed, 26 insertions(+), 10 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 05d95e424..ab3599898 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2021-03-17  Morten Welinder  <terra gnome org>
+
+       * plugins/reg_linear/gog-lin-reg.c
+       (gog_lin_reg_curve_get_equation): Simplify code using fewer
+       branches.
+
 2021-03-14  Morten Welinder <terra gnome org>
 
        * configure.ac: Post-release bump.
diff --git a/NEWS b/NEWS
index 40edbf611..605abe77f 100644
--- a/NEWS
+++ b/NEWS
@@ -1,5 +1,8 @@
 goffice 0.10.50:
 
+Morten:
+       * Simplify regression equation code.
+
 --------------------------------------------------------------------------
 goffice 0.10.49:
 
diff --git a/plugins/reg_linear/gog-lin-reg.c b/plugins/reg_linear/gog-lin-reg.c
index ede8adf28..4c5d70df7 100644
--- a/plugins/reg_linear/gog-lin-reg.c
+++ b/plugins/reg_linear/gog-lin-reg.c
@@ -92,18 +92,25 @@ gog_lin_reg_curve_get_equation (GogRegCurve *curve)
 {
        if (!curve->equation) {
                GogLinRegCurve *lin = GOG_LIN_REG_CURVE (curve);
+               const char *uminus = "\xe2\x88\x92";
+               double a = curve->a[1];
+               double b = curve->a[0];
+               const char *var = "x";
+               const char *times = "";
                if (lin->affine)
-                       curve->equation = (curve->a[0] < 0.)?
-                               ((curve->a[1] < 0)?
-                                       g_strdup_printf ("y = \xE2\x88\x92%gx \xE2\x88\x92 %g", -curve->a[1], 
-curve->a[0]):
-                                       g_strdup_printf ("y = %gx \xE2\x88\x92 %g", curve->a[1], 
-curve->a[0])):
-                               ((curve->a[1] < 0)?
-                                       g_strdup_printf ("y = \xE2\x88\x92%gx + %g", -curve->a[1], 
curve->a[0]):
-                                       g_strdup_printf ("y = %gx + %g", curve->a[1], curve->a[0]));
+                       curve->equation =
+                               g_strdup_printf ("y = %s%g%s%s %s %g",
+                                                (a < 0 ? uminus : ""),
+                                                fabs (a),
+                                                times, var,
+                                                (b < 0 ? uminus : "+"),
+                                                fabs (b));
                else
-                       curve->equation = (curve->a[1] < 0)?
-                               g_strdup_printf ("y = \xE2\x88\x92%gx", -curve->a[1]):
-                               g_strdup_printf ("y = %gx", curve->a[1]);
+                       curve->equation =
+                               g_strdup_printf ("y = %s%g%s",
+                                                (a < 0 ? uminus : ""),
+                                                fabs (a),
+                                                var);
        }
        return curve->equation;
 }


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