Fortran 到 Python 格式浮点数
我正在用Python3.2 编写一个前端GUI,最终输出的一部分基于fortran 格式。我找到了一些很好的例子,并且从 fortran 转换为 python 时没有遇到很多问题(例如 F10.3 == "{:10.3F}")
然而,我发现我的 python 试图模仿的一些示例输出存在一些差异。
其中 Fortran 代码包含 F10.0、F9.0 等元素。我在示例输出文件中看到用户提供的数据,例如 345.2300 或仅 0.0000 。 .0 不是意味着没有小数吗? 345.2300 不应该变成 345 吗?我发现这在Python中是正确的,它会自动四舍五入提供的所有小数,并且F10.0本质上是一个10位整数。 Fortran 中是否有我不明白的细微差别?提供的十进制数是否会覆盖 Fortran 中的格式代码?或者我对格式规范的理解是否正确,并且我尝试在 python 中创建的代码/输出存在一些问题。
我基本上对 Fortran 一无所知,所以我们将非常感谢您的帮助,
谢谢, -克里斯
编辑 我开始意识到也许我误解了这个 gui 的规范。我的 gui 生成一个文本文件,该文件被输入到另一个用 Fortran 编写的程序中。文本文件必须采用非常特定的格式,这是由(来自手册)fortran 格式语句规定的。在我拥有的示例文本文件中,我可以看到用户为其中一个变量键入 29.64578,根据规范,这将与 F10.0 匹配。但是,文本文件包含宽度为 10 的字符串“29.64578”。在我的 python 版本中,如果用户输入 29.64578 并且我使用“{:10.0F}”,则文本文件将显示“30”。
我应该在 python 中将 F10.0 翻译为“{:10.0G}”而不是“{:10.0F}”吗?
29.64578在fortran中与F10.0匹配不会出错吗?
假设 F10.0,Fortran 代码会对 29.64578 做什么?
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 examples, 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MSB 关于输入和输出的信息是正确的。如果您不清楚 FORTRAN 格式语句,那么以下使用
fortranformat
库(v 0.2.2,在 PyPI 上提供)的示例可能会有所帮助。请注意,该库已经针对 Linux 上的英特尔 FORTRAN 编译器 9.1 进行了广泛的测试(其他平台仅在奇怪的边缘情况下有所不同)。G
编辑描述符可能是不必要的。它根据输入和输出变量的类型和值改变其行为,但在这种情况下将模拟F
编辑描述符。nb 输入尾随数字的差异是由于浮点表示的性质造成的,29.64578 不能用有限数量的二进制位表示。
我应该证明我是这个库的作者;-)
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).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 theF
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 给出小数点,但小数点后没有数字。输入时,数字字符串中允许使用小数点后的其他数字,并覆盖格式说明符 - 重要的是宽度以及输入位于正确的列内。如果示例用于输入,它们可能会演示此功能。
.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.
是的,“Fw.0”编辑描述符意味着没有小数位。请参阅 Fortran 2008 标准 (N1830.pdf) 中的第 10.7.2.3.2 节。
要么您的 Fortran 编译器有问题,要么您误解了 Fortran 源代码和相应的输出。
请注意,一般来说,Fortran 格式设置相当复杂,并且将 Fortran 编辑描述符到 Python 的通用翻译并不是一个简单的项目。
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.