- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
源代码(我不确定这是哪个版本,它只是网站的摘录)。在 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/
我正在尝试为所有可表示的有限值生成具有大致相等概率的随机 double 。我相信这类似于随机签名的 exponential_distribution,uniform_distribution 将不起作
在 Visual C++ 2010 中,我试过这个 double d= DBL_MAX; double dblmaxintpart; modf(DBL_MAX, &dblmaxintpart); 在我
如果之前有人问过这个问题,我深表歉意,但我通读了许多 DBL_MAX 线程,但没有看到这个确切的问题。 在我的 float.h header 中,变量定义为 #define DBL_MAX
我刚刚读到有关 IEEE 754 的信息标准以了解如何single-precision和 double-precision实现了 float 。 所以我写了这个来检查我的理解: #include #
代码 #include #include #include int f( double x, double y, double z){ return (x+y)+z == x+(y+z); }
我在某处看到这段代码 printf("DBL_MAX : %g\n", (double) DBL_MAX); Actor 是必要的吗?我在标准中找不到任何指定 DBL_MAX 必须实际具有
这一行: NSLog(@"DBL_MAX: %f", DBL_MAX); 打印这个非常大的值:17976931348623157081452742373170435679807056752584499
我代表一个“物理上不可能”的值,在计算机上可能代表的最负值。 #include double physically_impossible = -DBL_MAX; 这个测试效果很好 if( physi
我在我的 iPhone 上运行这段代码: double d = DBL_MAX; NSString *s = [NSString stringWithFormat:@"%.0f", d]; doubl
我想要极端的值(value)。 #include using namespace std; #include #include #include #define THRESHOLD 2*DBL
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
我正在编写一个 ptree,在某些时候我需要将 DBL_MAX 放入(作为默认值)。打开生成的 xml 文件时,我看到了正确的数字。 但是当我使用 ptree.get 获取数字时抛出异常:conver
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 8 年前。 Improve t
我使用的是 Embarcadero RAD Studio 的评估版。我为静态库中的代码和主应用程序中的代码获得了不同的 DBL_MAX 值。 代码是 双 x = DBL_MAX; 在静态库的代码中,x
我的一个函数返回一个 double,我想用一个无效值来指示错误条件,例如 DBL_MIN、DBL_MAX 或 -DBL_MAX。 #include #include #include doubl
我正在登录(写入文件)backgroundTimeRemaining每次我的应用程序从挂起模式唤醒时的值,就在我使用过期处理程序开始 UIApplication 的后台任务之前,就像这样(在我发出网络
我正在查看由以下代码片段生成的日志消息。 Flog 是用于调用内部日志记录类的#define。 代码来 self 项目中包含的 Pod 中的源文件。我不知道这是否重要。 我对这条消息感到困惑,因为在执
源代码(我不确定这是哪个版本,它只是网站的摘录)。在 for 循环的最开始,注释说“我们已经获得了足够的数字,我们将忽略其余的”。 为什么这是真的?为什么这“并不一定意味着结果会溢出。”? /* Co
在我的代码中,我使用了在 math.h 中定义的 DBL_MAX。在 VS2010 中它工作正常,但在 VS2008 中出现错误: error: C2065: 'DBL_MAX' : undeclar
在进行转换测试时,我在 C++ 中遇到了一些奇怪的行为。 上下文 网上的C++引用表明std::numeric_limits::max()的返回值(在 limit.h 中定义)应该是 DBL_MAX
我是一名优秀的程序员,十分优秀!