I am not sure if this non-standard way of stating a Stack Overflow question is good or bad, but here goes:
What is the best (mathematical or otherwise technical) explanation why the code:
static void Main()
{
decimal[] arr =
{
42m,
42.0m,
42.00m,
42.000m,
42.0000m,
42.00000m,
42.000000m,
42.0000000m,
42.00000000m,
42.000000000m,
42.0000000000m,
42.00000000000m,
42.000000000000m,
42.0000000000000m,
42.00000000000000m,
42.000000000000000m,
42.0000000000000000m,
42.00000000000000000m,
42.000000000000000000m,
42.0000000000000000000m,
42.00000000000000000000m,
42.000000000000000000000m,
42.0000000000000000000000m,
42.00000000000000000000000m,
42.000000000000000000000000m,
42.0000000000000000000000000m,
42.00000000000000000000000000m,
42.000000000000000000000000000m,
};
foreach (var m in arr)
{
Console.WriteLine(string.Format(CultureInfo.InvariantCulture,
"{0,-32}{1,-20:R}{2:X8}", m, (double)m, m.GetHashCode()
));
}
Console.WriteLine("Funny consequences:");
var h1 = new HashSet<decimal>(arr);
Console.WriteLine(h1.Count);
var h2 = new HashSet<double>(arr.Select(m => (double)m));
Console.WriteLine(h2.Count);
}
gives the following "funny" (apparently incorrect) output:
42 42 40450000 42.0 42 40450000 42.00 42 40450000 42.000 42 40450000 42.0000 42 40450000 42.00000 42 40450000 42.000000 42 40450000 42.0000000 42 40450000 42.00000000 42 40450000 42.000000000 42 40450000 42.0000000000 42 40450000 42.00000000000 42 40450000 42.000000000000 42 40450000 42.0000000000000 42 40450000 42.00000000000000 42 40450000 42.000000000000000 42 40450000 42.0000000000000000 42 40450000 42.00000000000000000 42 40450000 42.000000000000000000 42 40450000 42.0000000000000000000 42 40450000 42.00000000000000000000 42 40450000 42.000000000000000000000 41.999999999999993 BFBB000F 42.0000000000000000000000 42 40450000 42.00000000000000000000000 42.000000000000007 40450000 42.000000000000000000000000 42 40450000 42.0000000000000000000000000 42 40450000 42.00000000000000000000000000 42 40450000 42.000000000000000000000000000 42 40450000 Funny consequences: 2 3
Tried this under .NET 4.5.2.
In
Decimal.cs
, we can see thatGetHashCode()
is implemented as native code. Furthermore, we can see that the cast todouble
is implemented as a call toToDouble()
, which in turn is implemented as native code. So from there, we can't see a logical explanation for the behaviour.In the old Shared Source CLI, we can find old implementations of these methods that hopefully sheds some light, if they haven't changed too much. We can find in comdecimal.cpp:
and
We can see that the the
GetHashCode()
implementation is based on the conversion todouble
: the hash code is based on the bytes that result after a conversion todouble
. It is based on the assumption that equaldecimal
values convert to equaldouble
values.So let's test the
VarR8FromDec
system call outside of .NET:In Delphi (I'm actually using FreePascal), here's a short program to call the system functions directly to test their behaviour:
Note that since Delphi and FreePascal have no language support for any floating-point decimal type, I'm calling system functions to perform the calculations. I'm setting
FortyTwo
first to42
. I then add1
and subtract1
. I then add0.1
and subtract0.1
. Et cetera. This causes the precision of the decimal to be extended the same way in .NET.And here's (part of) the output:
Thus showing that this is indeed a long-standing problem in Windows that merely happens to be exposed by .NET. It's system functions that are giving different results for equal decimal values, and either they should be fixed, or .NET should be changed to not use defective functions.
Now, in the new .NET Core, we can see in its decimal.cpp code to work around the problem:
This appears to be implemented in the current .NET Framework too, based on the fact that one of the wrong
double
values does give the same hash code, but it's not enough to completely fix the problem.As for the difference in hashes it indeed seems to be wrong (same value, different hash) -> but it is answered already by LukeH in his comment.
As for the casting to double, though.. I see it that way:
42000000000000000000000
has different (and less 'precise') binary representation than420000000000000000000000
and therefore you pay higher price for trying to round it.Why it matters? Apparently decimal keeps track of its 'precision'. So for example it is storing 1m as
1*10^0
but its equivalent 1.000m as1000*10^-3
. Most likely to be able to print it later as"1.000"
. Therefore when converting your decimal to double it's not 42 that you need to represent, but for example 420000000000000000 and this is far from optimal (mantissa and exponent are converted separately).According to a simulator I have found (js one for Java, so not exactly what we may have for C# and therefore a bit different results, but meaningful):
As you can see the value for 4.2E19 is less precise than for 4.2E20 and may end up being rounded to 4.19. If this is how the conversion to double happens then the result is not surprising. And since multiplying by 10, you'll usually encounter a number that is non-well-represented in binary, then we should expect such issues often.
Now to my mind its all the price for keeping trace of significant digits in decimal. If it was not important, we could always ex. normalize
4200*10^-2
to4.2*10^1
(as double does it) and conversion to double wouldn't be that error-prone in context of hashcodes. If it's worth it? Not me to judge.BTW: those 2 links provide nice reading about decimals binary representation: https://msdn.microsoft.com/en-us/library/system.decimal.getbits.aspx
https://msdn.microsoft.com/en-us/library/system.decimal.aspx