This notebook contains the Mathematica commands from Chapter 2 of Introduction to Scientific Programming by Joseph L. Zachary.

(2.1)  The simplest kind of an expession is a number.  It evaluates to itself.

57.8

(2.2)  This ill-formed number is an example of a syntax error.  Since Mathematica cannot make sense of it, it reports an errror.

57,8

(2.3)  Here we compute the quotient of two rational numbers.  Notice that the answer is expressed as an exact fraction in lowest terms.

57800000 / 5713000000

(2.4)  We demonstrate that the quotient from (2.3) is exact by multiplying it by the divisor, which yields the dividend.

289/28565 * 5713000000

(2.5)  We do the same computation as in (2.3), but this time using floating-point numbers.  We know these are floating-point numbers because they contain decimal points.  The answer is rounded off to ten digits, not counting the leading zero.

57800000. / 5713000000.

(2.6)  We do the computation from (2.5) one more time, this time using scientific notation to write the floating-point numbers.

57.8*^6 / 5.713*^9

(2.7)  We illustrate that floating-point arithmetic is not exact by multiplying the quotient from the previous example by the divisor.  The number that we get back is almost, but not exactly, the same as the dividend.

.0101173 * 5.713*^9

(2.8)  This illustrates how, by canelling units, Mathematica does dimensional analysis.

72 in * 2.54 cm/in

(2.9)  Here we square a rational number by using the exponentiation operator.

5280^2

(2.10)  This example shows how floating-point and rational numbers can be mixed in a single expression.  Because a floating-point number is involved, the answer is expressed as a floating-point number.

.0101173 * 27878400

(2.11)  We repeat the calculation from (2.10), this time using only rational numbers.  Notice that the result is expressed as a rational number.

289/28565 * 27878400

(2.12)  Here we divide two floating-point numbers, obtaining an intermediate floating-point result, which we multiply by a rational number.  Because the final multiplication involves both a floating-point number and a rational number, the rational number is first converted into floating-point form.

(57.8*^6 / 5.713*^9) * 27878400

(2.13)  This is the same calculation as in (2.12), but here we explicitly compute the square of 5280.

(57.8*^6 / 5.713*^9) * (5280^2)

(2.14)  This is an example of the user of the "Sqrt" built-in function.  It returns the square root of its parameter.

Sqrt[282053.]

(2.15)  The parameter to "Sqrt" can be an arbitrary expression.  Here we take the square root of the value of an expression that involves division, exponentiation, and multiplication.

Sqrt[(57.8*^6 / 5.713*^9) * (5280^2)]

(2.16)  Here we take the same square root as in (2.15), but we use rational numbers.  An exact result is produced using a radical.

Sqrt[(57800000 / 5713000000) * (5280^2)]

(2.17)  The "N" built-in function converts a rational number into floating-point form.

N[17952 * Sqrt[5/5713]]

(2.18)  The notebook interface permits the entry of expressions in a typeset format.  Here is a different version of (2.14).

(Sqrt[282053.])

(2.19)  And here is a different version of (2.16).

(Sqrt[(57800000/5713000000) (5280^2)])

(2.21)  There is a limit on the sizes of rational numbers that can be computed.  Although the limit aries from computer to computer, this expression will probably exceed the limit on your computer.  You will almost certainly end up having to abort the computation.

10^600000

(2.22)  Even though this expression involves large exponents, Mathematica is able to quickly reduce this to 1.

10^(600000-600000)

(2.23)  This is algebraically identical to (2.22), but you will nevertheless have to abort this calculation.

10^600000 / 10^600000

(2.24)  The value of this constant is the precision of Mathematica's floating-point numbers on your computer.

$MachinePrecision

(2.25)  This is an exact rational calculation ...

1234567890^2

(2.26)  ... and this is the same calculation done with floating-point numbers.  Notice that Mathematica only displays the first six digits of the mantissa.

1234567890.^2.

(2.27)  By using the built-in function "InputForm", we can see all of the digits of the number calculated in (2.26).  Notice that only 16 digits are available, even though there were 17 digits in the rational answer calculated in (2.25).

InputForm[1234567890.^2.]

(2.28)  Here we look at all of the digits in a quotient we evaluated several times earlier.

InputForm[57.8*^6 / 5.713*^9]

(2.29)  We use the 16-digit result from (2.28) to recalculate (2.10), where we earlier used only a six-digit multiplicand.  The product differs in the sixth digit.

0.01011727638718711 * 27878400

(2.30)  This is the largest floating-point number on your computer.

$MaxMachineNumber

(2.31)  And this is the smallest floating-point number on your computer.

$MinMachineNumber

(2.32)  If you try to calculate a floating-point number that is too big, you'll get an overflow error.

10.^1000000000.

(2.33)  And if you try to calculate a floating-point number that is too small, you'll get an underflow error.

10.^-1000000000.

(2.34)  These are all of the digits in the floating-point calculation for the problem studied in Chapter 2.

InputForm[Sqrt[(57.8*^6 / 5.713*^9) * (5280.^2)]]

(2.35)  And these are all the digits in the rational calculation, after it has been converted to floating-point form at the last step.

InputForm[N[17952 * Sqrt[5/5713]]]