C++ Digit algorithm

  • #1

Main Question or Discussion Point

I wrote a program to find the number of digits of an integer, but I always get the wrong result. Could someone point out the error? Here's the source code:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
int i=53443;
cout<<log(i);
return 0;
}


The log function gives me a weird result each time. I dont know why though. Im using dev c++ compiler...
 

Answers and Replies

  • #2
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
682
The log function computes the natural logarithm.
 
  • #3
Borek
Mentor
28,328
2,717
man log10
 
  • #4
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
682
Is this homework? I don't want to go much further with help if it is.
 
  • #5
This isnt homework. Im trying to write a program which needs this bit. I cant seem to figure this out. So is it log(e) or log (10) ?
 
  • #6
If thats it (the natural log- base 10 difference), I can figure the rest out. Is there perhaps some property of integers and the way this function works (the taylor series of log(1+x) maybe?) that's causing this?
 
  • #7
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
682
RTFM, or as Borek said, man log10 (and man log won't hurt). If you are not on a UNIX machine, there is always the internet. What does log() compute?

BTW, neither log() nor log10() is really what you want. Will this approach work on negative integers or on 64 bit (long long) integers?
 
  • #8
156
0
Personally I'd use sprintf & count the characters in the buffer....
 
  • #9
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
682
Since this is not homework, I will give my solution. Keep dividing by ten (the integer, not the double) until the result becomes zero. This works for all sizes of integers, negative integers, is much faster and more accurate than using log10(), and is much, much faster than sprintf.
Code:
int ndigits = 0;
for (same_int_type_as_i rem = i; rem != 0; rem /= 10) {
   ++ndigits;
}
 
  • #10
Borek
Mentor
28,328
2,717
When I went to bed yesterday I thought of strlen(itoa(abs(i))) but I am not addicted to the web enough to post such things immediately :wink: This is similar to sprintf, just much easier to code.
 
  • #11
When I went to bed yesterday I thought of strlen(itoa(abs(i))) but I am not addicted to the web enough to post such things immediately :wink: This is similar to sprintf, just much easier to code.
Since this is not homework, I will give my solution. Keep dividing by ten (the integer, not the double) until the result becomes zero. This works for all sizes of integers, negative integers, is much faster and more accurate than using log10(), and is much, much faster than sprintf.
Code:
int ndigits = 0;
for (same_int_type_as_i rem = i; rem != 0; rem /= 10) {
   ++ndigits;
}
Thank you. :cool: That helps :cool:.
 

Related Threads for: C++ Digit algorithm

  • Last Post
Replies
9
Views
3K
  • Last Post
Replies
7
Views
32K
Replies
3
Views
2K
  • Last Post
Replies
22
Views
10K
  • Last Post
Replies
16
Views
6K
Replies
3
Views
1K
  • Last Post
Replies
7
Views
2K
Replies
4
Views
2K
Top