C function for returning number of digits?

D

David Resnick

Jordan said:
Richard said:
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.

*g* Never knock the simple solution. You're quite right, of course

How about:
int length;
char digits[100]; /* should be big enough even for 128 bit longs */
sprintf(digits, "%d", n);
length = strlen(digits) - (n<0 ? 1 : 0);

For c99:

length = snprintf(0,0,"%d",n)-1;

Why the -1?

[#3] The snprintf function returns the number of characters
that would have been written had n been sufficiently large,
not counting the terminating null character, or a negative
value if an encoding error occurred. Thus, the null-
terminated output has been completely written if and only if
the returned value is nonnegative and less than n.

Return count doesn't include the terminating null. Sanity check on
this
with gcc:

temp(1186)$ cat foo.c
#include <stdio.h>

int main(void)
{
int foo = 10;
size_t len = snprintf(NULL, 0, "%d", foo);
printf("%d has len %lu\n", foo, (unsigned long) len);

return 0;
}
temp(1187)$ gcc -Wall -pedantic foo.c -o foo
temp(1188)$ foo
10 has len 2

-David
 
W

Walter Roberson

Ingo Menger said:
Niklas Norrthon schrieb:
I don't. I write the length of the char array as constant expression
involving sizeof (long) in the first place. For example
char digits[32 + 4 * sizeof (long)]
That should do it.

pow(2,332)-1 is 100 digits. Your expression 32 + 4 * sizeof (long)
will not work unless sizeof(long) is at least 17 and CHAR_BIT is
at least 20.

You should be allocating about
CHAR_BITS * sizeof long / (log(10)/log(2))
digits... though remember to allow for rounding and the sign.

Getting sufficient precision on (log(10)/log(2)) at compile
time could be tricky, so possibly the easiest would be to round
that down and overeastimate the number of digits, resulting in

CHAR_BIT * sizeof long / 3
 
M

Malcolm

Jordan Abel said:
At least
the sprintf solution returns something that some people _might_ consider
sensible [ number of digits in a number] even in the naivest
implementation [counting
the minus as a digit]
That is something which is hard to answer. Should a negative number assert
fail? Exhibit undefined behaviour? Return a negative number of digits?
 
K

Keith Thompson

Malcolm said:
Jordan Abel said:
At least
the sprintf solution returns something that some people _might_ consider
sensible [ number of digits in a number] even in the naivest
implementation [counting
the minus as a digit]
That is something which is hard to answer. Should a negative number assert
fail? Exhibit undefined behaviour? Return a negative number of digits?

It's easy to answer: just pick one and make it part of the problem
definition.

If the problem definition doesn't cover negative numbers, then either
it's an inadequate definition or you can just assume undefined
behavior.
 
S

Skarmander

Malcolm said:
Jordan Abel said:
At least
the sprintf solution returns something that some people _might_ consider
sensible [ number of digits in a number] even in the naivest
implementation [counting
the minus as a digit]
That is something which is hard to answer. Should a negative number assert
fail? Exhibit undefined behaviour? Return a negative number of digits?
Eh, no. The original function was declared as taking an int. Hence, it is
capable of processing negative numbers unless explicitly documented to do
otherwise. Hence, it should return the number of digits in the number. The
number of digits in a negative number is equal to the number of digits in
its absolute value. Confused people may have wanted to ask for the number of
characters instead, in which case the sign counts as a "digit", and the
function is poorly named.

What is hard to answer is why people keep insisting on guessing the intended
behavior of a function by its name instead of using a specification.

S.
 
W

Walter Roberson

Eh, no. The original function was declared as taking an int. Hence, it is
capable of processing negative numbers unless explicitly documented to do
otherwise. Hence, it should return the number of digits in the number. The
number of digits in a negative number is equal to the number of digits in
its absolute value. Confused people may have wanted to ask for the number of
characters instead, in which case the sign counts as a "digit", and the
function is poorly named.
What is hard to answer is why people keep insisting on guessing the intended
behavior of a function by its name instead of using a specification.

If you are going to use that kind of semantic logic, then I would
point out that the number of digits in 01234 is not the same as the
number of digits in 1234.

"digits" are not an inherent property of a number: they are a property
of the representation of the number under some given representation
schema. Whether the indication of negative values is to count
as a "digit" or not depends upon the representation schema.

For example, one could chose a representation schema involving octal
digits and with the property that a negative value is indicated by
a leading "9". How many "digits" are there in 9141 in such a
schema ? How many digits in the hex number B27F ?
 
S

Skarmander

Walter said:
If you are going to use that kind of semantic logic, then I would
point out that the number of digits in 01234 is not the same as the
number of digits in 1234.

"digits" are not an inherent property of a number: they are a property
of the representation of the number under some given representation
schema. Whether the indication of negative values is to count
as a "digit" or not depends upon the representation schema.
Oh yes, very well. The number of digits in the smallest decimal
representation of a number. Don't worry, I wouldn't have forgotten that had
I written the specification of the function. (Well, probably the "smallest"
part, because it's obvious that if leading zeros are allowed, anything goes.)
For example, one could chose a representation schema involving octal
digits and with the property that a negative value is indicated by
a leading "9". How many "digits" are there in 9141 in such a
schema ? How many digits in the hex number B27F ?

Three and four, respectively.

I won't deny that a certain amount of fudging and unclarity is always
involved, unless you want to break everything down to, say, the Peano
axioms. Some things are more reasonable than others when left unspecified,
however.

S.
 
M

Michael Mair

Richard said:
[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.

Yep. Quite funny for round-down integer division as allowed by
C89... ;-)

Cheers
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top