Problems with uint64_t in C++

Asked

Viewed 104 times

2

I have a problem with very large numbers. When I put the input 64 my program does not display the right answer, remembering that all smaller numbers are with normal output.

Exit from the program:

0 kg

Expected exit:

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";




}

1 answer

3


You’ll have to use a library BigInteger or create one of their own. Each has its advantages and disadvantages. I have chosen the Infint by being simple to pick up and use, but it does not have a function or exponentiation operator, which could eliminate this loop. 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";

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.