Reputation: 13
I am trying to use the format statement in FORTRAN 77 in order to define to precision with which I want to read a number.
Here are the lines of my code:
double precision x,y,z
open(u,FILE='points.dat',STATUS='OLD')
do 10 i=1,n
read(u,100) x,y,z
write(*,*),'check', x,y,z
enddo
100 format(3(2x,D14.2))
close(u)
I tried to read these numbers: 1.0542095e-03 -3.4176819e-02 -1.3796906e-01 with different format and the results are:
format(3(2x,E14.2)): check 0.0010542095 -0.034176819 -0.13796906
format(3(2x,E14.7)): check 0.0010542095 -0.034176819 -0.13796906
format(3(2x,D14.7)): check 0.0010542095 -0.034176819 -0.13796906
I would have expected the first line to give 0.001000000 and the second line to give 0.001054209 but it seems that format statement has no effect in the reading or maybe I did not use it properly. Anyone could explain me the effect of the format in the read function and how to use it properly?
Upvotes: 1
Views: 423
Reputation: 60008
Don't use the .2
part of the edit descriptor when reading. Never. It does very bad and confusing things. Always put there .0
.
What it actually does is that it does nothing at all when the number being read contains a decimal point. So far so it is good. But if the number does not contain a decimal point it will add one according to your number.
So if you read
'1'
using
read '(E10.2)', x
x
will become 0.01
.
If you read '1.0'
using the same, it becomes 1.0
.
It is so confusing that I highly suggest to use .0
in the real edit descriptors under all circumstances.
For rounding your numbers you could, for example, do
read '(E10.0)', x
x = aint(x*100)/100
Upvotes: 5