Numerical programming tips

4 10 2012

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.
Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: