Why don't integer division errors occur in floating point division?

Hello!

I am currently making my way through the documentation, and I was wondering why the floating point division operator doesn’t throw the same errors as the integer division function. In the documentation, it says:

" Integer division (the div function) has two exceptional cases: dividing by zero, and dividing the lowest negative number ( typemin ) by -1. Both of these cases throw a DivideError . The remainder and modulus functions ( rem and mod ) throw a DivideError when their second argument is zero."

When I do these same operations with the ‘/’ operator, I get:

julia> 1/0
Inf

julia> typemin(Int64)/-1
9.223372036854776e18

Why don’t these same errors get thrown when I do floating point division with the ‘/’ operator?

Edit:

I should have read a little bit further before posting. It says a little bit after that this is the result of the IEEE 754 standard.

3 Likes

The behavior of 1/0 seems clear to me by the IEEE 754 standard. However, I still don’t see why typemin(Int64)/-1 should produce a floating point value rather than throw an exception.

As far as I can tell it produces the correct answer, so the behaviour makes sense:

julia> m = typemin(Int64)
-9223372036854775808

julia> (m/-1) == big(m)/big(-1)
true
1 Like

Because the value can be represented using Float64, where it can’t using Int64.

This was also discussed recently in another thread: Division by zero runs without warning -> complicates finding bugs - #2 by stevengj

This is the oddest thread. The question was asked six years ago, then it was first responded to today.

3 Likes

Yes, I didn’t notice that @Guillermo_C was necroposting on an ancient thread…

integer / integer in Julia is always floating-point division. (Truncating integer division is represented by a different operation, div.)

5 Likes