Skip to content

Commit

Permalink
fix printing of Float32s (#33909)
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Apr 11, 2020
1 parent 96391cb commit 2ea0375
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
8 changes: 4 additions & 4 deletions base/ryu/Ryu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,23 @@ function writeexp(x::T,
return String(resize!(buf, pos - 1))
end

function Base.show(io::IO, x::T, forceuntyped::Bool=false) where {T <: Base.IEEEFloat}
function Base.show(io::IO, x::T, forceuntyped::Bool=false, fromprint::Bool=false) where {T <: Base.IEEEFloat}
compact = get(io, :compact, false)
buf = Base.StringVector(neededdigits(T))
typed = !forceuntyped && !compact && get(io, :typeinfo, Any) != typeof(x)
pos = writeshortest(buf, 1, x, false, false, true, -1,
x isa Float32 ? UInt8('f') : UInt8('e'), false, UInt8('.'), typed, compact)
(x isa Float32 && !fromprint) ? UInt8('f') : UInt8('e'), false, UInt8('.'), typed, compact)
write(io, resize!(buf, pos - 1))
return
end

function Base.string(x::T) where {T <: Base.IEEEFloat}
buf = Base.StringVector(neededdigits(T))
pos = writeshortest(buf, 1, x, false, false, true, -1,
x isa Float32 ? UInt8('f') : UInt8('e'), false, UInt8('.'), false, false)
UInt8('e'), false, UInt8('.'), false, false)
return String(resize!(buf, pos - 1))
end

Base.print(io::IO, x::Union{Float16, Float32}) = show(io, x, true)
Base.print(io::IO, x::Union{Float16, Float32}) = show(io, x, true, true)

end # module
3 changes: 2 additions & 1 deletion test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,8 @@ test_repr("(:).a")
@test isa(repr("text/plain", String(UInt8[0x00:0xff;])), String)

# don't use julia-specific `f` in Float32 printing (PR #18053)
@test sprint(print, 1f-7) == "1.0f-7"
@test sprint(print, 1f-7) == "1.0e-7"
@test string(1f-7) == "1.0e-7"

let d = TextDisplay(IOBuffer())
@test_throws MethodError display(d, "text/foobar", [3 1 4])
Expand Down

0 comments on commit 2ea0375

Please sign in to comment.