943,895 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3350
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 20th, 2006
1

Need explanation for reported negative sum

Expand Post »
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?
Reputation Points: 18
Solved Threads: 0
Newbie Poster
isaacmacdonald is offline Offline
5 posts
since Jun 2006
Sep 20th, 2006
1

Re: Need explanation for reported negative sum

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.
Last edited by FC Jamison; Sep 20th, 2006 at 11:39 am.
Team Colleague
Reputation Points: 92
Solved Threads: 21
Posting Pro in Training
FC Jamison is offline Offline
436 posts
since Jun 2004
Sep 20th, 2006
1

Re: Need explanation for reported negative sum

If sum is declared as double
Why would you declare sum as double in first place?
You can use long and it's variations.

sum is declared as an int, for the same range it reports -1270505460.
If int( or any other type) cannot accomodate the data then results are unpredictable.
Reputation Points: 197
Solved Threads: 12
Junior Poster
Grunt is offline Offline
147 posts
since Jul 2006
Sep 20th, 2006
1

Re: Need explanation for reported negative sum

Click to Expand / Collapse  Quote originally posted by FC Jamison ...
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.
Don't make any assumptions about size of types. Standard only guarantees minimum sizes.
Reputation Points: 197
Solved Threads: 12
Junior Poster
Grunt is offline Offline
147 posts
since Jul 2006
Sep 20th, 2006
1

Re: Need explanation for reported negative sum

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 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.
Team Colleague
Reputation Points: 92
Solved Threads: 21
Posting Pro in Training
FC Jamison is offline Offline
436 posts
since Jun 2004
Sep 20th, 2006
1

Re: Need explanation for reported negative sum

Instead of double use long long.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Sep 20th, 2006
1

Re: Need explanation for reported negative sum

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Sep 20th, 2006
0

Re: Need explanation for reported negative sum

Click to Expand / Collapse  Quote originally posted by andor ...
Instead of double use long long.
Only if he's using C or C99 to be more specific.
Reputation Points: 197
Solved Threads: 12
Junior Poster
Grunt is offline Offline
147 posts
since Jul 2006
Sep 20th, 2006
0

Re: Need explanation for reported negative sum

I think we lost track of the question...

He wanted to know WHY this happened using an int...not alternatives to using an int.
Team Colleague
Reputation Points: 92
Solved Threads: 21
Posting Pro in Training
FC Jamison is offline Offline
436 posts
since Jun 2004
Sep 20th, 2006
0

Re: Need explanation for reported negative sum

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...
Last edited by FC Jamison; Sep 20th, 2006 at 1:00 pm.
Team Colleague
Reputation Points: 92
Solved Threads: 21
Posting Pro in Training
FC Jamison is offline Offline
436 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Project related to some operating system topic
Next Thread in C++ Forum Timeline: Check Please!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC