[gnumeric] Complex: fix complex-to-string conversion.



commit 4aa65ed7c3d3265064f5eacac9d4c8fdfccd4a77
Author: Morten Welinder <terra gnome org>
Date:   Mon Jun 6 19:15:13 2022 -0400

    Complex: fix complex-to-string conversion.

 ChangeLog     |  5 +++++
 NEWS          |  1 +
 src/complex.c | 55 ++++++++++++++++++-------------------------------------
 3 files changed, 24 insertions(+), 37 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index f3ccaf990..b91119b47 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2022-06-06  Morten Welinder  <terra gnome org>
+
+       * src/complex.c (gnm_complex_to_string): Use go_dtoa as building
+       block.  Ensure uppercase "E" in exponential forms.
+
 2022-05-05  Morten Welinder  <terra gnome org>
 
        * src/xml-sax-read.c (xml_cell_set_array_expr): Fix inter-process
diff --git a/NEWS b/NEWS
index d02529067..7a9c19310 100644
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,7 @@ Morten:
        * Fix xlsx import of T.INV.
        * Fix inter-process array paste problem.  [#634]
        * Fix problem with xlsx number format 14.
+       * Fix problems with complex number rendering.  [#638]
 
 --------------------------------------------------------------------------
 Gnumeric 1.12.52
diff --git a/src/complex.c b/src/complex.c
index b9ee7984f..6a8652785 100644
--- a/src/complex.c
+++ b/src/complex.c
@@ -21,53 +21,34 @@
 char *
 gnm_complex_to_string (gnm_complex const *src, char imunit)
 {
-       char *re_buffer = NULL;
-       char *im_buffer = NULL;
-       char const *sign = "";
-       char const *suffix = "";
-       char *res;
-       char suffix_buffer[2];
-       const char *fmt = "%.*" GNM_FORMAT_g;
-       static int digits = -1;
-
-       if (digits == -1) {
-               gnm_float l10 = gnm_log10 (FLT_RADIX);
-               digits = (int)gnm_ceil (GNM_MANT_DIG * l10) +
-                       (l10 == (int)l10 ? 0 : 1);
-       }
+       GString *res = g_string_new (NULL);
+       gboolean have_real;
 
-       if (src->re != 0 || src->im == 0) {
-               /* We have a real part.  */
-               re_buffer = g_strdup_printf (fmt, digits, src->re);
+       have_real = src->re != 0 || src->im == 0;
+       if (have_real) {
+               // We have an real part.
+               go_dtoa (res, "!" GNM_FORMAT_G, src->re);
        }
 
        if (src->im != 0) {
-               /* We have an imaginary part.  */
-               suffix = suffix_buffer;
-               suffix_buffer[0] = imunit;
-               suffix_buffer[1] = 0;
+               // We have an imaginary part.
                if (src->im == 1) {
-                       if (re_buffer)
-                               sign = "+";
+                       if (have_real)
+                               g_string_append_c (res, '+');
                } else if (src->im == -1) {
-                       sign = "-";
+                       g_string_append_c (res, '-');
                } else {
-                       im_buffer = g_strdup_printf (fmt, digits, src->im);
-                       if (re_buffer && *im_buffer != '-' && *im_buffer != '+')
-                               sign = (src->im >= 0) ? "+" : "-";
+                       size_t olen = res->len;
+                       go_dtoa (res, "!" GNM_FORMAT_G, src->im);
+                       if (have_real &&
+                           res->str[olen] != '-' && res->str[olen] != '+')
+                               g_string_insert_c (res, olen,
+                                                  (src->im >= 0 ? '+' : '-'));
                }
+               g_string_append_c (res, imunit);
        }
 
-       res = g_strconcat (re_buffer ? re_buffer : "",
-                          sign,
-                          im_buffer ? im_buffer : "",
-                          suffix,
-                          NULL);
-
-       g_free (re_buffer);
-       g_free (im_buffer);
-
-       return res;
+       return g_string_free (res, FALSE);
 }
 
 /* ------------------------------------------------------------------------- */


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