Read a floating point data with F descriptor

为君一笑 提交于 2020-01-06 02:47:07

问题


I have a data file in the following format:

   76     1               28.19345173               20.57121917               21.66248682
   77     1               30.48106325               37.84001977               19.95790222
   78     1               30.49966847                        38               20.68928893
   79     1               1.246861056              0.6998957711               34.74201206

I need to do some manipulation on this data file. Thus, I started writing a code to read the 4 last columns of the data file. Here is the format I used to read the wanted information and write it in a new file:

Reading Format :

  format(21x,f16.11,10x,f16.11,10x,f16.11)

Writing Format:

format(A1,1x,f16.11,5x,f16.11,5x,f16.11)

Where A1 is automatically set to 1. The output of my code is :

1   28.19345173000       20.57121917000       21.66248682000
1   30.48106325000       37.84001977000       19.95790222000
1   30.49966847000        0.00000000038       20.68928893000
1    1.24686105600        0.69989577110       34.74201206000

As you can easily see my problem is with that 38 in the third row which is read and written as 0.00000000038. Can anyone kindly help me to figure what the best way is to solve the problem?


回答1:


In my opinion there is no place for using nonzero in the F edit descriptor for input. For output it is useful to control the number of decimal places, but it is harmful for input.

As francescalus shows in his answer, the d in (Fw.d) value causes literals without a decimal symbols to be interpretted as scaled by 10^d.

The solution is to just use F16.0 and not F16.11. Nonzero d part is completely unnecessary and harmful. There are just some strange use cases for it, but you don't have them here.




回答2:


The unexpected behaviour comes from the fact that the input field 38 has no decimal symbol. You can see this is different from all of the others.

Why is this an issue? Let's look at how the F edit descriptor, with Fw.d, is described for input (Fortran 2008, 10.7.2.3.2):

If the decimal symbol is omitted, the rightmost d digits of the string, with leading zeros assumed if necessary, are interpreted as the fractional part of the value represented.

For your edit F16.11 the fractional part consists of 11 digits. That leads to 3.8e-10.

A simple way to handle this is to ensure that all your floating point fields contain a decimal symbol.


As Vladimir F points out the simplest way is actually to modify the edit to use F16.0. This follows from the sentence before the one I quoted:

The d has no effect on input if the input field contains a decimal symbol.

So, using 0 as the fractional width in input has the desired result: when there is a decimal symbol the value 0 here is ignore; when there isn't, that's the same as having on fraction part. For output you can still use the format as you have it. It's just the input going wrong.



来源:https://stackoverflow.com/questions/31982406/read-a-floating-point-data-with-f-descriptor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!