Skip to content

Commit

Permalink
fix parse(ComplexF64, "inf")
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj committed Apr 12, 2019
1 parent 65f94b3 commit c36e70b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion base/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ function tryparse_internal(::Type{Complex{T}}, s::Union{String,SubString{String}
end

if i₊ == 0 # purely real or imaginary value
if iᵢ > 0 # purely imaginary
if iᵢ > i && !(iᵢ == i+1 && s[i] in ('+','-')) # purely imaginary (not "±inf")
x = tryparse_internal(T, s, i, iᵢ-1, raise)
x === nothing && return nothing
return Complex{T}(zero(x),x)
Expand Down
11 changes: 9 additions & 2 deletions test/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ end

# parsing complex numbers (#22250)
@testset "complex parsing" begin
for r in (1,0,-1), i in (1,0,-1), sign in ('-','+'), Im in ("i","j","im")
for s1 in (""," "), s2 in (""," "), s3 in (""," "), s4 in (""," ")
for sign in ('-','+'), Im in ("i","j","im"), s1 in (""," "), s2 in (""," "), s3 in (""," "), s4 in (""," ")
for r in (1,0,-1), i in (1,0,-1),
n = Complex(r, sign == '+' ? i : -i)
s = string(s1, r, s2, sign, s3, i, Im, s4)
@test n === parse(Complex{Int}, s)
Expand All @@ -293,6 +293,13 @@ end
@test n*parse(T,"1e-3") == parse(Complex{T}, string(s1, r, "e-3", s2, sign, s3, i, "e-3", Im, s4))
end
end
for r in (-1.0,-1e-9,Inf,-Inf,NaN), i in (-1.0,-1e-9,Inf,NaN)
n = Complex(r, sign == '+' ? i : -i)
s = lowercase(string(s1, r, s2, sign, s3, i, Im, s4))
@test n === parse(ComplexF64, s)
@test Complex(r) === parse(ComplexF64, string(s1, r, s2))
@test Complex(0,i) === parse(ComplexF64, string(s3, i, Im, s4))
end
end
@test parse(Complex{Float16}, "3.3+4i") === Complex{Float16}(3.3+4im)
@test parse(Complex{Int}, SubString("xxxxxx1+2imxxxx", 7, 10)) === 1+2im
Expand Down

0 comments on commit c36e70b

Please sign in to comment.