View unanswered posts | View active topics It is currently Wed Jul 30, 2014 5:55 pm






Reply to topic  [ 6 posts ] 
RobotC 3.60 Bug - Unsigned long 
Author Message
Rookie

Joined: Fri Apr 15, 2011 10:29 am
Posts: 37
Post 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);
}


Sun Oct 20, 2013 1:18 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post 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.


Sun Oct 20, 2013 3:21 pm
Profile
Rookie

Joined: Fri Apr 15, 2011 10:29 am
Posts: 37
Post 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


Sun Oct 20, 2013 7:26 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post 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".


Sun Oct 20, 2013 9:26 pm
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 614
Post 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.


Wed Oct 23, 2013 1:22 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post 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.


Wed Oct 23, 2013 2:01 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 6 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.