Help with fortran numerical precision

Click For Summary

Discussion Overview

The discussion revolves around a Fortran code issue related to numerical precision, specifically concerning the normalization of a function that is expected to yield a result of 1.0 but instead produces a value close to it. Participants explore the implications of using different precision settings in Fortran and the effects of numerical integration on the results.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant, Cayo, describes a problem with their Fortran code where the normalization factor is not yielding the expected double precision result.
  • Another participant suggests that the use of `kind=8` does not guarantee double precision, as its implementation can vary by compiler.
  • A later reply proposes using a parameter to define double precision explicitly, but Cayo reports that this change did not resolve the issue.
  • Another participant questions the expectation of achieving an exact normalization of 1.0, pointing out that the integral is being approximated over a finite grid, which inherently introduces error.
  • Cayo acknowledges this point and attributes the discrepancy to the grid size being too small.

Areas of Agreement / Disagreement

Participants express differing views on the expectations of numerical precision in the context of finite grid approximations. There is no consensus on the root cause of the issue, as some focus on precision settings while others emphasize the limitations of numerical integration.

Contextual Notes

Participants note that the Fortran standard does not specify how precision kinds are implemented, leading to potential variations in behavior across different compilers. The discussion also highlights the limitations of numerical methods when approximating integrals.

Cayo
Messages
3
Reaction score
0
Hello. I need help to understand why my code is not giving me double numerical precision.
In this piece of code, I create a function that is analytically normalized, but when I calculate the numerical normalization factor, it seems to be with single precision. Here it is:

Fortran:
program dyna
implicit none
integer i
integer, parameter :: Nq1=61
real(kind=8), parameter :: saw2au=1822.889950851334d0 
real(kind=8), parameter :: nitro=14.0067d0*saw2au 
real(kind=8), parameter :: mredu=nitro**2.0d0/(2.0d0*nitro)
real(kind=8) :: e0,pi,ch,x,x0,stepX,w,expo,c0,c1
complex(kind=8) :: soma,vec0(Nq1)
pi = 3.141592653589793d0
e0=0.005d0
x0=2.09970623d0 
stepX=0.01d0
w=2.d0*e0
c0 = ((mredu*w)/pi)**(1.d0/4.d0)
c1 = (4.d0/pi*(mredu*w)**3.d0)**(1.d0/4.d0)
do i=1,Nq1
  ch=(i-(Nq1-1.d0)/2.d0-1.d0)*stepX
  x=x0+ch 
  expo = dexp(-(x-x0)**2.d0*mredu*w/2.d0)
  vec0(i) = c0 * expo
end do
!----------------------------------------!
!normalizing                             !
soma=0.0d0                               !
do i=1,Nq1                               !
  soma=soma+conjg(vec0(i))*vec0(i)*stepX !
end do                                   !
vec0=vec0/sqrt(soma)                     !
!----------------------------------------!
write(*,'(a24,2(f23.15))')'normalization constant =',soma
end program

I get 0.999998932390816, and it should be 1.0d0.
I use complex vectors because later in the code they will become complex.
I don't understand where is the problem. Can someone help me, please?
Thanks in advance,
Cayo
 
Technology news on Phys.org
kind=8 doesn't guarantee that you will get double precision. The Fortran 90 standard doesn't specify how kind is to be implemented, so it is compiler-dependent.

If you want to insure that all variables are declared as double precision, you can use
Fortran:
integer, parameter :: dp = kind(1.d0)

real(kind=dp) :: x
complex(kind=dp) :: z
 
Thank you for the reply, you are correct.
But, I changed what you suggested and it didn't changed the result. Any other idea?
 
I looked at your code in more details, and I don't understand why you say that
Cayo said:
it should be 1.0d0.
You are approximating an integral from ##-\infty## to ##\infty## using a finite grid. Why would you expect that to be an exact? Check the values of vec0(1) and vec0(Nq1).
 
Hello DrClaude,
You are right. Thats the problem, the size of the grid is too small. Thank you for the help.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 54 ·
2
Replies
54
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K