home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / CMTEX330 / SOURCE / ARITH.C < prev    next >
C/C++ Source or Header  |  1992-02-19  |  2KB  |  193 lines

  1.  
  2. /*
  3.  * %Y%:%M%:%I%:%Q%
  4.  *
  5.  * Copyright 1987,1988,1991 Pat J Monardo
  6.  *
  7.  * Redistribution of this file is permitted through
  8.  * the specifications in the file COPYING.
  9.  *
  10.  *
  11.  */
  12.  
  13. #ifndef lint
  14. static char *sccsid = "%A%";
  15. #endif
  16.  
  17. #include "tex.h"
  18.  
  19. bool    arith_error;
  20. scal    remainder;
  21.  
  22. int 
  23. half (x)
  24.     int    x;
  25. {
  26.     return (odd(x) ? (x + 1) / 2 : x / 2);
  27. }
  28.  
  29.  
  30. scal
  31. round_decimals (k)
  32.     int    k;
  33. {
  34.     int    a;
  35.  
  36.     a = 0;
  37.     while (k > 0) {
  38.         decr(k);
  39.         a = (a + dig[k] * TWO) / 10;
  40.     }
  41.     return ((a + 1) / 2);
  42. }
  43.  
  44. void
  45. print_scaled (s)
  46.     scal    s;
  47. {
  48.     scal    delta;
  49.  
  50.     if (s < 0) {
  51.         print("-");
  52.         s = -s;
  53.     }
  54.     print_int(s / UNITY);
  55.     print(".");
  56.     s = 10 * (s % UNITY) + 5;
  57.     delta = 10;
  58.     do {
  59.         if (delta > UNITY) {
  60.             s += 0100000 - 50000;
  61.         }
  62.         print_char('0' + s / UNITY);
  63.         s = 10 * (s % UNITY);
  64.         delta *= 10;
  65.     } while (s > delta);
  66. }
  67.  
  68. scal
  69. mult_and_add (n, x, y, max_answer)
  70.     int    n;
  71.     scal    x;
  72.     scal    y;
  73.     scal    max_answer;
  74. {
  75.     
  76.     if (n < 0) {
  77.         x = -x;
  78.         n = -n;
  79.     }
  80.     if (n == 0) {
  81.         return y;
  82.     }
  83.     if (x <= (max_answer - y) / n
  84.     && -x <= (max_answer + y) / n) {
  85.         return (n * x + y);
  86.     }
  87.     arith_error = TRUE;
  88.     return 0;
  89. }
  90.  
  91. scal
  92. x_over_n (x, n)
  93.     scal    x;
  94.     int    n;
  95. {
  96.     bool    negative;
  97.     scal    quotient;
  98.  
  99.     negative = FALSE;
  100.     if (n == 0) {
  101.         arith_error = TRUE;
  102.         remainder = x;
  103.         return 0;
  104.     }
  105.     if (n < 0) {
  106.         x = -x;
  107.         n = -n;
  108.         negative = TRUE;
  109.     }
  110.     if (x >= 0) {
  111.         quotient = x / n;
  112.         remainder = x % n;
  113.     } else {
  114.         quotient = -(-x / n);
  115.         remainder = -(-x % n);
  116.     }
  117.     if (negative) {
  118.         negate(remainder);
  119.     }
  120.     return quotient;
  121. }
  122.  
  123. scal
  124. xn_over_d (x, n, d)
  125.     scal    x;
  126.     int    n;
  127.     int    d;
  128. {
  129.     int    t;
  130.     int    u;
  131.     int    v;
  132.     bool    positive;
  133.  
  134.     if (x >= 0) {
  135.         positive = TRUE;
  136.     } else {
  137.         x = -x;
  138.         positive = FALSE;
  139.     }
  140.     t = x % 0100000 * n;
  141.     u = x / 0100000 * n + t / 0100000;
  142.     v = u % d * 0100000 + t % 0100000;
  143.     if (u / d >= 0100000) {
  144.         arith_error = TRUE;
  145.     } else {
  146.         u = 0100000 * (u / d) + v / d;
  147.     }
  148.     if (positive) {
  149.         remainder = v % d;
  150.         return u;
  151.     } else {
  152.         remainder = - (v % d);
  153.         return -u;
  154.     }
  155. }
  156.  
  157. int
  158. badness (t, s)
  159.     scal    t;
  160.     scal    s;
  161. {
  162.     int    r;
  163.  
  164.     if (t == 0) {
  165.         return 0;
  166.     } else if (s <= 0) {
  167.         return INF_BAD;
  168.     } else {
  169.         if (t <= 7230584) {
  170.             r = t * 297 / s;
  171.         } else if (s >= 1663497) {
  172.             r = t / (s / 297);
  173.         } else {
  174.             r = t;
  175.         }
  176.         if (r > 1290) {
  177.             return INF_BAD;
  178.         } else {
  179.             return ((r * r * r + 0400000) / 01000000);
  180.         }
  181.     }
  182. }
  183.  
  184. void
  185. _arith_init ()
  186. {
  187. }
  188.  
  189. void
  190. _arith_init_once ()
  191. {
  192. }
  193.