InexactError: Int64(::Float64)

Viewed 1477

I am still learning the language Julia and i have this error. I am writing an mosquito population model and i am trying to run my main function a 100 times. This main function uses many other functions to calculate the subpopulation levels.


# Importing KNMI data 
xf = XLSX.readxlsx("C:/Scriptie_mosquitoes/knmi_csv.xlsx")
sh = xf["knmi_csv"]
temperature = sh["B3:B368"]
precip = sh["F3:F368"]

subpopulation_amount = 100

imat_list1 = zeros(100,length(temperature))
imat_list = Array{Float64}(imat_list1)

adul_list1 = zeros(100,length(temperature))
adul_list = Array{Float64}(adul_list1)

egg_list1 = zeros(100,length(temperature))
egg_list = Array{Float64}(egg_list1)

diaegg_list1 = zeros(100,length(temperature))
diaegg_list = Array{Float64}(diaegg_list1)

imat_list[1] = 100.0
adul_list[1] = 1000.0
egg_list[1] = 100.0
diaegg_list[1] = 100.0

for counter = 1:1:subpopulation_amount
    u = Distributions.Normal()
    temp_change = rand(u)
    tempa = temperature .+ temp_change
    println(tempa)

    e = Distributions.Normal()
    precip_change = rand(e)
    println("hallo", precip_change)


    println(counter,tempa,precip,precip_change)
    main(counter,tempa::Array{Float64,2},precip::Array{Any,2},precip_change::Float64,imat_list::Array{Float64,2},adul_list::Array{Float64,2},egg_list::Array{Float64,2},diaegg_list::Array{Float64,2})
end

However i get this error which i tried to fix with all the Float64 stuf. I doesn't work unfortunatly. I hope some of you guys see the problem or can help me with understanding the error message.

ERROR: InexactError: Int64(87.39533010546728)
Stacktrace:
 [1] Int64 at .\float.jl:710 [inlined]
 [2] convert at .\number.jl:7 [inlined]
 [3] setindex! at .\array.jl:825 [inlined]
 [4] main(::Int64, ::Array{Float64,2}, ::Array{Any,2}, ::Float64, ::Array{Float64,2}, ::Array{Float64,2}, ::Array{Float64,2}, ::Array{Float64,2}) at .\REPL[905]:19
 [5] top-level scope at .\REPL[938]:10

3 Answers

You can check the documentation for InexactError by typing ?InexactError:

help?> InexactError
search: InexactError

  InexactError(name::Symbol, T, val)

  Cannot exactly convert val to type T in a method of function name.

I think that explains it nicely. There is no Int64 that represents the value 87.39533010546728.

You have a variety of options available. Check their help to learn more about them:

julia> trunc(Int, 87.39533010546728)
87

julia> Int(round(87.39533010546728))
87

julia> Int(floor(87.39533010546728))
87

We do not see the code of main. However it seems that you are using values of one of the Arrays that you have as its argument to use for indexing some vector in your code. And since vector indices need to be integers it fails. Most likely some variable is in wrong place in your main - look around [] operators.

When debugging you could also try to change your Arrays to Int elements and see which change causes the problem to stop. E.g. round.(Int, tempa) etc.

The problem is just what it says: you cannot exactly represent a decimal number (87.39) as an integer.

You need to decide what you want to do here - one option is to just round() your decimal number before converting it to an integer.

It's hard to say from the code you posted where exactly the error occurs, but one potentially less obvious way for this to happen is if you try to index into an array (e.g. my_array[i]), and your calculations lead to i having a non-integer value.

Related