I need to do a little bit of math in some code I'm writing.

I simply need to use pythagorem theorem to solve for the magnitude of a side with two other known sides.

x = sqrt( sq(y) + sq(z) );

The largest values I anticipate seeing for my variables of y and z are around 150.

So I wrote some code to test out which variable types I need to use to properly hold my values.

void setup() {

Serial.begin(9600);

}

void loop(){

long x = 0;

x = sqrt(sq(150) + sq(150));

Serial.println(x);

}

In the serial monitor I was getting strange results, not the correct answer.

I then tried to elimnate the square root function to see if I would get the right answer before taking the square root:

void setup() {

Serial.begin(9600);

}

void loop(){

long x = 0;

x = sq(150) + sq(150);

Serial.println(x);

}

This was still giving me strange results. I guess it is some kind of data overflow? I thought a long variable type was supposed to hold up to the value of 2,147,483,647 I'm not even close to that. I should be getting an answer of 45000. If I only do one sq(15) and not the other, I get the correct answer of 22500. Why won't it add them together properly?

Thanks guys!