Formatting.jl
Formatting.jl copied to clipboard
printfmt("1:.4e, 40000.0) prints 3.10000e+04
printfmt("1:.4e, 40000.0)
should print 4.0000e+04
, but it prints 3.10000e+04
.
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
Julia 1.0.0 (2018-08-8) and Formatting 0.3.4, running on Windows 7.
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.
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
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.)
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")