# Working with Int64's

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.)

My understanding is that Xojo is casting it to the common type, which is Int32 when using literals. It doesn’t pay attention to what you’re ultimately assigning it to. So, for example, when you do `i32 + 1`, Xojo is coming up with a 32-bit result, then casting that to Int64. To be specific, you would have to cast the variables (or perhaps just the first variable) to Int64 before performing the calculation.

In the case of a literal, you can do one of these:

``````dim var as Int64 = 12345
const kVar as Int64 = 12345
i64 = CType( 12345, Int64 ) + someOtherNumber``````

(I haven’t tested all this, just off the top of my head.)

Ah. That makes sense.
Thanks Kem!

Kem is correct (as usual). The destination is not taken into account when determining the common type, only the values on the right hand side of the equation.