2
#include <iostream>
using namespace std;

int main() {
    long long a = 3400000000000000000;
    float b = 3400000000000000000;
    double c = 3400000015362424832;

    if (a == b && b == c && c != a) {
        cout << "true" << '\n';
    } else {
        cout << "false" << '\n';
    }
    return 0;
}

i don't know why that if-condition is true.... According to Syllogism, this condition is false. i think the reason is C++ operator principle... but i cant search this problem...

  • Welcome to SO. Please do not use various language tags but only the language you are using. Did you take into account the maximum precision of `float` data type? – Gerhardh Jun 26 '18 at 10:21
  • 3
    An interesting twist on a very old problem. Take a look at [/questions/588004/is-floating-point-math-broken](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) for answers. – r3mainer Jun 26 '18 at 10:22
  • Print the values of the variables. – molbdnilo Jun 26 '18 at 10:26
  • @molbdnilo print value is a = 3400000000000 b = 3400000077824.000000 c = 3400000077824.000000 i don't know why a == b – JaehyeonKim Jun 26 '18 at 10:28
  • To compare `a` and `b`, they have to first be converted to a common type - in this case `float`. I'm not the least surprised that `3400000000000000000` converted to `float` gives the same result for `a` and `b`. – Bo Persson Jun 26 '18 at 10:43
  • @김재현 Try to print `(float)a` and `(double)a`. – molbdnilo Jun 26 '18 at 10:46
  • According to this test program, https://ideone.com/m3qbfF, a and b actually result in the same (wrong) values when converted to float, so a== b yields true. – Rene Jun 26 '18 at 10:57

0 Answers0