Interesting reading here. To summarize:

- Use algebraic trickery to avoid loss of precision; e.g. never subtract nearly equal numbers, i.e. with plenty of similar decimal places.
- Use algebraic trickery to avoid overflow; e.g. factorials grow very rapidly, so use logarithms, and write code that doesn’t have to compute factorials before computing the logs.
- Use analytical approximations to avoid loss of precision; e.g. computing 1 + x for very small value of x (down to or smaller than machine precision, e.g. 1e-16) gives 1, so that e.g. log(1 + x) / x gives 0, which is wrong (should be 1 as log(1 + x) limits to x for small values of x). The solution is to approximate log(1 + x) for very small values of x, e.g. using power series.
- Don’t calculate results you can easily predict; e.g. exp(x) + 1 is essentially the same as exp(x) for large x.

### Like this:

Like Loading...

*Related*

## Leave a Reply