find digit length or the number of numbers in number ?

S

Steven

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;
}
 
C

Chris McDonald

Steven said:
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 ?
 
P

pemo

Steven said:
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)
 
S

Steven

Steven said:
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.
 
F

Fred Kleinschmidt

pemo said:
Steven said:
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.
 
A

Arndt Jonasson

Chris McDonald said:
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.
 
S

serrand

pemo said:
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
 
A

Arndt Jonasson

Arndt Jonasson said:
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.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top