Shop OBEX P1 Docs P2 Docs Learn Events
Floating point ( Greater than and Less than ) — Parallax Forums

Floating point ( Greater than and Less than )

grasshoppergrasshopper Posts: 438
edited 2008-11-19 22:30 in Propeller 1
I am always finding my self having to compare a floating point number with another floating point number to see if one is greater or less than the other. It seems that there has to be a better way of doing this than what I am currently doing.

I basically take both numbers and convert them into a non-floating number only to do the < > then converting them back to floating point. I ask if there is an object that has this built in feature? Perhaps I over looked something.

Thanks in advance.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-11-18 15:02
    Subtract the two numbers and look at the sign of the result like:

    if flt.fsub(a,b) < 0 ' Is a < b?
  • John AbshierJohn Abshier Posts: 1,116
    edited 2008-11-18 15:17
    float32 and float32full has a routine FCmp(a,b) where a and b are floating point numbers. It returns an integer. -1 if a < b, 0 if a == b, and 1 if a > b
    Unfortunately it is not if floatmath if you are using that library.
    Mike, you need to elaborate on checking the sign of the result of subtration.· flt.fsub(a,b) < 0 will not work because flt.fsub(a,b) is a float and 0 is an integer and < only works· for integers.· One could check bit 31.

    John Abshier

    Post Edited (John Abshier) : 11/18/2008 3:22:26 PM GMT
  • BergamotBergamot Posts: 185
    edited 2008-11-18 18:56
    John Abshier said...
    float32 and float32full has a routine FCmp(a,b) where a and b are floating point numbers. It returns an integer. -1 if a < b, 0 if a == b, and 1 if a > b
    Unfortunately it is not if floatmath if you are using that library.

    Mike, you need to elaborate on checking the sign of the result of subtration. flt.fsub(a,b) < 0 will not work because flt.fsub(a,b) is a float and 0 is an integer and < only works for integers. One could check bit 31.


    John Abshier

    If I understand Mike properly, he's exploiting the fact that signed 32-bit integers and 32-bit IEEE 754 floats use the same bit to store sign.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-11-18 19:07
    Yes. A negative floating point value will also be a negative integer (whatever the internal format of the floating point number is). A positive floating point value will also be a positive integer and the floating point value zero happens (deliberately) to be the integer value zero.
  • JasonDorieJasonDorie Posts: 1,930
    edited 2008-11-19 22:30
    If you know that the numbers are valid floats, you can compare the integer values against each other (load the 32 bit IEEE float into an integer register directly, no conversion).

    If the float is positive, the high bit will be zero. If the float is negative, the high bit will be 1. The remaining bits represent the exponent and mantissa of the floating point number.

    If both high bits are zero, the larger integer is ALSO the larger floating point value. If the high bits are different, one is negative and one is positive, and the integer comparison is still valid.

    If both high bits are one, the larger integer is the smaller value (the lower 31 bits are the absolute value of the number). This is the only case requiring special handling.

    This code will not handle NANs or infinity correctly, but if your floats are known to be valid numbers it works fine. I've done list sorting on floats this way and it's significantly faster than using "real" float comparisons.

    Thinking about it, it's quite possible that the assembly code in the Float32 object implements it exactly this way.

    Jason
Sign In or Register to comment.