尾数在 0 到 1 之间的科学计数法的 .NET 格式说明符
我正在使用一个 Fortran 程序,该程序期望使用 Fortran 的 E
格式说明符输入浮点数,这是科学记数法,除了尾数必须在 0 和 1 之间。因此,而不是:
"3147.3" --> "3.1473E3",
它需要
"3147.3" --> "0.31473E4".
我无法修改 Fortran 程序,因为它可以与其他一些也很特殊的程序一起使用。
看来 C# E
格式字符串会给我前者。有没有简单的方法可以在 C# 中实现后者?
I am working with a Fortran program that expects floating point numbers to be input using Fortran's E
format specifier, which is scientific notation, except the mantissa must be between 0 and 1. So instead of:
"3147.3" --> "3.1473E3",
it needs
"3147.3" --> "0.31473E4".
I am unable to modify the Fortran program, as it works with a few other programs that are also particular.
It would appear that the C# E
format string would give me the former. Is there any simple way to achieve the latter in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以像这样指定自定义格式。
You could specify a custom format like so.
我认为你正在解决一个不存在的问题。确实,Fortran E 输出 说明符的默认值在小数点前有一个前导零(可以修改)。但是,当 E 说明符用于输入时,它非常宽容并且不需要前导零 - 如果数字中有小数点并且数字适合格式指定的列,它会起作用的。
下面是一个 Fortran 程序示例和一个输入文件示例。
以及三种不同情况的示例输入:
我使用 gfortran 和 Intel ifort 测试了该程序。输出为:
因此,当使用 Fortran 的 E 格式说明符执行输入时,小数点前的数字不必为零。输入值甚至不需要使用 E 符号!
编辑/ PS 我将程序翻译为 FORTRAN 77 的固定格式源布局,并用 g77 编译它——它很好地读取了三个测试数字。 E 格式长期以来一直具有灵活的输入功能——可能是从 FORTRAN IV 开始,也许更久。
I think that you are solving a non-existent problem. It is true that the default of the Fortran E output specifier has a leading zero before the decimal point (this can be modified). But when the E specifier is used for input it is very tolerant and does not require the leading zero -- if you have a decimal point in the number and the number fits within the columns specified by the format, it will work.
Here is an example Fortran program, and an example input file.
and the sample input with three different cases:
I tested the program with gfortran and Intel ifort. The output was:
So when performing input using Fortran's E format specifier, it is not necessary that the digit before the decimal point be zero. It is not even necessary that the input value use E-notation!
Edit / P.S. I translated the program to the fixed-form source layout of FORTRAN 77 and compiled it with g77 -- it read the three test numbers just fine. The E-format has been flexible for input for a long time -- probably since FORTRAN IV, perhaps longer.
浮点数或双精度数的表示在 IEEE754 / IEC 60559:1989 中定义。您应该寻找库来提取尾数和指数。然后你可以除以 then 移动到逗号并从指数中减去步数来形成你的解决方案。
The representaiton of floats or doubles are defined in IEEE754 / IEC 60559:1989. You should look to find libraries to extract mantissa and exponent. Then you could just divide by then to move to comma and subtract the number of steps from the exponent to form your solution.
您可以采用类似于 Jeff M 的解决方案,并通过扩展方法实现它:
或者对于更复杂的东西(不确定 Fortran 浮点/双精度给出多少精度):
You could take something similar to Jeff M's solution, and implement it via extension method:
Or for something a little more complicated (not sure how much precision Fortran floats/doubles give):