home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 329.lha / MultiPlot / source / util.c < prev   
C/C++ Source or Header  |  1990-01-05  |  3KB  |  133 lines

  1. #include <ctype.h>
  2. #include <exec/types.h>
  3. #include "mp.h"
  4. extern int debug;
  5. /*************************************************************
  6. * Round a positive integer to the "simplest" number in [val..limit]
  7. */
  8.  
  9. #define lsdig(x)  (((x) >= 10) ? (x)-((x)/10)*10 : (x))
  10.  
  11. iround(val,dir,limit)
  12. int val, dir;
  13. float limit;
  14.  
  15. {
  16.    int tmpval;
  17.  
  18.    switch (dir) {
  19.       case UP:
  20.          tmpval = lsdig(val);
  21.          tmpval = ( (tmpval) ? (val+10-tmpval) : val );
  22.          if (tmpval > limit) return(val);
  23.          if (val <= 10) return(tmpval);
  24.          val = iround(tmpval/10,dir,limit/10);
  25.          return(val*10);
  26.       case DOWN:
  27.          tmpval = val-lsdig(val);
  28.          if (tmpval < limit) return(val);
  29.          if (val <= 10) return(tmpval);
  30.          val = iround(tmpval/10,dir,limit/10);
  31.          return(val*10);
  32.    }   
  33.    return(val);
  34. }
  35.  
  36.  
  37. /*************************************************************
  38. * Round an FFP to the "simplest" number in [val..limit]
  39. */
  40.  
  41. #define FLARGE 1.0e18
  42.  
  43. FFP fround(ffpval, dir, ffplimit)
  44. FFP ffpval, ffplimit;
  45. int dir;
  46. {
  47.    FFP val, limit, fact=1.;
  48.    
  49.    val = ffpval;   
  50.    limit = ffplimit;   
  51.  
  52.    if (debug) printf("fround: val=%f, dir=%d, limit=%f\n",ffpval,dir,ffplimit);
  53.    if (abs(val) < 1.e-4) return((FFP)0.);
  54.    if (val < 0.) {
  55.       val *= -1.; limit *= -1.; fact = -1.;
  56.       dir = (dir==UP ? DOWN : UP);
  57.    }
  58.    
  59.    while (val < 1.e5) {val*=10.; limit*=10.; fact*=10.;}
  60.    while (val > 1.e6) {val*=0.1; limit*=0.1; fact*=0.1;}
  61.    if ((int)val) {
  62.       val = (FFP) iround( (int)val, dir, limit);
  63.       if (debug) printf("fround: returning %f\n",val/fact);
  64.       val /= fact;
  65.       return(val);
  66.    }
  67.    else {
  68.       if (debug) printf("fround: returning 0.0\n");
  69.       return(0.0);
  70.    }
  71. }
  72.  
  73.  
  74. /***************************************************************************/
  75. char *getwrd(bp)
  76. char **bp;
  77. {
  78.    char *retval;
  79.    
  80.    while (**bp == ' ') (*bp)++;
  81.    retval = *bp;
  82.    while (**bp && (**bp != ' ') ) (*bp)++;
  83.    return(retval);
  84. }
  85.  
  86.  
  87. /***************************************************************************/
  88. numeric(cp)
  89. char *cp;
  90. {
  91.    int rv = FALSE;
  92.    char c;
  93.    
  94.    while(*(cp) == ' ') cp++;
  95.  
  96.    while (c = *(cp++)) {
  97.       if (isdigit(c)||(toupper(c)=='E')||(c=='-')||(c=='+')||(c=='.'))
  98.          rv = TRUE;
  99.       else if ( c==' ')
  100.          return(rv);
  101.       else
  102.          return(FALSE);
  103.    }
  104.    return(rv);
  105. }
  106.  
  107.  
  108. /***************************************************************************/
  109. same(a,b,len)
  110. char *a, *b;
  111. int len;
  112. {
  113.    char i=0;
  114.    
  115.    while(*a && *b) {
  116.       if (toupper(*a) != toupper(*b)) return(FALSE);
  117.       a++; b++; i++;
  118.       if (i==len) return(TRUE);
  119.    }
  120.    return(i==len);
  121. }
  122.  
  123. /***************************************************************************/
  124. void trch(oldch,newch,buf)
  125. char oldch, newch, *buf;
  126. {
  127.    while (*buf) {
  128.       if (*buf == oldch) *buf = newch;
  129.       buf++;
  130.    }
  131. }
  132.  
  133.