If I specifically declare a result variable as (u)Int64 and do some simple math using other types, why does it give weird results?
For example, in the example below, I declare variable ‘result64’ as UInt64 because I know the result can be a big number:
Dim result64 As UInt64
Dim i64 As Int64 = 2147483647
Dim i32 As Int32 = 2147483647
// This doesn't work
result64 = i32 * 5
system.DebugLog(result64.ToText)
// result64 = 2147483643
// This doesn't work
result64 = 2147483647 * 2147483647
system.DebugLog(result64.ToText)
// result64 = 1
// This doesn't work
result64 = i32 * i32
system.DebugLog(result64.ToText)
// result64 = 1
// This doesn't work
result64 = i32 + 1
system.DebugLog(result64.ToText)
// result64 = 18446744071562067968
// This works
result64 = 2147483648
system.DebugLog(result64.ToText)
// result64 = 2147483648
// But this doesn't work
result64 = 2147483647 + 1
system.DebugLog(result64.ToText)
// result64 = 18446744071562067968
// This works
result64 = i64 + 1
system.DebugLog(result64.ToText)
// result64 = 2147483648
// And obviously, this works
result64 = i64 * i64
system.DebugLog(result64.ToText)
// result64 = 4611686014132420609
Now I know that I’m spoiled because normally, Xojo interpret and casts most things automatically. But I’m trying to understand if there’s some specific reasoning behind it and how I should do these kind of things. I mean, Xojo knows that I’m expecting a result of UInt64 so why don’t things like 2147483647 + 1 normally work?
(I ran into this while doing some math on large numbers and Slider Controls that use Int32’s as Values. Things like SomeInt64 = Slider1.Value * 99999 go weird if the result is >2147483647.)