[Openmcl-devel] Floating point precision
Sudhir Shenoy
sshenoy at gol.com
Sat Jan 17 18:18:41 PST 2009
On Jan 18, 2009, at 9:32 AM, Brian Mastenbrook wrote:
>
> For financial calculations it's preferable to either decimal floats
> or keep everything in rational format to avoid cumulative errors
> from inexact binary representation.
Yes, this is true for currency related calculations (interest, tax,
etc.) and most accounting packages use BCD arithmetic as a result.
What I am doing, however, is valuation of complex instruments (options
and other derivatives) where you need double floats because, mostly,
you are working with probability distributions and other irrational
numbers such as natural logarithms. Inputs to the program from the
external world can be coerced to double-float by default but I was
unaware that conversions from integers or rationals to float would
always use single-float instead of double as per the standard. I now
have to go through the code and ensure that all constants are affixed
with 'd0' even where they are exact integers because of the danger of
intermediate results losing precision if these constants are passed to
math functions ...
It is a pity that there is no analogue to *read-default-float-format*
to specify this conversion rule. I guess that when the standard was
written, floating point calculations were deemed to be expensive and
the coming of cheap and fast floating point processors wasn't foreseen
(or maybe all scientific computation at the time used FORTRAN
anyhow :-).
Would it be a good idea to introduce a special variable in the CCL
package, e.g. *float-substitute-target*, that would default to 'single-
float but could be set to 'double-float if required (and introduce a
conditional in all places that an int->float conversion is performed)?
Cheers
Sudhir
More information about the Openmcl-devel
mailing list