Is it safe to compare decimals casted from doubles

Viewed 469

It is commonly known that it is not safe to compare double values with == operator. In this example it returns false:

double d1 = 0.11;
double d2 = 0.44 - 0.33;
Console.WriteLine(d1 == d2);

But, if we cast values to decimal, it returns true:

double d1 = 0.11;
double d2 = 0.44 - 0.33;
Console.WriteLine((decimal)d1 == (decimal)d2);

Is it always safe to compare decimals casted from double, or there are some cases where can give unexpected result?

Update: Hossein's example is good, it shows that it can be wrongly true. But I'm more interested to see if there are opposite examples, when we expect decimal values to be equal and they are not. Even wider, what exactly happens when we cast from double to decimal.

5 Answers
Related