Go Down

### Topic: Wierd math operations, am I just sleepy? (Read 1 time)previous topic - next topic

#### knuckles904

##### Aug 07, 2009, 09:30 am
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:
Code: [Select]
`long yaw, pitch, roll;  //three axesint joy_x_axis, joy_y_axis, accel_x_axis, accel_y_axis, accel_z_axis;//in the looppitch=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
Code: [Select]
`733      532      630      13001            -27971            -3260736      533      633      17408            -18710            -928736      533      633      17408            -18710            -928733      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.

#### AWOL

#1
##### Aug 07, 2009, 09:36 amLast Edit: Aug 07, 2009, 09:39 am by AWOL Reason: 1
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
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### knuckles904

#2
##### Aug 07, 2009, 09:18 pm
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

Go Up