In article <>,
Luke Wu <> 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

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 Bos) writes:
>Chris McDonald <> wrote:
>> "Luke Wu" <> 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.

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) <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.

And how do you fix this when the next version of your compiler ships
(which use 333 bit longs)?

A solution using snprintf could work, but io functions are quite complex
so I'd not be surprised if the log10 was faster. Personally I'd go for
either a counting loop, or a binary lookup table, depending on how
critical speed, and time for implementation are.

As a general rule, you shold only use casts when
a) you actually need one; or
b) it makes the code less ambiguous

This could be argued as a (b) since it would show that you really did
intend to return an int, and would prevent maintenance droids from
changing it in a tidy-up frenzy. Personally I don't think it does,
since the function name/purpose is self-documenting. YMMV.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

In article <>,
Niklas Norrthon <> wrote:
>And how do you fix this when the next version of your compiler ships
>(which use 333 bit longs)?
>A solution using snprintf could work, but io functions are quite complex
>so I'd not be surprised if the log10 was faster. Personally I'd go for
>either a counting loop, or a binary lookup table, depending on how
>critical speed, and time for implementation are.

If one is assuming that the next generation compiler might have 333 bit
longs (and of course the DS9000 has 666 bit longs whenever it feels like
it ;-) ), then one would need to initialize the binary lookup table
at runtime. The code to do that without risking overflow (UB) is
probably not so long, but is likely a bit interesting.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson

Both log10 and this also fall flat on negative numbers. a negative
number divided by a positive number is permitted to never result in
zero; and of course the log of a negative number is non-real. At least
the sprintf solution returns something that some people _might_ consider
sensible even in the naivest implementation [counting the minus as a
digit]

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!