# InexactError

**URL:** https://discourse.julialang.org/t/inexacterror/65907
**Category:** New to Julia
**Created:** [August 5, 2021, 7:47pm UTC](https://discourse.julialang.org/t/inexacterror/65907 "2021-08-05T19:47:14Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)
#### Post date: [August 5, 2021, 7:47pm UTC](https://discourse.julialang.org/t/inexacterror/65907/1 "2021-08-05T19:47:14Z")

</div>

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

```

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [August 5, 2021, 7:50pm UTC](https://discourse.julialang.org/t/inexacterror/65907/2 "2021-08-05T19:50:39Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Nash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nash/32/14482_2.png) [@Nash](https://discourse.julialang.org/u/Nash)
#### Post date: [August 5, 2021, 7:52pm UTC](https://discourse.julialang.org/t/inexacterror/65907/3 "2021-08-05T19:52:04Z")

</div>

But where do I do that?

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [August 5, 2021, 7:53pm UTC](https://discourse.julialang.org/t/inexacterror/65907/4 "2021-08-05T19:53:39Z")

</div>

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)`.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [August 5, 2021, 8:13pm UTC](https://discourse.julialang.org/t/inexacterror/65907/5 "2021-08-05T20:13:22Z")

</div>

> [@Nash](#):
>
> `# category is an Array containing Float64 from 1.0 to 8.0`

can you check the content?

---

<div class="post-metadata">

### Author: ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)
#### Post date: [August 5, 2021, 8:37pm UTC](https://discourse.julialang.org/t/inexacterror/65907/6 "2021-08-05T20:37:13Z")

</div>

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](https://stackoverflow.com/questions/40520131/convert-float-to-int-in-julia-lang) 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.
