Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
May 11th, 2007, 03:33 AM
 chaostheory
Registered User

Join Date: May 2007
Posts: 10
Time spent in forums: 2 h 23 m 45 sec
Reputation Power: 0
Finding the number of digits

Im trying to write a function in which you pass an integer as an argument and you get the number of digits that the integer contains back. Here is what I have, but the code is flawed:

int getsize(int x)
{
int n=1;

for(;
{
if(x%pow(10,n)==0)
return n;
n++;
}

}

I havent tested it out, but the most obvious flaw is that if the integer is a multiple of 10, then x%10 will give 0 which will terminate the loop prematurely.

Can someone point out which way I should head to get this function working? I can do the code on my own.

#2
May 11th, 2007, 04:14 AM
 requinix
Still alive

Join Date: Mar 2007
Location: Washington, USA
Posts: 12,869
Time spent in forums: 5 Months 1 Week 5 Days 6 h 19 m 22 sec
Reputation Power: 8977
...

Two paths here:
1) You discover that floor(log10(abs(x)))+1 will give you the number of digits in x, or

2) Make a loop that until integer x==0, you divide x by 10 and increment a counter
IOI-RLZ agrees: log is indeed good...

#3
May 11th, 2007, 04:28 AM
 WebDunce
Contributing User

Join Date: Nov 2005
Location: Northwest Florida
Posts: 208
Time spent in forums: 3 Days 22 h 8 m 28 sec
Reputation Power: 10
Quote:
 Originally Posted by requinix ... 2) Make a loop that until integer x==0, you divide x by 10 and increment a counter

i think it should be loop until x < 1, right?

(nevermind, both conditions work...as long as x is of type integer)

Last edited by WebDunce : May 11th, 2007 at 04:35 AM.

#4
May 11th, 2007, 05:25 AM
 chaostheory
Registered User

Join Date: May 2007
Posts: 10
Time spent in forums: 2 h 23 m 45 sec
Reputation Power: 0
Quote:
 Originally Posted by requinix ... Two paths here: 1) You discover that floor(log10(abs(x)))+1 will give you the number of digits in x, or 2) Make a loop that until integer x==0, you divide x by 10 and increment a counter

Whoa! Thank you. The log thing is cool. Thanks man.

#5
May 11th, 2007, 04:05 PM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406
Time spent in forums: 2 Months 9 h 47 m 28 sec
Reputation Power: 4080
The log10() thing won't work for negative numbers or 0 though. You could get around this by using:
(a) Check for 0 and return 1
(b) Check for - sign and then convert the number to positive and find its length and add 1 to it (for the - sign)

Another way would be to use the sprintf() function, which returns the number of characters that were used in the string:
Code:
```char buf[30];
int i = 1234;
int len = sprintf(buf, "%d", i);```
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

#6
May 12th, 2007, 03:04 AM
 clifford
Contributing User

Join Date: Aug 2003
Location: UK
Posts: 4,824
Time spent in forums: 1 Month 2 Days 21 h 1 m
Reputation Power: 1800
Quote:
 Originally Posted by Scorpions4ever (b) Check for - sign and then convert the number to positive and find its length and add 1 to it (for the - sign)

Or just use abs(x) as the argument to log10() as requinix did in his original suggestion. Your solution (both of them) counts the sign as a digit for negative numbers. It is not a digit, and counting it as such may not be the intention, it is certainly not what was asked.

If performance were an issue sprintf() is probably far slower.

Clifford

Clifford

#7
May 12th, 2007, 03:55 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 3,905
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 9 m 41 sec
Reputation Power: 1774
Logarithms are not exactly cheap either.

Code:
```#include <stdio.h>

int intlen ( int a ) {
int len = 0;
if ( a < 0 ) a = -a;
while ( a >= 10000 ) { len += 4; a /= 10000; }
while ( a >= 100   ) { len += 2; a /= 100; }
while ( a >= 10    ) { len ++;   a /= 10; }
return len + 1;
}

int main ( ) {
printf("%d\n", intlen(0) );
printf("%d\n", intlen(9) );
printf("%d\n", intlen(42) );
printf("%d\n", intlen(123456789) );
return 0;
}

\$ ./a.exe
1
1
2
9```
clifford agrees: Smart
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

#8
May 12th, 2007, 06:34 AM
 clifford
Contributing User

Join Date: Aug 2003
Location: UK
Posts: 4,824
Time spent in forums: 1 Month 2 Days 21 h 1 m
Reputation Power: 1800
Quote:
 Originally Posted by salem Logarithms are not exactly cheap either.
Nice solution. I benchmarked them all, and got some interesting results.

I used the following code:
Code:
```#include <limits.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>

int intlen1 ( int a )
{
int len = 0;
if ( a < 0 ) a = -a;
while ( a >= 10000 ) { len += 4; a /= 10000; }
while ( a >= 100   ) { len += 2; a /= 100; }
while ( a >= 10    ) { len ++;   a /= 10; }
return len + 1;
}

int intlen2( int a )
{
if( a == 0 )
{
return 1 ;
}
else
{
if ( a < 0 ) a = -a;
return (int)log10((double)a) + 1 ;
}
}

int intlen3( int a )
{
char buf[30] ;
int len = sprintf(buf, "%d", a);
if( a < 0 )
{
len-- ;
}

return len ;
}

#define ITERATIONS 10000000
int main()
{
int i ;
static int test_data[ITERATIONS] ;
clock_t end ;
clock_t start ;
volatile int checksum ;

for( i = 0; i < ITERATIONS; i++ )
{
test_data[i] = rand() << 17 | (rand() & 0xffff8000) << 1 | (rand() & 0x1) ;
}

checksum = 0 ;
start = clock() ;
for( i = 0; i < ITERATIONS; i++ )
{
for( i = 0; i < ITERATIONS; i++ )
{
checksum += intlen1( i ) ;
}
}
end = clock() ;
printf( "Integer algorithm:\t%d, %d\n", end - start, checksum ) ;

checksum = 0 ;
start = clock() ;
for( i = 0; i < ITERATIONS; i++ )
{
checksum += intlen2( i ) ;
}
end = clock() ;
printf( "Log10 algorithm:\t%d, %d\n", end - start, checksum ) ;

checksum = 0 ;
start = clock() ;
for( i = 0; i < ITERATIONS; i++ )
{
checksum += intlen3( i ) ;
}
end = clock() ;
printf( "String algorithm:\t%d, %d\n", end - start, checksum ) ;
}```

And MS VC++ 8, native Win32 code.

Debug build
----------------
Integer algorithm: 1562, 68888890
Log10 algorithm: 1844, 68888890
String algorithm: 8937, 68888890

Release build
----------------
Integer algorithm: 156, 68888890
Log10 algorithm: 719, 68888890
String algorithm: 4640, 68888890

The string algorithm does not benefit much from optimisation. Salem's solution is obviously superior.

Note I changed the floor() call to a simple cast to int in my version. This has a significant beneficial effect on execution time. Note also the cast to double, this is to disambiguate from the single precision overload in C++ compilation. If you cast to float as I originally did, you get the wrong answer for 7 digit values close to 10,000,000 (this is what the checksum was there for - to make sure they produced the same result).

It is interesting because I have in the past and would have in this case, suggested the log10 method. It remains easy to implement (and remember) and is reasonably fast on a system with an FPU at least. Since I generally do embedded systems development and have no FPU, I'll have to remember Salem's solution!)

Clifford

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Finding the number of digits