从输入文件读取实数值时的浮点异常 [英] Floating point exception when reading real values from an input file

查看:613
本文介绍了从输入文件读取实数值时的浮点异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从Fortran中的输入文件读取浮点值.

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
...

与toto.txt一起包含我的真实值的文本文件:

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.

但是使用fpe选项编译和执行时会遇到一些麻烦. 我在读取行上看到一个错误,如下所示:

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
...

我使用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?

推荐答案

浮点异常 inexact denormal 的发生频率很高,而且是在合法使用浮点算术期间是不精确的.几乎所有现实世界中的浮点算法.即使不是从文件或键盘读取单个数字,因为并非所有十进制数字都可以完全以二进制形式存储.异常现象的发生频率略有降低,但使用仍然可以合法.

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.

这篇关于从输入文件读取实数值时的浮点异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