Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Divide by zero but never called

Status
Not open for further replies.

belson17

Programmer
Mar 19, 2010
4
US
Hello,
I'm trying to compile code originally written for intel ifort compiler with gfortan, and I've come across an error that I was able to reproduce with the following small program:
Code:
      program testdivzero
      implicit none
      integer a,b
      parameter (b=0)
      
      do a=1,b
        write(*,*) 0.5/real(b)
      end do
      
      end program testdivzero

Compiling gives:
Code:
gfortran -c testdivzero.F 
testdivzero.F:8.22:

        write(*,*) 0.5/real(b)                                          
                      1
Error: Division by zero at (1)
testdivzero.F:7.72:

      do a=1,b                                                          
                                                                        1
Warning: DO loop at (1) will be executed zero times

So the compiler knows that the contents of the loop will never be executed, but it fails to realize that this also means the divide by zero can never occur.
Does anyone know a way to resolve this? Again, this is just to show the error, this program is pointless on its own but I get the error in my real project.
Thanks a lot!
 
Also... it gives the same problem without using real(), so if the write line is changed to:
Code:
write(*,*) 1/b
the same error and warning occur.
 
It depends on the level of optimization. Some levels look at just one statement, others look at the rest of it.

If something is declared as a parameter, then the code will be generated using that value - that is how it knows you are dividing by zero and looping zero times. To get up to the next level where it examines what you do inside the loop will take a bit of thinking. One way around it is to use a variable instead of a constant
Code:
program testdivzero
      implicit none
      integer a,b
      parameter (bzero=0)
      
      b = bzero
      do a=1,b
        write(*,*) 0.5/real(b)
      end do
      
      end program testdivzero
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top