# Printing exact decimal representations

**URL:** <https://discourse.julialang.org/t/printing-exact-decimal-representations/103365>\
**Category:** Numerics\
**Tags:** float, bigfloat\
**Created:** [August 29, 2023, 9:15pm UTC](https://discourse.julialang.org/t/printing-exact-decimal-representations/103365 "2023-08-29T21:15:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [August 29, 2023, 9:15pm UTC](https://discourse.julialang.org/t/printing-exact-decimal-representations/103365/1 "2023-08-29T21:15:17Z")

</div>

> [@Calculation of remainder, floating point issues](https://discourse.julialang.org/t/calculation-of-remainder-floating-point-issues/103338/8):
>
> I don’t remember off the top of my head if there is a function that always prints the exact decimal representation of a floating-point value, regardless of the precision and regardless of how many decimal digits are required?

I don’t think there is, but it would be great if there was one.

---

<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:** [August 30, 2023, 1:37am UTC](https://discourse.julialang.org/t/printing-exact-decimal-representations/103365/2 "2023-08-30T01:37:38Z")

</div>

> [@Calculation of remainder, floating point issues](https://discourse.julialang.org/t/calculation-of-remainder-floating-point-issues/103338/8):
>
> depends on the current precision setting

Not sure what you mean. Yes, the global setting may be used at construction time, but I don’t think it affects printing:

```julia-repl
julia> b = big(1.0)/3
0.3333333333333333333333333333333333333333333333333333333333333333333333333333348

julia> setprecision(BigFloat, 1)
1

julia> b
0.3333333333333333333333333333333333333333333333333333333333333333333333333333348

```

---

<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:** [August 30, 2023, 12:06pm UTC](https://discourse.julialang.org/t/printing-exact-decimal-representations/103365/3 "2023-08-30T12:06:36Z")

</div>

> [@nsajko](#):
>
> `julia> b`

Changing the precision doesn’t affect the variable `b` that you created previously. It will affect _new_ `BigFloat` variables and calculations:

```julia-auto
julia> setprecision(BigFloat, 1)
1

julia> big(1.0)/3 # rounds to 2^-2
0.25

julia> big(0.00625) # rounds to 2^-7
0.0078

```

If you want to print out all of the decimal digits of a `Float64` value via `BigFloat`, it will only work if the `BigFloat` precision is set sufficiently high.

The default precision (256 bits) is _often_ big enough to ensure exact decimal printing for `Float64`, if the magnitudes are not too extreme. Unfortunately, this is not always the case:

```julia-auto
julia> setprecision(256);

julia> exp2(-big"256")
8.636168555094444625386351862800399571116000364436281385023703470168591803162427e-78

julia> setprecision(4096);

julia> exp2(-big"256")
8.6361685550944446253863518628003995711160003644362813850237034701685918031624270579715075034722882265605472939461496635969950989468319466936530037770580747746862471103668212890625e-78

```

More digits are needed as the exponent gets bigger/smaller. Just trying things out empirically around `floatmin(Float64)` and `floatmax(Float64)`, it seems like **4096 bits** of precision is sufficient to show all of the decimal digits. (2048 is not enough!). You can also use `@printf("%0.1000e", x)`, but that prints a lot of trailing zeros.
