gcalctool r2282 - trunk/gcalctool



Author: kniederk
Date: Sun Oct 26 13:48:32 2008
New Revision: 2282
URL: http://svn.gnome.org/viewvc/gcalctool?rev=2282&view=rev

Log:
improved API for mppi, mpsqrt, mpnzr, mproot


Modified:
   trunk/gcalctool/ce_parser.y
   trunk/gcalctool/mp-convert.c
   trunk/gcalctool/mp-internal.h
   trunk/gcalctool/mp-trigonometric.c
   trunk/gcalctool/mp.c
   trunk/gcalctool/mp.h
   trunk/gcalctool/mpmath.c

Modified: trunk/gcalctool/ce_parser.y
==============================================================================
--- trunk/gcalctool/ce_parser.y	(original)
+++ trunk/gcalctool/ce_parser.y	Sun Oct 26 13:48:32 2008
@@ -133,7 +133,7 @@
 
 value: 
   exp {cp($1, $$);}
-| tPI %prec HIGH {mppi($$);} 
+| tPI %prec HIGH {mp_get_pi($$);} 
 ;
 
 exp: 
@@ -214,7 +214,7 @@
 func:
   tLOG10 term %prec HIGH {mplogn(10, $2, $$);}
 | tLOG2 term %prec HIGH {mplogn(2, $2, $$);}
-| tSQRT term %prec HIGH {mpsqrt($2, $$);}
+| tSQRT term %prec HIGH {mp_sqrt($2, $$);}
 | tLN term %prec HIGH {mpln($2, $$);}
 | tRAND %prec HIGH {calc_rand($$);}
 | tABS term %prec HIGH {mp_abs($2, $$);}

Modified: trunk/gcalctool/mp-convert.c
==============================================================================
--- trunk/gcalctool/mp-convert.c	(original)
+++ trunk/gcalctool/mp-convert.c	Sun Oct 26 13:48:32 2008
@@ -103,7 +103,7 @@
     }
 
     /* NORMALIZE RESULT */
-    mpnzr(rs, &re, z, 0);
+    mp_get_normalized_register(rs, &re, z, 0);
 
     /* Computing MAX */
     ib = max(MP.b * 7 * MP.b, 32767) / 16;
@@ -182,7 +182,7 @@
     }
 
     /* NORMALIZE RESULT */
-    mpnzr(rs, &re, z, 0);
+    mp_get_normalized_register(rs, &re, z, 0);
 
     /* Computing MAX */
     ib = max(MP.b * 7 * MP.b, 32767) / 16;

Modified: trunk/gcalctool/mp-internal.h
==============================================================================
--- trunk/gcalctool/mp-internal.h	(original)
+++ trunk/gcalctool/mp-internal.h	Sun Oct 26 13:48:32 2008
@@ -32,7 +32,7 @@
 void mpchk(int i, int j);
 void mpgcd(int *, int *);
 void mpmul2(int *, int, int *, int);
-void mpnzr(int, int *, int *, int);
+void mp_get_normalized_register(int reg_sign, int *reg_exp, int *z, int trunc);
 void mpexp1(const int *, int *);
 void mpmulq(int *, int, int, int *);
 void mp_reciprocal(const int *, int *);

Modified: trunk/gcalctool/mp-trigonometric.c
==============================================================================
--- trunk/gcalctool/mp-trigonometric.c	(original)
+++ trunk/gcalctool/mp-trigonometric.c	Sun Oct 26 13:48:32 2008
@@ -135,11 +135,11 @@
  *
  *  1. If (x < -1  or x > 1) then report DOMAIN error and return 0.
  *
- *  2. If (x = 0) then acos(x) = PI/2.
+ *  2. If (x == 0) then acos(x) = PI/2.
  *
- *  3. If (x = 1) then acos(x) = 0
+ *  3. If (x == 1) then acos(x) = 0
  *
- *  4. If (x = -1) then acos(x) = PI.
+ *  4. If (x == -1) then acos(x) = PI.
  *
  *  5. If (0 < x < 1) then  acos(x) = atan(sqrt(1-x^2) / x)
  *
@@ -151,7 +151,7 @@
     int MP1[MP_SIZE],  MP2[MP_SIZE];
     int MPn1[MP_SIZE], MPpi[MP_SIZE], MPy[MP_SIZE];
 
