문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

Use amounts in cents, not dollars.

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.

There is also the possibility to employ Interval Arithmetic

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top