From c29e190bbd47b095a24bbffd7b2f26b7ee52bf69 Mon Sep 17 00:00:00 2001 From: Erik Schnetter Date: Mon, 19 Jan 2015 15:29:33 -0500 Subject: [PATCH] Add `convert(Float16, BigFloat)` --- base/mpfr.jl | 5 +++++ test/mpfr.jl | 1 + 2 files changed, 6 insertions(+) diff --git a/base/mpfr.jl b/base/mpfr.jl index 607e6d4294174..091797268e825 100644 --- a/base/mpfr.jl +++ b/base/mpfr.jl @@ -174,11 +174,16 @@ convert(::Type{Float64}, x::BigFloat) = ccall((:mpfr_get_d,:libmpfr), Float64, (Ptr{BigFloat},Int32), &x, ROUNDING_MODE[end]) convert(::Type{Float32}, x::BigFloat) = ccall((:mpfr_get_flt,:libmpfr), Float32, (Ptr{BigFloat},Int32), &x, ROUNDING_MODE[end]) +# TODO: avoid double rounding +convert(::Type{Float16}, x::BigFloat) = convert(Float16, convert(Float32, x)) call(::Type{Float64}, x::BigFloat, r::RoundingMode) = ccall((:mpfr_get_d,:libmpfr), Float64, (Ptr{BigFloat},Int32), &x, to_mpfr(r)) call(::Type{Float32}, x::BigFloat, r::RoundingMode) = ccall((:mpfr_get_flt,:libmpfr), Float32, (Ptr{BigFloat},Int32), &x, to_mpfr(r)) +# TODO: avoid double rounding +call(::Type{Float16}, x::BigFloat, r::RoundingMode) = + convert(Float16, call(Float32, x, r)) promote_rule{T<:Real}(::Type{BigFloat}, ::Type{T}) = BigFloat promote_rule{T<:FloatingPoint}(::Type{BigInt},::Type{T}) = BigFloat diff --git a/test/mpfr.jl b/test/mpfr.jl index 613e5a90044c9..4ed130a5abf32 100644 --- a/test/mpfr.jl +++ b/test/mpfr.jl @@ -149,6 +149,7 @@ y = BigFloat(1) # convert from @test convert(Float64, BigFloat(0.5)) == 0.5 @test convert(Float32, BigFloat(0.5)) == float32(0.5) +@test convert(Float16, BigFloat(0.5)) == float16(0.5) # exponent x = BigFloat(0)