Infinity in Ruby

When dividing by zero, Ruby returns a ZeroDivisionError (divided by 0). However, when dividing by a zero using float, this time Ruby returns Infinity.

irb(main):001:0> 1 / 0
Traceback (most recent call last):
        5: from /usr/local/bin/irb:23:in `<main>'
        4: from /usr/local/bin/irb:23:in `load'
        3: from /var/lib/gems/2.7.0/gems/irb-1.2.4/exe/irb:11:in `<top (required)>'
        2: from (irb):1
        1: from (irb):1:in `/'
ZeroDivisionError (divided by 0)
irb(main):002:0> 1 / 0.0
=> Infinity

However, there is no constant named Infinity (you cannot call Infinity directly). To call Infinity, one should use the constant Float::INFINITY.

Not a Number (NaN)

Substracting or divising two infinite values returns NaN (Not a Number).

irb(main):001:0> infinity = 1 / 0.0
irb(main):002:0> infinity - infinity
=> NaN
irb(main):003:0> infinity / infinity
=> NaN
irb(main):004:0> infinity * infinity
=> Infinity

Source:

Links to this page
#ruby