Integer*8 does not work ????

Integer*8 does not work ????

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
print '(1x,a,i20)',"ab=",ab
print *,'a1=',a1
print *,'b1=',b1

4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Your program is incorrect. When you have:


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:


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


Retired 12/31/2016

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.

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.


Retired 12/31/2016

Leave a Comment

Please sign in to add a comment. Not a member? Join today