[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 586
  • Last Modified:

gets 1.#INF when adding two doubles

hello all!

im getting 1.#INF when im adding two doubles. i know for a fact that my number is not causing an overflow. so why am i getting this and how do i reslove it?

the partial source is below:

/**********************************************************
The preprocessor directives below are used as a replacement
for drand48() since visual c++ does not have a drand48() in
any of it's libraries. If code below seem to mess up the
simulation include drand48.c
**********************************************************/
#define srand48(s) srand(s)
#define drand48() (((double)rand())/((double)RAND_MAX))
#define lrand48() rand()


using namespace std;

enum{ Arrival, Departure};

struct Event
{
      double time;
      int type;

      Event(double tm = 0, int ty = 0)
      {
            time = tm;
            type = ty;
      }
};

struct Packet
{
      int size;
      double sTime;

      Packet(int s = 0, double sT = 0)
      {
            size = s;
            sTime = sT;
      }
};

double negExpDisT(double rate);



void main(int argc, char *argv[])
{

int itr, length, maxBuffer, debug, packetDrop;
double aRate, tRate, time, serverBusy;
Event curEvent;

length = debug = packetDrop = 0;
time = serverBusy = 0.0;

maxBuffer = atoi(argv[1]);
aRate = atof(argv[2]);
tRate = atof(argv[3]);
debug = atoi(argv[4]);

std::vector<Event> GEL;
std::vector<Event>::iterator iter;
Queue<Packet> Buffer(maxBuffer);

GEL.push_back(Event(negExpDisT(aRate),Arrival));

      for(int i = 0; i < 100000; i++)
      {            
            curEvent = GEL.front();
            
            if(GEL.size() == 1)
                  GEL.pop_back();
            else
                  iter = GEL.erase(GEL.begin());                        
            
            if(curEvent.type == Arrival)
            {
                  
                  time = curEvent.time;

                  if(debug)
                        cout << "Processing arrival event at time: " << time << endl;

                  Packet curPacket(1,negExpDisT(tRate));
                  insertEvent(GEL, Event(time + negExpDisT(aRate), Arrival));

                  if(length == 0)
                  {
                        length++;
                        insertEvent(GEL, Event(time + curPacket.sTime, Departure));
                  }
                  else if (length > 0 && ((length - 1) < maxBuffer))/**/
                  {
                        serverBusy++;
                        length++;
                        Buffer.enqueue(curPacket);                                                                    
                  }
                  else if (length > 0 && ((length - 1) >= maxBuffer)) /**/
                  {
                        serverBusy++;
                        packetDrop++;
                  }
                  //update stats here
            }
            else if(curEvent.type == Departure)
            {      
                  Packet curPacket;

                  time = curEvent.time;

                  if(debug)
                        cout << "Processing departure event at time: " << time << endl;

                  //update stats here
                                    
                  length--;

                  if(length > 0)
                  {
                        serverBusy++;
                        curPacket = Buffer.dequeue();
                        insertEvent(GEL, Event(time + curPacket.sTime, Departure));
                  }                  
            }
            else
            {
                  cout << "?!" << endl;
            }
      }
      
      cout << "Dropped packets: " << packetDrop << endl;
      cout << "Server busy: " << serverBusy << endl;
      cout << "Total time: " << time << endl;
      cout << "Server utilization: " << serverBusy/time << endl;
      cout << "Final buffer size: " << Buffer.size() << endl;
}

double negExpDisT(double rate)
{
      double u;
      u = drand48();
      return ((-1/rate)*log(1-u));
}

its completely fine until it reaches the 53131'st iteration but before that the values are perfectly good.

total time = 340677 at the 53130'th iteration so i know its way below oveflow.

help please :(
0
msgolez
Asked:
msgolez
1 Solution
 
jkrCommented:
>>im getting 1.#INF when im adding two doubles.

Where exactly? I would try to find that out by myself, but with only 'partial source', that won't work.
0
 
msgolezAuthor Commented:
cout << "Dropped packets: " << packetDrop << endl;
     cout << "Server busy: " << serverBusy << endl;
     cout << "Total time: " << time << endl;
     cout << "Server utilization: " << serverBusy/time << endl;
     cout << "Final buffer size: " << Buffer.size() << endl;

its completely fine until it reaches the 53131'st iteration but before that the values are perfectly good.
total time = 340677 at the 53130'th iteration so i know its way below oveflow.

could it be at the drand48() since i faked it using the preproccesor directives on top? visual c++ 6.0 doesnt have a drand48() function
0
 
msgolezAuthor Commented:
i mean what is "1.#INF" anyway and how do you get it? im guessing its a token for positive infinity?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
mrblueCommented:
"1.#INF" - means that number is converted to infinity

Example of such error (from MSDN):

cout << (float)1E300 << endl;

Maximum value representable by type float is 3.402823466E38 - a much smaller number than 1E300.

I think you should check if values like "rate" in negExpDisT() by which you divide are not zero.
0
 
grg99Commented:
I would add if statements to check the argument you pass to log().  Log(0) is undefine.

Also check the denominator of any divisions you do.  Dividing by zero is also a no-no.

Also after any  arithmetic operation ti wouldnt hurt to check for the result being impossibly big-- a simple check for the result being greater than 1.0E30 may catch a lot of bad results as early as possible.   Something like:


double CheckOkay( double x; char * Where ) {
  if( abs( x ) > 1.0E30 ) { fprintf( stderr, "Answer too big at %s: %g\n", Where, x ); return 1.0E30 }
else return x;
}


 Usage:

Answer = CheckOkay( "dubious divideop 1", dubiousnumerator / dubiousdenominator );
0
 
msgolezAuthor Commented:
yay!

thanks for all the replies guys
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now