views:

61

answers:

4

Hi everyone

I did a course at university that explained how (amongst other things) to order your mathematical execution to maximize precision and reduce the risk of rounding errors in a finite precision environment.

We are working on a financial system with your usual interest calculations and such. Can somebody please share/remind me how to structure your calculations as to minimze loss of precision?

I know that, for instance, division must be avoided. Also, when you divide, to divide the largest number first, if possible.

+5  A: 

The cardinal rule of numerical computing is to avoid subtracting nearly equal numbers. Multiplication and division are always accurate: you lose at most one bit of precision in performing a multiply or divide. But if two numbers agree to n bits, you can lose up to n bits of precision in their subtraction.

There are all kinds of tricks for avoiding such subtractions. For example, suppose you need to calculate exp(x) - 1 for small values of x. (This is something you might do in an interest calculation.) If x is so small that exp(x) equals 1 to all the precision of the computer, then the subtraction will give exactly 0, and the resulting relative error will be 100%. But if you use the Taylor approximation exp(x) - 1 = x + x^2/2 + ... you could get a more accurate answer. For example, exp(10^-17) - 1 will be completely inaccurate, but 10^-17, the one-term Taylor approximation, would be very accurate. This is how functions like expm1 work. See the explanation of log1p and expm1 here.

If you're concerned about numerical accuracy, you need to understand the anatomy of floating point numbers in order to know what is safe and what is not.

John D. Cook
When subtracting, if one number is very large and the other is small, the result will be the same large number unaltered; and that repeated many times can yield to a significant drift too.
fortran
Aha! I now remember the Taylor approximation used in this context. You are making exactly the kinds of arguments I was looking for. You don't have any online sources that I can continue reading up on? I'll check your blog soon.
Andre Luus
Besides the links in my answer, you might want to look at http://www.johndcook.com/blog/2010/07/27/sine-approximation-for-small-x/. It's specifically about sine functions, but more generally it's about Taylor approximations, relative error, etc.
John D. Cook
+2  A: 

Use amounts in cents, not dollars.

lhf
+1  A: 

Loss of precision is usually related to the use of floating point binary representations. A financial system should not use such representations and use arbitrary precision numbers instead (Such as BigDecimal in Java and decimal in .NET). That should be your first move.

Matias Valdenegro
Yes, we're already working with decimals.
Andre Luus
A: 

There is also the possibility to employ Interval Arithmetic

Ronny