Skip to content

Commit

Permalink
Add Float16 comparisons (#29916)
Browse files Browse the repository at this point in the history
* Add Float16 comparisons

* Add @eval

* Add union

* Add != to tests

(cherry picked from commit 1d3c371)
  • Loading branch information
sam0410 authored and KristofferC committed Feb 20, 2020
1 parent 26525a2 commit 6cb86ad
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
17 changes: 10 additions & 7 deletions base/float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -503,15 +503,18 @@ for Ti in (Int64,UInt64,Int128,UInt128)
end
end
end
for op in (:(==), :<, :<=)
@eval begin
($op)(x::Float16, y::Union{Int128,UInt128,Int64,UInt64}) = ($op)(Float64(x), Float64(y))
($op)(x::Union{Int128,UInt128,Int64,UInt64}, y::Float16) = ($op)(Float64(x), Float64(y))

==(x::Float32, y::Union{Int32,UInt32}) = Float64(x)==Float64(y)
==(x::Union{Int32,UInt32}, y::Float32) = Float64(x)==Float64(y)

<(x::Float32, y::Union{Int32,UInt32}) = Float64(x)<Float64(y)
<(x::Union{Int32,UInt32}, y::Float32) = Float64(x)<Float64(y)
($op)(x::Union{Float16,Float32}, y::Union{Int32,UInt32}) = ($op)(Float64(x), Float64(y))
($op)(x::Union{Int32,UInt32}, y::Union{Float16,Float32}) = ($op)(Float64(x), Float64(y))

<=(x::Float32, y::Union{Int32,UInt32}) = Float64(x)<=Float64(y)
<=(x::Union{Int32,UInt32}, y::Float32) = Float64(x)<=Float64(y)
($op)(x::Float16, y::Union{Int16,UInt16}) = ($op)(Float32(x), Float32(y))
($op)(x::Union{Int16,UInt16}, y::Float16) = ($op)(Float32(x), Float32(y))
end
end


abs(x::Float16) = reinterpret(Float16, reinterpret(UInt16, x) & 0x7fff)
Expand Down
10 changes: 10 additions & 0 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,16 @@ end
@test Float64(UInt128(3.7e19)) == 3.7e19
@test Float64(UInt128(3.7e30)) == 3.7e30
end
@testset "Float16 vs Int comparisons" begin
@test Inf16 != typemax(Int16)
@test Inf16 != typemax(Int32)
@test Inf16 != typemax(Int64)
@test Inf16 != typemax(Int128)
@test Inf16 != typemax(UInt16)
@test Inf16 != typemax(UInt32)
@test Inf16 != typemax(UInt64)
@test Inf16 != typemax(UInt128)
end
@testset "NaN comparisons" begin
@test !(NaN <= 1)
@test !(NaN >= 1)
Expand Down

0 comments on commit 6cb86ad

Please sign in to comment.