Comparing hard coded 0 to a possibly negative SINTEGER

mpullinmpullin Obvious Troll Account, Marked for DeletionPosts: 949
This is a function that wasn't working... it was returning 100 far too much of the time
DEFINE_FUNCTION INTEGER VOL_CONV_DB_NORMAL(SINTEGER nVAL){
    INTEGER nRESULT;
    if(nVAL < -500) return 0;
    if(nVAL > 0) return 100; // returns 100 when nVAL == -335

    // other stuff here doesn't get reached
}
This is the same function fixed
DEFINE_FUNCTION INTEGER VOL_CONV_DB_NORMAL(SINTEGER nVAL){
    INTEGER nRESULT;
    if(nVAL < -500) return 0;
    if(nVAL > -0) return 100; // does NOT return 100 when nVAL == -335

    // other stuff here gets reached
}
Notice the difference? It's tiny.

You would expect a comparison (-335 > 0) to come back false. And it would, if the data types were the same. Whenever you write a number hard in code NetLinx is going to treat it like a LONG, or an SLONG if it's negative. The parameter nVAL is an SINTEGER. For some reason, when it compares nVAL (containing -335) to the LONG value 0, it compares the SINTEGER -335 (binary 1000000101001111) to the LONG 0 (binary 00000000000000000000000000000000) and decides that -335 is bigger. But when comparing the SINTEGER -335 with the SLONG -0 (binary 10000000000000000000000000000000) it decides -0 is bigger?

That's what I think is going on at least. Comparing to -0 yielding a different result than comparing to 0 is silly, but if you think about what is probably going on with data type conversion of hard coded numbers, it kind of makes sense.

Something to keep in mind when dealing with negative numbers / signed & unsigned data types. If anyone here has a more accurate explanation of what's happening that would be great.

Comments

  • mushmush I programme in the dark! Posts: 285
    G'day Matt,

    I could not replicate your results.
    I used your function in the following way;
    DEFINE_VARIABLE
    volatile sinteger snTemp = -550
    volatile integer nTemp
    
    TIMELINE_EVENT [tlTest]
    {
    nTemp = VOL_CONV_DB_NORMAL(snTemp)
    send_string 0,"'snTemp=',itoa(snTemp),' nTemp=',itoa(nTemp)"
    snTemp ++
    }
    

    The results were as expected all the way through.
    (0000166196) snTemp=-336 nTemp=0
    (0000166294) snTemp=-335 nTemp=0
    (0000166386) snTemp=-334 nTemp=0
    
    (0000199978) snTemp=-1 nTemp=0
    (0000200082) snTemp=0 nTemp=0
    (0000200178) snTemp=1 nTemp=100
    (0000200277) snTemp=2 nTemp=100
    
    
    Are you using old firmware? (Straw clutching I know, but I can't think of anything else).
    Any other ideas?

    Cheers

    Mush
  • a_riot42a_riot42 AMX Wizard Posts: 1,619
    mpullin wrote: »
    But when comparing the SINTEGER -335 with the SLONG -0 (binary 10000000000000000000000000000000) it decides -0 is bigger?

    if (0 > -0)
    send_string 0, "'0 is greater than -0'"
    else if (0 < -0)
    send_string 0, "'-0 is greater than 0'"
    else if (0 == -0)
    send_string 0, "'-0 is equal to 0'"
    else
    send_string 0, "'If you see this then the laws of physics are broken'"

    What does this print out?
    Paul
  • mushmush I programme in the dark! Posts: 285
    a_riot42 wrote: »
    What does this print out?

    (0000278276) -0 is equal to 0
  • a_riot42a_riot42 AMX Wizard Posts: 1,619
    mush wrote: »
    (0000278276) -0 is equal to 0

    Thank goodness. I can now sleep soundly.
    Paul
  • mushmush I programme in the dark! Posts: 285
    a_riot42 wrote: »
    Thank goodness. I can now sleep soundly.
    Paul

    Does that mean you want me to run the same test with Zeds?
  • Jorde_VJorde_V UX Scientist Posts: 393
    if(slongzero < sintegerminthreefiftyfive){
        send_string 0,"'uh-oh'"
    }
    

    Luckily it doesn't uh-oh us on that. A singed long holding the value 0 > a singed integer holding the value -355.

    Checking if a long 0 returns uh-oh

    Edit:

    All is well on my 2100 here.

    using this:
    sinteger minus = -355
    long zero = 0
    
    if(zero > minus){
        send_string 0,"'All is well'"
    } else {
        send_string 0,"'Crap you broke it!'"
    }
    
    Line 131281 (13:45:28):: All is well
    
    Is what I get.
  • mpullinmpullin Obvious Troll Account, Marked for Deletion Posts: 949
    mush wrote: »
    I could not replicate your results.
    Oh. :(

    Well I'm still using NS2, if that has to do with anything...
  • mushmush I programme in the dark! Posts: 285
    mpullin wrote: »
    Well I'm still using NS2, if that has to do with anything...

    I wouldn't think so, I expect the firmware to be the more likely culprit but then again only an AMX boffin would really know.
  • Jorde_VJorde_V UX Scientist Posts: 393
    mush wrote: »
    I wouldn't think so, I expect the firmware to be the more likely culprit but then again only an AMX boffin would really know.

    Most likely, I'm running the latest available firmware: 3.50.430
  • Spire_JeffSpire_Jeff Formerly Caffeinated Programmer Posts: 1,917
    Since the original post is over a year old, and there doesn't seem to be any issue, I will indulge myself and add the following:

    I wonder if the processor can convert between languages? Anyone want to test the following?
    define_constant
    sinteger nil = 0;
    integer love = 0;
    long zero = 0;
    char off = 0;
    
    if(love == nil or love == zero or love == off)
       send_string 0,"'PLAY: All You Need is Love'"
    else
       send_string 0,"'Enjoy Utopia!'"
    

    :)

    Jeff
  • Jorde_VJorde_V UX Scientist Posts: 393
    Spire_Jeff wrote: »
    Since the original post is over a year old, and there doesn't seem to be any issue, I will indulge myself and add the following:

    I wonder if the processor can convert between languages? Anyone want to test the following?
    define_constant
    sinteger nil = 0;
    integer love = 0;
    long zero = 0;
    char off = 0;
    
    if(love == nil or love == zero or love == off)
       send_string 0,"'PLAY: All You Need is Love'"
    else
       send_string 0,"'Enjoy Utopia!'"
    

    :)

    Jeff

    changed to or to &&, reply: all you need is love :) (oh yeah and your off to of, as off is a reserved word)
Sign In or Register to comment.