# Purpose of zero() in a function

**URL:** <https://discourse.julialang.org/t/purpose-of-zero-in-a-function/18095>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 28, 2018, 12:08pm UTC](https://discourse.julialang.org/t/purpose-of-zero-in-a-function/18095 "2018-11-28T12:08:24Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![leejm516](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leejm516/32/8219_2.png) [@leejm516](https://discourse.julialang.org/u/leejm516)\
**Post date:** [November 28, 2018, 12:08pm UTC](https://discourse.julialang.org/t/purpose-of-zero-in-a-function/18095/1 "2018-11-28T12:08:24Z")

</div>

I do not know what to write in the title…

I am learning a lot by analyzing source codes in Julia packages I am using.  
Today, I saw an interesting line in lm.jl in the GLM package.

```julia
function deviance(r::LmResp)

    y = r.y

    mu = r.mu

    wts = r.wts

    v = zero(eltype(y)) + zero(eltype(y)) * zero(eltype(wts))

    if isempty(wts)

        @inbounds @simd for i = eachindex(y,mu)

            v += abs2(y[i] - mu[i])

        end

    else

        @inbounds @simd for i = eachindex(y,mu,wts)

            v += abs2(y[i] - mu[i])*wts[i]

        end

    end

    v

end

```

My question is in the fifth line. Why did the authors write such a line instead of `v = 0` or `v = 0.`?  
Is it for aiding type inference? Then, can it be replaced with `v = zero(eltype(y))*zero(eltype(wts))`?

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 28, 2018, 12:30pm UTC](https://discourse.julialang.org/t/purpose-of-zero-in-a-function/18095/2 "2018-11-28T12:30:29Z")

</div>

Yes, for type inference. It’s to make sure that `v` does not have to change its type during the subsequent loops as that would make it much slower. The same applies to why `wts` and `y` feature in the initialization.

---

<div class="post-metadata">

**Author:** ![leejm516](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leejm516/32/8219_2.png) [@leejm516](https://discourse.julialang.org/u/leejm516)\
**Post date:** [November 28, 2018, 12:54pm UTC](https://discourse.julialang.org/t/purpose-of-zero-in-a-function/18095/3 "2018-11-28T12:54:46Z")

</div>

Thank you for your answer… but still somewhat confusing.  
Then, where is zero(eltype(mu)) in this function? In the loops below, elements in mu are used to calculate v, but in the initialization, zero(eltype(y)) appears twice.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 28, 2018, 1:12pm UTC](https://discourse.julialang.org/t/purpose-of-zero-in-a-function/18095/4 "2018-11-28T13:12:58Z")

</div>

Good point. That is probably a bug, although if `y` and `mu` have the same eltype then it will not show. Why don’t you do a PR to fix it. If there is indeed a reason for it, then they will tell you there (please report back).

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [November 28, 2018, 1:19pm UTC](https://discourse.julialang.org/t/purpose-of-zero-in-a-function/18095/5 "2018-11-28T13:19:18Z")

</div>

> [@leejm516](#):
>
> Thank you for your answer… but still somewhat confusing.  
> Then, where is zero(eltype(mu)) in this function? In the loops below, elements in mu are used to calculate v, but in the initialization, zero(eltype(y)) appears twice.

That is probably just nonsense in `GLM.jl`. I’d guess `zero(eltype(r.y))` would be fine here: It is probably necessary to assume that the underlying `AbstractFloat` is closed under addition and multiplication and `abs2` anyway, and in case of heap-allocated big floats there is no reason to cause the 5 extra allocs. Not all packages are written in a perfectly idiomatic way.

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [November 29, 2018, 5:14pm UTC](https://discourse.julialang.org/t/purpose-of-zero-in-a-function/18095/6 "2018-11-29T17:14:10Z")

</div>

x-ref [Initialization of v in the deviance function · Issue #268 · JuliaStats/GLM.jl · GitHub](https://github.com/JuliaStats/GLM.jl/issues/268).

The 5 extra allocs of `BigFloat` are the least of your concerns if you just fitted a model on `BigFloat` data and are about to compute the deviance in `BigFloat`.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [November 29, 2018, 7:55pm UTC](https://discourse.julialang.org/t/purpose-of-zero-in-a-function/18095/7 "2018-11-29T19:55:22Z")

</div>

> [@foobar\_lv2](#):
>
> Not all packages are written in a perfectly idiomatic way.

Easy with the shade. Note that you could have just as easily armchair-quarterbacked a GLM.jl that had your proposed `zero(eltype(r.y))` instead if someone had come on discourse because it was slow with a non-closed numeric type. It’s hard to account for the quirks of every numeric type that folks will use in packages — so often authors try to strike a balance. It’s a pain in the butt to manually unroll the first two iterations of a loop, so these kinds of “good-enough” shortcuts are common and totally fine IMO (until, of course, its shown to be a significant problem, but Milan’s point is a good one about this being a drop in an ocean).

---

<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:** [November 29, 2018, 8:02pm UTC](https://discourse.julialang.org/t/purpose-of-zero-in-a-function/18095/8 "2018-11-29T20:02:13Z")

</div>

> [@mbauman](#):
>
> a non-closed numeric type

I know that the interface for `<:Real` isn’t even informally specified, but being closed under `+` (and `*`) is a reasonable expectation I guess. Is there a type which violates this?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [November 29, 2018, 8:03pm UTC](https://discourse.julialang.org/t/purpose-of-zero-in-a-function/18095/9 "2018-11-29T20:03:54Z")

</div>

> [@nalimilan](#):
>
> The 5 extra allocs of `BigFloat` are the least of your concerns if you just fitted a model on `BigFloat` data and are about to compute the deviance in `BigFloat` .

Unitful quantities change types upon multiplication. Not sure if the rest of this algorithm is unitful-safe, though.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [November 29, 2018, 8:28pm UTC](https://discourse.julialang.org/t/purpose-of-zero-in-a-function/18095/10 "2018-11-29T20:28:05Z")

</div>

> [@mbauman](#):
>
> Easy with the shade.

I’m not saying that `GLM.jl` is bad, or that I could do better. I apologize if I created that impression.

I’m just saying that “written more convoluted than it needs to be” is often a realistic explanation when wondering why something in a package works the way it does. Especially if there are no source comments explaining why something is not done in the obvious way.

Re types, we know from the signature that `r.y`, `r.mu` and `r.wts` are of the same type, which is `<:AbstractArray{<:AbstractFloat}`. Originally I wondered about type-stability for complex values (because `abs2` changes the type then), but these are excluded by the signature anyway; so my conclusion was “probably more complicated than it needs to be; otherwise someone should have written a source comment”.
