Help with fortran numerical precision

In summary, the conversation discusses a code that creates a function with analytical normalization, but when calculating the numerical normalization factor, it seems to be using single precision. The code uses complex vectors and the problem may be due to the compiler not guaranteeing double precision with the kind=8 declaration. However, upon further examination, it is discovered that the issue is due to a finite grid approximation and not an exact result.
  • #1
Cayo
3
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
  • #2
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
 
  • #3
Thank you for the reply, you are correct.
But, I changed what you suggested and it didn't changed the result. Any other idea?
 
  • #4
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).
 
  • #5
Hello DrClaude,
You are right. Thats the problem, the size of the grid is too small. Thank you for the help.
 

1. What is numerical precision in Fortran?

Numerical precision in Fortran refers to the accuracy with which a computer can represent and perform calculations on numbers. It is determined by the number of digits used to represent a number and the range of values that can be represented.

2. Why is numerical precision important in scientific computing?

Numerical precision is important in scientific computing because it affects the accuracy and reliability of calculations. In many scientific applications, even small errors in numerical precision can lead to significant errors in results.

3. How does Fortran handle numerical precision?

Fortran has built-in data types that allow for different levels of numerical precision. The most commonly used data types are REAL and DOUBLE PRECISION, which have different levels of precision and range. Fortran also has features such as KIND parameters and SELECTED_REAL_KIND that allow for more control over precision.

4. How can I optimize numerical precision in my Fortran code?

To optimize numerical precision in Fortran, it is important to choose the appropriate data types for your variables based on the level of precision and range needed. It is also helpful to use features such as KIND parameters and SELECTED_REAL_KIND to control precision. Additionally, it is important to avoid unnecessary conversions between data types and to use compiler options that optimize for precision.

5. What are some common pitfalls to watch out for with numerical precision in Fortran?

Some common pitfalls to watch out for with numerical precision in Fortran include using the wrong data type for a variable, performing calculations in a way that leads to loss of precision, and not considering the effects of rounding errors in calculations. It is also important to pay attention to compiler options and settings that may affect numerical precision.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
620
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
2
Replies
54
Views
4K
  • Programming and Computer Science
Replies
13
Views
3K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
4K
Back
Top