Solved

why  Floating point error: Domain?

Posted on 1997-12-19
7
2,873 Views
Last Modified: 2012-05-04
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
Comment
Question by:hiddencode
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1256800
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
 
LVL 10

Expert Comment

by:rbr
ID: 1256801
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
 

Expert Comment

by:Chaya
ID: 1256802
What values are you entering?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

by:LucHoltkamp
ID: 1256803
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
 
LVL 84

Expert Comment

by:ozo
ID: 1256804
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
 
LVL 3

Expert Comment

by:matth012098
ID: 1256805
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
 
LVL 2

Accepted Solution

by:
AlFa earned 100 total points
ID: 1256806
Look at log argument. I think it's the best candidate

AF
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now