PRB: Incorrect Results Returned, C Float Functions

Last reviewed: July 19, 1995
Article ID: Q79381
The information in this article applies to:
  • Microsoft FORTRAN for MS-DOS, versions 4.1, 5.0, and 5.1
  • Microsoft FORTRAN for OS/2, versions 4.1, 5.0 and 5.1

SYMPTOMS

When using the Microsoft FORTRAN Compiler version 4.1, 5.0, or 5.1, if FORTRAN code attempts to access a C float function, incorrect results may be returned from the C function.

CAUSE

FORTRAN expects a DOUBLE PRECISION number to be returned from both REAL and DOUBLE PRECISION functions. C functions of type float return only 4 bytes, and the FORTRAN run-time incorrectly interprets the value stored in the floating accumulator (__fac) as an 8-byte number.

RESOLUTION

This problem can be avoided by declaring the C function as type double, or by declaring the C function with the _fortran attribute.

MORE INFORMATION

Sample Code #1

The following code reproduces the problem:

FORTRAN Code

       interface to real function cthing[c](dog)
       real dog[value]
       end

       real dog
       real cthing,r
       dog=2
       r=cthing(dog)
       write(*,*) r
       end

C Code

#include <stdio.h>

float cthing(float dog) {
   float cat;
   printf("%f \n",dog);
   cat=3.0;
   printf("%f \n",cat);
   return cat;
}

Output: Incorrect Results Generated Here

2.000000 3.000000

   0.000000E+00


Sample Code #2

The following solution declares the C function with the _fortran attribute, and generates the expected output:

FORTRAN Code

       real dog,r
       real cthing
       dog=2.0
       r=cthing(dog)
       write(*,*) r
       end

C Code

#include <stdio.h>

float _fortran cthing(float *dog) {
  float cat;
  printf("%f \n",*dog);
  cat=3.0;
  printf("%f \n",cat);
  return cat;
}

Output

2.000000 3.000000

      3.000000


Additional reference words: 5.00 5.10 mixed language
KBCategory: kbprg kbinterop kbcode
KBSubcategory: FORTLngIss CLngIss


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: July 19, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.