关于使用STD :: Numeric_limits< t> :: Infinity()的算术操作
我有一个奇特的用例,其中我的边缘带有最初设置为std :: numeric_limits< double> :: Infinity()
的权重。这些权重将在程序执行中以后设置为其他。
现在我们有了上下文,这是主要问题。我需要将这些边缘的重量与我计算的某些重量的平方进行比较,并考虑到我计算的重量的平方,我还必须将边缘重量保持平整。当然,这将导致无穷大的初始繁殖。我想知道是否将双重设置乘以std :: numeric_limits< double> :: infinity()
本身是定义的行为。我可以期望它保持不变吗?
即使在cppreference上,我也找不到任何文档。
I have a peculiar usecase where I have some edges w/ double
weights set initially to std::numeric_limits<double>::infinity()
. These weights will be set to something else later on in program execution.
Now that we have the context, here is the main issue. I need to compare these edge weights to the square of some weight that I compute, and to account for the squaring of my computed weight, I will also have to square the edge weights. Of course, this will result in an initial multiplying of infinity by infinity. I am wondering if multiplying a double set to std::numeric_limits<double>::infinity()
by itself is defined behavior. Can I expect it to remain unchanged?
I could not find any documentation even on cppreference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将此答案限制在IEEE754上,使用+/- INF作为某种起始值会带来相当大的麻烦。
在IEEE754,
Inf乘以任何正浮点值(包括亚正态分子)值)是INF,类似于-inf。
换句话说,您需要将+0.0和-0.0视为特殊情况时,并且是签名负零会产生不同结果的罕见情况之一。如果您无法采用其他方案,请使用
std :: isnan
进行测试。Restricting this answer to IEEE754, using +/-Inf as some sort of starting value brings a fair bit of trouble.
Under IEEE754,
Inf multiplied by any positive floating point value (including a subnormal value) is Inf, and similarly for -Inf.
In other words, you need to treat +0.0 and -0.0 as special cases when multiplying, and is one of those rare cases where a signed negative zero produces a different result. Use
std::isnan
to test, if you cannot adopt some other scheme.