Take the 2-minute tour ×

I'm programming a game requiring evaluation of the function y = -x^2+49. When I type the following to my TI-84, I have to type it -(x)^2 + 49 or -x^2 + 49, and NOT (-x)^2 + 49. And here's how I implemented it in C++

pp[i].death = -(j)^2 + 49;  // Upside down hyperbola

I put a breakpoint to track the value, and it just doesn't makes sense. My j is in the range [-7, +7] so it should return results in the range [0, 49]. But the C++ code gives me 52 or some random number that's way off. Answers would be appreciated.

share|improve this question
add comment

1 Answer

up vote 10 down vote accepted

^ is the bitwise xor operator, not exponentiation.

Use std::pow instead.

share|improve this answer
Which also means he is using integers which is most likely wrong as well. –  Axel Gneiting May 21 '12 at 5:40
wow, I'm dunbfounded. Thank you –  Joey Arnold Andres May 21 '12 at 5:47
I can accomodate that with static_cast<int>(-pow(j,2)). not that dumb lol, I just need some serious reference poster. –  Joey Arnold Andres May 21 '12 at 5:50
Better yet, just use -j*j + 49. pow takes a lot longer. –  phkahler May 21 '12 at 13:30
@phkahler Yeah definitely, although ideally you'd want to use an integer exponent function. –  Pubby May 21 '12 at 13:35
add comment

Your Answer


