Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Fortran > Re: variable le...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 12 of 18 Topic 8182 of 8775
Post > Topic >>

Re: variable length of integer

by "jamesgiles@[EMAIL PROTECTED] " <jamesgiles@[EMAIL PROTECTED] > Apr 29, 2008 at 03:02 PM

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
 




 18 Posts in Topic:
variable length of integer
rudra <bnrj.rudra@[EMA  2008-04-26 11:03:17 
Re: variable length of integer
Bil Kleb <Bil.Kleb@[EM  2008-04-26 14:06:33 
Re: variable length of integer
Clive Page <junk@[EMAI  2008-04-28 09:56:44 
Re: variable length of integer
nospam@[EMAIL PROTECTED]   2008-04-28 20:44:12 
Re: variable length of integer
relaxmike <michael.bau  2008-04-29 03:01:34 
Re: variable length of integer
John Appleyard <spamtr  2008-04-29 14:48:11 
Re: variable length of integer
nospam@[EMAIL PROTECTED]   2008-04-29 09:25:43 
Re: variable length of integer
"jamesgiles@[EMAIL P  2008-04-29 13:59:18 
Re: variable length of integer
John Appleyard <spamtr  2008-04-30 10:58:43 
Re: variable length of integer
"jamesgiles@[EMAIL P  2008-04-29 14:11:18 
Re: variable length of integer
"James Van Buskirk&q  2008-04-29 15:16:10 
Re: variable length of integer
"jamesgiles@[EMAIL P  2008-04-29 15:02:51 
Re: variable length of integer
"Kurt Kallblad"  2008-04-30 11:52:39 
Re: variable length of integer
relaxmike <michael.bau  2008-04-30 01:03:01 
Re: variable length of integer
glen herrmannsfeldt <g  2008-04-30 00:20:59 
Re: variable length of integer
Catherine Rees Lay <ca  2008-04-30 10:22:25 
Re: variable length of integer
"jamesgiles@[EMAIL P  2008-04-30 11:12:35 
Re: variable length of integer
"jamesgiles@[EMAIL P  2008-04-30 11:29:39 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Oct 13 3:11:34 CDT 2008.