使用gfortran+;编译时,非常简单的Fortran代码会产生错误;fpe陷阱标志

使用gfortran+;编译时,非常简单的Fortran代码会产生错误;fpe陷阱标志,fortran,gfortran,Fortran,Gfortran,下面是简单的代码: 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

下面是简单的代码:

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.
这个简单的代码怎么可能产生错误呢


(实际情况是,我正在调试一个大得多的代码,我需要陷阱来发现代码中其他地方的一些问题。但我需要通过这个琐碎的输入语句,它们不知怎的把我绊倒了。)

如果你真的想在ieee_不精确上启用浮点陷阱,然后,您可能应该使用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 program

@高性能标记输入错误。修复了它。错误出现在
READ
语句中,相关标志为
-ffpe trap=precision
。也许这与将数字的文本表示转换为内部浮点表示有关。“如果您想将您的评论转换为答案,我将接受。”你同意我链接到的问题与你的问题非常接近,因此这个问题可以被视为重复问题吗?@WarrenWeckesser是的,我认为是。@bob.sacamento:precision是不精确的(不推荐使用的)别名。因此,它已从手册中删除,但它直到接受向后兼容。您链接到的在线手册是最新的开发版本,而您的手册页可能与您安装的gfortran版本相对应。如果您的gfortran足够大,手册页仍然会提到精度。