From a2a6d182dc27db943c886c4b863540d0f0981bd2 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Wed, 10 May 2017 10:50:51 -0500 Subject: [PATCH] Implement tryparse(Float16, str) via Float32 conversion --- base/parse.jl | 2 ++ test/parse.jl | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/base/parse.jl b/base/parse.jl index 27c00e36dcd07..754d670f9d0b8 100644 --- a/base/parse.jl +++ b/base/parse.jl @@ -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) diff --git a/test/parse.jl b/test/parse.jl index 5716c17491687..df2f38cfbd36f 100644 --- a/test/parse.jl +++ b/test/parse.jl @@ -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)