How does ruby handle zero division?

ruby, zero

Solution

Ruby just tracks the IEEE 754 Floating Point Standard. That Wikipedia page is not bad at explaining what you are seeing. Many modern languages take the same approach.

Intuitively, the behavior you see makes perfect sense. In general,

1/<small number> = <big number>

Therefore in the limit,

1/0 -> Infinity    and similarly    -1/0 -> -Infinity

`Infinity` is a constant understood by the floating point subsystem. On the other hand

0 / <any non-zero> = 0

So we have a conflict on 0/0. Should it be zero or infinity? The IEEE standard answer is "Not a Number", the `NaN` you are seeing, another floating point constant.

The constants `NaN` and plus or minus `Infinity` propagate through expressions in a way that also makes sense. For example:

Infinity + <any (necessarly finite) number> = Infinity

and

<any number> + NaN = NaN

And more interestingly:

1 / Infinity = 0

Which you can try yourself:

irb(main):005:0> 1.0 / (1.0 / 0.0)
=> 0.0

In this manner a floating point calculation can continue even when it has overflowed or divided by zero and still produce a reasonably informative answer (though after you know the standard well, you'll see that relying on the answer is usually a bad idea).

This is far from the only behavior that the Standard provides. Others can be selected. But Ruby does this for you. The source file numeric.c, function `Init_Numeric`, sets up the host processor so that division by zero propagates infinities. Other languages might make other choices, for example to generate an exception.

Problem

I am trying to find out how ruby handles zero division. Ruby returns different results based on the class. This is what I tried ``` 0/0 # => ZeroDivisionError: divided by 0 1/0 # => ZeroDivisionError: divided by 0 1.0/0 # => Infinity 0.0/0.0 # => NaN ``` What is happening here? Shouldn't I get a `ZeroDivisionError` for all the above cases? Update Is "Infinity" a standard data type, then? ``` (1.0/0).class # => Float ```

Original source