On Apr 29, 3:16=A0pm, "James Van Buskirk" <not_va...@[EMAIL PROTECTED]
> wrote:
=2E..
> The danger of posting untested code. =A0I have been bitten many times.
> I suppose you didn't intend that DO WHILE loop to be infinite, either
> did you? :)
No, probably not. Still, you never know when a good infinite loop
might come in handy. I suppose I should wait for even more bugs to be
discovered, but here's a re-post of the code corrected according to
the problems re****ted so far:
pure function IntToString(i)
! This version can be also used in I/O statements
implicit none
integer :: i
character( LenOfInt(i) ) :: IntToString
integer :: val , p , d
if ( i<0 ) then
IntToString(1:1) =3D '-'
endif
val =3D i
do p =3D len(IntToString), merge(2,1,i<0), -1
d =3D mod(val,10) + 10
val =3D val/10
IntToString(p:p) =3D "9876543210123456789"(d:d)
end do
end function IntToString
pure function LenOfInt(i)
implicit none
integer :: i, LenOfInt
integer :: val
LenOfInt =3D merge(0,1,i>0)
val =3D i
do while (val /=3D 0)
LenOfInt =3D LenOfInt + 1
val =3D val/10
end do
return
end function LenOfInt
In addition, I made the IntToString function PURE here. It can't be
made ELEMENTAL (obviously, since its very purpose is to return strings
whose length isn't constant). Like most Fortran functions, this one
does have potential side-effects. In this case, you might get an
error if the allocation of space for the result fails. Other than
that, it's hard to see any side-effects arising that I would not
regard as implementation bugs. And, since allocation failure is not
among the side-effects that preclude PURE, why not declare it that
way?
I *hope* this is now a clean copy!
--
J. Giles
"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare


|