c++ std 的 inf 的行为是否与常识无穷大完全一样

Does c++ std's inf behave exactly as common-sensical infinity

本文关键字:一样 无穷大 常识 inf std 是否 c++      更新时间:2023-10-16

i正在实现一种相当复杂的数值算法。它包含一些指数,其中我在打印中间结果时发现了一些指数,即有一些双重溢出。但是,终端打印inf(不是InfNaN),似乎不是双重的,甚至不是最小或最大表达双重的双重。但是,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()负无穷大)。