Chapter00.
nb 11
Note that the multiplication symbol following the 2 is optional. Also, Mathematica performs some
algebraic simplifications automatically. If you desire additional simplification, you can use the Sim-
plify function.
In[15]:= Simplify@%D
8 p2
Out[15]= 17 -
9
If you prefer a floating-point approximation of the result, you can use the N function. This function
takes one required argument, an expression, and evaluates it with floating-point arithmetic.
In[16]:= N@%D
Out[16]= 8.22702
Note that N can accept an optional second argument, a positive integer specifying the number of digits
of precision.
In[17]:= N@Pi, 10D
Out[17]= 3.141592654
Mathematica works with exact values such as integers and the symbol Pi differently from floating-
point values. By including any decimal in an expression, Mathematica will treat the entire expression
as a floating-point computation. Compare the following two expressions.
In[18]:= 2ê3 + 3ê2
13
Out[18]=
6
In[19]:= 2 ê 3 + 3.0 ê 2
Out[19]= 2.16667
The presence of 3.0 caused Mathematica to evaluate with floating-point computations rather than
rational arithmetic. Note that the trailing 0 is not necessary: entering 3. is sufficient to tell Mathematica
that you want the number evaluated using floating-point arithmetic.
In[20]:= 3. ê 5
Out[20]= 0.6
This discussion illustrates the concept of a type. A computer can be much more efficient if it knows
what kinds of things it will be working with. If the computer knows that one object is going to be a
floating-point number while another is going to be a string, it will allocate memory differently for the
two objects, for instance.
Types also allow programming languages to make use of operator overloading. This means that the
symbol + means one thing when applied to two integers, something else when applied to floating-point
numbers, and something completely different when applied to matrices. The concept of type is what
makes it possible for Mathematica to figure out which version of + is called for at the time.
In Mathematica, types are implemented using heads. In Mathematica, every expression is of the form
h[…], just like a function, at least in the internal representation. The symbol h is called the head of the
expression. You can use the function Head to determine the head of any expression.