home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / unix / volume26 / calc / part01 < prev    next >
Encoding:
Text File  |  1992-05-09  |  61.9 KB  |  2,110 lines

  1. Newsgroups: comp.sources.unix
  2. From: dbell@pdact.pd.necisa.oz.au (David I. Bell)
  3. Subject: v26i027: CALC - An arbitrary precision C-like calculator, Part01/21
  4. Sender: unix-sources-moderator@pa.dec.com
  5. Approved: vixie@pa.dec.com
  6.  
  7. Submitted-By: dbell@pdact.pd.necisa.oz.au (David I. Bell)
  8. Posting-Number: Volume 26, Issue 27
  9. Archive-Name: calc/part01
  10.  
  11. [ don't let the size and number of features fool you -- this is a great
  12.   calculator for those of us who can never remember the obscure and "help"-
  13.   less syntax of bc and dc.  it happens that you could use this package to
  14.   verify the correctness of your floating-point implementation, but even
  15.   if you're just trying to calculate disk geometries or sales tax, this is
  16.   very probably the best and easiest-to-use tool for the job.
  17.  
  18.   it builds and installs and runs perfectly on my ultrix system.
  19.  
  20.   --vix ]
  21.  
  22. Calc is an arbitrary precision calculator.  It handles large integers,
  23. fractions, real numbers (approximated using fractions), complex numbers,
  24. strings, and matrices.  The size of numbers and the precision of real
  25. calculations is limited only by the available memory and the run time.
  26.  
  27. Calc can be used interactively by the user to evaluate expressions.
  28. It can also be programmed using a language much like C.  This allows
  29. the user to define complicated functions which can then also be used
  30. interactively.
  31.  
  32. There are many built-in functions (e.g., gcd, comb, sin, exp, det).
  33. Many of these functions are useful in number theory.
  34.  
  35. In addition to the built-in types, calc can also handle new types of
  36. objects whose representation and operations are defined by the user
  37. (e.g., quadratic surds, quaternions, modular arithmetic).
  38.  
  39. Calc is written in C so as to be portable.  No assembly language is
  40. used, and machine dependencies are few.  When building calc, the major
  41. dependency is knowing whether your machine is big or little endian.
  42. Calc has been successfully built on several different types of machines.
  43.  
  44. The code for the arbitrary precision routines is independent of the code
  45. for the rest of the calculator.  These routines can be called by your own
  46. C programs to manipulate either large integers, or large fractions.
  47.  
  48. These sources are also available by anonymous ftp from ftp.uu.net in the
  49. directory pub/calc.  Calc will also be distributed as part of 4.4BSD, and
  50. has been given to the GNU project.
  51.  
  52. Enjoy!
  53.  
  54. David I. Bell
  55. dbell@pdact.pd.necisa.oz.au
  56.  
  57.  
  58. #! /bin/sh
  59. # This is a shell archive.  Remove anything before this line, then unpack
  60. # it by saving it into a file and typing "sh file".  To overwrite existing
  61. # files, type "sh file -c".  You can also feed this as standard input via
  62. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  63. # will see the following message at the end:
  64. #        "End of archive 1 (of 21)."
  65. # Contents:  README func.h help help/Makefile help/credit
  66. #   help/environment help/expression help/header help/help
  67. #   help/history help/interrupt help/intro help/list help/operator
  68. #   help/sums help/todo help/usage help/variable label.h lib
  69. #   lib/Makefile lib/bernoulli.cal lib/bigprime.cal lib/deg.cal
  70. #   lib/mersenne.cal lib/nextprim.cal lib/pell.cal lib/pi.cal
  71. #   lib/pollard.cal lib/psqrt.cal lib/solve.cal lib/sumsq.cal
  72. #   lib/unitfrac.cal lib/varargs.cal lint.sed stdarg.h string.h
  73. #   symbol.h version.c
  74. # Wrapped by dbell@elm on Tue Feb 25 15:20:52 1992
  75. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  76. if test -f 'README' -a "${1}" != "-c" ; then 
  77.   echo shar: Will not clobber existing file \"'README'\"
  78. else
  79. echo shar: Extracting \"'README'\" \(1357 characters\)
  80. sed "s/^X//" >'README' <<'END_OF_FILE'
  81. X
  82. X# Copyright (c) 1992 David I. Bell
  83. X# Permission is granted to use, distribute, or modify this source,
  84. X# provided that this copyright notice remains intact.
  85. X#
  86. X# Arbitrary precision calculator.
  87. X
  88. XI am allowing this calculator to be freely distributed for personal uses.
  89. XLike all multi-precision programs, you should not depend absolutely on
  90. Xits results, since bugs in such programs can be insidious and only rarely 
  91. Xshow up.
  92. X  
  93. X-dbell-
  94. X
  95. Xp.s. By Landon Curt Noll
  96. X
  97. XTo build calc:
  98. X
  99. X    1) Look at the makefile, and adjust it to suit your needs.
  100. X
  101. X    2) build some calc documentation:
  102. X
  103. X    (cd help; make full)    <- after this, read the file help/full
  104. X
  105. X    3) build calc:
  106. X
  107. X    make
  108. X
  109. X    4) test calc:
  110. X
  111. X    After you have built calc, you can test it by running calc and 
  112. X    giving calc the following input:
  113. X
  114. X    read lib/regress    <- error messages should be printed
  115. X    read lib/lucas
  116. X    read lib/lucas_chk
  117. X    lucas_chk(200)        <- should produce no error messages
  118. X    exit
  119. X
  120. XIf you find bugs, or better yet have bug fixes; or if you have suggested
  121. Xchanges, or better yet have patches, send them to both myself and DBell:
  122. X
  123. X    chongo@toad.com            {uunet,pyramid,sun}!hoptoad!chongo
  124. X    dbell@pdact.pd.necisa.oz.au     {uunet,pyramid}!pdact.pd.necisa.oz.au!dbell
  125. X
  126. XThe file doc/todo section points out some needs for calc.  Suggestions
  127. Xon other enhancements, and help in doing these are welcome.
  128. END_OF_FILE
  129. if test 1357 -ne `wc -c <'README'`; then
  130.     echo shar: \"'README'\" unpacked with wrong size!
  131. fi
  132. # end of 'README'
  133. fi
  134. if test -f 'func.h' -a "${1}" != "-c" ; then 
  135.   echo shar: Will not clobber existing file \"'func.h'\"
  136. else
  137. echo shar: Extracting \"'func.h'\" \(2162 characters\)
  138. sed "s/^X//" >'func.h' <<'END_OF_FILE'
  139. X/*
  140. X * Copyright (c) 1992 David I. Bell
  141. X * Permission is granted to use, distribute, or modify this source,
  142. X * provided that this copyright notice remains intact.
  143. X */
  144. X
  145. X
  146. X/*
  147. X * Structure of a function.
  148. X * The f_opcodes array is actually of variable size.
  149. X */
  150. Xtypedef struct func FUNC;
  151. Xstruct func {
  152. X    FUNC *f_next;            /* next function in list */
  153. X    unsigned long f_opcodecount;    /* size of opcode array */
  154. X    unsigned int f_localcount;    /* number of local variables */
  155. X    unsigned int f_paramcount;    /* max number of parameters */
  156. X    char *f_name;            /* function name */
  157. X    VALUE f_savedvalue;        /* saved value of last expression */
  158. X    long f_opcodes[1];        /* array of opcodes (variable length) */
  159. X};
  160. X
  161. X
  162. X/*
  163. X * Amount of space needed to allocate a function of n opcodes.
  164. X */
  165. X#define funcsize(n) (sizeof(FUNC) + (n) * sizeof(long))
  166. X
  167. X
  168. X/*
  169. X * Size of a character pointer rounded up to a number of opcodes.
  170. X */
  171. X#define PTR_SIZE ((sizeof(char *) + sizeof(long) - 1) / sizeof(long))
  172. X
  173. X
  174. Xextern FUNC *curfunc;        /* current function being compiled */
  175. Xextern FUNC *findfunc();    /* return function given index */
  176. Xextern char *namefunc();    /* return function name given index */
  177. Xextern BOOL evaluate();        /* evaluate a line */
  178. Xextern long adduserfunc();
  179. Xextern void beginfunc();    /* initialize a function for definition */
  180. Xextern int builtinopcode();    /* return the opcode for a built-in function */
  181. Xextern void addop();        /* add an opcode to the current function */
  182. Xextern void endfunc();        /* commit the just defined function for use */
  183. Xextern void addopindex();    /* add an opcode & index to the current func */
  184. Xextern void addoplabel();    /* add jump-type opcode + label to the func */
  185. Xextern void addopptr();        /* add an opcode + char ptr to the func */
  186. Xextern void showbuiltins();    /* show list of primitive builtin funcs */
  187. Xextern int getbuiltinfunc();    /* return the index of a built-in func */
  188. Xextern void builtincheck();    /* determine if the # of arguments are legal */
  189. Xextern void addopfunction();    /* add opcode + index + arg count to the func */
  190. Xextern void showfunctions();    /* show the list of user defined functs */
  191. Xextern void clearopt();        /* clear optimization done for next opcode */
  192. X
  193. X/* END CODE */
  194. END_OF_FILE
  195. if test 2162 -ne `wc -c <'func.h'`; then
  196.     echo shar: \"'func.h'\" unpacked with wrong size!
  197. fi
  198. # end of 'func.h'
  199. fi
  200. if test ! -d 'help' ; then
  201.     echo shar: Creating directory \"'help'\"
  202.     mkdir 'help'
  203. fi
  204. if test -f 'help/Makefile' -a "${1}" != "-c" ; then 
  205.   echo shar: Will not clobber existing file \"'help/Makefile'\"
  206. else
  207. echo shar: Extracting \"'help/Makefile'\" \(1602 characters\)
  208. sed "s/^X//" >'help/Makefile' <<'END_OF_FILE'
  209. X#
  210. X# help - makefile for calc help files
  211. X#
  212. X# Copyright (c) 1992 David I. Bell and Landon Curt Noll
  213. X# Permission is granted to use, distribute, or modify this source,
  214. X# provided that this copyright notice remains intact.
  215. X#
  216. X# Arbitrary precision calculator.
  217. X#
  218. X# calculator by David I. Bell
  219. X# makefile by Landon Curt Noll
  220. X
  221. X# Normally, the upper level makefile will set these values.  We provide
  222. X# a default here just in case you want to build from this directory.
  223. X#
  224. X# where to install things
  225. XHELPDIR= /usr/local/lib/calc/help
  226. X# how to build a directory
  227. XMKDIR=mkdir -p
  228. X#MKDIR=mkdir
  229. X
  230. X# The header file, along with these files form the full help file.
  231. X#
  232. XFULL_HELP_FILES=intro command expression define variable statement \
  233. X    operator types obj mat list file builtin config interrupt \
  234. X    history usage credit
  235. X
  236. X# We install these help files
  237. X#
  238. XHELP_FILES= ${FULL_HELP_FILES} full overview stdlib environment todo credit help
  239. X
  240. XSHELL= /bin/sh
  241. X
  242. Xall: ${HELP_FILES}
  243. X
  244. Xfull: header ${FULL_HELP_FILES}
  245. X    rm -f full
  246. X    cp header full
  247. X    chmod +w full
  248. X    @for i in ${FULL_HELP_FILES}; do \
  249. X        echo '' >> full; \
  250. X        echo '' >> full; \
  251. X        echo "cat $$i >> full"; \
  252. X        cat $$i >> full; \
  253. X    done
  254. X    chmod 0444 full
  255. X
  256. Xstdlib: ../lib/README
  257. X    rm -f stdlib
  258. X    cp ../lib/README stdlib
  259. X    chmod 0444 stdlib
  260. X
  261. Xclean:
  262. X
  263. Xclobber:
  264. X    rm -f full stdlib
  265. X
  266. Xinstall: all
  267. X    -@if [ ! -d ${HELPDIR} ]; then \
  268. X        echo "    ${MKDIR} ${HELPDIR}"; \
  269. X        ${MKDIR} ${HELPDIR}; \
  270. X    fi
  271. X    @for i in ${HELP_FILES}; do \
  272. X        echo "    rm -f ${HELPDIR}/$$i"; \
  273. X        rm -f ${HELPDIR}/$$i; \
  274. X        echo "    chmod 0444 $$i"; \
  275. X        chmod 0444 $$i; \
  276. X        echo "    cp $$i ${HELPDIR}"; \
  277. X        cp $$i ${HELPDIR}; \
  278. X    done
  279. END_OF_FILE
  280. if test 1602 -ne `wc -c <'help/Makefile'`; then
  281.     echo shar: \"'help/Makefile'\" unpacked with wrong size!
  282. fi
  283. # end of 'help/Makefile'
  284. fi
  285. if test -f 'help/credit' -a "${1}" != "-c" ; then 
  286.   echo shar: Will not clobber existing file \"'help/credit'\"
  287. else
  288. echo shar: Extracting \"'help/credit'\" \(847 characters\)
  289. sed "s/^X//" >'help/credit' <<'END_OF_FILE'
  290. XCredits
  291. X
  292. X    Written by David I. Bell.
  293. X
  294. X    Thanks for suggestions and encouragement from Peter Miller,
  295. X    Neil Justusson, and Landon Noll.
  296. X
  297. X    Portions of this program are derived from an earlier set of
  298. X    public domain arbitrarily precision routines which was posted
  299. X    to the net around 1984.  By now, there is almost no recognizable 
  300. X    code left from that original source.
  301. X
  302. X    Most of this source and binary is:
  303. X
  304. X        Copyright (c) 1992 David I. Bell
  305. X
  306. X    A few files are a joint copyright between David I. Bell and Landon Noll.
  307. X
  308. X    Permission is granted to use, distribute, or modify this source,
  309. X    provided that this copyright notice remains intact.
  310. X
  311. X    Send calc comments, suggestions, bug fixes, enhancements and
  312. X    interesting calc scripts that you would like you see included in
  313. X    future distributions to:
  314. X
  315. X        dbell@pdact.pd.necisa.oz.au  and  chongo@toad.com
  316. X
  317. X    Enjoy!
  318. END_OF_FILE
  319. if test 847 -ne `wc -c <'help/credit'`; then
  320.     echo shar: \"'help/credit'\" unpacked with wrong size!
  321. fi
  322. # end of 'help/credit'
  323. fi
  324. if test -f 'help/environment' -a "${1}" != "-c" ; then 
  325.   echo shar: Will not clobber existing file \"'help/environment'\"
  326. else
  327. echo shar: Extracting \"'help/environment'\" \(1659 characters\)
  328. sed "s/^X//" >'help/environment' <<'END_OF_FILE'
  329. XEnvironment variables
  330. X
  331. X    CALCPATH
  332. X
  333. X        A :-separated list of directories used to search for
  334. X        scripts filenames that do not begin with /, ./ or ~.
  335. X
  336. X        If this variable does not exist, a compiled value
  337. X        is used.  Typically compiled in value is:
  338. X
  339. X            .:./lib:~/lib:${LIBDIR}/calc
  340. X    
  341. X        where ${LIBDIR} is usually:
  342. X
  343. X            /usr/local/lib/calc
  344. X
  345. X        This value is used by the READ command.  It is an error
  346. X        if no such readable file is found.
  347. X    
  348. X    
  349. X    CALCRC
  350. X
  351. X        On startup (unless -h or -q was given on the command
  352. X        line), calc searches for files along the :-separated
  353. X        $CALCRC environment variable.
  354. X
  355. X        If this variable does not exist, a compiled value
  356. X        is used.  Typically compiled in value is:
  357. X
  358. X            ${LIBDIR}/startup:~/.calcrc
  359. X    
  360. X        where ${LIBDIR} is usually:
  361. X
  362. X            /usr/local/lib/calc
  363. X
  364. X        Missing files along the $CALCRC path are silently ignored.
  365. X    
  366. X    HOME
  367. X
  368. X        This value is taken to be the home directory of the
  369. X        current user.  It is used when files begin with '~/'.
  370. X
  371. X        If this variable does not exist, the home directory password 
  372. X        entry of the current user is used.  If that information
  373. X        is not available, '.' is used.
  374. X    
  375. X    PAGER
  376. X
  377. X        When invoking help, this environment variable is used
  378. X        to display a help file.
  379. X
  380. X        If this variable does not exist, a compiled value
  381. X        is used.  Typically compiled in value is something
  382. X        such as 'more', 'less', 'pg' or 'cat'.
  383. X    
  384. X    SHELL
  385. X
  386. X        When a !-command is used, the program indicated by
  387. X        this environment variable is used.
  388. X
  389. X        If this variable does not exist, a compiled value
  390. X        is used.  Typically compiled in value is something
  391. X        such as 'sh' is used.
  392. END_OF_FILE
  393. if test 1659 -ne `wc -c <'help/environment'`; then
  394.     echo shar: \"'help/environment'\" unpacked with wrong size!
  395. fi
  396. # end of 'help/environment'
  397. fi
  398. if test -f 'help/expression' -a "${1}" != "-c" ; then 
  399.   echo shar: Will not clobber existing file \"'help/expression'\"
  400. else
  401. echo shar: Extracting \"'help/expression'\" \(1413 characters\)
  402. sed "s/^X//" >'help/expression' <<'END_OF_FILE'
  403. XExpression sequences
  404. X
  405. X    This is a sequence of statements, of which expression statements
  406. X    are the commonest case.  Statements are separated with semicolons,
  407. X    and the newline character generally ends the sequence.  If any
  408. X    statement is an expression by itself, or is associated with an
  409. X    'if' statement which is true, then two special things can happen.
  410. X    If the sequence is executed at the top level of the calculator,
  411. X    then the value of '.' is set to the value of the last expression.
  412. X    Also, if an expression is a non-assignment, then the value of the
  413. X    expression is automatically printed if its value is not NULL.
  414. X    Some operations such as    pre-increment and plus-equals are also
  415. X    treated as assignments.
  416. X
  417. X    Examples of this are the following:
  418. X
  419. X    expression        sets '.' to        prints
  420. X    ----------        -----------        ------
  421. X    3+4            7            7
  422. X    2*4; 8+1; fact(3)    6            8, 9, and 6
  423. X    x=3^2            9            -
  424. X    if (3 < 2) 5; else 6    6            6
  425. X    x++            old x            -
  426. X    print fact(4)        -            24
  427. X    null()            null()            -
  428. X
  429. X    Variables can be defined at the beginning of an expression sequence.
  430. X    This is most useful for local variables, as in the following example,
  431. X    which sums the square roots of the first few numbers:
  432. X
  433. X    local s, i; s = 0; for (i = 0; i < 10; i++) s += sqrt(i); s
  434. X
  435. X    If a return statement is executed in an expression sequence, then
  436. X    the result of the expression sequence is the returned value.  In
  437. X    this case, '.' is set to the value, but nothing is printed.
  438. END_OF_FILE
  439. if test 1413 -ne `wc -c <'help/expression'`; then
  440.     echo shar: \"'help/expression'\" unpacked with wrong size!
  441. fi
  442. # end of 'help/expression'
  443. fi
  444. if test -f 'help/header' -a "${1}" != "-c" ; then 
  445.   echo shar: Will not clobber existing file \"'help/header'\"
  446. else
  447. echo shar: Extracting \"'help/header'\" \(64 characters\)
  448. sed "s/^X//" >'help/header' <<'END_OF_FILE'
  449. X        CALC - An arbitrary precision calculator.
  450. X            by David I. Bell
  451. END_OF_FILE
  452. if test 64 -ne `wc -c <'help/header'`; then
  453.     echo shar: \"'help/header'\" unpacked with wrong size!
  454. fi
  455. # end of 'help/header'
  456. fi
  457. if test -f 'help/help' -a "${1}" != "-c" ; then 
  458.   echo shar: Will not clobber existing file \"'help/help'\"
  459. else
  460. echo shar: Extracting \"'help/help'\" \(1210 characters\)
  461. sed "s/^X//" >'help/help' <<'END_OF_FILE'
  462. XFor more information while running calc, type  help  followed by one of the
  463. Xfollowing topics:
  464. X
  465. X    topic        description
  466. X    -----        -----------
  467. X    intro        a brief introduction to calc
  468. X    command        top level commands
  469. X    expression    expression sequences
  470. X    define        how to define functions
  471. X    variable    variables and variable declarations
  472. X    statement    flow control and declaration statements
  473. X    operator    math, relational, logic and variable access operators
  474. X    types        builtin data types
  475. X    obj        user defined data types
  476. X    mat        using matrices
  477. X    list        using lists
  478. X    file        using files
  479. X    builtin        builtin functions
  480. X    config        configuration parameters
  481. X    interrupt    how interrupts are handled
  482. X    history        command history
  483. X    usage        how to invoke the calc command
  484. X    todo        needed enhancements
  485. X    credit        who wrote calc, and who helped, copyright
  486. X    full        all of the above topics
  487. X    help        this file
  488. X    overview    brief overview of calc
  489. X    stdlib        description of some lib files shipped with calc
  490. X    environment    how environment variables effect calc
  491. X
  492. XFor example:
  493. X
  494. X    help usage
  495. X
  496. Xwill print the calc command usage information.  One can obtain calc help
  497. Xwithout invoking any startup code by running calc as follows:
  498. X
  499. X    calc -q help topic
  500. X
  501. Xwhere 'topic' is one of the topics listed above.
  502. END_OF_FILE
  503. if test 1210 -ne `wc -c <'help/help'`; then
  504.     echo shar: \"'help/help'\" unpacked with wrong size!
  505. fi
  506. # end of 'help/help'
  507. fi
  508. if test -f 'help/history' -a "${1}" != "-c" ; then 
  509.   echo shar: Will not clobber existing file \"'help/history'\"
  510. else
  511. echo shar: Extracting \"'help/history'\" \(1019 characters\)
  512. sed "s/^X//" >'help/history' <<'END_OF_FILE'
  513. XCommand history
  514. X
  515. X    There is a crude command history feature in the calculator.
  516. X    If a terminal line begins with an exclamation mark or back quote
  517. X    character (! or `), then the terminal line is handled specially.
  518. X
  519. X    The command just typed can be re-executed by typing the
  520. X    line '``' (two back quotes).
  521. X
  522. X    Other previous commands can be executed again by typing '`nn'
  523. X    (backquote <number>), where nn is the command number to be 
  524. X    ex-executed.  This number is displayed in the prompt for each 
  525. X    input line, so it is easy to re-execute commands that are still 
  526. X    visible on the screen.  Negative numbers can be used to re-execute 
  527. X    the n'th command back.
  528. X
  529. X    The list of 20 previous commands can be displayed by typing '`h'
  530. X    (backquote h).  By typing '`hnn' (backquote h <number>), one
  531. X    can ex-execute the last 'nn' commands.
  532. X
  533. X    Up to 255 commands are saved in the history stack.
  534. X
  535. X    A UNIX command can be executed by typing '!cmd', where cmd
  536. X    is the command to execute.  If cmd is not given, then a shell
  537. X    command level is started.
  538. END_OF_FILE
  539. if test 1019 -ne `wc -c <'help/history'`; then
  540.     echo shar: \"'help/history'\" unpacked with wrong size!
  541. fi
  542. # end of 'help/history'
  543. fi
  544. if test -f 'help/interrupt' -a "${1}" != "-c" ; then 
  545.   echo shar: Will not clobber existing file \"'help/interrupt'\"
  546. else
  547. echo shar: Extracting \"'help/interrupt'\" \(1306 characters\)
  548. sed "s/^X//" >'help/interrupt' <<'END_OF_FILE'
  549. XInterrupts
  550. X
  551. X    While a calculation is in progress, you can generate the SIGINT
  552. X    signal, and the calculator will catch it.  At appropriate points
  553. X    within a calculation, the calculator will check that the signal
  554. X    has been given, and will abort the calculation cleanly.  If the
  555. X    calculator is in the middle of a large calculation, it might be
  556. X    a while before the interrupt has an effect.
  557. X
  558. X    You can generate the SIGINT signal multiple times if necessary,
  559. X    and each time the calculator will abort the calculation at a more
  560. X    risky place within the calculation.  Each new interrupt prints a
  561. X    message of the form:
  562. X
  563. X        [Abort level n]
  564. X
  565. X    where n ranges from 1 to 3.  For n equal to 1, the calculator will
  566. X    abort calculations at the next statement boundary.  For n equal to 2,
  567. X    the calculator will abort calculations at the next opcode boundary.
  568. X    For n equal to 3, the calculator will abort calculations at the next
  569. X    lowest level arithmetic operation boundary.
  570. X
  571. X    If a final interrupt is given when n is 3, the calculator will
  572. X    immediately abort the current calculation and longjmp back to the
  573. X    top level command level.  Doing this may result in corrupted data
  574. X    structures and unpredictable future behavior, and so should only
  575. X    be done as a last resort.  You are advised to quit the calculator
  576. X    after this has been done.
  577. END_OF_FILE
  578. if test 1306 -ne `wc -c <'help/interrupt'`; then
  579.     echo shar: \"'help/interrupt'\" unpacked with wrong size!
  580. fi
  581. # end of 'help/interrupt'
  582. fi
  583. if test -f 'help/intro' -a "${1}" != "-c" ; then 
  584.   echo shar: Will not clobber existing file \"'help/intro'\"
  585. else
  586. echo shar: Extracting \"'help/intro'\" \(1895 characters\)
  587. sed "s/^X//" >'help/intro' <<'END_OF_FILE'
  588. XQuick introduction
  589. X
  590. X    This is an interactive calculator which provides for easy large
  591. X    numeric calculations, but which also can be easily programmed
  592. X    for difficult or long calculations.  It can accept a command line
  593. X    argument, in which case it executes that single command and exits.
  594. X    Otherwise, it enters interactive mode.  In this mode, it accepts
  595. X    commands one at a time, processes them, and displays the answers.
  596. X    In the simplest case, commands are simply expressions which are
  597. X    evaluated.  For example, the following line can be input:
  598. X
  599. X        3 * (4 + 1)
  600. X
  601. X    and the calculator will print 15.
  602. X
  603. X    The special '.' symbol (called dot), represents the result of the
  604. X    last command expression, if any.  This is of great use when a series
  605. X    of partial results are calculated, or when the output mode is changed
  606. X    and the last result needs to be redisplayed.  For example, the above
  607. X    result can be doubled by typing:
  608. X
  609. X        . * 2
  610. X
  611. X    and the calculator will print 30.
  612. X
  613. X    For more complex calculations, variables can be used to save the
  614. X    intermediate results.  For example, the result of adding 7 to the
  615. X    previous result can be saved by typing:
  616. X
  617. X        old = . + 7
  618. X
  619. X    Functions can be used in expressions.  There are a great number of
  620. X    pre-defined functions.  For example, the following will calculate
  621. X    the factorial of the value of 'old':
  622. X
  623. X        fact(old)
  624. X
  625. X    and the calculator prints 13763753091226345046315979581580902400000000.
  626. X    Notice that numbers can be very large. (There is a practical limit
  627. X    of several thousand digits before calculations become too slow.)
  628. X
  629. X    The calculator can calculate transcendental functions, and accept and
  630. X    display numbers in real or exponential format. For example, typing:
  631. X
  632. X        config("display", 50)
  633. X        epsilon(1e-50)
  634. X        sin(1)
  635. X
  636. X    prints "~.84147098480789650665250232163029899962256306079837".
  637. X
  638. X    The calculator also knows about complex numbers, so that typing:
  639. X
  640. X        (2+3i) * (4-3i)
  641. X
  642. X    prints "17+6i".
  643. END_OF_FILE
  644. if test 1895 -ne `wc -c <'help/intro'`; then
  645.     echo shar: \"'help/intro'\" unpacked with wrong size!
  646. fi
  647. # end of 'help/intro'
  648. fi
  649. if test -f 'help/list' -a "${1}" != "-c" ; then 
  650.   echo shar: Will not clobber existing file \"'help/list'\"
  651. else
  652. echo shar: Extracting \"'help/list'\" \(1880 characters\)
  653. sed "s/^X//" >'help/list' <<'END_OF_FILE'
  654. XUsing lists
  655. X
  656. X    Lists are a sequence of values which are doubly linked so that
  657. X    elements can be removed or inserted anywhere within the list.
  658. X    The function 'list' creates a list with possible initial elements.
  659. X    For example,
  660. X
  661. X        x = list(4, 6, 7);
  662. X
  663. X    creates a list in the variable x of three elements, in the order
  664. X    4, 6, and 7.
  665. X
  666. X    The 'push' and 'pop' functions insert or remove an element from
  667. X    the beginning of the list.  The 'append' and 'remove' functions
  668. X    insert or remove an element from the end of the list.  The 'insert'
  669. X    and 'delete' functions insert or delete an element from the middle
  670. X    (or ends) of a list.  The functions which insert elements return
  671. X    the null value, but the functions which remove an element return
  672. X    the element as their value.  The 'size' function returns the number
  673. X    of elements in the list.
  674. X
  675. X    Note that these functions manipulate the actual list argument,
  676. X    instead of returning a new list.  Thus in the example:
  677. X
  678. X        push(x, 9);
  679. X
  680. X    x becomes a list of four elements, in the order 9, 4, 6, and 7.
  681. X    Lists can be copied by assigning them to another variable.
  682. X
  683. X    An arbitrary element of a linked list can be accessed by using the
  684. X    double-bracket operator.  The beginning of the list has index 0.
  685. X    Thus in the new list x above, the expression x[[0]] returns the
  686. X    value of the first element of the list, which is 9.  Note that this
  687. X    indexing does not remove elements from the list.
  688. X
  689. X    Since lists are doubly linked in memory, random access to arbitrary
  690. X    elements can be slow if the list is large.  However, for each list
  691. X    a pointer is kept to the latest indexed element, thus relatively
  692. X    sequential accesses to the elements in a list will not be slow.
  693. X
  694. X    Lists can be searched for particular values by using the 'search'
  695. X    and 'rsearch' functions.  They return the element number of the
  696. X    found value (zero based), or null if the value does not exist in
  697. X    the list.
  698. END_OF_FILE
  699. if test 1880 -ne `wc -c <'help/list'`; then
  700.     echo shar: \"'help/list'\" unpacked with wrong size!
  701. fi
  702. # end of 'help/list'
  703. fi
  704. if test -f 'help/operator' -a "${1}" != "-c" ; then 
  705.   echo shar: Will not clobber existing file \"'help/operator'\"
  706. else
  707. echo shar: Extracting \"'help/operator'\" \(2549 characters\)
  708. sed "s/^X//" >'help/operator' <<'END_OF_FILE'
  709. XOperators
  710. X
  711. X    The operators are similar to C, but the precedence of most of
  712. X    the operators differs.  In addition, there are several additional
  713. X    operators, and some C operators are missing.  The following list
  714. X    gives the operators arranged in order of precedence, from the
  715. X    least tightly binding to the most tightly binding.
  716. X
  717. X
  718. X    ,    Comma operator.
  719. X        For situations in which a comma is used for another purpose
  720. X        (function arguments, array indexing, and the print statement),
  721. X        parenthesis must be used around the comma operator.
  722. X
  723. X    a?:b:c    Conditional value.
  724. X        The test for 'a' is identical to an if test.
  725. X
  726. X    =  +=  -=  *=  /=  %=  //=  &=  |=  <<=  >>=  ^=  **=
  727. X        Assignments.
  728. X
  729. X    ||    Conditional OR.
  730. X        Unlike C, the result is the first non-zero expression or 0,
  731. X        instead of just 0 or 1.
  732. X
  733. X    &&    Conditional AND.
  734. X        Unlike C, the result is the last expression or 0,
  735. X        instead of just 0 or 1.
  736. X
  737. X    ==  !=  <=  >=  <  >
  738. X        Relations.
  739. X
  740. X    +  -
  741. X        Binary plus and minus.
  742. X
  743. X    *  /  //  %
  744. X        Multiply, divide. and modulo.
  745. X        Please Note: The '/' operator is a fractional divide,
  746. X        whereas the '//' is an integral divide.  Thus think of '/'
  747. X        as division of real numbers, and think of '//' as division
  748. X        of integers (e.g., 8 / 3 is 8/3 whereas 8 // 3 is 2).
  749. X        The '%' is integral or fractional modulus (e.g., 11%4 is 3,
  750. X        and 10%pi() is ~.575222).
  751. X
  752. X    |    Logical OR.
  753. X        The signs of numbers do not affect the bit value.
  754. X
  755. X    &    Logical AND.
  756. X        The signs of numbers do not affect the bit value.
  757. X
  758. X    ^  **  <<  >>
  759. X        Powers and shifts.
  760. X        The '^' and '**' are both exponentiation (e.g., 2^3 is 8).
  761. X        The signs of numbers do not affect the bit values of shifts.
  762. X        These operators associate rightward (e.g., 1<<3^2 is 512).
  763. X
  764. X    +  -  !
  765. X        Unary operators.
  766. X        The '!' is the logical NOT operator.  Be careful about
  767. X        using this as the first character of a top level command,
  768. X        since it is also used for executing UNIX commands.
  769. X
  770. X    ++  --
  771. X        Pre or post indexing.
  772. X        These are applicable only to variables.
  773. X
  774. X    [ ]  [[ ]]  .  ( )
  775. X        Indexing, double-bracket indexing, element references,
  776. X        and function calls.  Indexing can only be applied to matrices,
  777. X        element references can only be applied to objects, but
  778. X        double-bracket indexing can be applied to matrices, objects,
  779. X        or lists.
  780. X
  781. X    variables  constants  .  ( )
  782. X        These are variable names and constants, the special '.' symbol,
  783. X        or a parenthesized expression.  Variable names begin with a
  784. X        letter, but then can contain letters, digits, or underscores.
  785. X        Constants are numbers in various formats, or strings inside
  786. X        either single or double quote marks.
  787. END_OF_FILE
  788. if test 2549 -ne `wc -c <'help/operator'`; then
  789.     echo shar: \"'help/operator'\" unpacked with wrong size!
  790. fi
  791. # end of 'help/operator'
  792. fi
  793. if test -f 'help/sums' -a "${1}" != "-c" ; then 
  794.   echo shar: Will not clobber existing file \"'help/sums'\"
  795. else
  796. echo shar: Extracting \"'help/sums'\" \(361 characters\)
  797. sed "s/^X//" >'help/sums' <<'END_OF_FILE'
  798. X58784 4 Makefile
  799. X34745 25 builtin
  800. X43439 6 command
  801. X641 9 config
  802. X9511 2 credit
  803. X37950 6 define
  804. X3174 4 environment
  805. X52378 3 expression
  806. X46486 15 file
  807. X42777 3 help
  808. X23416 2 history
  809. X51967 3 interrupt
  810. X30991 4 intro
  811. X35089 4 list
  812. X50373 9 mat
  813. X43038 14 obj
  814. X13133 5 operator
  815. X12056 14 overview
  816. X29205 17 statement
  817. X0 0 sums
  818. X1418 5 todo
  819. X129 8 types
  820. X47095 2 usage
  821. X19801 5 variable
  822. END_OF_FILE
  823. if test 361 -ne `wc -c <'help/sums'`; then
  824.     echo shar: \"'help/sums'\" unpacked with wrong size!
  825. fi
  826. # end of 'help/sums'
  827. fi
  828. if test -f 'help/todo' -a "${1}" != "-c" ; then 
  829.   echo shar: Will not clobber existing file \"'help/todo'\"
  830. else
  831. echo shar: Extracting \"'help/todo'\" \(2320 characters\)
  832. sed "s/^X//" >'help/todo' <<'END_OF_FILE'
  833. XNeeded enhancements
  834. X
  835. X    Send calc comments, suggestions, bug fixes, enhancements and
  836. X    interesting calc scripts that you would like you see included in
  837. X    future distributions to:
  838. X
  839. X        dbell@pdact.pd.necisa.oz.au  and  chongo@toad.com
  840. X
  841. X    The following items are in the calc wish list.  Programs like
  842. X    this can be extended and improved forever.  The following is a 
  843. X    list of changes which might be done someday.
  844. X
  845. X    *  Use faster multiply and divide algorithms for large numbers.
  846. X
  847. X    *  Add error handling statements, so that QUITs, errors from the 
  848. X       'eval' function, division by zeroes, and so on can be caught.
  849. X       This should be done using syntax similar to:
  850. X
  851. X            ONERROR statement DO statement;
  852. X
  853. X       Something like signal isn't versatile enough.
  854. X
  855. X    *  Add a debugging capability so that functions can be single stepped,
  856. X       breakpoints inserted, variables displayed, and so on.
  857. X
  858. X    *  Figure out how to write all variables out to a file, including
  859. X       deeply nested arrays, lists, and objects.
  860. X
  861. X    *  Make the command history useful:
  862. X
  863. X            allowing editing of old commands
  864. X            enable history to also re-fetch the results of old 
  865. X                commands, not just re-executing them
  866. X            allow the inclusion of old commands or values of old
  867. X               commands inside expressions.
  868. X
  869. X    *  Add a "ksh-like" history editor (both vi and emacs mode).
  870. X
  871. X    *  Implement pointers.
  872. X
  873. X    *  Add initializers for matrices.  Perhaps allow something line:
  874. X
  875. X        mat yummo[] = {2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107};
  876. X
  877. X    *  Eliminate the need for the define keyword by doing smarter parsing.
  878. X
  879. X    *  Allow results of a command (or all commands) to be re-directed to a 
  880. X       file or piped into a unix command.
  881. X
  882. X    *  Add some kind of #include and #define facility.  Perhaps use
  883. X       the C pre-processor itself?
  884. X
  885. X    *  Implement an autoload feature.  Associate a calc library filename
  886. X       with a function, object, matrix or variable.  On the 1st reference
  887. X       of such item, perform an automatic load of that file.
  888. X
  889. X    *  Allow one to undefine anything.  Allow one to test of anything
  890. X       is defined.
  891. X
  892. X    *  Support a more general input and output base mode and just dec, 
  893. X       hex or octal.
  894. X
  895. X    *  Allow support of POSIX bc via a translator reads bc commands, 
  896. X       converts it to calc and pipes it into calc.
  897. X
  898. X    *  Implement symbolic algebra, such as polynomial manipulation.
  899. END_OF_FILE
  900. if test 2320 -ne `wc -c <'help/todo'`; then
  901.     echo shar: \"'help/todo'\" unpacked with wrong size!
  902. fi
  903. # end of 'help/todo'
  904. fi
  905. if test -f 'help/usage' -a "${1}" != "-c" ; then 
  906.   echo shar: Will not clobber existing file \"'help/usage'\"
  907. else
  908. echo shar: Extracting \"'help/usage'\" \(551 characters\)
  909. sed "s/^X//" >'help/usage' <<'END_OF_FILE'
  910. XCalc command line
  911. X
  912. X    Calc has the following command line:
  913. X
  914. X        calc [-h] [-q] [calc_command ...]
  915. X
  916. X        -h    print a help message  (equivalent to the
  917. X            help command)
  918. X
  919. X        -q    By default, calc executes each file specified
  920. X            in the :-separated list found in the environment
  921. X            variable $CALCRC.  If $CALCRC does not exist,
  922. X            an internal default is used.
  923. X
  924. X    If some calc_commands arguments are given on the command line,
  925. X    calc executes these commands and then exists.  If no command
  926. X    line arguments are given, calc prompts and reads commands
  927. X    from standard input.
  928. END_OF_FILE
  929. if test 551 -ne `wc -c <'help/usage'`; then
  930.     echo shar: \"'help/usage'\" unpacked with wrong size!
  931. fi
  932. # end of 'help/usage'
  933. fi
  934. if test -f 'help/variable' -a "${1}" != "-c" ; then 
  935.   echo shar: Will not clobber existing file \"'help/variable'\"
  936. else
  937. echo shar: Extracting \"'help/variable'\" \(2423 characters\)
  938. sed "s/^X//" >'help/variable' <<'END_OF_FILE'
  939. XVariable declarations
  940. X
  941. X    Variables can be declared as either being global or local.
  942. X    Global variables are visible to all functions and on the command
  943. X    line.  Local variables are visible only within a single function or
  944. X    command sequence.  When the function or command sequence returns,
  945. X    the local variables are deleted.
  946. X
  947. X    To declare one or more variables, the 'local' or 'global' keywords
  948. X    are used, followed by the desired list of variable names, separated
  949. X    by commas.  The definition is terminated with a semicolon.  Examples
  950. X    of declarations are:
  951. X
  952. X        local    x, y, z;
  953. X        global    fred;
  954. X        local    foo, bar;
  955. X
  956. X    Within function declarations, all variables must be defined.
  957. X    But on the top level command line, assignments automatically define
  958. X    global variables as needed.  For example, on the top level command
  959. X    line, the following defines the global variable x if it had not
  960. X    already been defined:
  961. X
  962. X        x = 7
  963. X
  964. X    Variables have no fixed type, thus there is no need or way to
  965. X    specify the types of variables as they are defined.  Instead, the
  966. X    types of variables change as they are assigned to or are specified
  967. X    in special statements such as 'mat' and 'obj'.  When a variable is
  968. X    first defined using 'local' or 'global', it has the null type.
  969. X
  970. X    If a procedure defines a local variable name which matches a
  971. X    global variable name, or has a parameter name which matches a
  972. X    global variable name, then the local variable or parameter takes
  973. X    precedence within that procedure, and the global variable is not
  974. X    directly accessible.
  975. X
  976. X    There are no pointers in the calculator language, thus all
  977. X    arguments to user-defined functions are normally passed by value.
  978. X    This is true even for matrices, strings, and lists.  In order
  979. X    to circumvent this, the '&' operator is allowed before a variable
  980. X    when it is an argument to a function.  When this is done, the
  981. X    address of the variable is passed to the function instead of its
  982. X    value.  This is true no matter what the type of the variable is.
  983. X    This allows for fast calls of functions when the passed variable
  984. X    is huge (such as a large array).  However, the passed variable can
  985. X    then be changed by the function if the parameter is assigned into.
  986. X    The function being called does not need to know if the variable
  987. X    is being passed by value or by address.
  988. X
  989. X    Built-in functions and object functions always accept their
  990. X    arguments as addresses, thus there is no need to use '&' when
  991. X    calling built-in functions.
  992. END_OF_FILE
  993. if test 2423 -ne `wc -c <'help/variable'`; then
  994.     echo shar: \"'help/variable'\" unpacked with wrong size!
  995. fi
  996. # end of 'help/variable'
  997. fi
  998. if test -f 'label.h' -a "${1}" != "-c" ; then 
  999.   echo shar: Will not clobber existing file \"'label.h'\"
  1000. else
  1001. echo shar: Extracting \"'label.h'\" \(534 characters\)
  1002. sed "s/^X//" >'label.h' <<'END_OF_FILE'
  1003. X/*
  1004. X * Copyright (c) 1992 David I. Bell
  1005. X * Permission is granted to use, distribute, or modify this source,
  1006. X * provided that this copyright notice remains intact.
  1007. X */
  1008. X
  1009. X
  1010. X/*
  1011. X * Label structures.
  1012. X */
  1013. Xtypedef struct {
  1014. X    long l_offset;          /* offset into code of label */
  1015. X    long l_chain;          /* offset into code of undefined chain */
  1016. X    char *l_name;          /* name of label if any */
  1017. X} LABEL;
  1018. X
  1019. Xextern void initlabels(), definelabel(), addlabel();
  1020. Xextern void checklabels(), clearlabel(), setlabel();
  1021. Xextern void uselabel();
  1022. X
  1023. X/* END CODE */
  1024. END_OF_FILE
  1025. if test 534 -ne `wc -c <'label.h'`; then
  1026.     echo shar: \"'label.h'\" unpacked with wrong size!
  1027. fi
  1028. # end of 'label.h'
  1029. fi
  1030. if test ! -d 'lib' ; then
  1031.     echo shar: Creating directory \"'lib'\"
  1032.     mkdir 'lib'
  1033. fi
  1034. if test -f 'lib/Makefile' -a "${1}" != "-c" ; then 
  1035.   echo shar: Will not clobber existing file \"'lib/Makefile'\"
  1036. else
  1037. echo shar: Extracting \"'lib/Makefile'\" \(1215 characters\)
  1038. sed "s/^X//" >'lib/Makefile' <<'END_OF_FILE'
  1039. X#
  1040. X# lib - makefile for calc library scripts
  1041. X#
  1042. X# Copyright (c) 1992 David I. Bell and Landon Curt Noll
  1043. X# Permission is granted to use, distribute, or modify this source,
  1044. X# provided that this copyright notice remains intact.
  1045. X#
  1046. X# Arbitrary precision calculator.
  1047. X#
  1048. X# calculator by David I. Bell
  1049. X# makefile by Landon Curt Noll
  1050. X
  1051. X# Normally, the upper level makefile will set these values.  We provide
  1052. X# a default here just in case you want to build from this directory.
  1053. X#
  1054. X# where to install things
  1055. XLIBDIR= /usr/local/lib/calc
  1056. X# how to build a directory
  1057. XMKDIR=mkdir -p
  1058. X#MKDIR=mkdir
  1059. X
  1060. X# The calc files to install
  1061. X#
  1062. XCALC_FILES= README bigprime.cal deg.cal ellip.cal lucas.cal lucas_chk.cal \
  1063. X    lucas_tbl.cal mersenne.cal mod.cal nextprim.cal pell.cal pi.cal \
  1064. X    pollard.cal poly.cal psqrt.cal quat.cal regress.cal solve.cal \
  1065. X    sumsq.cal surd.cal unitfrac.cal varargs.cal
  1066. X
  1067. XSHELL= /bin/sh
  1068. X
  1069. Xall: ${CALC_FILES}
  1070. X
  1071. Xclean:
  1072. X
  1073. Xclobber:
  1074. X
  1075. Xinstall: all
  1076. X    -@if [ ! -d ${LIBDIR} ]; then \
  1077. X        echo "    ${MKDIR} ${LIBDIR}"; \
  1078. X        ${MKDIR} ${LIBDIR}; \
  1079. X    fi
  1080. X    @for i in ${CALC_FILES}; do \
  1081. X        echo "    rm -f ${LIBDIR}/$$i"; \
  1082. X        rm -f ${LIBDIR}/$$i; \
  1083. X        echo "    chmod 0444 $$i"; \
  1084. X        chmod 0444 $$i; \
  1085. X        echo "    cp $$i ${LIBDIR}"; \
  1086. X        cp $$i ${LIBDIR}; \
  1087. X    done
  1088. END_OF_FILE
  1089. if test 1215 -ne `wc -c <'lib/Makefile'`; then
  1090.     echo shar: \"'lib/Makefile'\" unpacked with wrong size!
  1091. fi
  1092. # end of 'lib/Makefile'
  1093. fi
  1094. if test -f 'lib/bernoulli.cal' -a "${1}" != "-c" ; then 
  1095.   echo shar: Will not clobber existing file \"'lib/bernoulli.cal'\"
  1096. else
  1097. echo shar: Extracting \"'lib/bernoulli.cal'\" \(1454 characters\)
  1098. sed "s/^X//" >'lib/bernoulli.cal' <<'END_OF_FILE'
  1099. X/*
  1100. X * Copyright (c) 1992 David I. Bell
  1101. X * Calculate the Nth Bernoulli number B(n).
  1102. X * This uses the following symbolic formula to calculate B(n):
  1103. X *
  1104. X *    (b+1)^(n+1) - b^(n+1) = 0
  1105. X *
  1106. X * where b is a dummy value, and each power b^i gets replaced by B(i).
  1107. X * For example, for n = 3:
  1108. X *    (b+1)^4 - b^4 = 0
  1109. X *    b^4 + 4*b^3 + 6*b^2 + 4*b + 1 - b^4 = 0
  1110. X *    4*b^3 + 6*b^2 + 4*b + 1 = 0
  1111. X *    4*B(3) + 6*B(2) + 4*B(1) + 1 = 0
  1112. X *    B(3) = -(6*B(2) + 4*B(1) + 1) / 4
  1113. X *
  1114. X * The combinatorial factors in the expansion of the above formula are
  1115. X * calculated interatively, and we use the fact that B(2i+1) = 0 if i > 0.
  1116. X * Since all previous B(n)'s are needed to calculate a particular B(n), all
  1117. X * values obtained are saved in an array for ease in repeated calculations.
  1118. X */
  1119. Xglobal Bn, Bnmax;
  1120. Xmat Bn[1001];
  1121. XBnmax = 0;
  1122. X
  1123. X
  1124. Xdefine B(n)
  1125. X{
  1126. X    local    nn, np1, i, sum, mulval, divval, combval;
  1127. X
  1128. X    if (!isint(n) || (n < 0))
  1129. X        quit "Non-negative integer required for Bernoulli";
  1130. X
  1131. X    if (n == 0)
  1132. X        return 1;
  1133. X    if (n == 1)
  1134. X        return -1/2;
  1135. X    if (isodd(n))
  1136. X        return 0;
  1137. X    if (n > 1000)
  1138. X        quit "Very large Bernoulli";
  1139. X
  1140. X    if (n <= Bnmax)
  1141. X        return Bn[n];
  1142. X
  1143. X    for (nn = Bnmax + 2; nn <= n; nn+=2) {
  1144. X        np1 = nn + 1;
  1145. X        mulval = np1;
  1146. X        divval = 1;
  1147. X        combval = 1;
  1148. X        sum = 1 - np1 / 2;
  1149. X        for (i = 2; i < np1; i+=2) {
  1150. X            combval = combval * mulval-- / divval++;
  1151. X            combval = combval * mulval-- / divval++;
  1152. X            sum += combval * Bn[i];
  1153. X        }
  1154. X        Bn[nn] = -sum / np1;
  1155. X    }
  1156. X    Bnmax = n;
  1157. X    return Bn[n];
  1158. X}
  1159. X
  1160. Xprint 'B(n) defined';
  1161. END_OF_FILE
  1162. if test 1454 -ne `wc -c <'lib/bernoulli.cal'`; then
  1163.     echo shar: \"'lib/bernoulli.cal'\" unpacked with wrong size!
  1164. fi
  1165. # end of 'lib/bernoulli.cal'
  1166. fi
  1167. if test -f 'lib/bigprime.cal' -a "${1}" != "-c" ; then 
  1168.   echo shar: Will not clobber existing file \"'lib/bigprime.cal'\"
  1169. else
  1170. echo shar: Extracting \"'lib/bigprime.cal'\" \(565 characters\)
  1171. sed "s/^X//" >'lib/bigprime.cal' <<'END_OF_FILE'
  1172. X/*
  1173. X * Copyright (c) 1992 David I. Bell
  1174. X * Permission is granted to use, distribute, or modify this source,
  1175. X * provided that this copyright notice remains intact.
  1176. X *
  1177. X * A prime test, base a, on p*2^x+1 for even x>m.
  1178. X */
  1179. X
  1180. Xdefine bigprime(a, m, p)
  1181. X{
  1182. X    local n1, n;
  1183. X
  1184. X    n1 = 2^m * p;
  1185. X    for (;;) {
  1186. X        m++;
  1187. X        n1 += n1;
  1188. X        n = n1 + 1;
  1189. X        if (isodd(m))
  1190. X            continue;
  1191. X        print m;
  1192. X        if (pmod(a, n1 / 2, n) != n1)
  1193. X            continue;
  1194. X        if (pmod(a, n1 / p, n) == 1)
  1195. X            continue;
  1196. X        print "    " : n;
  1197. X    }
  1198. X}
  1199. X
  1200. Xglobal lib_debug;
  1201. Xif (!isnum(lib_debug) || lib_debug>0) print "bigprime(a, m, p) defined";
  1202. END_OF_FILE
  1203. if test 565 -ne `wc -c <'lib/bigprime.cal'`; then
  1204.     echo shar: \"'lib/bigprime.cal'\" unpacked with wrong size!
  1205. fi
  1206. # end of 'lib/bigprime.cal'
  1207. fi
  1208. if test -f 'lib/deg.cal' -a "${1}" != "-c" ; then 
  1209.   echo shar: Will not clobber existing file \"'lib/deg.cal'\"
  1210. else
  1211. echo shar: Extracting \"'lib/deg.cal'\" \(2204 characters\)
  1212. sed "s/^X//" >'lib/deg.cal' <<'END_OF_FILE'
  1213. X/*
  1214. X * Copyright (c) 1992 David I. Bell
  1215. X * Permission is granted to use, distribute, or modify this source,
  1216. X * provided that this copyright notice remains intact.
  1217. X *
  1218. X * Calculate in degrees, minutes, and seconds.
  1219. X */
  1220. X
  1221. Xobj dms {deg, min, sec};
  1222. X
  1223. Xdefine dms(deg, min, sec)
  1224. X{
  1225. X    local ans;
  1226. X
  1227. X    if (isnull(sec))
  1228. X        sec = 0;
  1229. X    if (isnull(min))
  1230. X        min = 0;
  1231. X    obj dms ans;
  1232. X    ans.deg = deg;
  1233. X    ans.min = min;
  1234. X    ans.sec = sec;
  1235. X    fixdms(&ans);
  1236. X    return ans;
  1237. X}
  1238. X
  1239. X
  1240. Xdefine dms_add(a, b)
  1241. X{
  1242. X    local ans;
  1243. X
  1244. X    obj dms    ans;
  1245. X    ans.deg = 0;
  1246. X    ans.min = 0;
  1247. X    ans.sec = 0;
  1248. X    if (istype(a, ans)) {
  1249. X        ans.deg += a.deg;
  1250. X        ans.min += a.min;
  1251. X        ans.sec += a.sec;
  1252. X    } else
  1253. X        ans.deg += a;
  1254. X    if (istype(b, ans)) {
  1255. X        ans.deg += b.deg;
  1256. X        ans.min += b.min;
  1257. X        ans.sec += b.sec;
  1258. X    } else
  1259. X        ans.deg += b;
  1260. X    fixdms(&ans);
  1261. X    return ans;    
  1262. X}
  1263. X
  1264. X
  1265. Xdefine dms_neg(a)
  1266. X{
  1267. X    local ans;
  1268. X
  1269. X    obj dms ans;
  1270. X    ans.deg = -ans.deg;
  1271. X    ans.min = -ans.min;
  1272. X    ans.sec = -ans.sec;
  1273. X    return ans;
  1274. X}
  1275. X
  1276. X
  1277. Xdefine dms_sub(a, b)
  1278. X{
  1279. X    return a - b;
  1280. X}
  1281. X
  1282. X
  1283. Xdefine dms_mul(a, b)
  1284. X{
  1285. X    local ans;
  1286. X
  1287. X    obj dms ans;
  1288. X    if (istype(a, ans) && istype(b, ans))
  1289. X        quit "Cannot multiply degrees together";
  1290. X    if (istype(a, ans)) {
  1291. X        ans.deg = a.deg * b;
  1292. X        ans.min = a.min * b;
  1293. X        ans.sec = a.sec * b;
  1294. X    } else {
  1295. X        ans.deg = b.deg * a;
  1296. X        ans.min = b.min * a;
  1297. X        ans.sec = b.sec * a;
  1298. X    }
  1299. X    fixdms(&ans);
  1300. X    return ans;
  1301. X}
  1302. X
  1303. X
  1304. Xdefine dms_print(a)
  1305. X{
  1306. X    print a.deg : 'd' : a.min : 'm' : a.sec : 's' :;
  1307. X}
  1308. X
  1309. X
  1310. Xdefine dms_abs(a)
  1311. X{
  1312. X    return a.deg + a.min / 60 + a.sec / 3600;
  1313. X}
  1314. X
  1315. X
  1316. Xdefine fixdms(a)
  1317. X{
  1318. X    a.min += frac(a.deg) * 60;
  1319. X    a.deg = int(a.deg);
  1320. X    a.sec += frac(a.min) * 60;
  1321. X    a.min = int(a.min);
  1322. X    a.min += a.sec // 60;
  1323. X    a.sec %= 60;
  1324. X    a.deg += a.min // 60;
  1325. X    a.min %= 60;
  1326. X    a.deg %= 360;
  1327. X}
  1328. X
  1329. Xglobal lib_debug;
  1330. Xif (!isnum(lib_debug) || lib_debug>0) print "obj dms {deg, min, sec} defined"
  1331. Xif (!isnum(lib_debug) || lib_debug>0) print "dms(deg, min, sec) defined"
  1332. Xif (!isnum(lib_debug) || lib_debug>0) print "dms_add(a, b) defined"
  1333. Xif (!isnum(lib_debug) || lib_debug>0) print "dms_neg(a) defined"
  1334. Xif (!isnum(lib_debug) || lib_debug>0) print "dms_sub(a, b) defined"
  1335. Xif (!isnum(lib_debug) || lib_debug>0) print "dms_mul(a, b) defined"
  1336. Xif (!isnum(lib_debug) || lib_debug>0) print "dms_print(a) defined"
  1337. Xif (!isnum(lib_debug) || lib_debug>0) print "dms_abs(a) defined"
  1338. END_OF_FILE
  1339. if test 2204 -ne `wc -c <'lib/deg.cal'`; then
  1340.     echo shar: \"'lib/deg.cal'\" unpacked with wrong size!
  1341. fi
  1342. # end of 'lib/deg.cal'
  1343. fi
  1344. if test -f 'lib/mersenne.cal' -a "${1}" != "-c" ; then 
  1345.   echo shar: Will not clobber existing file \"'lib/mersenne.cal'\"
  1346. else
  1347. echo shar: Extracting \"'lib/mersenne.cal'\" \(843 characters\)
  1348. sed "s/^X//" >'lib/mersenne.cal' <<'END_OF_FILE'
  1349. X/*
  1350. X * Copyright (c) 1992 David I. Bell
  1351. X * Permission is granted to use, distribute, or modify this source,
  1352. X * provided that this copyright notice remains intact.
  1353. X *
  1354. X * Perform a primality test of 2^p-1, for prime p>1.
  1355. X */
  1356. X
  1357. Xdefine mersenne(p)
  1358. X{
  1359. X    local u, i, p_mask;
  1360. X
  1361. X    /* firewall */
  1362. X    if (! isint(p))
  1363. X        quit "p is not an integer";
  1364. X
  1365. X    /* two is a special case */
  1366. X    if (p == 2)
  1367. X        return 1;
  1368. X
  1369. X    /* if p is not prime, then 2^p-1 is not prime */
  1370. X    if (! ptest(p,10))
  1371. X        return 0;
  1372. X
  1373. X    /* calculate 2^p-1 for later mods */
  1374. X    p_mask = 2^p - 1;
  1375. X
  1376. X    /* lltest: u(i+1) = u(i)^2 - 2 mod 2^p-1 */
  1377. X    u = 4;
  1378. X    for (i = 2; i < p; ++i) {
  1379. X        u = u^2 - 2;
  1380. X        u = u&p_mask + u>>p;
  1381. X        if (u > p_mask)
  1382. X            u = u&p_mask + 1;
  1383. X    }
  1384. X
  1385. X    /* 2^p-1 is prime iff u(p) = 0 mod 2^p-1 */
  1386. X    return (u == p_mask);
  1387. X}
  1388. X
  1389. Xglobal lib_debug;
  1390. Xif (!isnum(lib_debug) || lib_debug>0) print "mersenne(p) defined";
  1391. END_OF_FILE
  1392. if test 843 -ne `wc -c <'lib/mersenne.cal'`; then
  1393.     echo shar: \"'lib/mersenne.cal'\" unpacked with wrong size!
  1394. fi
  1395. # end of 'lib/mersenne.cal'
  1396. fi
  1397. if test -f 'lib/nextprim.cal' -a "${1}" != "-c" ; then 
  1398.   echo shar: Will not clobber existing file \"'lib/nextprim.cal'\"
  1399. else
  1400. echo shar: Extracting \"'lib/nextprim.cal'\" \(450 characters\)
  1401. sed "s/^X//" >'lib/nextprim.cal' <<'END_OF_FILE'
  1402. X/*
  1403. X * Copyright (c) 1992 David I. Bell
  1404. X * Permission is granted to use, distribute, or modify this source,
  1405. X * provided that this copyright notice remains intact.
  1406. X *
  1407. X * Function to find the next prime (probably).
  1408. X */
  1409. X
  1410. Xdefine nextprime(n, tries)
  1411. X{
  1412. X    if (isnull(tries))
  1413. X        tries = 20;
  1414. X    if (iseven(n))
  1415. X        n++;
  1416. X    while (ptest(n, tries) == 0)
  1417. X        n += 2;
  1418. X    return n;
  1419. X}
  1420. X
  1421. Xglobal lib_debug;
  1422. Xif (!isnum(lib_debug) || lib_debug>0) print "nextprime(n, tries) defined";
  1423. END_OF_FILE
  1424. if test 450 -ne `wc -c <'lib/nextprim.cal'`; then
  1425.     echo shar: \"'lib/nextprim.cal'\" unpacked with wrong size!
  1426. fi
  1427. # end of 'lib/nextprim.cal'
  1428. fi
  1429. if test -f 'lib/pell.cal' -a "${1}" != "-c" ; then 
  1430.   echo shar: Will not clobber existing file \"'lib/pell.cal'\"
  1431. else
  1432. echo shar: Extracting \"'lib/pell.cal'\" \(1290 characters\)
  1433. sed "s/^X//" >'lib/pell.cal' <<'END_OF_FILE'
  1434. X/*
  1435. X * Copyright (c) 1992 David I. Bell
  1436. X * Permission is granted to use, distribute, or modify this source,
  1437. X * provided that this copyright notice remains intact.
  1438. X *
  1439. X * Solve Pell's equation; Returns the solution X to: X^2 - D * Y^2 = 1.
  1440. X * Type the solution to pells equation for a particular D.
  1441. X */
  1442. X
  1443. Xdefine pell(D)
  1444. X{
  1445. X    local X, Y;
  1446. X
  1447. X    X = pellx(D);
  1448. X    if (isnull(X)) {
  1449. X        print "D=":D:" is square";
  1450. X        return;
  1451. X    }
  1452. X    Y = isqrt((X^2 - 1) / D);
  1453. X    print X : "^2 - " : D : "*" : Y : "^2 = " : X^2 - D*Y^2;
  1454. X}
  1455. X
  1456. X
  1457. X/*
  1458. X * Function to solve Pell's equation
  1459. X * Returns the solution X to:
  1460. X *    X^2 - D * Y^2 = 1
  1461. X */
  1462. Xdefine pellx(D)
  1463. X{
  1464. X    local R, Rp, U, Up, V, Vp, A, T, Q1, Q2, n, ans, tmp;
  1465. X
  1466. X    mat ans[2,2];
  1467. X    mat tmp[2,2];
  1468. X
  1469. X    R = isqrt(D);
  1470. X    Vp = D - R^2;
  1471. X    if (Vp == 0)
  1472. X        return;
  1473. X    Rp = R + R;
  1474. X    U = Rp;
  1475. X    Up = U;
  1476. X    V = 1;
  1477. X    A = 0;
  1478. X    n = 0;
  1479. X    ans[0,0] = 1;
  1480. X    ans[1,1] = 1;
  1481. X    tmp[0,1] = 1;
  1482. X    tmp[1,0] = 1;
  1483. X    do {
  1484. X        T = V;
  1485. X        V = A * (Up - U) + Vp;
  1486. X        Vp = T;
  1487. X        A = U // V;
  1488. X        Up = U;
  1489. X        U = Rp - U % V;
  1490. X        tmp[0,0] = A;
  1491. X        ans *= tmp;
  1492. X        n++;
  1493. X    } while (A != Rp);
  1494. X    Q2 = ans[[1]];
  1495. X    Q1 = isqrt(Q2^2 * D + 1);
  1496. X    if (isodd(n)) {
  1497. X        T = Q1^2 + D * Q2^2;
  1498. X        Q2 = Q1 * Q2 * 2;
  1499. X        Q1 = T;
  1500. X    }
  1501. X    return Q1;
  1502. X}
  1503. X
  1504. Xglobal lib_debug;
  1505. Xif (!isnum(lib_debug) || lib_debug>0) print "pell(D) defined";
  1506. Xif (!isnum(lib_debug) || lib_debug>0) print "pellx(D) defined";
  1507. END_OF_FILE
  1508. if test 1290 -ne `wc -c <'lib/pell.cal'`; then
  1509.     echo shar: \"'lib/pell.cal'\" unpacked with wrong size!
  1510. fi
  1511. # end of 'lib/pell.cal'
  1512. fi
  1513. if test -f 'lib/pi.cal' -a "${1}" != "-c" ; then 
  1514.   echo shar: Will not clobber existing file \"'lib/pi.cal'\"
  1515. else
  1516. echo shar: Extracting \"'lib/pi.cal'\" \(1320 characters\)
  1517. sed "s/^X//" >'lib/pi.cal' <<'END_OF_FILE'
  1518. X/*
  1519. X * Copyright (c) 1992 David I. Bell
  1520. X * Permission is granted to use, distribute, or modify this source,
  1521. X * provided that this copyright notice remains intact.
  1522. X *
  1523. X * Calculate pi within the specified epsilon using the quartic convergence
  1524. X * iteration.
  1525. X */
  1526. X
  1527. Xdefine qpi(epsilon)
  1528. X{
  1529. X    local niter, yn, ym, tm, an, am, t, tn, sqrt2, epsilon2, count, digits;
  1530. X    local bits, bits2;
  1531. X
  1532. X    if (isnull(epsilon))
  1533. X        epsilon = epsilon();
  1534. X    digits = digits(1/epsilon);
  1535. X    if    (digits <=  8) { niter = 1; epsilon =    1e-8; }
  1536. X    else if (digits <= 40) { niter = 2; epsilon =  1e-40; }
  1537. X    else if (digits <= 170) { niter = 3; epsilon = 1e-170; }
  1538. X    else if (digits <= 693) { niter = 4; epsilon = 1e-693; }
  1539. X    else {
  1540. X        niter = 4;
  1541. X        t = 693;
  1542. X        while (t < epsilon) {
  1543. X            ++niter;
  1544. X            t *= 4;
  1545. X        }
  1546. X    }
  1547. X    epsilon2 = epsilon/(digits/10 + 1);
  1548. X    digits = digits(1/epsilon2);
  1549. X    sqrt2 = sqrt(2, epsilon2);
  1550. X    bits = abs(log2(epsilon)) + 1;
  1551. X    bits2 = abs(log2(epsilon2)) + 1;
  1552. X    yn = sqrt2 - 1;
  1553. X    an = 6 - 4 * sqrt2;
  1554. X    tn = 2;
  1555. X    for (count = 0; count < niter; count++) {
  1556. X        ym = yn;
  1557. X        am = an;
  1558. X        tn *= 4;
  1559. X        t = sqrt(sqrt(1-ym^4, epsilon2), epsilon2);
  1560. X        yn = (1-t)/(1+t);
  1561. X        an = (1+yn)^4*am-tn*yn*(1+yn+yn^2);
  1562. X        yn = bround(yn, bits2);
  1563. X        an = bround(an, bits2);
  1564. X    }
  1565. X    return (bround(1/an, bits));
  1566. X}
  1567. X
  1568. Xglobal lib_debug;
  1569. Xif (!isnum(lib_debug) || lib_debug>0) print "qpi(epsilon) defined";
  1570. END_OF_FILE
  1571. if test 1320 -ne `wc -c <'lib/pi.cal'`; then
  1572.     echo shar: \"'lib/pi.cal'\" unpacked with wrong size!
  1573. fi
  1574. # end of 'lib/pi.cal'
  1575. fi
  1576. if test -f 'lib/pollard.cal' -a "${1}" != "-c" ; then 
  1577.   echo shar: Will not clobber existing file \"'lib/pollard.cal'\"
  1578. else
  1579. echo shar: Extracting \"'lib/pollard.cal'\" \(630 characters\)
  1580. sed "s/^X//" >'lib/pollard.cal' <<'END_OF_FILE'
  1581. X/*
  1582. X * Copyright (c) 1992 David I. Bell
  1583. X * Permission is granted to use, distribute, or modify this source,
  1584. X * provided that this copyright notice remains intact.
  1585. X *
  1586. X * Factor using Pollard's p-1 method.
  1587. X */
  1588. X
  1589. Xdefine factor(N, B, ai, af)
  1590. X{
  1591. X    local    a, k, i, d;
  1592. X
  1593. X    if (isnull(B))
  1594. X        B = 1000;
  1595. X    if (isnull(ai))
  1596. X        ai = 2;
  1597. X    if (isnull(af))
  1598. X        af = ai + 20;
  1599. X    k = lcmfact(B);
  1600. X    d = lfactor(N, B);
  1601. X    if (d > 1)
  1602. X        return d;
  1603. X    for (a = ai; a <= af; a++) {
  1604. X        i = pmod(a, k, N);
  1605. X        d = gcd(i - 1, N);
  1606. X        if ((d > 1) && (d != N))
  1607. X            return d;
  1608. X    }
  1609. X    return 1;
  1610. X}
  1611. X
  1612. Xglobal lib_debug;
  1613. Xif (!isnum(lib_debug) || lib_debug>0) print "factor(N, B, ai, af) defined";
  1614. END_OF_FILE
  1615. if test 630 -ne `wc -c <'lib/pollard.cal'`; then
  1616.     echo shar: \"'lib/pollard.cal'\" unpacked with wrong size!
  1617. fi
  1618. # end of 'lib/pollard.cal'
  1619. fi
  1620. if test -f 'lib/psqrt.cal' -a "${1}" != "-c" ; then 
  1621.   echo shar: Will not clobber existing file \"'lib/psqrt.cal'\"
  1622. else
  1623. echo shar: Extracting \"'lib/psqrt.cal'\" \(1010 characters\)
  1624. sed "s/^X//" >'lib/psqrt.cal' <<'END_OF_FILE'
  1625. X/*
  1626. X * Copyright (c) 1992 David I. Bell
  1627. X * Permission is granted to use, distribute, or modify this source,
  1628. X * provided that this copyright notice remains intact.
  1629. X *
  1630. X * Calculate square roots modulo a prime.
  1631. X *
  1632. X * Returns null if number is not prime or if there is no square root.
  1633. X * The smaller square root is always returned.
  1634. X */
  1635. X
  1636. Xdefine psqrt(u, p)
  1637. X{
  1638. X    local    p1, q, n, y, r, v, w, t, k;
  1639. X
  1640. X    p1 = p - 1;
  1641. X    r = lowbit(p1);
  1642. X    q = p >> r;
  1643. X    t = 1 << (r - 1);
  1644. X    for (n = 2; ; n++) {
  1645. X        if (ptest(n, 1) == 0)
  1646. X            continue;
  1647. X        y = pmod(n, q, p);
  1648. X        k = pmod(y, t, p);
  1649. X        if (k == 1)
  1650. X            continue;
  1651. X        if (k != p1)
  1652. X            return;
  1653. X        break;
  1654. X    }
  1655. X    t = pmod(u, (q - 1) / 2, p);
  1656. X    v = (t * u) % p;
  1657. X    w = (t^2 * u) % p;
  1658. X    while (w != 1) {
  1659. X        k = 0;
  1660. X        t = w;
  1661. X        do {
  1662. X            k++;
  1663. X            t = t^2 % p;
  1664. X        } while (t != 1);
  1665. X        if (k == r)
  1666. X            return;
  1667. X        t = pmod(y, 1 << (r - k - 1), p);
  1668. X        y = t^2 % p;
  1669. X        v = (v * t) % p;
  1670. X        w = (w * y) % p;
  1671. X        r = k;
  1672. X    }
  1673. X    return min(v, p - v);
  1674. X}
  1675. X
  1676. X
  1677. Xglobal lib_debug;
  1678. Xif (!isnum(lib_debug) || lib_debug>0) print "psqrt(u, p) defined";
  1679. END_OF_FILE
  1680. if test 1010 -ne `wc -c <'lib/psqrt.cal'`; then
  1681.     echo shar: \"'lib/psqrt.cal'\" unpacked with wrong size!
  1682. fi
  1683. # end of 'lib/psqrt.cal'
  1684. fi
  1685. if test -f 'lib/solve.cal' -a "${1}" != "-c" ; then 
  1686.   echo shar: Will not clobber existing file \"'lib/solve.cal'\"
  1687. else
  1688. echo shar: Extracting \"'lib/solve.cal'\" \(1192 characters\)
  1689. sed "s/^X//" >'lib/solve.cal' <<'END_OF_FILE'
  1690. X/*
  1691. X * Copyright (c) 1992 David I. Bell
  1692. X * Permission is granted to use, distribute, or modify this source,
  1693. X * provided that this copyright notice remains intact.
  1694. X *
  1695. X * Solve the equation f(x) = 0 to within the desired error value for x.
  1696. X * The function 'f' must be defined outside of this routine, and the low
  1697. X * and high values are guesses which must produce values with opposite signs.
  1698. X */
  1699. X
  1700. Xdefine solve(low, high, epsilon)
  1701. X{
  1702. X    local flow, fhigh, fmid, mid, places;
  1703. X
  1704. X    if (isnull(epsilon))
  1705. X        epsilon = epsilon();
  1706. X    if (epsilon <= 0)
  1707. X        quit "Non-positive epsilon value";
  1708. X    places = highbit(1 + int(1/epsilon)) + 1;
  1709. X    flow = f(low);
  1710. X    if (abs(flow) < epsilon)
  1711. X        return low;
  1712. X    fhigh = f(high);
  1713. X    if (abs(flow) < epsilon)
  1714. X        return high;
  1715. X    if (sgn(flow) == sgn(fhigh))
  1716. X        quit "Non-opposite signs";
  1717. X    while (1) {
  1718. X        mid = bround(high - fhigh * (high - low) / (fhigh - flow), places);
  1719. X        if ((mid == low) || (mid == high))
  1720. X            places++;
  1721. X        fmid = f(mid);
  1722. X        if (abs(fmid) < epsilon)
  1723. X            return mid;
  1724. X        if (sgn(fmid) == sgn(flow)) {
  1725. X            low = mid;
  1726. X            flow = fmid;
  1727. X        } else {
  1728. X            high = mid;
  1729. X            fhigh = fmid;
  1730. X        }
  1731. X    }
  1732. X}
  1733. X
  1734. Xglobal lib_debug;
  1735. Xif (!isnum(lib_debug) || lib_debug>0) print "solve(low, high, epsilon) defined";
  1736. END_OF_FILE
  1737. if test 1192 -ne `wc -c <'lib/solve.cal'`; then
  1738.     echo shar: \"'lib/solve.cal'\" unpacked with wrong size!
  1739. fi
  1740. # end of 'lib/solve.cal'
  1741. fi
  1742. if test -f 'lib/sumsq.cal' -a "${1}" != "-c" ; then 
  1743.   echo shar: Will not clobber existing file \"'lib/sumsq.cal'\"
  1744. else
  1745. echo shar: Extracting \"'lib/sumsq.cal'\" \(879 characters\)
  1746. sed "s/^X//" >'lib/sumsq.cal' <<'END_OF_FILE'
  1747. X/*
  1748. X * Copyright (c) 1992 David I. Bell
  1749. X * Permission is granted to use, distribute, or modify this source,
  1750. X * provided that this copyright notice remains intact.
  1751. X *
  1752. X * Determine the unique two positive integers whose squares sum to the
  1753. X * specified prime.  This is always possible for all primes of the form
  1754. X * 4N+1, and always impossible for primes of the form 4N-1.
  1755. X */
  1756. X
  1757. Xdefine ss(p)
  1758. X{
  1759. X    local a, b, i, p4;
  1760. X
  1761. X    if (p == 2) {
  1762. X        print "1^2 + 1^2 = 2";
  1763. X        return;
  1764. X    }
  1765. X    if ((p % 4) != 1) {
  1766. X        print p, "is not of the form 4N+1";
  1767. X        return;
  1768. X    }
  1769. X    if (!ptest(p, min(p-2, 10))) {
  1770. X        print p, "is not a prime";
  1771. X        return;
  1772. X    }
  1773. X    p4 = (p - 1) / 4;
  1774. X    i = 2;
  1775. X    do {
  1776. X        a = pmod(i++, p4, p);
  1777. X    } while ((a^2 % p) == 1);
  1778. X    b = p;
  1779. X    while (b^2 > p) {
  1780. X        i = b % a;
  1781. X        b = a;
  1782. X        a = i;
  1783. X    }
  1784. X    print a : "^2 +" , b : "^2 =" , a^2 + b^2;
  1785. X}
  1786. X
  1787. Xglobal lib_debug;
  1788. Xif (!isnum(lib_debug) || lib_debug>0) print "ss(p) defined";
  1789. END_OF_FILE
  1790. if test 879 -ne `wc -c <'lib/sumsq.cal'`; then
  1791.     echo shar: \"'lib/sumsq.cal'\" unpacked with wrong size!
  1792. fi
  1793. # end of 'lib/sumsq.cal'
  1794. fi
  1795. if test -f 'lib/unitfrac.cal' -a "${1}" != "-c" ; then 
  1796.   echo shar: Will not clobber existing file \"'lib/unitfrac.cal'\"
  1797. else
  1798. echo shar: Extracting \"'lib/unitfrac.cal'\" \(849 characters\)
  1799. sed "s/^X//" >'lib/unitfrac.cal' <<'END_OF_FILE'
  1800. X/*
  1801. X * Copyright (c) 1992 David I. Bell
  1802. X * Permission is granted to use, distribute, or modify this source,
  1803. X * provided that this copyright notice remains intact.
  1804. X *
  1805. X * Represent a fraction as sum of distinct unit fractions.
  1806. X * The output is the unit fractions themselves, and in square brackets,
  1807. X * the number of digits in the numerator and denominator of the value left
  1808. X * to be found.  Numbers larger than 3.5 become very difficult to calculate.
  1809. X */
  1810. X
  1811. Xdefine unitfrac(x)
  1812. X{
  1813. X    local    d, di, n;
  1814. X
  1815. X    if (x <= 0)
  1816. X        quit "Non-positive argument";
  1817. X    d = 2;
  1818. X    do {
  1819. X        n = int(1 / x) + 1;
  1820. X        if (n > d)
  1821. X            d = n;
  1822. X        di = 1/d;
  1823. X        print '  [': digits(num(x)): '/': digits(den(x)): ']',, di;
  1824. X        x -= di;
  1825. X        d++;
  1826. X    } while ((num(x) > 1) || (x == di) || (x == 1));
  1827. X    print '  [1/1]',, x;
  1828. X}
  1829. X
  1830. X
  1831. Xglobal lib_debug;
  1832. Xif (!isnum(lib_debug) || lib_debug>0) print "unitfrac(x) defined";
  1833. END_OF_FILE
  1834. if test 849 -ne `wc -c <'lib/unitfrac.cal'`; then
  1835.     echo shar: \"'lib/unitfrac.cal'\" unpacked with wrong size!
  1836. fi
  1837. # end of 'lib/unitfrac.cal'
  1838. fi
  1839. if test -f 'lib/varargs.cal' -a "${1}" != "-c" ; then 
  1840.   echo shar: Will not clobber existing file \"'lib/varargs.cal'\"
  1841. else
  1842. echo shar: Extracting \"'lib/varargs.cal'\" \(547 characters\)
  1843. sed "s/^X//" >'lib/varargs.cal' <<'END_OF_FILE'
  1844. X/*
  1845. X * Copyright (c) 1992 David I. Bell
  1846. X * Permission is granted to use, distribute, or modify this source,
  1847. X * provided that this copyright notice remains intact.
  1848. X *
  1849. X * Example program to use 'varargs'.
  1850. X *
  1851. X * Program to sum the cubes of all the specified numbers.
  1852. X */
  1853. X
  1854. Xdefine sc()
  1855. X{
  1856. X    local s, i;
  1857. X
  1858. X    s = 0;
  1859. X    for (i = 1; i <= param(0); i++) {
  1860. X        if (!isnum(param(i))) {
  1861. X            print "parameter",i,"is not a number";
  1862. X            continue;
  1863. X        }
  1864. X        s += param(i)^3;
  1865. X    }
  1866. X    return s;
  1867. X}
  1868. X
  1869. Xglobal lib_debug;
  1870. Xif (!isnum(lib_debug) || lib_debug>0) print "sc(a, b, ...) defined";
  1871. END_OF_FILE
  1872. if test 547 -ne `wc -c <'lib/varargs.cal'`; then
  1873.     echo shar: \"'lib/varargs.cal'\" unpacked with wrong size!
  1874. fi
  1875. # end of 'lib/varargs.cal'
  1876. fi
  1877. if test -f 'lint.sed' -a "${1}" != "-c" ; then 
  1878.   echo shar: Will not clobber existing file \"'lint.sed'\"
  1879. else
  1880. echo shar: Extracting \"'lint.sed'\" \(1277 characters\)
  1881. sed "s/^X//" >'lint.sed' <<'END_OF_FILE'
  1882. X/^[a-z][a-z_]*\.c:$/d
  1883. X/name declared but never used or defined: .* \/usr\/include\/malloc.h(/d
  1884. X/implicitly declared to return int: _sysconf$/d
  1885. X/name used but not defined: _sysconf in /d
  1886. X/function returns value which is [a-z][a-z]* ignored: memcpy$/d
  1887. X/function returns value which is [a-z][a-z]* ignored: strcpy$/d
  1888. X/function returns value which is [a-z][a-z]* ignored: strncpy$/d
  1889. X/function returns value which is [a-z][a-z]* ignored: strcat$/d
  1890. X/function returns value which is [a-z][a-z]* ignored: memset$/d
  1891. X/function returns value which is [a-z][a-z]* ignored: fclose$/d
  1892. X/function returns value which is [a-z][a-z]* ignored: fflush$/d
  1893. X/function returns value which is [a-z][a-z]* ignored: fprintf$/d
  1894. X/function returns value which is [a-z][a-z]* ignored: printf$/d
  1895. X/function returns value which is [a-z][a-z]* ignored: sprintf$/d
  1896. X/function returns value which is [a-z][a-z]* ignored: vsprintf$/d
  1897. X/function returns value which is [a-z][a-z]* ignored: fputc$/d
  1898. X/function returns value which is [a-z][a-z]* ignored: fputs$/d
  1899. X/function returns value which is [a-z][a-z]* ignored: putchar$/d
  1900. X/function returns value which is [a-z][a-z]* ignored: ungetc$/d
  1901. X/function returns value which is [a-z][a-z]* ignored: system$/d
  1902. X/function returns value which is [a-z][a-z]* ignored: times$/d
  1903. END_OF_FILE
  1904. if test 1277 -ne `wc -c <'lint.sed'`; then
  1905.     echo shar: \"'lint.sed'\" unpacked with wrong size!
  1906. fi
  1907. # end of 'lint.sed'
  1908. fi
  1909. if test -f 'stdarg.h' -a "${1}" != "-c" ; then 
  1910.   echo shar: Will not clobber existing file \"'stdarg.h'\"
  1911. else
  1912. echo shar: Extracting \"'stdarg.h'\" \(904 characters\)
  1913. sed "s/^X//" >'stdarg.h' <<'END_OF_FILE'
  1914. X/*
  1915. X * Copyright (c) 1992 David I. Bell
  1916. X * Permission is granted to use, distribute, or modify this source,
  1917. X * provided that this copyright notice remains intact.
  1918. X */
  1919. X
  1920. X#include "args.h"
  1921. X
  1922. X#ifndef STDARG_H
  1923. X#define STDARG_H
  1924. X
  1925. X#ifdef VARARGS
  1926. X
  1927. X#include <varargs.h>
  1928. X
  1929. X#else /*VARARG*/
  1930. X
  1931. X#ifdef STDARG
  1932. X
  1933. X#if defined(__STDC__)
  1934. X#include <stdarg.h>
  1935. X#else
  1936. X%;%;%  BOGUS DEFINE!  Must use ANSI C when STDARG is defined  %;%;%
  1937. X#endif
  1938. X
  1939. X#else /*STDARG*/
  1940. X
  1941. X/*
  1942. X * SIMULATE_STDARG
  1943. X *
  1944. X * WARNING: This type of stdarg makes assumptions about the stack
  1945. X *         that may not be true on your system.  You may want to
  1946. X *        define STDARG (if using ANSI C) or VARARGS.
  1947. X */
  1948. X
  1949. Xtypedef char *va_list;
  1950. X#define va_start(ap,parmn) (void)((ap) = (char*)(&(parmn) + 1))
  1951. X#define va_end(ap) (void)((ap) = 0)
  1952. X#define va_arg(ap, type) \
  1953. X    (((type*)((ap) = ((ap) + sizeof(type))))[-1])
  1954. X
  1955. X#endif /*STDARG*/
  1956. X#endif /*VARARG*/
  1957. X
  1958. X#endif
  1959. X
  1960. X/* END CODE */
  1961. END_OF_FILE
  1962. if test 904 -ne `wc -c <'stdarg.h'`; then
  1963.     echo shar: \"'stdarg.h'\" unpacked with wrong size!
  1964. fi
  1965. # end of 'stdarg.h'
  1966. fi
  1967. if test -f 'string.h' -a "${1}" != "-c" ; then 
  1968.   echo shar: Will not clobber existing file \"'string.h'\"
  1969. else
  1970. echo shar: Extracting \"'string.h'\" \(529 characters\)
  1971. sed "s/^X//" >'string.h' <<'END_OF_FILE'
  1972. X/*
  1973. X * Copyright (c) 1992 David I. Bell
  1974. X * Permission is granted to use, distribute, or modify this source,
  1975. X * provided that this copyright notice remains intact.
  1976. X */
  1977. X
  1978. X
  1979. Xtypedef struct {
  1980. X    char *h_list;    /* list of strings separated by nulls */
  1981. X    long h_used;    /* characters used so far */
  1982. X    long h_avail;    /* characters available for use */
  1983. X    long h_count;    /* number of strings */
  1984. X} STRINGHEAD;
  1985. X
  1986. X
  1987. Xextern char *addstr(), *namestr(), *charstr(), *addliteral();
  1988. Xextern long findstr(), stringindex();
  1989. X
  1990. Xextern void initstr();
  1991. X
  1992. X/* END CODE */
  1993. END_OF_FILE
  1994. if test 529 -ne `wc -c <'string.h'`; then
  1995.     echo shar: \"'string.h'\" unpacked with wrong size!
  1996. fi
  1997. # end of 'string.h'
  1998. fi
  1999. if test -f 'symbol.h' -a "${1}" != "-c" ; then 
  2000.   echo shar: Will not clobber existing file \"'symbol.h'\"
  2001. else
  2002. echo shar: Extracting \"'symbol.h'\" \(1148 characters\)
  2003. sed "s/^X//" >'symbol.h' <<'END_OF_FILE'
  2004. X/*
  2005. X * Copyright (c) 1992 David I. Bell
  2006. X * Permission is granted to use, distribute, or modify this source,
  2007. X * provided that this copyright notice remains intact.
  2008. X */
  2009. X
  2010. X
  2011. X/*
  2012. X * Symbol Declarations.
  2013. X */
  2014. X#define SYM_UNDEFINED    0    /* undefined symbol */
  2015. X#define SYM_PARAM    1    /* paramater symbol */
  2016. X#define SYM_LOCAL    2    /* local symbol */
  2017. X#define SYM_GLOBAL    3    /* global symbol */
  2018. X
  2019. X
  2020. Xtypedef struct global GLOBAL;
  2021. Xstruct global {
  2022. X    int g_len;        /* length of symbol name */
  2023. X    char *g_name;        /* global symbol name */
  2024. X    VALUE g_value;        /* global symbol value */
  2025. X    GLOBAL *g_next;        /* next symbol in hash chain */
  2026. X};
  2027. X
  2028. X
  2029. X/*
  2030. X * Routines to search for global symbols.
  2031. X */
  2032. Xextern GLOBAL *addglobal(), *findglobal();
  2033. X
  2034. X
  2035. X/*
  2036. X * Routines to return names of variables.
  2037. X */
  2038. Xextern char *localname(), *globalname(), *paramname();
  2039. X
  2040. X
  2041. X/*
  2042. X * Other routines.
  2043. X */
  2044. Xextern long addlocal(), findlocal(), addparam(), findparam();
  2045. Xextern void initlocals();
  2046. Xextern void initglobals();
  2047. Xextern void initfunctions();
  2048. Xextern int writeglobals();
  2049. Xextern int symboltype();    /* return the type of a variable name */
  2050. Xextern void showglobals();    /* show the value of all global variables */
  2051. X
  2052. X/* END CODE */
  2053. END_OF_FILE
  2054. if test 1148 -ne `wc -c <'symbol.h'`; then
  2055.     echo shar: \"'symbol.h'\" unpacked with wrong size!
  2056. fi
  2057. # end of 'symbol.h'
  2058. fi
  2059. if test -f 'version.c' -a "${1}" != "-c" ; then 
  2060.   echo shar: Will not clobber existing file \"'version.c'\"
  2061. else
  2062. echo shar: Extracting \"'version.c'\" \(531 characters\)
  2063. sed "s/^X//" >'version.c' <<'END_OF_FILE'
  2064. X/*
  2065. X * Copyright (c) 1992 David I. Bell
  2066. X * Permission is granted to use, distribute, or modify this source,
  2067. X * provided that this copyright notice remains intact.
  2068. X *
  2069. X * version - determine the version of calc
  2070. X */
  2071. X
  2072. X#include <stdio.h>
  2073. X
  2074. X#define MAJOR_VER 1    /* major version */
  2075. X#define MINOR_VER 24    /* minor version */
  2076. X#define PATCH_LEVEL 7    /* patch level */
  2077. X
  2078. Xvoid
  2079. Xversion(stream)
  2080. X    FILE *stream;    /* stream to write version on */
  2081. X{
  2082. X    fprintf(stream, "calc version: %d.%d.%d\n", 
  2083. X        MAJOR_VER, MINOR_VER, PATCH_LEVEL);
  2084. X}
  2085. X
  2086. X/* END CODE */
  2087. END_OF_FILE
  2088. if test 531 -ne `wc -c <'version.c'`; then
  2089.     echo shar: \"'version.c'\" unpacked with wrong size!
  2090. fi
  2091. # end of 'version.c'
  2092. fi
  2093. echo shar: End of archive 1 \(of 21\).
  2094. cp /dev/null ark1isdone
  2095. MISSING=""
  2096. for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ; do
  2097.     if test ! -f ark${I}isdone ; then
  2098.     MISSING="${MISSING} ${I}"
  2099.     fi
  2100. done
  2101. if test "${MISSING}" = "" ; then
  2102.     echo You have unpacked all 21 archives.
  2103.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  2104. else
  2105.     echo You still need to unpack the following archives:
  2106.     echo "        " ${MISSING}
  2107. fi
  2108. ##  End of shell archive.
  2109. exit 0
  2110.