从FORTRAN 77文件中读取REAL的结果 - 奇数结果 [英] Reading REAL's from file in FORTRAN 77 - odd results

查看:324
本文介绍了从FORTRAN 77文件中读取REAL的结果 - 奇数结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在在FORTRAN 77里乱搞,我遇到了一个我似乎无法解决的问题。我试图读取类似于下面的文件:

  000120这里的描述18 7 10.15 
000176 65 20 56.95
...

最后一栏在每一行是一个金额(从不大于100)。我试图通过使用类似于下面的代码读取文件

 整数pid,数量,最小数量
实际价格$ (2,100,IOSTAT = iend)pid描述数量min_qty价格
100格式(I11,A25,I7,I6,F5)

除了最后一列之外,一切看起来都很好。当我检查价格的价值,例如说,第二行;而不是得到56.95我得到了类似于56.8999999999。

现在,我明白,我可能已经尾随9或什么都不是因为它不完全精确,但不应该是一个几乎接近95美分?也许有什么我做错了,我不确定。希望我只是没有坚持我的程序运行这样的!任何帮助非常感谢!

解决方案

这正是您用来读取文件的代码吗?你有X格式对齐列?如(I11,A25,2X,I7,3X,I6,3X,F5)(带有数值)。如果你把对齐关闭了,56.95只读为56.9,那么浮点不精确可以很容易地给你56.89999,这非常接近56.9。您也可以将行读入字符串中,并从子字符串中读取数字 - 这只需要精确地标识字符串的位置即可。一旦子字符串只包含空格和数字,你就可以使用一个不太挑剔的IO定向读取:read(string(30:80),*)qty,min_qty,price。


I'm currently messing around in FORTRAN 77 and I've ran into a problem that I can't seem to figure out. I'm trying to read from a file that looks similar to below:

000120     Description(s) here       18     7     10.15
000176     Description(s) here       65     20    56.95
...

The last column in each row is a monetary amount (never greater than 100). I am trying to read the file by using code similar to below

          integer pid, qty, min_qty
          real price
          character*40 descrip

          open(unit=2, file='inventory.dat', status='old')
          read(2, 100, IOSTAT=iend) pid, descript, qty, min_qty, price
100       format(I11, A25, I7, I6, F5)

Everything seems to be read just fine, except for the last column. When I check the value of price, say for example, for the second line; instead of getting 56.95 I get something like 56.8999999999.

Now, I understand that I might have trailing 9's or whatnot because it's not totally precise, but shouldn't it be a little closer to 95 cents? Maybe there's something that I'm doing wrong, I'm not sure. Hopefully I'm just not stuck with my program running like this! Any help is greatly appreciated!

解决方案

Is that exactly the code you use to read the file? Do you have "X" formats to align the columns? Such as (I11, A25, 2X, I7, 3X, I6, 3X, F5) (with made up values). If you got the alignment off by one and read only "56.9" for "56.95", then floating point imprecision could easily give you 56.89999, which is very close to 56.9

You could also read the line into a string and read the numbers from sub-strings -- this would require only precisely identifying the location of the string. Once the sub-strings contained only spaces and numbers, you could use a less-finicky IO directed read: read (string (30:80), *) qty, min_qty, price.

这篇关于从FORTRAN 77文件中读取REAL的结果 - 奇数结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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