- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Steve
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
I'm not sure what the standard has to say about this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page