2013-04-22 38 views
2

这是一个简短的问题,但我尽量给予尽可能多的细节。Fortran算术异常

我正在编译一个旧的,但仍然积极开发的科学Linux上的Fortran代码(f77标准)。此代码的推荐编译器是ifort和gfortran。使用gfortran我可以编译并运行这段代码。但是,如果我使用DEBUG = 1标志编译代码,但会以SEG FAULT结束。使用gdb步进通过导致错误的下列来源:

Program received signal SIGFPE, Arithmetic exception. 
timer (init=1, isecs=0) at myprog.f:1818 
1818 ISECS = 100*INT(TIME_CURRENT) 

如果我停止线1818的执行和检查ISECS和TIME_CURRENT我得到:

REAL*4 TIME_CURRENT  
CALL CPU_TIME(TIME_CURRENT) 
ISECS = 100*INT(TIME_CURRENT) 

该程序将终止

(gdb) ptype(TIME_CURRENT) 
type = real(kind=4) 
(gdb) ptype(ISECS) 
type = integer(kind=4) 

我试过更具体和使用:

ISECS = 100*INT(TIME_CURRENT,4) 

但它没有帮助。我看不出这是如何等于算术错误?

我(生成生成文件)调试编译标志是:

gfortran -fno-automatic -m32 -O0 -g \ 
     -ffpe-trap=invalid,zero,overflow,underflow,precision -static-libgfortran 

当我编译了调试的我不再收到赛格的错,但我的编译标志

gfortran -fno-automatic -m32 -O2 -ffast-math -static-libgfortran 

我不是Fortran程序员,所以任何援助将不胜感激。请注意,我正在编译64位系统,但强制32位编译,因为这是必需的。

回答

9

您的代码看起来不像FORTRAN 77,CPU_TIME来自Fortran 95.无论如何,您的调试选项非常严格。 -ffpe-trap=invalid,zero,overflow,underflow,precision意味着浮点运算的许多合法用途都会导致异常。我建议只使用-ffpe-trap=invalid,zero,overflow

具体从gfortran手册:

Some of the routines in the Fortran runtime library, like CPU_TIME , 
are likely to trigger floating point exceptions when "ffpe-trap=precision" 
is used. For this reason, the use of "ffpe-trap=precision" is not recommended. 
+0

谢谢你,这是一个很好的答案! – dmon 2013-04-22 13:33:30