...
> > print *, x, nearestx
...
> Second, the PRINT statement can do anything it likes.
> You need to use formatted output.
Please note that the PRINT statement shown (well, and any PRINT
statements for that matter) *IS* formatted. It uses list-directed
formatting. What you almost certainly intend is to suggest using an
explicit format.
It is a moderately common error for people to refer to list-directed as
"unformatted". The standard uses the term "list-directed". The term
"free format" is also common. The term "unformatted" is just wrong. This
terminology error can be important because it directly causes people to
write code that doesn't work. For example, I have seen people specify
form="unformatted" in an OPEN statement because they intended to use
what they referred to as unformatted output.
That kind of programming error is why I tend to give a knee-jerk
reaction like this one to that particular terminology error.
This terminology error also causes problems when people try to describe
code to someone helping them with it, but it is just one of many
problems with that mode of operation. It is one I've seen enough to know
to prompt for clarification of to make sure that they actually mean what
they said, much like I prompt when people describe something as being a
parameter (they often mean a dummy argument instead of a named
constant).
As somewhat of an aside, the standard does allow compilers a lot of
freedom in some details of list-directed output, but it isn't entirely
accurate to say that the compiler can do "anything it likes". In
particular, changing this particular example to use an explicit format
would not help. For list-directed output of a real, the compiler is
required to use an E or F format. The compiler can choose the particular
precision and field width, but it can't do just anything. It is
certainly possible for the compiler to choose a precision or field with
that is unsuitable for a particular application, in which case I quite
agree that specifying the details yourself in an explicit format is the
usually recommended solution.
However, in this particular case, the compiler's choice of precision and
field width does not appear to be problematic. The only potential
problem that I see here in the formatting relates to the possibility of
rounding. The standard does not guarantee the details of rounding in
formatted output (unless one is using the IEEE features of the f95 TR or
f2003). I would not guess that does not appear to be the problem here,
but if it were, it could apply equally to list-directed and explicit
formatting.
--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain