Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reading integers as reals (double precision)?

51 views
Skip to first unread message

john.chl...@gmail.com

unread,
Oct 12, 2012, 2:17:10 AM10/12/12
to
I have a data file that includes many real values without a decimal point, E.g.: 705201

I'm trying to read the file lines using (there's only one value per file line):

read(unit=funit, fmt="(a)", iostat=stat) line
...
read(line, "(f10.0)") Eng_GimRatio(6)
...

Of course f10.0 doesn't work. Is there some format that will?

I could read the values as integers and then convert them to double precision? Would that be best?

---John

Dieter Britz

unread,
Oct 12, 2012, 3:40:38 AM10/12/12
to
What about reading with free format, e.g.
read(unit=funit, *, iostat=stat) var...

You can read them straight into double precision variables, they
will be converted.

--
Dieter Britz

john.chl...@gmail.com

unread,
Oct 12, 2012, 4:01:36 AM10/12/12
to
Works for me! Thanks!

dpb

unread,
Oct 12, 2012, 9:55:41 AM10/12/12
to
On 10/12/2012 1:17 AM, john.chl...@gmail.com wrote:
> I have a data file that includes many real values without a decimal point, E.g.: 705201
>
> I'm trying to read the file lines using (there's only one value per file line):
>
> read(unit=funit, fmt="(a)", iostat=stat) line
> ...
> read(line, "(f10.0)") Eng_GimRatio(6)
> ...
>
> Of course f10.0 doesn't work. Is there some format that will?
...

What do you mean 'F10.0' doesn't work? Of course it will/does...

C:\Temp> copy jski.f90 clip:
program test
implicit none
real :: x
character(len=80) :: line='705201'

read(line,'(F10.0)') x
write(*,*) x
end program test

C:\Temp> df /nologo jski.f90
jski.f90

C:\Temp> jski
705201.0

C:\Temp>

There's no need to read into the character variable first, of course, I
simply did it as it
a) mimics your sample posted and
b) primarily because saved making another file and the open(... etc.

The F (or and E) format will only place an inferred decimal point in the
value on input conversion of there are leading spaces so the field width
doesn't coincide--perhaps that's the problem you're seeing?

As per usual, the difficulty in answering is often in the detail not
provided...the same program again w/ a modification--

C:\Temp> copy jski.f90 clip:
C:\Temp\JSKI.F90 => clip:
program test
implicit none
real :: x
! 00000000011
! 12345678901
character(len=80) :: line=' 705201'

read(line,'(F10.0)') x
write(*,*) x
end program test

C:\Temp> df /nologo jski.f90
jski.f90

C:\Temp> jski
70520.00

Now the last character of the character variable is truncated by the
field width...

As another said, you can use a list-directed read if the data are
formatted to allow it (and one value per record certainly will work) or
you can either make the field width sufficiently wide to cover the case
(altho this makes it problematical if multiple entries per record, of
course).

So, overall, the easier way on both programming and on matching input to
the program is list-directed where it can be used as alignment in fixed
field widths is indeed critical.

--

Richard Maine

unread,
Oct 12, 2012, 11:23:16 AM10/12/12
to
<john.chl...@gmail.com> wrote:

> I have a data file that includes many real values without a decimal point,
> E.g.: 705201
>
> I'm trying to read the file lines using (there's only one value per file
> line):
>
> read(unit=funit, fmt="(a)", iostat=stat) line
> ...
> read(line, "(f10.0)") Eng_GimRatio(6)
> ...
>
> Of course f10.0 doesn't work. Is there some format that will?

Others seem to have adequately already answered the question. Let me
just note that "doesn't work" is almost always an inadequate
description. In fact, as dpb pointed out, the above "of course" to the
contrary, it does work. Or anyway, it should work and it does for me.

I confess as to some curiosity as to what it is that "doesn't work" for
you. My crystal ball isn't working for me. I made a few wild guesses,
but when I tested them, they all worked too, so they are probably out.

