FIX: Incorrect Results from Mixed-Type Expressions

Last reviewed: September 16, 1997
Article ID: Q85306
4.00 4.01 4.10 5.00 5.10 | 4.10 5.00 5.10
MS-DOS                   | OS/2
kbtool kbfixlist kbbuglist kbcode

The information in this article applies to:

  • Microsoft FORTRAN for MS-DOS, versions 4.0, 4.01, 4.1, 5.0, and 5.1
  • Microsoft FORTRAN for OS/2, versions 4.1, 5.0, and 5.1

SYMPTOMS

A program compiled with Microsoft FORTRAN version 4.0, 4.01, 4.1, 5.0, or 5.1 under MS-DOS or version 4.1, 5.0, or 5.1 under OS/2 can generate incorrect results on mixed-type expressions when an INTEGER variable is used on both sides of an equation. The /Od compiler option, which is used to suppress optimization, has no effect.

CAUSE

The compiler is incorrectly converting intermediate values to the INTEGER type used on both sides of the arithmetic statement.

RESOLUTION

To avoid this problem, keep data types consistent within arithmetic expressions by using the intrinsic functions to force the conversion of intermediate expressions to the proper data type.

STATUS

Microsoft has confirmed this to be a problem in Microsoft FORTRAN versions 4.0, 4.01, 4.1, 5.0, and 5.1 for MS-DOS and versions 4.1, 5.0, and 5.1 for OS/2. This problem was corrected in FORTRAN PowerStation.

MORE INFORMATION

Sample Code 1

The following code reproduces the problem:

      integer*4 i, j
      i=10
      j=i-1.1   ! correct (10.0 - 1.1 = 8.9, truncated to 8)
      i=i-1.1   ! incorrect   (10 - 1 = 9)

      write(*,*) 'j=',j,' i=',i      ! outputs j=8 i=9
      end

Sample Code 2

The following code illustrates the solution:

      integer*4 i, j
      i=10
      j=i-1.1         ! correct (10.0 - 1.1 = 8.9, truncated to 8)

      i=real(i)-1.1   ! convert i to real, then set to an integer
                      ! variable when assigned
      write(*,*) 'j=',j,' i=',i      ! correct output generated
      end


Additional reference words: 4.00 5.10 5.00 5.10
KBCategory: kbtool kbfixlist kbbuglist kbcode
KBSubcategory: FLIss
Solution Type : kbfix


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 16, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.