views:

201

answers:

4

Hi,

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?

+3  A: 

Good discussion here

Martin Beckett
Thanks, my questions are more than that.
Tim
+2  A: 

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.

Thanks, my questions are more than that.
Tim
Ok. When I posted this answer, this was all you were asking for...
+2  A: 

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.

Peter Alexander
Thanks, can you try to answer my other questions please?
Tim
+1  A: 

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.

wich