home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / LIBSRC.ZOO / libsrc / stdlib / atof.c < prev    next >
C/C++ Source or Header  |  1992-02-03  |  4KB  |  151 lines

  1. /*-
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * the Systems Programming Group of the University of Utah Computer
  7.  * Science Department.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *    This product includes software developed by the University of
  20.  *    California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  */
  37.  
  38. #if defined(LIBC_SCCS) && !defined(lint)
  39. static char sccsid[] = "@(#)atof.c    5.2 (Berkeley) 4/12/91";
  40. #endif /* LIBC_SCCS and not lint */
  41.  
  42. /*
  43.  * simple atof() for IEEE 754 architectures
  44.  */
  45.  
  46. #include <machine/endian.h>
  47. #include <stdlib.h>
  48. #include <math.h>
  49. #include <ctype.h>
  50.  
  51. static double twoemax = 9007199254740992.;    /*2^53*/
  52.  
  53. /* attempt to be as exact as possible */
  54. static struct {
  55.     long low_word;
  56.     long high_word;
  57. } exp5[] = {
  58. #if    BYTE_ORDER == BIG_ENDIAN
  59.     { 0x40140000, 0x00000000 },    /* 5 */
  60.     { 0x40390000, 0x00000000 },    /* 25 */
  61.     { 0x40838800, 0x00000000 },    /* 625 */
  62.     { 0x4117d784, 0x00000000 },    /* 390625 */
  63.     { 0x4241c379, 0x37e08000 },    /* 152587890625 */
  64.     { 0x4493b8b5, 0xb5056e17 },    /* 2.3283064365387e+022 */
  65.     { 0x49384f03, 0xe93ff9f6 },    /* 5.42101086242753e+044 */
  66.     { 0x52827748, 0xf9301d33 },    /* 2.93873587705572e+089 */
  67.     { 0x65154fdd, 0x7f73bf3f }    /* 8.63616855509445e+178 */
  68. #else    /* BYTE_ORDER == LITTLE_ENDIAN */
  69.     { 0x00000000, 0x40140000 },    /* 5 */
  70.     { 0x00000000, 0x40390000 },    /* 25 */
  71.     { 0x00000000, 0x40838800 },    /* 625 */
  72.     { 0x00000000, 0x4117d784 },    /* 390625 */
  73.     { 0x37e08000, 0x4241c379 },    /* 152587890625 */
  74.     { 0xb5056e17, 0x4493b8b5 },    /* 2.3283064365387e+022 */
  75.     { 0xe93ff9f6, 0x49384f03 },    /* 5.42101086242753e+044 */
  76.     { 0xf9301d33, 0x52827748 },    /* 2.93873587705572e+089 */
  77.     { 0x7f73bf3f, 0x65154fdd }    /* 8.63616855509445e+178 */
  78. #endif
  79. };
  80.  
  81. double
  82. atof(p)
  83.     register const char *p;
  84. {
  85.     register int c;
  86.     register int exp = 0;
  87.     register int eexp = 0;
  88.     double fl = 0;
  89.     double flexp = 1.0;
  90.     int bexp;
  91.     int neg = 1;
  92.     int negexp = 1;
  93.  
  94.     while (isspace(*p))
  95.         ++p;
  96.  
  97.     if ((c = *p++) == '-')
  98.         neg = -1;
  99.     else if (c == '+')
  100.         /* skip it */;
  101.     else
  102.         --p;
  103.  
  104.     while ((c = *p++) && isdigit(c))
  105.         if (fl < twoemax)
  106.             fl = 10 * fl + (c-'0');
  107.         else
  108.             ++exp;
  109.  
  110.     if (c == '.')
  111.         while ((c = *p++) && isdigit(c))
  112.             if (fl < twoemax) {
  113.                 fl = 10 * fl + (c-'0');
  114.                 --exp;
  115.             }
  116.  
  117.     if (c == 'E' || c == 'e') {
  118.         if ((c = *p++) == '-')
  119.             negexp = -1;
  120.         else if (c == '+')
  121.             /* skip it */;
  122.         else
  123.             --p;
  124.         while ((c = *p++) && isdigit(c))
  125.             eexp = 10 * eexp + (c-'0');
  126.         if (negexp < 0)
  127.             eexp = -eexp;
  128.         exp += eexp;
  129.     }
  130.  
  131.     bexp = exp;
  132.     if (exp < 0)
  133.         exp = -exp;
  134.  
  135.     for (c = 0; exp && c < sizeof exp5 / sizeof exp5[0]; ++c) {
  136.         if (exp & 1)
  137.             flexp *= *(double *)&exp5[c];
  138.         exp >>= 1;
  139.     }
  140.  
  141.     if (bexp < 0)
  142.         fl /= flexp;
  143.     else
  144.         fl *= flexp;
  145.  
  146.     fl = ldexp(fl, bexp);
  147.  
  148.     return neg < 0 ? -fl : fl;
  149. }
  150.  
  151.