-    mppi(MPpi);
+    mp_get_pi(MPpi);
     mp_set_from_integer(1, MP1);
     mp_set_from_integer(-1, MPn1);
 
@@ -167,7 +167,7 @@
     } else { 
         mpmul(x, x, MP2);
         mp_subtract(MP1, MP2, MP2);
-        mpsqrt(MP2, MP2);
+        mp_sqrt(MP2, MP2);
         mpdiv(MP2, x, MP2);
         mp_atan(MP2, MPy);
         if (x[0] > 0) {
@@ -197,7 +197,7 @@
     } else {
         mpmul(x, x, MP1);
         mp_add_integer(MP1, -1, MP1);
-        mpsqrt(MP1, MP1);
+        mp_sqrt(MP1, MP1);
         mp_add(x, MP1, MP1);
         mpln(MP1, z);
     }
@@ -231,7 +231,7 @@
         mp_subtract(&MP.r[i2 - 1], x, &MP.r[i2 - 1]);
         mp_add(&MP.r[i3 - 1], x, &MP.r[i3 - 1]);
         mpmul(&MP.r[i2 - 1], &MP.r[i3 - 1], &MP.r[i3 - 1]);
-        mproot(&MP.r[i3 - 1], -2, &MP.r[i3 - 1]);
+        mp_root(&MP.r[i3 - 1], -2, &MP.r[i3 - 1]);
         mpmul(x, &MP.r[i3 - 1], z);
         mp_atan(z, z);
         return;
@@ -244,7 +244,7 @@
     }
 
     /* X == +-1 SO RETURN +-PI/2 */
-    mppi(z);
+    mp_get_pi(z);
     mpdivi(z, MP.r[i3 - 1] << 1, z);
 }
 
@@ -260,7 +260,7 @@
  
     mpmul(x, x, MP1);
     mp_add_integer(MP1, 1, MP1);
-    mpsqrt(MP1, MP1);
+    mp_sqrt(MP1, MP1);
     mp_add(x, MP1, MP1);
     mpln(MP1, z);
 }
@@ -306,7 +306,7 @@
         q <<= 1;
         mpmul(&MP.r[i3 - 1], &MP.r[i3 - 1], z);
         mp_add_integer(z, 1, z);
-        mpsqrt(z, z);
+        mp_sqrt(z, z);
         mp_add_integer(z, 1, z);
         mpdiv(&MP.r[i3 - 1], z, &MP.r[i3 - 1]);
     }
@@ -404,7 +404,7 @@
          *  COMPUTING PI/2 WITH ONE GUARD DIGIT.
          */
         ++MP.t;
-        mppi(&MP.r[i2 - 1]);
+        mp_get_pi(&MP.r[i2 - 1]);
         mpdivi(&MP.r[i2 - 1], 2, &MP.r[i2 - 1]);
         --MP.t;
         mp_subtract(&MP.r[i2 - 1], z, z);

Modified: trunk/gcalctool/mp.c
==============================================================================
--- trunk/gcalctool/mp.c	(original)
+++ trunk/gcalctool/mp.c	Sun Oct 26 13:48:32 2008
@@ -138,13 +138,13 @@
 L10:
     exp_result = y[1] + mpadd3(x, y, sign_prod, med);
     /* NORMALIZE, ROUND OR TRUNCATE, AND RETURN */
-    mpnzr(y_sign, &exp_result, z, trunc);
+    mp_get_normalized_register(y_sign, &exp_result, z, trunc);
     return;
 
 L20:
     exp_result = x[1] + mpadd3(y, x, sign_prod, med);
     /* NORMALIZE, ROUND OR TRUNCATE, AND RETURN */
-    mpnzr(x[0], &exp_result, z, trunc);
+    mp_get_normalized_register(x[0], &exp_result, z, trunc);
     return;
 }
 
@@ -432,7 +432,7 @@
     memset(MP.r + MP.t, 0, 4*sizeof(int));
 
     /* NORMALIZE RESULT AND RETURN */
