Go Down

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


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: [Select]
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

and Im getting the output
Code: [Select]

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.


Aug 07, 2009, 09:36 am Last 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.


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