Hmm, isn't that more of C a "feature"? I know I always get a warning in Visual C++ (highest warning level) when comparing signed with unsigned types. I could imagine it's because of binary representation...
unsigned int len = (unsigned int) ((unsigned int) l < ui) ? l : ui;
Edited typos
Last edited by linusa on 14 Aug 2011, 03:43, edited 2 times in total.
RWTH - Mindstorms NXT Toolbox for MATLAB
state of the art in nxt remote control programming http://www.mindstorms.rwth-aachen.de MotorControl now also in Python, .net, and Mathematica
muntoo wrote:
Besides, NXC should implicitly convert them, especially in a simple case such as this.
That's exactly the root of the problem I imagine: How would you convert?
When you have a large unsigned int (let's say 2^32-1), you can't convert it to a signed one! On the other hand, when you've got a negative signed int, how would you convert that to unsigned?
So you need some kind of logic, just by auto-casting it won't work. And I guess this logic is not built into NXC (yet). So it's a binary compare. Which makes sense, performance-wise. And then you've got strange things happening all over (one point might be http://en.wikipedia.org/wiki/Signed_num ... sentations ).
RWTH - Mindstorms NXT Toolbox for MATLAB
state of the art in nxt remote control programming http://www.mindstorms.rwth-aachen.de MotorControl now also in Python, .net, and Mathematica
yes, I'm already using this workaround temporarily, it works AFAIK, but again leads to spaghetti code -
so real-C-type-casting is appreciated anyway!
(I actually hate spaghetti code)
;)
inline float floatFromUI(const unsigned int ui) {
float f = ui;
return f;
}
inline long longFromUI(const unsigned int ui) {
long l = ui;
return l;
}
Yes, its annoying because of missing function overloading, and yes, NXC should have casting functions. But those aren't excuses for spaghetti
RWTH - Mindstorms NXT Toolbox for MATLAB
state of the art in nxt remote control programming http://www.mindstorms.rwth-aachen.de MotorControl now also in Python, .net, and Mathematica
, depending on how typcasts would be implemented in NXC.
RWTH - Mindstorms NXT Toolbox for MATLAB
state of the art in nxt remote control programming http://www.mindstorms.rwth-aachen.de MotorControl now also in Python, .net, and Mathematica