Annotation of 42BSD/usr.lib/learn/C/L43.1b, revision 1.1.1.1

1.1       root        1: #print
                      2: Write a subroutine myexp(x) which expects a floating
                      3: argument x and returns the floating point value
                      4: of e to the x.  An adequate algorithm
                      5: for the purpose is the standard Maclaurin series
                      6: 
                      7:    x            2       3       4
                      8:   e  = 1 + x + x /2! + x /3! + x /4! + ...
                      9: Name your routine myexp(), not exp().  You can test it, then,
                     10: by comparing it with the system routine as well as by
                     11: comparing it with tables.  Leave it on file
                     12: myexp.c, and then type "ready".
                     13: #once #create reldif.c
                     14: double reldif(a,b)
                     15:  double a,b;
                     16: {
                     17: double c,d;
                     18: if (a==0. && b==0.) return(0.);
                     19: c = a>0 ? a : -a;
                     20: d = b>0 ? b : -b;
                     21: c = c>d ? c : d;
                     22: return( (a-b)/c );
                     23: }
                     24: #once #create tzaqc.c
                     25: main()
                     26: {
                     27: double x,y, log(), myexp(), reldif();
                     28: for(x=1.; x<5.; x=+ 0.2)
                     29:   {
                     30:   y = myexp(x);
                     31:  if (reldif(x, log(y)) >1.e-5) return(1);
                     32:   }
                     33: return(0);
                     34: }
                     35: exp()
                     36: {
                     37: printf("Cheat! you called the system routine\n");
                     38: return(1.2);
                     39: }
                     40: #user
                     41: cc tzaqc.c myexp.o reldif.c
                     42: a.out
                     43: #succeed
                     44: /* one way */
                     45: double myexp(x)
                     46: double x;
                     47: {
                     48:        double term, sum, dabs();
                     49:        int n;
                     50:        
                     51:        term = sum = 1.0;
                     52:        n = 1;
                     53:        while (dabs(term) > dabs(sum)/1.e8) {
                     54:                term = term * x / n++;
                     55:                sum += term;
                     56:        }
                     57:        return(sum);
                     58: }
                     59: 
                     60: double dabs(x)
                     61: double x;
                     62: {
                     63:        return(x>0 ? x : -x);
                     64: }
                     65: #log
                     66: #next
                     67: 50.1a 10

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.