4

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?

R. N
  • 707
  • 11
  • 31
  • 1
    We will need to the complete code and the input file. Please see [mcve] . The behaviour is not normal and the problem is likely elsewhere. – Vladimir F Героям слава Feb 07 '17 at 19:42
  • 2
    Uh and don't use `inexact,denormal`, you really don't want to use that unless you know very well what you are doing! – Vladimir F Героям слава Feb 07 '17 at 19:44
  • 3
    from the gfortran docs: "Many, if not most, floating point operations incur loss of precision due to rounding, and hence the ffpe-trap=inexact is likely to be uninteresting in practice. " – agentp Feb 07 '17 at 20:28
  • I won't use `inexact, denormal` anymore, but is it not appreciate ? I thought the more mistakes I can catch, the better it is. Anyway, you are completely right, my mistake is from some where else. When I try to create a MWE, I get the same error but in an other part of the code long after the reading. So I tried to the "print toto" method to find where the problem was then after some test the error reappear at the readding line... It is quite disturbing to have a move error. I will try to find more information and edit my question. – R. N Feb 07 '17 at 21:50
  • 1
    No, the problem is that `inexact, denormal` does nut just catch mistakes. It catches also completely fine constructs. Way too often. – Vladimir F Героям слава Feb 08 '17 at 11:34
  • @VladimirF Ok, my others mistakes was about some other problems. I think, my error here was only about `inexact, denormal`, so you can turn your comment into an answer. – R. N Feb 10 '17 at 16:04

1 Answers1

4

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.