Page 1 of 2
[NXC][Bug] Different type comparisons
Posted: 13 Aug 2011, 22:28
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.
Re: [NXC][Bug] Different type comparisons
Posted: 13 Aug 2011, 22:45
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
Re: [NXC][Bug] Different type comparisons
Posted: 14 Aug 2011, 00:26
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
Re: [NXC][Bug] Different type comparisons
Posted: 14 Aug 2011, 03:47
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 ).
Re: [NXC][Bug] Different type comparisons
Posted: 14 Aug 2011, 07:57
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;
Re: [NXC][Bug] Different type comparisons
Posted: 14 Aug 2011, 12:10
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.
Re: [NXC][Bug] Different type comparisons
Posted: 14 Aug 2011, 12:17
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)
;)
Re: [NXC][Bug] Different type comparisons
Posted: 14 Aug 2011, 12:39
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
Re: [NXC][Bug] Different type comparisons
Posted: 14 Aug 2011, 12:48
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)
Re: [NXC][Bug] Different type comparisons
Posted: 14 Aug 2011, 12:54
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
, depending on how typcasts would be implemented in NXC.