Types of Numbers
Four underlying types of numbers are built into
Mathematica.
| Integer | arbitrary-length exact integer |
| Rational | integer/integer in lowest terms |
| Real | approximate real number, with any specified precision |
| Complex | complex number of the form number+number I |
Intrinsic types of numbers in Mathematica.
Rational numbers always consist of a ratio of two integers, reduced to lowest terms.
| Out[1]= |  |
|
Approximate real numbers are distinguished by the presence of an explicit decimal point.
| Out[2]= |  |
|
An approximate real number can have any number of digits.
| Out[3]= |  |
|
Complex numbers can have integer or rational components.
| Out[4]= |  |
|
They can also have approximate real number components.
| Out[5]= |  |
|
| 123 | an exact integer |
| 123. | an approximate real number |
| 123.0000000000000 | an approximate real number with a certain precision |
| 123.+0.I | a complex number with approximate real number components |
Several versions of the number 123.
You can distinguish different types of numbers in
Mathematica by looking at their heads. (Although numbers in
Mathematica have heads like other expressions, they do not have explicit elements which you can extract.)
The object 123 is taken to be an exact integer, with head Integer.
| Out[6]= |  |
|
The presence of an explicit decimal point makes Mathematica treat 123. as an approximate real number, with head Real.
| 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] type | test the type of a number |
Tests for different types of numbers.
NumberQ[x] tests for any kind of number.
| Out[8]= |  |
|
| Out[9]= |  |
|
If you use complex numbers extensively, there is one subtlety you should be aware of. When you enter a number like
123.,
Mathematica 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 0, Mathematica simplifies complex numbers to real ones.
| Out[10]= |  |
|
Here the imaginary part is only zero to a certain precision, so Mathematica retains the complex number form.
| 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, when we discuss, for example, the interpretation of powers and roots of complex numbers in
"Functions That Do Not Have Unique Values", the distinction will become significant.
One way to find out the type of a number in
Mathematica is just to pick out its head using
Head[expr]. For many purposes, however, it is better to use functions like
IntegerQ which 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
x has an explicit integer value.