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

don't mutate globals when constructing Rational from AbstractIrrational #55853

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
35 changes: 28 additions & 7 deletions base/irrationals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,41 @@ AbstractFloat(x::AbstractIrrational) = Float64(x)::Float64
Float16(x::AbstractIrrational) = Float16(Float32(x)::Float32)
Complex{T}(x::AbstractIrrational) where {T<:Real} = Complex{T}(T(x))

function _irrational_to_rational(::Type{T}, x::AbstractIrrational) where T<:Integer
o = precision(BigFloat)
function _irrational_to_rational_at_current_precision(::Type{T}, x::AbstractIrrational) where {T <: Integer}
bx = BigFloat(x)
r = rationalize(T, bx, tol = 0)
br = BigFloat(r, precision = precision(BigFloat) + 32)
if eps(bx) < abs(br - bx)
r
else
nothing # Error is too small, repeat with greater precision.
nsajko marked this conversation as resolved.
Show resolved Hide resolved
end
end
function _irrational_to_rational_at_precision(::Type{T}, x::AbstractIrrational, p::Int) where {T <: Integer}
f = let x = x
() -> _irrational_to_rational_at_current_precision(T, x)
end
setprecision(f, BigFloat, p)
end
function _irrational_to_rational_at_current_rounding_mode(::Type{T}, x::AbstractIrrational) where {T <: Integer}
if T <: BigInt
_throw_argument_error_irrational_to_rational_bigint() # avoid infinite loop
end
p = 256
while true
setprecision(BigFloat, p)
bx = BigFloat(x)
r = rationalize(T, bx, tol=0)
if abs(BigFloat(r) - bx) > eps(bx)
setprecision(BigFloat, o)
r = _irrational_to_rational_at_precision(T, x, p)
if r isa Number
return r
end
p += 32
end
end
function _irrational_to_rational(::Type{T}, x::AbstractIrrational) where {T <: Integer}
f = let x = x
() -> _irrational_to_rational_at_current_rounding_mode(T, x)
end
setrounding(f, BigFloat, RoundNearest)
end
Rational{T}(x::AbstractIrrational) where {T<:Integer} = _irrational_to_rational(T, x)
_throw_argument_error_irrational_to_rational_bigint() = throw(ArgumentError("Cannot convert an AbstractIrrational to a Rational{BigInt}: use rationalize(BigInt, x) instead"))
Rational{BigInt}(::AbstractIrrational) = _throw_argument_error_irrational_to_rational_bigint()
Expand Down