0

Here is the Fortran 90 code I have written. It is a function that I am using in a larger code but I am getting incorrect results.

I know mathematically the answer should be 1 (or close to 1 due to computation) but my answer result is 0.

Am I doing something wrong? Is there something that I do not know?

Here is the my code saved as test.f90 that I am compiling and running on windows gfortran:

program main
    implicit none

    real*8 :: y, t

    t = 0.0
    y = (1/3)*exp(5*t) + (2/3)*exp(-t) + t*t*exp(2*t)

    print*, 'y= ', y

end program main

1 Answers1

1

fortran considers 1/3 and 2/3 to be equal to zero. you should add a point to your numbers:

program main
implicit none

real*8 :: y, t

t = 0.0
y = (1./3.)*exp(5*t) + (2./3.)*exp(-t) + t*t*exp(2*t)

print*, 'y= ', y
  • This answer really should be expanded - why is `1/3` and `2/3` equal to 0, and why do the decimals help? Also, there are clearly duplicates to point to... – Ross Feb 16 '20 at 23:24
  • 1
    Furthermore, `2./3.` are typically single-precision reals, which means the result will be inaccurate compared to `2._dp/3._dp`, where `dp` is the relevant kind. Using `*8*` is not recommended. – Ross Feb 16 '20 at 23:28
  • 1
    have a look at this article: http://www.math.hawaii.edu/~hile/fortran/fort3.htm#double – Valometrics.com Feb 16 '20 at 23:30