机器精度 [英] machine precision

查看:329
本文介绍了机器精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道有没有像eps这样的东西代表C ++中的机器精度的价值?我可以使用它作为double可以表示的最小的正数吗?是否可以使用1.0 / eps作为最大正数,双精度可以表示?在哪里可以在C ++和C标准库中找到eps?

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?

感谢和问候!

UPDATE:

为了我的目的,我想计算一个权重作为距离的倒数,例如反距离加权插值://en.wikipedia.org/wiki/Inverse_distance_weighting)。

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;

但是距离可以为0,我需要使重量适合计算。如果只有一个距离dist [i]是0,则我们希望其相应的值值[i]是主导的。如果有几个距离是0,我想有他们的价值观同样的贡献的结果。任何想法如何实现它?

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?

推荐答案

这完全取决于你希望从数字的精度,非常大,但遭受极大的舍入误差。如果你需要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.

这篇关于机器精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