Introduction to Scientific ProgrammingComputational Problem Solving Using:Maple and CMathematica and C

Author:
Joseph L. Zachary
Online Resources:
Maple/C Version
Mathematica/C Version

Significant Digits and Interval Arithmetic Worksheet

Click below to download a Maple V worksheet. You can look at the appended non-interactive HTML version of the worksheet to learn what the worksheet covers.

This worksheet is designed to accompany Chapter 3 of Introduction to Scientific Programming: Computational Problem Solving Using Maple and C by Joseph L. Zachary. In it, we will use Maple to explore the methods of significant digits and interval arithmetic. (30Sep96)

Getting Started

To use this worksheet you will need to use some extensions to Maple that we have created. Read in our interval arithmetic package by evaluating the Maple command below. (You will need to have first installed our custom Maple library and configured Maple to use it.)

> with(interval);

Counting the Digits of a Mantissa

Let's begin by making sure that you know how to count the number of digits in the mantissa of a floating-point number. We have created a function "digitCount" that takes a floating-point number as its parameter and returns the number of digits in its mantissa.

In the absence of trailing and leading zeros, it is straightforward to count digits. Here we see that 1523. has a four-digit mantissa.

> digitCount(1523.);

Here is another way to write 1523. The value of the exponent does not change the significant digit count.

> digitCount(15.23e2);

And here is a third way.

> digitCount(.1523e4);

We have just seen three different ways to write 1523, and each example had four digits in its mantissa.

Trailing zeros are not ignored when counting digits. For example, notice the differences among the following examples.

> digitCount(5000.);

> digitCount(50e2);

> digitCount(.5e4);

Even though all three of these are ways of writing 5000, all three have different numbers of digits in their mantissas.

Leading zeros, unlike trailing zeros, are unimportant and can be disregarded when counting digits in a mantissa.

> digitCount(.51);

> digitCount(.0051e2);

> digitCount(.000051e4);

All three are ways of writing .51, and all three are written with two-digit mantissas.

The last example shows that leading zeros are ignored but trailing zeros are not.

> digitCount(.005100e2);

Exercises

Following are some calls to digitCount. See if you can predict each result before looking at it. Try some of you own examples, too.

> digitCount(100.0);

> digitCount(00002.10);

> digitCount(.0001e4);

> digitCount(1.0001e4);

Rounding Floating-Point Numbers

Next let's make sure that you understand about rounding. When we talk about "rounding a number to four digits," what we mean is "find the closest number with a four-digit mantissa." The function roundFloat will round to a specified number of digits. It always displays its answer with exactly one digit to the left of the decimal point in the mantissa.

If we round 16. to two digits

> roundFloat(16., 2);

we get back 16, since it already consists of two digits. If we round if to one digit,

> roundFloat(16., 1);

we get back 20. If we round it to three digits

> roundFloat(16., 3);

we still get back 16, but this time it is written with three digits in the mantissa.

If we round .000244 to two digits

> roundFloat(.000244, 2);

we get .00024 as the result. Notice how the .000244 was rounded down, because that was where the closest two-digit mantissa was. If we round .000246, on the other hand,

> roundFloat(.000246, 2);

it is rounded up. If a number falls exactly in the middle

> roundFloat(.000245, 2);

Maple rounds up, although it could just as well have rounded down.

Exercises

Here are a few more examples. Try to predict the answer before having Maple evaluate it. Try out some of your own examples, too.

> roundFloat(123., 2);

> roundFloat(1e10, 4);

> roundFloat(1.51, 1);

> roundFloat(.0002345, 1);

> roundFloat(15555550., 2);

> roundFloat(15444449., 2);

Significant Digits

No matter how sophisticated the measuring device, any physical measurement is an approximation. When we write a measurement in scientific notation, the number of digits in the mantissa conveys crucial information. For example, when we say that a measurement is "1500." (four significant digits), we are asserting that the true value, when rounded off to four digits, will be equal to 1500. When we say that a measurement is "1.5e3" (two significant digits), we are asserting that the true value, when rounded off to two digits, will be equal to 1500. There's a big difference in these two cases--assuming that both measurements are correct, the first is much more precise than the second.

To help understand what's going on, we'll use the function "findRange". If we give it a measurement written to some number of significant digits, it will give us back the range in which the true measurement must lie.

For eample, the only numbers which, when rounded to four digits, will equal 1500

> findRange(1500.);

