Fortran splats my output to asterisks - why?

2020-04-02 08:40发布

问题:

I'm having a hard time wrapping my head around formatting statements in Fortran.

Without formatting my output, this is what I do (inside a loop, so this happens a few times):

write(*,*) t*1E9

t here is real*8. The output is just what I'd expect - increments of 0.1, with some rounding errors:

0.0000000000000000     
0.10000000000000001     
0.20000000000000001     
0.29999999999999999     
0.40000000000000002     
0.50000000000000000     
0.59999999999999998     
0.69999999999999996     
0.79999999999999993     
0.89999999999999991     
0.99999999999999989

Now, I try to add a format statement:

write(*, '(F1.2)') t*1E9

and (with everything else the same) instead I get only asterisks in my output:

**
**
(etc...)

I've tried to read up on how this should work, and I can't figure out why this is happening. I've tried formats with more space for digits (F15.15 just gives me more asterisks per line), I've tried moving the format statement to its own, labelled line... I just can't seem to get the output I'd like.

What am I missing here?

回答1:

Fortran format statements are defined as:

Fw.d, where w is the number of characters to be used in total, and d is the number of characters after the decimal point. Here you are telling it that you need a float, that is 1 character wide in total, and 2 characters after the decimal point, something that is obviously not correct. So to get, for example, a float that is 4 characters in total, with 3 decimal places, you'd write:

write(*, '(F4.3)') t*1E9

See http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap05/format.html

Also, I should mention that the asterisks are indicative that the number cannot be displayed in the format stated.

EDIT:

Adding in the comment from george below:

"For E format the fieldwidth has to be at least 7 more than the number of decimals, eg E15.8. Four for the exponent, two for the lead 0. one for a possible '-'. I usually add one more extra space so numbers don't run together, E16.8"