Working with machine precision

Have you seen http://0.30000000000000004.com/?

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

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

4 Likes