Search code examples
pythonoptimizationsubtraction

substitute small floats in python


For an error function, I am computing the absolute error, namely abs(should-simulated).

However, unfortunately, my simulated values can be quite small, for example 1e-38. When I try to subtract them from should, the output is equal to should, because the values are too small for Python. To prevent this, I took the logarithm of should and simulated, however, this makes the error non-linear, which does not suit my purposes. I tried using the decimal package, but even that doesn't work with such small numbers. Is there any way of calculating a subtraction with small values without using the log or decimals?


Solution

  • The decimal package can deal with any arbitrary user-defined precision. You can get the default precision with decimal.getcontext().prec, and also set this value to get a better precision. On my machine, it is 28 by default which is not so huge but enough for most cases. You can set it to a huge value like 300 if you want a very extreme precision, but 50 should already be clearly enough. Note that a relative precision of 1e-42 is the one of an atom nucleus over the whole observable universe.