Le 09/04/2022 à 19:15, Ron Shepard a écrit :
> On 4/9/22 9:05 AM, Beliavsky wrote:
>> Since compilers treat the non-standard but very common
>>
>> real*4, real*8, integer*4, integer*8
>>
>> as
>>
>> use iso_fortran_env
>> real(kind=real32), real(kind=real64), integer(kind=int32),
>> integer(kind=int64)
>>
>> would it make sense to standardize the former set of declarations,
>> which is concise and clear, to mean the same as the latter?
>
> In my opinion, this would be a step backward for the language.
>
> There are many reasons why this should not be done, but one of them is
> that REAL*4 etc. were always just defined by the local compiler to map
> to its supported floating point formats. For byte addressable machines,
> that meant that your suggested mapping would be correct. But there are
> also 36-bit, 60-bit, and 64-bit machines that cannot conform to those
> mappings, so they must do something different. In the f77 time in the
> 1980s, I used those declarations extensively for exactly that reason, it
> was a way, with some care, to write portable (but nonstandard) code. I
> used REAL*8 declarations to run on machines with all of those word
> lengths. It wasn't pretty, but it served its purpose at the time.
>
> But now we have the fortran KIND system, which handles all of that much
> better. Yes, when you incorporate old f66 and f77 code into your current
> project, you need to edit in those changes, but that is not a big deal.
> Your minimal efforts are rewarded with clarity and portability. I have
> programs with hundreds of thousands of lines where I can change the
> precision globally by modifying a single line of code in a module. From
> the programmer's perspective, that is hard to beat.
>
> Numerical analysts are now experimenting with 16-bit real arithmetic to
> speed up critical parts of algorithms. If/when they get that working,
> the fortran KIND system is already set up to handle that. IEEE decimal
> arithmetic is another feature raising its head on the horizon. Again,
> the fortran KIND system is already there and waiting. 10-byte arithmetic
> is another example (although some compilers are currently doing this the
> wrong way, I think). 128-bit arithmetic and beyond, both reals and
> integers, are all handled perfectly well with the fortran KIND system.
>
> If a compiler supports real types with those numbers of bits (real32,
> real64, etc.), then the kind values are set appropriately. If there is
> more than one kind with that many bits, then the processor chooses which
> one to return. If the processor does not support a kind with exactly
> those bits, then the processor sets the value to either -1 or -2. The -2
> value means that some kind is available with higher precision. This
> might not be the best way to write portable code, but if you want to
> write code targeted to specific floating point formats, that is a
> reasonable way to do it. The IEEE intrinsic module goes even further if
> code is intended to target that floating point format, rounding modes,
> NaN, subnormals, etc., all available within the fortran KIND system.
> This is all in contrast to REAL*4, REAL*8, etc, which are mapped in a
> very loose manner to the underlying supported floating point formats.
>
> I think the fortran KIND system is well thought out and it works well in
> practice.
And yet, it still lacks something as simple as REAL*8, which would
select either the default REAL or the DOUBLE PRECISION, and nothing else.
kind=real64 is almost that, except that:
- it may not exist
- nothing garantees that real64 is kind(1.0) or kind(1.0d0)
OK, as of today there's probably no compiler where real64 does not exist
(or does exist but is inefficient). Nevertheless it can happen one day
or another...
I used to write:
integer, parameter :: sp = kind(1.0)
integer, parameter :: dp = kind(1.0d0)
integer, parameter :: sp_is_enough = min(max(precision(sp)-9,1),0)
integer, parameter :: double = sp*sp_is_enough + dp*(1-sp_is_enough)
so REAL(kind=double) is similar to the C "double", which is only
required to have at least 10 digits precision, and which can be
identical to "float". And most importantly it is always, and will always
be, defined.
kind=sp, kind=dp, and kind=double, cover 99.99% of the needs.