pow()

So im into my first 9hours of programming day and a problem occured.
While using the pow() function i had to search to make it work actually and now it wont calculate accuratly above 9.

here is the piece of code.

if ( prog == 2 )
{
cout<<"geef het cijfer in dat je wil omrekenen naar machten";
cout<<"\n";
cin >> a;
cout<<"geef nu het aantal machten aan";
cout<<"\n";
cin >> b;
cout << a; cout <<" tot de macht "; cout << b; cout<<" is";
cout<<"\n";
cout<<static_cast<int>(pow(static_cast<double>(a),b));
cout<<"\n";
cout<<"\n";
}

i have no idea how to proceed

im using Dev-c++
Above 9 what? With an input above 9? Because it does 10^3 fine. Or an output above 9? Which, again, seems not to be the case.

Give us an example input, and the output you see, and tell us why you think that's the wrong output.

im using Dev-c++

If you're using a version before 4.9.9.3, please don't. Really, really don't.
Last edited on
first off im using 4.9.8.0 (might that be the problem ?)

when i give in 10^10 i will get an output of

-2147483648

Wich obviously is wrong

The problem there is that a signed int value, which I'm guessing on your machine is 32 bits, can hold roughly the values

−2,147,483,648 to 2,147,483,647 (i.e. from −(2^31) to 2^31 − 1)

You will note that the value you are getting is the lower bound, suggesting that some kind of overflow has happened. Since 10^10 is 10 000 000 000, which is much larger than 2,147,483,647, it is impossible for an int to hold this value.

If you want to calculate this value, you will have to use a datatype that can handle it.
well i'm running on a 64 bit machine ? so i have no idea what to do.
You could learn about datatypes and see which of them can handle the numbers you expect to work with.

For example, you might decide you wanted to understand what an int is, so you would look here http://en.wikipedia.org/wiki/Integer_(computer_science) and see what values can be represented using signed and unsigned integers of various bit sizes.

You might decide that you need arbitrarily large integer types, and look into the common bignum libraries (such as http://gmplib.org/ ).

The point here is that you have realised that the computer cannot handle the infinite range of numbers that your mind can handle, and you will have to start learning about how it actually represents numbers, and you will have to decide what tradeoffs to make in in programming.




Thnx to youre wiki i got an hunch and made

cout<< static_cast<int>(pow(static_cast<double>(c),d));

into

cout<< static_cast<long long int>(pow(static_cast<double>(c),d));

and now it works like a charm.

Thank you much
Just be aware that a long long int has its limitations as well. If you are trying to do something like 1000 ^ 1000, you'll run into issues again.
im using Dev-c++
try this: http://orwellengine.blogspot.com/ I used to work on dev-c++ but it's far outdated, so if you want something OK, and keep the basic dev-c++ syntax, use the Orwell's update. It's realy awsome!
Topic archived. No new replies allowed.