UInt64 < 0?

I don’t understand why a UInt64 number can be less than 0.

var n As UInt64 = 15112794405743899764

if n < 0 then
beep
end if

Why is n less than 0 in this example?

Either some conversion to 32bit is occurring, or the problem lies in the 0

what happens if you try:

var n As UInt64 = 15112794405743899764
var zero as UINT64 = 0

if n < zero then
beep
end if

The Uint64 is not < 0, but Xojo expression solver is not smart enough and have few flaws, it basically uses signed math and not large enough intermediate math even when the expression have no signed arguments and have large values, for example; it sees 15112794405743899764 as -3333949667965651852

Xojo’s comparing changes it to an Int64 with sign.

See CompareNumbersMBS function in MBS Plugin to correctly handle those edge cases.

The largest safe positive int value in Xojo, currently, is a Int64 9223372036854775807, the largest number with the bit 63 (0 to 63, 64 bit) = 0. 15112794405743899764 has the sign bit = 1

This works as expected.

Oh boy, here we go again.

2 Likes

Thanks for your tips.

0 is a Integer by default since it’s undefined otherwise. Xojo’s compiler is not so smart here.

var n As UInt64 = 15112794405743899764
Var zero As Uint64 = 0 
If n < zero Then
beep // Should not happen
End if

// Alternative:
If n < CType(0, Uint64) Then
beep // Should not happen
End if

This way the compiler knows you want to compare a Uinteger instead if the default Integer
It’s best to think of any variable as being a some default (or undefined) unless othwerwise specified that way you won’t bump your head on these silly things for too long.