Problems with uint64_t in C ++

1

I'm having trouble with very large numbers. When I put the entrance 64 my program does not display the correct answer, remembering that all smaller numbers are with normal output.

Program exit:

0 kg

Expected output:

1537228672809129 kg

source code:

#include <iostream>    
using namespace std;



int main(){

    int a;
    uint64_t x=1,kg;
    cin>>a;
    for(int cont=0;cont<a;cont++){
        x*=2.0;
    }
    kg=x/12/1000;
    cout<<kg<<" kg\n";




}
    
asked by anonymous 30.05.2016 / 02:14

1 answer

2

You'll have to use a BigInteger library or create your own. Each has its advantages and disadvantages. I chose InfInt for being simple to pick up and use, but it does not have an exponentiation function or operator, which could eliminate this tie. But at least it works:

int a;
cin >> a;
InfInt x = 1, kg;
for (int cont = 0; cont < a; cont++) {
    x *= 2;
}
kg = x / 12000;
cout << kg << " kg\n";

See running on ideone and on CodingGround .

    
30.05.2016 / 02:56