https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93550

            Bug ID: 93550
           Summary: Implement control of leading zero in formatted numeric
                    output
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libfortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: thenlich+gccbug at gmail dot com
  Target Milestone: ---

In 2018, J3 Fortran Standards Committee agreed to add "leading zero format
control" to its work list for Fortran 202x.

While not (yet) part of the Fortran standard, it might make sense to implement
(as an experimental GNU extension) or at least prepare for this upcoming change
in order to clean up the library code, thus removing all hard-coded assumptions
regarding leading zero.

https://j3-fortran.org/doc/year/18/18-268.txt:

When using F format to output a value less than 1, the standard
allows the implementation to choose whether to include a
zero before the decimal symbol.

13.7.2.3.2 (F format) paragraph 11 says (in part):

"Leading zeros are not permitted except for an optional zero 
immediately to the left of the decimal symbol if the magnitude 
of the value in the output field is less than one. The optional 
zero shall appear if there would otherwise be no digits in the 
output field."

Similarly for E and D formats, when the scale factor is zero, 
13.7.2.3.2p11 says:

"Leading zeros are not permitted except for an optional zero 
immediately to the left of the decimal symbol if the magnitude of the 
value in the output field is less than one. The optional zero shall 
appear if there would otherwise be no digits in the output field."

EN, ES and EX formats do not have this option.

Some implementations always provide the leading zeros, some do
not, and this inconsistency results in user complaints, especially
when test programs produce different results across different
implementations. The inconsistency also occurs for G format when
the value is in a certain range, since E and F formatting is used.

Unlike the optional sign before a positive value, there is no
user control over the optional zero.

Use Cases
---------

Many users have complained over the years that this inconsistency
makes it difficult to automatically validate results across different
implementations and, in some cases where the leading zero is omitted, 
makes the output from a Fortran application unusable by applications
written in other languages.

Requirements
------------

- Add control edit descriptors, analogous to the SS, SP and S edit 
  descriptors, allowing the programmer to specify whether a leading
  zero is to appear in output for the F, E, D and G formats in the 
  cases where it is currently optional.

  The requirement of the leading zero when otherwise no digits would 
  appear is to remain. As with the existing S control descriptor,
  one of the new descriptors selects the implementation-defined 
  behavior, which remains the default.

- Continuing the analogy with SS, SP and S, and the SIGN= OPEN/INQUIRE 
  specifier, add a new specifier to OPEN and INQUIRE to allow 
  specification or inquiry of the current mode. Like SIGN=, this
  is to be a "changeable mode" (12.5.2p1). List-directed and namelist-
  directed output are to use the current "leading zero" mode.

https://j3-fortran.org/doc/year/19/19-156r1.txt:

Requirements
------------

- Add control edit descriptors, analogous to the SS, SP and S edit 
  descriptors, allowing the programmer to specify whether a leading
  zero is to appear in output for the F, E, D and G formats in the 
  cases where it is currently optional.

  The requirement of the leading zero when otherwise no digits would 
  appear is to remain. As with the existing S control descriptor,
  one of the new descriptors selects the implementation-defined 
  behavior, which remains the default.

- Continuing the analogy with SS, SP and S, and the SIGN= OPEN/INQUIRE 
  specifier, add a new specifier to OPEN and INQUIRE to allow 
  specification or inquiry of the current mode. Like SIGN=, this
  is to be a "changeable mode" (12.5.2p1). List-directed and namelist-
  directed output are to use the current "leading zero" mode.

Specification
-------------

1. Add the following control edit descriptors: LZS, LZP, LZ.

The LZS, LZP, and LZ edit descriptors temporarily change (12.5.2) the 
leading zero mode for the connection. The edit descriptors LZS, LZP, 
and LZ set the leading zero mode corresponding to the LEADING_ZERO= 
specifier values SUPPRESS, PRINT, and PROCESSOR_DEFINED, respectively.

The leading zero mode controls optional leading zero characters in numeric
output fields. When the leading zero mode is PRINT, the processor shall 
produce a leading zero in any position that normally contains an optional 
leading zero. When the leading zero mode is SUPPRESS, the processor shall
not produce a leading zero in such positions. When the leading zero mode
is PROCESSOR_DEFINED, the processor has the option of producing a leading 
zero or not in such positions, subject to 13.7.2(5) [rule about filling
field with asterisks and optional characters].

The LZS, LZP, and LZ edit descriptors affect only F and E editing during 
the execution of an output statement. The LZS, LZP, and LZ edit 
descriptors have no effect during the execution of an input statement.

2. Add the data transfer statement specifier LEADING_ZERO

The specifier value shall evaluate to PRINT, SUPPRESS, or 
PROCESSOR_DEFINED. The LEADING_ZERO= specifier temporarily changes 
(12.5.2) the leading zero mode for the connection. If the specifier 
is omitted, the mode is not changed. 

3. Add the OPEN specifier LEADING_ZERO=.

The specifier value shall evaluate to one of PRINT, SUPPRESS, or 
PROCESSOR_DEFINED. The LEADING_ZERO= specifier is permitted only for a 
connection for formatted input/output. It specifies the leading zero mode
for this connection. It is a changeable mode (12.5.2). If this specifier 
is omitted in an OPEN statement that initiates a connection, the default 
value is PROCESSOR_DEFINED. 

4. Add the INQUIRE specifier LEADING_ZERO to retrieve the leading zero
mode for a connected unit.

Reply via email to