Re: Currency Rounding Errors

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
.



Relevant Pages

  • Re: Currency Rounding Errors
    ... If you want to stop at that precision. ... What I did was my own Number class, that stored the digits in an array, ... The x86 is the first machine in history to waste 5 of the bits of the byte to create BCD. ... bytes and do weird instructions such as ADD followed by AAA, ...
    (microsoft.public.vc.mfc)
  • Re: how do I get more numbers past the decimal?
    ... Other common formats are 64 ... So for the multi precision arithmetic, ... BCD is no more and no less accurate on generic numbers than anything else., its virtue is its exact reproduction of decimal numbers like 12.3456. ... With BCD you generally use one byte per significant digit, so its as precise on the mantissa as the number of bytes allocated. ...
    (comp.lang.php)
  • Re: how do I get more numbers past the decimal?
    ... Other common formats are 64 ... So for the multi precision arithmetic, ... BCD is no more and no less accurate on generic numbers than anything else., its virtue is its exact reproduction of decimal numbers like 12.3456. ... With BCD you generally use one byte per significant digit, so its as precise on the mantissa as the number of bytes allocated. ...
    (comp.lang.php)
  • Re: how do I get more numbers past the decimal?
    ... Other common formats are 64 ... So for the multi precision arithmetic, ... BCD is no more and no less accurate on generic numbers than anything else., its virtue is its exact reproduction of decimal numbers like 12.3456. ... manipulation of numerical data for display can be greatly simplified ...
    (comp.lang.php)
  • Re: Call Array valued Fortran function from C
    ... documentation about gfortran array descriptors for you. ... double precision, dimension:: array ... type, bind:: descr ... integerunknown1 ...
    (comp.lang.fortran)