gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:45:35 25 4
gpt4 key购买 nike

源代码(我不确定这是哪个版本,它只是网站的摘录)。在 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) 导致程序控制不转到包含以下代码的代码:当前位数存入累加值。

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

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

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

关于c - 为什么当电流已经超过 `strtod` 时 `DBL_MAX*0.1` 忽略数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18930304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com