Solving Error in C++ Digit Algorithm Program

  • Context: C/C++ 
  • Thread starter Thread starter chaoseverlasting
  • Start date Start date
  • Tags Tags
    Algorithm C++
Click For Summary

Discussion Overview

The discussion revolves around a C++ program intended to calculate the number of digits in an integer. Participants explore potential errors in the implementation, particularly concerning the use of logarithmic functions and alternative methods for counting digits.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant notes that the log function computes the natural logarithm, which may not be suitable for counting digits.
  • Another participant questions whether the original poster is working on homework, suggesting a reluctance to provide extensive help if so.
  • A participant proposes using the log10 function instead, questioning the differences between log(e) and log(10).
  • One suggestion involves using a loop to divide the integer by ten until it reaches zero, which is presented as a more efficient method than using logarithmic functions.
  • Another participant mentions using sprintf to count characters in a buffer as an alternative approach.
  • There is a repeated suggestion to consider the properties of integers and the behavior of logarithmic functions, including the Taylor series expansion.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to solve the problem, with no consensus on a single method. Some advocate for logarithmic methods while others prefer iterative division or character counting.

Contextual Notes

Participants discuss the limitations of the log function in this context, including its applicability to negative integers and the potential issues with different integer types. There is also mention of the need for accuracy and efficiency in the proposed solutions.

chaoseverlasting
Messages
1,051
Reaction score
3
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;
count<<log(i);
return 0;
}


The log function gives me a weird result each time. I don't know why though. I am using dev c++ compiler...
 
Technology news on Phys.org
The log function computes the natural logarithm.
 
man log10
 
Is this homework? I don't want to go much further with help if it is.
 
This isn't homework. I am trying to write a program which needs this bit. I can't seem to figure this out. So is it log(e) or log (10) ?
 
If that's 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?
 
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?
 
Personally I'd use sprintf & count the characters in the buffer...
 
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
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
Borek said:
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.

D H said:
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:.
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 40 ·
2
Replies
40
Views
4K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 39 ·
2
Replies
39
Views
5K
  • · Replies 13 ·
Replies
13
Views
2K
Replies
12
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K