Create a new desktop app with this in Window.Opening event:
Dim value As UInt32 = val("-1")
MsgBox(Str(value))
Build a mac universal app on Xojo 2024r3.1. When you run the app on an Intel Mac it shows “4294967295”, but when run it on an ARM MacMini it shows “0”.
Is this a bug in Xojo, or are you just not meant to set a “U” variable to -1? Thanks
Yes, you’re definitely not meant to assign an out-of-range value to a variable; the results are unpredictable. Looks like you may be exposing a difference in how this situation is handled on the two different architectures.
But is it a bug? Should unpredictable behavior be … predictable? Consistent? I’d argue no, but I might also argue that value as UInt32 = -1 should throw an OutOfBoundsException.
If you assign a value that is larger (or smaller) than what the specific Integer type can hold, then the value will “overflow”. This means the value will wrap around to the corresponding largest or smallest value and continue from there.
Val takes a String and returns a Double. You’re then converting that to an Integer and assigned to an Unsigned Integer. That conversion path may be leading to differences for each platform.
UInteger and its variants are meant to be unsigned integers, so a negative value should probably be undefined.
If you think in bytes, it isn’t surprising that -1 gives you the maximum positive value for the unsigned integer size on the Mac - this is traditional behaviour.
0 isn’t necessarily wrong since a negative value shouldn’t exist in this type.
I agree they should be consistent, but it shouldn’t matter because they’re not meant to be used this way.
I agree. If a datatype has a constraint, then some check logic should be in place to ensure the assignment does not cause an overflow or error.
This problem is not unlike checking for a divide-by-zero condition.
If factor2.Equals(0.0, 1) Then
// avoid divide-by-zero error and just assign zero
calculated = 0.0
Else
// safe to calculate
calculated = factor1 / factor2
End If