Shop OBEX P1 Docs P2 Docs Learn Events
Spin If - Then Question — Parallax Forums

Spin If - Then Question

ajwardajward Posts: 1,130
edited 2014-07-13 21:41 in Propeller 1
Hi All...

I'm working with an accelerometer on my Activity Board and am running to a bit of a roadblock. Can the IF statement resolve a condition with fractional numbers? That is will: If "value a" < 0.019 Then "do something" work?

I'm converting the ± values from the accelerometer to an absolute value with the || operator and trying to branch to a subroutine using the IF statement. If I twiddle the code to pass a whole number the code works as expected. If I pass the fractional number, the subroutine isn't accessed.

I can't see anything in the Prop. manual to suggest a limitation, but maybe I missed something. I can post the whole program, but being somewhat lazy, I thought I'd ask about the IF statement first.

Thanks for any wisdom!!!!!!!

Amanda

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-07-08 13:44
    Amanda,

    Is this in Spin or C?

    In Spin, you can't compare against a floating point number like that. You might be able to in C.
  • Dave HeinDave Hein Posts: 6,347
    edited 2014-07-08 14:48
    Floating point numbers are represented as sign:exponent:mantissa. It is possible to determine if a floating point number is less than 0.019 by doing "if X < 0.019". In this case, Spin is actually comparing the binary representation of the floating point number in X with the binary representation of 0.019. This will work as long as X is positive, and is using the floating point representation.

    You cannot use the || operator on a floating point number. It only works on fixed point numbers. However, you can get the absolute value of a floating point number by zeroing out the MSB. You can do this by ANDing with $7FFF_FFFF.

    Are you sure that you're getting a floating point number from the accelerometer driver? If so, you're code should look something like this:
      X := GetAccValue
      X &= $7FFF_FFFF ' Get absolute value
      if X < 0.019
    
    If you're getting a fixed point value from the driver you will need to determine the fixed point value that is equivalent to 0.019, and use that in your comparison instead. You can also use the || operator with fixed point values.
  • ajwardajward Posts: 1,130
    edited 2014-07-11 00:27
    Thanks guys! I finally have a couple of days off work and I'll look back into this. Barring the appearance of another shiny thing! :-)

    Amanda

    Edit: Followup... The accelerometer outputs a whole number that's manipulated to represent the the G-forces acting on the module. I can use the IF statement on that value to branch to the appropriate subroutine. It works pretty well, tho' some tweaking is needed.
    I built the thing to detect unusual vibrations. I get an occasional Z-axis alert when some nearby resident in the building slams a door or goes tromping by in the hallway. :-)
  • NWCCTVNWCCTV Posts: 3,629
    edited 2014-07-13 20:59
    Is this in Spin or C?
    Duane, I think it is time for a vacation. The title says it all!!!
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-07-13 21:41
    NWCCTV wrote: »
    Duane, I think it is time for a vacation. The title says it all!!!

    I'm off to Hawaii.

    Amanda, I'm not sure if your familiar with using floating point numbers on the Prop. Depending on your application, you may not need floating point numbers, you can just used scaled integers. If you do need floating point, then your math all needs to be done with a floating point object like F32. F32 and similar objects have a "Cmp" (IIRC) method which will let you compare floats.
    PUB FCmp(a, b){{
      Floating point comparison.
      Parameters:
        a        32-bit floating point value
        b        32-bit floating point value
      Returns:   32-bit integer value
                 -1 if a < b
                  0 if a == b
                  1 if a > b
    }}
    

    You can see in the above comments, the FCmp method will let you now if the pair of floats are equal or which one is larger.
Sign In or Register to comment.