• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3124
  • Last Modified:

why Floating point error: Domain?

I always Floating point error: Domain.
Abnormal program termination

when I running my queuing simulation,
can anyone of you here can solve my problem
and tell me how the problem caused?

below is my program attached:
/* --------------------------------------------------------------------------
      INO Assignment 2 : Single Server Queue Similation

      LEE Siu Wing Thomas (21) 1302/2
-------------------------------------------------------------------------- */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

/* --------------------------------------------------------------------------
      Function Prototypes and Variable Declaration
-------------------------------------------------------------------------- */
float a,b;
char *string;
int         average_count; /* counter for calculate average */
long int    sims,       /* a counter of number of simulation */
      n,          /* number of service per simulation */
      nis,        /* number in system */
            nosms,      /* number of simulations to be done */
            co,         /* a counter to perform group report */
            cn,         /* a counter to control summary output */
            ca,         /* count arrivals */
            cs;         /* count services */

double      lambda,     /* mean interarrival time */
            mu,         /* mean service time */
            s,          /* serving time */
      ds,         /* duration of service time */
            w,          /* waiting time in system */
            wq,         /* waiting time in queue */
            nat,        /* next arrival time */
            iat,        /* interarrival time */
      net,        /* next event time */
      tle,        /* time of last event */
      nsct,       /* next service completion time */
            mnis,       /* mean number in system */
            mtis,       /* mean time in system */
      mniq,       /* mean number in queue */
            mtiq,       /* mean time in queue */
            ptsi,       /* proportion of server idle time */
            mptsi,      /* mean proportion of server idle time */
            a_mnis,     /* accumulate the mean number in system */
            a_mtis,     /* accumulate the mean time in system */
            a_mniq,     /* accumulate the mean number in queue */
            a_mtiq,     /* accumulate the mean time in queue */
            a_ptsi;     /* accumulate the proportion of server idle time */

FILE *outfile;

void initialize(void);     /* initialization function */
void timing_IAT_NAT(void); /* calculate IAT and NAT */
void timing_DS_NSCT(void); /* calculate DS and NSCT */
void arrive(void);         /* arrival event function */
void depart(void);         /* depart event function */
void report(void);         /* cal. stat. accumulator & print group report */
void summary(void);        /* cal. and print stat. summary report */
double expon(double mean); /* exponential variate generation function */

/* --------------------------------------------------------------------------
   Main Program Code
-------------------------------------------------------------------------- */
/* Main Program */
main()
{  /* open output file */
   outfile = fopen("MM1.OUT", "wt");

   /* read input parameters */
   printf ("Plz enter Total number of simulations to be done (NOSMS): ");
   scanf ("%ld",&nosms);
   printf ("Plz enter Number of services per simulation (N): ");
   scanf ("%ld",&n);
   printf ("Plz enter Number of services to generate group report (CO): ");
      scanf ("%ld",&co);
      printf ("Plz enter Mean interarrival time (Lambda): ");
/*      scanf("%s",string);
      lambda = atof(string);*/
      scanf ("%f",&a);
      lambda =a;
      printf ("Plz enter Mean service time (Mu): ");
/*      scanf("%s",string);
      mu = atof(string);*/
      scanf ("%f",&b);
      mu =b;
      printf ("Plz enter Initial number in system (NIS): ");
      scanf ("%ld",&nis);

      /* write report heading and input parameters */
      fprintf (outfile,"INO Assignment 2 : Single Server Queuing System\n");
      fprintf (outfile,"LEE Siu Wing Thomas (21) 1302/2\n\n");
   fprintf (outfile,"Total number of simulations to be done:        %8ld\n",nosms);
   fprintf (outfile,"Number of services per simulation:             %8ld\n",n);
   fprintf (outfile,"Number of services to generate group report:   %8ld\n",co);
   fprintf (outfile,"Mean interarrival time (Lambda):               %8.4lf minutes\n",lambda);
   fprintf (outfile,"Mean service time (Mu):                        %8.4lf minutes\n",mu);
      fprintf (outfile,"Number in system:                              %8ld\n",nis);

   /* assign a counter of number of simulations to 0 */
   sims = 0;

   /* initialize the random seed by using the system clock */
   srand(time(NULL));

   /* do the simulations until a counter of number of simulations equal to
      the total number of simulations to be done */
   while(sims < nosms)
   {  initialize();

      /* print the header and the statistical report */
      fprintf (outfile,"\nThe following is simulation number %3ld\n\n",sims +1);
      fprintf (outfile,"    CN       MNIQ      MNIS      MTIQ      MTIS      PTSI\n");
      fprintf (outfile,"---------------------------------------------------------\n");

      timing_IAT_NAT();

            /* is CN >= N? */
      {int tmp_counter=0;
      while ( !(cn >= n) )
      {  if (tmp_counter == 0);
         else cn += co;

         /* is NIS > 0? */
         if (nis > 0) timing_DS_NSCT();
         else  /* Next Event is an Arrival. */
               {   net = nat;
                  timing_DS_NSCT();
               }

         /* is NAT > NSCT? */
         /* if yes, Next Event is an Arrival */
         while ( !(nat > nsct) )
            arrive();
         /* if no, Next Event is Service Completion */
         depart();

         if (cs < cn);
         else summary();

         tmp_counter++;
      }}/* END while ( cn >= n ) */

      sims++;
      report();

   }/* END while (sims < nosms) */

   fclose(outfile);
   return 0;

}/* END Main Program */


