# Why sum\_kbn can't sum generators?

**URL:** https://discourse.julialang.org/t/why-sum-kbn-cant-sum-generators/1717
**Category:** Internals & Design
**Tags:** proposal
**Created:** [January 27, 2017, 4:16pm UTC](https://discourse.julialang.org/t/why-sum-kbn-cant-sum-generators/1717 "2017-01-27T16:16:14Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [January 27, 2017, 4:16pm UTC](https://discourse.julialang.org/t/why-sum-kbn-cant-sum-generators/1717/1 "2017-01-27T16:16:14Z")

</div>

```julia
sum_kbn(i for i = 1:10)

```

raises a MethodError:

> ERROR: MethodError: no method matching sum\_kbn(::Base.Generator{UnitRange{Int64},##9#10})  
> Closest candidates are:  
> sum\_kbn{T\<:AbstractFloat}(::AbstractArray{T\<:AbstractFloat,N}) at reduce.jl:237

Is this intentional? Something about the KBN summation prevents it from applying to an iterator (without instantiating it into an array)?

---

<div class="post-metadata">

### Author: ![swt30](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swt30/32/4667_2.png) [@swt30](https://discourse.julialang.org/u/swt30)
#### Post date: [January 28, 2017, 1:04am UTC](https://discourse.julialang.org/t/why-sum-kbn-cant-sum-generators/1717/2 "2017-01-28T01:04:18Z")

</div>

The KBN sum is for floating point numbers and doesn’t provide any benefit for integers, so it looks like it’s not defined for them: for example, `sum_kbn([1,2,3])` also doesn’t work. Try using a floating-point range in your generator:

```julia
sum_kbn(i for i = 1.0:1.0:10.0)

```

---

<div class="post-metadata">

### Author: ![swt30](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swt30/32/4667_2.png) [@swt30](https://discourse.julialang.org/u/swt30)
#### Post date: [January 28, 2017, 8:45am UTC](https://discourse.julialang.org/t/why-sum-kbn-cant-sum-generators/1717/3 "2017-01-28T08:45:17Z")

</div>

I’ve just realised that this doesn’t actually work! I thought I’d tested it but I may have accidentally tested normal `sum`. So yes, you are right that the definition of `sum_kbn` doesn’t allow for iterators: if you do `methods(sum_kbn)` you will see that it’s only defined for floating-point arrays. Adding a more general method based on [the existing one](https://github.com/JuliaLang/julia/blob/953029fea4614b0ed60a317307386d38f568a5b4/base/reduce.jl#L363) which accepts an iterable collection would be a nice pull request.

---

<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: [January 29, 2017, 1:40am UTC](https://discourse.julialang.org/t/why-sum-kbn-cant-sum-generators/1717/4 "2017-01-29T01:40:30Z")

</div>

Yes, it seems like there should really be `sum_kbn(itr)` and `sum_kbn(f::Function, itr)` methods.

This function has not gotten a huge amount of attention, probably because the `sum` function is nearly as accurate, and much faster: it uses [pairwise summation](https://en.wikipedia.org/wiki/Pairwise_summation), which has O(sqrt(log n)) rms error growth, which in most cases is almost indistinguishable from the O(1) error growth of `sum_kbn`.

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [January 30, 2017, 1:02pm UTC](https://discourse.julialang.org/t/why-sum-kbn-cant-sum-generators/1717/5 "2017-01-30T13:02:51Z")

</div>

[https://github.com/JuliaLang/julia/issues/20323](https://github.com/JuliaLang/julia/issues/20323)

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [February 16, 2017, 8:53pm UTC](https://discourse.julialang.org/t/why-sum-kbn-cant-sum-generators/1717/6 "2017-02-16T20:53:31Z")

</div>

@stevengj`sum(itr)` can’t do pairwise summation, because that requires length. So `sum_kbn` is actually even more useful for iterators.
