# Array{Float64}(undef, X) gives wrong results

**URL:** <https://discourse.julialang.org/t/array-float64-undef-x-gives-wrong-results/28991>\
**Category:** General Usage\
**Tags:** bug\
**Created:** [September 20, 2019, 9:36am UTC](https://discourse.julialang.org/t/array-float64-undef-x-gives-wrong-results/28991 "2019-09-20T09:36:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![JosephPollacco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josephpollacco/32/51404_2.png) [@JosephPollacco](https://discourse.julialang.org/u/JosephPollacco)\
**Post date:** [September 20, 2019, 9:36am UTC](https://discourse.julialang.org/t/array-float64-undef-x-gives-wrong-results/28991/1 "2019-09-20T09:36:26Z")

</div>

I am writing a complex hydrological model in JUILIA.

[https://github.com/joseph-pollacco/HyCatch-1D/tree/master/JULIA](https://github.com/joseph-pollacco/HyCatch-1D/tree/master/JULIA)

Problem in file:  
In file _WaterBalance.jl_

I was finding that when I run the model from the Julia REPL that **rarely** I get the correct results and other **most of the times** the results did not make sense (_e.g. NaN in the computed Array_).

I found that the problem was caused by initializing an empty array such as:

` WaterBalance =Array{Float64}(undef, 50000)`

I needed to perform a simple loop.

```julia
∑Evaporation= Array{Float64}(undef, 50000)
 function GLOBAL()
 for iT=2:N_iT
 …
  ∑Evaporation[iT] = ∑Evaporation[iT-1] + discret.ΔZ[N_iZevapo] * Qevaporation[iT] * ΔT[iT]

end

```

I required that

`∑Evaporation [1] = 0.0`

For reasons which I do not understand **the solution I found** was to initialize the Array by using zeros:

`WaterBalance = zeros(Float64, 50000)`

So my understanding is that there is a _ **bug in Array.** _

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 20, 2019, 9:42am UTC](https://discourse.julialang.org/t/array-float64-undef-x-gives-wrong-results/28991/2 "2019-09-20T09:42:34Z")

</div>

> [@JosephPollacco](#):
>
> So my understanding is that there is a _ **bug in Array.** _

While that is theoretically possible, it is much more likely that there is a bug in your code — you fail to overwrite some elements, and an `Array{T}(undef, ...)` can have random memory contents.

Isolating an MWE would be helpful, try to simplify your problem so that you can post it here.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [September 20, 2019, 11:17am UTC](https://discourse.julialang.org/t/array-float64-undef-x-gives-wrong-results/28991/3 "2019-09-20T11:17:05Z")

</div>

> [@JosephPollacco](#):
>
> I required that
> 
> `∑Evaporation [1] = 0.0`

`Array{Float64}(undef, 50000)` does not initialise the elements. That’s the `undef` part 🙂.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [September 21, 2019, 9:44am UTC](https://discourse.julialang.org/t/array-float64-undef-x-gives-wrong-results/28991/4 "2019-09-21T09:44:44Z")

</div>

> [@JosephPollacco](#):
>
> I required that
> 
> `∑Evaporation [1] = 0.0`

Just put exactly this before your loop?

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [September 21, 2019, 10:44am UTC](https://discourse.julialang.org/t/array-float64-undef-x-gives-wrong-results/28991/5 "2019-09-21T10:44:47Z")

</div>

> [@JosephPollacco](#):
>
> Array{Float64}(undef, 50000)

Exactly. Your routine starts with `iT = 2` (from `iT=2:N_iT`) and then access the first element in `∑Evaporation[iT-1]`.

As @Tamas_Papp and @greg_plowman pointed out, `undef` will not initialise anything in your array, so you will get a random piece of memory.

In fact, you can see below that the probability to get a zero with an `undef`-Array with length 100000 on my machine is around 6% (YMMV and especially there are some memory management things going on which will make this not-so-predictable), so I guess that roughly reflects your observations:

> I was finding that when I run the model from the Julia REPL that **rarely** I get the correct results and other **most of the times** the results did not make sense ( _e.g. NaN in the computed Array_ ).

```juliaREPL
julia> sum(iszero.(Array{Float64}(undef, 100000))). # in a fresh REPL session
6325

```

Note that executing this again indicates some kind of caching:

```juliaREPL
julia> sum(iszero.(Array{Float64}(undef, 100000)))
100000

julia> sum(iszero.(Array{Float64}(undef, 100000)))
100000

```

---

<div class="post-metadata">

**Author:** ![JosephPollacco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josephpollacco/32/51404_2.png) [@JosephPollacco](https://discourse.julialang.org/u/JosephPollacco)\
**Post date:** [September 22, 2019, 11:45pm UTC](https://discourse.julialang.org/t/array-float64-undef-x-gives-wrong-results/28991/6 "2019-09-22T23:45:36Z")

</div>

Thanks for your answer. I did initialize before the loop.

` ∑Evaporation [1] = 0.0`

But it did not sort out the problem. Sometimes the code run fine and other times no. The only solution was to initialize with zeros.

---

<div class="post-metadata">

**Author:** ![JosephPollacco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josephpollacco/32/51404_2.png) [@JosephPollacco](https://discourse.julialang.org/u/JosephPollacco)\
**Post date:** [September 22, 2019, 11:49pm UTC](https://discourse.julialang.org/t/array-float64-undef-x-gives-wrong-results/28991/7 "2019-09-22T23:49:12Z")

</div>

Thanks for providing a valid explanations of what I observed. Therefore what are the advantages of using:

`Array{Float64}(undef, 50000)`

Compared to use the traditional:

`WaterBalance = zeros(Float64, 50000)`

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [September 22, 2019, 11:53pm UTC](https://discourse.julialang.org/t/array-float64-undef-x-gives-wrong-results/28991/8 "2019-09-22T23:53:30Z")

</div>

The first is faster if you are going to start with some nonzero value.

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [September 23, 2019, 6:50am UTC](https://discourse.julialang.org/t/array-float64-undef-x-gives-wrong-results/28991/9 "2019-09-23T06:50:01Z")

</div>

If you create an array with `undef` “elements”, you basically instruct your computer to allocate that amount of memory without caring what’s inside it. If you use `zeros` however, you tell that you want to fill them with zeros after the allocation (you initialise it).

Initialisation to a specific value takes an extra amount of time and there are cases where this might be performance relevant.

It depends on your algorithm whether you explicitly need initial values or not. If you use that array to dump values in it and you are sure that by design you will never (read) access any elements of them which are uninitialised – which will lead to unexpected values/behaviour – you can go with `undef` and squeeze a bit more performance out of the computer. This however is clearly not true for you routine above.

Here are some measurements with `BenchmarkTools`:

```nohighlight
julia> using BenchmarkTools

julia> @btime Array{Float64}(undef, 50000);
  1.318 μs (2 allocations: 390.70 KiB)

julia> @btime zeros(Float64, 50000);
  11.076 μs (2 allocations: 390.70 KiB)

```

---

<div class="post-metadata">

**Author:** ![JosephPollacco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josephpollacco/32/51404_2.png) [@JosephPollacco](https://discourse.julialang.org/u/JosephPollacco)\
**Post date:** [September 23, 2019, 8:08am UTC](https://discourse.julialang.org/t/array-float64-undef-x-gives-wrong-results/28991/10 "2019-09-23T08:08:16Z")

</div>

Thanks Tamas, you beautifully explained the cons/pros of using undef.
