关于使用STD :: Numeric_limits< t> :: Infinity()的算术操作

发布于 2025-01-22 08:27:38 字数 347 浏览 0 评论 0原文

我有一个奇特的用例,其中我的边缘带有最初设置为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久光 2025-01-29 08:27:38

将此答案限制在IEEE754上,使用+/- INF作为某种起始值会带来相当大的麻烦。

在IEEE754,

  1. INF * 0.0 = NAN
  2. -INF * 0.0 = INF * -0.0 = -nan
  3. Inf * Inf * Inf = -Inf * -inf * -inf = Inf
  4. -Inf = -Inf = -Inf = -Inf

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,

  1. Inf * 0.0 = NaN
  2. -Inf * 0.0 = Inf * -0.0 = -NaN
  3. Inf * Inf = -Inf * -Inf = Inf
  4. -Inf * Inf = -Inf

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文