> I wrote a F90 version of this not more than a week ago!! Though using
> log10 to get the number of digits always seems to get the right
> answer, perhaps this is not necesserily guarenteed? Can a standard
> conforming compiler give ( int(log10(real(10**i))) /= i ) ?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
No compiler, standard or otherwise, will evaluate that as .TRUE.
The _definition_ of LOG10 is:
if (Y .EQ. 10**X) then (LOG10(Y) .EQ. X).
So, if we replace the Y in the second equation by 10**X, we get:
LOG10(10**X) .EQ. X
We can always get the number of digits in a positive integer by taking
the LOG of that integer in whatever base it is in. So, for a positive
integer N in base 10, it has LOG10(N) digits. A hexadeciamal integer
X has LOG16(X) hexadecimal digits. A binary integer B has LOG2(B)
binary digits.
Non-positive integers can be handled by the famous computed GOTO:
C23456789 123456789 123456789 123456789
GOTO(N) 10, 20, 30
10 IWIDTH = 1+INT(LOG10(REAL(-N)))
GOTO 40
20 IWIDTH = 1
GOTO 40
30 IWIDTH = INT(LOG10(REAL(-N)))
40 CONTINUE
Of course, this can be made prettier in Fortran90....
-jack
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|