Question

i want to do what the title says like this:

int number1;
cin>>number1;
num1len=log10(number1)+1;
cout<<"num of digits is "<<num1len<<"\n";

but when the number of digits is 11 and more the answer is always 7(6+1)

Does anyone knows why or what im i doing wrong?

Was it helpful?

Solution

What is 'wrong' is the maximum value which can be stored in a (signed) int :

#include <iostream>
#include <numeric>

int main()
{
    std::cout << std::numeric_limits<int>::max() << std::endl;
}

Gives me :

2147483647

OTHER TIPS

Floating-point data types, including double, store approximations. What you're finding by calling log10 is the number of places to the left of the decimal point, which is affected by at most one by the approximation process.

The question you asked, how to find the number of decimal digits in a number stored in binary floating-point, is meaningless. The number 7.1 has two decimal digits, however its approximate floating-point representation doesn't use decimal digits at all. To preserve the number of decimal digits, you'd need some decimal representation, not the C++ double data type.

Of course, all of this is applicable only to double, per the question title. Your code snippet doesn't actually use double.

You are running past the unsigned 32-bit boundary ... your number of 11 digits or more exceeds 0xFFFFFFFF, and so wraps around.

You need to use either unsigned long long or double for your number1 variable:

#include <iostream>
#include <cstdlib>
#include <cmath>

int
main ( int argc, char * argv[] )
{
  unsigned long long num; // or double, but note comments below
  std::cin >> num;
  std::cout << "Number of digits in " << num << " is " << ( (int) std::log10 ( num ) + 1 ) << std::endl;
  return 0;
}

Those large numbers will print in scientific notation by default when you send them to std::cout if you choose to use double as your data type, so you would want to throw some formatting in there. If you use an unsigned long long instead, they will print as they were entered, but you have to be sure that your platform supports unsigned long long.

EDIT: As mentioned by others, use of floating point values has other implications to consider, and is most likely not what you are ultimately trying to achieve. AFAIK, the integral type on a platform that yields the largest positive value is unsigned long long, so depending on the values you are looking to work with, see if that is available to you for use.

Others have pointed out that floating point numbers are approximations, so you can't really get an accurate count of digits in it.

But...you can get something approximate, by writing it out to a std::stringstream object, then converting it to a std::string, and getting the lenght of the said string. You'll of course have to deal with the fact that there may be non-digit characters in the string (like minus sign, decimal point, E for exponent etc). Also the number of digits you obtain in this manner would be dependent on formatting options you choose when writing to the stringstream object. But assuming that you know what formatting options you'd like to use, you can get the number of digits subject to these options.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top