Home AMX User Forum NetLinx Studio

Bug/Caveat? ITOA() and negative SINTEGER CONSTANT

What?s a little sign bit between friends?

Consider the following code:
DEFINE_DEVICE
dvTP	= 10001:1:0

DEFINE_CONSTANT
SINTEGER snNeg275Const = -275

DEFINE_VARIABLE
SINTEGER snNeg275Var	= -275
 
DEFINE_FUNCTION fnITOASignedConst (SINTEGER NegConstPassedIn) {

   SEND_STRING 0, "'ITOA(NegConstPassedIn) = ',ITOA(NegConstPassedIn)"   
}

DEFINE_EVENT

BUTTON_EVENT[dvTP,1] {

   PUSH: {
      SEND_STRING 0, "'ITOA(snNeg275Const) = ',ITOA(snNeg275Const)"
      SEND_STRING 0, "'ITOA(snNeg275Var) = ',ITOA(snNeg275Var)"
   }
}

BUTTON_EVENT[dvTP,2] {

   PUSH: {
      fnITOASignedConst(snNeg275Const)
   }
}

Output when button 1 is pushed:
Line 1 :: ITOA(snNeg275Const) = 65261 - 3:33:20
Line 2 :: ITOA(snNeg275Var) = -275 - 03:33:20

Output when button 2 is pushed:
Line 3 :: ITOA(NegConstPassedIn) = -275 - 03:33:23

ITOA() does not return negative numbers correctly when working on a SINTEGER CONSTANT as shown when button 1 is pushed.

However, if you pass the same SINTEGER CONSTANT into a function then ITOA() does return correctly as shown when button 2 is pushed.

Smells like a bug to me.

Reference:
ITOA
Converts a 32-bit signed integer to a decimal ASCII string. The syntax:

CHAR[ ] ITOA (LONG Num)

Parameters:

Num - a 32-bit signed integer to convert to a decimal string.

Result:

A character string that contains the decimal representation of the specified integer.

Example:

STRING = ITOA(501) // STRING = '501'

Comments

  • Now I know what a bug smells like.
    Joe Hebert wrote:
    Output when button 2 is pushed:
    Line 3 :: ITOA(NegConstPassedIn) = -275 - 03:33:23

    However, if you pass the same SINTEGER CONSTANT into a function then ITOA() does return correctly as shown when button 2 is pushed.
    To quote one of my friends at AMX, "why would you want to do that!"

    Looks like the constant is converted to a variable as it is passed into the function.

    Joe, send this link GM.
  • mpullinmpullin Posts: 949
    Joe Hebert wrote:
    Output when button 1 is pushed:
    Line 1 :: ITOA(snNeg275Const) = 65261 - 3:33:20
    Line 2 :: ITOA(snNeg275Var) = -275 - 03:33:20

    I think it's interesting, if obvious, that
    65536 - 275 = 65261

    so when displaying a negative constant it just subtracts the number from the maximum integer size (same thing that happens when you subtract a bigger non-signed integer from a smaller non-signed integer)
  • I believe this is actually a bug in the way constants are treated when they are initialized, NOT an issue with ITOA().
    I'll pass it along as necessary.

    Best thing for now is to work the issue as described in previous posts...
  • Note, ITOA() actually functions as SignedLong to ASCII.
    This is considered a "feature", not a bug, as one will should get expected results with integers, signed or unsigned. Per the NetLinx Keywords Help: "converts a 32-bit signed integer to convert to a decimal string".

    This is why you can get -275 in one case and 65261 in the other. Internally the sign bit is not at $8000 - as you would expect if the data were actually using only 16 bits.
Sign In or Register to comment.