|
Page 1 of 1
|
[ 6 posts ] |
|
| Author |
Message |
|
joetrotter
Rookie
Joined: Tue Aug 14, 2007 1:38 pm Posts: 16
|
 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 |
|
 |
|
mightor
Moderator
Joined: Wed Mar 05, 2008 8:14 am Posts: 2864 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
_________________| Some people, when confronted with a problem, think, "I know, I'll use threads," | and then two they hav erpoblesms. (@nedbat)| My Blog: I'd Rather Be Building Robots| ROBOTC 3rd Party Driver Suite: [ Project Page]
|
| Mon Oct 01, 2012 12:09 pm |
|
 |
|
joetrotter
Rookie
Joined: Tue Aug 14, 2007 1:38 pm Posts: 16
|
 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.
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 .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 |
|
 |
|
MHTS
Guru
Joined: Sun Nov 15, 2009 5:46 am Posts: 1023
|
 Re: casting
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 |
|
 |
|
nateww
Rookie
Joined: Fri Apr 15, 2011 10:29 am Posts: 35
|
 Re: casting
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 |
|
 |
|
MHTS
Guru
Joined: Sun Nov 15, 2009 5:46 am Posts: 1023
|
 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 |
|
|
|
Page 1 of 1
|
[ 6 posts ] |
|
Who is online |
Users browsing this forum: No registered users and 11 guests |
|
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
|
|