Conversion of a decimal to double number in C# results in a difference -


Summary of problem:

For some decimal values, when we change the decimal type to double , A small fraction is added in the result.

What's worse than this is that there can be two "par" decimal values, which is the result of different double values ​​when changed.

Code sample:

  decimal DCM = 8224055000.0000000000 M; // DCM = 8224055000 double DBL = convert toDouble (DCM); // dbl = 8224055000.000001 decimal dcm2 = convert toDecimal (dbl); // DCM2 = 8224055000 DoubleDouble 2 = Convert. Tud Double (DCM2); // dbl2 = 8224055000.0 decimal delta DCM = DCM2 - DCM; // Delta DCM = 0 Double Delta DBL = DBL2 - DBL; // deltaDbl = -0.00000095367431640625  

The results in the comments in the results are copied from the clock of the debugger. Number of digit numbers in comparison to the data type limit , So it can not be overflow (I think!).

What's more interesting than the two values ​​ equal (see the above code sample, see the "DCM" and "DCM 2", with zero equal to "Delta DCM") whose As a result, when different change (Code, "DBL" and "DBL2", which has a non-zero "Delta Doll")

I think it should be related to the difference in the bitwise framework of the two data type But do not know what can do! And I have to know what to do to make the conversion the way I need it. (Eg DCM 2 -> dlb2)

Interesting - Although I usually rely on general methods of writing Floating point value when you are interested in the exact results

Here's a little simple demonstration, which I have used some time ago.

  using the system; Class test {static zero major () {decimal DCM 1 = 8224055000.0000000000 M; Decimal DCM2 = 8224055000 meters; Double dbl1 = (double) DCM 1; Double DBL 2 = (double) DCM 2; Console.WriteLine (DoubleConverter.ToExactString (dbl1)); Console.WriteLine (DoubleConverter.ToExactString (dbl2)); }}  

Result:

  8224055000.00000095367431640625 8224055000  

The question is now that the original value (8224055000.0000000000) is an integer Is - and it is attainable as absolutely double - ends with additional data. I strongly suspect that in the algorithm decimal to double , but it is unfortunate.

It also violates Section 6.2.1 of C # space:

To convert from decimal to float or double, double the decimal value to the nearest double or float Goal is rounded in value. Although it can lose conversion accuracy, there is no exception to throw it at any time.

"The nearest double value" is clearly just 8224055000 ... so this is a bug IMO, this is not the one which I can fix soon enough. (This gives the same result as .net 4.0b1.)

To avoid a bug, possibly to make the decimal value first, after the decimal point, the additional 0s can be effectively " Remove ". This is a bit tricky because it includes 96-bit integer arithmetic - .NET 4.0 BigCenter class can make it easy, but this is not an option for you.


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -