# Misleading \`DivideError\` message

**URL:** <https://discourse.julialang.org/t/misleading-divideerror-message/46088>\
**Category:** New to Julia\
**Created:** [September 5, 2020, 4:05am UTC](https://discourse.julialang.org/t/misleading-divideerror-message/46088 "2020-09-05T04:05:40Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![kimikage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kimikage/32/14534_2.png) [@kimikage](https://discourse.julialang.org/u/kimikage)\
**Post date:** [September 5, 2020, 4:05am UTC](https://discourse.julialang.org/t/misleading-divideerror-message/46088/1 "2020-09-05T04:05:40Z")

</div>

The definition of `DivideError` has been unchanged for a long time, and I cannot decide whether it should be changed now, but the error message is unfriendly and misleading.  
[https://docs.julialang.org/en/v1/base/base/#Core.DivideError](https://docs.julialang.org/en/v1/base/base/#Core.DivideError)

Even though the type name is not `IntegerDivisionError`, it targets the “integer” division. I think this is due to the fact that “division by zero” on floating-point types does not raise an error.

However, `DivideError` can occur in many operations of various numeric types. Depending on the internal implementation, it may even occur in functions that take floating point numbers as input and output. Also, `DivideError` can be caused by overflow as well as division by zero, but IIUC, this is just a compromise in performance.

```julia
julia> typemin(Int)÷-1
ERROR: DivideError: integer division error

```

Therefore, the `DivideError` due to overflow is also not a good enough reason to limit the scope to integers only.

I think we should redefine `DivideError` as an error for the generic “division by zero” and just mention “integer division” as its typical cause. In that case, the error message would be something like “DivideError: division by zero or too small value”. (The “too small value” is not a formal phrase, though.)

Any thoughts?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 5, 2020, 7:04am UTC](https://discourse.julialang.org/t/misleading-divideerror-message/46088/2 "2020-09-05T07:04:32Z")

</div>

> [@kimikage](#):
>
> I think we should redefine `DivideError` as an error for the generic “division by zero” and just mention “integer division” as its typical cause.

Technically, this is a breaking change since currently it is documented to be an error for

> Integer division was attempted with a denominator value of 0.

Following this, the `÷ -1` example should _not_ throw this error, but this looks like a compromise, see

> <https://github.com/JuliaLang/julia/issues/14273>
>
> Currently, \`div(typemin(Int), -1\` throws a \`DivideError\`. Should it throw an \`Ov…erflowError\` instead? Arguably, there's nothing wrong per se in dividing by \`-1\`; it's just that the type \`Int\` can't hold the result.

Frankly, I am not sure that not being able to perform a division warrants its own error. I don’t think a lot code relies on this, but since we cannot get rid of it now at least we should not extend its scope. Whenever an arithmetic operation does not make sense or is infeasible, `DomainError` should be fine.

---

<div class="post-metadata">

**Author:** ![kimikage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kimikage/32/14534_2.png) [@kimikage](https://discourse.julialang.org/u/kimikage)\
**Post date:** [September 5, 2020, 7:48am UTC](https://discourse.julialang.org/t/misleading-divideerror-message/46088/3 "2020-09-05T07:48:05Z")

</div>

Indeed, extending its scope is a breaking change. As a practical matter, changing the error message will require some changes to the tests.

> Whenever an arithmetic operation does not make sense or is infeasible, `DomainError` should be fine.

I think `DomainError` is a valid error for “division by zero” in many cases, but it is just an error on the values of the input to a method. It can be even more misleading than “integer division error” if the condition for denominator to be zero is not simple.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 5, 2020, 8:23am UTC](https://discourse.julialang.org/t/misleading-divideerror-message/46088/4 "2020-09-05T08:23:41Z")

</div>

> [@kimikage](#):
>
> if the condition for denominator to be zero is not simple.

I would expect that if `div(::T, ::T)` is defined on a field, so is `zero(::T)`.

Generally, the granularity of exception hierarchies is tricky to get right, because of various considerations (informing the user, use in control flow/recovery paths, performance considerations). See the discussion for

> <https://github.com/JuliaLang/julia/pull/16111>
>
> The current behaviour of rational infinities under \`+\` and \`-\` differs from both… floats and mathematical intuition:
> 
> \`\`\` julia
> julia\> 1//0 + 1//0
> ERROR: DivideError: integer division error
> \[inlined code\] from ./rational.jl:19
> in +(::Rational{Int64}, ::Rational{Int64}) at ./rational.jl:179
> in eval(::Module, ::Any) at ./boot.jl:236
> \`\`\`
> 
> I have created a fix and tests to make sure rational infinities behave like floating point ones. This is not a very elegant fix, but I could not figure out a better way.

FWIW, I think that callers should strive to handle exceptions that they have a solution for, but accept that not all methods will deliver the most specific error and it is OK to just let it through. Moreover, exceptions should only be used for control flow as a last resort because they are really expensive.

If in some specific context

1. checking zero is expensive,
2. but can be done as part of `div` or similar,

I would recommend defining a method

```julia
struct DivisionByZero end # not <: Exception

@inline div_or_dbz(a, b) = zero(b) ? DivisionByZero() : div(a, b)

function div_or_dbz(a::T, b::T) where {T <: MySpecialType}
    ... # do the special thing
end

```

and using it accordingly.

---

<div class="post-metadata">

**Author:** ![kimikage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kimikage/32/14534_2.png) [@kimikage](https://discourse.julialang.org/u/kimikage)\
**Post date:** [September 5, 2020, 9:06am UTC](https://discourse.julialang.org/t/misleading-divideerror-message/46088/5 "2020-09-05T09:06:47Z")

</div>

> [@Tamas\_Papp](#):
>
> > [@kimikage](#):
> >
> > if the condition for denominator to be zero is not simple.
> 
> I would expect that if `div(::T, ::T)` is defined on a field, so is `zero(::T)` .

As you probably got my point, it was not about the group theory, but about APIs. I think the following function are not suitable for throwing `DomainError`:

```julia
f(a::AbstractArray{<:Real}) = div(maximum(a), minimum(a))

```

I’m not trying to avoid `DivideError` because the message is misleading. We actually can get `DivideError`s on operations of non-integer types, so I want to improve the message.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 5, 2020, 11:29am UTC](https://discourse.julialang.org/t/misleading-divideerror-message/46088/6 "2020-09-05T11:29:19Z")

</div>

> [@kimikage](#):
>
> it was not about the group theory, but about APIs.

I think that the two are closely related — even though abstract algebras are not perfectly mapped to the numerical types we use, keeping their properties in mind is necessary for designing APIs. For `<:Real`, this is a very difficult task with a lot of open issues.

> [@kimikage](#):
>
> I think the following function are not suitable for throwing `DomainError` :
> 
> ```julia
> f(a::AbstractArray{<:Real}) = div(maximum(a), minimum(a))
> 
> ```

I don’t understand why — is there a mathematical counterpart to `f` that is well-defined when \min(a) = 0?

---

<div class="post-metadata">

**Author:** ![kimikage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kimikage/32/14534_2.png) [@kimikage](https://discourse.julialang.org/u/kimikage)\
**Post date:** [September 5, 2020, 11:53am UTC](https://discourse.julialang.org/t/misleading-divideerror-message/46088/7 "2020-09-05T11:53:11Z")

</div>

> [@Tamas\_Papp](#):
>
> I don’t understand why — is there a mathematical counterpart to `f` that is well-defined when min(a) = 0 ?

Maybe the example wasn’t a good one; what if it’s SHA256 instead of `minimum`? SHA256 is also mathematically well-defined. Of course, you can still use `DomainError`, but the error message should be a tautology.

In any case, my concern is with the `DivideError` message.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 5, 2020, 12:37pm UTC](https://discourse.julialang.org/t/misleading-divideerror-message/46088/8 "2020-09-05T12:37:16Z")

</div>

> [@kimikage](#):
>
> what if it’s SHA256 instead of `minimum` ? SHA256 is also mathematically well-defined

Sure, but if that can be 0 (which I am not sure about, AFAIK the image does not contain all possible values), then again f isn’t well-defined.

> [@kimikage](#):
>
> In any case, my concern is with the `DivideError` message.

Yes, I realized that. I am just arguing against extending it to other errors.

It is still not clear to me why you want this. Do you want to `catch` it? Or just have a better error message for the user?

---

<div class="post-metadata">

**Author:** ![kimikage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kimikage/32/14534_2.png) [@kimikage](https://discourse.julialang.org/u/kimikage)\
**Post date:** [September 5, 2020, 1:52pm UTC](https://discourse.julialang.org/t/misleading-divideerror-message/46088/9 "2020-09-05T13:52:42Z")

</div>

> [@Tamas\_Papp](#):
>
> Do you want to `catch` it? Or just have a better error message for the user?

Primarily the latter. I don’t think the “integer” in the error message is helpful. If it’s useless anyway, I think “internal” is still more neutral and less misleading. However, if the “integer” is dropped from the error message, there is no longer any evidence that the error is an “integer division error” other than the documentation. So I thought it would be a good idea to change the specification (the documentation) to fit the actual situation.

Of course, I also agree with the importance of specifications in general. So if there is a reason why `DivideError` should only target integers, that should be kept. In that case, the error type just for the general “division by zero” does not exist in Julia’s `Core`/`Base`.

This is just a side note, but I made a PR open in FixedPointNumbers.jl that changes the internal implementation of `div` from an integer division to a floating-point division. Using floating-point is just an implementation matter, so the new implementation can also throw the `DivideError`. But this is nonsense in two ways. It does not use integer divisions, and the fixed-point numbers are not (always) integers.

I started this thread because if we want to change the `DivideError` messages and the specification, it is better to do it before the next LTS version is released.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 6, 2020, 7:19am UTC](https://discourse.julialang.org/t/misleading-divideerror-message/46088/10 "2020-09-06T07:19:41Z")

</div>

If its only about the error message, are you aware that `DomainError` allows you to specify one and it is printed? Eg

```julia
julia> throw(DomainError("I can't divide by zero"))
ERROR: DomainError with I can't divide by zero:

```

I think that this is the best approach, unless the caller wants to catch errors by type.

---

<div class="post-metadata">

**Author:** ![kimikage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kimikage/32/14534_2.png) [@kimikage](https://discourse.julialang.org/u/kimikage)\
**Post date:** [September 6, 2020, 9:10am UTC](https://discourse.julialang.org/t/misleading-divideerror-message/46088/11 "2020-09-06T09:10:31Z")

</div>

Sorry to mislead you, but FixedPointNumbers.jl is just about the motivation and is not technically relevant to this topic. This is a Julia’s issue, not a problem in packages or user codes.

Again, I have a problem with the `DivideError` message, and the friendly `DomainError` or `ArgumentError` or user-defined errors are not the solution.

Having said that, I appreciate your kind response.👍
