public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/45624] New: Division by zero compiler error
@ 2010-09-09 21:56 leftynm at umich dot edu
2010-09-09 22:19 ` [Bug fortran/45624] " pinskia at gcc dot gnu dot org
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: leftynm at umich dot edu @ 2010-09-09 21:56 UTC (permalink / raw)
To: gcc-bugs
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug fortran/45624] Division by zero compiler error
2010-09-09 21:56 [Bug fortran/45624] New: Division by zero compiler error leftynm at umich dot edu
@ 2010-09-09 22:19 ` pinskia at gcc dot gnu dot org
2010-09-09 22:26 ` kargl at gcc dot gnu dot org
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2010-09-09 22:19 UTC (permalink / raw)
To: gcc-bugs
------- Comment #1 from pinskia at gcc dot gnu dot org 2010-09-09 22:19 -------
PARAMETER are special as it is an exact replacement for those variables.
--
pinskia at gcc dot gnu dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
Severity|major |normal
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45624
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug fortran/45624] Division by zero compiler error
2010-09-09 21:56 [Bug fortran/45624] New: Division by zero compiler error leftynm at umich dot edu
2010-09-09 22:19 ` [Bug fortran/45624] " pinskia at gcc dot gnu dot org
@ 2010-09-09 22:26 ` kargl at gcc dot gnu dot org
2010-09-09 22:56 ` leftynm at umich dot edu
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: kargl at gcc dot gnu dot org @ 2010-09-09 22:26 UTC (permalink / raw)
To: gcc-bugs
------- Comment #2 from kargl at gcc dot gnu dot org 2010-09-09 22:25 -------
There is no way to fix this problem unless you
would like +inf along the diagonal.
gfortran will constant fold 1./alpha if alpha has
the parameter attribute. After all, this attribute
tells the compiler that alpha is a named constant,
ie., it will not change.
PS: Fortran bugs are never given a severity high
than normal unless the bug breaks bootstrapping
the compiler.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45624
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug fortran/45624] Division by zero compiler error
2010-09-09 21:56 [Bug fortran/45624] New: Division by zero compiler error leftynm at umich dot edu
2010-09-09 22:19 ` [Bug fortran/45624] " pinskia at gcc dot gnu dot org
2010-09-09 22:26 ` kargl at gcc dot gnu dot org
@ 2010-09-09 22:56 ` leftynm at umich dot edu
2010-09-09 23:20 ` dominiq at lps dot ens dot fr
2010-09-10 16:06 ` leftynm at umich dot edu
4 siblings, 0 replies; 6+ messages in thread
From: leftynm at umich dot edu @ 2010-09-09 22:56 UTC (permalink / raw)
To: gcc-bugs
------- 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
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug fortran/45624] Division by zero compiler error
2010-09-09 21:56 [Bug fortran/45624] New: Division by zero compiler error leftynm at umich dot edu
` (2 preceding siblings ...)
2010-09-09 22:56 ` leftynm at umich dot edu
@ 2010-09-09 23:20 ` dominiq at lps dot ens dot fr
2010-09-10 16:06 ` leftynm at umich dot edu
4 siblings, 0 replies; 6+ messages in thread
From: dominiq at lps dot ens dot fr @ 2010-09-09 23:20 UTC (permalink / raw)
To: gcc-bugs
------- Comment #4 from dominiq at lps dot ens dot fr 2010-09-09 23:20 -------
You can use the option -fno-range-check. However, the code itself and the
sentence "since I want to protect this variable" in comment #3 let me suspect
that you have not understood what PARAMETER is for: a variable with the
PARAMETER "attribute" is an alias for its value and exists only during the
compilation.
What you seem to want is the attribute PROTECTED, i.e. a variable that can be
changed only in a specified place as in (F2003 standard draft):
An example of the PROTECTED attribute:
MODULE temperature
REAL, PROTECTED :: temp_c, temp_f
CONTAINS
SUBROUTINE set_temperature_c(c)
REAL, INTENT(IN) :: c
temp_c = c
temp_f = temp_c*(9.0/5.0) + 32
END SUBROUTINE
END MODULE
The PROTECTED attribute ensures that the variables temp_c and temp_f cannot be
modi\fed other than
via the set_temperature_c procedure, thus keeping them consistent with each
other.
The PROTECTED attribute is a f2003 addition, implemented at least from gfortran
4.4.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45624
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Bug fortran/45624] Division by zero compiler error
2010-09-09 21:56 [Bug fortran/45624] New: Division by zero compiler error leftynm at umich dot edu
` (3 preceding siblings ...)
2010-09-09 23:20 ` dominiq at lps dot ens dot fr
@ 2010-09-10 16:06 ` leftynm at umich dot edu
4 siblings, 0 replies; 6+ messages in thread
From: leftynm at umich dot edu @ 2010-09-10 16:06 UTC (permalink / raw)
To: gcc-bugs
------- 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
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2010-09-10 16:06 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-09-09 21:56 [Bug fortran/45624] New: Division by zero compiler error leftynm at umich dot edu
2010-09-09 22:19 ` [Bug fortran/45624] " pinskia at gcc dot gnu dot org
2010-09-09 22:26 ` kargl at gcc dot gnu dot org
2010-09-09 22:56 ` leftynm at umich dot edu
2010-09-09 23:20 ` dominiq at lps dot ens dot fr
2010-09-10 16:06 ` leftynm at umich dot edu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).