Quote:
Originally Posted by PhilBot
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);
|
Operator precedence for Multiplicative and Additive instructions is relatively high, whereas the ternary operator is quite low, however the '?' forms a 'sequence point' and considers anything to the left, within the sequence, to be used as the scalar value.
Parenthesis is, of course, the correct way to create the desired association, and when using the ternary operator is a good habit to adopt.