Float to int

hi all
if i have array c = [1.0, 2.0, 2.0, 1.0] and i want to convert it to Int in this way c = [1,2,2,1]
i tried some thing like that

for i in count
     i = Int64(i) 
     println(i) # the output her is 1 then 2 => like what i need 
end

println(count) # the output her is in float64 it didn't change

how i can solve that ?

and one more thing please

and if i need to Define an array
how i can do that ?
Suppose i need array with size 26

You cannot convert an array inplace, but you could just do c = Int.(c) to assign a new array of Ints to c.

7 Likes

You can solve this with broadcast

c = [1.0, 2.0, 2.0, 1.0]
println(typeof(c))

c = Int.(c) #Change from Float to Int
println(typeof(c))

The dot between Int and c tells Julia to apply the Int conversion elementwise to the array.

Can you be more specific with what you are looking for for an array? If you are just looking to allocate space for a 26 element vector, you can do that by:
Array{Any, 1}(undef, 26)

The Any tells Julia the type of the Array, the 1 tells Julia the dimension of the matrix(above gives you a vector), and undef would allocate the space, but not populate it with anything.

2 Likes

it was wonderful if it work

i tried this and their is an error (from REPL)

error during bootstrap:
LoadError("main.jl", 1, MethodError(Array{Any,1}, (Int64, 4), 0x0000000000006890))

this work

household = Array{Int64}(undef,length(a),1)

thanks

You don’t want the 1 there probably. That would create a n x 1 matrix, and you probably want a vector instead.

3 Likes

or


julia> c = [1.0, 2.0, 2.0, 1.0]
4-element Array{Float64,1}:
 1.0
 2.0
 2.0
 1.0

julia> cints = map(Int, c)
4-element Array{Int64,1}:
 1
 2
 2
 1
2 Likes

If you are not sure, whether conversion will be successful, e.g. after a json parsing, you can define

int_if_possible(v) = all(v .% 1 .== 0) ? Int.(v) : v

so

julia> v = [1.0, 2.0, 3.0];

julia> int_if_possible(v)
3-element Vector{Int64}:
 1
 2
 3

julia> v = [1.0, 2.0, 3.1];

julia> int_if_possible(v)
3-element Vector{Float64}:
 1.0
 2.0
 3.1

1 Like

You are looking for isinteger. But generally this is likely not a good approach. If you want Ints just use round(Int, x) or trunc(Int, x) or any related function. A mixture of exact Ints and floats is likely not very useful or meaningful.

Also please don’t resurrect ancient topics (half a decade in this case). If you have questions, just open a new topic (and link back if you think the context is still relevant).

4 Likes

Will do, thanks

Also, this is potentially slow and needlessly allocates a temporary array. I suggest

all(isinteger, v) 

instead.

Well, I understood that isinteger is by far the better option than my x % 1 == 0. But if we’re already continuing the discussion here, I’d like to add another version:

function int_if_possible(v::AbstractArray{T, N}) where {T, N}
    all(isinteger.(v)) ? Array{Int, N}(v) : v
end

which can handle multiple dimensions as well as empty arrays.

Moreover, here’s a benchmark on various versions. Interestingly, the broadcasting one is the fastest.

function int_if_possible_1(v)
    all(v .% 1 .== 0) ? Int.(v) : v
end

function int_if_possible_2(v::AbstractArray{T, N}) where {T, N}
    all(isinteger.(v)) ? Array{Int, N}(v) : v
end

function int_if_possible_3(v::AbstractArray{T, N}) where {T, N}
    all(isinteger, v) ? Array{Int, N}(v) : v
end

function int_if_possible_4(v::AbstractArray{T, N}) where {T, N}
    all_integer = true
    for x in v
        isinteger(x) || (all_integer=false; break)
    end
    all_integer ? Array{Int, N}(v) : v
end

v = 1:1.0:100_000

using BenchmarkTools

@btime int_if_possible_1(v);
#  981.000 μs (5 allocations: 793.61 KiB)

@btime int_if_possible_2(v);
#  350.400 μs (5 allocations: 793.61 KiB)

@btime int_if_possible_3(v);

#  477.300 μs (3 allocations: 781.32 KiB)

@btime int_if_possible_4(v);
#  475.200 μs (3 allocations: 781.32 KiB)

That wasn’t my point. My main point was that it is generally not a good idea to create intermediate arrays needlessly. So I suggest all(isinteger, v) instead of all(isinteger.(v)) to avoid allocations.

It can happen in some cases that allocating is faster, but generally, the non-allocating version is better.

Also, for this benchmark you should use variable interpolation for more accurate results, ie.

@btime int_if_possible_3($v); 
1 Like

Don’t worry, I did understood that and I was surprised to see that the allocating version was faster. That’s why I mentioned it. Maybe I should have been clearer.
My main point, however, was the handling of multiple dimensions and empty arrays.