[gcalctool/gcalctool-newui2] Use base 10



commit e7c5104703de2786ac1cd0034b75378356a63978
Author: Robert Ancell <robert ancell gmail com>
Date:   Sat Jul 11 07:12:56 2009 +1000

    Use base 10

 src/mp.c |   32 +++++++++++++++++++++-----------
 src/mp.h |   11 +++++++++--
 2 files changed, 30 insertions(+), 13 deletions(-)
---
diff --git a/src/mp.c b/src/mp.c
index 1755fe1..c153a28 100644
--- a/src/mp.c
+++ b/src/mp.c
@@ -92,17 +92,21 @@ mpunfl(MPNumber *z)
 static int
 pow_ii(int x, int n)
 {
-    int pow = 1;
+    int p = 1;
+    
+    if (n <= 0)
+        return 1;
 
-    if (n > 0) {
-        for (;;) { 
-            if (n & 01) pow *= x;
-            if (n >>= 1) x *= x;
-            else break;
-        }
+    for (;;) { 
+        if (n & 01)
+            p *= x;
+        if (n >>= 1)
+            x *= x;
+        else
+            break;
     }
 
-    return(pow);
+    return p;
 }
 
 
@@ -166,7 +170,7 @@ mpext(int i, int j, MPNumber *x)
 void
 mp_init(int accuracy)
 {
-    int i, k, w;
+    int i, k, w, b;
 
     /* DETERMINE LARGE REPRESENTABLE INTEGER W OF FORM 2**K - 1 */
     /*  ON CYBER 76 HAVE TO FIND K <= 47, SO ONLY LOOP
@@ -202,6 +206,12 @@ mp_init(int accuracy)
 
     /* B IS THE LARGEST POWER OF 2 SUCH THAT (8*B*B-1) <= W */
     MP.b = pow_ii(2, (k - 3) / 2);
+    
+    /* Make a multiple of 10 so fractions can be represented exactly */
+    b = 1;
+    while (MP.b % (10 * b) != MP.b)
+        b *= 10;
+    MP.b = b;
 
     /* 2E0 BELOW ENSURES AT LEAST ONE GUARD DIGIT */
     MP.t = (int) ((float) (accuracy) * log((float)10.) / log((float) MP.b) + 
@@ -607,14 +617,14 @@ mp_divide(const MPNumber *x, const MPNumber *y, MPNumber *z)
     int i, ie, iz3;
     MPNumber t;
 
-    /* CHECK FOR DIVISION BY ZERO */
+    /* x/0 */
     if (y->sign == 0) {
         mperr("*** ATTEMPTED DIVISION BY ZERO IN CALL TO MP_DIVIDE ***");
         mp_set_from_integer(0, z);
         return;
     }
 
-    /* CHECK FOR X = 0 */
+    /* 0/y = 0 */
     if (x->sign == 0) {
         mp_set_from_integer(0, z);
         return;
diff --git a/src/mp.h b/src/mp.h
index 1fc782a..2e8433f 100644
--- a/src/mp.h
+++ b/src/mp.h
@@ -52,8 +52,15 @@ typedef struct
    /* Exponent (to base MP.b) */
    int exponent;
 
-   /* Normalized fraction */
-   int fraction[MP_SIZE]; // Size MP.t?
+   /* Normalized fraction
+    * 
+    * If exponent > 0, contains 'exponent' elements
+    * x = sign * (fraction[0]*MP.b^(exponent-1) + fraction[1]*MP.b^(exponent-2) + ...)
+    * 
+    * If exponent <= 0, contains 1-'exponent' elements
+    * x = sign * (... + fraction[0]*MP.b^(exponent-2) + fraction[1]*MP.b(^exponent-1))
+    */
+   int fraction[MP_SIZE];
 } MPNumber;
 
 typedef enum { MP_RADIANS, MP_DEGREES, MP_GRADIANS } MPAngleUnit;



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