C函数源码解读:atof

作用:将一个asii字符串转化为double类型的数据 
头文件:stdlib.h 

C代码   收藏代码
  1. /* Convert a string to a double.  */  
  2. double  
  3. atof (const char *nptr)  
  4. {  
  5.   return strtod (nptr, (char **) NULL);  
  6. }  


C代码   收藏代码
  1. #if HAVE_CONFIG_H  
  2. # include <config.h>  
  3. #endif  
  4.   
  5. #include <errno.h>  
  6. #ifndef errno  
  7. extern int errno;  
  8. #endif  
  9.   
  10. #include <ctype.h>  
  11.   
  12. #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))  
  13. # define IN_CTYPE_DOMAIN(c) 1  
  14. #else  
  15. # define IN_CTYPE_DOMAIN(c) isascii(c)  
  16. #endif  
  17.   
  18. #define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))  
  19. #define ISDIGIT(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))  
  20. #define TOLOWER(c) (IN_CTYPE_DOMAIN (c) ? tolower(c) : (c))  
  21.   
  22. #include <math.h>  
  23.   
  24. #include <float.h>  
  25. #include <stdlib.h>  
  26. #include <string.h>  
  27.   
  28. /* Convert NPTR to a double.  If ENDPTR is not NULL, a pointer to the 
  29.    character after the last one used in the number is put in *ENDPTR.  */  
  30. double  
  31. strtod (const char *nptr, char **endptr)  
  32. {  
  33.   register const char *s;  
  34.   short int sign;  
  35.   
  36.   /* The number so far.  */  
  37.   double num;  
  38.   
  39.   int got_dot;                  /* Found a decimal point.  */  
  40.   int got_digit;                /* Seen any digits.  */  
  41.   
  42.   /* The exponent of the number.  */  
  43.   long int exponent;  
  44.   
  45.   if (nptr == NULL) /*如果为空串,则结束转换*/  
  46.     {  
  47.       errno = EINVAL;  
  48.       goto noconv; /*转向处理无法转换的代码*/  
  49.     }  
  50.   
  51.   s = nptr;  
  52.   
  53.   /* Eat whitespace.  */  
  54.   while (ISSPACE (*s))  
  55.     ++s;  
  56.   
  57.   /* Get the sign.  */  
  58.   sign = *s == '-' ? -1 : 1;  
  59.   if (*s == '-' || *s == '+')  
  60.     ++s;  
  61.   
  62.   num = 0.0;  
  63.   got_dot = 0;  
  64.   got_digit = 0;  
  65.   exponent = 0;  
  66.   for (;; ++s)  
  67.     {  
  68.       if (ISDIGIT (*s))  
  69.         {  
  70.           got_digit = 1;  
  71.   
  72.           /* Make sure that multiplication by 10 will not overflow.  */  
  73.           if (num > DBL_MAX * 0.1)  
  74.             /* The value of the digit doesn't matter, since we have already 
  75.                gotten as many digits as can be represented in a `double'. 
  76.                This doesn't necessarily mean the result will overflow. 
  77.                The exponent may reduce it to within range. 
  78.  
  79.                We just need to record that there was another 
  80.                digit so that we can multiply by 10 later.  */  
  81.             ++exponent;  
  82.           else  
  83.             num = (num * 10.0) + (*s - '0');  
  84.   
  85.           /* Keep track of the number of digits after the decimal point. 
  86.              If we just divided by 10 here, we would lose precision.  */  
  87.           if (got_dot)  
  88.             --exponent;  
  89.         }  
  90.       else if (!got_dot && *s == '.')  
  91.         /* Record that we have found the decimal point.  */  
  92.         got_dot = 1;  
  93.       else  
  94.         /* Any other character terminates the number.  */  
  95.         break;  
  96.     }  
  97.   
  98.   if (!got_digit)  
  99.     goto noconv;  
  100.   
  101.   if (TOLOWER (*s) == 'e')  
  102.     {  
  103.       /* Get the exponent specified after the `e' or `E'.  */  
  104.       int save = errno;  
  105.       char *end;  
  106.       long int exp;  
  107.   
  108.       errno = 0;  
  109.       ++s;  
  110.       exp = strtol (s, &end, 10);  
  111.       if (errno == ERANGE)  
  112.         {  
  113.           /* The exponent overflowed a `long int'.  It is probably a safe 
  114.              assumption that an exponent that cannot be represented by 
  115.              a `long int' exceeds the limits of a `double'.  */  
  116.           if (endptr != NULL)  
  117.             *endptr = end;  
  118.           if (exp < 0)  
  119.             goto underflow;  
  120.           else  
  121.             goto overflow;  
  122.         }  
  123.       else if (end == s)  
  124.         /* There was no exponent.  Reset END to point to 
  125.            the 'e' or 'E', so *ENDPTR will be set there.  */  
  126.         end = (char *) s - 1;  
  127.       errno = save;  
  128.       s = end;  
  129.       exponent += exp;  
  130.     }  
  131.   
  132.   if (endptr != NULL)  
  133.     *endptr = (char *) s;  
  134.   
  135.   if (num == 0.0)  
  136.     return 0.0;  
  137.   
  138.   /* Multiply NUM by 10 to the EXPONENT power, 
  139.      checking for overflow and underflow.  */  
  140.   
  141.   if (exponent < 0)  
  142.     {  
  143.       if (num < DBL_MIN * pow (10.0, (double) -exponent))  
  144.         goto underflow;  
  145.     }  
  146.   else if (exponent > 0)  
  147.     {  
  148.       if (num > DBL_MAX * pow (10.0, (double) -exponent))  
  149.         goto overflow;  
  150.     }  
  151.   
  152.   num *= pow (10.0, (double) exponent);  
  153.   
  154.   return num * sign;  
  155.   
  156. overflow:  
  157.   /* Return an overflow error.  */  
  158.   errno = ERANGE;  
  159.   return HUGE_VAL * sign;  
  160.   
  161. underflow:  
  162.   /* Return an underflow error.  */  
  163.   if (endptr != NULL)  
  164.     *endptr = (char *) nptr;  
  165.   errno = ERANGE;  
  166.   return 0.0;  
  167.   
  168. noconv:  
  169.   /* There was no number.  */  
  170.   if (endptr != NULL)  
  171.     *endptr = (char *) nptr;  
  172.   return 0.0;  
  173. }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值