Floating point exception when reading real values from an input file

六月ゝ 毕业季﹏ 提交于 2019-12-20 02:56:23

问题


I try to read a float value from an input file in Fortran.

To do so I use this code :

...
INTEGER            :: nf
REAL               :: re

OPEN(newunit=nf, file='toto.txt')

READ(unit=nf, fmt=*) re
...

with toto.txt a text file containing my real value :

10.1001 ! this value is supposed to be read by the Fortran program 

If I compile and execute like this, everything works well.

But I get some trouble when I compile and execute with fpe option. I have a error at the readding line that looks like:

Program received signal SIGFPE: Floating-point exception - erroneous arithmetic operation

Backtrace for this error
#0  0xfffffff
#1  0xfffffff
...

I use a gfortran command : gfortran -g1 -c -fbacktrace -ffpe-trap=invalid,zero,overflow,underflow,inexact,denormal -Wall -fcheck=all my_prog.f90

I assume my read action is not proper. So is that error normal? Is there a proper way to read real values?


回答1:


The floating point exceptions inexact and denormal happen way too often and during legitimate use of floating point arithmetic is inexact. Almost all real-world floating point arithmetic. Even reading a single number from file or keyboard, because not all decimal numbers can be stored exactly in binary. Denormal happens slightly less often, but the use can still be legitimate.

Therefore it is not useful to trap these floating point exceptions. Even underflow is debatable. I would not trap it by default, but I can see its usefulness.



来源:https://stackoverflow.com/questions/42098438/floating-point-exception-when-reading-real-values-from-an-input-file

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