[gcalctool] Move mp_cast_to_exponential_string from math-equation.c



commit 0236d614b1d45fc27697054dee3c04c55ed42666
Author: Robert Ancell <robert ancell gmail com>
Date:   Wed Apr 21 09:25:39 2010 +1000

    Move mp_cast_to_exponential_string from math-equation.c

 src/math-equation.c |   70 +-------------------------------------------------
 src/mp-convert.c    |   62 +++++++++++++++++++++++++++++++++++++++++++++
 src/mp.h            |    9 ++++++
 3 files changed, 73 insertions(+), 68 deletions(-)
---
diff --git a/src/math-equation.c b/src/math-equation.c
index c698cee..f180c94 100644
--- a/src/math-equation.c
+++ b/src/math-equation.c
@@ -1116,72 +1116,6 @@ math_equation_toggle_bit(MathEquation *equation, guint bit)
 }
 
 
-/* Convert engineering or scientific number in the given base. */
-// FIXME: Move into mp-convert.c
-static void
-make_eng_sci(MathEquation *equation, char *target, int target_len, const MPNumber *x, int default_base, int base_)
-{
-    char fixed[MAX_DIGITS], *c;
-    MPNumber t, z, base, base3, base10, base10inv, mantissa;
-    int eng, exponent = 0;
-    GString *string;
-    const char *super_digits[] = {"�", "¹", "²", "³", "�", "�", "�", "�", "�", "�"};
-
-    string = g_string_sized_new(target_len);
-
-    eng = equation->priv->format == ENG;
-
-    mp_abs(x, &z);
-    if (mp_is_negative(x))
-        g_string_append(string, "â??");
-    mp_set_from_mp(&z, &mantissa);
-
-    mp_set_from_integer(base_, &base);
-    mp_xpowy_integer(&base, 3, &base3);
-    mp_xpowy_integer(&base, 10, &base10);
-    mp_set_from_integer(1, &t);
-    mp_divide(&t, &base10, &base10inv);
-
-    if (!mp_is_zero(&mantissa)) {
-        while (!eng && mp_is_greater_equal(&mantissa, &base10)) {
-            exponent += 10;
-            mp_multiply(&mantissa, &base10inv, &mantissa);
-        }
-
-        while ((!eng &&  mp_is_greater_equal(&mantissa, &base)) ||
-                (eng && (mp_is_greater_equal(&mantissa, &base3) || exponent % 3 != 0))) {
-            exponent += 1;
-            mp_divide(&mantissa, &base, &mantissa);
-        }
-
-        while (!eng && mp_is_less_than(&mantissa, &base10inv)) {
-            exponent -= 10;
-            mp_multiply(&mantissa, &base10, &mantissa);
-        }
-
-        mp_set_from_integer(1, &t);
-        while (mp_is_less_than(&mantissa, &t) || (eng && exponent % 3 != 0)) {
-            exponent -= 1;
-            mp_multiply(&mantissa, &base, &mantissa);
-        }
-    }
-
-    mp_cast_to_string(&mantissa, default_base, base_, equation->priv->accuracy, !equation->priv->show_zeroes, fixed, MAX_DIGITS);
-    g_string_append(string, fixed);
-    g_string_append_printf(string, "Ã?10");
-    if (exponent < 0) {
-        exponent = -exponent;
-        g_string_append(string, "â?»");
-    }
-    snprintf(fixed, MAX_DIGITS, "%d", exponent);
-    for (c = fixed; *c; c++)
-        g_string_append(string, super_digits[*c - '0']);
-
-    strncpy(target, string->str, target_len);
-    g_string_free(string, TRUE);
-}
-
-
 /* Convert MP number to character string. */
 //FIXME: What to do with this?
 void
@@ -1201,10 +1135,10 @@ display_make_number(MathEquation *equation, char *target, int target_len, const
         mp_cast_to_string(x, math_equation_get_base(equation), 16, equation->priv->accuracy, !equation->priv->show_zeroes, target, target_len);
         break;
     case SCI:
-        make_eng_sci(equation, target, target_len, x, math_equation_get_base(equation), 10);
+        mp_cast_to_exponential_string(x, math_equation_get_base(equation), 10, equation->priv->accuracy, !equation->priv->show_zeroes, false, target, target_len);
         break;
     case ENG:
-        make_eng_sci(equation, target, target_len, x, math_equation_get_base(equation), 10);
+        mp_cast_to_exponential_string(x, math_equation_get_base(equation), 10, equation->priv->accuracy, !equation->priv->show_zeroes, true, target, target_len);
         break;
     }
 }
