2

I am having trouble with the syntax for computing distance formula using math and its sqrt function. Here is the equation. I tested it out and the answers are integers.

sqrt(double ((x1 - 0)^2) + double ((x2 - 0)^2))
dustinyourface
  • 313
  • 1
  • 3
  • 9
  • 5
    Hint: the ^ operator does not do what you think it does. – Borgleader Jan 14 '16 at 02:07
  • Possible duplicate of [Why is my power operator (^) not working?](http://stackoverflow.com/questions/4843304/why-is-my-power-operator-not-working) – phuclv Jan 14 '16 at 02:41
  • it's for C, but similar for C++ and all other C-based languages like Java, Javascript... Google for C++ operator first before asking here – phuclv Jan 14 '16 at 02:42

1 Answers1

5

In C/C++, ^ is the operator for bitwise exclusive-or (xor). I assume what you are looking for is way to raise a number to the power of 2. For this you can use the pow function from the C standard library:

pow(double n, double exp);

Specifically,

sqrt(double (pow(x1 - 0, 2)) + double (pow(x2 - 0, 2))
Levi
  • 1,921
  • 1
  • 14
  • 18