- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
consider:
integer*4 a,b
integer*8 ab
print *,a*b
ab=a*b
print *,ab
In both cases you get a negative answer, if a*b is above 2**31, i.e. a=50000, b=50000.
The compiler apparently does not realize that the product of two 32 bit integers can be as long
as 62 bits (I am assuming they are positive or negative 31 bit numbers).
So it does not store an intermediate result of more than 32 bits.
I am wondering if there is a way to multiply these numbers together without making them integer*8
types first.
Notice that when the END RESULT is typed properly, the compiler still does not do the mutilply assuming a 62 bit result.
It probably generates an integer overflow, which isn't trapped currently.
integer*4 a,b
integer*8 ab
print *,a*b
ab=a*b
print *,ab
In both cases you get a negative answer, if a*b is above 2**31, i.e. a=50000, b=50000.
The compiler apparently does not realize that the product of two 32 bit integers can be as long
as 62 bits (I am assuming they are positive or negative 31 bit numbers).
So it does not store an intermediate result of more than 32 bits.
I am wondering if there is a way to multiply these numbers together without making them integer*8
types first.
Notice that when the END RESULT is typed properly, the compiler still does not do the mutilply assuming a 62 bit result.
It probably generates an integer overflow, which isn't trapped currently.
Link Copied
5 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - billsincl
consider:
integer*4 a,b
integer*8 ab
print *,a*b
ab=a*b
print *,ab
In both cases you get a negative answer, if a*b is above 2**31, i.e. a=50000, b=50000.
The compiler apparently does not realize that the product of two 32 bit integers can be as long
as 62 bits (I am assuming they are positive or negative 31 bit numbers).
So it does not store an intermediate result of more than 32 bits.
I am wondering if there is a way to multiply these numbers together without making them integer*8
types first.
Notice that when the END RESULT is typed properly, the compiler still does not do the mutilply assuming a 62 bit result.
It probably generates an integer overflow, which isn't trapped currently.
integer*4 a,b
integer*8 ab
print *,a*b
ab=a*b
print *,ab
In both cases you get a negative answer, if a*b is above 2**31, i.e. a=50000, b=50000.
The compiler apparently does not realize that the product of two 32 bit integers can be as long
as 62 bits (I am assuming they are positive or negative 31 bit numbers).
So it does not store an intermediate result of more than 32 bits.
I am wondering if there is a way to multiply these numbers together without making them integer*8
types first.
Notice that when the END RESULT is typed properly, the compiler still does not do the mutilply assuming a 62 bit result.
It probably generates an integer overflow, which isn't trapped currently.
In Fortran, the result of a*b where a and b are the same type will have the same type as the arguments.
("If every operand in a numeric expression is of the same data type, the result is also of that type."). So in your example, a*b will be Integer*4. Then when it is stored into ab, it will be converted to Integer*8. To make the result of a*b be anything other than Integer*4, you will need to make a to be Integer*8 first.
David
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
To add to what David said - in Fortran, the type and kind of what is on the left of the assignment, the "end result", plays no part in determining the type and kind of the right side expression. It is a common mistake to think that the compiler should "see" that the value is being assigned to an INTEGER(8) and then somehow calculate the expression to that kind. It doesn't work that way in Fortran.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - David White
In Fortran, the result of a*b where a and b are the same type will have the same type as the arguments.
("If every operand in a numeric expression is of the same data type, the result is also of that type."). So in your example, a*b will be Integer*4. Then when it is stored into ab, it will be converted to Integer*8. To make the result of a*b be anything other than Integer*4, you will need to make a to be Integer*8 first.
David
AB=int8(a)*int8(b) or
print *,int8(a)*int8(b)
I was wondering if there's a way to force the compiler to check for integer overflows first.
The central problem is trying to evaluate A*b mod C where a,b and C are all integer(4) types.
One could decompose either A or B into BITS, then do an add and shift a bit at a time.
That of course is considerably slower than the machine multiply.
What about using a machine language routine instead?
I wish I knew more about Intel machine code.
Are there examples of how to do that anywhere?
I'm not sure that even C++ can solve this problem.
The application is the RSA encoding/decoding algorithm, which some of you may be familiar with - -
Yours; Bill
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
No, there's no way to have the compiler check for overflow and then do the computation another way.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ab = INT8(a) * INT8(b)
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