以下简单代码:
program small_test
double precision :: a, b, c, d
open(5,file='infile.dat',status='old')
READ (5,*) a, b, c, d
print *, a, b, c, d
end program当我使用gfortran编译时,没有陷阱标志,工作得很好:
$> gfortran small_test.f90输入数据是
0.087266463 0.087266463 3. 100.输出是
8.7266463000000002E-002 8.7266463000000002E-002 3.0000000000000000 100.00000000000000如预期的那样。
但当我编译来捕获浮点错误时,
gfortran -ffpe-trap=invalid,zero,overflow,underflow,precision,denormal -fdump-core small_test.f90代码出现错误失败。
Program received signal SIGFPE: Floating-point exception - erroneous arithmetic operation.这个简单的代码怎么可能产生错误呢?
(真正发生的是,我正在调试一个更大的代码,我需要陷阱来发现代码中的其他问题。但我需要克服这个琐碎的输入语句,在那里他们不知何故把我绊倒了。)
发布于 2019-01-08 18:50:21
如果您真的想在ieee_inexact上启用浮点陷阱,那么您可能应该使用Fortran语言提供的工具来控制异常,而不是编译器选项。试一试
program small_test
use ieee_arithmetic
implicit none
double precision :: a, b, c, d
logical flag
open(5,file='infile.dat',status='old')
if (ieee_support_flag(ieee_inexact)) then
call ieee_get_halting_mode(ieee_inexact, flag)
call ieee_set_halting_mode(ieee_inexact, .false.)
end if
read (5,*) a, b, c, d
print *, a, b, c, d
if (ieee_support_flag(ieee_inexact)) then
call ieee_set_halting_mode(ieee_inexact, flag)
end if
end programhttps://stackoverflow.com/questions/54096969
复制相似问题