Re: Monetary calculations in CL
- From: Greg Menke <gusenet@xxxxxxxxxxx>
- Date: Thu, 14 Feb 2008 13:57:16 -0500
"John Thingstad" <jpthing@xxxxxxxxx> writes:
På Thu, 14 Feb 2008 13:50:18 +0100, skrev Greg Menke <gusenet@xxxxxxxxxxx>:
It sounds reasonable for accounting purposes but is insufficient for
financial arithmetic- there are often rules for the application of
things like yield rates that specify a number of places of precision and
specific rules for rouding; for example, maintain 12 decimal places and
round the 13th (by implication do not employ precision to more than the
13th place to the right of the decimal). Intermediate products of some
calculations can easily consume 20 or more significant digits.
Simple arithmetic on such calculations is inappropriate. Cobol handles
this sort of thing quite well (intentionally I am sure). CL's numeric
types will easily handle the required precision but the rounding rules
will probably be somewhat troublesome. The class-based approach is
probably the way to handle it- perhaps an "interest" class where the #
of places of significance and the rounding policy are supplied.
Gregm
Well the most straightforward way is Binary Coded Decimal (BCD).
One nibble (4 bits) is reserved for each digit 0-9. So you write a
library to access numbers this way.
(Many processors have support for this.) Writing such a library is a bit
cumbersome, but still easier than dealing with the imprecision
explicitly in bignums I would think. (Because humans think of precision
in digits not binary)
Probably the easiest way would be to try and do it how COBOL does- since
it was designed to handle this sort of problem. Its worth observing
that when doing this kind of math, conformance to the specified rounding
policy is far more important than speed, both in the operational sense,
and when the time comes to audit the system, and maybe even more
importantly when you have to start tracking down where the 1 cent error
came from...
This is perhaps an example of an "Anti-Greenspun Law", such as, "Any
sufficiently complex financial application contains an ad-hoc,
informally specified, bug ridden slow implementation of COBOL's fixed
place numeric types".
I've seen it happen on a number of occasions, and I've perpetrated it
myself.
Gregm
.
- References:
- Monetary calculations in CL
- From: Chaitanya Gupta
- Re: Monetary calculations in CL
- From: Kent M Pitman
- Re: Monetary calculations in CL
- From: Chaitanya Gupta
- Re: Monetary calculations in CL
- From: Greg Menke
- Re: Monetary calculations in CL
- From: John Thingstad
- Monetary calculations in CL
- Prev by Date: Re: Paul Graham's Arc is released today... what is the long term impact?
- Next by Date: Re: restricted lisp
- Previous by thread: Re: Monetary calculations in CL
- Next by thread: Re: Monetary calculations in CL
- Index(es):
Relevant Pages
|