# find digit length or the number of numbers in number ?

Discussion in 'C Programming' started by Steven, Feb 3, 2006.

1. ### StevenGuest

Hi,

I am trying to find out how many digits there are in a given number.
The macro listed below works fine when applied to an INT, however when
doing Doubles with numbers > then a billion ?? It stops working.

Anyone any idea's ?

Thankx !

Steven.

#include <stdio.h>
#include <math.h>

#define DIGLEN(x) (x ? (int)(log10((double)(abs(x)))) + 1 : 1)

int main(int argc, char *argv[]) {
int i = 123;
double j = 807319385.29;
double k = 12258983401.75;

printf("%3d: %d\n", DIGLEN(i), i);
printf("%3d: %.2f\n", DIGLEN(j), j);
printf("%3d: %.2f\n", DIGLEN(k), k);

return 0;
}

Steven, Feb 3, 2006

2. ### Chris McDonaldGuest

Steven <> writes:

>I am trying to find out how many digits there are in a given number.
>The macro listed below works fine when applied to an INT, however when
>doing Doubles with numbers > then a billion ?? It stops working.

Format the value with sprintf(), then call strlen().
Then again, how many digits do you want there to be in the double 1.0/3.0 ?

--
Chris.

Chris McDonald, Feb 3, 2006

3. ### pemoGuest

"Steven" <> wrote in message
news:...
> Hi,
>
> I am trying to find out how many digits there are in a given number.
> The macro listed below works fine when applied to an INT, however when
> doing Doubles with numbers > then a billion ?? It stops working.
>
> Anyone any idea's ?
>
> Thankx !
>
> Steven.
>
>
> #include <stdio.h>
> #include <math.h>
>
> #define DIGLEN(x) (x ? (int)(log10((double)(abs(x)))) + 1 : 1)
>
> int main(int argc, char *argv[]) {
> int i = 123;
> double j = 807319385.29;
> double k = 12258983401.75;
>
> printf("%3d: %d\n", DIGLEN(i), i);
> printf("%3d: %.2f\n", DIGLEN(j), j);
> printf("%3d: %.2f\n", DIGLEN(k), k);
>
> return 0;
> }

Do you mean 'significant' digits, e.g., that 3.142 resolve to be '1'?

If yes, then you probably want:

#define DIGLEN(x) (x ? (int)(log10((double)(fabs(x)))) + 1 : 1)

e.g., fabs instead of abs.

btw, according to the latest C std, abs() is prototyped in stdlib.h, not
math.h (fabs() is in there though)

pemo, Feb 3, 2006
4. ### StevenGuest

On Fri, 3 Feb 2006 13:16:50 -0000, "pemo" <>
wrote:

>
> "Steven" <> wrote in message
> news:...
> > Hi,
> >
> > I am trying to find out how many digits there are in a given number.
> > The macro listed below works fine when applied to an INT, however when
> > doing Doubles with numbers > then a billion ?? It stops working.
> >
> > Anyone any idea's ?
> >
> > Thankx !
> >
> > Steven.
> >
> >
> > #include <stdio.h>
> > #include <math.h>
> >
> > #define DIGLEN(x) (x ? (int)(log10((double)(abs(x)))) + 1 : 1)
> >
> > int main(int argc, char *argv[]) {
> > int i = 123;
> > double j = 807319385.29;
> > double k = 12258983401.75;
> >
> > printf("%3d: %d\n", DIGLEN(i), i);
> > printf("%3d: %.2f\n", DIGLEN(j), j);
> > printf("%3d: %.2f\n", DIGLEN(k), k);
> >
> > return 0;
> > }

>
> Do you mean 'significant' digits, e.g., that 3.142 resolve to be '1'?
>
> If yes, then you probably want:
>
> #define DIGLEN(x) (x ? (int)(log10((double)(fabs(x)))) + 1 : 1)
>
> e.g., fabs instead of abs.
>
> btw, according to the latest C std, abs() is prototyped in stdlib.h, not
> math.h (fabs() is in there though)

Oww.. Great thankx !

I included math.h for log10(..)

Thanks again for the answer and the stdlib.h reference.

Steven.

Steven, Feb 3, 2006
5. ### Chris McDonaldGuest

Steven <> writes:

>Oww.. Great thankx !

>I included math.h for log10(..)

>Thanks again for the answer and the stdlib.h reference.

