# Incorrect summation of Float64

**URL:** <https://discourse.julialang.org/t/incorrect-summation-of-float64/93376>\
**Category:** General Usage\
**Created:** [January 22, 2023, 10:56pm UTC](https://discourse.julialang.org/t/incorrect-summation-of-float64/93376 "2023-01-22T22:56:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-s-gardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-s-gardner/32/30210_2.png) [@alex-s-gardner](https://discourse.julialang.org/u/alex-s-gardner)\
**Post date:** [January 22, 2023, 10:56pm UTC](https://discourse.julialang.org/t/incorrect-summation-of-float64/93376/1 "2023-01-22T22:56:17Z")

</div>

Can others reproduce this? I’m getting a strange result when adding `0.1 +0.2`

```julia
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.8.2 (2022-09-29)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> 0.1 +0.1
0.2

julia> 0.1 +0.2
0.30000000000000004

julia> 0.2 +0.2
0.4

```

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [January 22, 2023, 10:58pm UTC](https://discourse.julialang.org/t/incorrect-summation-of-float64/93376/3 "2023-01-22T22:58:29Z")

</div>

This is the expected behavior for floating-point arithmetic: [https://0.30000000000000004.com/](https://0.30000000000000004.com/)

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [January 22, 2023, 11:02pm UTC](https://discourse.julialang.org/t/incorrect-summation-of-float64/93376/4 "2023-01-22T23:02:07Z")

</div>

> [@PSA: floating-point arithmetic](https://discourse.julialang.org/t/psa-floating-point-arithmetic/8678):
>
> Sometimes people are surprised by the results of floating-point calculations such as julia\> 5/6 0.8333333333333334 # shouldn't the last digit be 3? julia\> 2.6 - 0.7 - 1.9 2.220446049250313e-16 # shouldn't the answer be 0? These are not bugs in Julia. They’re consequences of the IEEE-standard 64-bit binary representation of floating-point numbers that is burned into computer hardware, which Julia and many other languages use by default. Brief explanation You can t…

---

<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:** [January 22, 2023, 11:03pm UTC](https://discourse.julialang.org/t/incorrect-summation-of-float64/93376/5 "2023-01-22T23:03:33Z")

</div>

I suspect that almost everyone who does computer programming has asked this question once in some forum😁 It comes up again and again.

Here’s one write up:

> [@PSA: floating-point arithmetic](https://discourse.julialang.org/t/psa-floating-point-arithmetic/8678):
>
> Sometimes people are surprised by the results of floating-point calculations such as julia\> 5/6 0.8333333333333334 # shouldn't the last digit be 3? julia\> 2.6 - 0.7 - 1.9 2.220446049250313e-16 # shouldn't the answer be 0? These are not bugs in Julia. They’re consequences of the IEEE-standard 64-bit binary representation of floating-point numbers that is burned into computer hardware, which Julia and many other languages use by default. Brief explanation You can t…

Hah. Ninja’ed by the author!

---

<div class="post-metadata">

**Author:** ![alex-s-gardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-s-gardner/32/30210_2.png) [@alex-s-gardner](https://discourse.julialang.org/u/alex-s-gardner)\
**Post date:** [January 22, 2023, 11:03pm UTC](https://discourse.julialang.org/t/incorrect-summation-of-float64/93376/6 "2023-01-22T23:03:34Z")

</div>

Thank you … I had no idea… rather fascinating

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [January 23, 2023, 2:40am UTC](https://discourse.julialang.org/t/incorrect-summation-of-float64/93376/7 "2023-01-23T02:40:29Z")

</div>

For even more fun, check out how tricky rounding is:

```julia
julia> (round(-0.5), round(0.5))
(-0.0, 0.0)

```

Python 3.10:

```python
>>> (round(-0.5), round(0.5))
(0, 0)

```

Python 2.7:

```python
>>> (round(-0.5), round(0.5))
(-1.0, 1.0)

```

Javascript (in Edge):

```javascript
> with(Math){ [round(-0.5), round(0.5)] }
< [-0, 1]

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 23, 2023, 5:30am UTC](https://discourse.julialang.org/t/incorrect-summation-of-float64/93376/8 "2023-01-23T05:30:22Z")

</div>

In Julia, we can access the distinct rounding modes.

```julia
julia> (round(Int, -0.5), round(Int, 0.5))
(0, 0)

julia> M = RoundToZero
RoundingMode{:ToZero}()

julia> (round(-0.5, M), round(0.5, M))
(-0.0, 0.0)

julia> M = RoundFromZero
RoundingMode{:FromZero}()

julia> (round(-0.5, M), round(0.5, M))
(-1.0, 1.0)

julia> M = RoundUp
RoundingMode{:Up}()

julia> (round(-0.5, M), round(0.5, M))
(-0.0, 1.0)

```

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [January 23, 2023, 10:41am UTC](https://discourse.julialang.org/t/incorrect-summation-of-float64/93376/9 "2023-01-23T10:41:15Z")

</div>

It’s completely correct (given it’s assumptions) for Float64/IEEE.

You can do accurately with rationals (in Julia, and in Raku, formerly Perl 6, rationals are the default number type), with:

```julia
julia> 1//10 + 2//10 # Rational math is slower
3//10

julia> Float64(1//10 + 2//10) # You can print out in a user-friendly fashion in the end, not sure it always applies, since bitstring(Float64(1//10 + 2//10))
0.3

```

All decimal numbers are also accurate with (this is slower than Float64, since it doesn’t have hardware support, though some systems have that, and then I think not with this package, though likely faster than with rationals):

> **[GitHub - JuliaMath/DecFP.jl: Julia IEEE decimal floating-point via the Intel...](https://github.com/JuliaMath/DecFP.jl)**
>
> Julia IEEE decimal floating-point via the Intel decimal-float library - GitHub - JuliaMath/DecFP.jl: Julia IEEE decimal floating-point via the Intel decimal-float library

Note, with it 0.3 is accurate, but e.g. 1/3 isn’t, then rationals are better).

julia\> big"0.1" + big"0.2" # BigFloat has same problem, but is closer to the truth than Float64 (slower than it or rationals):  
0.3000000000000000000000000000000000000000000000000000000000000000000000000000017

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [January 23, 2023, 10:51am UTC](https://discourse.julialang.org/t/incorrect-summation-of-float64/93376/10 "2023-01-23T10:51:15Z")

</div>

> [@uniment](#):
>
> For even more fun, check out how tricky rounding is:

That’s not tricky because of floating point numbers, there are simply different decisions taken because rounding isn’t unequivocally defined. That has nothing to do with floating point arithmetic, half integers are exactly represented by floating point numbers.
