# Occasionally NaNs when using similar()

**URL:** <https://discourse.julialang.org/t/occasionally-nans-when-using-similar/48224>\
**Category:** New to Julia\
**Created:** [October 12, 2020, 1:59pm UTC](https://discourse.julialang.org/t/occasionally-nans-when-using-similar/48224 "2020-10-12T13:59:23Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 12, 2020, 2:49pm UTC](https://discourse.julialang.org/t/occasionally-nans-when-using-similar/48224/8 "2020-10-12T14:49:00Z")

</div>

Or just

```julia
dh = zero(h)

```

(notice `zero`, not `zeros`).

The nice thing about `zero` (singular) is that it preserves type

```julia
julia> d = Diagonal(ones(5))
5×5 Diagonal{Float64,Array{Float64,1}}:
 1.0 ⋅ ⋅ ⋅ ⋅ 
  ⋅ 1.0 ⋅ ⋅ ⋅ 
  ⋅ ⋅ 1.0 ⋅ ⋅ 
  ⋅ ⋅ ⋅ 1.0 ⋅ 
  ⋅ ⋅ ⋅ ⋅ 1.0

julia> zeros(eltype(d), size(d))
5×5 Array{Float64,2}:
 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0

julia> zero(d)
5×5 Diagonal{Float64,Array{Float64,1}}:
 0.0 ⋅ ⋅ ⋅ ⋅ 
  ⋅ 0.0 ⋅ ⋅ ⋅ 
  ⋅ ⋅ 0.0 ⋅ ⋅ 
  ⋅ ⋅ ⋅ 0.0 ⋅ 
  ⋅ ⋅ ⋅ ⋅ 0.0

```

---

_[View the full topic](https://discourse.julialang.org/t/occasionally-nans-when-using-similar/48224)._
