FIX: INTEGER*2 Multiply by Power of 2 Produces Wrong ResultsLast reviewed: September 11, 1997Article ID: Q63360 |
5.00 | 5.00
MS-DOS | OS/2
kbtool kbfixlist kbbuglist
The information in this article applies to:
SYMPTOMSAn application that multiplies an INTEGER*2 variable by an integer constant that is a power of 2 produces incorrect results. Specifying the /4I4 compiler option switch does not have any effect on the results. Compiling the code with the /4Yb option and running the application does not provide any indication of a problem. Compiling the code with the /Od option to disable optimizations and running the application does not change the behavior.
CAUSEThe compiler generates incorrect code when the result of the multiplication is larger than the range of an INTEGER*2 variable (-32,768 to 32,767).
RESOLUTIONTo address this problem, perform one of the following steps:
STATUSMicrosoft has confirmed this to be a problem in FORTRAN version 5.0 for MS-DOS and OS/2. This problem was corrected in FORTRAN version 5.1 for MS-DOS and OS/2.
MORE INFORMATIONThis error occurs when the result of the multiplication is larger than the range of an INTEGER*2 variable (-32,768 to 32,767). The compiler generates a shift instruction (SHL) to perform a fast multiply. The compiler does not use a four byte work space when it performs the shift; instead, it allocates only two bytes for its work space. Under these circumstances, significant bits can be lost or shifted into the sign bit when magnitude of the result of the multiplication is too large for an INTEGER*2 variable. The following code example demonstrates this problem and one technique to work around it.
Sample CodeC Compile options needed: None
INTEGER*2 K/1234/ INTEGER*2 M/32/, N/33/ WRITE(*, *) K*32, K*33 ! Displays incorrect results for K*32 WRITE(*, *) K*M, K*N ! Displays correct results for K*M END |
Additional reference words: 5.00 buglist5.00 fixlist5.10
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |