home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / misc / icalc.lzh / icalc / src / function.c < prev    next >
C/C++ Source or Header  |  1992-01-28  |  3KB  |  144 lines

  1. /*
  2. *    icalc - complex-expression parser
  3. *
  4. *    Built-in functions not included in cmath.c, since they're not
  5. *    so mathematical.
  6. *
  7. *    (C) Martin W Scott, 1991.
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <math.h>
  12. #include <time.h>
  13. #include "complex.h"
  14. #include "constant.h"
  15.  
  16. extern Symbol *ans;
  17.  
  18. static int prec = 8;        /* number of decimal places to print */
  19.                 /* (when they exist) */
  20.  
  21. void cprin(fp, prefix, suffix, z)    /* print a complex number to file fp */
  22.     FILE *fp;
  23.     char *prefix, *suffix;
  24.     Complex z;
  25. {
  26.     fprintf(fp, prefix);
  27.  
  28.     if (z.imag == 0.0)
  29.         fprintf(fp, "%10.*lg", prec, z.real);
  30.     else if (z.real == 0.0)
  31.         fprintf(fp, "%10.*lg i", prec, z.imag);
  32.     else
  33.         fprintf(fp, "%.*g %c %.*g i",
  34.             prec, z.real, sign(z.imag), prec, abs(z.imag));
  35.  
  36.     fprintf(fp, suffix);
  37. }
  38.  
  39. Complex printres(z)    /* prints and returns its argument */
  40.     Complex z;
  41. {
  42.     cprin(stdout, "\t", "\n", z);
  43.     return z;
  44. }
  45.  
  46. Complex precision(z)    /* adjust decimal places shown */
  47.     Complex z;
  48. {
  49.     prec = (int)z.real;
  50.     return ans->u.val;
  51. }
  52.  
  53.  
  54. static double integer(x)    /* round x to NEAREST integer */
  55.     double x;        /* if fractional part is 0.5, round UP */
  56. {
  57.     double f,i;
  58.  
  59.     f = modf(x,&i);
  60.     if (abs(f) >= 0.5)
  61.         if (x >= 0.0)
  62.             return i + 1.0;
  63.         else
  64.             return i - 1.0;
  65.     else return i;
  66. }
  67.  
  68. Complex cinteger(z)    /* return REAL part to nearest integer */
  69.     Complex z;
  70. {
  71.     z.real = integer(z.real);
  72.     z.imag = 0.0;
  73.  
  74.     return z;
  75. }
  76.     
  77. Complex cceil(z)    /* ceiling of REAL part of z */
  78.     Complex z;
  79. {
  80.     z.real = ceil(z.real);
  81.     z.imag = 0.0;
  82.     return z;
  83. }
  84.     
  85. Complex cfloor(z)    /* ceiling of REAL part of z */
  86.     Complex z;
  87. {
  88.     z.real = floor(z.real);
  89.     z.imag = 0.0;
  90.     return z;
  91. }
  92.  
  93. Complex csign(z)    /* sign of REAL part of z */
  94.     Complex z;
  95. {
  96.     if (z.real > 0.0) z.real = 1.0;
  97.     else if (z.real < 0.0) z.real = -1.0;
  98.     z.imag = 0.0;
  99.     return z;
  100. }
  101.  
  102. /*
  103. *    gettime(lasttime) 
  104. *
  105. *    returns the system time in seconds, less the real part of its
  106. *    argument, lasttime.
  107. *
  108. *    eg.     gettime({0,0}) returns system time.
  109. *        gettime({3600,0}) returns system time less one hour.
  110. *
  111. *    Thus, it can be used as a simple timer within icalc scripts.
  112. */
  113.  
  114.  
  115. #ifdef LATTICE & AMIGA        /* use higher-resolution time stuff */
  116.  
  117. Complex gettime(lasttime)
  118.     Complex lasttime;
  119. {
  120.     double realtime;
  121.     unsigned int clock[2];
  122.  
  123.     timer(clock);
  124.     realtime = clock[0] + clock[1] / 1e6L;
  125.  
  126.     lasttime.real = realtime - lasttime.real;
  127.     lasttime.imag = 0.0L;
  128.  
  129.     return lasttime;
  130. }
  131.  
  132. #else    /* not on AMIGA - could possibly use other system-specific functions */
  133.  
  134. Complex gettime(lasttime)
  135.     Complex lasttime;
  136. {
  137.     lasttime.real = (double)time(NULL) - lasttime.real;
  138.     lasttime.imag = 0.0L;
  139.  
  140.     return lasttime;
  141. }
  142.  
  143. #endif
  144.