Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

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

Open
mohamedmoussa89 opened this issue Oct 23, 2018 · 6 comments
Open

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

mohamedmoussa89 opened this issue Oct 23, 2018 · 6 comments

Comments

@mohamedmoussa89
Copy link

mohamedmoussa89 commented Oct 23, 2018

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

@jmkuhn
Copy link
Contributor

jmkuhn commented Oct 23, 2018

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

@mohamedmoussa89
Copy link
Author

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

@jmkuhn
Copy link
Contributor

jmkuhn commented Oct 24, 2018

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.

@mohamedmoussa89
Copy link
Author

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

@stevengj
Copy link
Member

stevengj commented Jul 6, 2021

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
Copy link
Member

stevengj commented Jul 6, 2021

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")

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants