0
bool func(double &u, double &v){
    .
    .
    .
    if((u+v) <= 1.0)
        return false;
    cout <<"u:  " << u << "  v:  " << v << endl;
    return true;
}

I have a function like this. Output is:

u: 0.914812  v:  0.0851877

Their sum is: 0.9999997. However the program does not go in the if statement. It returns true. Why?

user3817833
  • 97
  • 1
  • 9
  • could you send link of that question? – user3817833 May 02 '16 at 14:03
  • [Works for me](http://ideone.com/96oQ8D) – James Adkison May 02 '16 at 14:04
  • Values shown are rounded, there are probably several more digits which brings sum over 1 – Revolver_Ocelot May 02 '16 at 14:05
  • 1
    This question isn't a dup of the referenced question. Just look at the very top of that one. It's about the testing-for-equality fallacy. Then there's lots of discussion of the philosophy of floating point numbers. This question could be about precision of printing even with a numeric system which wasn't subject to the flakiness of floats. Suppose the question was "why is 1KB + 1 KB > 3KB?" and we discovered the questioner was dividing integers by 1024 for printing. – zeromus May 02 '16 at 14:22

1 Answers1

2

Consider 0.914812499999999 and 0.0851877499999999. Perhaps those numbers aren't what you think they are. Print them with more precision

zeromus
  • 1,648
  • 13
  • 14