View unanswered posts | View active topics It is currently Wed Jun 20, 2018 10:50 pm

 Page 1 of 1 [ 6 posts ]
 Print view Previous topic | Next topic
casting
Author Message
Rookie

Joined: Tue Aug 14, 2007 1:38 pm
Posts: 18
casting
I have taught my students about casting and they did an exercise on casting. RobotC version 3.04 worked great, but version 3.5 would not cast correctly. ??????????????

Mon Oct 01, 2012 11:52 am

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Re: casting
Perhaps you could supply an example piece of code that demonstrates this issue. You're not giving us a whole lot to go on here and the question marks aren't helping.

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]

Mon Oct 01, 2012 12:09 pm
Rookie

Joined: Tue Aug 14, 2007 1:38 pm
Posts: 18
Re: casting
Hey mightor,

Sorry for the short original post. I only had a couple minutes between classes. I am using the NXT to teach different topics. When talking about casting we typed the following program in.

{
float x =5;
float y = 10;
float z;
z=(int)(x/y);
}

version 3.04 gives z a value of 0 which I think is correct
version 3.05 and 3.5 give z a value of .5

Can you explain what is causing the same program to give different values on different versions?

Thanks for the help

Mon Oct 01, 2012 4:57 pm
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
Re: casting
 joetrotter wrote:Hey mightor,Sorry for the short original post. I only had a couple minutes between classes. I am using the NXT to teach different topics. When talking about casting we typed the following program in.task main(){ float x =5;float y = 10;float z;z=(int)(x/y);}version 3.04 gives z a value of 0 which I think is correct version 3.05 and 3.5 give z a value of .5Can you explain what is causing the same program to give different values on different versions? Thanks for the help

One of the things that 3.5x does was to improve compiler warnings/errors. z = (int)(x/y) is not syntactically correct. z is declared as a float and your type cast resulted to an integer. You can't assign an integer to a float. I think the previous RobotC was tolerant of that and automatically promote the type to float for you. But strictly speaking, the code is incorrect. If you do this instead z = (float)((int)(x/y)) then it should compile fine. I checked this with Visual Studio and your code was giving a compiler error:
error C4244: '=' : conversion from 'int' to 'float', possible loss of data
So even though the code compiled fine with 3.04 does not mean it was correct.

Mon Oct 01, 2012 5:22 pm
Rookie

Joined: Fri Apr 15, 2011 10:29 am
Posts: 37
Re: casting
MHTS wrote:
 joetrotter wrote:Hey mightor,Sorry for the short original post. I only had a couple minutes between classes. I am using the NXT to teach different topics. When talking about casting we typed the following program in.task main(){ float x =5;float y = 10;float z;z=(int)(x/y);}version 3.04 gives z a value of 0 which I think is correct version 3.05 and 3.5 give z a value of .5Can you explain what is causing the same program to give different values on different versions? Thanks for the help

One of the things that 3.5x does was to improve compiler warnings/errors. z = (int)(x/y) is not syntactically correct. z is declared as a float and your type cast resulted to an integer. You can't assign an integer to a float. I think the previous RobotC was tolerant of that and automatically promote the type to float for you. But strictly speaking, the code is incorrect. If you do this instead z = (float)((int)(x/y)) then it should compile fine. I checked this with Visual Studio and your code was giving a compiler error:
error C4244: '=' : conversion from 'int' to 'float', possible loss of data
So even though the code compiled fine with 3.04 does not mean it was correct.

No, casting is allowed at any time, and there are often good reasons to cast something to a smaller/shorter/less accurate data type. Try the above code out in any standard C compiler, and it should not complain. (Verified locally using clang on my Mac..)

This is definitely a bug..

Nate

Tue Oct 02, 2012 9:44 am
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
Re: casting
After re-reading the original post carefully, I have misread it the first time. I thought the issue was the compiler either should or should not compile the code without error. But after rereading it, if (int) cast does not lose the 0.5 (which is the intention), then I agree it's a RobotC bug. I think it is a bug in the optimization that RobotC thinks since you are type casting a float to int and then assigning to float, it must be a NOP.

Tue Oct 02, 2012 1:22 pm
Display posts from previous:  Sort by
 Page 1 of 1 [ 6 posts ]

#### Who is online

Users browsing this forum: No registered users and 2 guests

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

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning