Catch integer exceptions in Fortran

徘徊边缘 提交于 2019-12-01 22:03:28

There is nothing in the Fortran standard that deals with integer overflow. As it stands you can't even rely on integers wrapping round when a computation exceeds the maximum value representable in the chosen kind. So, while a test such as

huge(2_8)+1 < 0_8

is likely to work with most current compilers (at least the ones I have used recently) it's not guaranteed.

I am sure that neither Intel Fortran nor gfortran provide compiler-generated run-time checks for integer overflow either. I'm not sure about other compilers but I'll be (pleasantly) surprised to learn that any of them do.

I think, therefore, that you have to proceed with your current approach.

gfortran will catch integer overflow with -ftrapv flag, see man gcc:

-ftrapv This option generates traps for signed overflow on addition, subtraction, multiplication operations.

ifort does not seem to have that capability.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!