Comparing hard coded 0 to a possibly negative SINTEGER
mpullin
Obvious Troll Account, Marked for Deletion
This is a function that wasn't working... it was returning 100 far too much of the time
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.
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
-
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 -
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 -
-
-
Thank goodness. I can now sleep soundly.
Paul
Does that mean you want me to run the same test with Zeds? -
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. -
Oh.I could not replicate your results.
Well I'm still using NS2, if that has to do with anything... -
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. -
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 -
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 -
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)
Leave a Comment
Categories
- All Categories
- 2.5K AMX General Discussion
- 922 AMX Technical Discussion
- 514 AMX Hardware
- 502 AMX Control Products
- 3 AMX Video Distribution Products
- 9 AMX Networked AV (SVSI) Products
- AMX Workspace & Collaboration Products
- 3.4K AMX Software
- 151 AMX Resource Management Suite Software
- 386 AMX Design Tools
- 2.4K NetLinx Studio
- 135 Duet/Cafe Duet
- 248 NetLinx Modules & Duet Modules
- 57 AMX RPM Forum
- 228 MODPEDIA - The Public Repository of Modules for Everyone
- 943 AMX Specialty Forums
- 2.6K AMXForums Archive
- 2.6K AMXForums Archive Threads
- 1.5K AMX Hardware
- 432 AMX Applications and Solutions
- 249 Residential Forum
- 182 Tips and Tricks
- 146 AMX Website/Forums