topic Q about integer multiplication in Intel® Fortran Compiler
https://community.intel.com/t5/Intel-Fortran-Compiler/Q-about-integer-multiplication/m-p/1140629#M137089
<P>Suppose we have:</P><P><EM><STRONG>Integer(4) A,B</STRONG></EM></P><P><EM><STRONG>integer(8) C</STRONG></EM></P><P><EM><STRONG>C = A* B</STRONG></EM></P><P>YOU WOULD THINK THE COMPILER WOULD BE SMART ENOUGH TO USE THE RIGHT MULTIPLICATION,</P><P>BUT IT DOES NOT. - - IT GIVES THE WRONG ANSWER.</P><P>99.99 PERCENT OF THE TIME, THE ANSWER WOULD NOT FIT INTO A 4 BYTE RESULT-</P><P>THAT'S WHY THE INTEGER(8) TYPE IS REQUIRED.</P><P>iS THERE A WAY TO FORCE IT TO GIVE THE RIGHT RESULT ?</P><P>Probably Dr. Fortran has dealt with this before.</P>Sat, 26 Jan 2019 09:10:52 GMTWSinc2019-01-26T09:10:52ZQ about integer multiplication
https://community.intel.com/t5/Intel-Fortran-Compiler/Q-about-integer-multiplication/m-p/1140629#M137089
<P>Suppose we have:</P><P><EM><STRONG>Integer(4) A,B</STRONG></EM></P><P><EM><STRONG>integer(8) C</STRONG></EM></P><P><EM><STRONG>C = A* B</STRONG></EM></P><P>YOU WOULD THINK THE COMPILER WOULD BE SMART ENOUGH TO USE THE RIGHT MULTIPLICATION,</P><P>BUT IT DOES NOT. - - IT GIVES THE WRONG ANSWER.</P><P>99.99 PERCENT OF THE TIME, THE ANSWER WOULD NOT FIT INTO A 4 BYTE RESULT-</P><P>THAT'S WHY THE INTEGER(8) TYPE IS REQUIRED.</P><P>iS THERE A WAY TO FORCE IT TO GIVE THE RIGHT RESULT ?</P><P>Probably Dr. Fortran has dealt with this before.</P>Sat, 26 Jan 2019 09:10:52 GMThttps://community.intel.com/t5/Intel-Fortran-Compiler/Q-about-integer-multiplication/m-p/1140629#M137089WSinc2019-01-26T09:10:52ZDefine "smart". The Fortran
https://community.intel.com/t5/Intel-Fortran-Compiler/Q-about-integer-multiplication/m-p/1140630#M137090
<P>Define "smart". The Fortran 2008 Standard, all 603 pages of it, does not contain a single instance of "smart". </P><P>The kind of reasoning that you indulged in may also be extended to cases such as</P>
<PRE class="brush:fortran; class-name:dark;">integer :: I, J
real :: X
..
I = 2
J = 5
...
X = I/J
...</PRE>
<P>to argue that X should have the -- or a -- representable value closest to 0.4. However X = 0 according to the rules of Fortran.</P>
<P>Here is what the standard does say, in the context of integer expressions and integer multiplication.</P>
<BLOCKQUOTE><P>7.5.5.2.4 Evaluation of numeric intrinsic operations<BR /> 1 The execution of any numeric operation whose result is not defined by the arithmetic used by the processor is prohibited.</P></BLOCKQUOTE>
<P> </P>
<BLOCKQUOTE><P>7.1.9.3</P>
<P> 4 (second item of unnumbered list)</P>
<P>For an expression x1 op x2 where op is a numeric intrinsic binary operator with both operands of the same<BR />type and kind type parameters, or with one real and one complex with the same kind type parameters, the<BR />kind type parameter of the expression is identical to that of each operand.</P></BLOCKQUOTE>
<P>Your assertion, "99.99 percent of the time, the answer would not fit into a 4 byte result", probably does not apply to 99.99 percent of Fortran programs, which may use integer variables of just default kind (4 byte, these days), multiplying such variables and assuming that the product fits into 4 bytes. Many production compilers do not provide any facilities to check for integer overflow, so it is the programmers' responsibility to avoid integer overflow.</P>
<P>If, in a specific program that you write, you anticipate that multiplication may cause integer overflow, you have to provide code to handle that.</P>Sat, 26 Jan 2019 12:29:00 GMThttps://community.intel.com/t5/Intel-Fortran-Compiler/Q-about-integer-multiplication/m-p/1140630#M137090mecej42019-01-26T12:29:00ZThe issue is the same in C
https://community.intel.com/t5/Intel-Fortran-Compiler/Q-about-integer-multiplication/m-p/1140631#M137091
<P>The issue is the same in C and I think many other languages.</P><P>As mecej4 said, you must force the multiplication to be done in integer(8) if it is possible that the result will overflow integer(4).</P><P>C=int8(A)*B</P><P>will do the job.</P><P>Dont forget also that if the result of an integer(4) operation is between 2147483647 and 4294967295, it will interpreted as negative.</P><P> </P>Sat, 26 Jan 2019 12:42:00 GMThttps://community.intel.com/t5/Intel-Fortran-Compiler/Q-about-integer-multiplication/m-p/1140631#M137091GVautier2019-01-26T12:42:00Z