[Bug fortran/45624] New: Division by zero compiler error

2010-09-09 Thread leftynm at umich dot edu
I am running Fortran 90 on a MacBook Pro, 10.6.4, with a 2.8 GHz Intel Core 2
Duo.
I use terminal to run the code, and my compiler is gfortran.
When I type gfortran -v this is what it tells me:

Using built-in specs.
Target: i386-apple-darwin8.10.1
Configured with: /tmp/gfortran-20090604/ibin/../gcc/configure
--prefix=/usr/local/gfortran --enable-languages=c,fortran
--with-gmp=/tmp/gfortran-20090604/gfortran_libs --enable-bootstrap
Thread model: posix
gcc version 4.5.0 20090604 (experimental) [trunk revision 148180] (GCC) 


Ok, I hope that is enough information off the bat.  Here is my problem I
encountered in my code, shown here in a most simplified form to focus on the
problem:

PROGRAM test

IMPLICIT NONE
REAL*8, PARAMETER   :: alpha = 0.D0
REAL*8, DIMENSION(5,5)  :: matrix
INTEGER :: i

matrix = 0.D0

DO i=1,5
IF (alpha == 0.D0) THEN
matrix(i,i) = 0.D0
ELSE
matrix(i,i) = 1.D0/alpha
ENDIF
ENDDO

END PROGRAM


When I compile, I get the following:
$ gfortran test.F90 

test.F90:14.26:

matrix(i,i) = 1.D0/alpha
  1
Error: Division by zero at (1)


Yes alpha is zero, and yes that line shows me dividing by it, however, I only
divide by this number when it is non-zero.  In my code, most of the time this
number is non-zero. When it is zero, I treat it with an IF-THEN-ELSE type
scenario that does not divide by zero.  The compiler doesn't realize this.

I've tried other variants (such as ELSEIF or switching the order, or using
IF-statements instead of IF-blocks), but they don't seem to matter.

I tried searching for this problem in forums, and I did read the through
gfortran manual (to avoid a RTFM answer).  However, I wasn't able to find a
similar problem or an option to fix this.  

It may seem that not making the variable alpha a PARAMETER will fix the
problem.  However, this isn't an option because of how the problem manifests
within my code.  I need to set this variable as a parameter because of how it
is used elsewhere and passed between modules and subroutines (of course it
doesn't matter in the toy program I showed here).

Thanks for your time.


-- 
   Summary: Division by zero compiler error
   Product: gcc
   Version: 4.5.0
Status: UNCONFIRMED
  Severity: major
  Priority: P3
 Component: fortran
AssignedTo: unassigned at gcc dot gnu dot org
    ReportedBy: leftynm at umich dot edu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45624



[Bug fortran/45624] Division by zero compiler error

2010-09-09 Thread leftynm at umich dot edu


--- Comment #3 from leftynm at umich dot edu  2010-09-09 22:56 ---
Ok, if the fact that the variable is a parameter causes this error, I'll have
to adjust the rest of my code accordingly to work around this problem.  It
seems like there should be a better way to fix this, since I want to protect
this variable.
But, I'll take it.  Thanks for the quick reply.


-- 

leftynm at umich dot edu changed:

   What|Removed |Added

 Status|UNCONFIRMED |RESOLVED
 Resolution||WORKSFORME


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45624



[Bug fortran/45624] Division by zero compiler error

2010-09-10 Thread leftynm at umich dot edu


--- Comment #5 from leftynm at umich dot edu  2010-09-10 16:06 ---
Thanks guys.  Yeah, I guess my use of PARAMETER wasn't consistent with how it
works.  I was using it to set a variable such that it cannot be changed.  I
found a work around though lets me keep it as a PARAMETER, but allows me to
compile.

PROGRAM test

IMPLICIT NONE
REAL*8, PARAMETER   :: alpha = 0.D0
REAL*8, DIMENSION(5,5)  :: matrix
INTEGER :: i
REAL*8  :: dummy

matrix = 0.D0

DO i=1,5
IF (alpha == 0.D0) THEN
matrix(i,i) = 0.D0
ELSE
dummy = alpha
matrix(i,i) = 1.D0/dummy
ENDIF
ENDDO

END PROGRAM

This is what I need, since when I don't make my variable a PARAMETER, it causes
other problems.  This requires less work to fix, so I'm happy with it.  I tried
PROTECTED, but my code has to be F90 because it's a small part of a bigger
project.

Thanks to everyone though.  I wouldn't have figured this out without your
inputs.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45624