# InexactError in Callback

**URL:** https://discourse.julialang.org/t/inexacterror-in-callback/102648
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [August 9, 2023, 5:27pm UTC](https://discourse.julialang.org/t/inexacterror-in-callback/102648 "2023-08-09T17:27:37Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jack\_lee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jack_lee/32/47431_2.png) [@jack\_lee](https://discourse.julialang.org/u/jack_lee)
#### Post date: [August 9, 2023, 5:27pm UTC](https://discourse.julialang.org/t/inexacterror-in-callback/102648/1 "2023-08-09T17:27:37Z")

</div>

When I learn the [user-cut](https://jump.dev/JuMP.jl/stable/tutorials/linear/callbacks/) callback function ,I rewrite the code below:

```julia
function user_cut()
    Random.seed!(13)
    N = 30
    capacity = 10
    weights,values = rand(N),rand(N)
    model = Model(GLPK.Optimizer)
    @variable(model,x[1:N],Bin)
    @constraint(model,weights' * x <= capacity)
    @objective(model,Max,x' * values)
    
    function call_back(cb_data)
        x_val = callback_value.(cb_data,x)
        total = weights' * x_val
        println("callback total : $(total)")
# num = sum(1 for i in 1:N if x_val[i] > 1e-4)
        num = sum(Int,x_val)
        if total > capacity
            con = @build_constraint(sum(x) <= num - 1)
            println("add $(con)")
            MOI.submit(model,MOI.UserCut(cb_data),con)
        end
    end
    set_attribute(model,MOI.UserCutCallback(),call_back)
    optimize!(model)
    sum(value.(x))
end

```

This is the official code:

```julia
num = sum(1 for i in 1:N if x_val[i] > 1e-4)

```

But When I change it to :

```julia
sum(Int,x_val)

```

It raised error:

```julia
callback total : 10.0
InexactError: Int64(23.047802735569988)

```

Can somebody explain why?

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [August 9, 2023, 5:59pm UTC](https://discourse.julialang.org/t/inexacterror-in-callback/102648/2 "2023-08-09T17:59:32Z")

</div>

You are requesting conversion from a Float64 to an Int64 and no exact [conversion](https://docs.julialang.org/en/v1/base/base/#Core.InexactError) exists.

```julia
julia> Int(23.0478)
ERROR: InexactError: Int64(23.0478)
Stacktrace:
 [1] Int64(x::Float64)
   @ Base ./float.jl:900
 [2] top-level scope
   @ REPL[33]:1

julia> Int(23.0)
23

```

Perhaps you mean

```julia
num = sum(trunc, x_val)

```

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [August 10, 2023, 8:49pm UTC](https://discourse.julialang.org/t/inexacterror-in-callback/102648/3 "2023-08-10T20:49:23Z")

</div>

Other options are `num = sum(x_val .> 1e-4)` or `num = sum(round.(Int, x_val))`.

But really, there’s nothing wrong with what you wrote to begin with.
