Fortran捕捉浮点数非法运算

Fortran捕捉浮点数非法运算

调试相当老的Fortran程序,有NaN输出,想在有NaN运算的地方引起异常让程序立即core dump。测试显示ifort明显比gfortran功能好很多。


代码片段
program test
   use,intrinsic::&
        &ieee_arithmetic,only:ieee_value,ieee_quiet_nan,ieee_signaling_nan
   real::x,y
   !x=ieee_value(x,ieee_signaling_nan)
   x=ieee_value(x,ieee_quiet_nan)
   y=10.0
   write(*,*) x+y
   write(*,*) x*y
   write(*,*) y/x
   
   stop
end 

!----------------------------------------------------------------------------------------------------------------------------------
!  gfortran 抓不到snan异常
!  gfortran -finit-real=snan  -Wall -fcheck=all -fbacktrace -ffpe-trap=invalid,zero,overflow,underflow,denormal
!
!
!  ifort -fpe0 可以抓到snan异常
!  ifort -Ofast -fpe0 可以抓到snan异常
!  ifort -g -traceback -Ofast -fpe0
!  ifort -g -traceback -init=snan -Ofast -fpe0
!------------------------------------------------------------------------------------------------------------------------------------

类型 ifort gfortran
bounds-checking -check bounds -fbounds-check
runtime detection of unintialized variable -auto -trapuv -check uninit -finit=nan -finit-int=xxx -finit-logical=xxx
floating point trapping -fpe 0 -ffpe-trap=invalid,zero,overflow
debugger flags -g -g
generate stack trace -traceback -fbacktrace
information -diag-remark

功能语句

error stop 1 ! foftran 2008
call backtrace ! gnu 扩展
call abort ! gpu 扩展

参考
  • setting-a-fortran-variable-to-NaN
  • intrinsics types
  • gfortran: float point exception
  • iso_fortran_env
  • gfortran-ffpe-trap-compiling-options
  • challenge-testing-inf-and-nan-with-gfortran-13-ofast

你可能感兴趣的:(Fortran)