I can get Infinity
and NaN
by
n = 9.0 / 0 #=> Infinity
n.class #=> Float
m = 0 / 0.0 #=> NaN
m.class #=> Float
but when I want to access Infinity
or NaN
directly:
Infinity #=> uninitialized constant Infinity (NameError)
NaN #=> uninitialized constant NaN (NameError)
What are Infinity
and NaN
? Are they objects, keywords, or something else?
What you see printed as
Infinity
andNaN
are just the string representations for two special instances of theFloat
class, not keywords or literals. They are returned by floating point division by 0 or by referencing the constantsFloat::INFINITY
andFloat::NAN
.