Skip to content

should x // y such that iszero(x) && iszero(y) throw an ArgumentError or a DivideError? #56754

Open
@nsajko

Description

The fact that dividing zero by zero throws DivideError in some cases, but ArgumentError in other cases seems inconsistent:

julia> 0 // 0
ERROR: ArgumentError: invalid rational: zero(Int64)//zero(Int64)
Stacktrace:
 [1] __throw_rational_argerror_zero(T::Type)
   @ Base ./rational.jl:30
 [2] Rational
   @ ./rational.jl:32 [inlined]
 [3] Rational
   @ ./rational.jl:48 [inlined]
 [4] //(n::Int64, d::Int64)
   @ Base ./rational.jl:84
 [5] top-level scope
   @ REPL[15]:1

julia> (0 // 1) // 0
ERROR: DivideError: integer division error
Stacktrace:
 [1] div
   @ ./int.jl:297 [inlined]
 [2] div
   @ ./int.jl:229 [inlined]
 [3] divgcd(x::Int64, y::Int64)
   @ Base ./rational.jl:54
 [4] //(x::Rational{Int64}, y::Int64)
   @ Base ./rational.jl:87
 [5] top-level scope
   @ REPL[16]:1

FWIW:

julia> Base.infer_exception_type(//, Tuple{Int, Int})
Union{DivideError, ArgumentError, OverflowError}

julia> Base.infer_exception_type(//, Tuple{Int, Rational{Int}})
Union{DivideError, OverflowError}

julia> Base.infer_exception_type(//, Tuple{Rational{Int}, Int})
Union{DivideError, OverflowError}

julia> Base.infer_exception_type(//, Tuple{Rational{Int}, Rational{Int}})
Union{DivideError, OverflowError}

Should 0 // 0 be changed to throw DivideError? Should (0 // 1) // 0 be changed to throw ArgumentError?

The specific reason I'm interested in this inconsistency is that I'm implementing a type very similar to Rational in a package. So I need to decide what to throw there.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    error handlingHandling of exceptions by Julia or the usermathsMathematical functionsrationalsThe Rational type and values thereof

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions