0
double precision, PARAMETER :: pi = 3.14159265358979323846
integer, parameter :: N =100
double precision :: h
h = 1.0/N

do i = 0, N-1
    u(0, i) = sin(pi*i*h)
end do

It does not give correct values of sin. Is something wrong here?

Black Heart
  • 649
  • 1
  • 7
  • 19
  • 1
    Why do you think you are getting incorrect values? What do you expect, and what do you get instead? Note that the value you give for PI has too many digits in it to be stored precisely in a double precision variable on 64-bit platforms. – FredK Jan 22 '19 at 23:34
  • 1
    you are assigning a single precision value into your ´pi´ constant. You should specify a kind specifier into the literal, like `3.14159265358979323846d0`. Actually, there are much better ways to work with real-type precision, like specifying a kind parameter to the real type, selecting the kind according to a specific precision (`selected_real_kind`), or a storage size (`iso_fortran_env` constants, like `real64`). – Rodrigo Rodrigues Jan 22 '19 at 23:36
  • 2
    What is your declaration of `u`? You seem to be using `u` as if its indices start at 0, which is not the default for Fortran. If you have declared `double precision :: u(5, N)` then the first value of `u` would be at `(1, 1)`, not `(0, 0)` – chw21 Jan 22 '19 at 23:47
  • 1
    REAL*8, dimension(0:500,-5:N+5) :: u. This is how "u" is declared – Black Heart Jan 22 '19 at 23:49
  • Well, it works for me. What are the wrong values you get? What happens when you just `print*, sin(pi/2)`? – chw21 Jan 23 '19 at 01:23
  • I agree it is almost surely a duplicate. @BlackHeart You really have to report your incorrect values in the question. It is really necesaryf or potential reopenning. Also, all declarations must be present. – Vladimir F Героям слава Jan 23 '19 at 07:08
  • 2
    Also, you should change `1.0/N` to someting like `1.d0/N` or `dble(1)/dble(N)` to force double precision for `h`. Here, the variable `h` in in double precision but the computation `1.0/N` is not. – Pierre de Buyl Jan 23 '19 at 10:50

0 Answers0