InexactError

The following code throws the error InexactError: Int64(0.003781915448214505) and I am unsure why:

cost = fill(0,8,8)
for i = 2:100
    row = Int64(category[i]) # category is an Array containing Float64 from 1.0 to 8.0
    col = Int64(category[i+1]) 
    cost[row,col] = cost[row,col] + costChange[i+1] # costChange is an Array containing Float64
end

What do you think Julia should do when you try to cast 0.0037 as an Int64?

But where do I do that?

I would bet that category contains that value, not only values between 1.0 and 8.0.

But more than that, I think you want round(Int, x) rather than Int(x).

1 Like

can you check the content?

For some context, “casting” (after @pdeffebach) means making something a different type. Julia Int64(x) converts/casts into 64-bit integer and doesn’t specify what to do if x doesn’t exactly “fit” an integer. This contrasts with Python Int which (I think) first truncates if necessary.

I believe the documentation could be improved in this regard, because this is a common thing to do. I had a hard time finding docs for Int and Int64. There is this stackoverflow on similar topic. I guess it’s debatable whether Julia should rather accept Int64(3.5), which might be more similar to Python or Fortran. A downside is that some prefer an explicit error, and consider it better to explicitly specify rounding/truncating.