home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnuc / string / rcs / strtod.c,v < prev    next >
Encoding:
Text File  |  1992-08-02  |  5.1 KB  |  181 lines

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