Skip to content

Commit

Permalink
Implement tryparse(Float16, str) via Float32 conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed May 10, 2017
1 parent a994a0c commit a2a6d18
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
2 changes: 2 additions & 0 deletions base/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ tryparse(::Type{Float32}, s::SubString{String}) = ccall(:jl_try_substrtof, Nulla

tryparse(::Type{T}, s::AbstractString) where {T<:Union{Float32,Float64}} = tryparse(T, String(s))

tryparse(::Type{Float16}, s::AbstractString) = convert(Nullable{Float16}, tryparse(Float32, s))

function parse(::Type{T}, s::AbstractString) where T<:AbstractFloat
result = tryparse(T, s)
if isnull(result)
Expand Down
4 changes: 4 additions & 0 deletions test/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,10 @@ end
# error throwing branch from #10560
@test_throws ArgumentError Base.tryparse_internal(Bool, "foo", 1, 2, 10, true)

@test tryparse(Float64, "1.23") === Nullable(1.23)
@test tryparse(Float32, "1.23") === Nullable(1.23f0)
@test tryparse(Float16, "1.23") === Nullable(Float16(1.23))

# PR #17393
for op in (:.==, :.&, :.|, :.≤)
@test parse("a $op b") == Expr(:call, op, :a, :b)
Expand Down

0 comments on commit a2a6d18

Please sign in to comment.