Velocity Reviews > C function for returning number of digits?

# C function for returning number of digits?

Luke Wu
Guest
Posts: n/a

 11-29-2005
Is there a C function that returns the number of digits in an input
int/long?

example:

numdigits(123) returns 3
numdigits(1232132) returns 7

Chris McDonald
Guest
Posts: n/a

 11-29-2005
"Luke Wu" <(E-Mail Removed)> writes:

>Is there a C function that returns the number of digits in an input
>int/long?

>example:

>numdigits(123) returns 3
>numdigits(1232132) returns 7

Learn about lagarithms, then apply that knowledge to standard C.

--
Chris.

Luke Wu
Guest
Posts: n/a

 11-29-2005

Chris McDonald wrote:
> "Luke Wu" <(E-Mail Removed)> writes:
>
> >Is there a C function that returns the number of digits in an input
> >int/long?

>
> >example:

>
> >numdigits(123) returns 3
> >numdigits(1232132) returns 7

>
>
> Learn about lagarithms, then apply that knowledge to standard C.
>

Ohh my.... didn't think it would be a two liner.......thank you
#include<math.h>

int numdigits(int n)
return log10(n) + 1;

Chris McDonald
Guest
Posts: n/a

 11-29-2005
"Luke Wu" <(E-Mail Removed)> writes:

>Chris McDonald wrote:
>> "Luke Wu" <(E-Mail Removed)> writes:
>>
>> >Is there a C function that returns the number of digits in an input
>> >int/long?

>>
>> >example:

>>
>> >numdigits(123) returns 3
>> >numdigits(1232132) returns 7

>>
>>
>> Learn about lagarithms, then apply that knowledge to standard C.
>>

>Ohh my.... didn't think it would be a two liner.......thank you
>#include<math.h>

>int numdigits(int n)
> return log10(n) + 1;

Check its prototype - it's double log10(double x);
--
Chris.

Walter Roberson
Guest
Posts: n/a

 11-29-2005
In article <(E-Mail Removed) .com>,
Luke Wu <(E-Mail Removed)> wrote:
>Is there a C function that returns the number of digits in an input
>int/long?

>example:

>numdigits(123) returns 3
>numdigits(1232132) returns 7

There is no standard one, no, but you can write your own.

But first you will have to define:
- whether the negative sign counts as a digit or not
- whether +0 and 0 and -0 have different number of digits

Be sure to test your routine against LONG_MIN and LONG_MAX
(which will not necessarily have the same number of digits.)
And don't just take absolute values: it is common for
(-LONG_MIN) to exceed LONG_MAX.
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes

Richard Heathfield
Guest
Posts: n/a

 11-29-2005
Luke Wu said:

> Is there a C function that returns the number of digits in an input
> int/long?
>
> example:
>
> numdigits(123) returns 3
> numdigits(1232132) returns 7

int numdigits(int n)
{
int count = 1; /* bound to be at least one digit! */
while(n != 0)
{
n /= 10;
++count;
}
return count;
}

This function ignores signs.

Note that the logarithm method is a little tighter in source terms, but
involves a call to log(), which can be expensive, computationally speaking.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Richard Heathfield
Guest
Posts: n/a

 11-29-2005
[attempting to supersede previous (broken) code]

Luke Wu said:

> Is there a C function that returns the number of digits in an input
> int/long?
>
> example:
>
> numdigits(123) returns 3
> numdigits(1232132) returns 7

int numdigits(int n)
{
int count = 0;
do
{
++count;
n /= 10;
}
while(n != 0);
return count;
}

This function ignores signs.

Note that the logarithm method is a little tighter in source terms, but
involves a call to log(), which can be expensive, computationally speaking.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Richard Bos
Guest
Posts: n/a

 11-29-2005
Chris McDonald <(E-Mail Removed)> wrote:

> "Luke Wu" <(E-Mail Removed)> writes:
>
> >Ohh my.... didn't think it would be a two liner.......thank you
> >#include<math.h>

>
> >int numdigits(int n)
> > return log10(n) + 1;

>
> Check its prototype - it's double log10(double x);

Yes. And with #include <math.h>, the ints and doubles will be
automatically converted back and forth. Since double is required to
support at least 10 digits, integers of the size the OP used will be
converted correctly, without any floating point error. There may still
be a rounding error _within_ log10(), but that can't be helped, only
worked around.

Richard

Chris McDonald
Guest
Posts: n/a

 11-29-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) (Richard Bos) writes:

>Chris McDonald <(E-Mail Removed)> wrote:

>> "Luke Wu" <(E-Mail Removed)> writes:
>>
>> >Ohh my.... didn't think it would be a two liner.......thank you
>> >#include<math.h>

>>
>> >int numdigits(int n)
>> > return log10(n) + 1;

>>
>> Check its prototype - it's double log10(double x);

>Yes. And with #include <math.h>, the ints and doubles will be
>automatically converted back and forth. Since double is required to
>support at least 10 digits, integers of the size the OP used will be
>converted correctly, without any floating point error. There may still
>be a rounding error _within_ log10(), but that can't be helped, only
>worked around.

Thanks; my mistake.
Perhaps my coding style tends to be too pedantic, as I would have employed
casts in both places.

--
Chris.

Keith Thompson
Guest
Posts: n/a

 11-29-2005
"Luke Wu" <(E-Mail Removed)> writes:
> Chris McDonald wrote:
>> "Luke Wu" <(E-Mail Removed)> writes:
>>
>> >Is there a C function that returns the number of digits in an input
>> >int/long?

>>
>> >example:

>>
>> >numdigits(123) returns 3
>> >numdigits(1232132) returns 7

>>
>>
>> Learn about lagarithms, then apply that knowledge to standard C.
>>

>
> Ohh my.... didn't think it would be a two liner.......thank you
> #include<math.h>
>
> int numdigits(int n)
> return log10(n) + 1;

I don't think a floating-point solution is best here. A loop using
integer arithmetic is likely to be faster and more accurate. For that
matter, a binary search on a lookup table holding powers of 10 is
likely to be even quicker.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post pembed2012 C Programming 1 02-27-2012 08:21 PM Mark Piffer C Programming 9 05-15-2009 07:54 AM randomtalk@gmail.com Python 11 05-02-2006 10:26 AM Protoman C++ 14 12-11-2005 07:11 PM FrisbeeŽ MCSE 37 09-26-2005 04:06 PM