[goffice] GOFormat: improve rendering of general format.



commit 6791e76259bd99e5f13cefe6c781777be757aaa2
Author: Morten Welinder <terra gnome org>
Date:   Mon Apr 28 14:29:11 2014 -0400

    GOFormat: improve rendering of general format.
    
    When width==-1, the general format should render values as short as
    possible.  That didn't always work right.  For example, for 75776.21
    we could get 75776.20000000001 [total of 16 digits]

 ChangeLog                 |   12 ++++++++++++
 NEWS                      |    2 ++
 goffice/math/go-dtoa.c    |   19 +++++++++++++------
 goffice/utils/go-format.c |   11 +++++++----
 tests/test-format.c       |    3 +++
 5 files changed, 37 insertions(+), 10 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index fc82bae..4ca6f57 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2014-04-28  Morten Welinder  <terra gnome org>
+
+       * tests/test-format.c (test_general_format): Simple test for
+       shortest-representation.
+
+       * goffice/utils/go-format.c (go_render_general): When we have
+       infinite room, just use go_dtoa to get the shortest
+       representation.
+
+       * goffice/math/go-dtoa.c (go_dtoa): Improve short-representation
+       handling.
+
 2014-03-24 Andreas J. Guelzow <aguelzow pyrshep ca>
 
        * goffice/utils/go-format.c (go_format_output_number_element_to_odf):
diff --git a/NEWS b/NEWS
index b75662d..e5bb826 100644
--- a/NEWS
+++ b/NEWS
@@ -12,6 +12,8 @@ Morten:
        * Portability improvements.
        * Fix UMR in graph view.
        * Improve GOFormat export to ods.
+       * Improve testing.
+       * Improve go_dtoa and use it for go_format_general.
 
 --------------------------------------------------------------------------
 goffice 0.10.14:
diff --git a/goffice/math/go-dtoa.c b/goffice/math/go-dtoa.c
index e2e42a7..1610b38 100644
--- a/goffice/math/go-dtoa.c
+++ b/goffice/math/go-dtoa.c
@@ -499,25 +499,32 @@ go_dtoa (GString *dst, const char *fmt, ...)
                const char *dec = (fl & FLAG_ASCII)
                        ? "."
                        : go_locale_get_decimal()->str;
-               const char *dot = strstr (dst->str + oldlen, dec);
-               if (dot) {
+               gboolean again = TRUE;
+               while (again) {
+                       GString *alt;
+                       const char *dot = strstr (dst->str + oldlen, dec);
+                       long double dalt;
+                       if (!dot)
+                               break;
+
                        /*
                         * This is crude.  We have a dot, so try to render
                         * the same number with less precision and check
                         * that the result round-trips.
                         */
-                       GString *alt = g_string_new (NULL);
-                       long double dalt;
 
+                       alt = g_string_new (NULL);
                        fmt_fp (alt, d, w, p - 1, fl, t);
-
                        dalt = strto (alt->str, is_long, (fl & FLAG_ASCII));
 
                        if (dalt == d) {
                                g_string_truncate (dst, oldlen);
                                go_string_append_gstring (dst, alt);
                                if (debug) g_printerr ("  --> %s\n", dst->str);
-                       }
+                               p--;
+                       } else
+                               again = FALSE;
+
                        g_string_free (alt, TRUE);
                }
        }
diff --git a/goffice/utils/go-format.c b/goffice/utils/go-format.c
index 0cd4258..4fd2241 100644
--- a/goffice/utils/go-format.c
+++ b/goffice/utils/go-format.c
@@ -4932,9 +4932,12 @@ SUFFIX(go_render_general) (PangoLayout *layout, GString *str,
        }
 
        if (col_width == -1) {
-               measure = go_format_measure_zero;
-               col_width = INT_MAX;
-               sign_width = 0;
+#ifdef DEBUG_GENERAL
+               g_printerr ("Rendering %.20" FORMAT_G " to needed width\n", val);
+#endif
+               go_dtoa (str, "=!^" FORMAT_G, val);
+               SETUP_LAYOUT;
+               return;
        } else {
                int w;
 
@@ -4951,7 +4954,7 @@ SUFFIX(go_render_general) (PangoLayout *layout, GString *str,
        }
 
 #ifdef DEBUG_GENERAL
-       g_printerr ("Rendering %" FORMAT_G " to width %d (<=%d digits)\n",
+       g_printerr ("Rendering %.20" FORMAT_G " to width %d (<=%d digits)\n",
                 val, col_width, maxdigits);
 #endif
        if (val == 0)
diff --git a/tests/test-format.c b/tests/test-format.c
index 551144b..97c0809 100644
--- a/tests/test-format.c
+++ b/tests/test-format.c
@@ -117,6 +117,9 @@ test_general_format (void)
        test_general_format_1 (6861116509411105.0, 16, "6.8611165094E+15");
        test_general_format_1 (6861116509411105.0, 15, "6.861116509E+15");
        test_general_format_1 (6861116509411105.0, 14, "6.86111651E+15");
+
+       /* Only 15 digits needed.  Lots of terminating zeros.  */
+       test_general_format_1 (75776.21, -1, "75776.21");
 }
 
 /* ------------------------------------------------------------------------- */


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