# Working with machine precision

**URL:** <https://discourse.julialang.org/t/working-with-machine-precision/29576>\
**Category:** General Usage\
**Created:** [October 7, 2019, 7:49am UTC](https://discourse.julialang.org/t/working-with-machine-precision/29576 "2019-10-07T07:49:35Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![aahaselgrove](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aahaselgrove/32/9839_2.png) [@aahaselgrove](https://discourse.julialang.org/u/aahaselgrove)\
**Post date:** [October 7, 2019, 7:49am UTC](https://discourse.julialang.org/t/working-with-machine-precision/29576/1 "2019-10-07T07:49:35Z")

</div>

If I execute `3*0.1`, Julia prints the value as `0.30000000000000004`. I believe the inaccuracy is caused by machine precision, but it doesn’t seem useful to me that Julia would display this. I also note that I have never encountered this kind of issue working with numbers greater than 1. What’s the cause of this issue, and what’s the best way around it?

---

<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:** [October 7, 2019, 7:53am UTC](https://discourse.julialang.org/t/working-with-machine-precision/29576/2 "2019-10-07T07:53:22Z")

</div>

> [@aahaselgrove](#):
>
> but it doesn’t seem useful to me that Julia would display this.

It is hard for Julia to know that you don’t want the result printed as accurately as possible. You can use the `round` function if you want to show fewer digits of precision. There is also the PR [https://github.com/JuliaLang/julia/pull/29249](https://github.com/JuliaLang/julia/pull/29249) to allow showing things in the REPL in a “compact” version which would end up being printed as:

```julia
julia> sprint(show, 0.1*3, context=:compact=>true)
"0.3"

```

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [October 7, 2019, 8:15am UTC](https://discourse.julialang.org/t/working-with-machine-precision/29576/3 "2019-10-07T08:15:05Z")

</div>

Well `round` does only work raliable in the windows built, see [this thread](https://discourse.julialang.org/t/round-can-anybody-explain-this-behavior-to-me/29230/17)

---

<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:** [October 7, 2019, 8:18am UTC](https://discourse.julialang.org/t/working-with-machine-precision/29576/4 "2019-10-07T08:18:29Z")

</div>

This was written in the context of “I only want to see a few digits and I don’t care about exact accuracy”.

---

<div class="post-metadata">

**Author:** ![aahaselgrove](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aahaselgrove/32/9839_2.png) [@aahaselgrove](https://discourse.julialang.org/u/aahaselgrove)\
**Post date:** [October 7, 2019, 8:22am UTC](https://discourse.julialang.org/t/working-with-machine-precision/29576/5 "2019-10-07T08:22:53Z")

</div>

I’d question the definition of exact accuracy - clearly, the exact solution is 0.3.

Also, 0.4 cannot be represented exactly, but `(0.1 + 0.1 + 0.1) + 0.1` produces `0.4`. Not quite sure where the 4e-16 has gone, as I would have assumed numerical inaccuracies would compound.

---

<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:** [October 7, 2019, 8:25am UTC](https://discourse.julialang.org/t/working-with-machine-precision/29576/6 "2019-10-07T08:25:57Z")

</div>

> [@aahaselgrove](#):
>
> I’d question the definition of exact accuracy - clearly, the exact solution is 0.3.

Please read through [PSA: floating-point arithmetic](https://discourse.julialang.org/t/psa-floating-point-arithmetic/8678) and see if you have further questions.

---

<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:** [October 7, 2019, 11:40am UTC](https://discourse.julialang.org/t/working-with-machine-precision/29576/7 "2019-10-07T11:40:03Z")

</div>

I find it weird that executing `3/10` prints `0.3` but executing `3 * 0.1` prints `0.30000000000000004`.

---

<div class="post-metadata">

**Author:** ![aahaselgrove](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aahaselgrove/32/9839_2.png) [@aahaselgrove](https://discourse.julialang.org/u/aahaselgrove)\
**Post date:** [October 7, 2019, 11:46am UTC](https://discourse.julialang.org/t/working-with-machine-precision/29576/8 "2019-10-07T11:46:41Z")

</div>

I understand how floating-point arithmetic works. The examples given there demonstrate catastrophic cancellation, and the result of all the calculations is equal to eps(). The article gives this example to demonstrate that all these calculations are performed in the same way. However, the examples which I am demonstrating can’t be replicated in other languages.

Eg. (in C, what I would expect)

```julia
printf("%1.16e\n", .3);
3.0000000000000004

printf("%1.16e\n", .1 + .1 + .1);
3.0000000000000004

```

Eg. (in Julia)

```julia
> .3
0.3
> .1 + .1 + .1
0.30000000000000004

```

---

<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:** [October 7, 2019, 12:13pm UTC](https://discourse.julialang.org/t/working-with-machine-precision/29576/9 "2019-10-07T12:13:51Z")

</div>

Have you seen [http://0.30000000000000004.com/](http://0.30000000000000004.com/)?

> [@aahaselgrove](#):
>
> Eg. (in C, what I would expect)

That seems to print 2.9999999999999999e-01 (which Julia also does with `@printf`):

```julia
julia> using Printf

julia> @printf "%1.16e\n" .3
2.9999999999999999e-01

```

Anyway, Julia uses the GRISU (or Ryu) algorithm to print the float. It guarantees that the generated strings can be parsed back to the original number while minimizing the number of digits.

And indeed:

```julia
julia> 2.9999999999999999e-01
0.3

julia> 0.3
0.3

```

so printing `0.3` is shorter here and thus preferable. But in the original example, `0.3` and `0.30000000000000004` are different floating point numbers and it would thus be an error to print `0.3` since it doesn’t parse back to the original number.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 7, 2019, 12:29pm UTC](https://discourse.julialang.org/t/working-with-machine-precision/29576/10 "2019-10-07T12:29:17Z")

</div>

> [@aahaselgrove](#):
>
> I understand how floating-point arithmetic works.

Are you sure? Cf

> [@aahaselgrove](#):
>
> Eg. (in C, what I would expect)
> 
> ```julia
> printf("%1.16e\n", .3);
> 3.0000000000000004
> 
> ```

which is incorrect.

> [@aahaselgrove](#):
>
> However, the examples which I am demonstrating can’t be replicated in other languages.

When working with 64-bit IEEE floats, _all_ languages would perform these operations the same way, they may just print the results differently. Eg R would do

```R

> z = 0.1 * 3
> z
[1] 0.3
> z - 0.3
[1] 5.551115e-17

```

Julia made the choice of printing floating values which are not equal differently. This is pretty much a given at this point and unlikely to change.

As for the workarounds, you can round the numbers before printing (`round`), or just print rounded values using the standard library (`Printf.sprint` & friends), or

> **[GitHub - JuliaIO/Formatting.jl: A Julia package to provide Python-like...](https://github.com/JuliaIO/Formatting.jl)**
>
> A Julia package to provide Python-like formatting support - GitHub - JuliaIO/Formatting.jl: A Julia package to provide Python-like formatting support

or some other package (there are many), use decimal arithmetic

> **[GitHub - JuliaMath/Decimals.jl: Pure Julia decimal arithmetic library.](https://github.com/JuliaMath/Decimals.jl)**
>
> Pure Julia decimal arithmetic library. Contribute to JuliaMath/Decimals.jl development by creating an account on GitHub.

or the built-in rational type `3//10`.

---

<div class="post-metadata">

**Author:** ![aahaselgrove](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aahaselgrove/32/9839_2.png) [@aahaselgrove](https://discourse.julialang.org/u/aahaselgrove)\
**Post date:** [October 8, 2019, 6:54am UTC](https://discourse.julialang.org/t/working-with-machine-precision/29576/11 "2019-10-08T06:54:54Z")

</div>

> [@kristoffer.carlsson](#):
>
> That seems to print 2.9999999999999999e-01 (which Julia also does with `@printf` ):

Must have missed a compile, that put me off track \>.\<.

> [@kristoffer.carlsson](#):
>
> Anyway, Julia uses the GRISU (or Ryu) algorithm to print the float. It guarantees that the generated strings can be parsed back to the original number while minimizing the number of digits.

This is what I was missing. I’d suggest it would be useful to add this to the floating-point PSA referenced above.
