Counter changing to a negative number

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

Join Date: Oct 2006
Posts: 14
Reputation: spacecowboy123 is an unknown quantity at this point 
Solved Threads: 0
spacecowboy123 spacecowboy123 is offline Offline
Newbie Poster

Counter changing to a negative number

 
0
  #1
Oct 30th, 2006
Hi All,

I'm having a bit of trouble with a simulation I am writing in C++. Any help to solve the problem would be appreciated.

The simuation needs to run 5500000000 times.

When it gets to 2100000000, the counter I am using changes to -210000000. So as you can see, it will never finsh and do what I want.

I presume this is to do with how I have declared the variables, but am unsure of how to solve this.

Selected code below

  1. int clock = 0;
  2.  
  3. // Start the simulation
  4. while(clock < simulationlength)
  5. {
  6.  
  7. //arrival process on the corporate links
  8. if (clock % 4 == 0)
  9. {

Hope you can help.

Cheers
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,377
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Counter changing to a negative number

 
0
  #2
Oct 30th, 2006
use unsigned long intead of int. But first check for the maximum value for your compiler in the file limits.h. The maximum value for an unsigned long on my compiler is 4,294,967,295 -- slightly smaller than the value you want. You may have to go with a 64-bit integer, longlong on some compilers and __int64 on others.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 57
Reputation: may4life is an unknown quantity at this point 
Solved Threads: 2
may4life may4life is offline Offline
Junior Poster in Training

Re: Counter changing to a negative number

 
0
  #3
Nov 1st, 2006
Yeah, integers wrap around themselves. For example if you had a signed (takes positive and negative numbers, i think the min is -32,000 something and max is 32,000 something), then when you get to that maximum number and add 1 it will wrap around and start from its min.

If you have an unsigned variable (only positives), the maximum number it can take is double the normal. If you use unsigned long int, you will be able to use upto a value in the millions
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC