home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff218.lzh / EdLib / strtod.c < prev    next >
C/C++ Source or Header  |  1989-06-04  |  5KB  |  211 lines

  1. /*
  2.  * edlib v1.1 Copyright 1989 Edwin Hoogerbeets
  3.  * This code is freely redistributable as long as no charge other than
  4.  * reasonable copying fees are levied for it.
  5.  */
  6.  
  7.  
  8. /*  File   : strtod.c
  9.     Author : Richard A. O'Keefe @ Quintus Computer Systems, Inc.
  10.     Updated: Saturday March 11, 1989
  11.     Defines: double strtod(char *str, char**ptr)
  12. */
  13.  
  14. /*  This is an implementation of the strtod() function described in the
  15.     System V manuals, with a different name to avoid linker problems.
  16.     All that str2dbl() does itself is check that the argument is well-formed
  17.     and is in range.  It leaves the work of conversion to atof(), which is
  18.     assumed to exist and deliver correct results (if they can be represented).
  19.  
  20.     There are two reasons why this should be provided to the net:
  21.     (a) some UNIX systems do not yet have strtod(), or do not have it
  22.         available in the BSD "universe" (but they do have atof()).
  23.     (b) some of the UNIX systems that *do* have it get it wrong.
  24.         (some crash with large arguments, some assign the wrong *ptr value).
  25.     There is a reason why *we* are providing it: we need a correct version
  26.     of strtod(), and if we give this one away maybe someone will look for
  27.     mistakes in it and fix them for us (:-).
  28. */
  29.  
  30. /*  The following constants are machine-specific.  MD{MIN,MAX}EXPT are
  31.     integers and MD{MIN,MAX}FRAC are strings such that
  32.         0.${MDMAXFRAC}e${MDMAXEXPT} is the largest representable double,
  33.         0.${MDMINFRAC}e${MDMINEXPT} is the smallest representable +ve double
  34.     MD{MIN,MAX}FRAC must not have any trailing zeros.
  35.     The values here are for IEEE-754 64-bit floats.
  36.     It is not perfectly clear to me whether an IEEE infinity should be
  37.     returned for overflow, nor what a portable way of writing one is,
  38.     so HUGE is just 0.MAXFRAC*10**MAXEXPT (this seems still to be the
  39.     UNIX convention).
  40.  
  41.     I do know about <values.h>, but the whole point of this file is that
  42.     we can't always trust that stuff to be there or to be correct.
  43. */
  44.  
  45. extern  double  atof();         /* Only called when result known to be ok */
  46.  
  47. #include "edlib.h"
  48. #include <ctype.h>
  49. #include <errno.h>
  50. extern  int     errno;
  51.  
  52. double strtod(str, ptr)
  53. char *str;
  54. char **ptr;
  55. {
  56.   int sign, scale, dotseen;
  57.   int esign, expt;
  58.   char *save;
  59.   register char *sp, *dp;
  60.   register int c;
  61.   char *buforg, *buflim;
  62.   char buffer[64];                /* 45-digit significand + */
  63.                                   /* 13-digit exponent */
  64.   sp = str;
  65.   while (*sp == ' ')
  66.     sp++;
  67.  
  68.   sign = 1;
  69.  
  70.   if (*sp == '-') {
  71.     sign -= 2;
  72.     sp++;
  73.   }
  74.  
  75.   dotseen = 0;
  76.   scale = 0;
  77.   dp = buffer;
  78.  
  79.   *dp++ = '0';
  80.   *dp++ = '.';
  81.  
  82.   buforg = dp;
  83.   buflim = buffer+48;
  84.  
  85.   for (save = sp; c = *sp; sp++) {
  86.  
  87.     if (c == '.') {
  88.  
  89.       if (dotseen)
  90.         break;
  91.  
  92.       dotseen++;
  93.  
  94.     } else if ( !isdigit(c) ) {
  95.       break;
  96.  
  97.     } else if (c == '0') {
  98.  
  99.       if (dp != buforg) {
  100.  
  101.         /* This is not the first digit, so we want to keep it */
  102.         if (dp < buflim) {
  103.           *dp++ = c;
  104.           scale += 1;
  105.         }
  106.  
  107.       } else {
  108.  
  109.         /* No non-zero digits seen yet */
  110.         /* If a . has been seen, scale must be adjusted */
  111.         if (dotseen)
  112.           scale--;
  113.       }
  114.     } else {
  115.  
  116.       /* This is a nonzero digit, so we want to keep it */
  117.       if (dp < buflim)
  118.         *dp++ = c;
  119.  
  120.       /* If it precedes a ., scale must be adjusted */
  121.       if (!dotseen)
  122.         scale++;
  123.     }
  124.   }
  125.   if (sp == save) {
  126.     if (ptr)
  127.       *ptr = str;
  128.     errno = EDOM;               /* what should this be? */
  129.     return ZERO;
  130.   }
  131.  
  132.   while (dp > buforg && dp[-1] == '0')
  133.     --dp;
  134.  
  135.   if (dp == buforg)
  136.     *dp++ = '0';
  137.  
  138.   *dp = '\0';
  139.  
  140.   /*  Now the contents of buffer are
  141.       +--+--------+-+--------+
  142.       |0.|fraction|\|leftover|
  143.       +--+--------+-+--------+
  144.                    ^dp points here
  145.       where fraction begins with 0 iff it is "0", and has at most
  146.       45 digits in it, and leftover is at least 16 characters.
  147.   */
  148.  
  149.   save = sp;
  150.   expt = 0;
  151.   esign = 1;
  152.  
  153.   do {
  154.     c = *sp++;
  155.  
  156.     if (c != 'e' && c != 'E')
  157.       break;
  158.  
  159.     c = *sp++;
  160.  
  161.     if ( c == '-' ) {
  162.       esign -= 2;
  163.       c = *sp++;
  164.     } else if (c == '+' || c == ' ') {
  165.       c = *sp++;
  166.     }
  167.  
  168.     if ( !isdigit(c) )
  169.       break;
  170.  
  171.     while (c == '0')
  172.       c = *sp++;
  173.  
  174.     for (; isdigit(c); c = *sp++)
  175.       expt = expt*10 + toint(c);
  176.  
  177.     if (esign < 0)
  178.       expt = -expt;
  179.  
  180.     save = sp-1;
  181.  
  182.   } while (0);
  183.  
  184.   if (ptr)
  185.     *ptr = save;
  186.  
  187.   expt += scale;
  188.  
  189.   /*  Now the number is sign*0.fraction*10**expt  */
  190.   errno = ERANGE;
  191.  
  192.   if (expt > MDMAXEXPT) {
  193.     return(HUGE*sign);
  194.   } else if (expt == MDMAXEXPT) {
  195.     if (strcmp(buforg, MDMAXFRAC) > 0)
  196.       return(HUGE*sign);
  197.   } else if (expt < MDMINEXPT) {
  198.     return(ZERO*sign);
  199.   } else if (expt == MDMINEXPT) {
  200.     if (strcmp(buforg, MDMINFRAC) < 0)
  201.       return(ZERO*sign);
  202.   }
  203.  
  204.   /*  We have now established that the number can be  */
  205.   /*  represented without overflow or underflow  */
  206.   (void) sprintf(dp, "e%d", expt);
  207.   errno = 0;
  208.   return(atof(buffer)*sign);
  209. }
  210.  
  211.