myexpression{T <: Integer}(a::T) = myexpression(convert(Int128, a))
# You can also define for BigInt, depending on the range you will need
# for `a`. Order is important.
myexpression(a::Int128) = a*((a+12)*(a+24)*(a+36)*(a+48))

julia>  myexpression(16140)
1103425489430061096960

On Sat, Nov 05 2016, Andrey Stepnov wrote:

> Hi Community!
>
> Suppose we have the following code:
>
> a = 1
> y = 0
> t = false
>
> while t == false
>        y = a*((a+12)*(a+24)*(a+36)*(a+48))
>        if mod(y,10^6) == 0
>        println(a)
>        println(y)
>        t = true
>        end
>        a += 1
>        end
>
> And the output will be:
>
> 16140
> -3379154992512000000
>
> Actually it happens because we reached max positive Int64:
>
> julia> typemax(Int64)
> 9223372036854775807
>
> julia> typemax(Int64) + 1
> -9223372036854775808
>
> julia> bits(9223372036854775807)
> "0111111111111111111111111111111111111111111111111111111111111111"
>
> julia> bits(-9223372036854775808)
> "1000000000000000000000000000000000000000000000000000000000000000"
>
>
> OK! Lets use Int128!
>
> julia> x = typemax(Int128)
> 170141183460469231731687303715884105727
>
> julia> x::Int128 = a*((a+12)*(a+24)*(a+36)*(a+48))
> -3037790465414534075
>
> julia> Int128(a*((a+12)*(a+24)*(a+36)*(a+48)))
> -3037790465414534075
>
> julia> big(a*((a+12)*(a+24)*(a+36)*(a+48)))
> -3037790465414534075
>
> julia> BigInt(a*((a+12)*(a+24)*(a+36)*(a+48)))
> -3037790465414534075
>
> Not that we need, because `a` in a*((a+12)*(a+24)*(a+36)*(a+48)) has type 
> Int64! After convert `a` to Int128 - all fine:
>
> julia> a = convert(Int128,a)
> 16141
>
> julia> typeof(a)
> Int128
>
> julia> a*((a+12)*(a+24)*(a+36)*(a+48))
> 1103766853957158562885
>
> julia> while t == false
>        y = a*((a+12)*(a+24)*(a+36)*(a+48))
>        if mod(y,10^6) == 0
>        println("a = $a")
>        println("y = $y")
>        t = true
>        end
>        a += 1
>        end
> a = 62452
> y = 951844491850031104000000
>
>
> OK! 
>
> But, is it possible to get result in Int128 even if `a` has Int64 type? 
>
> For example, anybody can has a lot of code and not wants explicit define 
> type for a lot of variables for the good of single expression...
>
> Andrey Stepnov

Reply via email to