# Issue with data type in Arrays

**URL:** https://discourse.julialang.org/t/issue-with-data-type-in-arrays/34537
**Category:** New to Julia
**Created:** [February 12, 2020, 8:01pm UTC](https://discourse.julialang.org/t/issue-with-data-type-in-arrays/34537 "2020-02-12T20:01:51Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Nalt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalt/32/12795_2.png) [@Nalt](https://discourse.julialang.org/u/Nalt)
#### Post date: [February 12, 2020, 8:01pm UTC](https://discourse.julialang.org/t/issue-with-data-type-in-arrays/34537/1 "2020-02-12T20:01:51Z")

</div>

I am new to Julia and have been recommended to post here for questions about my Julia related work.

Here is my code that attempts to create a stochastic SIR model: [https://paste.ofcode.org/riNhvYWMcaxUwP9PU6wKm6](https://paste.ofcode.org/riNhvYWMcaxUwP9PU6wKm6) but I get the following error:

> InexactError: Int64(18.45312925286407)
> 
> Stacktrace:  
> [1] Int64 at .\float.jl:709 [inlined]  
> [2] convert at .\number.jl:7 [inlined]  
> [3] push! at .\array.jl:868 [inlined]  
> [4] stochasticSIR(::Array{Float64,1}, ::Array{Float64,1}, ::Int64) at .\In[18]:33  
> [5] top-level scope at In[18]:51

This apparently means that a Float64 number is trying to be converted into an integer. I do not know where in my code that is done. How is this fixed and why is this happening?

---

<div class="post-metadata">

### Author: ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)
#### Post date: [February 12, 2020, 8:09pm UTC](https://discourse.julialang.org/t/issue-with-data-type-in-arrays/34537/2 "2020-02-12T20:09:27Z")

</div>

The stack trace points to line 33:

```julia
 push!(sirData.time, sirData.time[end] + k) # time step by k

```

and, indeed, you’ve initialized `sirData.time` to be `[0]` on line 17 which makes it a vector of ints. But `k` is a float (from the previous line).

What happens when you change that to

```julia
sirData = (dataₛ = [initialState[1]], dataᵢ = [initialState[2]], dataᵣ = [initialState[3]], time = [0.0])

```

?

Also, you don’t need semicolons as line / statement terminators.

Here’s a MWE:

```julia
julia> k = rand(Exponential(1/0.2))
3.1726199691536108

julia> a = [0]
1-element Array{Int64,1}:
 0

julia> push!(a, k)
ERROR: InexactError: Int64(3.1726199691536108)
...

julia> a = [0.0]
1-element Array{Float64,1}:
 0.0

julia> push!(a, k)
2-element Array{Float64,1}:
 0.0
 3.1726199691536108

```

---

<div class="post-metadata">

### Author: ![Nalt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalt/32/12795_2.png) [@Nalt](https://discourse.julialang.org/u/Nalt)
#### Post date: [February 12, 2020, 8:50pm UTC](https://discourse.julialang.org/t/issue-with-data-type-in-arrays/34537/3 "2020-02-12T20:50:27Z")

</div>

@anon94023334 Thank you, that fixed that issue. I actually did not realize that the “33” on line [4] meant line 33 in the stochasticSIR function. I will note that for future debugging.

I have another question, but if I need to make a thread for it please just say so.

I wrote this code to be my interpretation of [Stochastic SIR model (discrete state, continuous time) in Julia - epirecipes](http://epirecip.es/epicookbook/chapters/sir-stochastic-discretestate-continuoustime/julia) but it is not doing what I thought it would do since when I examine each array in the sirOutput tuple they do not have matching lengths nor does the data they contain make sense. At this point the issue is a fundamental problem with how my code operates and I do not know what flaw in my architecture is. Do you?

---

<div class="post-metadata">

### Author: ![non-Jedi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-jedi/32/3645_2.png) [@non-Jedi](https://discourse.julialang.org/u/non-Jedi)
#### Post date: [February 12, 2020, 9:02pm UTC](https://discourse.julialang.org/t/issue-with-data-type-in-arrays/34537/4 "2020-02-12T21:02:14Z")

</div>

Your example code is relatively small. Have you tried stepping through it with [Debugger.jl](https://github.com/JuliaDebug/Debugger.jl) to find the point where it’s execution diverges from your expectation?

---

<div class="post-metadata">

### Author: ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)
#### Post date: [February 12, 2020, 9:17pm UTC](https://discourse.julialang.org/t/issue-with-data-type-in-arrays/34537/5 "2020-02-12T21:17:44Z")

</div>

This is getting to be more complex than I have time for, but

```julia
if randNum < (probᵢ / probₜ)
            push!(sirData.dataₛ, sirData.dataₛ[end] - 1);
            push!(sirData.dataᵢ, sirData.dataᵢ[end] + 1);
        else
            push!(sirData.dataᵢ, sirData.dataᵢ[end] - 1);
            push!(sirData.dataᵣ, sirData.dataᵣ[end] +1)
        end

```

pretty much guarantees that the `data\_x` arrays will be of different lengths: you’re pushing to `data\i` in both cases, but only pushing to the others conditionally.
