本文介绍了在Fortran90中将较低的精度编号分配给较高的精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

有关在Fortran90中分配文字的几个问题.使用gfortran 4.6.

A few questions regarding assigning literals in Fortran90. Using gfortran 4.6.

程序:

program scratch
  implicit none
  integer, parameter :: RP = selected_real_kind(15)
  real(kind=RP) :: w,x,z
  real :: y

  w=2.2_RP
  x=2.2
  y=2.2
  z=2.125

  print*, w
  print*, x
  print*, y
  print '(F25.23)', y
  print*, z
end program scratch

输出:

   2.2000000000000002
   2.2000000476837158
   2.20000005
   2.20000004768371582031250
   2.1250000000000000

对于每个打印输出,我想了解编译器/处理器在做什么.例如. y用小数点后的7位数字显示,但是如果我们格式化输出,这些其他数字又是哪里来的呢?它们似乎与将默认值real文字2.2分配给更高的精度kind=RP实数x时发生的情况相同.那么这些文字会被转换为基数2,然后再更改精度还是类似的东西?

For each of the printouts I'd like to understand what the compiler/processor is doing. E.g. y is displayed with 7 digits after the decimal point, but if we format the output, where are these other digits coming from? They seem like they're the same ones that occur when a default-real literal 2.2 is assigned to a higher precision, kind=RP real x. So are the literals being converted to base 2, then changing precision afterwards, or something like that?

推荐答案

让我们看一下y的赋值.它是一个单精度变量,您可以为其分配一个单精度值.十进制值将转换为所使用的浮点表示形式,在大多数平台上,其为IEEE单精度,即二进制浮点类型.它具有23位的分数,8位的指数和一个符号位.由于2.2在二进制浮点中不能精确表示,因此您可以得到最接近的(希望是)可表示的值.

Let's look at the assignment to y. It is a single-precision variable and you assign a single-precision value to it. The decimal value is converted to the floating point representation used, which on most platforms is IEEE single precision, a binary floating point type. This has 23 bits of fraction, 8 bits of exponent and a sign bit. Because 2.2 isn't exactly representable in binary floating point, you get the closest (hopefully) representable value.

打印到更多位置时,这些其他数字"是将单精度值转换为十进制数-由于十进制数不精确,因此它倾向于具有其他非零数字.一些实现将为您提供合理数量的附加数字,一些实现可能会在一段时间后开始为您提供零,而某些实现可能仅会为您提供随机数字.

Those "other digits" when you print to more places are the single precision value converted to decimal - since it isn't exact in decimal it tends to have additional non-zero digits. Some implementations will give you a reasonable number of additional digits, some might start to give you zeroes after a while, and some might just give random digits.

2.125在二进制浮点中可以精确表示,因此可以在两个方向上精确转换.

2.125 is exactly representable in binary floating point, so it can be exactly converted in both directions.

这篇关于在Fortran90中将较低的精度编号分配给较高的精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 11:21