Fortran 将我的输出添加到星号 - 为什么? [英] Fortran splats my output to asterisks - why?

查看:34
本文介绍了Fortran 将我的输出添加到星号 - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解 Fortran 中的格式化语句.

I'm having a hard time wrapping my head around formatting statements in Fortran.

不格式化我的输出,这就是我所做的(在循环中,所以这会发生几次):

Without formatting my output, this is what I do (inside a loop, so this happens a few times):

write(*,*) t*1E9

t 这里是 real*8.输出正是我所期望的——增量为 0.1,有一些舍入误差:

t here is real*8. The output is just what I'd expect - increments of 0.1, with some rounding errors:

0.0000000000000000     
0.10000000000000001     
0.20000000000000001     
0.29999999999999999     
0.40000000000000002     
0.50000000000000000     
0.59999999999999998     
0.69999999999999996     
0.79999999999999993     
0.89999999999999991     
0.99999999999999989

现在,我尝试添加格式声明:

Now, I try to add a format statement:

write(*, '(F1.2)') t*1E9

并且(其他一切都相同)相反,我的输出中只有星号:

and (with everything else the same) instead I get only asterisks in my output:

**
**
(etc...)

我试图了解这应该如何工作,但我无法弄清楚为什么会发生这种情况.我尝试了具有更多数字空间的格式(F15.15 只是给我每行更多的星号),我尝试将格式语句移动到它自己的标记行...我只是可以'似乎没有得到我想要的输出.

I've tried to read up on how this should work, and I can't figure out why this is happening. I've tried formats with more space for digits (F15.15 just gives me more asterisks per line), I've tried moving the format statement to its own, labelled line... I just can't seem to get the output I'd like.

我在这里错过了什么?

推荐答案

Fortran格式语句定义为:

Fortran format statements are defined as:

Fw.d,其中w是总共要使用的字符数,d是小数点后的字符数.在这里,您告诉它您需要一个浮点数,总共 1 个字符宽,小数点后 2 个字符,这显然是不正确的.因此,例如,要获得一个总共 4 个字符、小数点后 3 位的浮点数,您可以这样写:

Fw.d, where w is the number of characters to be used in total, and d is the number of characters after the decimal point. Here you are telling it that you need a float, that is 1 character wide in total, and 2 characters after the decimal point, something that is obviously not correct. So to get, for example, a float that is 4 characters in total, with 3 decimal places, you'd write:

write(*, '(F4.3)') t*1E9

参见 http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap05/format.html

另外,我应该提一下,星号表示该数字不能以所述格式显示.

Also, I should mention that the asterisks are indicative that the number cannot be displayed in the format stated.

在下面添加乔治的评论:

Adding in the comment from george below:

"对于 E 格式,字段宽度必须至少比小数位数多 7,例如 E15.8.四个代表指数,两个代表前导 0.一个代表可能的 '-'.我通常加一个更多的额外空间,所以数字不会一起运行,E16.8"

"For E format the fieldwidth has to be at least 7 more than the number of decimals, eg E15.8. Four for the exponent, two for the lead 0. one for a possible '-'. I usually add one more extra space so numbers don't run together, E16.8"

这篇关于Fortran 将我的输出添加到星号 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