ROBOTC.net forums
http://robotc.net/forums/

RobotC 3.60 Bug - Unsigned long
http://robotc.net/forums/viewtopic.php?f=63&t=7019
Page 1 of 1

Author:  nateww [ Sun Oct 20, 2013 1:18 pm ]
Post subject:  RobotC 3.60 Bug - Unsigned long

Just doing some quick checks on the code (making sure our code deals with overflow).

It appears that unsigned long doesn't work (at least, the data doesn't print out correctly in the display).

With the following program, I would expect
Quote:
2147483647
2147483648


But, instead I get
Quote:
2147483647
-2147483648


Code:
task main()
{
//    byte overflow = 127;
//    unsigned byte overflow = 127;
//    int overflow = 32767;
//    unsigned int overflow = 32767;
//    long overflow = 2147483647;
    unsigned long overflow = 2147483647;
    nxtDisplayString(0, "%d", overflow);
    overflow++;
    nxtDisplayString(1, "%d", overflow);

    // Give time to view the output
    wait1Msec(5 * 1000);
}

Author:  MHTS [ Sun Oct 20, 2013 3:21 pm ]
Post subject:  Re: RobotC 3.60 Bug - Unsigned long

Did you try %u instead of %d? %d is used to print a value as a signed integer.
Code:
    nxtDisplayString(0, "%u", overflow);

It doesn't matter what your variable type is, the format string dictates how the variable is interpreted.
The increment operator will deal with signed and unsigned the same way. Signed and unsigned is significant only when doing comparison. For example:
Code:
unsigned long uValue = 0x80000000;
long sValue = 0x7fffffff;
if (uValue > (unsigned long)sValue)
{
// This should be true.
}

if ((long)uValue > sValue)
{
// This should be false.
}

Your code should give you:
Code:
2147483647
-1

For example, to demonstrate the effect of the format string:
Code:
task main()
{
    long sValue = 0x7fffffff;
    unsigned long uValue = 0x7fffffff;

    nxtDisplayTextLine(0, "%x %x", sValue, uValue);
    sValue++;
    uValue++;
    nxtDisplayTextLine(1, "%x %x", sValue, uValue);
    nxtDisplayTextLine(2, "%d %u", sValue, uValue);
    nxtDisplayTextLine(3, "%u %d", sValue, uValue);
   
    while (true)
    {
        wait1Msec(100);
    }
}

Wait ... You are right, it seems there is a bug in the format string %d. I am expecting the LCD display will have the following:
Code:
7fffffff 7fffffff
80000000 80000000
-1 2147483648
2147483648 -1

But instead, I got:
Code:
7fffffff 7fffffff
80000000 80000000
-2147483648 2147483648
2147483648 -2147483648

I will file a bug.

Author:  nateww [ Sun Oct 20, 2013 7:26 pm ]
Post subject:  Re: RobotC 3.60 Bug - Unsigned long

MHTS wrote:
Did you try %u instead of %d? %d is used to print a value as a signed integer.
Code:
    nxtDisplayString(0, "%u", overflow);



%u is not documented as a valid specifier.
http://www.robotc.net/support/nxt/Minds ... isplay.htm

MHTS wrote:
I will file a bug.


I thought this forum was the way to file a bug. :)

Thanks for the quick look into this!

Nate

Author:  MHTS [ Sun Oct 20, 2013 9:26 pm ]
Post subject:  Re: RobotC 3.60 Bug - Unsigned long

nateww wrote:
%u is not documented as a valid specifier.
http://www.robotc.net/support/nxt/Minds ... isplay.htm

I thought this forum was the way to file a bug. :)

I always feel that RobotC documentation is not very accurate. There are a lot of things missing. You may want to file a documentation bug with them.
And yes, this forum is one way to file a bug but it depends on how often the RobotC folks monitor the forums. To make sure it doesn't fall through the cracks, sending email to support@robotc.net will open a formal ticket, so it will be "on the record".

Author:  Dick Swan [ Wed Oct 23, 2013 1:22 am ]
Post subject:  Re: RobotC 3.60 Bug - Unsigned long

MHTS wrote:
Wait ... You are right, it seems there is a bug in the format string %d. I am expecting the LCD display will have the following:
Code:
7fffffff 7fffffff
80000000 80000000
-1 2147483648
2147483648 -1

But instead, I got:
Code:
7fffffff 7fffffff
80000000 80000000
-2147483648 2147483648
2147483648 -2147483648

I will file a bug.


This is not a bug.

Start with value 0x7FFFFFFF. When you increment you get 0x80000000. If the number is signed then this is int overflow; I think the exact behavior may be allowed to be implementation dependant and can be undefined but generally most CPUs will result in 0x80000000.

For "signed" this (0x80000000) should print at "-2147483648" and not "-1". "-1" value is 0xFFFFFFFF!

NOTE: The functions that are "based" on C's sprintf function work with 32 bit values. 'sprintf' does not do any type validation on them; the format is purely driven by the "%" format codes.

Author:  MHTS [ Wed Oct 23, 2013 2:01 am ]
Post subject:  Re: RobotC 3.60 Bug - Unsigned long

Dick Swan wrote:
Start with value 0x7FFFFFFF. When you increment you get 0x80000000. If the number is signed then this is int overflow; I think the exact behavior may be allowed to be implementation dependant and can be undefined but generally most CPUs will result in 0x80000000.

For "signed" this (0x80000000) should print at "-2147483648" and not "-1". "-1" value is 0xFFFFFFFF!

NOTE: The functions that are "based" on C's sprintf function work with 32 bit values. 'sprintf' does not do any type validation on them; the format is purely driven by the "%" format codes.

Sorry, you are right. I didn't think right, 0x80000000 is -MAX_LONG - 1 not -1.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/