Shop OBEX P1 Docs P2 Docs Learn Events
Floating point compare — Parallax Forums

Floating point compare

Chuck RiceChuck Rice Posts: 210
edited 2008-04-09 23:33 in Propeller 1
.

is this syntax valid?


if (fp.sin (fp.fsub(Lon2 , Lon1)) < 0.0)



In other words, can I compare against a floating point constant?

.

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-04-09 20:40
    It will work but only by chance, spin does not provide any support for floating point numbers except constructing constants. IOW floating point comparisons are not supported. But if you disolve what you are doing into the corresponding bit manipulations of IEEE single floating point, 0.0 is equal to $0000_0000, so 0.0 = 0 this is called a congruent representation. x < 0.0 is a negative value test, it turns out that the msb of ieee floating point is the sign bit. This also happens to be the bit in integer math which is set when the value is negative, so the comparison in integer math done within spin happens to produce the same result as a true floating point comparison for this example, but trying to use this technique will not work for anything besides > 0.0,· < 0.0 or == 0.0

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • Chuck RiceChuck Rice Posts: 210
    edited 2008-04-09 20:43
    .

    So what is the correct way? I searched Using Parallax Propeller™ Floating Point Routines for compare, but got no hits.

    Would it be:

    if (fp.sin (fp.fsub(Lon2 , Lon1)) < constant(0.0))
    

    Post Edited (Chuck Rice) : 4/9/2008 8:49:35 PM GMT
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-04-09 21:03
    Your original statement was fine, the compiler knows to convert a floating point constant·into the ieee representation. As far as library support, I don't know I've never found the need to use floating point in my projects. But equality testing is trivial and can actually be done in integer math using a converted floating point number as the comparator, so all that would be need is a if_above and/or if_below routine which diasects the sign, exponent and mantissa from the numbers and compare it in an iterative approach (ie if signs differ, value with bit set is less than value with bit clear; if the sign is the same then compare the exponent the one with the larger exponent is greater; if exponents are the same the one with the larger mantissa is greater)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.

    Post Edited (Paul Baker (Parallax)) : 4/9/2008 9:12:37 PM GMT
  • John AbshierJohn Abshier Posts: 1,116
    edited 2008-04-09 22:28
    You probably want to use FCmp(NumberA, NumberB)
    returns 1 if NumberA > Number B, -1 if NumberA < NumberB, 0 if equal
  • Chuck RiceChuck Rice Posts: 210
    edited 2008-04-09 23:33
    John Abshier said...
    You probably want to use FCmp(NumberA, NumberB)
    returns 1 if NumberA > Number B, -1 if NumberA < NumberB, 0 if equal

    Doh! I sure missed that one. Thanks!
Sign In or Register to comment.