How to compare values that are floats when using FloatMath?
ElectricAye
Posts: 4,561
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)...
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,
Mark
*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!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Watching the world pass me by, one photon at a time.
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,
Mark
*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!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Watching the world pass me by, one photon at a time.
Comments
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.
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.
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.
thanks,
Mark