Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > find digit length or the number of numbers in number ?

Reply
Thread Tools

find digit length or the number of numbers in number ?

 
 
Steven
Guest
Posts: n/a
 
      02-03-2006
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;
}
 
Reply With Quote
 
 
 
 
Chris McDonald
Guest
Posts: n/a
 
      02-03-2006
Steven <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
pemo
Guest
Posts: n/a
 
      02-03-2006

"Steven" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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)


 
Reply With Quote
 
Steven
Guest
Posts: n/a
 
      02-03-2006
On Fri, 3 Feb 2006 13:16:50 -0000, "pemo" <(E-Mail Removed)>
wrote:

>
> "Steven" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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.
 
Reply With Quote
 
Chris McDonald
Guest
Posts: n/a
 
      02-03-2006
Steven <(E-Mail Removed)> 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.
 
Reply With Quote
 
Fred Kleinschmidt
Guest
Posts: n/a
 
      02-03-2006

"pemo" <(E-Mail Removed)> wrote in message
news:drvl42$3om$(E-Mail Removed)...
>
> "Steven" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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


 
Reply With Quote
 
Arndt Jonasson
Guest
Posts: n/a
 
      02-03-2006

Chris McDonald <(E-Mail Removed)> writes:
> Steven <(E-Mail Removed)> 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.
 
Reply With Quote
 
serrand
Guest
Posts: n/a
 
      02-03-2006
pemo wrote:
> "Steven" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>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
 
Reply With Quote
 
Arndt Jonasson
Guest
Posts: n/a
 
      02-03-2006

Arndt Jonasson <(E-Mail Removed)> writes:
> Chris McDonald <(E-Mail Removed)> writes:
> > Steven <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(8-bit binary to two digit bcd) or (8-bit binary to two digit seven segment) Fangs VHDL 3 10-26-2008 06:41 AM
Add 4 20 digit numbers in C thehobbit C Programming 15 11-09-2007 01:06 AM
Displaying 2 digit numbers in vb.net? zullok2b ASP .Net 0 10-30-2007 05:35 PM
Length of digit after decimal point Stefantastisk ASP .Net 5 11-22-2006 02:57 PM
[Newbie]: How to find a specified digit in a number? Mathias Python 3 11-10-2004 06:20 PM



Advertisments