-    mpnzr(x[0], &offset_exp, y, 1);
+    mp_get_normalized_register(x[0], &offset_exp, y, 1);
 }
 
 /* RETURNS Y = INTEGER PART OF X (TRUNCATED TOWARDS 0), FOR MP X AND Y.
@@ -634,7 +634,7 @@
 
     /* CHECK FOR ZERO DIVIDEND */
     if (rs == 0) {
-        mpnzr(rs, &re, z, 0);
+        mp_get_normalized_register(rs, &re, z, 0);
         return;
     }
 
@@ -706,7 +706,7 @@
             goto L210;
         
         /* NORMALIZE AND ROUND RESULT */
-        mpnzr(rs, &re, z, 0);
+        mp_get_normalized_register(rs, &re, z, 0);
         return;
     }
     
@@ -768,7 +768,7 @@
         
         ++k;
         if (k > i2) {
-            mpnzr(rs, &re, z, 0);
+            mp_get_normalized_register(rs, &re, z, 0);
             return;
         }
         ++i;
@@ -1047,7 +1047,7 @@
 }
 
 
-/*  ROUTINE CALLED BY MPDIV AND MPSQRT TO ENSURE THAT
+/*  ROUTINE CALLED BY MPDIV AND MP_SQRT TO ENSURE THAT
  *  RESULTS ARE REPRESENTED EXACTLY IN T-2 DIGITS IF THEY
  *  CAN BE.  X IS AN MP NUMBER, I AND J ARE INTEGERS.
  */
@@ -1456,7 +1456,7 @@
     }
 
     /* NORMALIZE AND ROUND RESULT */
-    mpnzr(rs, &re, z, 0);
+    mp_get_normalized_register(rs, &re, z, 0);
 }
 
 
@@ -1560,7 +1560,7 @@
         /* NORMALIZE AND ROUND OR TRUNCATE RESULT */
         if (c == 0)
         {
-            mpnzr(rs, &re, z, trunc);
+            mp_get_normalized_register(rs, &re, z, trunc);
             return;
         }
         
@@ -1639,12 +1639,12 @@
 
 
 /*  ASSUMES LONG (I.E. (T+4)-DIGIT) FRACTION IN
- *  R, SIGN = RS, EXPONENT = RE.  NORMALIZES,
- *  AND RETURNS MP RESULT IN Z.  INTEGER ARGUMENTS RE IS
+ *  R, SIGN = REG_SIGN, EXPONENT = REG_EXP.  NORMALIZES,
+ *  AND RETURNS MP RESULT IN Z.  INTEGER ARGUMENTS REG_EXP IS
  *  NOT PRESERVED. R*-ROUNDING IS USED IF TRUNC == 0
  */
 void
