j3-fortran / fortran_proposals

Proposals for the Fortran Standard Committee
175 stars 14 forks source link

Grouping numbers with spaces in free form #311

Open tkoenig1 opened 11 months ago

tkoenig1 commented 11 months ago

There are two advantages of fixed form: First, it makes it possible to write

      PROGRAMME MAIN

for those who prefer British spelling, and it offers the possibility of grouping numbers, like

      MILLION = 1 000 000

The first advantage is not really big, but the second one is actually a serious use case when trying not to lose count of digits.

So, a syntax to allow grouping of numbers would come in handy. Other languages have underscores, but that is already taken in Fortran for KIND. But what about actually allowing spaces in free form in numbers?

FortranFan commented 11 months ago

@tkoenig1 wrote Aug. 5, 2023 5:53 PM EDT:

a syntax to allow grouping of numbers would come in handy

It will be great if you @tkoenig1 can draft a proposal in the J3 paper format and post it here: https://j3-fortran.org/doc/meeting/231

These are the kind of proposals toward which WG5 must show vision to address and open up a "lane" for fast and easy discussion and ready acceptance into the Fortran standard. One should not have to wait 5 to 10 years.

septcolor commented 10 months ago

For namelist files, it has different interpretations for arrays?

program main
    implicit none
    integer :: arr(3), inp
    namelist /param/ arr

    open(newunit=inp, file="param.dat", status="old")
    arr(:) = 0
    read(inp, nml=param)
    print *, arr(:)       !! 1  200  0
    close(inp)
end

!! param.dat
&param
arr = 1 200 000

  !! (The above line is the same as)
  !! arr = 1, 200, 000
  !! arr(1:3) = 1, 200, 000
/

Using double underscores (like 1__000__000) or tilde (like 1~000~000) might be another approach (cf: Concatenation of arrays and strings in the D language below)

ivan-pi commented 9 months ago

This would've been handy for me in the past, when copying routines from Julia.

FortranFan commented 9 months ago

This would've been handy for me in the past, when copying routines from Julia.

Are you implying some corollary to the jaded disclaimer, "past performance is no guarantee of future results"!!!?

Since"(t)his would've been handy for" you "in the past", can a reader see this as useful in the future too? or not?!

ivan-pi commented 9 months ago

Sorry I was unclear. I once did a porting of the Julia's erfinv function, which contains grouped numbers:

        return x * @horner(t, 0.16030_49558_44066_229311e2,
                             -0.90784_95926_29603_26650e2,
                              0.18644_91486_16209_87391e3,
                             -0.16900_14273_46423_82420e3,
                              0.65454_66284_79448_7048e2,
                             -0.86421_30115_87247_794e1,
                              0.17605_87821_39059_0) /

The grouping absolutely helps guide the eye when entering digits by hand, and it would save me the effort of deleting the underscores. I'd appreciate having it in the future.

Python allows grouping digits too:

$ python3
Python 3.10.9 (main, Jan 11 2023, 09:18:20) [Clang 14.0.6 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 1.000_10_3_4
1.0001034

Using a different symbol than _ would however introduce friction for people used to Python or Julia.

tkoenig1 commented 3 months ago

Using a different symbol than _ would however introduce friction for people used to Python or Julia.

One could argue that Python and Julia introduced the friction by deviating from Fortran's prior use of _ :-)