lie in the range from 1499.5 to 1500.5. By comparison, the numbers which, when rounded to two digits, will equal 1500

> findRange(1.5e3);

lie in the much broader range from 1450 to 1550. The first measurement, which was presumably made by a much more precise instrument than the second one, conveys more precise information.

Here are a few more examples. The measurement 1,

> findRange(1.);

written with only one digit, represents a true measurements that lies anywhere from .5 to 1.5. But if we measure more precisely and claim 1.00 as our result,

> findRange(1.00);

we've pinned things down much more closely.

Exercises

Here are some for you to try out. As before, try to figure out what the answer should be before asking Maple to evaluate them. And try some of your own.

> findRange(1.23e5);

> findRange(.0001);

> findRange(7.0001);

> findRange(19.2e5);

Interval Arithmetic

We have now discussed how to count the number of digits in a mantissa, what it means to round a number to some number of digits, and the importance of writing physical measurements so as to indicate the number of significant digits. We can now consider what it means to do arithmetic with measurements.

Suppose that we multiply 3.6 and 4.5, both of which are known to two significant digits.

> 3.6 * 4.5;

Maple gives the answer with a four-digit mantissa, but that doesn't mean that all of the digits in the product are meaningful. The usual rule of thumb is that a product such as this should be rounded to the same number of digits as there are in the least precise measurement involved in the calculation. This is called the method of significant digits. Since both of our measurements here have two significant digits, we should round the 16.20 to 16.

If we take the resulting 16 at face value, we might be tempted to conclude that the true product must lie between 15.5 and 16.5. But we can put exact bounds on the true product.

The smallest value the product can possibly take is if the 3.6 is actually 3.55 and the 4.5 is actually 4.45. In this case, the product is

> 3.55 * 4.45;

The largest value that the product can possibly take is if the 3.6 is actually 3.65 and the 4.5 is actually 4.55. In this case, the product is

> 3.65 * 4.55;

Assuming that the measurements 3.6 and 4.5 are both accurate to two digits, the true answer must lie between 15.7975 and 16.6075.

This approach is called the method of interval arithmetic. The bounds on the true answer that we obtained with interval arithmetic are slightly different than the ones obtained via the method of significant digits. This is because the method of significant digits is only a rule of thumb.

The method of significant digits gets less useful as more numbers become involved in a calculation. Consider

> (3.61 * 4.5) / 17.7;

Here we get a ten-digit answer. The method of significant digits would have us round this to two digits, obtaining .92 as our answer, meaning that the true answer should lie between .915 and .925. By interval arithmetic we obtain the low bound as

> (3.605 * 4.45) / 17.75;

and the high bound as

> (3.615 * 4.55) / 17.65;

The true answer thus lies between .903 and .932. The method of significant digits would have had us believe otherwise.

It can get rather tricky to compute bounds via interval arithmetic. To compute the low bound in the last example, we had to use the smallest possible values for the measurements in the numerator and the largest possible values for the measurements in the denominator, and then reverse the process to obtain the high bound.

The final function provided by the library that you loaded at the beginning of this worksheet, "intervalEval", will do interval arithmetic for us. We give it as its argument the expression that we wish to evaluate with the measurements enclosed in backquotes. (The backquote key is in the upper left-hand corner of most keyboards.) With the example above, we simply do

> intervalEval(`3.61` * `4.5` / `17.7`);

and we get back the interval in which the result must lie.

If a computation involves a number that is NOT a measurement, we simply leave off the backquotes and write it as usual. For example, the calculation of the circumference of the earth from Chapter 3 of the text is

> intervalEval(360 / 7.2 * 50e2 * 0.1575);

We get an exact answer because we didn't put the measurements in backquotes. When we do that

> intervalEval((360 / `7.2`) * (`50e2` * `0.1575`));

we get bounds on the true answer.

Exercises

Doing interval arithmetic can take a big of ingenuity. Given the ranges in which several the measurements involved in a calculation must lie, you must figure out what is the smallest possible answer and what is the largest possible answer. If you don't have access to Maple and our library functions, it can take a bit of thought. As a final exercise, we will give some expressions. Use interval arithmetic to calculate the interval in which the true value must lie, and compare it to the result predicted by the method of significant digits. Then check your answer with "intervalEval". As always, try out some examples of your own.

> 2.5e2 * 2.4e1 + 11.;

> sqrt(45.1) * .001;

> 4.5^3.001 + 100.00;