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

问题描述

限时删除!!



没有格式化我的输出,这就是我所做的(在一个循环内部,所以这发生了几次):

$ $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ >

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

$ $ $ $ $ $ $ $ $ $ 0.10000000000000001 $
0.20000000000000001
0.29999999999999999
0.40000000000000002
0.50000000000000000
0.59999999999999998
0.69999999999999996
0.79999999999999993
0.89999999999999991
0.99999999999999989

现在,我尝试添加一个格式语句:

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

和(与其他一切相同),而我得到只有星号在我的输出:

  ** 
**
(etc ...)

我试着读到这应该如何工作,我不明白为什么会发生这种情况。我已经尝试格式更多的空间数字( F15.15 只是给了我更多的星号每行),我试着将格式声明移动到自己的标签行...我只是不能得到我想要的输出。

我在这里错过了什么?

$ b $ p $ f $ c $ f $ d $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ c>,其中w是总共使用的字符数,d是小数点后面的字符数。在这里你要告诉它,你需要一个浮点数,总共是1个字符,在小数点后面有2个字符,这显然是不正确的。例如,为了得到总共4个字符的浮点数,并且有3位小数,你可以这样写:

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



另外,我应该提及星号表示数字不能以所述格式显示。 / b>

编辑:



在下面的george中添加注释:

对于E格式,字段宽度必须至少比小数位数多7,例如E15.8,四位指数,两位数字,一位用于可能的 - 。一个额外的空间,所以数字不会一起运行,E16.8


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 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...)

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.

What am I missing here?

解决方案

Fortran format statements are defined as:

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

See 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.

EDIT:

Adding in the comment from george below:

"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将我的输出输出为星号 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:58