Home » Archimedes archive » Archimedes World » AW-1996-03-Disc 2.adf » !AcornAns_AcornAns » MathFns/c/main

MathFns/c/main

This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.

Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.

Tape/disk: Home » Archimedes archive » Archimedes World » AW-1996-03-Disc 2.adf » !AcornAns_AcornAns
Filename: MathFns/c/main
Read OK:
File size: 0FE0 bytes
Load address: 0000
Exec address: 0000
File contents
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>

#include "wimpt.h"
#include "werr.h"
#include "bbc.h"
#include "os.h"

#include "swis.h"

#define one (1<<16)

extern int rbbcinc(int r, int k);
extern int div_frac16(int number, int divisor);
extern int mul_frac16(int x, int a);
extern int mul_frac16c(int x, int a);
extern int sqrt_frac28(unsigned int x);
extern int gauss16(void);
extern void sgauss16(int seed);
extern int cos16(int a);
extern int sin16(int a);
extern int exp16(int a);
extern int ln16(int a);
extern int pow16(int a, int b);

typedef enum {multi, nonmulti} monitor;
typedef enum {pow_fn, sin_fn, cos_fn, exp_fn, ln_fn} fn;

monitor monitortype;
int linesep;
double xs, ys;

char *fnname[] = {"x ^ 0.375", "sin x.pi/2", "cos x.pi/2", "exp x", "ln x"};
double fnxl[] = {0, -4, -4, -1, -1};
double fnxh[] = {1, 4, 4, 1.5, 8.4};
double fnyl[] = {0, -1, -1, -0.8, -1.6};
double fnyh[] = {1, 1, 1, 4, 2.368};

monitor read_monitor_type(void)
{
  int r2;
  os_swi3r(6, 161,133,0, 0,0,&r2);
  if ((r2/4 & 3) == 1) return multi;
  else return nonmulti;
}

void set_scales(int a, int b, int w, int h, double xl, double yl, double xh, double yh, BOOL axes, BOOL guides)
{
  int u,v;
  xs = w/(xh-xl);
  ys = h/(yh-yl);
  u = a-(int)(xl*xs);
  v = b-(int)(yl*ys);
  bbc_origin(0, 0);
  bbc_gwindow(a, b, a+w, b+h);
  bbc_gcol(0, 2);
  if (axes) {
    bbc_move(a, v);
    bbc_drawby(w, 0);
    bbc_move(u, b);
    bbc_drawby(0, h);
  }
  if (guides) {
    bbc_move(a, v+(int)(ys/65536.0));
    bbc_drawby(w, 0);
    bbc_move(a, v-(int)(ys/65536.0));
    bbc_drawby(w, 0);
    bbc_gcol(0, 3);
    bbc_move(a, v+(int)(ys/131072.0));
    bbc_drawby(w, 0);
    bbc_move(a, v-(int)(ys/131072.0));
    bbc_drawby(w, 0);
  }
  bbc_origin(u, v);
  bbc_gcol(0, 7);
}

int fnvalue(int f, int x)
{
  switch (f)
  {
    case pow_fn:
      return pow16(x, 24576);
      break;
    case sin_fn:
      return sin16(x);
      break;
    case cos_fn:
      return cos16(x);
      break;
    case exp_fn:
      return exp16(x);
      break;
    case ln_fn:
      return ln16(x);
      break;
  }
  return 0;
}

double fnerror(int f, int x)
{
  switch (f)
  {
    case pow_fn:
      return pow16(x, 24576)/65536.0-pow(x/65536.0, 0.375);
      break;
    case sin_fn:
      return sin16(x)/65536.0-sin(1.570796327*x/65536.0);
      break;
    case cos_fn:
      return cos16(x)/65536.0-cos(1.570796327*x/65536.0);
      break;
    case exp_fn:
      return exp16(x)/65536.0-exp(x/65536.0);
      break;
    case ln_fn:
      return x<=0 ? 0.01 : ln16(x)/65536.0-log(x/65536.0);
     /*nb 0.01 is a bodge to produce no visible points on graph for x<=0 since fns not defined in that range*/
      break;
  }
  return 0;
}

