Completely ridiculous bugs
by math_explorer, Dec 6, 2012, 10:07 AM
Guess why this doesn't compile (on my machine with an oldish but still pretty normal g++ 4.2.1).
Click to reveal hidden text
#include <iostream> #include <cmath> using namespace std; int x1, x2, y1, y2; int main(){ cin >> x1 >> y1 >> x2 >> y2; int xd = x2 - x1; int yd = y2 - y1; double dist = sqrt(xd*xd + yd*yd); cout << dist; }
Click to reveal hidden text
Here are some things it's not:
- "#include <cmath>" plus "using namespace std;" is essentially equivalent to "#include <math.h>". Yes, the compiler manages to find "sqrt".
- Number coercion (the int-to-double lurking in the computational line) works fine.
- Using <iostream> and <cmath> don't conflict or anything.
- No, it's not about ending with a "return 0;"
I don't know why I'm doing this; it feels like such a natural possible mistake that maybe the C++ programmers who will read this already know.
Ready for the answer?
- "#include <cmath>" plus "using namespace std;" is essentially equivalent to "#include <math.h>". Yes, the compiler manages to find "sqrt".
- Number coercion (the int-to-double lurking in the computational line) works fine.
- Using <iostream> and <cmath> don't conflict or anything.
- No, it's not about ending with a "return 0;"
I don't know why I'm doing this; it feels like such a natural possible mistake that maybe the C++ programmers who will read this already know.
Ready for the answer?