机器精度
我想知道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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
#include
你有小正值 =
std::numeric_limits::denorm_min()
最大正值 =
std::numeric_limits< ;float>::max()
显然这也适用于其他类型。
请参阅 numeric_limits
并且不,最小正值的倒数不等于最大。
Using
#include <limits>
you haveSmall 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.
只是寻找数字限制信息?
该链接显示了如何使用 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.
这完全取决于您希望数字达到的精度,双精度型中的最大值非常大,但会遭受巨大的舍入误差。例如,如果您需要 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.