# Why do simple arithmetics cause weird Precision bugs

**URL:** <https://discourse.julialang.org/t/why-do-simple-arithmetics-cause-weird-precision-bugs/119218>\
**Category:** General Usage\
**Created:** [September 9, 2024, 4:18pm UTC](https://discourse.julialang.org/t/why-do-simple-arithmetics-cause-weird-precision-bugs/119218 "2024-09-09T16:18:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![The\_Mastermage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/the_mastermage/32/49444_2.png) [@The\_Mastermage](https://discourse.julialang.org/u/The_Mastermage)\
**Post date:** [September 9, 2024, 4:18pm UTC](https://discourse.julialang.org/t/why-do-simple-arithmetics-cause-weird-precision-bugs/119218/1 "2024-09-09T16:18:44Z")

</div>

Why does it happen when i do

```jl
295.8 - 360.0

```

that i am returned with

```julia
-64.19999999999999

```

I know this has something todo with Loss of Precission due to the way floating point numbers work. But i Find it a bit weird that this happens in such a “simple” calculation when my old Calculator can do it without loss of Precision.  
Can someone enlighten me here again. Its been a while since I did 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:** [September 9, 2024, 4:26pm UTC](https://discourse.julialang.org/t/why-do-simple-arithmetics-cause-weird-precision-bugs/119218/2 "2024-09-09T16:26:41Z")

</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…

In short, `295.8` is not the number you think it is. It’s actually 295.80000000000001136868377216160297393798828125:

```julia-auto
julia> using Printf; @printf "%.60f" 295.8
295.800000000000011368683772161602973937988281250000000000000000

```

`295.8` is used to input (and output) that number because there’s no other floating point number closer to the exact 295.8 than that one. So then when you do maths with that number, you can get further roundings away from what you’d otherwise expect.

To make this even more tangible, you can see that if you subtract `295` from the above _true value_, the answer must be 0.80000000000001136868377216160297393798828125, or the nearest floating point number that rounds to it:

```Julia
julia> 295.8-295
0.8000000000000114

```

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 9, 2024, 4:40pm UTC](https://discourse.julialang.org/t/why-do-simple-arithmetics-cause-weird-precision-bugs/119218/3 "2024-09-09T16:40:40Z")

</div>

I guess Julia is a million time faster than your old Calculator by default. If you want full precision for decimal number you can use the following package: [GitHub - JuliaMath/Decimals.jl: Pure Julia decimal arithmetic library.](https://github.com/JuliaMath/Decimals.jl) (and you lose a little bit of performance)

---

<div class="post-metadata">

**Author:** ![The\_Mastermage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/the_mastermage/32/49444_2.png) [@The\_Mastermage](https://discourse.julialang.org/u/The_Mastermage)\
**Post date:** [September 9, 2024, 5:00pm UTC](https://discourse.julialang.org/t/why-do-simple-arithmetics-cause-weird-precision-bugs/119218/4 "2024-09-09T17:00:03Z")

</div>

ah right thats how it was, its been way too long since I had this in University.

---

<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:** [September 9, 2024, 8:27pm UTC](https://discourse.julialang.org/t/why-do-simple-arithmetics-cause-weird-precision-bugs/119218/5 "2024-09-09T20:27:22Z")

</div>

Old calculators don’t actually do this any differently, they just do some rounding by default, which papers over most such issues. Doing that causes other issues in a programming language, however, which is why we don’t do that.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [September 9, 2024, 8:34pm UTC](https://discourse.julialang.org/t/why-do-simple-arithmetics-cause-weird-precision-bugs/119218/6 "2024-09-09T20:34:00Z")

</div>

The new calculator in Android has really innovated in this space. Hans-J Boehm (of garbage collector fame)'s [“Towards an API for the Real Numbers”](https://dl.acm.org/doi/pdf/10.1145/3385412.3386037) says

> We no longer receive bug reports about inaccurate results, as we  
> occasionally did for the 2014 floating-point-based calculator.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [September 9, 2024, 8:39pm UTC](https://discourse.julialang.org/t/why-do-simple-arithmetics-cause-weird-precision-bugs/119218/7 "2024-09-09T20:39:23Z")

</div>

But they use arbitrary precision for intermediate results which is prohibitive for Julia as default.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [September 9, 2024, 8:42pm UTC](https://discourse.julialang.org/t/why-do-simple-arithmetics-cause-weird-precision-bugs/119218/8 "2024-09-09T20:42:12Z")

</div>

Agreed.

---

<div class="post-metadata">

**Author:** ![The\_Mastermage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/the_mastermage/32/49444_2.png) [@The\_Mastermage](https://discourse.julialang.org/u/The_Mastermage)\
**Post date:** [September 10, 2024, 11:12am UTC](https://discourse.julialang.org/t/why-do-simple-arithmetics-cause-weird-precision-bugs/119218/9 "2024-09-10T11:12:11Z")

</div>

this is actualy realy interesting. One thing I always was wondering. Would it not also make sense that for (not too large) Numbers we can store the digits left of the decimal seperator in an integer and the numbers right of the decimal seperator as an Integer. And using a carry on flag. Essentialy turning the calculation into two integer Calculations?

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [September 10, 2024, 11:27am UTC](https://discourse.julialang.org/t/why-do-simple-arithmetics-cause-weird-precision-bugs/119218/10 "2024-09-10T11:27:03Z")

</div>

> [@The\_Mastermage](#):
>
> Would it not also make sense that for (not too large) Numbers we can store the digits left of the decimal seperator in an integer and the numbers right of the decimal seperator as an Integer.

You’re describing a variant on fixed-point representation. Fixed-point is what was used before we had floating-point. It’s still more or less widely used, but its applications are not as general-purpose as with floating-point.

> [@The\_Mastermage](#):
>
> Essentialy turning the calculation into two integer Calculations?

Keep in mind floating-point is sometimes also implemented using integer operations, when hardware support is missing.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [September 10, 2024, 11:44am UTC](https://discourse.julialang.org/t/why-do-simple-arithmetics-cause-weird-precision-bugs/119218/11 "2024-09-10T11:44:49Z")

</div>

> [@jar1](#):
>
> The new calculator in Android has really innovated in this space.

The approach they base their software on only works with a few hardcoded operations.

IMO the continued fraction-based approaches are more interesting. Some are referenced in the above paper. They all stem from one of R.W. Gosper’s HAKMEM notes: [HAKMEM -- CONTENTS -- DRAFT, NOT YET PROOFED](http://www.hakmem.org/#item101B). Couple that with a CAS for checking equality.
