Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29390 Discussions

simple question relevant to format

yugn
Beginner
1,332 Views
I paste the code first:

open(5,file='read-test.txt',status='old')
read(5,'(F8.2)') rd
write(6,'(F8.2)') rd

content in read-test.txt:
.0028 .0197 270.25 40.0


here's the question:
why the read command whose format is '(F8.2)', reads all 4bit fraction , while the write command strictly output only 2bit fraction? I'm confused by the different behaviors between read and write.
0 Kudos
8 Replies
Steven_L_Intel1
Employee
1,332 Views
You should read the section of the Compaq Fortran Language Reference Manual pertaining to F format (11.2.4.1)

Briefly, on input, the only effect the .2 has is if the input value has no decimal point - then it assumes the rightmost two digits are the fraction.

It is often the case that formats do different things on input and output. This is explained in the LRM.

Steve
0 Kudos
yugn
Beginner
1,332 Views
thank you, steve
It's always a good time to post question here!

What LRM stands for?
0 Kudos
Intel_C_Intel
Employee
1,332 Views
Steve pointed to chapter 11.2.4.1 (F editing) in Language Reference Manual (LRM). It's I/O formatting (pages 11-16 in the printed version or 291 in electronic version (pdf)). Examples from there (separated by semicolon)
Format; Input; Value
F8.5; 123456789; 123.45678
F8.5; -1234.567; -1234.56
F8.5; 24.77E+2; 2477.0
F5.2; 1234567.89; 123.45

Format; Value; Output
F8.5; 2.3547188; ^2.35472
F9.3; 8789.7361; ^8789.736
F2.1; 51.44; **
F10.4; -23.24352; ^^-23.2435
F5.2; 325.013; ******
F5.2; -.2; -0.20

A.
0 Kudos
Steven_L_Intel1
Employee
1,332 Views
Hmm - the examples in the manual don't seem to have taken rounding into account. I'll look into that.
0 Kudos
Intel_C_Intel
Employee
1,332 Views
Steve,
I checked it, it's OK (I mean LRM) for double precision - 1234.45678 (no rounding) while for single it's rounded to 123.4568.

A.
0 Kudos
yugn
Beginner
1,332 Views
thanks, arturguzik, I finally found the topic in Online Document(with this keyword F8.5). I didnt find LRM's pdf file.

I think, it is not necessary for read to take rounding into account, but it is necessary for write.
0 Kudos
Intel_C_Intel
Employee
1,332 Views
All docs in html and pdf format you can download from here.

A.
0 Kudos
Steven_L_Intel1
Employee
1,332 Views
Read rounds the value to the closest representable value - the .n part of the format doesn't matter for this.

Steve
0 Kudos
Reply