0

源代码(我不确定这是哪个版本,它只是网站的摘录)。在 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;
}
4

1 回答 1

1

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

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

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

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

于 2013-09-21T11:44:04.013 回答