min/max and string arguments

Steve Kargl sgk@troutmask.apl.washington.edu
Thu Jan 1 20:25:00 GMT 2009


On Thu, Jan 01, 2009 at 08:29:26PM +0100, Daniel Franke wrote:
> 
> Happy new year, everyone!

Likewise!

> 
> Let's start this year with a small puzzle :)
> 
> What should be the proper output of this code fragment?
> 
> 	print *, MIN ("foo", "", "bar")
> 	end
> 
> gfortran answers "bar" while an equivalent C-fragment using strcmp() would 
> result in "".

Looks like a bug.  MIN/MAX state

   For arguments of character type, the result is the value that would
   be selected by application of intrinsic relational operators; that is,
   the collating sequence for characters ...

I suppose the question becomes:  What is the collating sequence for
the empty string ""?.  Is "" the ASCII NUL character?  Of course,
Fortran strings are not NULL terminated.  Section 4.4.4.3 discusses
the collating sequence, but it does not call out any special handling
of zero length strings.

It is instructive to read the description of LGT(...) and friends.  The
comparison LGT("", " ") would actually compare the strings " " and " ".
There appears to be requirement of extending the strings to equal lengths
in MIN/MAX.

> gfortran's behavior becomes especially interesting with:
> 
> 	min("", " ")   ->   " "
> 	max("", " ")   ->   " "
> 
> while one would probably expect "" and " " respectively?!

No.  I'd expect " " for both.

13.7.76 MIN(...)

    For arguments of character type, the length of the result is the length
    of the longest argument.
....
    If the selected argument is shorter than the longest argument, the
    result is extended with blanks on the right to the length of the
    longest argument.

There is similar text in 13.7.72 for MAX(...).  What does 
min("", "a") return?  Here, I'd expect " " if a zero length string
is considered to be composed of the ASCII NUL character.

-- 
Steve



More information about the Fortran mailing list