Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fortran 77 Real to Int rounding Direction?

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?

like image 761
Jzl5325 Avatar asked Dec 05 '13 21:12

Jzl5325


People also ask

How do I round a number in Fortran?

NINT(A) rounds its argument to the nearest whole number.

How do you declare a REAL number and integer in Fortran?

Fortran can use also scientific notation to represent real numbers. The sequence "En" attached to the end of a number, where n is an integer, means that the number is to be multiplied by 10n. Here are various ways of writing the number 12.345: 1.2345E1 , .

What is REAL in Fortran?

The REAL statement specifies the type of a symbolic constant, variable, array, function, or dummy function to be real, and optionally specifies array dimensions and size, and initializes with values.

What is the default variable type for integers and real numbers in Fortran?

With -i2 , the default length of LOGICAL quantities is 2 bytes. Ordinary integers follow the FORTRAN rules about occupying the same space as a REAL variable. They are assumed to be equivalent to the C type long int , and 2-byte integers are of C type short int .


1 Answers

Here is one example of code and the output, it's an extension of previous answer:

PROGRAM test
implicit none
integer               :: i=0
real                  :: dummy = 0.

do i = 0,30
       dummy = -1.0 + (i*0.1)
       write(*,*) i, dummy , int(dummy) , nint(dummy) ,floor(dummy)
enddo
stop
end PROGRAM test

This is the output:

 $ ./test
      0  -1.000000              -1          -1          -1
      1 -0.9000000               0          -1          -1
      2 -0.8000000               0          -1          -1
      3 -0.7000000               0          -1          -1
      4 -0.6000000               0          -1          -1
      5 -0.5000000               0          -1          -1
      6 -0.4000000               0           0          -1
      7 -0.3000000               0           0          -1
      8 -0.2000000               0           0          -1
      9 -9.9999964E-02           0           0          -1
     10  0.0000000E+00           0           0           0
     11  0.1000000               0           0           0
     12  0.2000000               0           0           0
     13  0.3000001               0           0           0
     14  0.4000000               0           0           0
     15  0.5000000               0           1           0
     16  0.6000000               0           1           0
     17  0.7000000               0           1           0
     18  0.8000001               0           1           0
     19  0.9000000               0           1           0
     20   1.000000               1           1           1
     21   1.100000               1           1           1
     22   1.200000               1           1           1
     23   1.300000               1           1           1
     24   1.400000               1           1           1
     25   1.500000               1           2           1
     26   1.600000               1           2           1
     27   1.700000               1           2           1
     28   1.800000               1           2           1
     29   1.900000               1           2           1
     30   2.000000               2           2           2          

I hope that this can better clarify the question

EDIT: Compiled with ifort 2013 on xeon

like image 85
pm2r Avatar answered Oct 25 '22 01:10

pm2r