473,386 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

find digit length or the number of numbers in number ?

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;
}
Feb 3 '06 #1
8 8438
Steven <St****@yahoo.com> 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.
Feb 3 '06 #2

"Steven" <St****@yahoo.com> wrote in message
news:36********************************@4ax.com...
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)
Feb 3 '06 #3
On Fri, 3 Feb 2006 13:16:50 -0000, "pemo" <us***********@gmail.com>
wrote:

"Steven" <St****@yahoo.com> wrote in message
news:36********************************@4ax.com...
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.
Feb 3 '06 #4
Steven <St****@yahoo.com> 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.
Feb 3 '06 #5

"pemo" <us***********@gmail.com> wrote in message
news:dr**********@news.ox.ac.uk...

"Steven" <St****@yahoo.com> wrote in message
news:36********************************@4ax.com...
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
Feb 3 '06 #6
pemo wrote:
"Steven" <St****@yahoo.com> wrote in message
news:36********************************@4ax.com...
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
Feb 3 '06 #7

Chris McDonald <ch***@csse.uwa.edu.au> writes:
Steven <St****@yahoo.com> 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.
Feb 3 '06 #8

Arndt Jonasson <do********@invalid.net> writes:
Chris McDonald <ch***@csse.uwa.edu.au> writes:
Steven <St****@yahoo.com> 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.
Feb 3 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kueishiong Tu | last post by:
I have a network stream which I got from HttpWebResponse and does not support seeking, How do I find the length of the network stream? HttpWebResponse* response = dynamic_cast<HttpWebResponse*>...
11
by: mailar | last post by:
Hi, I am using DB2 version 8.2 on Windows XP platform. I have a table 'EMP' with one field named 'NAME' of type CHAR , size 80. Also , I have a record with NAME='john' Now, when I try to...
2
by: Kueishiong Tu | last post by:
I have a network stream which I got from HttpWebResponse and does not support seeking, How do I find the length of the network stream? HttpWebResponse* response = dynamic_cast<HttpWebResponse*>...
9
by: Stan Cook | last post by:
Can anyone tell me how to get the length of a number. I know len(string) will get the length of a string, but it doesn't like len(int). I seem to remember something like %s string. I tried to...
2
by: raghunadhs | last post by:
hi every body! can u say "is there any built in method to find the length of an array", so that it should say how many elements are int that array. i used UBOUND method, but it gives the size...
4
by: sunil | last post by:
How do I find the length of page in target iframe? I am looking for the code or give me idea to find the length
9
by: goelvivek | last post by:
write a program to find the length of the string without using control structures and without using string.h header files???
2
by: karimufeed | last post by:
I am working on an access project for pension calculation. I want to find total length of service between two dates. i.e. if the Date of entry into service is 15/3/1980 and the date of retirement...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.