-
c++ question
i do not know why but when I try and calculate 4.2 million and 5000 it gives me -884901888. i am using dev-c++ 4.9.9.2 as my compiler.
here is my code
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int blocks = 2400000;
int weight = 5000;
int total;
total = blocks * weight;
cout << "Weight is: " << total << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
-
Hi,
Could there be a possibility that the number of bits used to represent a memory location of type *int* may not hold the result of the integer multiplication being performed?
I am not sure, but, I myself am asking that question.
Cheers
Phenom
-
i can not believe i didnt think of that thank you.
-
You need to use long or double or something like that. I believe that int only goes from -32767 to 32766 (or something along those lines). So, once it hits the maximum, it's starting over at the minimum and continuing with the rest.
Don't quote me on the range, as I don't have my programming books right here. But it's definitely lower than what you're trying. So long would be the right choice (I believe)
Have a great day:)
Patrick
-
yea you need to use a double, long also dose not go high enough at least that's what oneof my books says.
scratch what i said before I remembered how to format the out_put