juanchoc
August 9th, 2009, 04:45 PM
Hi!
I was wondering if it is possible to make a reimplementation of the .NET Decimal structure, but limiting its precision so that it fits as a 32-bit or 64-bit data type?
The .NET Decimal implementation is 128-bit and gives a 28 decimal number fixed point precision, which provides much better numerical stability for scientific/financial computation in comparison with the floating point Double.
That is the good side of the Decimal data type, but this increase in precision comes at a very high cost... Performance on Decimal operations is really slower in comparison with double operations, ranging from 20x to 40x slower (the actual performance hit depends on a wide number of factors, but it's really noticeable in any of the different scenarios possibles).
This is mainly due to a hardware problem from what I've seen.
Double operations on a 64-bit OS/processor are atomic (meaning that each operation on a Double, is a single indivisible operation), while Decimal operations are non-atomic (meaning that each Decimal operation, requires more than one processor operation).
This is a common trade-off between performance and precision, if you need performance you use Double, if you need precision you use Decimal.
My question is, wouldn't it be good to have something in between? Some of the precision of the fixed point Decimal, with some of the added performance of 64-bit/32-bit data types?
I'll be thankful for any info or comments on this subject.
Thanks in advance,
JCC
I was wondering if it is possible to make a reimplementation of the .NET Decimal structure, but limiting its precision so that it fits as a 32-bit or 64-bit data type?
The .NET Decimal implementation is 128-bit and gives a 28 decimal number fixed point precision, which provides much better numerical stability for scientific/financial computation in comparison with the floating point Double.
That is the good side of the Decimal data type, but this increase in precision comes at a very high cost... Performance on Decimal operations is really slower in comparison with double operations, ranging from 20x to 40x slower (the actual performance hit depends on a wide number of factors, but it's really noticeable in any of the different scenarios possibles).
This is mainly due to a hardware problem from what I've seen.
Double operations on a 64-bit OS/processor are atomic (meaning that each operation on a Double, is a single indivisible operation), while Decimal operations are non-atomic (meaning that each Decimal operation, requires more than one processor operation).
This is a common trade-off between performance and precision, if you need performance you use Double, if you need precision you use Decimal.
My question is, wouldn't it be good to have something in between? Some of the precision of the fixed point Decimal, with some of the added performance of 64-bit/32-bit data types?
I'll be thankful for any info or comments on this subject.
Thanks in advance,
JCC