The first C program in the book, reformatted slightly, is
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
double A[5] = {[0] = 9.0, [1] = 2.9, [2] = 3.0E+25, [3] = .00007};
for (size_t i = 0; i < 5; i++) printf("element %zu is %g, \tits square is %g\n",
i, A[i], A[i] * A[i]);
}
and a similar Fortran code is
implicit none
integer, parameter :: dp = kind(1.0d0)
integer :: i
double precision :: a(5) = [9.0_dp, 2.9_dp, 3.0e25_dp, .00007_dp, 0.0_dp]
do i=1, 5
print*,"element", i, "is", a(i), ", its square is", a(i)**2
end do
end
The output of the C program is
element 0 is 9, its square is 81
element 1 is 2.9, its square is 8.41
element 2 is 3e+25, its square is 9e+50
element 3 is 7e-05, its square is 4.9e-09
element 4 is 0, its square is 0
and of the Fortran program is
element 1 is 9.0000000000000000 , its square is 81.000000000000000
element 2 is 2.8999999999999999 , its square is 8.4100000000000001
element 3 is 3.0000000000000001E+025 , its square is 9.0000000000000003E+050
element 4 is 6.9999999999999994E-005 , its square is 4.8999999999999992E-009
element 5 is 0.0000000000000000 , its square is 0.0000000000000000
The C output does look neater than Fortranās. I wonder what format string would make the Fortran output look closer to that of C. The C printf
statement is harder to read than the Fortran print
statement. A beginner would wonder what the #include
statements do in C, why
int main(void)
follows, and why the Fortran code has implicit none
, defines dp
, and uses it repeatedly. I think C giving A[4]
a value of 0.0 because it was not set is a misfeature. Itās better to fill unset variables with nonsense as Fortran compilers do, to alert the user of a probable bug. Only the Fortran output reveals round-off error. Whether that is good is arguable.