Skip to main content

Software  >  Rational  >  C and C++ Compilers  >  

Call to Fortran subroutine ignores and changes the numeric locale

 Technote (FAQ)
 
Question
Floating point I/O inside a Fortran subroutine ignores my locale setting, and changes LC_NUMERIC to "C" ("POSIX"). Why does this happen, and what can I do?
 
Cause
The Fortran Runtime Environment sets LC_NUMERIC to "POSIX" to implement floating-point I/O using a period (".") as the decimal separator.
 
Answer
Changing the locale

The Fortran Runtime Environment (RTE) implementation changes LC_NUMERIC to "POSIX" before the first Fortran I/O statement. (This is documented in the "XL Fortran Compiler Reference"; you can find a PDF version of this document by following the "XL Fortran Manuals" link below.)

The Fortran RTE will return LC_NUMERIC to its original value at the end of the program, but for a mixed-language program this may leave C or C++ code with a different locale than expected after a call to a Fortran subroutine.

To ensure that Fortran I/O and C or C++ code expecting a numeric locale other than "POSIX" behave correctly, it is recommended to set LC_NUMERIC to "POSIX" before calling a Fortran subroutine, and to reset it after the call returns.

For example:
void do_fsub(double val)
{
  char * ploc = setlocale(LC_NUMERIC, NULL);
  setlocale(LC_NUMERIC, "POSIX");
  fsub(val);
  setlocale(LC_NUMERIC, ploc);
}

Changing the decimal separator

While Fortran requires "POSIX" for its numeric locale when doing I/O, the Fortran 2003 standard and IBM XL Fortran V11.1 provide a standard-compliant method to specify a decimal separator other than the period (".") using the DECIMAL= specifier. (This is documented in the "XL Fortran Language Reference" for V11.1; you can find a PDF version of this document by following the "XL Fortran Manuals" link below.)

The DECIMAL= specifier accepts the values "POINT" and "COMMA"; "POINT" is the default. By using the DECIMAL= specifier, Fortran can read and write floating-point values where a comma (",") is used as the decimal separator instead of a period (".").

For example:
subroutine fsub(v)
real, intent(in) :: v

open(8,FILE="regular")
open(9,FILE="special",DECIMAL="COMMA")

write(8,*) "by default in 'regular' v prints:", v
write(9,*) "by default in 'special' v prints:", v
write(8,*,DECIMAL="COMMA") "switching decimal in 'regular'," &
    "v prints:", v
write(9,*,DECIMAL="POINT") "switching decimal in 'special'," &
    "v prints:", v
end subroutine fsub

 
Related information
XL Fortan Manuals
 
 
Cross Reference information
Segment Product Component Platform Version Edition
Software DevelopmentXL C Enterprise Edition for AIX AIXAll VersionsEnterprise Edition
Software DevelopmentXL FortranRTEAll Platforms10.1, 9.1, 11.1All Editions
Software DevelopmentXL FortranCompilerAll Platforms10.1, 9.1, 11.1All Editions
 
 

Copyright and trademark information
IBM, the IBM logo and ibm.com are trademarks of International Business Machines Corp., registered in many jurisdictions worldwide. Other product and service names might be trademarks of IBM or other companies. A current list of IBM trademarks is available on the Web at "Copyright and trademark information" at www.ibm.com/legal/copytrade.shtml.
Rate this page
Please take a moment to complete this form to help us better serve you.
This material provides me with the information I need.




This material is clear and easy to understand.




Did the information help you to achieve your goal?
What updates, improvements, or related information would you like to see in this document?
Your response will be used to improve our document content. Requests for assistance, if applicable, should be submitted through your normal support channel as we cannot respond from this site.
Input the verification number to submit feedback:
Document information
 Product categories:
 Software
 Software Development
 Traditional Progamming Language & Compilers
 XL C Enterprise Edition for AIX
 Operating system(s):
  AIX, Blue Gene/L, Blue Gene/P, Linux/QS21 blade, Linux/System p
 Software version:
  All Versions
 Software edition:
  All Editions
 Reference #:
  1288943
 IBM Group:
 Software Group
 Modified date:
 2007-12-06

Translate My Page
 
 

Rate this page

Help us improve this page. Your response will be used to improve our document content. Requests for assistance, if applicable, should be submitted through your normal support channel as we cannot respond from this site.