Strange issue comparing unsigned integers

I’m having an issue comparing unsigned integers. A UInt32 with a large value is not evaluating as greater than a small one:

var largeUINT : System.UInt32 = 4190966295;
var smallUINT : System.UInt32 = 20;

Debug.Log("(4190966295 > 20) : " + (4190966295 > 20));
Debug.Log("(" + largeUINT + " > 20) : " + (largeUINT > 20));
Debug.Log("(4190966295 > " + smallUINT + ") : " + (4190966295 > smallUINT));
Debug.Log("(" + largeUINT + " > " + smallUINT + ") : " + (largeUINT > smallUINT));

Output:

(4190966295 > 20) : True
(4190966295 > 20) : False
(4190966295 > 20) : False
(4190966295 > 20) : False

Originally I was trying to compare the larger value (in a variable) against the smaller value as a numeric literal. I thought maybe it was a casting issue and putting them both in variables would help but it doesn’t seem to. I couldn’t find any instruction online for explicitly casting a numeric literal. Anyone know what’s happening? Thank you in advance!

So I just tested, and sure enough, same result in UnityScript.

So I ported to C#, and it worked just fine.

I have a feeling that like other versions of ECMAscript, during comparisons the type can be converted. And that large uint became an int or something and thusly became negative and smaller.

I would understand the possibility of conversion to int on the 2nd and 3rd cases, since I don’t know how to force the numeric literal into a specific type. But it would seem that at least the 4th case would evaluate as true since both arguments are UInts. Surely direct comparison of UInts is possible! Is this a compiler bug or am I missing something?
Moving to C# isn’t very practical for me at this point, so any other workaround suggestions would be most appreciated.

There’s probably something, maybe, I don’t know. I don’t write unityscript at all. I mean I like know the language enough to read and write it if I MUST… but I’m hardly intimate with it to deal with this kind of issue.

Hopefully someone else can come along and help you out.

I’d say it’s a bug; use the bug reporter app to report it. In the meantime you could use ulong instead of uint (you can just use uint instead of System.UInt32 by the way), or if that’s not feasible, at least use temporary ulong variables converted from uint when making the comparison.

–Eric

Thanks, guys. The input is much appreciated. I’ll submit a bug. I’m on Unity version 3.5.6f4 right now, so I’ll check to make sure it hasn’t already been fixed. I’ve been dragging my feet on updating.

For now, directly calling largeUINT.CompareTo(smallUINT) seems to be a valid workaround for me. So I’m guessing the UnityScript compiler is invoking something else? :stuck_out_tongue:

As soon as I have time, I’m moving to C# and not looking back! I’m tired of JS quirkiness.

I checked with Unity 4 before I posted and it’s not fixed.

–Eric

Same here, tested in 3.5 and 4 before posting as well.

Bug report submitted.
Thanks again, guys.