Skip to content

Commit

Permalink
Fix issue #63 - incorrect results with python format e
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottPJones committed Jan 30, 2024
1 parent 88dff5c commit d116e94
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/fmtcore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,14 @@ function _pfmt_e(out::IO, fs::FormatSpec, x::AbstractFloat)
else
rax = round(ax; sigdigits = fs.prec + 1)
e = floor(Integer, log10(rax)) # exponent
u = rax * exp10(-e) # significand
u = round(rax * exp10(-e); sigdigits = fs.prec + 1) # significand
i = 0
v10 = 1
while isinf(u)
i += 1
i > 18 && (u = 0.0; e = 0; break)
v10 *= 10
u = v10 * rax * exp10(-e - i)
u = round(v10 * rax * exp10(-e - i); sigdigits = fs.prec + 1)
end
end

Expand Down
6 changes: 5 additions & 1 deletion test/fmtspec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ end
# Issue #110 (in Formatting.jl)
f = FormatExpr("{:+d}")
for T in (Int8, Int16, Int32, Int64, Int128)
@test format(f, typemin(T)) = string(typemin(T))
@test format(f, typemin(T)) == string(typemin(T))
end
end

Expand Down Expand Up @@ -257,6 +257,9 @@ end
@test pyfmt("+11.3e", 1.0e-309) == "+1.000e-309"
@test pyfmt("+11.3e", 1.0e-313) == "+1.000e-313"

# issue #108 (from Formatting.jl)
@test pyfmt(".1e", 0.0003) == "3.0e-04"
@test pyfmt(".1e", 0.0006) == "6.0e-04"
end

@testset "Format special floating point value" begin
Expand All @@ -279,6 +282,7 @@ end
@test pyfmt("<5f", Inf) == "Inf "
@test pyfmt("^5f", Inf) == " Inf "
@test pyfmt(">5f", Inf) == " Inf"

@test pyfmt("*<5f", Inf) == "Inf**"
@test pyfmt("⋆<5f", Inf) == "Inf⋆⋆"
@test pyfmt("*^5f", Inf) == "*Inf*"
Expand Down

0 comments on commit d116e94

Please sign in to comment.