int main(void)
{
  int x, xmax, xinc;
  fn function;
  char dummy;

  monitortype = read_monitor_type();
  bbc_mode(monitortype==multi ? 20 : 12);
  linesep = monitortype==multi ? 20 : 40;

  for (function = pow_fn; function<=ln_fn; function++) {
    bbc_clg();
    bbc_gcol(0, 2);
    bbc_vdu(5);
    bbc_move(64, 992);
    printf(fnname[function]);
    bbc_move(64, 480);
    printf("error (point falling tween inner lines implies less than 0.5*1/65536)");
    set_scales(128+64,512+64,1024-128,512-128, fnxl[function], fnyl[function], fnxh[function], fnyh[function], TRUE, FALSE);
    xmax = (int)(fnxh[function]*one);
    xinc = (int)(one/xs);
    for (x=(int)(fnxl[function]*one); x<=xmax; x+=xinc) {
      bbc_plot(69, (int)(x*xs/one), (int)(fnvalue(function, x)*ys/one));
    }
    set_scales(128+64,0+64,1024-128,512-128, fnxl[function], -1/32768.0, fnxh[function], 1/32768.0, FALSE, TRUE);
    xinc = xinc/4;
    for (x=(int)(fnxl[function]*one); x<=xmax; x+=xinc) {
      bbc_plot(69, (int)(x*xs/one), (int)(fnerror(function, x)*ys));
    }
    bbc_origin(0,0);
    bbc_gwindow(0,0,1279,1023);
    bbc_move(1152, 131);
    printf("Press a");
    bbc_move(1152, 131-linesep);
    printf("key ...");
    bbc_vdu(4);
    os_cli("fx 15 1");
    bbc_cursor(0);
    dummy = bbc_get();
  }

  return 0;
}
00000000  23 69 6e 63 6c 75 64 65  20 3c 73 74 64 69 6f 2e  |#include <stdio.|
00000010  68 3e 0a 23 69 6e 63 6c  75 64 65 20 3c 73 74 64  |h>.#include <std|
00000020  6c 69 62 2e 68 3e 0a 23  69 6e 63 6c 75 64 65 20  |lib.h>.#include |
00000030  3c 73 74 72 69 6e 67 2e  68 3e 0a 23 69 6e 63 6c  |<string.h>.#incl|
00000040  75 64 65 20 3c 74 69 6d  65 2e 68 3e 0a 23 69 6e  |ude <time.h>.#in|
00000050  63 6c 75 64 65 20 3c 6d  61 74 68 2e 68 3e 0a 0a  |clude <math.h>..|
00000060  23 69 6e 63 6c 75 64 65  20 22 77 69 6d 70 74 2e  |#include "wimpt.|
00000070  68 22 0a 23 69 6e 63 6c  75 64 65 20 22 77 65 72  |h".#include "wer|
00000080  72 2e 68 22 0a 23 69 6e  63 6c 75 64 65 20 22 62  |r.h".#include "b|
00000090  62 63 2e 68 22 0a 23 69  6e 63 6c 75 64 65 20 22  |bc.h".#include "|
000000a0  6f 73 2e 68 22 0a 0a 23  69 6e 63 6c 75 64 65 20  |os.h"..#include |
000000b0  22 73 77 69 73 2e 68 22  0a 0a 23 64 65 66 69 6e  |"swis.h"..#defin|
000000c0  65 20 6f 6e 65 20 28 31  3c 3c 31 36 29 0a 0a 65  |e one (1<<16)..e|
000000d0  78 74 65 72 6e 20 69 6e  74 20 72 62 62 63 69 6e  |xtern int rbbcin|
000000e0  63 28 69 6e 74 20 72 2c  20 69 6e 74 20 6b 29 3b  |c(int r, int k);|
000000f0  0a 65 78 74 65 72 6e 20  69 6e 74 20 64 69 76 5f  |.extern int div_|
00000100  66 72 61 63 31 36 28 69  6e 74 20 6e 75 6d 62 65  |frac16(int numbe|
00000110  72 2c 20 69 6e 74 20 64  69 76 69 73 6f 72 29 3b  |r, int divisor);|
00000120  0a 65 78 74 65 72 6e 20  69 6e 74 20 6d 75 6c 5f  |.extern int mul_|
00000130  66 72 61 63 31 36 28 69  6e 74 20 78 2c 20 69 6e  |frac16(int x, in|
00000140  74 20 61 29 3b 0a 65 78  74 65 72 6e 20 69 6e 74  |t a);.extern int|
00000150  20 6d 75 6c 5f 66 72 61  63 31 36 63 28 69 6e 74  | mul_frac16c(int|
00000160  20 78 2c 20 69 6e 74 20  61 29 3b 0a 65 78 74 65  | x, int a);.exte|
00000170  72 6e 20 69 6e 74 20 73  71 72 74 5f 66 72 61 63  |rn int sqrt_frac|
00000180  32 38 28 75 6e 73 69 67  6e 65 64 20 69 6e 74 20  |28(unsigned int |
00000190  78 29 3b 0a 65 78 74 65  72 6e 20 69 6e 74 20 67  |x);.extern int g|
000001a0  61 75 73 73 31 36 28 76  6f 69 64 29 3b 0a 65 78  |auss16(void);.ex|
000001b0  74 65 72 6e 20 76 6f 69  64 20 73 67 61 75 73 73  |tern void sgauss|
000001c0  31 36 28 69 6e 74 20 73  65 65 64 29 3b 0a 65 78  |16(int seed);.ex|
000001d0  74 65 72 6e 20 69 6e 74  20 63 6f 73 31 36 28 69  |tern int cos16(i|
000001e0  6e 74 20 61 29 3b 0a 65  78 74 65 72 6e 20 69 6e  |nt a);.extern in|
000001f0  74 20 73 69 6e 31 36 28  69 6e 74 20 61 29 3b 0a  |t sin16(int a);.|
00000200  65 78 74 65 72 6e 20 69  6e 74 20 65 78 70 31 36  |extern int exp16|
00000210  28 69 6e 74 20 61 29 3b  0a 65 78 74 65 72 6e 20  |(int a);.extern |
00000220  69 6e 74 20 6c 6e 31 36  28 69 6e 74 20 61 29 3b  |int ln16(int a);|
00000230  0a 65 78 74 65 72 6e 20  69 6e 74 20 70 6f 77 31  |.extern int pow1|
00000240  36 28 69 6e 74 20 61 2c  20 69 6e 74 20 62 29 3b  |6(int a, int b);|
00000250  0a 0a 74 79 70 65 64 65  66 20 65 6e 75 6d 20 7b  |..typedef enum {|
00000260  6d 75 6c 74 69 2c 20 6e  6f 6e 6d 75 6c 74 69 7d  |multi, nonmulti}|
00000270  20 6d 6f 6e 69 74 6f 72  3b 0a 74 79 70 65 64 65  | monitor;.typede|
00000280  66 20 65 6e 75 6d 20 7b  70 6f 77 5f 66 6e 2c 20  |f enum {pow_fn, |
00000290  73 69 6e 5f 66 6e 2c 20  63 6f 73 5f 66 6e 2c 20  |sin_fn, cos_fn, |
000002a0  65 78 70 5f 66 6e 2c 20  6c 6e 5f 66 6e 7d 20 66  |exp_fn, ln_fn} f|
000002b0  6e 3b 0a 0a 6d 6f 6e 69  74 6f 72 20 6d 6f 6e 69  |n;..monitor moni|
000002c0  74 6f 72 74 79 70 65 3b  0a 69 6e 74 20 6c 69 6e  |tortype;.int lin|
000002d0  65 73 65 70 3b 0a 64 6f  75 62 6c 65 20 78 73 2c  |esep;.double xs,|
000002e0  20 79 73 3b 0a 0a 63 68  61 72 20 2a 66 6e 6e 61  | ys;..char *fnna|
000002f0  6d 65 5b 5d 20 3d 20 7b  22 78 20 5e 20 30 2e 33  |me[] = {"x ^ 0.3|
00000300  37 35 22 2c 20 22 73 69  6e 20 78 2e 70 69 2f 32  |75", "sin x.pi/2|
00000310  22 2c 20 22 63 6f 73 20  78 2e 70 69 2f 32 22 2c  |", "cos x.pi/2",|
00000320  20 22 65 78 70 20 78 22  2c 20 22 6c 6e 20 78 22  | "exp x", "ln x"|
00000330  7d 3b 0a 64 6f 75 62 6c  65 20 66 6e 78 6c 5b 5d  |};.double fnxl[]|
00000340  20 3d 20 7b 30 2c 20 2d  34 2c 20 2d 34 2c 20 2d  | = {0, -4, -4, -|
00000350  31 2c 20 2d 31 7d 3b 0a  64 6f 75 62 6c 65 20 66  |1, -1};.double f|
00000360  6e 78 68 5b 5d 20 3d 20  7b 31 2c 20 34 2c 20 34  |nxh[] = {1, 4, 4|
00000370  2c 20 31 2e 35 2c 20 38  2e 34 7d 3b 0a 64 6f 75  |, 1.5, 8.4};.dou|
00000380  62 6c 65 20 66 6e 79 6c  5b 5d 20 3d 20 7b 30 2c  |ble fnyl[] = {0,|
00000390  20 2d 31 2c 20 2d 31 2c  20 2d 30 2e 38 2c 20 2d  | -1, -1, -0.8, -|
000003a0  31 2e 36 7d 3b 0a 64 6f  75 62 6c 65 20 66 6e 79  |1.6};.double fny|
000003b0  68 5b 5d 20 3d 20 7b 31  2c 20 31 2c 20 31 2c 20  |h[] = {1, 1, 1, |
000003c0  34 2c 20 32 2e 33 36 38  7d 3b 0a 0a 6d 6f 6e 69  |4, 2.368};..moni|
000003d0  74 6f 72 20 72 65 61 64  5f 6d 6f 6e 69 74 6f 72  |tor read_monitor|
000003e0  5f 74 79 70 65 28 76 6f  69 64 29 0a 7b 0a 20 20  |_type(void).{.  |
000003f0  69 6e 74 20 72 32 3b 0a  20 20 6f 73 5f 73 77 69  |int r2;.  os_swi|
00000400  33 72 28 36 2c 20 31 36  31 2c 31 33 33 2c 30 2c  |3r(6, 161,133,0,|
00000410  20 30 2c 30 2c 26 72 32  29 3b 0a 20 20 69 66 20  | 0,0,&r2);.  if |
00000420  28 28 72 32 2f 34 20 26  20 33 29 20 3d 3d 20 31  |((r2/4 & 3) == 1|
00000430  29 20 72 65 74 75 72 6e  20 6d 75 6c 74 69 3b 0a  |) return multi;.|
00000440  20 20 65 6c 73 65 20 72  65 74 75 72 6e 20 6e 6f  |  else return no|
00000450  6e 6d 75 6c 74 69 3b 0a  7d 0a 0a 76 6f 69 64 20  |nmulti;.}..void |
00000460  73 65 74 5f 73 63 61 6c  65 73 28 69 6e 74 20 61  |set_scales(int a|
00000470  2c 20 69 6e 74 20 62 2c  20 69 6e 74 20 77 2c 20  |, int b, int w, |
00000480  69 6e 74 20 68 2c 20 64  6f 75 62 6c 65 20 78 6c  |int h, double xl|
00000490  2c 20 64 6f 75 62 6c 65  20 79 6c 2c 20 64 6f 75  |, double yl, dou|
000004a0  62 6c 65 20 78 68 2c 20  64 6f 75 62 6c 65 20 79  |ble xh, double y|
000004b0  68 2c 20 42 4f 4f 4c 20  61 78 65 73 2c 20 42 4f  |h, BOOL axes, BO|
000004c0  4f 4c 20 67 75 69 64 65  73 29 0a 7b 0a 20 20 69  |OL guides).{.  i|
000004d0  6e 74 20 75 2c 76 3b 0a  20 20 78 73 20 3d 20 77  |nt u,v;.  xs = w|
000004e0  2f 28 78 68 2d 78 6c 29  3b 0a 20 20 79 73 20 3d  |/(xh-xl);.  ys =|
000004f0  20 68 2f 28 79 68 2d 79  6c 29 3b 0a 20 20 75 20  | h/(yh-yl);.  u |
00000500  3d 20 61 2d 28 69 6e 74  29 28 78 6c 2a 78 73 29  |= a-(int)(xl*xs)|
00000510  3b 0a 20 20 76 20 3d 20  62 2d 28 69 6e 74 29 28  |;.  v = b-(int)(|
00000520  79 6c 2a 79 73 29 3b 0a  20 20 62 62 63 5f 6f 72  |yl*ys);.  bbc_or|
00000530  69 67 69 6e 28 30 2c 20  30 29 3b 0a 20 20 62 62  |igin(0, 0);.  bb|
00000540  63 5f 67 77 69 6e 64 6f  77 28 61 2c 20 62 2c 20  |c_gwindow(a, b, |
00000550  61 2b 77 2c 20 62 2b 68  29 3b 0a 20 20 62 62 63  |a+w, b+h);.  bbc|
00000560  5f 67 63 6f 6c 28 30 2c  20 32 29 3b 0a 20 20 69  |_gcol(0, 2);.  i|
00000570  66 20 28 61 78 65 73 29  20 7b 0a 20 20 20 20 62  |f (axes) {.    b|
00000580  62 63 5f 6d 6f 76 65 28  61 2c 20 76 29 3b 0a 20  |bc_move(a, v);. |
00000590  20 20 20 62 62 63 5f 64  72 61 77 62 79 28 77 2c  |   bbc_drawby(w,|
000005a0  20 30 29 3b 0a 20 20 20  20 62 62 63 5f 6d 6f 76  | 0);.    bbc_mov|
000005b0  65 28 75 2c 20 62 29 3b  0a 20 20 20 20 62 62 63  |e(u, b);.    bbc|
000005c0  5f 64 72 61 77 62 79 28  30 2c 20 68 29 3b 0a 20  |_drawby(0, h);. |
000005d0  20 7d 0a 20 20 69 66 20  28 67 75 69 64 65 73 29  | }.  if (guides)|
000005e0  20 7b 0a 20 20 20 20 62  62 63 5f 6d 6f 76 65 28  | {.    bbc_move(|
000005f0  61 2c 20 76 2b 28 69 6e  74 29 28 79 73 2f 36 35  |a, v+(int)(ys/65|
00000600  35 33 36 2e 30 29 29 3b  0a 20 20 20 20 62 62 63  |536.0));.    bbc|
00000610  5f 64 72 61 77 62 79 28  77 2c 20 30 29 3b 0a 20  |_drawby(w, 0);. |
00000620  20 20 20 62 62 63 5f 6d  6f 76 65 28 61 2c 20 76  |   bbc_move(a, v|
00000630  2d 28 69 6e 74 29 28 79  73 2f 36 35 35 33 36 2e  |-(int)(ys/65536.|
00000640  30 29 29 3b 0a 20 20 20  20 62 62 63 5f 64 72 61  |0));.    bbc_dra|
00000650  77 62 79 28 77 2c 20 30  29 3b 0a 20 20 20 20 62  |wby(w, 0);.    b|
00000660  62 63 5f 67 63 6f 6c 28  30 2c 20 33 29 3b 0a 20  |bc_gcol(0, 3);. |
00000670  20 20 20 62 62 63 5f 6d  6f 76 65 28 61 2c 20 76  |   bbc_move(a, v|
00000680  2b 28 69 6e 74 29 28 79  73 2f 31 33 31 30 37 32  |+(int)(ys/131072|
00000690  2e 30 29 29 3b 0a 20 20  20 20 62 62 63 5f 64 72  |.0));.    bbc_dr|
000006a0  61 77 62 79 28 77 2c 20  30 29 3b 0a 20 20 20 20  |awby(w, 0);.    |
000006b0  62 62 63 5f 6d 6f 76 65  28 61 2c 20 76 2d 28 69  |bbc_move(a, v-(i|
000006c0  6e 74 29 28 79 73 2f 31  33 31 30 37 32 2e 30 29  |nt)(ys/131072.0)|
000006d0  29 3b 0a 20 20 20 20 62  62 63 5f 64 72 61 77 62  |);.    bbc_drawb|
000006e0  79 28 77 2c 20 30 29 3b  0a 20 20 7d 0a 20 20 62  |y(w, 0);.  }.  b|
000006f0  62 63 5f 6f 72 69 67 69  6e 28 75 2c 20 76 29 3b  |bc_origin(u, v);|
00000700  0a 20 20 62 62 63 5f 67  63 6f 6c 28 30 2c 20 37  |.  bbc_gcol(0, 7|
00000710  29 3b 0a 7d 0a 0a 69 6e  74 20 66 6e 76 61 6c 75  |);.}..int fnvalu|
00000720  65 28 69 6e 74 20 66 2c  20 69 6e 74 20 78 29 0a  |e(int f, int x).|
00000730  7b 0a 20 20 73 77 69 74  63 68 20 28 66 29 0a 20  |{.  switch (f). |
00000740  20 7b 0a 20 20 20 20 63  61 73 65 20 70 6f 77 5f  | {.    case pow_|
00000750  66 6e 3a 0a 20 20 20 20  20 20 72 65 74 75 72 6e  |fn:.      return|
00000760  20 70 6f 77 31 36 28 78  2c 20 32 34 35 37 36 29  | pow16(x, 24576)|
00000770  3b 0a 20 20 20 20 20 20  62 72 65 61 6b 3b 0a 20  |;.      break;. |
00000780  20 20 20 63 61 73 65 20  73 69 6e 5f 66 6e 3a 0a  |   case sin_fn:.|
00000790  20 20 20 20 20 20 72 65  74 75 72 6e 20 73 69 6e  |      return sin|
000007a0  31 36 28 78 29 3b 0a 20  20 20 20 20 20 62 72 65  |16(x);.      bre|
000007b0  61 6b 3b 0a 20 20 20 20  63 61 73 65 20 63 6f 73  |ak;.    case cos|
000007c0  5f 66 6e 3a 0a 20 20 20  20 20 20 72 65 74 75 72  |_fn:.      retur|
000007d0  6e 20 63 6f 73 31 36 28  78 29 3b 0a 20 20 20 20  |n cos16(x);.    |
000007e0  20 20 62 72 65 61 6b 3b  0a 20 20 20 20 63 61 73  |  break;.    cas|
000007f0  65 20 65 78 70 5f 66 6e  3a 0a 20 20 20 20 20 20  |e exp_fn:.      |
00000800  72 65 74 75 72 6e 20 65  78 70 31 36 28 78 29 3b  |return exp16(x);|
00000810  0a 20 20 20 20 20 20 62  72 65 61 6b 3b 0a 20 20  |.      break;.  |
00000820  20 20 63 61 73 65 20 6c  6e 5f 66 6e 3a 0a 20 20  |  case ln_fn:.  |
00000830  20 20 20 20 72 65 74 75  72 6e 20 6c 6e 31 36 28  |    return ln16(|
00000840  78 29 3b 0a 20 20 20 20  20 20 62 72 65 61 6b 3b  |x);.      break;|
00000850  0a 20 20 7d 0a 20 20 72  65 74 75 72 6e 20 30 3b  |.  }.  return 0;|
00000860  0a 7d 0a 0a 64 6f 75 62  6c 65 20 66 6e 65 72 72  |.}..double fnerr|
00000870  6f 72 28 69 6e 74 20 66  2c 20 69 6e 74 20 78 29  |or(int f, int x)|
00000880  0a 7b 0a 20 20 73 77 69  74 63 68 20 28 66 29 0a  |.{.  switch (f).|
00000890  20 20 7b 0a 20 20 20 20  63 61 73 65 20 70 6f 77  |  {.    case pow|
000008a0  5f 66 6e 3a 0a 20 20 20  20 20 20 72 65 74 75 72  |_fn:.      retur|
000008b0  6e 20 70 6f 77 31 36 28  78 2c 20 32 34 35 37 36  |n pow16(x, 24576|
000008c0  29 2f 36 35 35 33 36 2e  30 2d 70 6f 77 28 78 2f  |)/65536.0-pow(x/|
000008d0  36 35 35 33 36 2e 30 2c  20 30 2e 33 37 35 29 3b  |65536.0, 0.375);|
000008e0  0a 20 20 20 20 20 20 62  72 65 61 6b 3b 0a 20 20  |.      break;.  |
000008f0  20 20 63 61 73 65 20 73  69 6e 5f 66 6e 3a 0a 20  |  case sin_fn:. |
00000900  20 20 20 20 20 72 65 74  75 72 6e 20 73 69 6e 31  |     return sin1|
00000910  36 28 78 29 2f 36 35 35  33 36 2e 30 2d 73 69 6e  |6(x)/65536.0-sin|
00000920  28 31 2e 35 37 30 37 39  36 33 32 37 2a 78 2f 36  |(1.570796327*x/6|
00000930  35 35 33 36 2e 30 29 3b  0a 20 20 20 20 20 20 62  |5536.0);.      b|
00000940  72 65 61 6b 3b 0a 20 20  20 20 63 61 73 65 20 63  |reak;.    case c|
00000950  6f 73 5f 66 6e 3a 0a 20  20 20 20 20 20 72 65 74  |os_fn:.      ret|
00000960  75 72 6e 20 63 6f 73 31  36 28 78 29 2f 36 35 35  |urn cos16(x)/655|
00000970  33 36 2e 30 2d 63 6f 73  28 31 2e 35 37 30 37 39  |36.0-cos(1.57079|
00000980  36 33 32 37 2a 78 2f 36  35 35 33 36 2e 30 29 3b  |6327*x/65536.0);|
00000990  0a 20 20 20 20 20 20 62  72 65 61 6b 3b 0a 20 20  |.      break;.  |
000009a0  20 20 63 61 73 65 20 65  78 70 5f 66 6e 3a 0a 20  |  case exp_fn:. |
000009b0  20 20 20 20 20 72 65 74  75 72 6e 20 65 78 70 31  |     return exp1|
000009c0  36 28 78 29 2f 36 35 35  33 36 2e 30 2d 65 78 70  |6(x)/65536.0-exp|
000009d0  28 78 2f 36 35 35 33 36  2e 30 29 3b 0a 20 20 20  |(x/65536.0);.   |
000009e0  20 20 20 62 72 65 61 6b  3b 0a 20 20 20 20 63 61  |   break;.    ca|
000009f0  73 65 20 6c 6e 5f 66 6e  3a 0a 20 20 20 20 20 20  |se ln_fn:.      |
00000a00  72 65 74 75 72 6e 20 78  3c 3d 30 20 3f 20 30 2e  |return x<=0 ? 0.|
00000a10  30 31 20 3a 20 6c 6e 31  36 28 78 29 2f 36 35 35  |01 : ln16(x)/655|
00000a20  33 36 2e 30 2d 6c 6f 67  28 78 2f 36 35 35 33 36  |36.0-log(x/65536|
00000a30  2e 30 29 3b 0a 20 20 20  20 20 2f 2a 6e 62 20 30  |.0);.     /*nb 0|
00000a40  2e 30 31 20 69 73 20 61  20 62 6f 64 67 65 20 74  |.01 is a bodge t|
00000a50  6f 20 70 72 6f 64 75 63  65 20 6e 6f 20 76 69 73  |o produce no vis|
00000a60  69 62 6c 65 20 70 6f 69  6e 74 73 20 6f 6e 20 67  |ible points on g|
00000a70  72 61 70 68 20 66 6f 72  20 78 3c 3d 30 20 73 69  |raph for x<=0 si|
00000a80  6e 63 65 20 66 6e 73 20  6e 6f 74 20 64 65 66 69  |nce fns not defi|
00000a90  6e 65 64 20 69 6e 20 74  68 61 74 20 72 61 6e 67  |ned in that rang|
00000aa0  65 2a 2f 0a 20 20 20 20  20 20 62 72 65 61 6b 3b  |e*/.      break;|
00000ab0  0a 20 20 7d 0a 20 20 72  65 74 75 72 6e 20 30 3b  |.  }.  return 0;|
00000ac0  0a 7d 0a 0a 69 6e 74 20  6d 61 69 6e 28 76 6f 69  |.}..int main(voi|
00000ad0  64 29 0a 7b 0a 20 20 69  6e 74 20 78 2c 20 78 6d  |d).{.  int x, xm|
00000ae0  61 78 2c 20 78 69 6e 63  3b 0a 20 20 66 6e 20 66  |ax, xinc;.  fn f|
00000af0  75 6e 63 74 69 6f 6e 3b  0a 20 20 63 68 61 72 20  |unction;.  char |
00000b00  64 75 6d 6d 79 3b 0a 0a  20 20 6d 6f 6e 69 74 6f  |dummy;..  monito|
00000b10  72 74 79 70 65 20 3d 20  72 65 61 64 5f 6d 6f 6e  |rtype = read_mon|
00000b20  69 74 6f 72 5f 74 79 70  65 28 29 3b 0a 20 20 62  |itor_type();.  b|
00000b30  62 63 5f 6d 6f 64 65 28  6d 6f 6e 69 74 6f 72 74  |bc_mode(monitort|
00000b40  79 70 65 3d 3d 6d 75 6c  74 69 20 3f 20 32 30 20  |ype==multi ? 20 |
00000b50  3a 20 31 32 29 3b 0a 20  20 6c 69 6e 65 73 65 70  |: 12);.  linesep|
00000b60  20 3d 20 6d 6f 6e 69 74  6f 72 74 79 70 65 3d 3d  | = monitortype==|
00000b70  6d 75 6c 74 69 20 3f 20  32 30 20 3a 20 34 30 3b  |multi ? 20 : 40;|
00000b80  0a 0a 20 20 66 6f 72 20  28 66 75 6e 63 74 69 6f  |..  for (functio|
00000b90  6e 20 3d 20 70 6f 77 5f  66 6e 3b 20 66 75 6e 63  |n = pow_fn; func|
00000ba0  74 69 6f 6e 3c 3d 6c 6e  5f 66 6e 3b 20 66 75 6e  |tion<=ln_fn; fun|
00000bb0  63 74 69 6f 6e 2b 2b 29  20 7b 0a 20 20 20 20 62  |ction++) {.    b|
00000bc0  62 63 5f 63 6c 67 28 29  3b 0a 20 20 20 20 62 62  |bc_clg();.    bb|
00000bd0  63 5f 67 63 6f 6c 28 30  2c 20 32 29 3b 0a 20 20  |c_gcol(0, 2);.  |
00000be0  20 20 62 62 63 5f 76 64  75 28 35 29 3b 0a 20 20  |  bbc_vdu(5);.  |
00000bf0  20 20 62 62 63 5f 6d 6f  76 65 28 36 34 2c 20 39  |  bbc_move(64, 9|
00000c00  39 32 29 3b 0a 20 20 20  20 70 72 69 6e 74 66 28  |92);.    printf(|
00000c10  66 6e 6e 61 6d 65 5b 66  75 6e 63 74 69 6f 6e 5d  |fnname[function]|
00000c20  29 3b 0a 20 20 20 20 62  62 63 5f 6d 6f 76 65 28  |);.    bbc_move(|
00000c30  36 34 2c 20 34 38 30 29  3b 0a 20 20 20 20 70 72  |64, 480);.    pr|
00000c40  69 6e 74 66 28 22 65 72  72 6f 72 20 28 70 6f 69  |intf("error (poi|
00000c50  6e 74 20 66 61 6c 6c 69  6e 67 20 74 77 65 65 6e  |nt falling tween|
00000c60  20 69 6e 6e 65 72 20 6c  69 6e 65 73 20 69 6d 70  | inner lines imp|
00000c70  6c 69 65 73 20 6c 65 73  73 20 74 68 61 6e 20 30  |lies less than 0|
00000c80  2e 35 2a 31 2f 36 35 35  33 36 29 22 29 3b 0a 20  |.5*1/65536)");. |
00000c90  20 20 20 73 65 74 5f 73  63 61 6c 65 73 28 31 32  |   set_scales(12|
00000ca0  38 2b 36 34 2c 35 31 32  2b 36 34 2c 31 30 32 34  |8+64,512+64,1024|
00000cb0  2d 31 32 38 2c 35 31 32  2d 31 32 38 2c 20 66 6e  |-128,512-128, fn|
00000cc0  78 6c 5b 66 75 6e 63 74  69 6f 6e 5d 2c 20 66 6e  |xl[function], fn|
00000cd0  79 6c 5b 66 75 6e 63 74  69 6f 6e 5d 2c 20 66 6e  |yl[function], fn|
00000ce0  78 68 5b 66 75 6e 63 74  69 6f 6e 5d 2c 20 66 6e  |xh[function], fn|
00000cf0  79 68 5b 66 75 6e 63 74  69 6f 6e 5d 2c 20 54 52  |yh[function], TR|
00000d00  55 45 2c 20 46 41 4c 53  45 29 3b 0a 20 20 20 20  |UE, FALSE);.    |
00000d10  78 6d 61 78 20 3d 20 28  69 6e 74 29 28 66 6e 78  |xmax = (int)(fnx|
00000d20  68 5b 66 75 6e 63 74 69  6f 6e 5d 2a 6f 6e 65 29  |h[function]*one)|
00000d30  3b 0a 20 20 20 20 78 69  6e 63 20 3d 20 28 69 6e  |;.    xinc = (in|
00000d40  74 29 28 6f 6e 65 2f 78  73 29 3b 0a 20 20 20 20  |t)(one/xs);.    |
00000d50  66 6f 72 20 28 78 3d 28  69 6e 74 29 28 66 6e 78  |for (x=(int)(fnx|
00000d60  6c 5b 66 75 6e 63 74 69  6f 6e 5d 2a 6f 6e 65 29  |l[function]*one)|
00000d70  3b 20 78 3c 3d 78 6d 61  78 3b 20 78 2b 3d 78 69  |; x<=xmax; x+=xi|
00000d80  6e 63 29 20 7b 0a 20 20  20 20 20 20 62 62 63 5f  |nc) {.      bbc_|
00000d90  70 6c 6f 74 28 36 39 2c  20 28 69 6e 74 29 28 78  |plot(69, (int)(x|
00000da0  2a 78 73 2f 6f 6e 65 29  2c 20 28 69 6e 74 29 28  |*xs/one), (int)(|
00000db0  66 6e 76 61 6c 75 65 28  66 75 6e 63 74 69 6f 6e  |fnvalue(function|
00000dc0  2c 20 78 29 2a 79 73 2f  6f 6e 65 29 29 3b 0a 20  |, x)*ys/one));. |
00000dd0  20 20 20 7d 0a 20 20 20  20 73 65 74 5f 73 63 61  |   }.    set_sca|
00000de0  6c 65 73 28 31 32 38 2b  36 34 2c 30 2b 36 34 2c  |les(128+64,0+64,|
00000df0  31 30 32 34 2d 31 32 38  2c 35 31 32 2d 31 32 38  |1024-128,512-128|
00000e00  2c 20 66 6e 78 6c 5b 66  75 6e 63 74 69 6f 6e 5d  |, fnxl[function]|
00000e10  2c 20 2d 31 2f 33 32 37  36 38 2e 30 2c 20 66 6e  |, -1/32768.0, fn|
00000e20  78 68 5b 66 75 6e 63 74  69 6f 6e 5d 2c 20 31 2f  |xh[function], 1/|
00000e30  33 32 37 36 38 2e 30 2c  20 46 41 4c 53 45 2c 20  |32768.0, FALSE, |
00000e40  54 52 55 45 29 3b 0a 20  20 20 20 78 69 6e 63 20  |TRUE);.    xinc |
00000e50  3d 20 78 69 6e 63 2f 34  3b 0a 20 20 20 20 66 6f  |= xinc/4;.    fo|
00000e60  72 20 28 78 3d 28 69 6e  74 29 28 66 6e 78 6c 5b  |r (x=(int)(fnxl[|
00000e70  66 75 6e 63 74 69 6f 6e  5d 2a 6f 6e 65 29 3b 20  |function]*one); |
00000e80  78 3c 3d 78 6d 61 78 3b  20 78 2b 3d 78 69 6e 63  |x<=xmax; x+=xinc|
00000e90  29 20 7b 0a 20 20 20 20  20 20 62 62 63 5f 70 6c  |) {.      bbc_pl|
00000ea0  6f 74 28 36 39 2c 20 28  69 6e 74 29 28 78 2a 78  |ot(69, (int)(x*x|
00000eb0  73 2f 6f 6e 65 29 2c 20  28 69 6e 74 29 28 66 6e  |s/one), (int)(fn|
00000ec0  65 72 72 6f 72 28 66 75  6e 63 74 69 6f 6e 2c 20  |error(function, |
00000ed0  78 29 2a 79 73 29 29 3b  0a 20 20 20 20 7d 0a 20  |x)*ys));.    }. |
00000ee0  20 20 20 62 62 63 5f 6f  72 69 67 69 6e 28 30 2c  |   bbc_origin(0,|
00000ef0  30 29 3b 0a 20 20 20 20  62 62 63 5f 67 77 69 6e  |0);.    bbc_gwin|
00000f00  64 6f 77 28 30 2c 30 2c  31 32 37 39 2c 31 30 32  |dow(0,0,1279,102|
00000f10  33 29 3b 0a 20 20 20 20  62 62 63 5f 6d 6f 76 65  |3);.    bbc_move|
00000f20  28 31 31 35 32 2c 20 31  33 31 29 3b 0a 20 20 20  |(1152, 131);.   |
00000f30  20 70 72 69 6e 74 66 28  22 50 72 65 73 73 20 61  | printf("Press a|
00000f40  22 29 3b 0a 20 20 20 20  62 62 63 5f 6d 6f 76 65  |");.    bbc_move|
00000f50  28 31 31 35 32 2c 20 31  33 31 2d 6c 69 6e 65 73  |(1152, 131-lines|
00000f60  65 70 29 3b 0a 20 20 20  20 70 72 69 6e 74 66 28  |ep);.    printf(|
00000f70  22 6b 65 79 20 2e 2e 2e  22 29 3b 0a 20 20 20 20  |"key ...");.    |
00000f80  62 62 63 5f 76 64 75 28  34 29 3b 0a 20 20 20 20  |bbc_vdu(4);.    |
00000f90  6f 73 5f 63 6c 69 28 22  66 78 20 31 35 20 31 22  |os_cli("fx 15 1"|
00000fa0  29 3b 0a 20 20 20 20 62  62 63 5f 63 75 72 73 6f  |);.    bbc_curso|
00000fb0  72 28 30 29 3b 0a 20 20  20 20 64 75 6d 6d 79 20  |r(0);.    dummy |
00000fc0  3d 20 62 62 63 5f 67 65  74 28 29 3b 0a 20 20 7d  |= bbc_get();.  }|
00000fd0  0a 0a 20 20 72 65 74 75  72 6e 20 30 3b 0a 7d 0a  |..  return 0;.}.|
00000fe0