Skip to content

Commit be01f9f

Browse files
committed
fix infinite printing
1 parent 21032e8 commit be01f9f

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/Quadmath.jl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -559,10 +559,19 @@ function parse(::Type{Float128}, s::AbstractString)
559559
end
560560

561561
function string(x::Float128)
562-
lng = 64
563-
buf = Array{UInt8}(undef, lng + 1)
564-
lng = @quad_ccall(libquadmath.quadmath_snprintf(buf::Ptr{UInt8}, (lng+1)::Csize_t, "%.35Qg"::Ptr{UInt8}, x::(Cfloat128...))::Cint)
565-
return String(resize!(buf, lng))
562+
if isfinite(x)
563+
lng = 64
564+
buf = Array{UInt8}(undef, lng + 1)
565+
lng = @quad_ccall(libquadmath.quadmath_snprintf(buf::Ptr{UInt8}, (lng+1)::Csize_t, "%.35Qg"::Ptr{UInt8}, x::(Cfloat128...))::Cint)
566+
return String(resize!(buf, lng))
567+
elseif x==Inf128
568+
return "Inf"
569+
elseif x==-Inf128
570+
return "-Inf"
571+
else
572+
return "NaN"
573+
end
574+
566575
end
567576

568577
function show(io::IO, x::Float128)

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ end
194194
show(iob, Float128(f))
195195
s = String(take!(iob))
196196
@test s == s_expected
197+
if !isfinite(f)
198+
@test string(Float128(f)) == string(f)
199+
end
197200
@test isequal(parse(Float128, string(Float128(f))), Float128(f))
198201
end
199202
@test parse(Float128,"3.0") == Float128(3.0)

0 commit comments

Comments
 (0)