Last week I posted a concise solution to the problem of writing a
Fortran90 function to convert an integer into a trimmed left-justified
character string. I am returning to the subject only because one
statement in my code gave several reputable compilers a touch of
indigestion:
CHARACTER(LEN=12 - MAX(0,SIGN(1,intval)) - VERIFY(TRANSFER(ACHAR(48+ &
(/(MOD(MAX(1,ABS(intval))/10**n,10), n=9,0,-1)/) ), &
"1234567890"), ACHAR(48)) ) :: STRING
I can't actually see anything wrong with this, myself. And critical
reaction has been favourable (I think):
"May God have mercy on your soul. :-)" (Peter Shenkin)
"Clive, you are definitely evil... :-)" (Steve Lionel)
In fact the NAG compilers were the only ones I could find which compiled
and ran it without problems. So here is an alternative solution, longer
but perhaps more elegant:
CHARACTER(LEN=12 - MAX(0,SIGN(1,intval)) - VERIFY( &
ACHAR(48+MOD(ABS(intval)/1000000000,10)) // &
ACHAR(48+MOD(ABS(intval)/100000000,10)) // &
ACHAR(48+MOD(ABS(intval)/10000000,10)) // &
ACHAR(48+MOD(ABS(intval)/1000000,10)) // &
ACHAR(48+MOD(ABS(intval)/100000,10)) // &
ACHAR(48+MOD(ABS(intval)/10000,10)) // &
ACHAR(48+MOD(ABS(intval)/1000,10)) // &
ACHAR(48+MOD(ABS(intval)/100,10)) // &
ACHAR(48+MOD(ABS(intval)/10,10)) // &
ACHAR(48+MOD(MAX(1,ABS(intval)),10)), ACHAR(48))) :: string
I am pleased to report that this form works with Digital, NAG, Salford,
and Lahey f90 compilers. Even though it is a little longer than before it
is, I think, still more compact than the function posted by Malcolm Cohen
which only works under the Fortran95 standard.
Unfortunately SUN and MIPSpro f90 compilers will not compiler either
version, and both compilers give identical error messages. I wonder if
they are based on the same code? Thse failures are disappointing but
perhaps not entirely inconsistent with the reputations of these compilers;
I'll send them my bug reports shortly.
It is also disappointing that neither algorithm converts (-HUGE(0)-1)
correctly, simply because on every system that I've tried so far the
intrinsic ABS returns a negative result given this number as an argument.
I think both my functions will work on non-ASCII machines (though I still
doubt if any with Fortran90 compilers really exist) but only on 32-bit
integers. The extension to 64-bit integers is fairly obvious, but the
editing will be more tedious in the second case.
--
Clive Page, Internet: [log in to unmask]
Dept of Physics & Astronomy,
University of Leicester, Phone +44 116 252 3551
Leicester, LE1 7RH, U.K. Fax +44 116 252 3311
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|