当电流已经超过`DBL_MAX * 0.1`时,为什么`strtod`只是忽略数字

时间:2013-09-21 07:57:36

标签: c double strtod

源代码(我不确定这是哪个版本,它只是网站的摘录)。在for循环的最开始,评论说“我们已经掌握了足够的数字,而我们只会忽略其余部分”。

为什么这是真的?为什么这个“并不一定意味着结果会溢出。”?

/* Convert NPTR to a double.  If ENDPTR is not NULL, a pointer to the
   character after the last one used in the number is put in *ENDPTR.  */
double
strtod (const char *nptr, char **endptr)
{
  register const char *s;
  short int sign;

  /* The number so far.  */
  double num;

  int got_dot;                  /* Found a decimal point.  */
  int got_digit;                /* Seen any digits.  */

  /* The exponent of the number.  */
  long int exponent;

  if (nptr == NULL) 
    {
      errno = EINVAL;
      goto noconv; 
    }

  s = nptr;

  /* Eat whitespace.  */
  while (ISSPACE (*s))
    ++s;

  /* Get the sign.  */
  sign = *s == '-' ? -1 : 1;
  if (*s == '-' || *s == '+')
    ++s;

  num = 0.0;
  got_dot = 0;
  got_digit = 0;
  exponent = 0;
  for (;; ++s)
    {
      if (ISDIGIT (*s))
        {
          got_digit = 1;

          /* Make sure that multiplication by 10 will not overflow.  */
          if (num > DBL_MAX * 0.1)
            /* The value of the digit doesn't matter, since we have already
               gotten as many digits as can be represented in a `double'.
               This doesn't necessarily mean the result will overflow.
               The exponent may reduce it to within range.

               We just need to record that there was another
               digit so that we can multiply by 10 later.  */
            ++exponent;
          else
            num = (num * 10.0) + (*s - '0');

          /* Keep track of the number of digits after the decimal point.
             If we just divided by 10 here, we would lose precision.  */
          if (got_dot)
            --exponent;
        }
      else if (!got_dot && *s == '.')
        /* Record that we have found the decimal point.  */
        got_dot = 1;
      else
        /* Any other character terminates the number.  */
        break;
    }

  if (!got_digit)
    goto noconv;

  if (TOLOWER (*s) == 'e')
    {
      /* Get the exponent specified after the `e' or `E'.  */
      int save = errno;
      char *end;
      long int exp;

      errno = 0;
      ++s;
      exp = strtol (s, &end, 10);
      if (errno == ERANGE)
        {
          /* The exponent overflowed a `long int'.  It is probably a safe
             assumption that an exponent that cannot be represented by
             a `long int' exceeds the limits of a `double'.  */
          if (endptr != NULL)
            *endptr = end;
          if (exp < 0)
            goto underflow;
          else
            goto overflow;
        }
      else if (end == s)
        /* There was no exponent.  Reset END to point to
           the 'e' or 'E', so *ENDPTR will be set there.  */
        end = (char *) s - 1;
      errno = save;
      s = end;
      exponent += exp;
    }

  if (endptr != NULL)
    *endptr = (char *) s;

  if (num == 0.0)
    return 0.0;

  /* Multiply NUM by 10 to the EXPONENT power,
     checking for overflow and underflow.  */

  if (exponent < 0)
    {
      if (num < DBL_MIN * pow (10.0, (double) -exponent))
        goto underflow;
    }
  else if (exponent > 0)
    {
      if (num > DBL_MAX * pow (10.0, (double) -exponent))
        goto overflow;
    }

  num *= pow (10.0, (double) exponent);

  return num * sign;

overflow:
  /* Return an overflow error.  */
  errno = ERANGE;
  return HUGE_VAL * sign;

underflow:
  /* Return an underflow error.  */
  if (endptr != NULL)
    *endptr = (char *) nptr;
  errno = ERANGE;
  return 0.0;

noconv:
  /* There was no number.  */
  if (endptr != NULL)
    *endptr = (char *) nptr;
  return 0.0;
}

1 个答案:

答案 0 :(得分:1)

从字面上回答你的第一个问题,“为什么这是真的?”,这是因为代码if (num > DBL_MAX * 0.1)导致程序控制不能转到将当前数字合并到累加值中的代码。

代码以这种方式编写的原因是作者可能发现停止处理数字比设计和实现completely correct conversion routine更容易。此代码读取数字并在num中构建一个值。例如,如果输入为“1234”,则代码将num设置为1,然后设置为12(1•10 + 2),然后设置为123(12•10 + 3),然后设置为1234(123•10 + 4) )。如果输入包含如此多的数字以至于接近double的最大有限值,则继续此过程是不安全的,因为算术可能溢出双精度的最大有限值。相反,程序只计算数字(通过递增其exponent),以便稍后可以调整它们。

即使有这么多的数字,它们本身也会溢出一个double的最大有限值,最终值可能不会溢出,因为可能存在负指数。例如,你可以有一千个十进制数字,后跟“e-1000”,它们一起代表一个小于一的数字。

此代码允许在浮点运算中舍入以影响其结果,并且在需要从十进制到double的正确舍入转换时不应使用此代码。