Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the types of Infinity and NaN?

Tags:

ruby

nan

infinity

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?

like image 921
yaodong Avatar asked Aug 27 '13 08:08

yaodong


People also ask

What data type is NaN What about infinity?

Infinity and NaN :: Data Types (Programming) MATLAB uses the special values inf , -inf , and NaN to represent values that are positive and negative infinity, and not a number respectively. MATLAB represents infinity by the special value inf .

What category do NaN and infinity come under?

Contents. INF ("infinity"), NAN ("not a number"), and Null are special-valued system constants that Analytica returns in particular conditions, such as exceptions. These constants can also be used as values in expressions. An indeterminant number, which is a numeric value that is not well-defined, such as 0/0 .

Is NaN and infinity same?

In floating-point calculations, NaN is not the same as infinity, although both are typically handled as special cases in floating-point representations of real numbers as well as in floating-point operations.

What type of value is NaN?

NaN stands for Not A Number and is one of the common ways to represent the missing value in the data. It is a special floating-point value and cannot be converted to any other type than float. NaN value is one of the major problems in Data Analysis.


1 Answers

What you see printed as Infinity and NaN are just the string representations for two special instances of the Float class, not keywords or literals. They are returned by floating point division 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"
like image 169
toro2k Avatar answered Oct 15 '22 22:10

toro2k