C语言 为什么“strtod”在电流已经超过“DBL_MAX*0.1”时忽略数字



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

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

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

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

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

最新更新