4 ms·
I still think the best way to deal with this stuff is to use interval arithmetic. Calculate the numbers with rounding set to round up, and separately to round d
by rjmunro 5y ago
I still think the best way to deal with this stuff is to use interval arithmetic. Calculate the numbers with rounding set to round up, and separately to round down and compare the results. See PyInterval https://pyinterval.readthedocs.io/ https://pyinterval.readthedocs.io/ for an implementation.
>>> from interval import interval
>>> data = (interval(1_000_000_000.1), interval(1.1)) * 50_000
>>> sum(data) / len(data)
interval([500000000.5971994, 500000000.601347])
The result is definitely between the 2 values. If you try another method to do the calculation, (e.g. sorting first) you will know which one was better without the answer having to be obvious as it is in this case.
You might find one method is better at the lower bound, the other is better at the higher bound, which means you can narrow the value further than using either individually. It's a really powerful system that I think should be much better known.
- nestorD 5y agoYou should be okay with a sum but interval arithmetic can blow up in a way that is not correlated with the numerical accuracy of the algorithm. Newton's method, for example, is strongly convergent but produces diverging intervals.
- uyt 5y agoWhy does that happen? Is it because the errors are in opposite directions and will cancel each other out?
- nestorD 5y agoThat (and overall the unability to take correlation between error into account) and the fact that intervals propagated are upper bounds for some things like the division.
- rjmunro 5y agoYou can use other algorithms in these cases, and you can do things like converge your interval on a result by testing different values and seeing if the result is above the root or below the root, (or if it's within the error, so not possible to determine).