Skip to content

Commit

Permalink
Merge pull request JuliaLang#9844 from eschnett/mpfr-float16
Browse files Browse the repository at this point in the history
Add `convert(Float16, BigFloat)`
  • Loading branch information
ivarne committed Jan 22, 2015
2 parents bb05118 + c29e190 commit 180a6c5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
5 changes: 5 additions & 0 deletions base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 180a6c5

Please sign in to comment.