Back to index

wims  3.65+svn20090927
Defines | Functions | Variables
evalue.c File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define NAN   log(-1)
#define evalname_no   (sizeof(evalname)/sizeof(evalname[0]))

Functions

char * moneyprint (char *p, double s)
void init_random (void)
double drand (double m)
double irand (double n)
double sign (double d)
double myround (double d)
double mylog2 (double d)
double sec (double d)
double csc (double d)
double cotan (double d)
double cotanh (double d)
double factorial (double d)
double binomial (double d1, double d2)
double max (double d1, double d2)
double min (double d1, double d2)
double gcd (double n1, double n2)
double lcm (double n1, double n2)
int get_evalcnt (void)
char * get_evalname (int i)
int get_evaltype (int i)
int evaltab_verify (void)
int search_evaltab (char *p)
int get_evalue_error (void)
void set_evalue_error (int e)
void set_evalue_pointer (char *p)
int eval_getpos (char *name)
void eval_setval (int pos, double v)
char * get_evalue_pointer (void)
double _evalue (int ord)
char * _substit (char *p)
double strevalue (char *p)
int evalue_compile (char *p)

Variables

struct {
char * name
int type
double val
double(* f1 )(double parm)
double(* f2 )(double parm1, double parm2)
evalname []
static char * evalue_pt
int evalue_error
char *(* substitute )(char *p) = _substit

Define Documentation

#define evalname_no   (sizeof(evalname)/sizeof(evalname[0]))

Definition at line 268 of file evalue.c.

#define NAN   log(-1)

Definition at line 20 of file evalue.c.


Function Documentation

double _evalue ( int  ord)

Definition at line 308 of file evalue.c.

{
    double d,dd;
    int i,k;
    char buf[32];

    
    if(evalue_error) return NAN;
    d=0;
    while(*evalue_pt=='+') evalue_pt++;
    if(*evalue_pt==0) return 0; /* empty string */
    switch(*evalue_pt) {
       case '(':
       evalue_pt++; d=_evalue(')');goto vld;
       case '|':
       if(ord=='|') {
           evalue_pt++; return 0;
       }
       evalue_pt++; d=fabs(_evalue('|'));goto vld;
       case '-':
       evalue_pt++; d=-_evalue(6);goto vld;
    }
    if((128&*evalue_pt)!=0) {
       k=(*evalue_pt)&255; evalue_pt++;
       if(k>=130 && k<140) {
           i=(k-130)*200; k=(*evalue_pt)&255; evalue_pt++;
           if(k<33 || k>=233) goto badeval;
           i+=k-33; if(i<0 || i>=evalname_no) goto badeval;
           goto ename;
       }
       if(k>=140 && k<150) {
           i=(k-140)*200; k=(*evalue_pt)&255; evalue_pt++;
           if(k<33 || k>=233) goto badeval;
           if(ev_var==NULL || ev_varcnt==NULL) goto badeval;
           i+=k-33; if(i<0 || i>=*ev_varcnt) goto badeval;
           goto vname;
       }
       evalue_pt++; goto badeval;
    }
    if(*evalue_pt=='.' || myisdigit(*evalue_pt))
       {d=strtod(evalue_pt,&evalue_pt);goto binary;}
    for(i=0;myisalnum(*(evalue_pt+i)) && i<16; i++)
      buf[i]=*(evalue_pt+i);
    buf[i]=0; evalue_pt+=i;
    if(i==0) goto badeval;
    if(ev_varcnt!=NULL && ev_var!=NULL && *ev_varcnt>0)
      for(i=0;i<*ev_varcnt;i++) {
         if(strcmp(buf,ev_var[i].name)==0) {
             vname: d=ev_var[i].value; goto vld;
         }
      }
    i=search_list(evalname,evalname_no,sizeof(evalname[0]),buf);
    ename: if(i>=0) switch(evalname[i].type) {
       case 0: {
           d=evalname[i].val;
           if(evalname[i].f1!=NULL) {
              if(d==0) d=NAN; if(d==1) d=HUGE_VAL;
           }
           break;
       }
       case 1: {
           if(*evalue_pt!='(') return NAN;
           evalue_pt++;
           d=evalname[i].f1(_evalue(')')); break;
       }
       case 2: {
           double parm1,parm2;
           if(*evalue_pt!='(') return NAN;
           evalue_pt++;
           parm1=_evalue(',');parm2=_evalue(')');
           d=evalname[i].f2(parm1,parm2); break;
       }
       default: {    /* This is impossible. */
           return NAN;
       }
    }
    else {
       badeval: evalue_error=-1; return NAN;
    }
  vld:
    if(evalue_error) return NAN;
  binary:
    if(*evalue_pt=='!') {
       evalue_pt++; d=factorial(d);
    }
    if(*evalue_pt==ord) {evalue_pt++;goto ok;}
    if(*evalue_pt==0 || 
       (ord<10 && (*evalue_pt==',' || *evalue_pt==';' || *evalue_pt==')'
                 || *evalue_pt=='|')))
       goto ok;
    switch(*evalue_pt) {
              case '+':
                     if(ord<=8) break;
                     evalue_pt++; d+=_evalue(8);goto vld;
              case '-':
                     if(ord<=8) break;
                     evalue_pt++; d-=_evalue(8);goto vld;
              case '*':
                     if(ord<=6) break;
                     evalue_pt++; d*=_evalue(6);goto vld;
              case '/':
                     if(ord<=6) break;
                     evalue_pt++; dd=_evalue(6);
                     if(dd==0) {evalue_error=10;return NAN;}
                     d/=dd;goto vld;
       case '%': {
           int di, ddi;
           if(ord<=6) break;
           evalue_pt++; dd=_evalue(6);
           if(dd==0) {evalue_error=10;return NAN;}
           di=d; ddi=dd; d=di%ddi;goto vld;
       }
       case '^': {
           if(ord<5) break;
           evalue_pt++; d=pow(d,_evalue(5));goto vld;
       }
       default : {
           /*if(ord<=6) break;
           d*=_evalue(6);goto vld;*/
           return NAN;
       }
    }
    ok: return d;
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* _substit ( char *  p)

Definition at line 436 of file evalue.c.

{
    return p;
}
double binomial ( double  d1,
double  d2 
)

Definition at line 136 of file evalue.c.

{
    return factorial(d1)/(factorial(d2)*factorial(d1-d2));
}

Here is the call graph for this function:

double cotan ( double  d)

Definition at line 113 of file evalue.c.

{
    return 1/tan(d);
}
double cotanh ( double  d)

Definition at line 119 of file evalue.c.

{
    return 1/tanh(d);
}
double csc ( double  d)

Definition at line 109 of file evalue.c.

{      return 1/sin(d);     }
double drand ( double  m)

Definition at line 63 of file evalue.c.

{
    double r;
    r=((double) random()+(double) random()/(double) RAND_MAX);
    return (r/(double) RAND_MAX)*m;
}

Here is the caller graph for this function:

int eval_getpos ( char *  name)

Definition at line 291 of file evalue.c.

{
    return search_list(evalname,evalname_no,sizeof(evalname[0]),name);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void eval_setval ( int  pos,
double  v 
)

Definition at line 297 of file evalue.c.

{
    if(pos>=0 && pos<evalname_no) evalname[pos].val=v;
}

Here is the caller graph for this function:

int evaltab_verify ( void  )

Definition at line 273 of file evalue.c.

{return verify_order(evalname,evalname_no,sizeof(evalname[0]));}

Here is the call graph for this function:

Here is the caller graph for this function:

int evalue_compile ( char *  p)

Definition at line 460 of file evalue.c.

{
    char *p1, *p2, *pe, name[256], buf[8];
    int i,k;
    
    k=0;
    for(p1=p; *p1; p1++) if((128&*p1)!=0) return -1;
    nospace(p);
    for(p1=find_mathvar_start(p); *p1; p1=find_mathvar_start(pe)) {
       pe=find_mathvar_end(p1);
       if(!myisalpha(*p1)) continue;
       p2=pe; if(p2-p1>16) continue;
       memmove(name,p1,p2-p1); name[p2-p1]=0;
       if(ev_varcnt!=NULL && ev_var!=NULL && *ev_varcnt>0) {
           for(i=0;i<*ev_varcnt && strcmp(name,ev_var[i].name)!=0;i++);
           if(i<*ev_varcnt && i<2000) {
              buf[0]=i/200+140; buf[1]=i%200+33; buf[2]=0;
              string_modify(p,p1,p2,"%s",buf);
              pe=p1+2; k++; continue;
           }
       }
       i=search_list(evalname,evalname_no,sizeof(evalname[0]),name);
       if(i>=0 && i<2000) {
           buf[0]=i/200+130; buf[1]=i%200+33; buf[2]=0;
           string_modify(p,p1,p2,"%s",buf);
           pe=p1+2; k++; continue;
       }
    }
    return k;
}

Here is the call graph for this function:

Here is the caller graph for this function:

double factorial ( double  d)

Definition at line 125 of file evalue.c.

{
    int i,n; double t;
    n=d;
    if(n<0 || n!=d) return NAN;
    if(n>1000) return HUGE_VAL;
    t=1; for(i=1;i<=n;i++) t=t*i;
    return t;
}

Here is the caller graph for this function:

double gcd ( double  n1,
double  n2 
)

Definition at line 154 of file evalue.c.

{
    unsigned long long int l1, l2, ll;
    n1=abs(n1); n2=abs(n2);
    if(!finite(n1) || !finite(n2) || n1<0 || n2<0 ||
       n1>1E18 || n2>1E18) return NAN;
    l1=n1; l2=n2;
    if(l1<l2) {
       ll=l1;l1=l2;l2=ll;
    }
    if(l1==0) return NAN;
    while(l2>0) {
       ll=l2;l2=l1%l2;l1=ll;
    }
    return l1;
}

Here is the caller graph for this function:

int get_evalcnt ( void  )

Definition at line 270 of file evalue.c.

{return evalname_no;}

Here is the caller graph for this function:

char* get_evalname ( int  i)

Definition at line 271 of file evalue.c.

{return evalname[i].name;}

Here is the caller graph for this function:

int get_evaltype ( int  i)

Definition at line 272 of file evalue.c.

{return evalname[i].type;}

Here is the caller graph for this function:

int get_evalue_error ( void  )

Definition at line 281 of file evalue.c.

{ return evalue_error; }
char* get_evalue_pointer ( void  )

Definition at line 303 of file evalue.c.

{
    return evalue_pt;
}
void init_random ( void  )

Definition at line 51 of file evalue.c.

{
    int r;
    struct timeval t;
/*    initstate(1,rand_buf,RAND_BUF_SIZE); */
    gettimeofday(&t,NULL);
    r=t.tv_usec+t.tv_sec*1000;
    if(r<0) r=-r; if(r==0) r=1;
    srandom(r);
}

Here is the caller graph for this function:

double irand ( double  n)

Definition at line 72 of file evalue.c.

{
    int  end,r;
    if(n==0) return 0;
    if(n>0) end=n; else end=-n;
    r=(double) random()*end/RAND_MAX;
    if(r==n) r--;
    if(n>0) return r; else return -r;    
}

Here is the caller graph for this function:

double lcm ( double  n1,
double  n2 
)

Definition at line 171 of file evalue.c.

{
    return n1*n2/gcd(n1,n2);
}

Here is the call graph for this function:

double max ( double  d1,
double  d2 
)

Definition at line 142 of file evalue.c.

{
    if(!finite(d1) || !finite(d2)) return NAN;
    if(d1<d2) return d2; else return d1;
}

Here is the caller graph for this function:

double min ( double  d1,
double  d2 
)

Definition at line 147 of file evalue.c.

{
    if(!finite(d1) || !finite(d2)) return NAN;
    if(d1<d2) return d1; else return d2;
}

Here is the caller graph for this function:

char* moneyprint ( char *  p,
double  s 
)

Definition at line 26 of file evalue.c.

{
    char *p1, *p2, buf[16];
    int t, t1, t2;
    if(s<0) {*p++='-'; s=-s;}
    if(s>999999) s=999999;
    t=floor(s*100+0.5); if(t>99999999) t=99999999; if(t<0) t=0;
    if(t==0) {*p++='0'; *p=0; return p;}
    t1=t/100; t2=t%100; p1=buf+10;
    for(*p1--=t1%10+'0',t1/=10;t1>0;*p1--=t1%10+'0',t1/=10);
    p2=buf+11;
    if(t2) {
       *p2++='.';
       *p2++=t2/10+'0'; t2%=10;
       if(t2) *p2++=t2+'0';
    }
    p1++; *p2=0; memmove(p,p1,p2-p1+1); p+=p2-p1;
    return p;
}

Here is the caller graph for this function:

double mylog2 ( double  d)

Definition at line 99 of file evalue.c.

{
    return log(d)/log(2);
}
double myround ( double  d)

Definition at line 91 of file evalue.c.

{
    long int t;
    if(d<0) t=d-0.5; else t=d+0.5;
    return t;
}
int search_evaltab ( char *  p)

Definition at line 274 of file evalue.c.

                            {
    return search_list(evalname,evalname_no,sizeof(evalname[0]),p);
}

Here is the call graph for this function:

Here is the caller graph for this function:

double sec ( double  d)

Definition at line 105 of file evalue.c.

{      return 1/cos(d);     }

Here is the caller graph for this function:

void set_evalue_error ( int  e)

Definition at line 282 of file evalue.c.

{evalue_error=e; return;}

Here is the caller graph for this function:

void set_evalue_pointer ( char *  p)

Definition at line 285 of file evalue.c.

{
    evalue_pt=p;
}

Here is the caller graph for this function:

double sign ( double  d)

Definition at line 83 of file evalue.c.

{
    if(d==0) return 0;
    if(d<0) return -1;
    else return 1;
}

Here is the caller graph for this function:

double strevalue ( char *  p)

Definition at line 444 of file evalue.c.

{
    char buf[MAX_LINELEN+1];
    
    if(p==NULL) return 0;
    mystrncpy(buf,p,sizeof(buf));
    substitute(buf); nospace(buf);
    if(check_parentheses(buf,0)) return NAN;
    set_evalue_error(0);
    set_evalue_pointer(buf);
    return _evalue(10);
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

struct { ... } evalname[]

Definition at line 279 of file evalue.c.

char* evalue_pt [static]

Definition at line 278 of file evalue.c.

char*(* substitute)(char *p) = _substit

Definition at line 441 of file evalue.c.