Print

Print


Renchi Raju wrote:
...
what is wrong with casting the right hand side expressions'
> floating point literals to the precision of the left hand side
> variable and do it ONLY for the floating point literals. for eg in a case
> like:
> integer, parameter :: SP = selected_real_kind(6,20)
> integer, parameter :: DP = selected_real_kind(14,30)
>
> real(DP) :: a
> real(SP)  :: b
> a = sin(3.0)
> b = sin(3.0)
>
> currently one would write this as:
> a = sin(3.0_DP)
> b = sin(3.0_SP)
>
> why not just use whats expected on the lhs and properly cast the literals
> on the rhs to the required precision to whats needed.

Fortran doesn't have sufficient information to apply this rule uniformly.
And without making such a rule uniform, it complicates the language.
What would be the rule for:

   Write(11) sin(3.0)

Does this write a single precision value or a double?  It depends on what
is expected by the unseen program that subsequently reads the data back
in.

In alnguages like Ada, you can have the rule you describe because there is
no context in the language where the required result type of an expression
cannot be determined from context.  In Fortran, that is simply not true.

Now, I have proposed in the past that the standard require context-based
promotion where such things are possible and simply disallow KIND-less
expressions in those places where context was inconclusive.  That's always
been soundly rejected (and not for completely bad reasons, both sides
have a reasonable case).  Right now, Fortran only has one context-based
type decision: the NULL intrinsic function without the optional MOLD
argument.

--
J. Giles