7.4: Test/Debug the Program
- Page ID
- 54270
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)Once the program is written, testing should be performed to ensure that the program works. The testing will be based on the specific parameters of the program. In this example, each of the three possible values for the discriminant should be tested.
C:\mydir> period Pendulum Period Calculation Program Enter Length and Angle values: 120.0 15.0 The Period is: 2.20801973 C:\mydir>
For this program, the results can be verified with a calculator. A series of different values should be used for testing. If the program does not work, the program comments provide a checklist of steps and can be used to help debug the program.
Error Terminology
In case the program does not work, it helps to understand some basic terminology about where or what the error might be.
Compiler Error
Compiler errors are generated when the program is compiled. This means that the compiler does not understand the instructions. The compiler will provide a list of errors with the line number of each error. It is recommended to address the errors from the top down. Resolving an error at the top can clear multiple errors further down.
Typical compiler errors include misspelling a statement and/or omitting a variable declaration. For example, if the correct Fortran statement “write (*,*)
” is entered incorrectly as “wrote (*,*)
”, an error will be generated.
In this case, the compiler error displayed will appear as follows:
c:\mydir> gfortran -o period period.f95 period.f95:13.1: wrote (*,*) 1 Error: Unclassifiable statement at (1)
The first digit, 13 in this example, represents the line number where the error occurred. Using a text editor that displays line numbers, the statement that caused the error can be quickly found and corrected.
If the declaration for the variable length
is omitted, the error would appear as follows:
c:\mydir> gfortran -o period period.f95 period.f95:17.18: read (*,*) length, angle 1 Error: Symbol 'length' at (1) has no IMPLICIT type
In this case, the error is shown on line 18 (first digit after the “:”). However, the actual error is that the variable length is not declared. Each error should be reviewed and evaluated.
Run-time Error
A run-time error is something that causes the program to crash. For example, if a number is requested and a letter is entered, it will cause a run-time error.
For example, the period program expects two real numbers to be entered. If the user enters letters, x and y, in this example, an error will be generated during the execution of the program as follows:
c:\mydir> period Pendulum Period Calculation Program Enter Length and Angle values: x y At line 17 of file period.f95 (unit = 5, file = 'stdin') Fortran runtime error: Bad real number in item 1 of list input
The program was expecting numeric values and letters were provided. Since letters are not meaningful in this context, it is an error and the program “crashes” or stops with an error message.
Later chapters will provide additional information on how to deal with such errors. Until then, providing the correct data type will avoid this kind of error.
Logic Error
A logic error is when the program executes, but does not produce the correct result. For example, coding a provided formula incorrectly or attempting to compute the average of a series of numbers before calculating the sum would be considered a logic error.
For example, the correct formula for the period of a pendulum is as follows:
pperiod = 2.0 * pi * sqrt(length/gravity) * & ( 1.0 + 1.0/4.0 * sin(alpha/2.0)**2 )
If the formula is typed incorrectly or incompletely as follows:
pperiod = 2.0 * pi * sqrt(length/gravity) * & ( 1.0 + 1/4 * sin(alpha/2.0)**2 )
The 1 over 4 is entered as “1/4” which are interpreted as integers. As integers, “1/4” results in 0. The compiler will accept this, perform the calculations, and provide an incorrect result.
The program would compile and execute as follows.
c:\mydir> period Pendulum Period Calculation Program Enter Length and Angle values: 120.0 15.0 The period is: 2.19865513
However, an incorrect answer would be generated as shown. This is why testing the program is required. Logic errors can be the most difficult to find.
One of the best ways to handle logic errors is to avoid them by careful development of the algorithm and writing the code.
If the program has a logic error, one way to find the error is to display intermediate values. Further information will be provided in later chapters regarding advice on finding logic errors.