Makes me almost wonder whether you didn't actually try it because you
assumed that, of course, it wouldn't work.

> I could read the values as integers and then convert them to double
> precision? Would that be best?

That will work, but then so will the original, or list-directed as
Dieter suggested.

I'd probably lean sligtly towards the list-directed option, but I'm
still curious about what the alleged problem was.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain

glen herrmannsfeldt

unread,
Oct 12, 2012, 12:50:11 PM10/12/12
to
Richard Maine <nos...@see.signature> wrote:
> <john.chl...@gmail.com> wrote:

>> I have a data file that includes many real values without a decimal point,

(snip)
>> read(unit=funit, fmt="(a)", iostat=stat) line
>> read(line, "(f10.0)") Eng_GimRatio(6)

Why read into line, and then read from there?

There are some reasons to do that, but none that I can
see from what was (before snip) given.

>> Of course f10.0 doesn't work. Is there some format that will?

> Others seem to have adequately already answered the question. Let me
> just note that "doesn't work" is almost always an inadequate
> description. In fact, as dpb pointed out, the above "of course" to the
> contrary, it does work. Or anyway, it should work and it does for me.

I it is likely to fail for Fortran 66 and, I believe for Fortran 77.
I am not sure when BLANK='NULL' was added to OPEN. Before that,
or now with BLANK='ZERO' the value must be right justified in
ten columns. (And note that it will still fail for numbers longer
than 10 digits.

It can also fail if line is too small to hold the whole thing.
(Say, CHARACTER*1 for example.)

Since internal READ doesn't have an OPEN, there isn't any place
to put BLANK=.

> I confess as to some curiosity as to what it is that "doesn't work" for
> you. My crystal ball isn't working for me. I made a few wild guesses,
> but when I tested them, they all worked too, so they are probably out.

> Makes me almost wonder whether you didn't actually try it because you
> assumed that, of course, it wouldn't work.

>> I could read the values as integers and then convert them to double
>> precision? Would that be best?

The idea behind the .0 part of the format descriptor is to
specify the position of the decimal point if it isn't supplied
in the input data.

> That will work, but then so will the original, or list-directed as
> Dieter suggested.

List directed is a good choice. Explicit format is most useful when
the data has fixed columns. In the days of punched cards, it was
somewhat usual to punch the fields with no extra space in between,
and sometimes with no decimal point. You could read a card like

123453927851203

with 3F5.2 into a three element REAL array, and get
(values approximating) 123.45, 392.78, and 512.03. Sometimes cards
would be preprinted with lines separating the columns making it easier
to read (by people).

This made some sense on cards, but much less for terminal input.

> I'd probably lean sligtly towards the list-directed option, but I'm
> still curious about what the alleged problem was.

Also, in many cases you can do the list directed read directly,
without the intermediate line variable.

-- glen
Message has been deleted

Robin Vowels

unread,
Oct 13, 2012, 2:59:38 AM10/13/12
to
On Oct 12, 5:17 pm, john.chludzin...@gmail.com wrote:

> I have a data file that includes many real values without a decimal point, E.g.: 705201

They are not "real" values; they are integers.

> I'm trying to read the file lines using (there's only one value per file line):

> read(unit=funit, fmt="(a)", iostat=stat) line
> ...
> read(line, "(f10.0)") Eng_GimRatio(6)
> ...

> Of course f10.0 doesn't work. Is there some format that will?

F10.0 does "work". It will allow a field of 10 digits to be read;
a decimal point will be assumed after the final digit.

As others have pointed out, this input task is best solved by one
READ statement, and without an explicit format.

john.chl...@gmail.com

unread,
Oct 13, 2012, 6:38:45 AM10/13/12
to
Mea culpa ... it is a large file and I was looking to the line above were I thought it was getting the "wrong" value from.

Yes indeed, "F10.0" works just fine.

---John
0 new messages