为什么不引发浮点异常 (FPE)?
Why Floating Point Exception (FPE) is not raised?
我试图在 Fortran 代码中捕获一些 FPE,但它似乎并不适用于所有环境。
我写了下面的代码作为例子:
program test
implicit none
doubleprecision, dimension(1) :: x ! used to lure the compiler
x(1) = 1d0
print *, 'start'
print *, 1d0 / (1d0 - x(1))
print *, 'end'
end program test
我的一台机器 (1) 使用 CentOS 7.6.1810、gfortran 4.8.5,并且有一个 Intel Xenon 作为 CPU。另一个 (2) 使用 Debian 10 Buster(实际上这个在 Windows 10 上,但我使用 WSL 的 debian 应用程序),gfortran 9.2.0 并且有一个 Intel Core i5 作为 CPU .
当我使用
编译上面的代码时
gfortran -o test -ffpe-trap=zero -fbacktrace -g -Og test.f90
在机器 1 上执行代码,我得到了我期望的结果,即
$ ./test
start
Program received signal SIGFPE: Floating-point exception - erroneous arithmetic operation.
Backtrace for this error:
#0 0x7F2374BEC697
#1 0x7F2374BECCDE
#2 0x7F23740E827F
#3 0x400858 in MAIN__ at test.f90:9
然而,当我在机器 2 上做同样的事情时,我只得到:
$ ./test
start
为什么?我应该怎么做才能在机器 2 上获得 SIGFPE(和其他信号)错误消息?
安装 WSL 2 解决了问题。
$ ./test
start
Program received signal SIGFPE: Floating-point exception - erroneous arithmetic operation.
Backtrace for this error:
#0 0x7feab2aee8b0 in ???
#1 0x7feab2aedae3 in ???
#2 0x7feab276883f in ???
at /build/glibc-vjB4T1/glibc-2.28/signal/../sysdeps/unix/sysv/linux/x86_64/sigaction.c:0
#3 0x4011fb in test
at /home/user/test/test.f90:9
#4 0x401299 in main
at /home/user/test/test.f90:11
Floating point exception
我试图在 Fortran 代码中捕获一些 FPE,但它似乎并不适用于所有环境。
我写了下面的代码作为例子:
program test
implicit none
doubleprecision, dimension(1) :: x ! used to lure the compiler
x(1) = 1d0
print *, 'start'
print *, 1d0 / (1d0 - x(1))
print *, 'end'
end program test
我的一台机器 (1) 使用 CentOS 7.6.1810、gfortran 4.8.5,并且有一个 Intel Xenon 作为 CPU。另一个 (2) 使用 Debian 10 Buster(实际上这个在 Windows 10 上,但我使用 WSL 的 debian 应用程序),gfortran 9.2.0 并且有一个 Intel Core i5 作为 CPU .
当我使用
编译上面的代码时gfortran -o test -ffpe-trap=zero -fbacktrace -g -Og test.f90
在机器 1 上执行代码,我得到了我期望的结果,即
$ ./test
start
Program received signal SIGFPE: Floating-point exception - erroneous arithmetic operation.
Backtrace for this error:
#0 0x7F2374BEC697
#1 0x7F2374BECCDE
#2 0x7F23740E827F
#3 0x400858 in MAIN__ at test.f90:9
然而,当我在机器 2 上做同样的事情时,我只得到:
$ ./test
start
为什么?我应该怎么做才能在机器 2 上获得 SIGFPE(和其他信号)错误消息?
安装 WSL 2 解决了问题。
$ ./test
start
Program received signal SIGFPE: Floating-point exception - erroneous arithmetic operation.
Backtrace for this error:
#0 0x7feab2aee8b0 in ???
#1 0x7feab2aedae3 in ???
#2 0x7feab276883f in ???
at /build/glibc-vjB4T1/glibc-2.28/signal/../sysdeps/unix/sysv/linux/x86_64/sigaction.c:0
#3 0x4011fb in test
at /home/user/test/test.f90:9
#4 0x401299 in main
at /home/user/test/test.f90:11
Floating point exception