From 936147bbd969fba9573e62e4246450492968647f Mon Sep 17 00:00:00 2001 From: Lilith Orion Hafner Date: Thu, 24 Apr 2025 11:20:02 -0500 Subject: [PATCH 1/3] Fix and test float16 rational comparison --- base/rational.jl | 4 +++- test/rational.jl | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/base/rational.jl b/base/rational.jl index 8c5244ea4bad3..426858f927e91 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -441,9 +441,11 @@ fma(x::Rational, y::Rational, z::Rational) = x*y+z <=(x::Rational, y::Integer ) = x.num <= widemul(x.den,y) <=(x::Integer , y::Rational) = widemul(x,y.den) <= y.num +widen_float16(x) = x +widen_float16(x::Float16) = widen(x) function ==(x::AbstractFloat, q::Rational) if isfinite(x) - (count_ones(q.den) == 1) & (x*q.den == q.num) + (count_ones(q.den) == 1) & (widen_float16(x)*q.den == q.num) else x == q.num/q.den end diff --git a/test/rational.jl b/test/rational.jl index fe707cb7d3521..b07842e759f99 100644 --- a/test/rational.jl +++ b/test/rational.jl @@ -837,3 +837,8 @@ end @test_throws OverflowError numerator(Int8(1)//Int8(31) + Int8(8)im//Int8(3)) end end + +@testset "Float16 comparison" begin + @test Float16(6.0e-8) == big(1//16777216) == 1//16777216 + @test Float16(6.0e-8) == 1//16777216 +end From a9600c3a64d4bfe09c1bb7adce44e5b2e12dcad2 Mon Sep 17 00:00:00 2001 From: Lilith Orion Hafner Date: Thu, 24 Apr 2025 11:24:04 -0500 Subject: [PATCH 2/3] Simplify --- base/rational.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/base/rational.jl b/base/rational.jl index 426858f927e91..477a3aa91511d 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -441,11 +441,10 @@ fma(x::Rational, y::Rational, z::Rational) = x*y+z <=(x::Rational, y::Integer ) = x.num <= widemul(x.den,y) <=(x::Integer , y::Rational) = widemul(x,y.den) <= y.num -widen_float16(x) = x -widen_float16(x::Float16) = widen(x) +==(x::Float16, q::Rational) = widen(x) == q function ==(x::AbstractFloat, q::Rational) if isfinite(x) - (count_ones(q.den) == 1) & (widen_float16(x)*q.den == q.num) + (count_ones(q.den) == 1) & (x*q.den == q.num) else x == q.num/q.den end From e94dd3ef02262ab34e87615b02dc3cd8bea0077e Mon Sep 17 00:00:00 2001 From: Lilith Orion Hafner Date: Thu, 24 Apr 2025 12:21:20 -0500 Subject: [PATCH 3/3] Update base/rational.jl Co-authored-by: Oscar Smith --- base/rational.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/base/rational.jl b/base/rational.jl index 477a3aa91511d..707b740fb2048 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -441,10 +441,9 @@ fma(x::Rational, y::Rational, z::Rational) = x*y+z <=(x::Rational, y::Integer ) = x.num <= widemul(x.den,y) <=(x::Integer , y::Rational) = widemul(x,y.den) <= y.num -==(x::Float16, q::Rational) = widen(x) == q function ==(x::AbstractFloat, q::Rational) if isfinite(x) - (count_ones(q.den) == 1) & (x*q.den == q.num) + (count_ones(q.den) == 1) & (ldexp(x, top_set_bit(q.den-1)) == q.num) else x == q.num/q.den end