简体   繁体   English

从输入文件读取实际值时出现浮点异常

[英]Floating point exception when reading real values from an input file

I try to read a float value from an input file in Fortran .我尝试从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 :使用 toto.txt 包含我的真实值的文本文件:

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.但是当我使用fpe选项编译和执行时,我遇到了一些麻烦。 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我使用gfortran命令: 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?有没有正确的方法来读取真实值?

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.我不会默认捕获它,但我可以看到它的用处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM