机器精度

发布于 2024-08-19 20:22:49 字数 655 浏览 6 评论 0原文

我想知道C++中是否有类似eps的东西来表示机器精度的值?我可以用它作为 double 可以表示的最小正数吗?是否可以使用 1.0/eps 作为 double 可以表示的最大正数?在哪里可以找到 C++ 和 C 标准库中的 eps?

谢谢和问候!


更新:

出于我的目的,我想将权重计算为距离的倒数,例如反距离加权插值(http://en.wikipedia.org/wiki/Inverse_distance_weighting)。

double wgt = 0, wgt_tmp, result = 0;
for (int i = 0; i < num; i++)
{
   wgt_tmp = 1.0/dist[i];
   wgt += wgt_tmp;
   result += wgt_tmp * values[i];
}
results /= wgt;

但是距离可以为 0,我需要使权重适合计算。如果只有一个距离 dist[i] 为 0,我希望其对应的值 value[i] 占主导地位。如果有几个距离为0,我希望它们的值对结果有同等的贡献。知道如何实施吗?

I wonder if there is something like eps to represent the value of machine precision in C++? Can I use it as the smallest positive number that a double can represent? Is it possible to use 1.0/eps as the max positive number that a double can represent? Where can I find eps in both C++ and C standard libraries?

Thanks and regards!


UPDATE:

For my purpose, I would like to compute a weight as reciprocal of a distance for something like inverse distance weighting interpolation (http://en.wikipedia.org/wiki/Inverse_distance_weighting).

double wgt = 0, wgt_tmp, result = 0;
for (int i = 0; i < num; i++)
{
   wgt_tmp = 1.0/dist[i];
   wgt += wgt_tmp;
   result += wgt_tmp * values[i];
}
results /= wgt;

However the distance can be 0 and I need to make the weight suitable for computation. If there is only one distance dist[i] is 0, I would like its corresponding value values[i] to be dominant. If there are several distances are 0, I would like to have their values to contribute equally to the result. Any idea how to implement it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

泪眸﹌ 2024-08-26 20:22:49

使用 #include 你有

小正值 = std::numeric_limits::denorm_min()

最大正值 = std::numeric_limits< ;float>::max()

显然这也适用于其他类型。

请参阅 numeric_limits

并且不,最小正值的倒数不等于最大。

Using #include <limits> you have

Small positive value = std::numeric_limits<float>::denorm_min()

Largest positive value = std::numeric_limits<float>::max()

Obviously this applies to other types as well.

See numeric_limits

And no, the inverse of the smallest positive value does not equal the largest.

情话难免假 2024-08-26 20:22:49

只是寻找数字限制信息?

该链接显示了如何使用 C++ 标准库查找 epsilon、非规范化最小值等。 C 标准库中没有这些的等效项。您需要自己计算它们(维基百科关于“机器 epsilon”的文章给出了一个例子)...

至于算法,无法帮助您,这不是您最初问题的一部分,抱歉。

Just looking for numeric limits information?

The link shows how to find the epsilon, denormalized min, etc., using the C++ Standard Library. There is no equivalent for these in the C Standard Library. You would need to compute them yourself (the Wikipedia article on "machine epsilon" gives an example)...

As for the algorithm, can't help you there, and this wasn't part of your original question, sorry.

清旖 2024-08-26 20:22:49

这完全取决于您希望数字达到的精度,双精度型中的最大值非常大,但会遭受巨大的舍入误差。例如,如果您需要 1e-3 的精度,则浮点后至少需要 10 位,这意味着在双精度数的情况下,您不应有任何大于尾数中的位数减 10 的指数,即52 - 10 = 42,最大约为 4e12,相应的最小约为 2.5e-13。

This depends entirely on the precision you desire from your numbers, the maximum value in a double is very large, but suffers from tremendous rounding errors. If you need a precision of 1e-3 for instance you need at least 10 bits after the floating point, meaning you should not have any exponent greater than the number of bits in the mantissa minus 10, in the case of a double, that is 52 - 10 = 42, leaving you with a maximum of about 4e12 and a corresponding minimum of about 2.5e-13.

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