Java 中的 Fortran I/O 描述符?

发布于 2025-01-02 00:34:12 字数 446 浏览 4 评论 0原文

我目前必须通过创建一个 ascii 输出文件来与旧版 Fortran 程序进行交互,程序需要读入该文件,执行一些计算,然后我必须解析二进制输出。在大多数情况下,我可以正常工作,但遇到了一些小问题。

我遇到的问题是 Fortran 描述符。有没有一种简单的方法可以将像 E9.3 这样的描述符放入 Java 中的 DecimalFormat 对象中?

我的 DecimalFormat 仅适用于小于 1 的数字。 1如0.123,则写在指数上带符号,如1.230E-01。但是,对于大于 1 (12.345) 的数字,它们会写为 1.234E01,而不是 1.234E+01。据我所知,该程序期望“+”号作为指数的一部分。

我当前使用的 DecimalFormat 字符串是“#0.000E00”,如果需要,我当前正在进行一些字符串操作以在其中插入“+”(在 E 之后但数字之前)。必须有更好的方法来做到这一点。任何帮助将不胜感激。

I'm currently having to interface with a legacy Fortran program by creating an ascii output file that the program needs to read in, perform some calculations, and then I have to parse the binary output. For the most part, I have this working, but I'm running into a slight snag.

The problem I'm running into is with Fortran descriptors. Is there an easy way I can get a descriptor like E9.3 into a DecimalFormat object in Java?

The DecimalFormat I have works only for numbers less than 1. For numbers < 1 such as 0.123, they are written out with the sign on the exponential, as 1.230E-01. However, for numbers greater than 1 (12.345), they are written out as 1.234E01 rather than 1.234E+01. From what I'm being told, the program is expecting that '+' sign as part of the exponential.

The DecimalFormat string I'm currently using is "#0.000E00", and I'm currently pulling some string manipulation to insert the '+' in there (after the E but before the digits) if necessary. There has to be a better way to do this. Any assistance would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花间憩 2025-01-09 00:34:12

你真的确定你需要这个吗?也许它只是一个编译器扩展,但值得检查 Fortran 程序是否确实接受您的形式 1.234E01。我的两个编译器(gfortran 和 Solaris Studio)都接受 E9.3 编辑描述符。

Are you really sure you need this? Maybe it's only a compiler extension, but it's worth checking if the Fortran program actually accepts your form 1.234E01. My both compilers (gfortran and Solaris Studio) do accept them with E9.3 edit descriptor.

荒人说梦 2025-01-09 00:34:12

如果您有权访问 fortran 程序的源代码,则可以将其更改为使用 * 描述符读取输入文件,例如,如果您读取 data 的数组 data code>n 值,例如将其更改

read(iUnit,'(10E9.3)') data(10)

为:

read(iUnit, *) data(10)

或以未格式化的方式写入文件,然后读取二进制数据。

If you have access to the source of the fortran program, you could change it to read the input file using the * descriptor, e.g. if you read in an array data of n values, e.g. change this:

read(iUnit,'(10E9.3)') data(10)

to something like:

read(iUnit, *) data(10)

or to write the file unformatted and then read in the binary data.

轮廓§ 2025-01-09 00:34:12

如果您需要整个字符串为 9 个字符,则意味着小数部分还剩下 3 个字符。您可以使用 printf 样式格式来表达这一点,如下所示:

groovy:000> String.format("%.3E", 0.000123456789)         
===> 1.235E-04
groovy:000> String.format("%.3E", 1234.56789)         
===> 1.235E+03

如果您需要总共九个字符,即使是负数,并且前导“-”计入字符数,则必须我们使用 if 对负数使用不同的格式。

If you need the whole string to be 9 characters, it means there'll be three characters left for the decimal part. You can express this with printf style formatting like this:

groovy:000> String.format("%.3E", 0.000123456789)         
===> 1.235E-04
groovy:000> String.format("%.3E", 1234.56789)         
===> 1.235E+03

If you need it to be nine characters in total even for negative numbers, with the leading '-' counting against the character count, you'll have to us an if to use a different format for negative numbers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文