[Openmcl-devel] Floating point precision

Brian Mastenbrook brian at mastenbrook.net
Sun Jan 18 00:32:23 UTC 2009


On Jan 17, 2009, at 5:37 PM, Sudhir Shenoy wrote:

> My question arose from a bug I had in some code for financial
> instrument valuation where my results were accurate to about 3
> decimals (as compared to a desk calculator) and I thought I was doing
> all double-precision arithmetic. I had set *read-default-float-format*
> but somewhere in the calculations a rational was being passed to a
> math function and was the cause of loss of precision.

For financial calculations it's preferable to either decimal floats or  
keep everything in rational format to avoid cumulative errors from  
inexact binary representation. There is a good overview page (and  
library code in C) for decimal floats here:

http://speleotrove.com/decimal/

The FAQ gives a good overview of why this is preferable:

http://speleotrove.com/decimal/decifaq1.html

Your desk calculator is most likely using decimal floats as well as  
this is by far the most common mode for standalone calculators.

I would love to see a Common Lisp implementation that used decimal  
floats instead of binary floats, but I'm not aware of such an animal  
right now.

--
Brian Mastenbrook
brian at mastenbrook.net
http://brian.mastenbrook.net/




More information about the Openmcl-devel mailing list