# Error in my code

**URL:** <https://discourse.julialang.org/t/error-in-my-code/104930>\
**Category:** New to Julia\
**Created:** [October 13, 2023, 2:13pm UTC](https://discourse.julialang.org/t/error-in-my-code/104930 "2023-10-13T14:13:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Beck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex_beck/32/202454_2.png) [@Alex\_Beck](https://discourse.julialang.org/u/Alex_Beck)\
**Post date:** [October 13, 2023, 2:13pm UTC](https://discourse.julialang.org/t/error-in-my-code/104930/1 "2023-10-13T14:13:16Z")

</div>

I have this code 🙂  
‘’'function virus\_spread(transmission\_rate, initial\_infected, total\_population, simulation\_duration)  
# Initialize arrays to track data  
time = 1:simulation\_duration  
susceptibles = Int[total\_population - initial\_infected]  
infected = Int[initial\_infected]  
recovered = Int[0]

```
for day in 2:simulation_duration
    # Calculate the number of new infections
    new_infections = Int[susceptibles[day - 1] * transmission_rate * infected[day - 1] / total_population]

    # Calculate the number of recoveries (hypothetically, after some time)
    recoveries_day = Int[infected[day - 1] - new_infections]

    # Update arrays
    push!(susceptibles, susceptibles[day - 1] - new_infections)
    push!(infected, infected[day - 1] + new_infections - recoveries_day)
    push!(recovered, recovered[day - 1] + recoveries_day)
end

return time, susceptibles, infected, recovered

```

end

# Simulation parameters

transmission\_rate = 0.2 # Transmission rate (0.2 means one person infects 20% of the population)  
initial\_infected = 1  
total\_population = 1000  
simulation\_duration = 100

# Run the simulation

time, susceptibles, infected, recovered = virus\_spread(transmission\_rate, initial\_infected, total\_population, simulation\_duration)

# Plot the results

plot(time, susceptibles, label=“Susceptibles”)  
plot!(time, infected, label=“Infected”)  
plot!(time, recovered, label=“Recovered”, xlabel=“Days”, ylabel=“Number of People”, legend=true)  
‘’’

and I have this error:  
‘’‘ERROR: LoadError: InexactError: Int64(0.1998)  
Stacktrace:  
[1] Int64  
@ ./float.jl:900 [inlined]  
[2] convert  
@ ./number.jl:7 [inlined]  
[3] setindex!  
@ ./array.jl:969 [inlined]  
[4] getindex  
@ ./array.jl:403 [inlined]  
[5] virus\_spread(transmission\_rate::Float64, initial\_infected::Int64, total\_population::Int64, simulation\_duration::Int64)  
@ Main ~/Documents/sujet veille technologique/Julia/propagation.jl:10  
[6] top-level scope  
@ ~/Documents/sujet veille technologique/Julia/propagation.jl:31  
‘’’  
can you help me ?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [October 13, 2023, 2:20pm UTC](https://discourse.julialang.org/t/error-in-my-code/104930/2 "2023-10-13T14:20:04Z")

</div>

Here you are probably ending up with non-integer numbers:

```julia
new_infections = Int[susceptibles[day - 1] * transmission_rate * infected[day - 1] / total_population]

```

when you do `Int[]` you are initialising an array of number of type `Int`, which cannot hold non-integer numbers. Maybe you are looking for `round(Int, some_number)` to round to integer?

---

<div class="post-metadata">

**Author:** ![Alex\_Beck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex_beck/32/202454_2.png) [@Alex\_Beck](https://discourse.julialang.org/u/Alex_Beck)\
**Post date:** [October 13, 2023, 2:22pm UTC](https://discourse.julialang.org/t/error-in-my-code/104930/3 "2023-10-13T14:22:07Z")

</div>

where do I put that ? I don’t understand the problème

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [October 13, 2023, 2:30pm UTC](https://discourse.julialang.org/t/error-in-my-code/104930/4 "2023-10-13T14:30:42Z")

</div>

Dividing two integers can result in a non-integer.

```julia-repl
julia> 4/3
1.3333333333333333

```

You are then trying to put that non-integer into an array of integers, but Julia doesn’t know how you want to round it, so it throws an `InexactError`.

```julia-repl
julia> Int[4/3]
ERROR: InexactError: Int64(1.3333333333333333)

```

You need to round the result yourself first.

```julia-repl
julia> Int[round(Int,4/3)] # round to nearest
1-element Vector{Int64}:
 1

julia> Int[ceil(4/3)] # round up
1-element Vector{Int64}:
 2

julia> Int[floor(4/3)] # round down
1-element Vector{Int64}:
 1

```

Please read [this post](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757) to make it easier to help you next time. (It is hard to read your code and the plotting lines are irrelevant to your problem.)

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [October 19, 2023, 10:39am UTC](https://discourse.julialang.org/t/error-in-my-code/104930/5 "2023-10-19T10:39:42Z")

</div>

Rather than as e.g. one of the suggestions:

> [@Nathan\_Boyer](#):
>
> `julia> Int[ceil(4/3)] # round up`

You might want to do `Int32[ceil(my_calc_estimate; sigdigits=1)]`.

```julia
julia> ceil(24.4; sigdigits=1)
30.0

julia> ceil(2.4; sigdigits=1)
3.0

```

Integers are good for countable things, but not _measurable_ things. And while infections are _seemingly_ countable (I guess in practice never), an estimate of infection rate isn’t. I.e. it’s arguably in the same category of measureable things, since it’s a calculation that gives a fractional number, and could arguably just be kept as such with maybe one fractional digit? Or rounded, and then it needs NOT to be to the closest integer. Since it’s an estimate anyway, to the nearest tens or hundreds might be arguable, or just to one significant digit (2?). And I argue for ceil[ing] rather than closest in your case, to not round to zero, if there’s some risk.

About the Int32 rather than just Int or Int64. Int converts in practice to Int64 (unless on 32-bit julia then Int32). If you want your code to be portable then you may want to chose Int64 explicitly, or if you do not expect higher than typemax(Int32) = 2147483647 infections (hopefully not!) then Int32 should do and save space. UInt32 gets you twice as many possible and Int64 practically infinite, and less worry about overflow. You can also do ceil or round, and still keep as Float6 (or Float32 for space).
