 # Wierd math operations, am I just sleepy?

Alright so this problem has been bugging me for most of the night tho I just realized what exactly it was. Basically, multiplying a number by itself or any other number is not working. Take a look at the output Im getting
Code:

``````long yaw, pitch, roll;  //three axes
int joy_x_axis, joy_y_axis, accel_x_axis, accel_y_axis, accel_z_axis;
//in the loop
pitch=sq(accel_x_axis);
roll=sq(accel_x_axis)+sq(accel_y_axis)+sq(accel_z_axis);
yaw=accel_x_axis*accel_y_axis;
Serial<<accel_x_axis<<"\t"<<accel_y_axis<<"\t"<<accel_z_axis<<"\t";
Serial<<pitch<<"\t\t"<<roll<<"\t\t"<<yaw<<endl;
``````

and Im getting the output

``````733      532      630      13001            -27971            -3260
736      533      633      17408            -18710            -928
736      533      633      17408            -18710            -928
733      532      632      13001            -25447            -3260
``````

if you take a calculator you will see that 733^2=537289, 733^2+532^2+632^2=1219737, and 733*532=389956.
Why isnt anything coming out right? Nothing should have floating point problems. Im sure this is something Ill hit myself in the morning but for now its driving me mad. Please help!

Yes this is all from a nunchuck example.

What is the prototype for "sq"?

You're squaring "int"s and summing; if the result is an "int" (-32768..32768), then the wrapping you're seeing is entirely consistent.

Promote the "int"s to "long"s before squaring and summing.

1219737 = 0x129c99, truncated to 16 bits is 0x9c99 = -25447

thanks that did it. for some reason I thought that only the variable holding the end result of the math needed to be big enough, not every variable being multiplied. i think im getting rusty