I’m about to create a dictionary where each value has a numeric key, obtained by an experimental calculation.
I know that double and any other floating-point type is not well suited to be used as a key, since it’s hard to compare two floating-point numbers regarding uniqueness or equality.
Does anyone knows if Decimal is a good candidate with this respect? The alternative would be to convert the double to string with a given precision, but that sounds to me like an inelegant workaround.
There is not much difference between using
floatordecimalas key in dictionary. Both represent number with exponent – so both suffer from the same comparison issue just in different scale.Either would be ok for Dictionary if you need bit-wise equality, if you need to keys for “about the same value” you need to make it custom key which would round values in some known way to put similar results in the same bucket…