Programming Assignment I

Due on Friday, September 26, 2008 @ 11pm

100 points

The cosine function cos(x) can be represented as an infinite series as shown below. An obvious way of computing cos(x) is to sum the terms until a term is less than certain tolerance value, say 0.0000005, in single precision.

This is not a good method to compute cos(x) accurately and Fortran 90 does not use it to implement the COS(x) intrinsic function, because there could be so many numerical problems. However, this is a very good chance for you to gain some experience in finite precision arithmetic.

The problem you should solve is very simple:

Read in a real number x, and sum the above infinite series until a term is smaller than the given tolerance 0.0000005. Note that x can be any value, large or small, positive or negative, etc. You should not expect x to be of some special type and in some range. Moreover, to yield a reasonably good result, some understanding of trigonometry functions would be needed. A very small tolerance or even the use of double precision may not help much. Consequently, use the default single precision only (i.e., REAL without any KIND).

Input and Output

The input to your program consists of an unknown number of lines, each of which has exactly one correctly entered real value. The following input has five lines, and the values are 1000.0, 0.5, -20.5, 0.0, and +3.1456.

1000.0
0.5
-20.5
0.0
+3.1456

For each x, your program should print the following four output lines. The first line shows the input value x, the second is the COS(x) computed using the infinite series (i.e., your COS(x)), the third shows the result computed by Fortran 90's intrinsic function COS(x), and the fourth is the absolute value of the difference of the two computed values.


Input Value    = x
My cos(x)      = cos(x) computed using the above infinite series
Fortran cos(x) = Result from Fortran 90 intrinsic function COS(x)
Error          = The absolute difference of the two computed value

The above four output lines should be repeated for each input value with a blank line between two groups. The complete output should look like the following:


*** CS3911 Programming Assignment #1 ***
***   COS(x) Computation Experiment  ***

--- the above four lines for the first input value ---

--- the above four lines for the second input value ---

--- output for other input values ---

*** Done due to end-of-file ***

The following is a possible input file, which will be used as a sample testing input:


-70000.0
-10000.0
-1500.0
-50.0
-10.0
-1.0
0.0
1.5
3.14156
7.0
15.0
100.0
1000.0
100000.0
500000.0

Submission Guidelines

  1. All programs must be written in ANSI Fortran 90.
  2. Use the submit command to submit your work. You can submit as many times as you want, but only the last on-time one will be graded.
  3. Your program should be named as prog1.f90. Note that Unix filename is case sensitive. As a result, PROG1.f90, prog1.F90, pROG1.f90, etc are not acceptable.
  4. We will use the following approach to compile and test your programs:
    f90 prog1.f90 -o prog1  <-- compile your program
    prog1 < our-test-file   <-- test your program
    
    This procedure may be repeated a number of times with different input files to see if your program works correctly.
  5. Your implementation should fulfill the program specification as stated. Any deviation from the specification will cause you to receive zero point.
  6. No late submission will be graded.
  7. My rule of grading is deduction based. I assume you always receive 100%. If you missed something, some points will be deducted based on the following rules. Read these rules carefully. Otherwise, you may receive low or very low grade.
  8. Since the rules are all clearly stated, no leniency will be given and none of the above conditions is negotiable. So, if you have anything in doubt, please ask for clarification.
  9. Click here to see how your program will be graded.