[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference rusure::math

Title:Mathematics at DEC
Moderator:RUSURE::EDP
Created:Mon Feb 03 1986
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:2083
Total number of notes:14613

561.0. "LOG base 10" by EAGLE7::DANTOWITZ (David .. DTN: 226-6957 -- LTN2-1/H07) Fri Aug 08 1986 18:03

    
    Does anyone have any quick algorithms to compute the integer
    portion of log X base 10?  
    
    I need to know how many decimal digits a number has (using the
    binary form of the number).  The algorithm should be expressed
    in a hhl, not MACRO.
    
    	David
T.RTitleUserPersonal
Name
DateLines
561.1CLT::GILBERTeager like a childFri Aug 08 1986 19:056
    digits := 1;  temp := 10;
    while x >= temp do
	begin
	digits := digits + 1;
	temp := temp * 10;
	end;
561.2What HLL's do you know?MODEL::YARBROUGHFri Aug 08 1986 19:265
    (In Fortran:)
    	INTEGER digits
    	REAL	X
    	...
    	digits = ALOG10(X)+1.
561.3The fastest solutionEAGLE7::DANTOWITZDavid .. DTN: 226-6957 -- LTN2-1/H07Fri Aug 08 1986 20:3214
    
    I was looking for a QUICK algorithm that would be faster
    than the following:
    
    
    IF X LSS 10 then 0
    else IF X LSS 100 then 1
    else IF X LSS 1000 then 2
    
    etc ... I suppose that this is the fastest.

    
    
    
561.4Just a thoughtVIRTUE::HALLYBFree the quarks!Fri Aug 08 1986 22:008
    Sounds like microcode being developed here.  OK, is this a 32-bit
    unsigned integer?  31 bits including sign?  Any chance of parallel
    threads of execution?  For example, since 10^3 ~= 2^10 I would guess
    you have a chance of splitting the problem into 22 left bits and
    10 right bits.  If all 22 left bits are zero, you could look up
    the answer in a small RAM.  If any left 22 bits are nonzero, you
    could solve the problem for 22 left bits and add 3.  It all depends
    on how the bits fall.    
561.5CLT::GILBERTeager like a childFri Aug 08 1986 22:3815
How about :-)

    if x lss 10^8 then
	if x lss 10^4 then
	    if x lss 10^2 then
		if x lss 10^1 then 0 else 1
	    else
		if x lss 10^3 then 2 else 3
	else
	    if x lss 10^6 then
		if x lss 10^5 then 4 else 5
	    else
		if x lss 10^7 then 6 else 7
    else
	...
561.6?Data Type?CHOVAX::YOUNGChi-SquareSat Aug 09 1986 16:168
    One question:
    
    What is the data type of X?  Signed longword, Unsigned word, Real
    type-F, Real type-G, etc..? This is very important for finding the
    fastest algorithim.
    
    -- Barry
    
561.7?Distribution?CHOVAX::YOUNGChi-SquareSat Aug 09 1986 16:3615
    Another question:
    
    Did you want the fastest routine for a particular distribution?
    Usually uniform, but maybe reverse exponential?  Since in uniform
    distribution, those longwords with 10 digits outnumber all those
    with less than 10 digits COMBINED, this can be very important. 
    Reverse exponential, on the other hand, would assume that there
    is an equal chance of the number being a 1 digit, or 2 digit, ...
    or 10 digit number.
    
    Or, did you in fact just want the fastest algorithim based on O
    notation? : O(log i), O(i), O(k), ... etc.
    
    -- Barry
    
561.8A solution.CHOVAX::YOUNGChi-SquareSat Aug 09 1986 18:0925
    I have tested all the algorithm presented here, with the exception
    of the microcode suggestion.  I have assumed longword signed integers
    with uniform distribution.
    
    I believe that the fastest algorithim is the one that you listed,
    David, except that you should reverse the order of your tests. 
    That is, test for >= 10^9 first, then >= 10^8, et cetera:
    
    	If (x .ge. 10^9) then digits = 10
    	Else if (x .ge. 10^8) then digits = 9
    	.
    	.
    	.
    	Else if (x .ge. 10^1) then digits = 2
    	Else digits = 1
    	End if
    
    Using Fortran on a 780, this routine takes ~4.5 microseconds, as
    opposed to ~6.5 microseconds for Peters second routine (the next
    fastest), and ~23 microseconds for your original routine.
    
    I have the sources of these tests if you want them.
    
    -- Barry
    
561.9 Thanks EAGLEA::DANTOWITZDavid .. DTN: 226-6957 -- LTN2-1/H07Sun Aug 10 1986 14:3816
    The reason for my query is because I need to know the log base
    10 for a conversion routine.  (32-bits to decimal digits ...
    no microcode.)  The RTL routines like to right justify such
    conversions, so you end up with spaces.  Rather than searching for
    the first non-space I decided it's quicker to figure out how many
    digits you SHOULD have - thus my question.
    
    The distribution is currently not known.  Basically there will be
    quite a lot in the 0 to 100 and some others that may be higher.
    It's not uniform but will most likely be biased to the 0-100 range
    (~60% - I'm guessing)
    
    Thanks for the help.
    
    David
561.10Tweak to do negative numbers also...TLE::BRETTMon Aug 11 1986 20:1187
    I know you said "in a hll, not MACRO", but I never could obey
    instructions and the following is almost certainly expressible in
    your chosen language (although FORTRAN/COBOL/BASIC may have fun...)
          
    /Bevin
    
    
.entry Z_LOG10,^m<r2>
    movl    @4(ap),r2
    cvtld   r2,r0
    extzv   #7,#7,r0,r1
    movzbl  L_TABLE[r1],r0
    cmpl    r2,N_TABLE[r1]
    bleq    10$
    incl    r0
10$:ret

N_TABLE:
.long	0 
.long	9
.long	9
.long	9
.long	9
.long	9
.long	9
.long	99
.long	99
.long	99
.long	999
.long	999
.long	999
.long	999
.long	9999
.long	9999
.long	9999
.long	99999
.long	99999
.long	99999
.long	999999
.long	999999
.long	999999
.long	999999
.long	9999999
.long	9999999
.long	9999999
.long	99999999
.long	99999999
.long	99999999
.long	999999999
.long	999999999

L_TABLE:
.byte	0			       ;  0
.byte	1                              ;  1 
.byte	1                              ;  2 
.byte	1                              ;  3 
.byte	1                              ;  4 
.byte	1                              ;  5 
.byte	1                              ;  6 
.byte	2                              ;  7 
.byte	2                              ;  8 
.byte	2                              ;  9 
.byte	3                              ; 10 
.byte	3                              ; 11 
.byte	3                              ; 12 
.byte	3                              ; 13 
.byte	4                              ; 14 
.byte	4                              ; 15 
.byte	4                              ; 16 
.byte	5                              ; 17 
.byte	5                              ; 18 
.byte	5                              ; 19 
.byte	6                              ; 20 
.byte	6                              ; 21 
.byte	6                              ; 22 
.byte	6                              ; 23 
.byte	7                              ; 24 
.byte	7                              ; 25 
.byte	7                              ; 26 
.byte	8                              ; 27 
.byte	8                              ; 28 
.byte	8                              ; 29 
.byte	9                              ; 30 
.byte	9                              ; 31 

.end
    
561.11Palindromic HLLTOOK::APPELLOFCarl J. AppellofTue Aug 12 1986 13:113
    Come on, Bevin, how about giving it to us in the HLL to end all
    HLLs: ADA?