Debugging Strange Fortran Code: A Simple Delta Function

In summary, a delta function in Fortran is a mathematical concept used to represent a very narrow and infinitely tall spike at a specific point in a function, often used in numerical analysis and simulations. Debugging strange Fortran code is important as it helps identify and fix errors or bugs, improving efficiency and performance. To debug strange Fortran code, you can use debugging tools such as a debugger or a profiler, or employ techniques like print statements or code reviews. Some common errors in Fortran code that can be difficult to debug include syntax, logical, and memory-related errors. To prevent these errors in the future, it is recommended to follow good coding practices, document changes, and use debugging tools and techniques.
  • #1
phynewb
13
0
Hi guys,
I just wrote a simple code for delta function and print it out. Here it is.

Code:
      program main
        implicit none
        integer i
        real*8 del
        real*8,parameter:: ep=1D-2
        real*8,parameter:: pi=3.1415926
        do i=-10,10,1
        write(*,*) i,del(real(i))  ! use function
        enddo
        do i=-10,10,1
        write(*,*) i,(1.0/pi*ep/((real(i))**2+ep**2)) ! no use function
        enddo
        end

      function del(x)
      implicit none
      real*8 del
      real*8 x
      real*8,parameter::ep=1D-2
      real*8,parameter:: pi=3.1415926

      del=(1.0/pi*ep/(x**2+ep**2))

      return
      end
It turns out to be quite wrong by using function del. I cannot figure out what's wrong with the function because the expression is the same. Can you tell me how to debug the function? Thanks!
 
Technology news on Phys.org
  • #2
phynewb said:
Hi guys,
I just wrote a simple code for delta function and print it out. Here it is.

Code:
      program main
        implicit none
        integer i
        real*8 del
        real*8,parameter:: ep=1D-2
        real*8,parameter:: pi=3.1415926
        do i=-10,10,1
        write(*,*) i,del(real(i))  ! use function
        enddo
        do i=-10,10,1
        write(*,*) i,(1.0/pi*ep/((real(i))**2+ep**2)) ! no use function
        enddo
        end

      function del(x)
      implicit none
      real*8 del
      real*8 x
      real*8,parameter::ep=1D-2
      real*8,parameter:: pi=3.1415926

      del=(1.0/pi*ep/(x**2+ep**2))

      return
      end
It turns out to be quite wrong by using function del. I cannot figure out what's wrong with the function because the expression is the same. Can you tell me how to debug the function? Thanks!

What output do your two loops produce?
 
  • #3
Mark44 said:
What output do your two loops produce?

It shows
-10 31.830990148286446
-9 4.42379935910539249E-054
-8 3.96072696157977595E-306
-7 31.830990148286446
-6 4.42379936653263965E-054
-5 0.0000000000000000
-4 31.830990148286446
-3 4.42379937841623373E-054
-2 0.0000000000000000
-1 31.830990148286446
0 4.42380394766179441E-054
1 31.830990148286446
2 4.42380242655975552E-054
3 31.830990148286446
4 4.42380241467614869E-054
5 31.830990148286446
6 4.42380240873434528E-054
7 31.830990148286446
8 4.42380240279254245E-054
9 31.830990148286446
10 4.42380239982164045E-054

-10 3.18309583173281331E-005
-9 3.92974701861929112E-005
-8 4.97358443944407051E-005
-7 6.49610718106421125E-005
-6 8.84191714697638248E-005
-5 1.27323451299340604E-004
-4 1.98942445036508832E-004
-3 3.53673738606087140E-004
-2 7.95754859835665312E-004
-1 3.18278073675496917E-003
0 31.830990148286446
1 3.18278073675496917E-003
2 7.95754859835665312E-004
3 3.53673738606087140E-004
4 1.98942445036508832E-004
5 1.27323451299340604E-004
6 8.84191714697638248E-005
7 6.49610718106421125E-005
8 4.97358443944407051E-005
9 3.92974701861929112E-005
10 3.18309583173281331E-005The upper part is from the function and the lower part is calculated without function. It shall look like sharp gaussian distribution around 0 instead of random distribution.
 
  • #4
I'm pretty sure this line is causing the problem:
Code:
write(*,*) i,del(real(i))  ! use function

In the call to your del function, you are converting an integer value, i, to a four-byte real (a single-precision floating point value), but the function is expecting an eight-byte real (a double-precision floating point value).

I think this will fix the problem:
Code:
write(*,*) i,del(dble(i))  ! use function

Here's a link to a page with more information - http://www.liv.ac.uk/HPC/HTMLF90Course/HTMLF90CourseNotesnode114.html .
 
Last edited by a moderator:
  • #5
Mark44 said:
I'm pretty sure this line is causing the problem:
Code:
write(*,*) i,del(real(i))  ! use function

In the call to your del function, you are converting an integer value, i, to a four-byte real (a single-precision floating point value), but the function is expecting an eight-byte real (a double-precision floating point value).

I think this will fix the problem:
Code:
write(*,*) i,del(dble(i))  ! use function

Here's a link to a page with more information - http://www.liv.ac.uk/HPC/HTMLF90Course/HTMLF90CourseNotesnode114.html .

Thanks Mark44. It really works! I am always troubled by this accuracy stuff...
Thank you a lot :p
 
Last edited by a moderator:
  • #6
It's more about the size, in bytes, of the variables you're using. For floating point variables, the choices are real*4 (single precision - 4 bytes) and real*8 (double precision - 8 bytes). real() converts whatever to a 4-byte floating point value and dble() converts whatever to an 8-byte floating point value.

Make sure that what you pass to a function is what it is expecting - otherwise you'll have problems.
 

1. What is a delta function in Fortran?

A delta function in Fortran is a mathematical concept used to represent a very narrow and infinitely tall spike at a specific point in a function. It is often used in numerical analysis and simulations to model point sources or to approximate certain continuous functions.

2. Why is debugging strange Fortran code important?

Debugging strange Fortran code is important because it helps identify and fix errors or bugs in the code. These errors can lead to incorrect results or program crashes, which can be costly and time-consuming to fix. Debugging can also help improve the efficiency and performance of the code.

3. How do I debug strange Fortran code?

To debug strange Fortran code, you can use debugging tools such as a debugger or a profiler. These tools help you track the execution of the code and identify any errors or inefficiencies. You can also use techniques such as print statements or code reviews to identify and fix errors.

4. What are some common errors in Fortran code that can be difficult to debug?

Some common errors in Fortran code that can be difficult to debug include syntax errors, logical errors, and memory-related errors. These errors can be caused by typos, incorrect use of operators or functions, or improper memory allocation. They can be challenging to identify and fix, especially in complex code.

5. How can I prevent strange Fortran code errors in the future?

To prevent strange Fortran code errors in the future, you can follow good coding practices such as writing clear and organized code, using comments to explain the code, and testing the code regularly. It is also helpful to document any changes made to the code and to keep backups of previous versions in case errors occur. Additionally, using debugging tools and techniques can help catch errors early on and prevent them from causing significant issues.

Similar threads

  • Programming and Computer Science
Replies
4
Views
587
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
22
Views
4K
Back
Top