Shop OBEX P1 Docs P2 Docs Learn Events
How to compare values that are floats when using FloatMath? — Parallax Forums

How to compare values that are floats when using FloatMath?

ElectricAyeElectricAye Posts: 4,561
edited 2009-06-02 15:43 in Propeller 1
Hi all you geniuses out there,

I would like to compare two floating point numbers. As I understand it, the Propeller only works with integers, so I suspect that things like "<" and "=>" will not work if I use them in, say, an IF statement. I see that the Float32 has a compare function Fcmp*. But how should we compare numbers using plain ole FloatMath??? Is there an easy way to do it or are we condemned to create racks of IF statements using the Fsub function and subtract the two floats and see if the residuals are greater than zero point zero? In other words, do we do this.... (assuming A and B are both floats)...


  IF (f.Fsub(A,B)) > 0.0
         'Pull out your hair.

  IF (f.Fsub(A,B)) < 0.0
         'Pull out the hair of somebody else.

    IF (f.Fsub(A,B)) == 0.0
         'Wish you had hair to pull.





The problem I see with the above code is that I'm still using the ">" operator, which I suppose only works on integers, and 0.0 is a float.

Is there some trick to getting around this? Or is my conceptualization of this so outside of reality that it's not even wrong?

thanks,
Markeyes.gif




*At least I presume that is what it is. The actual Float32 object, like so many other objects in the OBEX, is so insanely stingy with comments that it doesn't even bother to tell what Fcmp is! mad.gif

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Watching the world pass me by, one photon at a time.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-06-02 15:07
    The Fcmp function subtracts two floating point numbers and returns an integer result that's less than zero if "<", greater than zero if ">" and equal to zero if "==". You would write "IF f.Fcmp(a,b) == 0" to test for A == B, "IF f.Fcmp(a,b) < 0" to test for A < B, etc.

    I think you could also use Fsub and compare to an integer 0 since the integer sign bit is used for the floating point sign and a floating point zero is the same as an integer zero.
  • ElectricAyeElectricAye Posts: 4,561
    edited 2009-06-02 15:16
    Thanks, Mike,
    that's good to know for the Float32 object. But is there a similar function in the FloatMath object? or some trick I can employ with Floatmath? Or must I use Float32? I'm reluctant to try Float32 because I know it hogs up at least one cog.

    thanks,
    Mark

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Watching the world pass me by, one photon at a time.
  • Mike GreenMike Green Posts: 23,101
    edited 2009-06-02 15:33
    Read one of the descriptions of the IEEE Floating Point Standard (babbage.cs.qc.cuny.edu/IEEE-754/References.xhtml). Like I said, the integer sign bit is the same as the floating point sign bit, so a negative floating point number looks like a negative integer and a positive non-zero floating point number looks like a positive non-zero integer. A floating point zero is the same as an integer zero. You can use Fsub and do an integer comparison with zero like "IF F.Fsub(A,B) == 0". This works the same for FloatMath as for Float32 or Float32Full.
  • ElectricAyeElectricAye Posts: 4,561
    edited 2009-06-02 15:43
    Mike Green said...
    ...Like I said, the integer sign bit is the same as the floating point sign bit, so a negative floating point number looks like a negative integer and a positive non-zero floating point number looks like a positive non-zero integer. A floating point zero is the same as an integer zero....

    Oooooh, now I get it. And I confess, I looked over the IEEE thing on wikipedia because I just knew somebody would direct me there.... but there's just no way in the world I would have figured out that on my own. I'm sure glad I addressed my post to "all you geniuses out there". That would be you, Mike. smile.gif

    thanks,
    Mark

    smile.gifsmile.gifsmile.gifsmile.gifsmile.gif
Sign In or Register to comment.