繁体   English   中英

不精确错误:Int64(::Float64)

[英]InexactError: Int64(::Float64)

我仍在学习语言 Julia 并且出现此错误。 我正在编写蚊子种群 model 并且我正在尝试运行我的主要 function 100 次。 这个主要的 function 使用许多其他函数来计算子种群水平。


# 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

但是我得到了这个错误,我试图用所有的 Float64 stuf 来修复它。 不幸的是,我不工作。 我希望你们中的一些人看到了这个问题,或者可以帮助我理解错误信息。

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

您可以通过键入?InexactError检查InexactError的文档:

help?> InexactError
search: InexactError

  InexactError(name::Symbol, T, val)

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

我认为这很好地解释了它。 没有表示值87.39533010546728Int64

您有多种选择。 查看他们的帮助以了解更多信息:

julia> trunc(Int, 87.39533010546728)
87

julia> Int(round(87.39533010546728))
87

julia> Int(floor(87.39533010546728))
87

我们没有看到main的代码。 但是,您似乎正在使用Array之一的值作为其参数,用于索引代码中的某些向量。 而且由于向量索引需要是整数,所以它失败了。 很可能某些变量在您的 main 中的位置错误 - 环顾[]运算符。

调试时,您还可以尝试将Arrays更改为Int元素,并查看导致问题停止的更改。 例如round.(Int, tempa)等。

问题就是它所说的:您不能将十进制数 (87.39) 精确地表示为 integer。

您需要在这里决定要做什么 - 一种选择是在将十进制数转换为 integer 之前将其round()

很难从您发布的代码中确切地说出错误发生的位置,但是发生这种情况的一种可能不太明显的方法是,如果您尝试对数组进行索引(例如my_array[i] ),并且您的计算导致i有一个非整数值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM