julia lang - ways to check overflow when converting Array{Int64} to Array{Int32} -



julia lang - ways to check overflow when converting Array{Int64} to Array{Int32} -

is there way check overflow when converting array{int64} array{int32}?

julia> x = [0,100,2^50] 3-element array{int64,1}: 0 100 1125899906842624 julia> int32(x) 3-element array{int32,1}: 0 100 0

i know 1 naive way check all(typemin(int32) .< x .< typemax(int32)). looking more efficient method.

edit:

i tried compare devectozied less elegant function g.

f(x::array{int64, 1}) = all(typemin(int32) .< x .< typemax(int32)) function g(x::array{int64, 1}) = typemin(int32) b = typemax(int32) z in x if !(a<z<b) homecoming false end end homecoming true end x = convert(array, (2^31-10000):(2^31)); @time in 1:1000; f(x); end @time in 1:1000; g(x); end

it turns out g uses less memory , hence much faster f:

julia> @time in 1:1000; f(x); end elapsed time: 0.046384046 seconds (13186456 bytes allocated) julia> @time in 1:1000; g(x); end elapsed time: 0.015128743 seconds (7824 bytes allocated)

i found direct exam method faster. discovered above, memory usage reason. in case, can exchange convenience run speed , accomplish improve performance.

assumption

you're concerned if there error if array{int64} when converted array{int32}. case when number > 2^32.

julia> int32(2^42) 0

but trying represent greater 2^31 - 1 problem because of utilize of 2's complement.

julia> x = 2^31 - 1 2147483647 julia> int32(x) == x true julia> y = 2^31 2147483648 julia> int32(y) == y false comparison

the 2 functions used comparing

function check1(x) all(typemin(int32) .< x .< typemax(int32)) end function check2(x) ok = true; const min = -2^31 const max = 2^31 - 1; = 1:length(x) v = x[i] if v > max || v < min ok = false; break; end end ok end results

check2 faster because stop 1 bad value found, faster under worst conditions

julia> z = convert(array{int64},ones(10000)); julia> @time = 1:1000 check2(z) end elapsed time: 0.008574832 seconds (0 bytes allocated) julia> @time = 1:1000 check1(z) end elapsed time: 0.036393418 seconds (13184000 bytes allocated) embellished

if @inbounds macro used, speed consistently increased approximately 60%

@inbounds v = x[i]

new results

julia> @time = 1:1000 check2(z) end elapsed time: 0.005379673 seconds (0 bytes allocated) verification

while verifying correctness, stumbled on this

julia> u = [ 2^31 - 2; 2^31 - 1; 2^31 ] 3-element array{int64,1}: 2147483646 2147483647 2147483648 julia> n in u println(check1([n])) end true false false julia> n in u println(check2([n])) end true true false

it looks check1 incorrect, 2^31 - 1 can represented 32 bit integer. instead check1 should utilize <=.

function check1(x) all(typemin(int32) .<= x .<= typemax(int32)) end

julia-lang

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -