[gcalctool] Make mp_set_from_integer handle 64 bit integers Add mp_set_from_unsigned_integer Use bool in mp_cast



commit 1f7118e328351b91a43c610e2d9eb8a1079bd309
Author: Robert Ancell <robert ancell gmail com>
Date:   Mon Mar 1 10:58:24 2010 +1100

    Make mp_set_from_integer handle 64 bit integers
    Add mp_set_from_unsigned_integer
    Use bool in mp_cast_to_string

 src/mp-convert.c |   57 +++++++++++++++++++++++++++++++++++++++++------------
 src/mp.h         |    5 +++-
 2 files changed, 48 insertions(+), 14 deletions(-)
---
diff --git a/src/mp-convert.c b/src/mp-convert.c
index 9a5b8c7..23e9389 100644
--- a/src/mp-convert.c
+++ b/src/mp-convert.c
@@ -177,27 +177,58 @@ mp_set_from_double(double dx, MPNumber *z)
 void
 mp_set_from_integer(int64_t x, MPNumber *z)
 {
+    int i;
+
     memset(z, 0, sizeof(MPNumber));
 
+    if (x == 0) {
+        z->sign = 0;
+        return;
+    }
+
     if (x < 0) {
         x = -x;
         z->sign = -1;
     }
     else if (x > 0)
         z->sign = 1;
-    else
-        z->sign = 0; /* Optimisation for indicating zero */
-
-    z->exponent = 1;
-    z->fraction[0] = x;
-    while (z->fraction[0] >= MP_BASE) {
-        int i;
-        for (i = z->exponent; i >= 0; i--)
-            z->fraction[i] = z->fraction[i-1];
-        z->fraction[0] = z->fraction[1] / MP_BASE;
-        z->fraction[1] = z->fraction[1] % MP_BASE;
+
+    while (x != 0) {
+        z->fraction[z->exponent] = x % MP_BASE;
+        x = x / MP_BASE;
         z->exponent++;
     }
+    for (i = 0; i < z->exponent / 2; i++) {
+        int t = z->fraction[i];
+        z->fraction[i] = z->fraction[z->exponent - i - 1];
+        z->fraction[z->exponent - i - 1] = t;
+    }
+}
+
+
+void
+mp_set_from_unsigned_integer(uint64_t x, MPNumber *z)
+{
+    int i;
+
+    memset(z, 0, sizeof(MPNumber));
+
+    if (x == 0) {
+        z->sign = 0;
+        return;
+    }
+    z->sign = 1;
+
+    while (x != 0) {
+        z->fraction[z->exponent] = x % MP_BASE;
+        x = x / MP_BASE;
+        z->exponent++;
+    }
+    for (i = 0; i < z->exponent / 2; i++) {
+        int t = z->fraction[i];
+        z->fraction[i] = z->fraction[z->exponent - i - 1];
+        z->fraction[z->exponent - i - 1] = t;
+    }
 }
 
 
@@ -480,7 +511,7 @@ mp_cast_to_double(const MPNumber *x)
 
 
 static void
-mp_cast_to_string_real(const MPNumber *x, int base, int accuracy, int trim_zeroes, int force_sign, GString *string)
+mp_cast_to_string_real(const MPNumber *x, int base, int accuracy, bool trim_zeroes, bool force_sign, GString *string)
 {
     static char digits[] = "0123456789ABCDEF";
     MPNumber number, integer_component, fractional_component, temp;
@@ -570,7 +601,7 @@ mp_cast_to_string_real(const MPNumber *x, int base, int accuracy, int trim_zeroe
 
 
 void
-mp_cast_to_string(const MPNumber *x, int base, int accuracy, int trim_zeroes, char *buffer, int buffer_length)
+mp_cast_to_string(const MPNumber *x, int base, int accuracy, bool trim_zeroes, char *buffer, int buffer_length)
 {
     MPNumber x_real, x_im;
     GString *string;
diff --git a/src/mp.h b/src/mp.h
index 52592f3..9cc8526 100644
--- a/src/mp.h
+++ b/src/mp.h
@@ -224,6 +224,9 @@ void   mp_set_from_double(double x, MPNumber *z);
 /* Sets z = x */
 void   mp_set_from_integer(int64_t x, MPNumber *z);
 
+/* Sets z = x */
+void   mp_set_from_unsigned_integer(uint64_t x, MPNumber *z);
+
 /* Sets z = numerator ÷ denominator */
 void   mp_set_from_fraction(int64_t numerator, int64_t denominator, MPNumber *z);
 
@@ -260,7 +263,7 @@ uint64_t mp_cast_to_unsigned_int(const MPNumber *x);
  * If 'trim_zeroes' is non-zero then strip off trailing zeroes.
  * Fractional components are truncated at 'max_digits' digits.
  */
-void   mp_cast_to_string(const MPNumber *x, int base, int max_digits, int trim_zeroes, char *buffer, int buffer_length);
+void   mp_cast_to_string(const MPNumber *x, int base, int max_digits, bool trim_zeroes, 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]