home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Tex / Tex29 / StTeXsrc.zoo / src / arith.c < prev    next >
C/C++ Source or Header  |  1988-03-13  |  3KB  |  168 lines

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