home *** CD-ROM | disk | FTP | other *** search
/ Vectronix 2 / VECTRONIX2.iso / FILES_01 / MARK_WC1.LZH / SRC / ATOD.C next >
Text File  |  1988-04-27  |  827b  |  36 lines

  1. /*
  2.  * Atod converts the string `num' to a double and returns the value.
  3.  * Note, that if there is a non-digit in the string, or if there is
  4.  * an overflow, then it exits with an appropriate error message.
  5.  * Also note that atod accepts leading zero for octal and leading
  6.  * 0x for hexidecimal, and that in the latter case, a-f and A-F are
  7.  * both accepted as digits.
  8.  */
  9. double
  10. atod(num)
  11. char    *num;
  12. {
  13.     register char    *str;
  14.     register int    i;
  15.     double        res    = 0,
  16.             base    = 10;
  17.  
  18.     str = num;
  19.     i = *str++;
  20.     if (i == '0')
  21.         if ((i=*str++) == 'x') {
  22.             i = *str++;
  23.             base = 0x10;
  24.         } else
  25.             base = 010;
  26.     for (; i != '\0'; i=*str++) {
  27.         i = todigit(i);
  28.         if (i >= base)
  29.             die("bad number `%s'", num);
  30.         res = res * base + i;
  31.         if (res+1 == res)
  32.             die("Number too big `%s'", num);
  33.     }
  34.     return (res);
  35. }
  36.