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

Add method to rationalize Rational #43427

Merged
merged 17 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
14 changes: 11 additions & 3 deletions base/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ function rationalize(::Type{T}, x::AbstractFloat, tol::Real) where T<:Integer
if tol < 0
throw(ArgumentError("negative tolerance $tol"))
end

Copy link
Member

Choose a reason for hiding this comment

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

Better to avoid spurious whitespace changes like this.

T<:Unsigned && x < 0 && __throw_negate_unsigned()
isnan(x) && return T(x)//one(T)
isinf(x) && return unsafe_rational(x < 0 ? -one(T) : one(T), zero(T))
Expand All @@ -169,7 +170,6 @@ function rationalize(::Type{T}, x::AbstractFloat, tol::Real) where T<:Integer
a = trunc(x)
r = x-a
y = one(x)

tolx = oftype(x, tol)
nt, t, tt = tolx, zero(tolx), tolx
ia = np = nq = zero(T)
Expand Down Expand Up @@ -216,8 +216,16 @@ function rationalize(::Type{T}, x::AbstractFloat, tol::Real) where T<:Integer
end
rationalize(::Type{T}, x::AbstractFloat; tol::Real = eps(x)) where {T<:Integer} = rationalize(T, x, tol)::Rational{T}
rationalize(x::AbstractFloat; kvs...) = rationalize(Int, x; kvs...)
rationalize(::Type{T}, x::Complex; kvs...) where {T<:Integer} = Complex(rationalize(T, x.re, kvs...)::Rational{T}, rationalize(T, x.im, kvs...)::Rational{T})
Copy link
Member

Choose a reason for hiding this comment

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

Oh wow, this looks like it was actually a bug. Thanks for catching that. Would be good to add a test for it.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe something like:

diff --git a/test/rational.jl b/test/rational.jl
--- test/rational.jl
+++ test/rational.jl
@@ -638,5 +638,11 @@
     @test rationalize(float(pi)im) == 0//1 + 165707065//52746197*im
     @test rationalize(Int8, float(pi)im) == 0//1 + 22//7*im
     @test rationalize(1.192 + 2.233im) == 149//125 + 2233//1000*im
     @test rationalize(Int8, 1.192 + 2.233im) == 118//99 + 67//30*im
+
+    # test: rationalize(x::Complex; kvs...)
+    precise_next = 7205759403792795//72057594037927936
+    @assert Float64(precise_next) == nextfloat(0.1)
+    @test rationalize(nextfloat(0.1) * im; tol=0) == precise_next * im
+    @test rationalize(0.1im; tol=eps(0.1)) == rationalize(0.1im)
 end

rationalize(x::Complex; kvs...) = Complex(rationalize(Int, x.re, kvs...), rationalize(Int, x.im, kvs...))
rationalize(::Type{T}, x::Complex; kvs...) where {T<:Integer} = Complex(rationalize(T, x.re; kvs...)::Rational{T}, rationalize(T, x.im; kvs...)::Rational{T})
rationalize(x::Complex; kvs...) = Complex(rationalize(Int, x.re; kvs...), rationalize(Int, x.im; kvs...))
rationalize(::Type{T}, x::Rational, tol::Real) where {T<:Integer} = rationalize(T, float(x), tol)::Rational{T}
rationalize(::Type{T}, x::Rational; tol::Real = zero(x)) where {T<:Integer} = rationalize(T, float(x), tol)::Rational{T}
rationalize(x::Rational{T}; kvs...) where{T} = rationalize(T, x; kvs...)
rationalize(::Type{T}, x::Integer, tol::Real) where {T<:Integer} = rationalize(T, Rational(x, 1), tol)
tantheta01 marked this conversation as resolved.
Show resolved Hide resolved
rationalize(::Type{T}, x::Integer; tol::Real = 0) where {T<:Integer} = Rational(x, 1)
rationalize(x::Integer; kvs...) = Rational(x)



"""
numerator(x)
Expand Down
2 changes: 2 additions & 0 deletions test/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ using Test

@test @inferred(rationalize(Int, 3.0, 0.0)) === 3//1
@test @inferred(rationalize(Int, 3.0, 0)) === 3//1
@test @inferred(rationalize(Int, 33//100, 0.1)) === 1//3
@test @inferred(rationalize(Int, 3, 0.0)) === 3//1
@test_throws OverflowError rationalize(UInt, -2.0)
@test_throws ArgumentError rationalize(Int, big(3.0), -1.)
# issue 26823
Expand Down