Error in my code

I have this code :slight_smile:
‘’'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 ?

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

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?

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

Dividing two integers can result in a non-integer.

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> Int[4/3]
ERROR: InexactError: Int64(1.3333333333333333)

You need to round the result yourself first.

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

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

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

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