J3/04-249 To: J3 From: Craig Dedo Date: February 16, 2004 Subject: Variable Format Expressions Number: Title: Variable Format Expressions Submitted By: Craig Dedo Status: For Consideration References: Basic Functionality: A variable format expression (VFE) allows a programmer to use an arithmetic expression at any place in a format edit descriptor where an integer can be used. Rationale: The current Fortran 95 standard and the Fortran 2003 FCD only allow format edit descriptors which are either character expressions or hard coded into the source code. A VFE provides the application programmer a convenient and straightforward method of varying the format edit descriptor at run time, as conditions require. Many application programmers consider VFEs to be easier to use and less error prone than the only current alternative of building format specifications in character expressions. Thus, VFEs offer the application developer an opportunity to save on programming time and effort, with a consequent increase in productivity. It is the intent of this proposal to ratify existing practice. A survey of existing compilers indicates that most implementations which offer this feature do so in a similar, if not identical, manner. Estimated Impact: There would be no impact on existing codes. There should be little or no conflict with the existing extension, since the extension has nearly identical technical specification in existing implementations. The existing technical specification is well known and well understood, so there should be little, if any, ambiguity. Preliminary work I did on this feature in 1996 and 1997 indicates that a complete specification in the standard would be around half a page of text. There should be no interaction with other features. Detailed Specification: A variable format expression is a scalar numeric expression enclosed inside angle brackets, <>. A VFE can be used in a format edit descriptor any place that an integer can. During program execution, the VFE is re-evaluated each time it is encountered in the normal format scan. If the VFE is not of integer data type, it is converted to integer type before it is used, according to the usual rules for data type conversion. A VFE may be of any KIND type that is defined on the processor. The VFE can be any valid scalar numeric expression. It can include dummy arguments, function references, and named constants. It shall not include any array valued data item or any variable with the POINTER attribute. Any function referenced in a VFE shall not execute any I/O operation. The value of a VFE must obey the restrictions on magnitude applying to its use in the format, or an error occurs. VFEs shall not be used in a character format specification (10.1.2). VFEs are evaluated each time they are encountered in the scan of the format. If the value of the variable used in the expression changes during the execution of the I/O statement, the new value is used the next time the format item containing the expression is processed. Example Consider the following statements: INTEGER :: IWIDTH, IDIGITS, IEXPONENT, NF, NE REAL, ALLOCATABLE :: FREAL (:), EREAL(:) ! Intervening statements defining the values of NF and NE ALLOCATE FREAL (NF) ALLOCATE EREAL (NE) ! Intervening statements defining the elements of FREAL, EREAL, IWIDTH, IDIGITS, ! and IEXPONENT. WRITE (*, 601) FREAL, EREAL 601 FORMAT (5X, F., & & 5X, E.E) If, on one execution of the WRITE statement, NF is 5, NE is 3, IWIDTH is 12, IDIGITS is 7, and IEXPONENT is 2, then statement 601 would be equivalent to: 601 FORMAT (5X, 5F12.7, 5X, 3E18.7E2) If, on a subsequent execution of the WRITE statement, NF is 6, NE is 2, IWIDTH is 10, IDIGITS is 5, and IEXPONENT is 3, then statement 601 would be equivalent to: 601 FORMAT (5X, 6F10.5, 5X, 2E16.5E3) History: