1

below code is weird. when i debug it, i can see both "result" and "round" are 5, but output is "false". Any idea?

double result = Math.Log (243, 3);   // 5
double round = Math.Round (result);  // 5
Console.WriteLine (result == round);
jojo
  • 13,583
  • 35
  • 90
  • 123

1 Answers1

0

Comparison of floating point with equality operator could have loss of precision while rounding values. You can fix this by compare a difference with epsilon. Use a Tolerance like this:

Console.WriteLine(Math.Abs(result - round) < 0.0000001); // True
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109