从文件读取的值在 Fortran 中存储为不同的值
我有一个输入文件,第一行包含以下小数。
0.5053102074297753
我有一个 Fortran 90 程序,它读取文件并输出值。
read(*,*) answer
write(*,"(F20.16)") answer
这是输出:
0.5053101778030396
显然,存储的内容与读取的内容不同。问题是,为什么?
I have an input file and the first line contains the following decimal.
0.5053102074297753
I have a Fortran 90 program which reads the file and outputs the value.
read(*,*) answer
write(*,"(F20.16)") answer
This is the output:
0.5053101778030396
Apparently, what is stored is not the same as what is read. The question is, Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答案如何声明?如果它是单精度实数,则只能期望大约 6 个小数位的精度。
此外,值会转换为二进制以进行内部存储和计算。这可能会导致舍入和其他问题,但这里的差异太大,不足以成为原因。
要将答案声明为双精度,请使用以下命令:
这将保证答案至少有 14 位小数位。 “DRK”可以在您的整个程序中使用。根据您的编译器,您可以尝试要求更多的数字......它可能会提供这样的类型。很少需要超过双精度的值。
How is answer declared? If it is a single precision real you can only expect about 6 decimal digits of precision.
Also, values are converted to binary for internal storage and computations. This can cause rounding and other issues, but the difference here is too large for this to be the cause.
To declare answer as double precision, use the following:
This will guarantee that answer has at least 14 decimal digits. "DRK" can be used throughout your program. Depending on your compiler, you can try asking for even more digits ... it may provide a such a type. Rarely is more than double precision necessary.
每个计算机科学家都应该了解浮点运算。
默认实数精度不足以存储小数部分有 16 位小数的数字。
What Every Computer Scientist Should Know About Floating-Point Arithmetic.
The default real precision is not enough to store the number with 16 decimal places in the fractional part.