Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Irrational vs BigFloat comparisons #29196

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions base/irrationals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ symbol `sym`.
"""
struct Irrational{sym} <: AbstractIrrational end

show(io::IO, x::Irrational{sym}) where {sym} = print(io, "$sym = $(string(float(x))[1:15])...")
show(io::IO, x::Irrational{sym}) where {sym} = print(io, "$sym = $(string(float(x))[1:min(end,15)])...")

promote_rule(::Type{<:AbstractIrrational}, ::Type{Float16}) = Float16
promote_rule(::Type{<:AbstractIrrational}, ::Type{Float32}) = Float32
Expand All @@ -30,24 +30,25 @@ Float16(x::AbstractIrrational) = Float16(Float32(x))
Complex{T}(x::AbstractIrrational) where {T<:Real} = Complex{T}(T(x))

@pure function Rational{T}(x::AbstractIrrational) where T<:Integer
o = precision(BigFloat)
p = 256
while true
setprecision(BigFloat, p)
prec_orig = precision(BigFloat)
for prec in Iterators.countfrom(256, 32)
setprecision(BigFloat, prec)
bx = BigFloat(x)
r = rationalize(T, bx, tol=0)
if abs(BigFloat(r) - bx) > eps(bx)
setprecision(BigFloat, o)
setprecision(BigFloat, prec_orig)
return r
end
p += 32
end
end

(::Type{Rational{BigInt}})(x::AbstractIrrational) = throw(ArgumentError("Cannot convert an AbstractIrrational to a Rational{BigInt}: use rationalize(Rational{BigInt}, x) instead"))

@pure function (t::Type{T})(x::AbstractIrrational, r::RoundingMode) where T<:Union{Float32,Float64}
setprecision(BigFloat, 256) do
T(BigFloat(x), r)
for prec in Iterators.countfrom(256, 32)
bx = BigFloat(x, prec)
u = T(bx, r)
u != bx && return u
end
end

Expand Down Expand Up @@ -76,12 +77,23 @@ end
<(x::Float32, y::AbstractIrrational) = x <= Float32(y,RoundDown)
<(x::AbstractIrrational, y::Float16) = Float32(x,RoundUp) <= y
<(x::Float16, y::AbstractIrrational) = x <= Float32(y,RoundDown)
<(x::AbstractIrrational, y::BigFloat) = setprecision(precision(y)+32) do
big(x) < y
end
<(x::BigFloat, y::AbstractIrrational) = setprecision(precision(x)+32) do
x < big(y)

function cmp(x::BigFloat, y::AbstractIrrational)
if cmp(x, Float64(y, RoundDown)) < 0
return -1
elseif cmp(x, Float64(y, RoundUp)) > 0
return +1
else
for prec in Iterators.countfrom(precision(x), 32)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems dangerous, since if someone defines an "irrational" constant that is actually rational it will never terminate.

How about just something for prec in (precision(x), max(precision(x), precision(BigFloat)) + 32) and return 0 if both of these report equality.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I agree: that would only occur if (a) you define an irrational as a rational value, and (b) compare it to that same rational. I'm not really sure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #26550 for why some AbstractIrrational subtypes may define rational values.

c = cmp(x, BigFloat(y, prec))
c != 0 && return c
end
end
end
cmp(x::AbstractIrrational, y::BigFloat) = -cmp(y,x)

<(x::AbstractIrrational, y::BigFloat) = !isnan(y) && cmp(y,x) > 0
<(x::BigFloat, y::AbstractIrrational) = !isnan(x) && cmp(x,y) < 0

<=(x::AbstractIrrational, y::AbstractFloat) = x < y
<=(x::AbstractFloat, y::AbstractIrrational) = x < y
Expand Down
10 changes: 10 additions & 0 deletions test/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -944,3 +944,13 @@ end
@test big(typeof(complex(x, x))) == typeof(big(complex(x, x)))
end
end

Base.@irrational zzz 1.0 1+pi*BigFloat(1e-100)

@testset "Irrational edge cases" begin
@test Float64(zzz, RoundDown) < Float64(zzz, RoundUp)
@test BigFloat(1.0) < zzz
@test !(nextfloat(BigFloat(1.0)) < zzz)
@test !(zzz < BigFloat(1.0))
@test zzz < nextfloat(BigFloat(1.0))
end