[PLUG] OT: Formula For Determining # Of Digits In An Integer

Galen Seitz galens at seitzassoc.com
Thu Feb 17 22:57:45 UTC 2005


Jason Van Cleve <jason at vancleve.com> wrote:

> Quoth Galen Seitz, on Thu, 17 Feb 2005 14:05:41 -0800:
> 
> > Brute force?
> > 
> > y = floor(log10((double)x)) + 1;
> 
> The "log10" is probably what I'm looking for.  I should have noted I'm
> doing this in Java; and Java has a one-argument 'log()' method, but it's
> base "e", and it looks like I need a base-ten logorithm?
> 
> > y = 1;
> > while (x /= 10)
> >   y++;
> 
> This is basically what I have now.  It's just that the method I'm
> writing is already called in a loop, so I wanted a single-operation
> formula instead of a loop algorithm.
> 

What's your concern?  Speed?  The integer loop is almost guaranteed to
beat any floating point implementation.

galen




More information about the PLUG mailing list