-
September 13th, 2009, 10:25 PM
#1
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;
}
-
September 13th, 2009, 10:58 PM
#2
Junior Member
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
-
September 13th, 2009, 11:13 PM
#3
i can not believe i didnt think of that thank you.
-
September 14th, 2009, 12:34 AM
#4
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
-
September 14th, 2009, 01:41 AM
#5
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
Last edited by Riot; September 14th, 2009 at 11:38 PM.
Similar Threads
-
By jm459 in forum Tech Humor
Replies: 1
Last Post: April 14th, 2004, 01:41 PM
-
By Fasheezy in forum Hardware
Replies: 5
Last Post: February 5th, 2004, 04:25 PM
-
By smirc in forum AntiOnline's General Chit Chat
Replies: 6
Last Post: May 13th, 2002, 04:35 PM
-
By smirc in forum AntiOnline's General Chit Chat
Replies: 3
Last Post: May 13th, 2002, 03:24 AM
-
By NetSyn in forum Site Feedback/Questions/Suggestions
Replies: 15
Last Post: May 8th, 2002, 01:45 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|