# LoadError: InexactError: Int64 caused by decimals

**URL:** <https://discourse.julialang.org/t/loaderror-inexacterror-int64-caused-by-decimals/101717>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [July 18, 2023, 7:41am UTC](https://discourse.julialang.org/t/loaderror-inexacterror-int64-caused-by-decimals/101717 "2023-07-18T07:41:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![martina.gherardi](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@martina.gherardi](https://discourse.julialang.org/u/martina.gherardi)\
**Post date:** [July 18, 2023, 7:41am UTC](https://discourse.julialang.org/t/loaderror-inexacterror-int64-caused-by-decimals/101717/1 "2023-07-18T07:41:11Z")

</div>

Hi,

In order to print my output values for the integer variable γ, I wrote

```julia
matrix_gamma = zeros(Int, length(K), length(T))
for (i,k) in enumerate(K)
    for (j,t) in enumerate(T)
        matrix_gamma[i,j] = value(γ[k,t])
    end
end

```

However I obtained

```julia
ERROR: LoadError: InexactError: Int64(7.000000000000023)

```

There is a way to avoid this error (without changing the type `Int` into `Float64`) ?

Thank you in advance,  
Martina

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [July 18, 2023, 7:50am UTC](https://discourse.julialang.org/t/loaderror-inexacterror-int64-caused-by-decimals/101717/2 "2023-07-18T07:50:06Z")

</div>

Hi Martina,  
apparently `value(γ[k,t])` does not always return an `Int`. You perhaps want to round off the value? If so you can just do `round(Int, value(γ[k,t]))`, which round the value to the nearest `Int`.

Also you can use broadcasting to replace your loops and shorten the code:  
`matrix_gamma = round.(Int, value.(γ[K,T]))`

Note this makes a copy of `γ` which you can avoid with using a `@view` if you care about the allocation. The following should be fully equivalent to your code:  
`matrix_gamma = round.(Int, value.(@view γ[K,T]))`

Cheers,  
Adrian

---

<div class="post-metadata">

**Author:** ![martina.gherardi](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@martina.gherardi](https://discourse.julialang.org/u/martina.gherardi)\
**Post date:** [July 18, 2023, 8:03am UTC](https://discourse.julialang.org/t/loaderror-inexacterror-int64-caused-by-decimals/101717/3 "2023-07-18T08:03:00Z")

</div>

@abraemer Thank you for your answer and great suggestions to improve my code.

It is strange that `value(γ[k,t])` does not always return an `Int` because in my optimization model I declared gamma as an integer variable

```julia
@variable(Model, γ[k in K, t in T]>= 0,Int)

```

Do you know a way to avoid this error approximation?

Best,  
Martina

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [July 18, 2023, 8:17am UTC](https://discourse.julialang.org/t/loaderror-inexacterror-int64-caused-by-decimals/101717/4 "2023-07-18T08:17:01Z")

</div>

I haven’t worked with JuMP (where I think `@variable` stems from) but I assume that internally there is some tolerance to the integer constraint. Probably due to numerical reasons. I found this [stackoverflow post](https://stackoverflow.com/questions/57267961/julia-jump-returns-binary-variable-at-0-99) which seems related and mentions that solvers have some “Integer feasibility tolerance” so perhaps you need to look at the documentation of the solver you use and its treatment of integer constraints.

That being said, if you require exact `Int`s then rounding is probably what you want to do.

---

<div class="post-metadata">

**Author:** ![abelsiqueira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abelsiqueira/32/47269_2.png) [@abelsiqueira](https://discourse.julialang.org/u/abelsiqueira)\
**Post date:** [July 18, 2023, 10:53am UTC](https://discourse.julialang.org/t/loaderror-inexacterror-int64-caused-by-decimals/101717/5 "2023-07-18T10:53:35Z")

</div>

You can use `round(Int, value(y[k, t]))` to round to the nearest integer as type Int.
