-1

I run a gfortran code for multiplication and division and they give me different results when compared to results by c++,c etc. All use double precision. I believe that double precision I use in fortran is not correct or it is broken... I've checked numbers with calculator and fortran seems to create some numbers in the near end of decimals... Below is the code in gfortran,

PROGRAM problem
!-----------------------------------------------------------------------
integer, parameter :: dp = selected_real_kind(15,307)
real(dp) :: answer,w

open(1, file = 'problem.txt')  
w=0.99
answer=w
do i = 10, 0, -1
answer = answer*w
write (*,"(E18.9)",advance="yes") answer
!-print results to text file
write (1,"(E18.9)",advance="yes") answer
end do
write(*,*) "Done."
close(1)
END PROGRAM problem

gfortran results are,

   0.980100019E+00
   0.970299028E+00
   0.960596047E+00
   0.950990096E+00
   0.941480204E+00
   0.932065411E+00
   0.922744766E+00
   0.913517327E+00
   0.904382162E+00
   0.895338349E+00
   0.886384974E+00

my calculator shows,

 0.9801
 0.970299
 0.96059601
 0.950990049

Am I missing something in variable type declaration or is it intrinsic in gfortran?

Aschoolar
  • 343
  • 3
  • 9

1 Answers1

1

Although you've declared w to be double precision, you've initialised it to 0.99, which is only a single-precision constant. In order to initialise w as a double precision constant, you need w = 0.99_dp.

veryreverie
  • 2,871
  • 2
  • 13
  • 26