Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

int() function in Fortran 77

João_A_
Beginner
1,859 Views

I have come across a problem when I use the int() function. I have an real array mEL and I want to convert some of its components to integers for comparison with real array COORDS. I have to use int() due to rounding errors in COORDS array, so ZERO is not quite zero is 3.D-77 or similar:

      DO I = 1,nLines
          IF (int(mEL(I,1)).EQ.int(COORDS(1,1)) .AND.
 1              int(mEL(I,2)).EQ.int(COORDS(2,1)) .AND. 
 2              int(mEL(I,3)).EQ.int(COORDS(3,1))) THEN

          .....do something

The problem is that during the run the outcome of int(COORDS(1,1)) changes but the value of COORDS(1,1) is the same. For example:

COORDS(1,1) = 1829.0000000

During 1000 runs I get int(COORDS(1,1))=1829. However, after some runs I get: int(COORDS(1,1))=1828! but COORDS(1,1)=1829!

Any ideas why this happens?

Even stranger is when I have

COORDS(1,1) = 1829.0000000 and COORDS(1,2) = 1829.0000000 and
int(COORDS(1,1)) = 1828 and int(COORDS(1,2)) = 1829!!!
0 Kudos
1 Solution
andrew_4619
Honored Contributor III
1,859 Views

You are possibly better using NINT (nearest integer) rather than INT (truncation) in your example. 

View solution in original post

0 Kudos
6 Replies
Steven_L_Intel1
Employee
1,859 Views

It probably isn't exactly 1829 but maybe a very small amount under that rounds to 1829 when you display it with fewer digits.

0 Kudos
andrew_4619
Honored Contributor III
1,860 Views

You are possibly better using NINT (nearest integer) rather than INT (truncation) in your example. 

0 Kudos
João_A_
Beginner
1,859 Views

Yes, I should have thought about it. Thanks for your help. I'll see if I can solve my problem with this in mind.

0 Kudos
João_A_
Beginner
1,859 Views

@app4619: Many thanks for your suggestion!

0 Kudos
TimP
Honored Contributor III
1,859 Views

ieee_rint in default rounding mode is just slightly different but should be more optimizable than NINT.

0 Kudos
Steven_L_Intel1
Employee
1,859 Views

I think Tim means IEEE_RINT - there is no IEEE_NINT. While theoretically it would be more optimizable, in our implementation IEEE_RINT is always a function call so it can't participate in optimization.

0 Kudos
Reply