Prev Up Next
Go backward to 6.8 Streams
Go up to 6 Labs
Go forward to 6.10 Bowling program

6.9 Floating Point Precision

This lab will look at one of the limitations of floating point numbers, limited precision. It will also use functions. The use of functions and procedures is a way to break a larger problem up in to smaller parts and to logically divide programs.

Your program needs to print out a header with your name and the lab number (6.9). It will also need 3 column headings, one for the number, one for the next larger number, and one for the difference.

Use the following values when you run your program.

  1. 0.1
  2. 1.0
  3. 10.0
  4. 100.0
  5. 1,000.0
  6. 10,000.0
  7. 100,000.0
  8. 1,000,000.0
  9. 1,000,000,000
If your program can handle zero (0) use it as well.

For this program use both a procedure and a function. To the function pass on of the above numbers. This function needs to return the difference to the next number. Use a procedure for printing out the results of each number. These should include the number, the next number, and the delta. Please print all of the numbers in scientific notation. Make sure that you print with enough precision to show the difference between the initial number and the "next" one.

Mark your output with:

Place the lab in the instructor hand in box in BUS R6E, the "terminal room".As an example of a function and a function call here is a program in Pascal:
{ Sample program showing a function call  }
{ Louis Taber  10/14/97                   }

program func(input,output);

               { Global variables }
var
   i, j: integer;

               { Declaration of the function - SumSquare }
 
function SumSquare( ArgOne:integer; ArgTwo:integer): integer;
begin
SumSquare := ArgOne*ArgOne + ArgTwo*ArgTwo; 
end;           { SumSquare }


{ Main program  - execution starts here }

begin

{ call SumSquare in a loop as part of writeln }

writeln(' Function Demo Program ');

for i :=3 to 4 do
  for j := 4 to 5 do
    writeln(' i =', i:3,'    j =', j:3,
            '    i^2+j^2 = ', SumSquare( i, j):4);
     
writeln;

{ set a variable to result of SumSquare }

i := SumSquare( 23, 56);
writeln(' i =', 23:3,'    j =', 56:3,
        '    i^2+j^2 = ', i:4);

end.
The resulting output when the program is run:
 Function Demo Program
 i =  3    j =  4    i^2+j^2 =   25
 i =  3    j =  5    i^2+j^2 =   34
 i =  4    j =  4    i^2+j^2 =   32
 i =  4    j =  5    i^2+j^2 =   41

 i = 23    j = 56    i^2+j^2 = 3665
As an example of a procedure call, global variables, and argument passing here is a program in Pascal:
{ Sample program showing a procedure call }
{ Louis Taber  10/11/97                   }

program proc(input,output);

              { Global variables }
var
   i, j, k: integer;

               { Declaration of the procedure - variables }
 
procedure variables(var VarArg:integer; j:integer);
begin
writeln;
writeln(' In procedure ');
writeln(' i is global and is passed as VarArg');
writeln(' j is a passed value,  k is global');
writeln(' VarArg is the local name for ');
writeln('        the first passed argument ');
writeln('  i= ',i:2,'  j= ',j:2,
        '  k= ',k:2,'  VarArg= ',VarArg:2);

i := 1;        { Change the global variable } 
j := 1;        { Change the local variable  }
k :=1;         { Change the global variable }
writeln(' In procedure after i,j, & k are changed');
writeln('  i= ',i:2,'  j= ',j:2,
        '  k= ',k:2,'  VarArg= ',VarArg:2);
writeln(' Note that VarArg changed! ');

VarArg:=4;     { Change the passed argument }
writeln(' In procedure after VarArg is changed');
writeln('  i= ',i:2,'  j= ',j:2,
        '  k= ',k:2,'  VarArg= ',VarArg:2);
writeln(' Note that i changed! ');
writeln('  i and VarArg are two names for the ');
writeln('               same variable location. '); 
end;           { variables }


{ Main program  - execution starts here }

begin

i := 0;  j := 0;  k :=0;
writeln(' Before procedure ');
writeln('  i= ',i:2,'  j= ',j:2,'  k= ',k:2);

              { Call procedure variables   }
              { with values i and 8        }
              {  i is called VarArg in procedure }
variables( i, 8);
              { Return from procedure here }
writeln;
writeln(' After procedure ');
writeln('  i= ',i:2,'  j= ',j:2,'  k= ',k:2);
writeln;

end.
When the program is run it produces the following output:
 Before procedure
  i=  0  j=  0  k=  0

 In procedure
 i is global and is passed as VarArg
 j is a passed value,  k is global
 VarArg is the local name for
        the first passed argument
  i=  0  j=  8  k=  0  VarArg=  0
 In procedure after i,j, & k are changed
  i=  1  j=  1  k=  1  VarArg=  1
 Note that VarArg changed!
 In procedure after VarArg is changed
  i=  4  j=  1  k=  1  VarArg=  4
 Note that i changed!
  i and VarArg are two names for the
               same variable location.

 After procedure
  i=  4  j=  0  k=  1


Instructor: ltaber@pima.edu** Red's Home page** The Pima College Site

Prev Up Next