# "sum(\[1.0, 1e100, 1.0, -1e100\])" fails

**URL:** https://discourse.julialang.org/t/sum-1-0-1e100-1-0-1e100-fails/76502
**Category:** General Usage
**Tags:** question, numbers
**Created:** [February 15, 2022, 6:37pm UTC](https://discourse.julialang.org/t/sum-1-0-1e100-1-0-1e100-fails/76502 "2022-02-15T18:37:01Z")
**Posts on this page:** 1
**Showing post:** 10

<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: [February 15, 2022, 9:11pm UTC](https://discourse.julialang.org/t/sum-1-0-1e100-1-0-1e100-fails/76502/10 "2022-02-15T21:11:29Z")

</div>

In general, this is not a language issue. Python is not “failing”, nor is Julia. You’ll get exactly the same thing if you just use `+` in _any_ language using double-precision floating-point arithmetic (the default precision in most languages).

As another example, `sum([1e-100, 1.0, 1e-100, -1.0])` also gives `0.0` — it is exactly the same problem, just rescaled, in which the exactly rounded answer is `2e-100`. Is this a big error or a small error?

The key question is, _compared to what_.

- It is a big error (100%) compared to the correct sum (i.e. the relative forward error). That’s because we are computing an _ill-conditioned_ function (a sum with a [catastrophic cancellation](https://en.wikipedia.org/wiki/Catastrophic_cancellation)), so the relative error in the output is large even for tiny roundoff errors. You have to be careful when doing _anything_ ill-conditioned in finite precision.

- It is a small error compared to the magnitude of the _inputs_, i.e. the typical scale of the problem we are giving it (`1e100` in your example, `1.0` in mine). A more precise way of saying this is that the “backwards relative error” is small.

Summation is the easiest case to analyze, and the easiest for which you can write exactly rounded algorithms like `fsum` or `xsum` that do the computation in effectively infinite precision, as well as nearly perfect algorithms like compensated summation. But this simplicity is a two-edged sword — it also means that summation perhaps gets disproportionate attention, when in fact ill-conditioning and similar problems are something you have to be aware for _anything_ in finite-precision arithmetic.

---

_[View the full topic](https://discourse.julialang.org/t/sum-1-0-1e100-1-0-1e100-fails/76502)._
