# What is the meaning of \`total = f\_value + zero(f\_value)\`?\`

**URL:** <https://discourse.julialang.org/t/what-is-the-meaning-of-total-f-value-zero-f-value/1156>\
**Category:** Internals & Design\
**Created:** [December 26, 2016, 6:22pm UTC](https://discourse.julialang.org/t/what-is-the-meaning-of-total-f-value-zero-f-value/1156 "2016-12-26T18:22:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [December 26, 2016, 6:22pm UTC](https://discourse.julialang.org/t/what-is-the-meaning-of-total-f-value-zero-f-value/1156/1 "2016-12-26T18:22:41Z")

</div>

What is the reason to add `zero(f_value)` in line [https://github.com/JuliaLang/julia/blob/071c60a32773edc1e0d0d50c0aa1c138f4f7a36f/base/statistics.jl#L18](https://github.com/JuliaLang/julia/blob/071c60a32773edc1e0d0d50c0aa1c138f4f7a36f/base/statistics.jl#L18)

Does it not evaluate to `f_value` without any change of type?

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [December 26, 2016, 10:21pm UTC](https://discourse.julialang.org/t/what-is-the-meaning-of-total-f-value-zero-f-value/1156/2 "2016-12-26T22:21:12Z")

</div>

I think that code simply got edited, and the original reason for having a zero(f\_value) got lost.  
I think that code could be done more simply as follows:

```julia
function mean(f::Callable, iterable)
    state = start(iterable)
    done(iterable, state) &&
        throw(ArgumentError("mean of empty collection undefined: $(repr(iterable))"))
    count = 0
    total = zero(f_value)
    while true
        value, state = next(iterable, state)
        total += f(value)
        count += 1
        done(iterable, state) && return total/count
    end
end

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [December 26, 2016, 10:57pm UTC](https://discourse.julialang.org/t/what-is-the-meaning-of-total-f-value-zero-f-value/1156/3 "2016-12-26T22:57:50Z")

</div>

Perhaps for type stability when `Bool` iterators are passed?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 26, 2016, 10:58pm UTC](https://discourse.julialang.org/t/what-is-the-meaning-of-total-f-value-zero-f-value/1156/4 "2016-12-26T22:58:56Z")

</div>

Probably it was for type stability, in case `typeof(f_value + zero(f_value)) != typeof(f_value)`. If that happens, initializing `total = f_value` would cause the variable `total` to change type in the subsequent loop, leading to slow code.

In older versions of Julia, arithmetic with narrower integer types would automatically be widened to `Int`, leading to type instabilities if you weren’t careful in this way. Nowadays, the only built-in type for which `+` returns a different type is probably `Bool`: `true + false === 1`.

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [December 27, 2016, 4:33pm UTC](https://discourse.julialang.org/t/what-is-the-meaning-of-total-f-value-zero-f-value/1156/5 "2016-12-27T16:33:02Z")

</div>

> [@ScottPJones](#):
>
> total = zero(f\_value)

`f_value` does not seem to be defined.

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [December 27, 2016, 4:36pm UTC](https://discourse.julialang.org/t/what-is-the-meaning-of-total-f-value-zero-f-value/1156/6 "2016-12-27T16:36:39Z")

</div>

Does it even make sense to compute the average of booleans?

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [December 27, 2016, 4:37pm UTC](https://discourse.julialang.org/t/what-is-the-meaning-of-total-f-value-zero-f-value/1156/7 "2016-12-27T16:37:22Z")

</div>

Ah, yes! My bad, so the code does need to get the first value from the iterator and evaluate f(value) to get the initial value for total (but it doesn’t need zero() then).  
I can imagine cases where you might want to know the ratio of trues / total, so you’d get 0.0 - 1.0 for a result.  
If that f\_value + zero(f\_value) is really needed though to get the correct type for the loop, that line should be commented so that people don’t stumble upon it in the future, or have the intent made clearer.  
If narrower integer types are no longer automatically widened to Int, this code might be breaking now for small integer types, since `total` really needs to be able to hold `N*typemax(typeof(f_value))` without overflowing (where N is the [unknown] number of values returned by the iterator.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 28, 2016, 12:04am UTC](https://discourse.julialang.org/t/what-is-the-meaning-of-total-f-value-zero-f-value/1156/8 "2016-12-28T00:04:59Z")

</div>

The average of booleans is the fraction that are true, so yes, it makes sense and is one of the most basic things you want to know about a collection of booleans.
