1
float a = 0.95f;
float b = 5.05f;
float ab = (a + b);
bool isTrue = (ab == 6.0f);
bool isFalse = ((a + b) == 6.0f);
bool isTrueAgain = ((float)(a + b) == 6.0f);

Why isTrue is true?

Why isFalse is false?

enter image description here

Stefan
  • 17,448
  • 11
  • 60
  • 79
JoJ
  • 133
  • 1
  • 7
  • 3
    Funny addition: `ab == (a + b)` equals `false` – Stefan Aug 17 '18 at 13:00
  • 2
    These may be of interest - http://blog.paranoidcoding.com/2014/12/22/redundant-cast.html https://www.ecma-international.org/publications/files/ECMA-ST/ECMA-335.pdf (12.1.3) – mjwills Aug 17 '18 at 13:10
  • In summary - `(a + b)` doesn't have to be a `float` on the last line (according to the C# spec) - so its value can be different to that shown on the third line. See https://dotnetfiddle.net/spRgWV . – mjwills Aug 17 '18 at 13:12

0 Answers0