I have two questions regarding .NET's decimal
data type determinism:
Are
decimal
type computations cross-platform deterministic? Or in other words, will math operations ondecimal
type produce exactly the same results on all platforms?Is casting
decimal
number tofloat
ordouble
cross-platform deterministic (casts will always produce exactly the same result)?
double
in relation to trailing zeroes of thedecimal
. It is possible to have twodecimal
values that only differ by the number of trailing zeroes, and therefore these twodecimal
s are considered equal (even if they have distinct internal representations), such that when you convert them todouble
, the resultingdouble
values are distinct. Addition: This also proves that the conversion todouble
does not always pick the nearest destination. Want example? – Jalisajaliscovar a = ((double)200.000000000000000000000M).ToString("R"); var b = ((double)200.0000000000000000000000M).ToString("R"); var c = ((double)200.00000000000000000000000M).ToString("R");
. Taken from late answer here. Because theSystem.Decimal
implementation ofGetHashCode()
works by first converting todouble
and then truncating thatdouble
a bit, the decimal involved ina
here even has the wrongdecimal.GetHashCode()
. The other two have the right hash code. – Jalisajalisco