### Introduction to Scientific Programming Computational Problem Solving Using: Maple and C Mathematica and C

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

# Rational Number Tutorial

In this tutorial we will explore the nature of rational numbers, as explained in Chapter 2. The tutorial will help you understand the significance of the minimum and maximum integers in a rational number system and the meaning of overflow.

## Simulation

We will be using a rational number simulator throughout this tutorial. You can start it by clicking on the following button.

If you see this, then Java is not running in your browser!
An applet would normally go here...

As discussed in Chapter 2, a rational number system is characterized by

• a maximum integer (maxint) and
• a minimum integer (minint).
Such a system contains all of the integers between minint and maxint, as well as all ratios of those integers (except that zero cannot be used as a denominator).

The rational number simulator allows you to design your own rational number systems. When the simulator first appears, it will be simulating a system with a minimum integer of -2 and a maximum integer of 2. You can verify this by looking at the two controls at the top of the simulator window.

The simulator also displays a list and a graph of the numbers in the rational number system. Finally, the simulator allows you to calculate the values of arithmetic expressions in the rational number system. (Initially, it displays the result of adding 1 and 1.)

Locate all of these pieces of information in the simulator window before going on.

## The Numbers in a Rational Number System

With a minimum integer of -2 and a maximum integer of 2, the simulator reveals that there are only seven valid rational numbers. Those numbers include
```-2  -1  0  1  2
```
because these are the integers between -2 and 2, and the fractions
```-2/-2  -2/-1  -2/1  -2/2
-1/-2  -1/-1  -1/1  -1/2
0/-2   0/-1   0/1   0/2
1/-2   1/-1   1/1   1/2
2/-2   2/-1   2/1   2/2
```
because these are all valid ratios of the five integers.

Of course, the five integers and the twenty fractions listed above contain many duplicates, which is why there are only seven numbers listed and graphed in the simulator window.

Let's modify the rational number system being simulated and see how that changes the collection of available numbers. Use the controls at the top of the window to change the minimum integer to -3 and the maximum integer to 3.

Increasing the range of the allowable integers adds eight new numbers to the rational number system.

## Exercises

1. Try to get a feel for how the minimum and maximum integers control what rational numbers exist. To do this, you will need to adjust the controls at the top of the simulator window. (The graph at the bottom of the simulator is sometimes slow to redisplay. If you get impatient, you can interrupt it by using the Halt menu at the top of the simulator window.)

2. Below are several sets of numbers. For each, what is the simplest rational number system that contains each number in the set? Use the rational number simulator to verify your answers.
• {1/2, 1/3, 1/4}
• {6, 5/6, -6, -5/6}
• {1/2, 1/3, 1/4, -1/2, -1/3, -1/4}

## Overflow

Now let's turn our attention to what happens when we do arithmetic calculations with rational numbers. Let's return to a simple system with a minimum integer of -2 and a maximum integer of 2. Either do it yourself, or click below.

We will now begin using the expression input region, which should be displaying the result of calculating "1+1". Delete the contents of the input region and enter the product "1*1" instead. When you hit the return key, the product of the two numbers in the current rational number system will be displayed.

Now enter the expression "2+2". This time when you hit the return key, the message

```Computation error - 4 out of bounds
```
will be displayed. This error is called an overflow error, and it occurred because the value of the expression was too large to represent in the current system: 4 is not a valid rational number when the minimum integer is -2 and the maximum integer is 2. You can verify this by looking at the list of rational numbers that the simulator displays.

Now use the controls to increase the maximum integer to 4. Because 4 is a member of this system, the value "2+2" will be displayed.

Rational number systems on computers have the following property: When you ask for an arithmetic expression to be evaluated, you will either get an exact answer or an overflow error. An overflow error occurs when either the numerator or the denominator of the result would be either smaller than the minimum integer or larger than the maximum integer.

Note that overflow can occur in the middle of a complicated expression, even though the answer can be represented in a rational number system. To see this, try evaluating the expression "4+4-4" in the current system. The simulator will report the overflow that occurs when it tries to add 4 and 4. If you enter the algebraically identical "4+(4-4)" instead, you will not get an overflow. Be sure that you understand the difference.

## Exercises

1. Use the simulator to get a better feel for what overflow is. Experiment with different minimum integers, maximum integers, and arithmetic calculations.

Below are several arithmetic calculations. What is the simplest rational number system in which the calculations can be performed without an overflow error occurring? Use the simulator to verify your answers.

• 2 * 2 / -3
• 1/2 + 1/3
• 1/3 * -1/3
• 3 - 4