What are the types of Infinity and NaN?

I can get Infinity and NaN on

 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?

+4
source share
2 answers

What you see printed as Infinity and NaN are just string representations for two special instances of the Float class, not keywords or literals. They are returned by dividing by floating point by 0 or by referencing the constants Float::INFINITY and Float::NAN .

 Float::INFINITY.class # => Float Float::INFINITY.to_s # => "Infinity" Float::NAN.class # => Float Float::NAN.to_s # => "NaN" 
+5
source

If you need inf / nan literal, use the following command:

 >> Float::INFINITY => Infinity >> Float::NAN => NaN 

See List of Constant Constants

+3
source

Source: https://habr.com/ru/post/1499092/


All Articles