Re: Currency Rounding Errors
- From: "Mihai N." <nmihai_year_2000@xxxxxxxxx>
- Date: Tue, 21 Apr 2009 01:49:14 -0700
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.
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)
--
Mihai Nita [Microsoft MVP, Visual C++]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
.
- Follow-Ups:
- Re: Currency Rounding Errors
- From: Joseph M . Newcomer
- 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
- Currency Rounding Errors
- Prev by Date: Re: C++ vs. C#
- Next by Date: Re: SetTimer() & callback handler
- Previous by thread: Re: Currency Rounding Errors
- Next by thread: Re: Currency Rounding Errors
- Index(es):
Relevant Pages
|