Fortran: can you explain this formatting string
Asked Answered
S

2

2

I have a Fortran program which I need to modify, so I'm reading it and trying to understand. Can you please explain what the formatting string in the following statement means:

write(*,'(1p,(5x,3(1x,g20.10)))') x(jr,1:ncols)
Swap answered 12/3, 2014 at 11:29 Comment(0)
P
5

http://www.fortran.com/F77_std/rjcnf0001-sh-13.html

breifly, you are writing three general (g) format floats per line. Each float has a total field width of 20 characters and 10 places to the right of the decimal. Large magnitude numbers are in exponential form.

The 1xs are simply added spaces (which could as well have been accomplished by increasing the field width ie, g21.10 since the numbers are right justified. The 5x puts an additional 5 spaces at the beginning of each line.

The somewhat tricky thing here is tha lead 1p which is a scale factor. It causes the mantissa of all exponential form numbers produced by the following g format to be multiplied by 10, and the exponent changed accordingly, ie instead of the default,

    g17.10  ->  b0.1234567890E+12

you get:

  1p,g17.10  -> b1.2345678900E+11

b denotes a blank in the output. Be sure to allow room for a - in your field width count...

for completeness in the case of scale greater than one the number of decimal places is reduced (preserving the total precision) ie,

  3p,g17.10  -> b123.45678900E+09  ! note only 8 digits after the decimal

that is 1p buys you a digit of precision over the default, but you don't get any more. Negative scales cost you precision, preserving the 10 digits:

    -7p,g17.10  ->  b0.0000000123E+19

I should add, the p scale factor edit descriptor does something completely different on input. Read the docs...

Pikestaff answered 12/3, 2014 at 12:49 Comment(1)
Thank you so much for this extensive answer!Swap
N
2

I'd like to add slightly to George's answer. Unfortunately this is a very nasty (IMO) part of Fortran. In general, bear in mind that a Fortran format specification is automatically repeated as long as there are values remaining in the input/output list, so it isn't necessary to provide formats for every value to be processed.

Scale factors

In the output, all floating point values following kP are multiplied by 10k. Fields containing exponents (E) have their exponent reduced by k, unless the exponent format is fixed by using EN (engineering) or ES (scientific) descriptors. Scaling does not apply to G editing, unless the value is such that E editing is applied. Thus, there is a difference between (1P,G20.10) and (1P,F20.10).

Grouping

A format like n() repeats the descriptors within parentheses n times before proceeding.

Norbertonorbie answered 12/3, 2014 at 13:15 Comment(3)
Also, I echo George's recommendation of reading documentation on this. IBM has pretty extensive information on this topic.Norbertonorbie
Now that EN and ES are available, I no longer use G. IMO, prior to that, it was useful to modify E. G can be really dangerous; as mentioned in the other answer, its behavior is different between output and input!Philana
@M.S.B. I think you could wallpaper a house with a printout of all the format descriptors, not to mention all the parameters of open, read and write... I love Fortran, but not this part :PNorbertonorbie

© 2022 - 2024 — McMap. All rights reserved.