Fortran 77 Real to Int rounding Direction?

fortran, rounding

Solution

`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`).

Problem

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?

Original source