Types of Numbers

Four underlying types of numbers are built into the Wolfram System.

Integerarbitrarylength exact integer
Rationalinteger/integer in lowest terms
Realapproximate real number, with any specified precision
Complexcomplex number of the form number+number I

Intrinsic types of numbers in the Wolfram System.

Rational numbers always consist of a ratio of two integers, reduced to lowest terms.
In[1]:=
Click for copyable input
Out[1]=
Approximate real numbers are distinguished by the presence of an explicit decimal point.
In[2]:=
Click for copyable input
Out[2]=
An approximate real number can have any number of digits.
In[3]:=
Click for copyable input
Out[3]=
Complex numbers can have integer or rational components.
In[4]:=
Click for copyable input
Out[4]=
They can also have approximate real number components.
In[5]:=
Click for copyable input
Out[5]=
123an exact integer
123.an approximate real number
123.0000000000000an approximate real number with a certain precision
123.+0.Ia complex number with approximate real number components

Several versions of the number 123.

You can distinguish different types of numbers in the Wolfram System by looking at their heads. (Although numbers in the Wolfram System have heads like other expressions, they do not have explicit elements which you can extract.)

The object is taken to be an exact integer, with head Integer.
In[6]:=
Click for copyable input
Out[6]=
The presence of an explicit decimal point makes the Wolfram System treat as an approximate real number, with head Real.
In[7]:=
Click for copyable input
Out[7]=
NumberQ[x]test whether x is any kind of number
IntegerQ[x]test whether x is an integer
EvenQ[x]test whether x is even
OddQ[x]test whether x is odd
PrimeQ[x]test whether x is a prime integer
Head[x]===typetest the type of a number

Tests for different types of numbers.

NumberQ[x] tests for any kind of number.
In[8]:=
Click for copyable input
Out[8]=
is treated as a Real, so IntegerQ gives False.
In[9]:=
Click for copyable input
Out[9]=

If you use complex numbers extensively, there is one subtlety you should be aware of. When you enter a number like , the Wolfram System treats it as an approximate real number, but assumes that its imaginary part is exactly zero. Sometimes you may want to enter approximate complex numbers with imaginary parts that are zero, but only to a certain precision.

When the imaginary part is the exact integer , the Wolfram System simplifies complex numbers to real ones.
In[10]:=
Click for copyable input
Out[10]=
Here the imaginary part is only zero to a certain precision, so the Wolfram System retains the complex number form.
In[11]:=
Click for copyable input
Out[11]=

The distinction between complex numbers whose imaginary parts are exactly zero, or are only zero to a certain precision, may seem like a pedantic one. However, for example, when the interpretation of powers and roots of complex numbers is discussed in "Functions That Do Not Have Unique Values", the distinction becomes significant.

One way to find out the type of a number in Wolfram System is just to pick out its head using Head[expr]. For many purposes, however, it is better to use functions like IntegerQ that explicitly test for particular types. Functions like this are set up to return True if their argument is manifestly of the required type, and to return False otherwise. As a result, IntegerQ[x] will give False unless has an explicit integer value.