Fortran 77 Real to Int rounding Direction?

Viewed 17130

Porting a bit of Fortran 77 code. It appears that REAL variables are being assigned to INTEGER variables. I do not have a method to run this code and wonder what the behavior is in the following case:

REAL*4 A
A = 123.25
B = INT(A)

B = 123 or B = 124?

How about at the 0.5 mark?

REAL*4 C
C = 123.5
D = INT(C)

D = 123 or D = 123.5?

3 Answers

INT is always rounding down:

From the GCC documentation:

These functions return a INTEGER variable or array under the following rules:

(A) If A is of type INTEGER, INT(A) = A

(B) If A is of type REAL and |A| < 1, INT(A) equals 0. If |A| \geq 1, then INT(A) equals the largest integer that does not exceed the range of A and whose sign is the same as the sign of A.

(C) If A is of type COMPLEX, rule B is applied to the real part of A.

If you want to round to the nearest integer, use NINT.

So, in your case B and D are always 123 (if they are declared as integer).

Only as completion to the existing answers I want to add an example how the commercial rounds can be realized without using NINT by

      L = INT(F + 0.5)

where L is INTEGER and F is a positive REAL number. I've found this in FORTRAN 77 code samples from the last century.

Extending this to negative REAL numbers by

      L = SIGN(1.0,F)*INT(ABS(F) + 0.5)

and going back to the 80th of last century, the minimal code example looks like this

      PROGRAM ROUNDTEST
      DO 12345 I=0,30
      F = -1.0 + I * 0.1
      J = INT(F)
      K = NINT(F)
      L = SIGN(1.0,F)*INT(ABS(F) + 0.5)
      PRINT *, I, F, J, K, L
12345 CONTINUE
      END

which creates the output

$ ./ROUNDTEST
           0  -1.00000000              -1          -1          -1
           1 -0.899999976               0          -1          -1
           2 -0.800000012               0          -1          -1
           3 -0.699999988               0          -1          -1
           4 -0.600000024               0          -1          -1
           5 -0.500000000               0          -1          -1
           6 -0.399999976               0           0           0
           7 -0.300000012               0           0           0
           8 -0.199999988               0           0           0
           9  -9.99999642E-02           0           0           0
          10   0.00000000               0           0           0
          11  0.100000024               0           0           0
          12  0.200000048               0           0           0
          13  0.300000072               0           0           0
          14  0.399999976               0           0           0
          15  0.500000000               0           1           1
          16  0.600000024               0           1           1
          17  0.700000048               0           1           1
          18  0.800000072               0           1           1
          19  0.899999976               0           1           1
          20   1.00000000               1           1           1
          21   1.10000014               1           1           1
          22   1.20000005               1           1           1
          23   1.29999995               1           1           1
          24   1.40000010               1           1           1
          25   1.50000000               1           2           2
          26   1.60000014               1           2           2
          27   1.70000005               1           2           2
          28   1.79999995               1           2           2
          29   1.90000010               1           2           2
          30   2.00000000               2           2           2

ROUNDTEST is compiled and linked by gfortran version 7.4.0 by

$ gfortran.exe ROUNDTEST.FOR -o ROUNDTEST

Hope this helps you if you have to deal with old FORTRAN code.

Related