c++ std 的 inf 的行为是否与常识无穷大完全一样
Does c++ std's inf behave exactly as common-sensical infinity
i正在实现一种相当复杂的数值算法。它包含一些指数,其中我在打印中间结果时发现了一些指数,即有一些双重溢出。但是,终端打印inf
(不是Inf
或NaN
),似乎不是双重的,甚至不是最小或最大表达双重的双重。但是,inf
似乎表现得好像是扩展的实际数字系统(视角是正面和负面无限是一个真实数字,而不是涉及限制的陈述的缩写)。
我已经进行了测试:(G 版本:" Apple LLVM版本8.0.0(Clang-800.0.42.1)")
#include <iostream> // cout
#include <cmath> // exp, log
#include <limits> // data ranges
int main(void)
{
const double dMin =std::numeric_limits<int>::min();
const double dMax =std::numeric_limits<int>::max();
double p1 =std::exp(710);
double p2 =std::exp(750);
double p3 =std::exp(800);
double p4 =-std::log(0.0);
std::cout << "p1 :=std::exp(710) =" << p1 << 'n';
std::cout << "p2 :=std::exp(750) =" << p2 << 'n';
std::cout << "p3 :=std::exp(800) =" << p3 << 'n';
std::cout << "p4 :=-std::log(0.0) =" << p4 << 'n';
std::cout << "does p1==dMax? " << ( (p1==dMax) ? "yes" : "no" ) << 'n';
std::cout << "does p1==-dMin? " << ( (p1==-dMin) ? "yes" : "no" ) << 'n';
std::cout << "does p1==p2? " << ( (p1==p2) ? "yes" : "no" ) << 'n';
std::cout << "does p2==p3? " << ( (p2==p3) ? "yes" : "no" ) << 'n';
std::cout << "does p3==p4? " << ( (p3==p4) ? "yes" : "no" ) << 'n';
std::cout << "does 3*p1==p2/2+1? " << ( (3*p1==p2/2+1) ? "yes" : "no" ) << 'n';
std::cout << "does (-p1)*(-p2)==p3*p3*p3? " << ( ((-p1)*(-p2)==p3*p3*p3) ? "yes" : "no" ) << 'n';
std::cout << "does std::log(p2)==std::exp(p3)? " << ( (std::log(p2)==std::exp(p3)) ? "yes" : "no" ) << 'n';
}
输出:
p1 :=std::exp(710) =inf
p2 :=std::exp(750) =inf
p3 :=std::exp(800) =inf
p4 :=-std::log(0.0) =inf
does p1==dMax? no
does p1==-dMin? no
does p1==p2? yes
does p2==p3? yes
does p3==p4? yes
does 3*p1==p2/2+1? yes
does (-p1)*(-p2)==p3*p3*p3? yes
does std::log(p2)==std::exp(p3)? yes
inf
似乎确实类似于我们的无限概念,但不等于STD的最大和最小。
假设inf
的工作方式与常识无穷大一样,是否安全?我可以依靠涉及inf
的算法的中间步骤中的操作,将其视为真正的无限,并且最终仅处理其结果?还是如果没有,我应该抓住它吗?但是,我该怎么能,因为它不是最大或最小双重呢?
将此答案限制为IEEE754浮点, INF来自诸如 1.0 / 0.0
之类的计算,-inf来自 -1.0 / 0.0
。
另一种类型NaN
(不是数字)来自0.0 / 0.0
。
设置为+Inf
的两个浮点变量将彼此比较true
,同上-Inf
。它们代表std::numeric_limits<double>::max()
的不同数量。+Inf
大于除本身和NaN
以外的所有值。
请注意,设置为NaN
的两个浮点变量将彼此比较 true
,或与此相关的其他任何内容。
(对于价值,std::log(0.0)
必须返回-Inf
,而不是NaN
)。请参阅http://en.cppreference.com/w/cpp/numeric/math/log
正如其他答案已经提到的那样,浮点号的行为并未明确定义,但是IEC-559/ieee-754和大多数实施都使用此。第一件事是检查您的实施是否有效地使用了:
static_assert(std::numeric_limits<double>::is_iec559, "Stupid implementation!");
通过该断言,事情变得容易得多!首先,您可以使用以下实用程序函数来获得a true Infinity(您不需要is_iec559
才能真正工作...):
std::numeric_limits<double>::infinity()
更好,IEEE-754对于无限行为有明确定义的规则:http://steve.hollasch.net/cgindex/coding/ieeefloat.html#operations 1 ,例如-inf * -inf = +inf
,+inf + +inf = +Inf
等。标准的数学功能也具有无限的定义明确的行为,因此您也可以依靠它们。
因此,如果您的实现遵循IEEE标准,是,您可以依靠Infinity上的中间操作,只检查最终结果。
1 这不是官方资源,但据我所知,这是正确的。
做 c std的inf ...
C 标准没有指定无限表现的任何要求 - 或该无穷大可以用浮点数表示。
如果您的系统使用符合特定浮点标准的浮点数,则该标准可能会指定无限的行为。
似乎INF确实类似于我们的无限概念,但不等于STD的最大和最小。
确实。numeric_limits::max()
被定义为返回最大有限值。
[to catch infinity]如何,因为它不是最大或最小双重的?
如果通过 catch ,您的意思是要检查一个值是无穷大,可以与numeric_limits::infinity()
进行比较,或者如果您有C 11,则可以检查std::isinf()
负无穷大)。
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 为什么在C++中对链表这样做?(像堆叠一样处理它们)
- 堆栈和队列是否像C++中的数组一样传递?
- 是否有技术原因阻止 Java 中的 final C++ 像 const 一样严格?
- 无穷大而循环时具有递归函数
- 在C++中比较无穷大和无穷大
- 访问数据成员(本身是对象)的数据成员,就好像它们是类成员一样
- 我们可以将集合的值存储在变量中吗?就像我们可以将数组的值存储在变量中一样
- 我如何在C++像在 Python 中一样实现 f 字符串?
- 自制的上衣:看起来一样,但不完全相同
- 如何使用 redis-plus-plus 存储二进制数据,就像我想存储结构一样?@for_stack?
- 如何像在 C++ 中处理数组一样对 .txt 文件中的字符进行寻址?
- 如何将两个字符串加在一起,就好像它们是变量一样?
- 像 CPP 中的控制台一样的应用程序
- 为什么C++中没有常量引用,就像常量指针一样?
- 为什么将两个对象分配给另一个对象后,两个对象不一样?
- 有没有像给定的一样的 std::optional_function
- Java 是否像C++模板一样具有泛型推论?
- 记忆栅栏和记忆屏障是一样的吗
- c++ std 的 inf 的行为是否与常识无穷大完全一样