Need explanation for reported negative sum

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2006
Posts: 5
Reputation: isaacmacdonald is an unknown quantity at this point 
Solved Threads: 0
isaacmacdonald isaacmacdonald is offline Offline
Newbie Poster

Need explanation for reported negative sum

 
1
  #1
Sep 20th, 2006
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 434
Reputation: FC Jamison is on a distinguished road 
Solved Threads: 20
Team Colleague
FC Jamison's Avatar
FC Jamison FC Jamison is offline Offline
Posting Pro in Training

Re: Need explanation for reported negative sum

 
1
  #2
Sep 20th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: Need explanation for reported negative sum

 
1
  #3
Sep 20th, 2006
Originally Posted by isaacmacdonald View Post
If sum is declared as double
Why would you declare sum as double in first place?
You can use long and it's variations.

Originally Posted by isaacmacdonald View Post
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.
The key to eliminating bugs from your code is learning from your mistakes.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: Need explanation for reported negative sum

 
1
  #4
Sep 20th, 2006
Originally Posted by FC Jamison View Post
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.
The key to eliminating bugs from your code is learning from your mistakes.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 434
Reputation: FC Jamison is on a distinguished road 
Solved Threads: 20
Team Colleague
FC Jamison's Avatar
FC Jamison FC Jamison is offline Offline
Posting Pro in Training

Re: Need explanation for reported negative sum

 
1
  #5
Sep 20th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: Need explanation for reported negative sum

 
1
  #6
Sep 20th, 2006
Instead of double use long long.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,623
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Need explanation for reported negative sum

 
1
  #7
Sep 20th, 2006
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: Need explanation for reported negative sum

 
0
  #8
Sep 20th, 2006
Originally Posted by andor View Post
Instead of double use long long.
Only if he's using C or C99 to be more specific.
The key to eliminating bugs from your code is learning from your mistakes.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 434
Reputation: FC Jamison is on a distinguished road 
Solved Threads: 20
Team Colleague
FC Jamison's Avatar
FC Jamison FC Jamison is offline Offline
Posting Pro in Training

Re: Need explanation for reported negative sum

 
0
  #9
Sep 20th, 2006
I think we lost track of the question...

He wanted to know WHY this happened using an int...not alternatives to using an int.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 434
Reputation: FC Jamison is on a distinguished road 
Solved Threads: 20
Team Colleague
FC Jamison's Avatar
FC Jamison FC Jamison is offline Offline
Posting Pro in Training

Re: Need explanation for reported negative sum

 
0
  #10
Sep 20th, 2006
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC