a^b digit calculation with big numbers

Hello, I was trying to calculate number of digits when you a^b, found out this way with logorithm and it works with small numbers, however if i pick numbers like 123456798 and 987654321 the program shows answer: 7.99162e+09, and what it should show is 998952458. Let's say that a, b (1 ≤ a, b ≤ 10^16). Can anyone help me ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include <math.h>
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
double a, b;
cin >> a >> b;
long long digit;
digit = b * log10(a);

cout <<ceil(digit)<<endl;
        
return 0;
}
Because the number is so long the number gets truncated to what I guess is a standard form for big numbers, maybe you are looking for this function in the included <iomanip> header? http://www.cplusplus.com/reference/iomanip/setprecision/
including both math.h and cmath can cause problems in some enviroments. They are the same functions, just cmath has the c++ additions, so use that one.

I was thinking digit should be a double, but that did not change the answer.


are you sure about your expected answer? (I have no idea, just asking if you are absolutely sure).

I wonder if it is just roundoff error? Did you try a 10b or something? My compiler thinks long double is 12 bytes (used to be 10, must have had a hardware change while I was out of th game) ... you could see if something like that gives the right answer.

Last edited on
pokolas wrote:
what it should show is 998952458

How did you come to that conclusion @pokolas?
log10(123456789) == 8.091514977

8.091514977 * 987654321 = 7991619730.4702...
Yes, that's the conclusion I came to as well, @tpb.
Topic archived. No new replies allowed.