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

Integer*8 does not work ????

WSinc
New Contributor I
918 Views
This subroutine gives wrong results.
The variables a and b should be able to hold positive inetegers up to 2 billion or so, so the result should be good to 4e+18 or so. None of these ansawers come out right.
==========================================================
subroutine test_i8
integer*4 a/1000000000/
integer*4 b/1000000000/
integer*4 a1,b1
integer*8 ab
ab=a*b
b1=ab/a
a1=ab/b
print '(1x,a,i20)',"ab=",ab
print *,'a1=',a1
print *,'b1=',b1
end
0 Kudos
3 Replies
Steven_L_Intel1
Employee
918 Views
Your program is incorrect. When you have:
ab=a*b

the expression a*b is evaluated as INTEGER*4 and overflows. Because overflow checking is off by default, you get "wrong" results. It does not matter what type ab is - the language rules say that since a and b are each INTEGER*4, that's the type of the result.

One way to fix this is to say instead:

ab=int(a,8)*int(b,8)


This converts a and b to INTEGER*8 before doing the multiplication.

Steve
0 Kudos
WSinc
New Contributor I
918 Views
So if integer*2 multiplies are promoted to integer*4, why not be consistent and promote the integer*4 multiply to integer*8?

I'm not sure what the standard has to say about this.
0 Kudos
Steven_L_Intel1
Employee
918 Views
integer*2 multiplies are not promoted to integer*4. You might be able to come up with a test case that makes it look as if that's happening, but if so, it's accidental.

The standard is quite clear in saying that the kind of the variable on the left side of the assignment is not a factor in evaluating the expression on the right side. The standard also has clear rules about how expressions are evaluated and when any type conversion is done.

If you had done something like:

i8 = i8 * i4

then a conversion of the i4 to i8 is done before the multiply.

Steve
0 Kudos
Reply