Formatting.jl icon indicating copy to clipboard operation
Formatting.jl copied to clipboard

printfmt("1:.4e, 40000.0) prints 3.10000e+04

Open mohamedmoussa89 opened this issue 6 years ago • 6 comments

printfmt("1:.4e, 40000.0) should print 4.0000e+04, but it prints 3.10000e+04.

mohamedmoussa89 avatar Oct 23 '18 06:10 mohamedmoussa89

What version of Julia and Formatting are you using? With Julia 1.0.0 and Formatting 0.3.4 I get:

julia> printfmt("{1:.4e}", 40000.0)
4.0000e+04

jmkuhn avatar Oct 23 '18 18:10 jmkuhn

Julia 1.0.0 (2018-08-8) and Formatting 0.3.4, running on Windows 7. julia_bug

mohamedmoussa89 avatar Oct 23 '18 22:10 mohamedmoussa89

What do you get for the following?

julia> printfmt("{1:.4e}", 39999.9)
4.0000e+04
julia> printfmt("{1:.4e}", 40000.1)
4.0000e+04

I don't have access to a Windows 7 machine but it seems odd that this would be Windows specific.

jmkuhn avatar Oct 24 '18 13:10 jmkuhn

julia> Formatting.printfmt("{1:.4e}",40000.0)
3.10000e+04
julia> Formatting.printfmt("{1:.4e}",40000.1)
3.10000e+04
julia> Formatting.printfmt("{1:.4e}",39999.0)
3.9999e+04

mohamedmoussa89 avatar Oct 25 '18 06:10 mohamedmoussa89

A similar issue was reported on discourse:

julia> using Formatting

julia> format("{1:.4e}", 3.0e-8)
"2.10000e-08"

This is with Formatting v0.4.2 and Julia 1.6.1 on macOS.

(The printfmt("{1:.4e}",40000.0) example from above works fine for me.)

stevengj avatar Jul 06 '21 19:07 stevengj

In general, I get lots of erroneous results for formatting numbers that are approximately integers times negative powers of ten:

julia> for i = 1:1000
           x = (rand(Bool) ? +1 : -1) * rand(1:9) * exp10(rand(-10:10))
           s = format("{1:.4e}", x)
           if !isapprox(x, parse(Float64, s), rtol=1e-3)
               @show x, s
           end
       end
(x, s) = (0.0006000000000000001, "5.10000e-04")
(x, s) = (-7.000000000000001e-5, "-6.10000e-05")
(x, s) = (0.0006000000000000001, "5.10000e-04")
(x, s) = (-0.00030000000000000003, "-2.10000e-04")
(x, s) = (0.0006000000000000001, "5.10000e-04")
(x, s) = (6.000000000000001e-8, "5.10000e-08")
(x, s) = (3.0000000000000004e-8, "2.10000e-08")
(x, s) = (-6.000000000000001e-8, "-5.10000e-08")
(x, s) = (7.000000000000001e-5, "6.10000e-05")
(x, s) = (-0.0006000000000000001, "-5.10000e-04")
(x, s) = (-3.0000000000000004e-8, "-2.10000e-08")
(x, s) = (-0.00030000000000000003, "-2.10000e-04")
(x, s) = (-3.0000000000000004e-8, "-2.10000e-08")
(x, s) = (6.000000000000001e-8, "5.10000e-08")
(x, s) = (-0.0006000000000000001, "-5.10000e-04")
(x, s) = (7.000000000000001e-5, "6.10000e-05")
(x, s) = (3.0000000000000004e-8, "2.10000e-08")
(x, s) = (-3.0000000000000004e-8, "-2.10000e-08")
(x, s) = (-3.0000000000000004e-8, "-2.10000e-08")
(x, s) = (3.0000000000000004e-8, "2.10000e-08")
(x, s) = (-3.0000000000000004e-8, "-2.10000e-08")
(x, s) = (-0.0006000000000000001, "-5.10000e-04")
(x, s) = (0.00030000000000000003, "2.10000e-04")
(x, s) = (0.0006000000000000001, "5.10000e-04")

stevengj avatar Jul 06 '21 20:07 stevengj