Fortran如何将实数转换为整数

How does Fortran convert a real number to Integer

例如,

SUBROUTINE DoSomething (Z,L)                             
  IMPLICIT DOUBLE PRECISION (A-H,O-Z)                               

  D=Z*77.1234567D0                                           
  L=D                                                      

  RETURN                                                         
 END     

为了便于讨论,让我们假设 D 等于 -1.5,在这种情况下,L 等于 -1 或 -2。 也就是说,向上舍入还是向下舍入?

提前致谢。

在使用内部函数 int 后转换为用于赋值的整数类型。效果定义为(F2008 13.7.81)

If A is of type real, there are two cases: if|A|<1, INT(A) has the value 0; if |A| ≥1, INT(A) is the integer whose magnitude is the largest integer that does not exceed the magnitude of A and whose sign is the same as the sign of A.

在这种情况下,L 将取值 -1

要么使用最接近的整数 NINT(),要么使用 INT()INT() 仅 returns 数字的有符号整数部分。 NINT() 工作方式如下:

If a is greater than zero, NINT(a) has the value INT(a+ 0.5); if a is less than or equal to zero, NINT(a) has the value INT(a- 0.5).

具体来说NINT(0.5d0) = 1