Skip to content

Commit 1d3c371

Browse files
sam0410stevengj
authored andcommitted
Add Float16 comparisons (#29916)
* Add Float16 comparisons * Add @eval * Add union * Add != to tests
1 parent bbd5414 commit 1d3c371

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

base/float.jl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -503,15 +503,18 @@ for Ti in (Int64,UInt64,Int128,UInt128)
503503
end
504504
end
505505
end
506+
for op in (:(==), :<, :<=)
507+
@eval begin
508+
($op)(x::Float16, y::Union{Int128,UInt128,Int64,UInt64}) = ($op)(Float64(x), Float64(y))
509+
($op)(x::Union{Int128,UInt128,Int64,UInt64}, y::Float16) = ($op)(Float64(x), Float64(y))
506510

507-
==(x::Float32, y::Union{Int32,UInt32}) = Float64(x)==Float64(y)
508-
==(x::Union{Int32,UInt32}, y::Float32) = Float64(x)==Float64(y)
509-
510-
<(x::Float32, y::Union{Int32,UInt32}) = Float64(x)<Float64(y)
511-
<(x::Union{Int32,UInt32}, y::Float32) = Float64(x)<Float64(y)
511+
($op)(x::Union{Float16,Float32}, y::Union{Int32,UInt32}) = ($op)(Float64(x), Float64(y))
512+
($op)(x::Union{Int32,UInt32}, y::Union{Float16,Float32}) = ($op)(Float64(x), Float64(y))
512513

513-
<=(x::Float32, y::Union{Int32,UInt32}) = Float64(x)<=Float64(y)
514-
<=(x::Union{Int32,UInt32}, y::Float32) = Float64(x)<=Float64(y)
514+
($op)(x::Float16, y::Union{Int16,UInt16}) = ($op)(Float32(x), Float32(y))
515+
($op)(x::Union{Int16,UInt16}, y::Float16) = ($op)(Float32(x), Float32(y))
516+
end
517+
end
515518

516519

517520
abs(x::Float16) = reinterpret(Float16, reinterpret(UInt16, x) & 0x7fff)

test/numbers.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,16 @@ end
993993
@test Float64(UInt128(3.7e19)) == 3.7e19
994994
@test Float64(UInt128(3.7e30)) == 3.7e30
995995
end
996+
@testset "Float16 vs Int comparisons" begin
997+
@test Inf16 != typemax(Int16)
998+
@test Inf16 != typemax(Int32)
999+
@test Inf16 != typemax(Int64)
1000+
@test Inf16 != typemax(Int128)
1001+
@test Inf16 != typemax(UInt16)
1002+
@test Inf16 != typemax(UInt32)
1003+
@test Inf16 != typemax(UInt64)
1004+
@test Inf16 != typemax(UInt128)
1005+
end
9961006
@testset "NaN comparisons" begin
9971007
@test !(NaN <= 1)
9981008
@test !(NaN >= 1)

0 commit comments

Comments
 (0)