/* initialization function */
void initialize(void)
{  cn = co;
   s = 0;
   w = 0;
   wq = 0;
   nat = 0;
   net = 0;
   tle = 0;
   nsct = 0;
   ca = 0;
   cs = 0;
   a_mnis = 0;
   a_mtis = 0;
   a_mniq = 0;
   a_mtiq = 0;
   a_ptsi = 0;
   average_count = 0;

}/* END initialization function */


/* calculate IAT and NAT */
void timing_IAT_NAT(void)
{  iat = expon(lambda);
   nat += iat;

}/* END calculate IAT and NAT */


/* calculate DS and NSCT */
void timing_DS_NSCT(void)
{  ds = expon(mu);
   nsct = ds + net;

}/* END calculate DS and NSCT */


/* arrival event function */
void arrive(void)
{  net = nat;
   ca++;
   w += nis * (net - tle);
   if (nis > 0)
   {  s += net -tle;
      wq += (nis - 1) * (net - tle);
   }
   nis++;
   tle = net;
   timing_IAT_NAT();

}/* END arrival event function */


/* depart event function */
void depart(void)
{  net = nsct;
   cs++;
   w += nis * (net - tle);
   s += net - tle;
   if (nis > 0) wq += (nis - 1) * (net - tle);
   nis--;
   tle = net;

}/* END depart event function */


/* cal. stat. accumulator & print group report */
void report(void)
{  fprintf (outfile,"---------------------------------------------------------\n");
   fprintf (outfile,"(Avg.) %6.3lf%6.3lf%6.3lf%6.3lf%6.3lf\n",a_mniq/average_count,
            a_mnis/average_count,a_mtiq/average_count,a_mtis/average_count,a_ptsi/average_count);

}/* END cal. stat. accumulator & print group report */

/* cal. and print stat. summary report */
void summary(void)
{  mtis = w/ca;
   a_mtis += mtis;
   mtiq = wq/ca;
   a_mtiq += mtiq;
   mnis = w/net;
   a_mnis += mnis;
   mniq = wq/net;
   a_mniq += mniq;
   ptsi = (net - s)/net;
   a_ptsi += ptsi;
   average_count++;

      fprintf (outfile,"%6ld %6.3lf%6.3lf%6.3lf%6.3lf%6.3lf\n\n",cn,mniq,mnis,mtiq,mtis,ptsi);

}/* END cal. and print stat. summary report */


/* exponential variate generation function */
double expon(double mean)
{  double u;

   /* generate a u(0,1) random value */
   u = 0.0;
   while ( u == 0.0 )
   {  u = rand() % 131;
      u = u/131;
   }

   /* return an exponential random value */
   return ( (-1*(log(u)))/(1/mean) );

}/* END exponential variate generation function */
0
hiddencode
Asked:
hiddencode
1 Solution
 
ozoCommented:
fprintf (outfile,"(Avg.) %6.3lf%6.3lf%6.3lf%6.3lf%6.3lf\n",a_mniq/average_count,
         a_mnis/average_count,a_mtiq/average_count,a_mtis/average_count,a_ptsi/average_count);

At this point average_count is 0, so a_mniq/average_count, etc. is nan
0
 
rbrCommented:
Strabge that you get a floating point error. I analiezed your program and i think you shoulkd get a Null Pointer assignment, since you use string withoout having any memory assigned to it.
Replace char *string with char string[100] and check if the program works correctly.
0
 
ChayaCommented:
What values are you entering?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LucHoltkampCommented:
ozo's right, I think (compiled and debugged the code...). Please make it an answer and get it done with. I keep looking here :)
.luc.
0
 
ozoCommented:
We can't really get it done with until hiddencode has compiled and debugged the code
and accepted an answer.
If hiddencode has abandoned this question, I'd prefer to let it expire unanswered
than to let the autograde time out with a C.


0
 
matth012098Commented:
A domain error occurs with most math functions in the standard math library when you pass the calling function a number that is out of its range eg sqrt(-1). The best bet would be to place a range check before all the math functions you are using, and call an error routine if this occurs. You will then know exactly where  the error is coming from.
0
 
AlFaCommented:
Look at log argument. I think it's the best candidate

AF
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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