单独测试,只要不带For都可以成功运行。
如果想要使用For,就不能使用Int8类型定义classes。
虽然实际中用Int64就行,但还是想知道这个错误的原因。
classes = zeros(Int8,6)
"classes = zeros(Int64,6)成功运行"
for i in rand(15:50,1000)
if i <=18.5
classes[1] += 1
elseif i in 18.5:25
classes[2] += 1
elseif i in 25:30
classes[3] += 1
elseif i in 30:35
classes[4] += 1
elseif i in 35:40
classes[5] += 1
else
classes[6] += 1
end
end
"
去除For后判断部分成功运行
i=12
if i == 12
classes[1] += 1
end
"
print(classes)
错误报告:
ERROR: LoadError: InexactError: trunc(Int8, 128)
Stacktrace:
[1] throw_inexacterror(f::Symbol, #unused#::Type{Int8}, val::Int64)
@ Core .\boot.jl:612
[2] checked_trunc_sint
@ .\boot.jl:634 [inlined]
[3] toInt8
@ .\boot.jl:649 [inlined]
[4] Int8
@ .\boot.jl:759 [inlined]
[5] convert
@ .\number.jl:7 [inlined]
[6] setindex!(A::Vector{Int8}, x::Int64, i1::Int64)
@ Base .\array.jl:903
[7] top-level scope
@ C:\Users\22518\AppData\Local\Temp\tempCodeRunnerFile.julia:15
in expression starting at C:\Users\22518\AppData\Local\Temp\tempCodeRunnerFile.julia:3