Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when reading in float in Fortran

Tags:

fortran

This should be quite simple, but I can't manage to read in a floating point number in Fortran. My program test.f looks like this:

  PROGRAM TEST
  open(UNIT=1,FILE='test.inp')
  read(1,'(f3.0)')line

  STOP
  END

The input file test.inp simply contains a single float: 1.2

Now the compiling of my testfile goes fine, but when I run it I get an error:

At line 4 of file test.f (unit = 1, file = 'test.inp')

Fortran runtime error: Expected REAL for item 1 in formatted transfer, got INTEGER

(f3.0)

^

I've tried different modifications of the code and also googling for the error message, but with no result. Any help would be greatly appreciated!

Regards, Frank

like image 705
user1094052 Avatar asked May 06 '26 23:05

user1094052


1 Answers

Your variable line is implicitly defined as integer. This doesn't work with thef edit descriptor. If you want to read an integer use i edit descriptor (i3 for example). Otherwise declare line as real to math the "f" descriptor.

Note beside: the .0 is not a problem, because if Fortran gets a number with decimal point the .0 part in the descriptor is ignored. It is only used when an number without a decimal is entered and then it uses the number behind the decimal point in the desciptor to add a decimal point into the right place. For with F8.5, 123456789 is read as 123.45678. More ont this here http://software.intel.com/sites/products/documentation/hpc/compilerpro/en-us/fortran/lin/compiler_f/lref_for/source_files/pghredf.htm .

like image 87
Vladimir F Героям слава Avatar answered May 09 '26 05:05

Vladimir F Героям слава



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!