[NXC][Bug] Different type comparisons

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

[NXC][Bug] Different type comparisons

Post by muntoo »

Code: Select all

// #define min(a,b) ((a) < (b) ? (a) : (b))
// #include "minmax.nxc"


task main()
{
	unsigned int ui = 441;
	long l = 64;
	// unsigned int len = min(l, ui);
	unsigned int len = l < ui ? l : ui;
	
	NumOut(0, 0, len, 1);
	Wait(1000);
}
Displays 441, not 64! This bug was torturing me in NXTFileManager. :)
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
linusa
Posts: 228
Joined: 16 Oct 2010, 11:44
Location: Aachen, Germany
Contact:

Re: [NXC][Bug] Different type comparisons

Post by linusa »

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...

Try it with casting, two versions. Either:

Code: Select all

unsigned int len = (unsigned int) (l < ui) ? l : ui;
or, the extreme careful "correct" version:

Code: Select all

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
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: [NXC][Bug] Different type comparisons

Post by muntoo »

NXC doesn't support C-like type casting. :( (I put it on the wishlist a few months ago, IIRC.)

Besides, NXC should implicitly convert them, especially in a simple case such as this.

Output NBC code:

Code: Select all

	set ui, 441
	set l, 64
	mov __signed_stack_001main, __constVal64
	mov __DU0main, ui
	cmp 0, __zfmain, __signed_stack_001main, __DU0main
	mov __D0main, __zfmain
	brtst 4, __NXC_Label_610, __zfmain
	mov __D0main, l
	jmp __NXC_Label_611
__NXC_Label_610:
	mov __DU0main, ui
__NXC_Label_611:
	mov len, __DU0main
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
linusa
Posts: 228
Joined: 16 Oct 2010, 11:44
Location: Aachen, Germany
Contact:

Re: [NXC][Bug] Different type comparisons

Post by linusa »

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
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: [NXC][Bug] Different type comparisons

Post by HaWe »

the solution is - also according to my own wish on the wish list some months ago - :
explicite type casting like in ANSI C.

Code: Select all

long l;
unsigned int ui;
float f;

f=(float)ui;
l=(long)ui;
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: [NXC][Bug] Different type comparisons

Post by spillerrec »

You can manually cast like this:

Code: Select all

unsigned int ui = 42;
float ui2 = ui;
I'm not sure if it casts correctly in all cases though. I will do some testing.
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: [NXC][Bug] Different type comparisons

Post by HaWe »

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)
;)
linusa
Posts: 228
Joined: 16 Oct 2010, 11:44
Location: Aachen, Germany
Contact:

Re: [NXC][Bug] Different type comparisons

Post by linusa »

doc-helmut wrote:yes, I'm already using this workaround temporarily, it works AFAIK, but again leads to spaghetti code -
I don't see the spaghetti code:

Code: Select all

long l;
unsigned int ui;
float f;

f=floatFromUI(ui);
l=longFromUI(ui);
with

Code: Select all

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
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: [NXC][Bug] Different type comparisons

Post by HaWe »

...will probably not work in cases of macros like

Code: Select all

unsigned int ui;
printf("%e", (float)ui);
# Error: constant or constant expression expected
File "c:\Temp\temp.nxc" ; line 15
# string msg = FormatNum("%e", floatFromUI(ui))
#----------------------------------------------------------

(CMIIW)
Last edited by HaWe on 14 Aug 2011, 12:56, edited 1 time in total.
linusa
Posts: 228
Joined: 16 Oct 2010, 11:44
Location: Aachen, Germany
Contact:

Re: [NXC][Bug] Different type comparisons

Post by linusa »

doc-helmut wrote:...will probably not work in cases like
Yep, tried

Code: Select all

unsigned int ui;
printf("%e", floatFrom(ui));
, fails with good old "constant expression expected". So I guess this problem would probably also occur with

Code: Select all

printf("%e", (float)ui);
, 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
Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests