0

I recently stumbled upon this article:

http://www.codeproject.com/Articles/383871/Demystify-Csharp-floating-point-equality-and-relat

that explains why comparison between (theoretically) identical doubles could lead to unexpected results. I wonder if using double.Equals() circumvent this issue or still a workaround like the one proposed in the article is needed.

I usually compare values like

if(x==123d)

but I understand this must be avoided; I'm unsure if even

if(x.Equals(123d))

is unsafe too.

ccalboni
  • 12,120
  • 5
  • 30
  • 38
  • Same issue: http://referencesource.microsoft.com/#mscorlib/system/double.cs,bceeea32c8ba20a7,references Use decimal or something like: `var diff = Math.Abs(x - 123d); if(diff < 0.00000001)...` – Tim Schmelter Dec 16 '15 at 13:20
  • 2
    Those are the same. You have to roll your own code to do an epsilon comparison. – juharr Dec 16 '15 at 13:20

0 Answers0