| | |
Need explanation for reported negative sum
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2006
Posts: 5
Reputation:
Solved Threads: 0
I wrote a simple program that sums the square of integers m -> n. If sum is declared as double it gives something like 4.16..x10^10 for integers 1 -5000. However, if sum is declared as an int, for the same range it reports -1270505460.
Obviously this has something to do with the 4-bytes allocated for int, but vague intuition != explanation. Can someone give me a good explanation for this phenomenon?
Obviously this has something to do with the 4-bytes allocated for int, but vague intuition != explanation. Can someone give me a good explanation for this phenomenon?
This is because of the size limitations of data types.
an int is only 4 bytes...and has a range from -2,147,483,648 to 2,147,483,647
A double uses 8 bytes (and some floating point algorithm that is beyond my comprehension at the moment) and has a range from +/-1.7E-308 to +/-1.7E308.
Your number simply went out of the range of an integer.
Interestingly enough...integers will simply loop through thaie cycle...i.e. if the number is larger than 2,147,483,647 it will restart the count at -2,147,483,648...whild doubles that exceed their upper limit will cause an error.
an int is only 4 bytes...and has a range from -2,147,483,648 to 2,147,483,647
A double uses 8 bytes (and some floating point algorithm that is beyond my comprehension at the moment) and has a range from +/-1.7E-308 to +/-1.7E308.
Your number simply went out of the range of an integer.
Interestingly enough...integers will simply loop through thaie cycle...i.e. if the number is larger than 2,147,483,647 it will restart the count at -2,147,483,648...whild doubles that exceed their upper limit will cause an error.
Last edited by FC Jamison; Sep 20th, 2006 at 11:39 am.
Why would you declare sum as double in first place?
You can use long and it's variations.
If int( or any other type) cannot accomodate the data then results are unpredictable.
You can use long and it's variations.
If int( or any other type) cannot accomodate the data then results are unpredictable.
The key to eliminating bugs from your code is learning from your mistakes.
•
•
•
•
This is because of the size limitations of data types.
an int is only 4 bytes...and has a range from -2,147,483,648 to 2,147,483,647
A double uses 8 bytes (and some floating point algorithm that is beyond my comprehension at the moment) and has a range from +/-1.7E-308 to +/-1.7E308.
Your number simply went out of the range of an integer.
Interestingly enough...integers will simply loop through thaie cycle...i.e. if the number is larger than 2,147,483,647 it will restart the count at -2,147,483,648...whild doubles that exceed their upper limit will cause an error.
The key to eliminating bugs from your code is learning from your mistakes.
True...the data types and sizes shown are typical on Windows systems, and the sizes and ranges may be different on other operating systems...
You can determine the size of an integer using
Still...my explanation is the most plausable...he has simply exceeded to range of an int.
You can determine the size of an integer using
sizeof(int);.Still...my explanation is the most plausable...he has simply exceeded to range of an int.
Last edited by FC Jamison; Sep 20th, 2006 at 12:10 pm.
If you are working with C++ and your application really demands high precision and range why not try out some third party libraries like these:
http://www.nongnu.org/hpalib/
http://www.tc.umn.edu/~ringx004/mapm-main.html
http://cliodhna.cop.uop.edu/~hetrick/c-sources.html
Though these libraries require you to study them before you use them, they are worth the effort.
HOpe it helped, bye.
http://www.nongnu.org/hpalib/
http://www.tc.umn.edu/~ringx004/mapm-main.html
http://cliodhna.cop.uop.edu/~hetrick/c-sources.html
Though these libraries require you to study them before you use them, they are worth the effort.
HOpe it helped, bye.
I don't accept change; I don't deserve to live.
Straight from the textbook...
When a variable is assigned a number that is too large for its data type, it overflows. Likewise, assigning a value that is too small for a variable causes it to underflow.
Typically, when an integer overflows, its contents wrap around to that data type's lowest possible value...
When a variable is assigned a number that is too large for its data type, it overflows. Likewise, assigning a value that is too small for a variable causes it to underflow.
Typically, when an integer overflows, its contents wrap around to that data type's lowest possible value...
Last edited by FC Jamison; Sep 20th, 2006 at 1:00 pm.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Project related to some operating system topic
- Next Thread: Check Please!
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