Whoa! Don't you consider that the computational requirements of calling
log10() will be a little excessive (as you don't need all of its accuracy)?

--
Chris.

Chris McDonald, Feb 3, 2006
6. ### Fred KleinschmidtGuest

"pemo" <> wrote in message
news:drvl42\$3om\$...
>
> "Steven" <> wrote in message
> news:...
>> Hi,
>>
>> I am trying to find out how many digits there are in a given number.
>> The macro listed below works fine when applied to an INT, however when
>> doing Doubles with numbers > then a billion ?? It stops working.
>>
>> Anyone any idea's ?
>>
>> Thankx !
>>
>> Steven.
>>
>>
>> #include <stdio.h>
>> #include <math.h>
>>
>> #define DIGLEN(x) (x ? (int)(log10((double)(abs(x)))) + 1 : 1)
>>
>> int main(int argc, char *argv[]) {
>> int i = 123;
>> double j = 807319385.29;
>> double k = 12258983401.75;
>>
>> printf("%3d: %d\n", DIGLEN(i), i);
>> printf("%3d: %.2f\n", DIGLEN(j), j);
>> printf("%3d: %.2f\n", DIGLEN(k), k);
>>
>> return 0;
>> }

>
> Do you mean 'significant' digits, e.g., that 3.142 resolve to be '1'?
>

No, from a physics/mathematics standpoint, 3.142 has 4 significant digits
(that is, it is pi to four significant digits).
3.0000 has 5 significant digits.
3 has one significant digit.

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project

Fred Kleinschmidt, Feb 3, 2006
7. ### Arndt JonassonGuest

Chris McDonald <> writes:
> Steven <> writes:
>
> >Oww.. Great thankx !

>
> >I included math.h for log10(..)

>
> >Thanks again for the answer and the stdlib.h reference.

>
>
> Whoa! Don't you consider that the computational requirements of calling
> log10() will be a little excessive (as you don't need all of its accuracy)?

Besides, it may very well give the wrong result for powers of ten.
log10(10000) might be 5.00000000, or it might be 4.999999999. In the
latter case, the macro gives the wrong answer.

Arndt Jonasson, Feb 3, 2006
8. ### serrandGuest

pemo wrote:
> "Steven" <> wrote in message
> news:...
>
>>Hi,
>>
>>I am trying to find out how many digits there are in a given number.
>>The macro listed below works fine when applied to an INT, however when
>>doing Doubles with numbers > then a billion ?? It stops working.
>>
>>Anyone any idea's ?
>>
>>Thankx !
>>
>>Steven.
>>
>>
>>#include <stdio.h>
>>#include <math.h>
>>
>>#define DIGLEN(x) (x ? (int)(log10((double)(abs(x)))) + 1 : 1)
>>
>>int main(int argc, char *argv[]) {
>>int i = 123;
>>double j = 807319385.29;
>>double k = 12258983401.75;
>>
>>printf("%3d: %d\n", DIGLEN(i), i);
>>printf("%3d: %.2f\n", DIGLEN(j), j);
>>printf("%3d: %.2f\n", DIGLEN(k), k);
>>
>>return 0;
>>}

>
>
> Do you mean 'significant' digits, e.g., that 3.142 resolve to be '1'?
>
> If yes, then you probably want:
>
> #define DIGLEN(x) (x ? (int)(log10((double)(fabs(x)))) + 1 : 1)
>
> e.g., fabs instead of abs.
>
> btw, according to the latest C std, abs() is prototyped in stdlib.h, not
> math.h (fabs() is in there though)
>
>

You may have some problems with rounding...
.... and the number of digits is closely linked with a choosen (?) representation

May be you could write something like
#define DIGLEN(x, y) (x ? (int)(log10 (fabs(x)+0.5e-##y)) +1 : 1)
in order to have the number of digits for printing with

printf("%3d: %.3f\n", DIGLEN(p, 3 p);
printf("%3d: %.5f\n", DIGLEN(p, 5 p); ...

Xavier

serrand, Feb 3, 2006
9. ### Arndt JonassonGuest

Arndt Jonasson <> writes:
> Chris McDonald <> writes:
> > Steven <> writes:
> >
> > >Oww.. Great thankx !

> >
> > >I included math.h for log10(..)

> >
> > >Thanks again for the answer and the stdlib.h reference.

> >
> >
> > Whoa! Don't you consider that the computational requirements of calling
> > log10() will be a little excessive (as you don't need all of its accuracy)?

>
> Besides, it may very well give the wrong result for powers of ten.
> log10(10000) might be 5.00000000, or it might be 4.999999999. In the
> latter case, the macro gives the wrong answer.

I'll follow up my own article to say that of course log10(10000) should
be close to 4, not 5, and that I'm not sure of this anyway. Maybe log10
does guarantee the correct result - the man page I'm looking at doesn't
say so explicitly.

Arndt Jonasson, Feb 3, 2006

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.