开发者

Fortran to Python format floats

I'm writing a front end gui in Python3.2, and part of the final output is based off of a fortran format. I've found a good few exam开发者_运维百科ples, and haven't had many problems translating from fortran to python (e.g. F10.3 == "{:10.3F}")

However, I'm seeing some differences in some of the example output my python is trying to mimic.

Where the fortran code contains elements such as F10.0, F9.0, .... I see user supplied data on the example output file such as 345.2300 or just 0.0000. Is it not true that .0 implies no decimals? Shouldn't 345.2300 become just 345? I find this to be true in python, it will automatically round up any decimals supplied, and F10.0 would essentially be a 10 digit Integer. Is there a slight difference in Fortran that I don't understand? Does a supplied decimal number override the format code in Fortran? Or is my understanding of the format specification correct and there is some issue in the code/output I'm trying to create in python.

I know basically nothing of fortran, so any help would be very appreciated

Thanks, -Chris

EDIT I'm starting to realize that maybe I'm misinterpreting my specifications for this gui. My gui generates a text file which is fed to another program written in Fortran. The text file must be in a very specific format, which is dictated by (from the manual) fortran format statements. In the example text files that I have, I can see a user typed 29.64578 for one of the variables, and according to the specifications, this will be matched with F10.0. However, the text file contains a string 10 in width, " 29.64578". In my python version, if a user typed 29.64578 and I used "{:10.0F}", the text file would show " 30".

Should I be translating F10.0 to "{:10.0G}" instead of "{:10.0F}" in my python?

Will 29.64578 not cause an error when matched with F10.0 in fortran?

What would the fortran code do with 29.64578, assuming F10.0?


M. S. B. is correct about the input and ouptut. If you are unclear about FORTRAN format statements then the following examples using the fortranformat library (v 0.2.2, available on PyPI) may help. Note the library has been extensively tested against Intel FORTRAN compiler 9.1 on Linux (other platforms only differ in weird edge cases).

>>> import fortranformat as ff
>>> # Input
>>> line = ff.FortranRecordReader('(F10.0)')
>>> line.read('29.64578')
  [29.645779999999998]
>>> # Output
>>> vals = [29.64578]
>>> line = ff.FortranRecordWriter('(F10.0)')
>>> line.write(vals)
  '       30.'

The G edit descriptor is probably unnecessary. It changes its behaviour depending on the type and value of input and output variables, but in this case will emulate the F edit descriptor.

n.b. The difference in the trailing digits on input is due to the nature of floating point representation, 29.64578 cannot be represented in a finite number of binary bits.

I should qualify that I am the author of this library ;-)


.0 on output gives a decimal point but no digit after the decimal point. On input, other numbers of digits after the decimal are allowed in the numeric string and override the format specifier -- what matters is the width and that the input is within the correct columns. If the examples are for input, they could be demonstrating this feature.


Yes, a "Fw.0" edit descriptor implies no fractional digits. See Section 10.7.2.3.2 in the Fortran 2008 standard (N1830.pdf).

Either your Fortran compiler is buggy, or you're misinterpreting the Fortran source code and the corresponding output.

Note that in general, Fortran formatting is quite complex, and a general purpose translation of Fortran edit descriptors to Python will not be a straightforward project.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