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

overflow in Rational arithmetic #2960

Closed
JeffBezanson opened this issue Apr 28, 2013 · 5 comments
Closed

overflow in Rational arithmetic #2960

JeffBezanson opened this issue Apr 28, 2013 · 5 comments
Assignees
Labels
domain:rationals The Rational type and values thereof kind:bug Indicates an unexpected problem or unintended behavior status:help wanted Indicates that a maintainer wants help on an issue or pull request
Milestone

Comments

@JeffBezanson
Copy link
Sponsor Member

Rational - Int comparisons are susceptible to overflow:

julia> 3//2 > typemax(Int)
true

This is due to multiplying the integer argument by the rational's denominator.

Other operations that use * this way are similarly afflicted:

julia> r = (typemax(Int)-1)//typemax(Int)
9223372036854775806//9223372036854775807

julia> div(r,2)
-4611686018427387903
@quinnj
Copy link
Member

quinnj commented Jun 10, 2014

Looks like we've made some progress on this:

julia> 3//2 > typemax(Int)
false

@Keno
Copy link
Member

Keno commented Jun 10, 2014

Given how easy it is to overflow Rational, maybe it would be a good idea to make rations use checked arithmetic?

@vtjnash vtjnash modified the milestones: 1.0, 0.4 Aug 2, 2014
@vtjnash vtjnash self-assigned this Aug 2, 2014
@vtjnash
Copy link
Sponsor Member

vtjnash commented Aug 2, 2014

Rationals need to be fixed 0.4 (what this? they even had their own tag), so I've picked a random issue and tagged it 0.4.

And yes, I'm volunteering myself if nobody else beats me too it.

@simonbyrne
Copy link
Contributor

Note that comparisons are broken even without overflow:

julia> a = 1//3
1//3

julia> b = 1/3
0.3333333333333333

julia> BigFloat(a) > BigFloat(b)
true

julia> a > b
false

julia> a < b
false

julia> a == b
false

julia> a >= b
true

julia> a <= b
true

However I don't know of any great way to fix this. One idea would be to sequentially compare each element of the continued fraction expansion of each number (obtained via the Euclidean algorithm).

@simonbyrne
Copy link
Contributor

These should now all be sorted: if not, please reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:rationals The Rational type and values thereof kind:bug Indicates an unexpected problem or unintended behavior status:help wanted Indicates that a maintainer wants help on an issue or pull request
Projects
None yet
Development

No branches or pull requests

5 participants