Is there an equivalent of the decimal type in D?

Sean Kelly sean at f4.ca
Sat Jan 27 15:15:15 PST 2007


Jeff Nowakowski wrote:
> Sean Kelly wrote:
>> Unless you're simply dealing with numbers too large to be represented 
>> exactly with floating-point (16 digits for 64-bit double, and I can't 
>> remember the limit for 80-bit real offhand), I think it makes more 
>> sense to use floating point internally and round to the appropriate 
>> degree of precision for display.
> 
> Floating point is a very bad idea for representing money.  If you're 
> adding up currencies then you want an exact representation, not "close 
> enough".  Sooner or later you will get bitten by rounding error if you 
> use floats.

And this is more of a problem than rounding error with BCD?  Most BCD 
representations I've seen only use around 6 decimal places, which is far 
too few in some cases.

>> The other consideration would be if the financial spec required 
>> rounding to specific degrees of precision at various points in the 
>> calculation, though rounding with floating-point is still obviously an 
>> option.
> 
> A very bad option.  If the interest rate is 0.0239 or whatever, then 
> that is what you should use, not the "close enough" floating point.  You 
> then use Banker's Rounding whenever you adjust an account.

I still fail to see how a floating point calculation that adjusts to the 
appropriate precision for display will be less precise than BCD.  If 
that's the case, why is floating point used for science applications 
where precisions is typically even more important?


Sean


More information about the Digitalmars-d-learn mailing list