Quote:
Originally Posted by WesleyC
The code goes something like this:
output = (input * input / 127) * (input > 0) ? 1 : -1;
|
This is a great piece of math, but when I tried it, it didn't work...
It only served to reinforce my motto "when in doubt, add parenthesis".
It appears that the compiler gives greater precedence to * than it does to ?
So what the code was doing was this:
output = ((input * input / 127) * (input > 0)) ? 1 : -1;
not this:
output = (input * input / 127) * ((input > 0) ? 1 : -1);
As written, the code only returned +/- 1. With parenthesis added as in the second example, the code did what was intended.
The other cool by-product of this code is that it adds in a small dead-band to the joystick. Any offset less than 13 returns a zero after interger math truncation.
__________________
Phil Malone
Garrett Engineering And Robotics Society (GEARS) founder.
http://www.GEARSinc.org
FRC1629 Mentor, FTC2818 Coach, FTC4240 Mentor, FLL NeXTGEN Mentor