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

Strange behaviour with INT()

geoff_tolton
Beginner
581 Views
Hi
If I have
avmax = 1.0
astep = 0.2
div = avmax/astep
n1 = int(avmax/astep)
n2 = int(div)
WRITE (*,*) 'int(avmax/astep)=', n1
WRITE (*,*) 'int(div), (where div=avmax/astep) =', n2
when I compile this with my CVF 6.6C I get the output
int(avmax/astep)= 4
int(div), (where div=avmax/astep) = 5
On every other compiler I have access to I get 5 and 5 both times. Can anyone explain this strange behaviour?
Any insight would be most welcome
Thanks
0 Kudos
3 Replies
Steven_L_Intel1
Employee
581 Views
I get 5 for both values using CVF 6.6C3 (6.6-4088).
0 Kudos
TimP
Honored Contributor III
581 Views

As you've found out, this is non-portable code. The results may change even with the same compiler, if you change the optimization switches. With default switches, I would expect CVF to be performing these evaluations in double precision, but 0.2 is a single precision constant, and you made sure of it by assigning the value to a single precision variable.

Your code says something like:

Take the binary single precision approximation to 0.2, invert it using the working precision (probably double), and give me thewhole part. Then do the same thing, but round off to single precision before taking the whole part. Evidently, the result depends on whether rounding 0.2 off to single precision rounds up or down. The compiler is not permitted to use decimal arithmetic to pre-calculate the results.

If you meant to use nint() rather than int(), that could be expected to give portable results on operations like these.

0 Kudos
geoff_tolton
Beginner
581 Views
I was, in fact, using /opt:0 since I had been debugging. If
I use /opt:4 (default) then I get 5 both times as suggested.
I have a shed-load of legacy code riddled with this sort
of statement, and I have no idea whether INT or NINT
was intended. What joy!
Thanks for your comments.
Geoff
0 Kudos
Reply