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

Some arithmetic operations not working with Interval{Rational} #470

Closed
lucaferranti opened this issue Jun 2, 2021 · 1 comment · Fixed by #567
Closed

Some arithmetic operations not working with Interval{Rational} #470

lucaferranti opened this issue Jun 2, 2021 · 1 comment · Fixed by #567

Comments

@lucaferranti
Copy link
Member

lucaferranti commented Jun 2, 2021

I found these issues while fixing #466 ,

julia> a = 1//1..2//1
[1//1, 2//1]

julia> b = 0//1..2//1
[0//1, 2//1]

julia> a/b
ERROR: MethodError: no method matching Rational{Int64}(::Float64, ::RoundingMode{:Up})
Closest candidates are:
  Rational{T}(::AbstractFloat) where T<:Integer at rational.jl:118
  (::Type{T})(::T) where T<:Number at boot.jl:760
Stacktrace:
 [1] /(a::Interval{Rational{Int64}}, b::Interval{Rational{Int64}})
   @ IntervalArithmetic C:\Users\lucaa\.julia\dev\IntervalArithmetic\src\intervals\arithmetic.jl:199
 [2] top-level scope
   @ REPL[146]:1

julia> (-1//0..1//1) + (-1//0..1//1)
ERROR: DivideError: integer division error
Stacktrace:
 [1] div
   @ .\int.jl:261 [inlined]
 [2] divgcd
   @ .\rational.jl:44 [inlined]
 [3] +(x::Rational{Int64}, y::Rational{Int64})
   @ Base .\rational.jl:284
 [4] +
   @ C:\Users\lucaa\.julia\dev\IntervalArithmetic\src\intervals\rounding.jl:82 [inlined]
 [5] +(a::Interval{Rational{Int64}}, b::Interval{Rational{Int64}})
   @ IntervalArithmetic C:\Users\lucaa\.julia\dev\IntervalArithmetic\src\intervals\arithmetic.jl:86
 [6] top-level scope
   @ REPL[148]:1

For division, I think the issue stems in the round_expr function, I think for rationals the fallback method that does nothing should be called, but this does not seem to be the case (might be wrong)

For addition, the issue is not really in IntervalArithmetic, because

julia> (-1//0) + (-1//0)
ERROR: DivideError: integer division error
Stacktrace:
 [1] div
   @ .\int.jl:261 [inlined]
 [2] divgcd
   @ .\rational.jl:44 [inlined]
 [3] +(x::Rational{Int64}, y::Rational{Int64})
   @ Base .\rational.jl:284
 [4] top-level scope
   @ REPL[157]:1

I guess hard coding the arithmetic operations for intervals of rationals and manually taking care of those corner cases would work, it shouldn't be hard since with rationals there are no rounding issues. Maybe not a super elegant solution though.

@lucaferranti
Copy link
Member Author

Also, there are some operations (like those supposed to return NaN, e.g. midpoint of empty interval) cannot work (at least in a type stable manner) since rationals don't have a way to represent NaN

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants