Shop OBEX P1 Docs P2 Docs Learn Events
ELSEIF's, Square Roots, and Floating Points, OH MY!! — Parallax Forums

ELSEIF's, Square Roots, and Floating Points, OH MY!!

mhamen3mhamen3 Posts: 69
edited 2011-07-27 08:25 in Propeller 1
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

Comments

  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2011-07-26 15:15
    mhamen3 wrote: »
    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)

    Like this:
    IF x > 10
       SERVO.set(blah blah blah)
    ElSEIF x > 5
       SERVO.set(yadda yadda)
    ElSEIF x > 1
       SERVO.set(yak yak)
    ELSE
       SERVO.set(poof)
    
    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.
    mhamen3 wrote: »
    2) How do I use the square root "^^" operator? I've been doing it like this: ^^(x*x+y*y)
    And yes, I think that works as you would expect, as long as you are expecting signed integer results all 'round.
    mhamen3 wrote: »
    3) Is math in SPIN strictly integer math by default? Could someone pass me a code example using a floating point object?
    Yes.
    Example (using Float32):
    temp := FLT.fround(FLT.fadd(FLT.fmul(-379.223, FLT.log(FLT.FFloat(rtime))), 4009.74))
    
    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).
  • mhamen3mhamen3 Posts: 69
    edited 2011-07-26 15:37
    Thanks, Bobb. Any idea about how to use the square root operator?
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2011-07-26 15:48
    mhamen3 wrote: »
    Thanks, Bobb. Any idea about how to use the square root operator?
    Yeah, I replied to that too, though it was a late edit...
    mhamen3 wrote: »
    2) How do I use the square root "^^" operator? I've been doing it like this: ^^(x*x+y*y)
    And yes, I think that works as you would expect, as long as you are expecting signed integer results all 'round.
    pythag := ^^(x*x+y*y)
    
  • mhamen3mhamen3 Posts: 69
    edited 2011-07-26 19:34
    Thanks again,

    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.
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-07-27 07:54
    Spin uses 32-bit signed variables for most of its operations. This is not an issue as long as everything stays within a 31-bit range (i.e., 0 to 2,147,483,647). In your square-root computation, if x happened to be 46,341, then x*x would be 2,147,488,281 ($80001219). Because the most significant bit is non-zero, this would be interpreted as a negative number instead of a positive number.
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2011-07-27 08:25
    And keep in mind, each operation needs to stay within that 31-bit limit. So the results of both x * x and y * y needs to be below 2,147,483,647, as well as the sum of those (x * x + y * y).

    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).
Sign In or Register to comment.