diff --git a/src/mp-convert.c b/src/mp-convert.c
index 6d4f85a..90c5f0a 100644
--- a/src/mp-convert.c
+++ b/src/mp-convert.c
@@ -639,6 +639,68 @@ mp_cast_to_string(const MPNumber *x, int default_base, int base, int accuracy, b
 }
 
 
+void
+mp_cast_to_exponential_string(const MPNumber *x, int default_base, int base_, int max_digits, bool trim_zeroes, bool eng_format, char *buffer, int buffer_length)
+{
+    char fixed[1024], *c;
+    MPNumber t, z, base, base3, base10, base10inv, mantissa;
+    int exponent = 0;
+    GString *string;
+    const char *super_digits[] = {"�", "¹", "²", "³", "�", "�", "�", "�", "�", "�"};
+
+    string = g_string_sized_new(buffer_length);
+
+    mp_abs(x, &z);
+    if (mp_is_negative(x))
+        g_string_append(string, "â??");
+    mp_set_from_mp(&z, &mantissa);
+
+    mp_set_from_integer(base_, &base);
+    mp_xpowy_integer(&base, 3, &base3);
+    mp_xpowy_integer(&base, 10, &base10);
+    mp_set_from_integer(1, &t);
+    mp_divide(&t, &base10, &base10inv);
+
+    if (!mp_is_zero(&mantissa)) {
+        while (!eng_format && mp_is_greater_equal(&mantissa, &base10)) {
+            exponent += 10;
+            mp_multiply(&mantissa, &base10inv, &mantissa);
+        }
+
+        while ((!eng_format &&  mp_is_greater_equal(&mantissa, &base)) ||
+                (eng_format && (mp_is_greater_equal(&mantissa, &base3) || exponent % 3 != 0))) {
+            exponent += 1;
+            mp_divide(&mantissa, &base, &mantissa);
+        }
+
+        while (!eng_format && mp_is_less_than(&mantissa, &base10inv)) {
+            exponent -= 10;
+            mp_multiply(&mantissa, &base10, &mantissa);
+        }
+
+        mp_set_from_integer(1, &t);
+        while (mp_is_less_than(&mantissa, &t) || (eng_format && exponent % 3 != 0)) {
+            exponent -= 1;
+            mp_multiply(&mantissa, &base, &mantissa);
+        }
+    }
+
+    mp_cast_to_string(&mantissa, default_base, base_, max_digits, trim_zeroes, fixed, 1024);
+    g_string_append(string, fixed);
+    g_string_append_printf(string, "Ã?10");
+    if (exponent < 0) {
+        exponent = -exponent;
+        g_string_append(string, "â?»");
+    }
+    snprintf(fixed, 1024, "%d", exponent);
+    for (c = fixed; *c; c++)
+        g_string_append(string, super_digits[*c - '0']);
+
+    strncpy(buffer, string->str, buffer_length);
+    g_string_free(string, TRUE);
+}
+
+
 static int
 char_val(char **c, int base)
 {
diff --git a/src/mp.h b/src/mp.h
index 38d82ab..28fbbc0 100644
--- a/src/mp.h
+++ b/src/mp.h
@@ -265,6 +265,15 @@ uint64_t mp_cast_to_unsigned_int(const MPNumber *x);
  */
 void   mp_cast_to_string(const MPNumber *x, int default_base, int base, int max_digits, bool trim_zeroes, char *buffer, int buffer_length);
 
+/* Converts x to a string representation in exponential form.
+ * The string is written into 'buffer' which is guaranteed to be at least 'buffer_length' octets in size.
+ * If not enough space is available the string is truncated.
+ * The numbers are written in 'base' (e.g. 10).
+ * If 'trim_zeroes' is non-zero then strip off trailing zeroes.
+ * Fractional components are truncated at 'max_digits' digits.
+ */
+void   mp_cast_to_exponential_string(const MPNumber *x, int default_base, int base, int max_digits, bool trim_zeroes, bool eng_format, char *buffer, int buffer_length);
+
 /* Sets z = sin x */
 void   mp_sin(const MPNumber *x, MPAngleUnit unit, MPNumber *z);
 



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