Skip to content

Commit

Permalink
Float16: convert to various number types; round, trunc, floor, ceil.
Browse files Browse the repository at this point in the history
These additional operations are needed to get the random and linalg
tests closer to passing for the FloatRange change [#5636]. We really
need to make a decision about how first-class we want Float16 to be.
I'm starting to suspect that we should just bite the bullet and make
all the things that work for other floating-point types work for the
Float16 type also. It's just easier than having them be half-broken
and try to explain that they're "just for storage".
  • Loading branch information
StefanKarpinski committed Feb 24, 2014
1 parent 46fa6ea commit aa724dc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
10 changes: 10 additions & 0 deletions base/float16.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ function convert(::Type{Float16}, val::Float32)
reinterpret(Float16, h)
end

for T in (Bool, Int128, Uint128, Complex, Rational)
@eval convert(::Type{$T}, x::Float16) = convert($T,float32(x))

This comment has been minimized.

Copy link
@JeffBezanson

JeffBezanson Feb 24, 2014

Member

Converting to Bool and Complex works without this patch. And I believe this patch will make convert(Complex, float16(x)) give a Complex64 instead of a Complex{Float16}. Not sure we want that.

This comment has been minimized.

Copy link
@JeffBezanson

JeffBezanson Feb 24, 2014

Member

e.g.

julia> convert(Complex, float16(2.1))
2.0996094f0 + 0.0f0im

julia> complex(float16(2.1))
float16(2.1) + float16(0.0)im

This comment has been minimized.

Copy link
@StefanKarpinski

StefanKarpinski Feb 24, 2014

Author Member

Ah, sorry about that. Good catch.

end
convert{T<:Number}(::Type{T}, x::Float16) = convert(T,float32(x))

round(x::Float16) = float16(round(float32(x)))
trunc(x::Float16) = float16(trunc(float32(x)))
floor(x::Float16) = float16(floor(float32(x)))
ceil(x::Float16) = float16( ceil(float32(x)))

isnan(x::Float16) = reinterpret(Uint16,x)&0x7fff > 0x7c00
isinf(x::Float16) = reinterpret(Uint16,x)&0x7fff == 0x7c00
isfinite(x::Float16) = reinterpret(Uint16,x)&0x7c00 != 0x7c00
Expand Down
3 changes: 2 additions & 1 deletion base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ BigFloat(x::Integer) = BigFloat(BigInt(x))
BigFloat(x::Union(Bool,Int8,Int16,Int32)) = BigFloat(convert(Clong,x))
BigFloat(x::Union(Uint8,Uint16,Uint32)) = BigFloat(convert(Culong,x))

BigFloat(x::Union(Float16,Float32)) = BigFloat(float64(x))
BigFloat(x::Union(Float16,Float32,Float16)) = BigFloat(float64(x))
BigFloat(x::Rational) = BigFloat(num(x)) / BigFloat(den(x))

convert(::Type{Rational}, x::BigFloat) = convert(Rational{BigInt}, x)
convert(::Type{BigFloat}, x::Rational) = BigFloat(x) # to resolve ambiguity
convert(::Type{BigFloat}, x::Float16) = BigFloat(x) # to resolve ambiguity
convert(::Type{BigFloat}, x::Real) = BigFloat(x)
convert(::Type{FloatingPoint}, x::BigInt) = BigFloat(x)

Expand Down

0 comments on commit aa724dc

Please sign in to comment.