-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
06691dd
Add Method to Rationalize Rational
tantheta01 9491c16
Merge branch 'master' of https://github.com/tantheta01/julia
tantheta01 2c536ea
Merge branch 'JuliaLang:master' into master
tantheta01 32497af
Update base/rational.jl
tantheta01 25edfef
Update base/rational.jl
tantheta01 c06b479
Update base/rational.jl
tantheta01 109661c
Merge branch 'JuliaLang:master' into master
tantheta01 850e0de
Rationalize rational, ignore tolerance
tantheta01 a737e1a
Merge branch 'master' into master
oscardssmith ab264e6
Merge branch 'master' into master
oscardssmith 4b70c62
more tests
oscardssmith 797f27b
fix overflow and unbound type param.
oscardssmith af59db9
fix tests
oscardssmith 0f8368d
typo
oscardssmith e680308
fix test
oscardssmith ff0e53d
Update rational.jl
oscardssmith d12ccd0
Merge branch 'master' into master
oscardssmith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,10 +173,11 @@ julia> typeof(numerator(a)) | |
BigInt | ||
``` | ||
""" | ||
function rationalize(::Type{T}, x::AbstractFloat, tol::Real) where T<:Integer | ||
function rationalize(::Type{T}, x::Union{AbstractFloat, Rational}, tol::Real) where T<:Integer | ||
if tol < 0 | ||
throw(ArgumentError("negative tolerance $tol")) | ||
end | ||
|
||
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)) | ||
|
@@ -188,7 +189,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) | ||
|
@@ -233,10 +233,21 @@ function rationalize(::Type{T}, x::AbstractFloat, tol::Real) where T<:Integer | |
return p // q | ||
end | ||
end | ||
rationalize(::Type{T}, x::AbstractFloat; tol::Real = eps(x)) where {T<:Integer} = rationalize(T, x, tol)::Rational{T} | ||
rationalize(::Type{T}, x::AbstractFloat; tol::Real = eps(x)) where {T<:Integer} = rationalize(T, x, tol) | ||
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}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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...), rationalize(T, x.im; kvs...)) | ||
rationalize(x::Complex; kvs...) = Complex(rationalize(Int, x.re; kvs...), rationalize(Int, x.im; kvs...)) | ||
rationalize(::Type{T}, x::Rational; tol::Real = 0) where {T<:Integer} = rationalize(T, x, tol) | ||
rationalize(x::Rational; kvs...) = x | ||
rationalize(x::Integer; kvs...) = Rational(x) | ||
function rationalize(::Type{T}, x::Integer; kvs...) where {T<:Integer} | ||
if Base.hastypemax(T) # BigInt doesn't | ||
x < typemin(T) && return unsafe_rational(-one(T), zero(T)) | ||
x > typemax(T) && return unsafe_rational(one(T), zero(T)) | ||
end | ||
return Rational{T}(x) | ||
end | ||
|
||
|
||
""" | ||
numerator(x) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.