Printing exact decimal representations

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

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

julia> b = big(1.0)/3
0.3333333333333333333333333333333333333333333333333333333333333333333333333333348

julia> setprecision(BigFloat, 1)
1

julia> b
0.3333333333333333333333333333333333333333333333333333333333333333333333333333348

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

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> 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.

1 Like