0

I tried to express 10 power 10 but it didn't work, so I thought the problem is the range of the data type.

Finally I changed the type from int to __int64 but it isn't yet working.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int a, r, n;

    __int64  d;

    cin >> a; //  a=10
    cin >> r; //  r=10
    cin >> n; //  n=10

    d = a * pow (r, n - 1);

    cout << int(d);
}

My expected answer is 10000000000 but result of the operation is 1410065408

What's the problem?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Hamp
  • 39
  • 5
  • 11
    The problem is that you converted the answer to an `int` with `cout << int(d)`, and the answer doesn't fit in an `int`. – user3386109 Jun 23 '20 at 00:58
  • 2
    In `` there is `std::int64_t`, which is a portable way of getting a 64 bit signed integer if one exists, instead of `__int64`. – François Andrieux Jun 23 '20 at 01:00
  • 1
    Also, don't mix float and int. Use a for loop and multiply by ten ten times – Jeffrey Jun 23 '20 at 01:11
  • Woah, thanks, @francois, good to know – Jeffrey Jun 23 '20 at 01:36
  • Thank you every one . – Hamp Jun 23 '20 at 18:29
  • 1
    you can also write 1e10 – pradeexsu Nov 05 '20 at 08:18
  • 1
    @FrançoisAndrieux no, all overloads with integer exponent [has been removed since C++11](https://en.cppreference.com/w/cpp/numeric/math/pow). The only overload the accepts integral values also casts the value internally to floating-point types: *7) A set of overloads or a function template for all combinations of arguments of arithmetic type not covered by 1-3). **If any argument has integral type, it is cast to `double`. If any argument is `long double`, then the return type Promoted is also `long double`, otherwise the return type is always `double`.*** – phuclv Mar 13 '21 at 03:39

3 Answers3

1

You're trying to convert __int64 into int which can't hold values like 10000000000 in the cout statement.

Don't confuse between __int64 (holds 8-bytes data) and int (holds 4-bytes data). Simply remove the conversion int().

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
-1
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int a,n;

    long long int  d; //change the type to long long int


    cin >> a;
    cin >> n; //  n=10

    d =  pow (a, n );

    cout << d;

}

The problem was the range of your data type. By changing to "long long int" you'll get the expected answer 10000000000.

  • `long long int` is not likely to be larger than `__int64` and a 64 bit integer is definitely large enough already anyway. You've removed the cast from the `cout` which is why you get the expected result. – François Andrieux Jun 23 '20 at 12:25
  • Thank you sir I really appreciate – Hamp Jun 23 '20 at 18:31
  • [Why should I not #include ?](https://stackoverflow.com/q/31816095/995714), [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/995714) – phuclv Mar 08 '21 at 07:07
-3

Use powl(a,b) instead of pow(a,b) for large integers as in your case.

Abhi Gupta
  • 17
  • 6