home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / h / hp11 / Amiga_Code / c / support < prev    next >
Encoding:
Text File  |  1992-05-07  |  2.2 KB  |  145 lines

  1. /*-> c.support */
  2. #include "math.h"
  3.  
  4. #include "types.h"
  5. #include "support.h"
  6.  
  7. double sign(r)
  8. double r;
  9. {
  10.    if (r < 0.0) return(-1.0);
  11.    else if (r == 0.0) return (0.0);
  12.    else return(1.0);
  13. }
  14.  
  15. void Rect(r, phi, x, y)
  16. double r, phi, *x, *y;
  17. {
  18.    *x = r * cos(phi);
  19.    *y = r * sin(phi);
  20. }
  21.  
  22. void Polar(x, y, r, phi)
  23. double x, y, *r, *phi;
  24. {
  25.    *r = sqrt(x * x + y * y);
  26.    *phi = atan2(y, x);
  27. }
  28.  
  29. double stirling(n)
  30. double n;
  31. {
  32.    double y = 1 / (12 * n);
  33.  
  34.    return (pow(n / E, n) * sqrt(2 * PI * n) * (1 + y * (1 + y * (0.5 - y *
  35.  (4.6333333333333333 + y * 4.7583333333333333)))));
  36. }
  37.  
  38. double gamma(x)
  39. double x;
  40. {
  41.    double fx, tx, res, i;
  42.  
  43.    if (x >= 15.0) return(stirling(x - 1));
  44.    else {
  45.       if ((fx = modf(x, &tx)) < 0) { tx -= 1.0; fx += 1.0; } /* give real int &
  46.  frac */
  47.  
  48.       if (fx == 0 && tx < 0) return(-HUGE_VAL);
  49.       if (tx < -200) return(0.0); /* Underflow */
  50.  
  51.       res = stirling(fx + 14.0);
  52.       for (i = 14.0; i >= tx; i -= 1.0) res /= i + fx;
  53.  
  54.       return(res);
  55.    }
  56. }
  57.  
  58. double factorial(x)
  59. int x;
  60. {
  61.    double r = 1.0;
  62.  
  63.    if (x > 250) r = HUGE_VAL; /* Certainly too big */
  64.    else for (; x > 0; x--) r *= x;
  65.  
  66.    return(r);
  67. }
  68.  
  69. double Perm(x, y)
  70. int x, y;
  71. {
  72.    double i, res = 1.0, lim = x - y;
  73.  
  74.    for (i = x; i > lim; i -= 1.0) res *= i;
  75.  
  76.    return(res);
  77. }
  78.  
  79. double Comb(x, y)
  80. int x,y;
  81. {
  82.    double i, lim = y, res = Perm(x, y);
  83.  
  84.    for (i = 1; i <= lim; i += 1.0) res /= i;
  85.  
  86.    return(res);
  87. }
  88.  
  89. double hr(x)
  90. double x;
  91. {
  92.    double h, m, s;
  93.  
  94.    /* f = modf(x, &i) returns the frcational part of x in f and the integral
  95.  part in i (all double) */
  96.    m = 100.0 * modf(x, &h);
  97.    s = 100.0 * modf(m, &m);
  98.  
  99.    return(h + m / 60.0 + s / 3600.0);
  100. }
  101.  
  102. double hms(x)
  103. double x;
  104. {
  105.    double h, m, s;
  106.  
  107.    m = 60.0 * modf(x, &h);
  108.    s = 60.0 * modf(m, &m);
  109.  
  110.    return(h + m / 100.0 + s / 10000.0);
  111. }
  112.  
  113. double trunc(x)
  114. double x;
  115. {
  116.    modf(x, &x);
  117.    return(x);
  118. }
  119.  
  120. double frac(x)
  121. double x;
  122. {
  123.    return(modf(x, &x));
  124. }
  125.  
  126. double asinh(x)
  127. double x;
  128. {
  129.    return(log(x + sqrt(x * x + 1)));
  130. }
  131.  
  132. double acosh(x)
  133. double x;
  134. {
  135.    if (x < 1.0) return(0.0);
  136.    else return(log(x + sqrt(x * x -1)));
  137. }
  138.  
  139. double atanh(x)
  140. double x;
  141. {
  142.    if (x > 1.0) return(0.0);
  143.    else return(log((1.0 + x) / (1.0 - x)) / 2.0);
  144. }
  145.