Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 10th, 2007, 10:16 AM   #1
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
counting digits

Hey,

im writing some code for padding files.

Im wondering how i can count the number of digits in an integer...

i.e.

int a = 232;
int b = 9343;
int c = 23;
int d = 1;

a would have 3 digits, b would have 4 digits, c would have 2 and d would have 1 digit.

i've looked all over - all i can find is bitwise stuff - thats not what i want - the number of set bits is not always the same as the number of digits in the integer...

hope someone can help!

thanks!

rwm is offline   Reply With Quote
Old Apr 10th, 2007, 10:42 AM   #2
Eoin
Hobbyist Programmer
 
Eoin's Avatar
 
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3 Eoin is on a distinguished road
Well you have one obvious option which would be convert to a string and count the characters (possibly accounting for leading zeros depending on your task).

Another option might be to test the int against being less than 10, 100, 1000, etc. The max size for a int will mean you won't have to perform countless comparisons.

Finally you could look into log's, particularly log10 of the int.
__________________
Visit my website BinaryNotions.
Eoin is offline   Reply With Quote
Old Apr 10th, 2007, 10:44 AM   #3
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
Hey Eoin,

Thanks for the quick reply!

Can you explain the logs a bit more please?

Thanks!
rwm is offline   Reply With Quote
Old Apr 10th, 2007, 10:46 AM   #4
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2 pegasus001 is on a distinguished road
You can divide it by ten until it reaches 0. Like :
213 / 10 = 21
21 / 10 = 2
2 / 10 = 0

So you have 3 numbers.
__________________
You never test the depth of a river with both feet.
The believer is happy. The doubter is wise.
Free speech carries with it some freedom to listen.
The next generation will always surpass the previous one. It`s one of the never ending cycles of life.
pegasus001 is offline   Reply With Quote
Old Apr 10th, 2007, 10:53 AM   #5
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
hey sweet!

ok so obviously using log10 - i would have to use the ceiling function?

for example, say our integer is 235, log10 returns 2.37 - and there are actually 3 numbers in the integer, so use celing(2.37) to get 3...

that right?
rwm is offline   Reply With Quote
Old Apr 10th, 2007, 10:57 AM   #6
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
ok so this works:

//number of digits
inline int num_digits(int num) {
	return ceil(log10(num));
}

but lots of warnings! :/

thanks guys!
rwm is offline   Reply With Quote
Old Apr 10th, 2007, 11:01 AM   #7
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2 pegasus001 is on a distinguished road
I tried in the windows calculator log555 and the result is 2.744etc. So i guess this was not what eoin ment.
__________________
You never test the depth of a river with both feet.
The believer is happy. The doubter is wise.
Free speech carries with it some freedom to listen.
The next generation will always surpass the previous one. It`s one of the never ending cycles of life.
pegasus001 is offline   Reply With Quote
Old Apr 10th, 2007, 11:03 AM   #8
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,453
Rep Power: 7 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
I have a problem with overkilling the simple things I guess... So I won't kill all of your fun, I'll leave the leading zero issue up to you to solve (if there is even a potential for it to be a problem)

#include <iostream>
#include <sstream>

int CountDigits (int source);
std::string itos (int num);

int main (void)
{
    int x = 0;
    int y = 12345;
    
    x = CountDigits(y);
    
    std::cout << "There are " << x << " digits in " << y << std::endl;
    std::cin >> y;
    
    return 0;
}

int CountDigits (int source)
{
    int count = 0;
    std::string z = itos(source);
    return z.length();
}

std::string itos (int num)
{
   std::ostringstream myStream;
   myStream << num << std::flush;
   return (myStream.str());
}
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Apr 10th, 2007, 11:09 AM   #9
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2 pegasus001 is on a distinguished road
Sorry if this question seems stupid but why do you have to write this :
std::cin >> y;
__________________
You never test the depth of a river with both feet.
The believer is happy. The doubter is wise.
Free speech carries with it some freedom to listen.
The next generation will always surpass the previous one. It`s one of the never ending cycles of life.
pegasus001 is offline   Reply With Quote
Old Apr 10th, 2007, 11:14 AM   #10
Eoin
Hobbyist Programmer
 
Eoin's Avatar
 
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3 Eoin is on a distinguished road
Hi pegasus001, that result is perfectly ok. log555 = 2.744 implies 10^2.744 = 555. It's just a convenient fact that after rounding up the answer gives you the number of digits.

So for example log_2(x) {here I'm saying log to the base b of x) gives you the number of binary digits which would be necessary to hold x.

rvm, the warnings are because log10 take and returns a float or double. So you should specify the cast manually and ideally account for when there would be an overflow. Boost.numeric_cast can do this nicely.

P.S. a solution based on pegasus' suggestion of dividing by ten would probably be the most optimal as the log functions are very expensive.
__________________
Visit my website BinaryNotions.
Eoin is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using maths to split an integer into it's seperate digits? bivhitscar C 12 Feb 22nd, 2006 11:31 PM
Counting the Days.... Ghost C# 2 Dec 8th, 2005 11:33 PM
Counting occurrence of numbers in C stabule C 6 Nov 15th, 2005 10:49 AM
Showing more digits BebopFusion C 4 Jul 15th, 2005 3:57 PM
Help with a counting problem. AngelX Visual Basic 4 Apr 2nd, 2005 6:04 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:11 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC