ELSEIF's, Square Roots, and Floating Points, OH MY!!
mhamen3
Posts: 69
I was wondering if anyone could offer me some code examples of how to use a few things.
1 ) I'm comfortable using IF statements but I'm not too sure about the ELSEIF's. Would it be like this?
IF x > 10
SERVO.set(blah blah blah)
ElSEIF
SERVO.set(yadda yadda)
Or is the ELSEIF supposed to be indented under the IF?
2) How do I use the square root "^^" operator? I've been doing it like this: ^^(x*x+y*y)
3) Is math in SPIN strictly integer math by default? Could someone pass me a code example using a floating point object?
Thanks in advance for any help guys!!
-Marc
1 ) I'm comfortable using IF statements but I'm not too sure about the ELSEIF's. Would it be like this?
IF x > 10
SERVO.set(blah blah blah)
ElSEIF
SERVO.set(yadda yadda)
Or is the ELSEIF supposed to be indented under the IF?
2) How do I use the square root "^^" operator? I've been doing it like this: ^^(x*x+y*y)
3) Is math in SPIN strictly integer math by default? Could someone pass me a code example using a floating point object?
Thanks in advance for any help guys!!
-Marc
Comments
Like this: Each ELSEIF gets a condition. And the ELSE at the end catches all other things that did not meet any of the conditions. But if you get more than one ELSEIF, it is faster and uses less memory to use a CASE instead.
And yes, I think that works as you would expect, as long as you are expecting signed integer results all 'round.
Yes.
Example (using Float32): This converts rtime to a floating point value, then does this math: (ln(rtime) * -379.223) + 4009.74 and returns an integer result into temp.
Note that float "constants" even if they are integers must have a decimal point. So if you are doing float math, and you want to add var to 12. You would do this: FLT.fadd(var, 12.0).
And yes, I think that works as you would expect, as long as you are expecting signed integer results all 'round.
What do you mean by a signed result? I've looked around but haven't found anything about what that means.
Please excuse my ignorance, I'm new.
This can all be circumvented by using float math. But float math is way slower and you only get 23-bits of precision (which is enough for most peoples' applications).