Re: Currency Rounding Errors
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Tue, 21 Apr 2009 09:53:05 -0400
See below...
On Tue, 21 Apr 2009 01:49:14 -0700, "Mihai N." <nmihai_year_2000@xxxxxxxxx> wrote:
****2**64 should handle most situations dealing with real objects
True. At the time when I was trying to solve such things
I did not have 64 bits, only 16
the entire U.S. national budget can be represented in 10**15 cents
If you want to stop at that precision.
(what I did was not for financial stuff, so 3 digits precision was
not enough)
But BCD does not generally solve the problem that
if you multiply m1.n1 * m2.n2 you get (m1+m2).(n1.n2) bits of precision.
No, it does not.
What I did was my own Number class, that stored the digits in an array,
and implemented the basic operations on top of that.
The performance was abismal compared to native types,
but I had arbitrary precision, no overflows, no rounding probelms.
And to save memory (at the time it mattered) and get some extra
performance, I was doing it on an "array of nibbles" instead of
array of bytes. So BCD was not enough.
The x86 is the first machine in history to waste 5 of the bits of the byte to create BCD.
Most systems historically put BCD in nybbles.
****
****
but in the x86, you have to loop over the
bytes and do weird instructions such as ADD followed by AAA, SUB
followed by AAS, and so on, in a loop.
There's a whole bunch of such funny instructions such as
decimal-adjust-before-divide and decimal-adjust-before-multiply as well.
Yes, that was my task :-)
So I'm not sure how BCD is more desirable than fixed-point-integer,
Maybe it is not anymore. In the PC + DOS time I did not have anythiong better
(and BCD was just an implementation detail)
Even in MS-DOS you could do arbitrary-precision arithmetic by using the instructions ADC
and SBB. Multiply and divide were a bit tricky, but Knuth actually outlined the core
algorithms in Volume 1: Fundamental Algorithms, back in the late 1960s. But certainly
scaling from 16 bits to 64 bits as native operations changes the picture a lot.
joe
****
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Follow-Ups:
- Re: Currency Rounding Errors
- From: Mihai N.
- Re: Currency Rounding Errors
- References:
- Currency Rounding Errors
- From: Jonathan Wood
- Re: Currency Rounding Errors
- From: Mihai N.
- Re: Currency Rounding Errors
- From: Joseph M . Newcomer
- Re: Currency Rounding Errors
- From: Mihai N.
- Re: Currency Rounding Errors
- From: Joseph M . Newcomer
- Re: Currency Rounding Errors
- From: Mihai N.
- Re: Currency Rounding Errors
- From: Joseph M . Newcomer
- Re: Currency Rounding Errors
- From: Mihai N.
- Currency Rounding Errors
- Prev by Date: Re: SetTimer() & callback handler
- Next by Date: Re: Currency Rounding Errors
- Previous by thread: Re: Currency Rounding Errors
- Next by thread: Re: Currency Rounding Errors
- Index(es):
Relevant Pages
|