-6

I was just wondering why

cout << (1000 < 1000) << endl;

gives 0, while

cout << (1000 < pow(10, 3)) << endl;

gives 1.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
gaurab
  • 17
  • 1

1 Answers1

1

std::pow does not work on integral types. If you supply it integers, it will cast them to double (see 7th overload on the linked page).
The result of the call is then a double, which happens to be slightly biased up (as floating-point arithmetic is lossy), enough to compare strictly larger with a literal value of 1000 directly converted to double.
See What Every Computer Scientist Should Know About Floating-Point Arithmetic.

Quentin
  • 62,093
  • 7
  • 131
  • 191
  • Doubles (and floats for that matter) can exactly represent the numbers 10, 3 and 1000, so that isn't the issue. I suspect there is a difference in the implementation of `pow` between libraries. – Qantas 94 Heavy Jan 14 '15 at 11:06
  • @Qantas94Heavy Indeed the issue is not representing the result, but calculating it. A converted literal does not have that problem, hence the difference. – Quentin Jan 14 '15 at 12:13