-mpnzr(int rs, int *re, int *z, int trunc)
+mp_get_normalized_register(int reg_sign, int *reg_exp, int *z, int trunc)
 {
     int i__1;
 
@@ -1652,15 +1652,16 @@
     int round;
 
     i2 = MP.t + 4;
-    if (rs == 0) {
+    if (reg_sign == 0) {
         /* STORE ZERO IN Z */
         z[0] = 0;
         return;
     }
     
     /* CHECK THAT SIGN = +-1 */
-    if (abs(rs) > 1) {
-        mperr("*** SIGN NOT 0, +1 OR -1 IN CALL TO MPNZR.\nPOSSIBLE OVERWRITING PROBLEM ***\n");
+    if (abs(reg_sign) > 1) {
+        mperr("*** SIGN NOT 0, +1 OR -1 IN CALL TO MP_GET_NORMALIZED_REGISTER.\n"
+	      "POSSIBLE OVERWRITING PROBLEM ***\n");
         z[0] = 0;
         return;
     }
@@ -1680,7 +1681,7 @@
 
     if (is != 0) {
         /* NORMALIZE */
-        *re -= is;
+        *reg_exp -= is;
         i2m = i2 - is;
         for (j = 1; j <= i2m; ++j) {
             k = j + is;
@@ -1740,27 +1741,27 @@
 
             /* EXCEPTIONAL CASE, ROUNDED UP TO .10000... */
             if (j > MP.t) {
-                ++(*re);
+                ++(*reg_exp);
                 MP.r[0] = 1;
             }
         }
     }
 
     /* CHECK FOR OVERFLOW */
-    if (*re > MP.m) {
-        mpovfl(z, "*** OVERFLOW OCCURRED IN MPNZR ***\n");
+    if (*reg_exp > MP.m) {
+        mpovfl(z, "*** OVERFLOW OCCURRED IN MP_GET_NORMALIZED_REGISTER ***\n");
         return;
     }
 
     /* CHECK FOR UNDERFLOW */
-    if (*re < -MP.m) {
+    if (*reg_exp < -MP.m) {
         mpunfl(z);
         return;
     }
     
     /* STORE RESULT IN Z */
-    z[0] = rs;
-    z[1] = *re;
+    z[0] = reg_sign;
+    z[1] = *reg_exp;
     for (i = 1; i <= MP.t; ++i)
         z[i + 1] = MP.r[i - 1];
 }
@@ -1792,18 +1793,18 @@
 }
 
 
-void
-mppi(int *x)
-{
-    int i2;
-    float prec;
-
-/*  SETS MP X = PI TO THE AVAILABLE PRECISION.
+/*  SETS MP Z = PI TO THE AVAILABLE PRECISION.
  *  USES PI/4 = 4.ARCTAN(1/5) - ARCTAN(1/239).
  *  TIME IS O(T**2).
  *  DIMENSION OF R MUST BE AT LEAST 3T+8
  *  CHECK LEGALITY OF B, T, M AND MXR
  */
+void
+mp_get_pi(int *z)
+{
+    int i2;
+    float prec;
+
 
     mpchk(3, 8);
 
@@ -1812,17 +1813,17 @@
     i2 = (MP.t << 1) + 7;
     mp_atan1N(5, &MP.r[i2 - 1]);
     mpmuli(&MP.r[i2 - 1], 4, &MP.r[i2 - 1]);
-    mp_atan1N(239, x);
-    mp_subtract(&MP.r[i2 - 1], x, x);
-    mpmuli(x, 4, x);
+    mp_atan1N(239, z);
+    mp_subtract(&MP.r[i2 - 1], z, z);
+    mpmuli(z, 4, z);
 
 /* RETURN IF ERROR IS LESS THAN 0.01 */
 
-    prec = fabs(mp_cast_to_float(x) - 3.1416);
+    prec = fabs(mp_cast_to_float(z) - 3.1416);
     if (prec < 0.01) return;
 
     /* FOLLOWING MESSAGE MAY INDICATE THAT B**(T-1) IS TOO SMALL */
-    mperr("*** ERROR OCCURRED IN MPPI, RESULT INCORRECT ***\n");
+    mperr("*** ERROR OCCURRED IN MP_GET_PI, RESULT INCORRECT ***\n");
 }
 
 
@@ -1925,7 +1926,7 @@
 
 
 /*  RETURNS Z = 1/X, FOR MP X AND Z.
- *  MPROOT (X, -1, Z) HAS THE SAME EFFECT.
+ *  MP_ROOT (X, -1, Z) HAS THE SAME EFFECT.
  *  DIMENSION OF R MUST BE AT LEAST 4*T+10 IN CALLING PROGRAM
  *  (BUT Z(1) MAY BE R(3T+9)).
  *  NEWTONS METHOD IS USED, SO FINAL ONE OR TWO DIGITS MAY
@@ -2032,33 +2033,33 @@
 }
 
 
-/*  RETURNS Y = X**(1/N) FOR INTEGER N, ABS(N) <= MAX (B, 64).
- *  AND MP NUMBERS X AND Y,
+/*  RETURNS Z = X^(1/N) FOR INTEGER N, ABS(N) <= MAX (B, 64).
+ *  AND MP NUMBERS X AND Z,
  *  USING NEWTONS METHOD WITHOUT DIVISIONS.   SPACE = 4T+10
- *  (BUT Y(1) MAY BE R(3T+9))
+ *  (BUT Z.EXP MAY BE R(3T+9))
  */
 void
-mproot(int *x, int n, int *y)
+mp_root(const int *x, int n, int *z)
 {
     /* Initialized data */
     static const int it[9] = { 0, 8, 6, 5, 4, 4, 4, 4, 4 };
 
     float r__1;
 
-    int i2, i3, ex, np, ts, it0, ts2, ts3, xes;
+    int i2, i3, ex, np, ts, it0, ts2, ts3;
     float rx;
 
     /* CHECK LEGALITY OF B, T, M AND MXR */
     mpchk(4, 10);
 
     if (n == 1) {
-        mp_set_from_mp(x, y);
+        mp_set_from_mp(x, z);
         return;
     }
 
     if (n == 0) {
-        mperr("*** N = 0 IN CALL TO MPROOT ***\n");
-        y[0] = 0;
+        mperr("*** N == 0 IN CALL TO MP_ROOT ***\n");
+        z[0] = 0;
         return;
     }
 
@@ -2069,48 +2070,48 @@
 
     /* LOSS OF ACCURACY IF NP LARGE, SO ONLY ALLOW NP <= MAX (B, 64) */
     if (np > max(MP.b, 64)) {
-        mperr("*** ABS(N) TOO LARGE IN CALL TO MPROOT ***\n");
-        y[0] = 0;
+        mperr("*** ABS(N) TOO LARGE IN CALL TO MP_ROOT ***\n");
+        z[0] = 0;
         return;
     }
 
     /* LOOK AT SIGN OF X */
     if (x[0] == 0) {
-        /* X = 0 HERE, RETURN 0 IF N POSITIVE, ERROR IF NEGATIVE */
-        y[0] = 0;
+        /* X == 0 HERE, RETURN 0 IF N POSITIVE, ERROR IF NEGATIVE */
+        z[0] = 0;
         if (n > 0)
             return;
 
-        mperr("*** X = 0 AND N NEGATIVE IN CALL TO MPROOT ***\n");
-        y[0] = 0;
+        mperr("*** X == 0 AND N NEGATIVE IN CALL TO MP_ROOT ***\n");
+        z[0] = 0;
         return;
     }
     
     if (x[0] < 0  &&  np % 2 == 0) {
-        mperr("*** X NEGATIVE AND N EVEN IN CALL TO MPROOT ***\n");
-        y[0] = 0;
+        mperr("*** X NEGATIVE AND N EVEN IN CALL TO MP_ROOT ***\n");
+        z[0] = 0;
         return;
     }
 
-    /* GET EXPONENT AND DIVIDE BY NP */
-    xes = x[1];
-    ex = xes / np;
+    /* DIVIDE EXPONENT BY NP */
+    ex = x[1] / np;
 
     /* REDUCE EXPONENT SO RX NOT TOO LARGE OR SMALL. */
-    x[1] = 0;
-    rx = mp_cast_to_float(x);
+    {
+      int tmp_x[MP_SIZE];
+      mp_set_from_mp(x, tmp_x);
+      tmp_x[1] = 0;
+      rx = mp_cast_to_float(tmp_x);
+    }
 
     /* USE SINGLE-PRECISION ROOT FOR FIRST APPROXIMATION */
-    r__1 = exp(((float) (np * ex - xes) * log((float) MP.b) - 
+    r__1 = exp(((float) (np * ex - x[1]) * log((float) MP.b) - 
            log((fabs(rx)))) / (float) np);
     mp_set_from_float(r__1, &MP.r[i2 - 1]);
 
     /* SIGN OF APPROXIMATION SAME AS THAT OF X */
     MP.r[i2 - 1] = x[0];
 
-    /* RESTORE EXPONENT */
-    x[1] = xes;
-
     /* CORRECT EXPONENT OF FIRST APPROXIMATION */
     MP.r[i2] -= ex;
 
@@ -2167,7 +2168,7 @@
              *  OR THAT THE INITIAL APPROXIMATION OBTAINED USING ALOG AND EXP
              *  IS NOT ACCURATE ENOUGH.
              */
-            mperr("*** ERROR OCCURRED IN MPROOT, NEWTON ITERATION NOT CONVERGING PROPERLY ***\n");
+            mperr("*** ERROR OCCURRED IN MP_ROOT, NEWTON ITERATION NOT CONVERGING PROPERLY ***\n");
         }
     }
 
@@ -2175,11 +2176,11 @@
     MP.t = ts;
     if (n >= 0) {
         mppwr(&MP.r[i2 - 1], n - 1, &MP.r[i2 - 1]);
-        mpmul(x, &MP.r[i2 - 1], y);
+        mpmul(x, &MP.r[i2 - 1], z);
         return;
     }
 
-    mp_set_from_mp(&MP.r[i2 - 1], y);
+    mp_set_from_mp(&MP.r[i2 - 1], z);
 }
 
 
@@ -2263,30 +2264,30 @@
     mpchk(1, 4);
 }
 
-/*  RETURNS Y = SQRT(X), USING SUBROUTINE MPROOT IF X > 0.
+/*  RETURNS Z = SQRT(X), USING SUBROUTINE MP_ROOT IF X > 0.
  *  DIMENSION OF R IN CALLING PROGRAM MUST BE AT LEAST 4T+10
- *  (BUT Y(1) MAY BE R(3T+9)).  X AND Y ARE MP NUMBERS.
+ *  (BUT Z.EXP MAY BE R(3T+9)).  X AND Z ARE MP NUMBERS.
  *  CHECK LEGALITY OF B, T, M AND MXR
  */
 void
-mpsqrt(int *x, int *y)
+mp_sqrt(const int *x, int *z)
 {
     int i, i2, iy3;
 
     mpchk(4, 10);
 
-    /* MPROOT NEEDS 4T+10 WORDS, BUT CAN OVERLAP SLIGHTLY. */
+    /* MP_ROOT NEEDS 4T+10 WORDS, BUT CAN OVERLAP SLIGHTLY. */
     i2 = MP.t * 3 + 9;
     if (x[0] < 0) {
-        mperr("*** X NEGATIVE IN CALL TO SUBROUTINE MPSQRT ***\n");
+        mperr("*** X NEGATIVE IN CALL TO SUBROUTINE MP_SQRT ***\n");
     } else if (x[0] == 0) {
-        y[0] = 0;
+        z[0] = 0;
     } else {
-        mproot(x, -2, &MP.r[i2 - 1]);
+        mp_root(x, -2, &MP.r[i2 - 1]);
         i = MP.r[i2 + 1];
-        mpmul(x, &MP.r[i2 - 1], y);
-        iy3 = y[2];
-        mpext(i, iy3, y);
+        mpmul(x, &MP.r[i2 - 1], z);
+        iy3 = z[2];
+        mpext(i, iy3, z);
     }
 }
 

Modified: trunk/gcalctool/mp.h
==============================================================================
--- trunk/gcalctool/mp.h	(original)
+++ trunk/gcalctool/mp.h	Sun Oct 26 13:48:32 2008
@@ -73,12 +73,12 @@
 void mpln(int *, int *);
 void mpmul(const int *, const int *, int *);
 void mpmuli(int *, int, int *);
-void mppi(int *);
+void mp_get_pi(int *z);
 void mppwr(const int *, int, int *);
 void mppwr2(int *, int *, int *);
 void mpset(int, int, int);
-void mproot(int *, int, int *);
-void mpsqrt(int *, int *);
+void mp_root(const int *x, int n, int *z);
+void mp_sqrt(const int *x, int *z);
 
 /* mp-convert.c */
 void   mp_set_from_mp(const int *, int *);

Modified: trunk/gcalctool/mpmath.c
==============================================================================
--- trunk/gcalctool/mpmath.c	(original)
+++ trunk/gcalctool/mpmath.c	Sun Oct 26 13:48:32 2008
@@ -229,12 +229,12 @@
     int MP1[MP_SIZE], MP2[MP_SIZE];
 
     if (v->ttype == DEG) {
-        mppi(MP1);
+        mp_get_pi(MP1);
         mpmul(s1, MP1, MP2);
         mp_set_from_integer(180, MP1);
         mpdiv(MP2, MP1, t1);
     } else if (v->ttype == GRAD) {
-        mppi(MP1);
+        mp_get_pi(MP1);
         mpmul(s1, MP1, MP2);
         mp_set_from_integer(200, MP1);
         mpdiv(MP2, MP1, t1);
@@ -254,7 +254,7 @@
         case DEG:
             mp_set_from_integer(180, MP1);
             mpmul(s1, MP1, MP2);
-            mppi(MP1);
+            mp_get_pi(MP1);
             mpdiv(MP2, MP1, t1);
             break;
 
@@ -265,7 +265,7 @@
         case GRAD:
             mp_set_from_integer(200, MP1);
             mpmul(s1, MP1, MP2);
-            mppi(MP1);
+            mp_get_pi(MP1);
             mpdiv(MP2, MP1, t1);
             break;
 



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