calc(1) 							       calc(1)



1mNAME0m
       calc - arbitrary precision calculator

1mSYNOPSIS0m
       4mcalc24m [1m-c22m] [1m-C22m] [1m-d22m]
	    [1m-D calc_debug[:resource_debug[:user_debug]]22m]
	    [1m-e22m] [1m-h22m] [1m-i22m] [1m-m mode22m] [1m-O22m]
	    [1m-p22m] [1m-q22m] [1m-s22m] [1m-u22m] [1m-v22m] [1m[--] calc_cmd ...22m]

       4m#!/usr/bin/calc24m [other_flags ...] 1m-f0m

1mDESCRIPTION0m

       CALC OPTIONS



       1m-c	22mContinue reading command lines even after a scan/parse error has
	      caused the abandonment of a line.  Note that  this  option  only
	      deals  with  scanning and parsing of the calc language.  It does
	      not deal with execution or run-time errors.

	      For example:

		   calc read many_errors.cal

	      will cause 1mcalc 22mto abort on the first syntax error, whereas:

		   calc -c read many_errors.cal

	      will cause 1mcalc 22mto try to process each line being  read  despite
	      the scan/parse errors that it encounters.

	      By  default, calc startup resource files are silently ignored if
	      not found.  This flag will report missing startup resource files
	      unless 1m-d 22mis also given.


       1m-C	22mPermit  the execution of custom builtin functions.  Without this
	      flag, calling the custom() builtin function will simply generate
	      an error.

	      Use  of  this  flag may cause 1mcalc 22mto execute functions that are
	      non-standard and that are not portable.	Custom	builtin  func-
	      tions are disabled by default for this reason.


       1m-d	22mDisable  the  printing  of  the  opening title.  The printing of
	      resource file debug and informational messages is also  disabled
	      as if 1mconfig("resource_debug", 0) 22mhad been executed.

	      For example:

		   calc "read qtime; qtime(2)"

	      will output something like:

		   qtime(utc_hr_offset) defined
		   It's nearly ten past six.

	      whereas:

		   calc -d "read qtime; qtime(2)"

	      will just say:

		   It's nearly ten past six.

	      This  flag  disables  the  reporting  of	missing  calc  startup
	      resource files.


       1m-D 22mcalc_debug[:resource_debug[:user_debug]]
	      Force  the   initial   value   of   config("calc_debug"),   con-
	      fig("resource_debug") and config("user_debug").

	      The  :  separated strings are interpreted as signed 32 bit inte-
	      gers.  After an optional leading sign a leading  zero  indicates
	      octal  conversion,  and  a  leading ``0x'' or ``0X'' hexadecimal
	      conversion.  Otherwise, decimal conversion is assumed.

	      By default, 4mcalc_debug24m is 0, 4mresource_debug24m is 3 and  4muser_debug0m
	      is 0.

	      For more information use the following 1mcalc 22mcommand:

		   help config


       1m-e	22mIgnore  any	environment  variables	on  startup.   The getenv()
	      builtin will still return values, however.


       1m-f	22mThis flag is required when using calc in 1mshell script mode22m.   It
	      must be at the end of the initial 1m#!  22mline of the script.

	      This  flag  is  normally only at the end of a calc shell script.
	      If the first line of an executable file begins 1m#!   22mfollowed  by
	      the absolute pathname of the 1mcalc 22mprogram and the flag 1m-f 22mas in:

		   4m#!/usr/bin/calc24m [other_flags ...] 1m-f0m

	      the rest of the file will be processed  in  1mshell  script  mode22m.
	      See  1mSHELL  SCRIPT  MODE  22msection  of  this	man  page below for
	      details.

	      The actual form of this flag is:

		   1m-f 22mfilename

	      On systems that treat an executable that begins with  1m#!   22mas  a
	      script,  the path of the executable is appended by the kernel as
	      the final argument to the exec() system call.  This is  why  the
	      1m-f 22mflag at the very end of the 1m#!	22mline.

	      It is possible use 1m-f filename 22mon the command line:

		   4mcalc24m [other_flags ...] 1m-f 22mfilename

	      This  will  cause  calc  to  process  lines in 1mfilename 22min 1mshell0m
	      1mscript mode22m.

	      Use of 1m-f 22mimplies 1m-s22m.  In addition, 1m-d 22mand 1m-p 22mare implied if	1m-i0m
	      is not given.


       1m-h	22mPrint  a help message.  This option implies 1m-q22m.  This is equiva-
	      lent to the 1mcalc 22mcommand help help.	The help facility  is  dis-
	      abled unless the mode is 5 or 7.	See 1m-m22m.


       1m-i	22mBecome  interactive	if  possible.  This flag will cause 1mcalc 22mto
	      drop into interactive mode after the 4mcalc_cmd24m arguments  on	the
	      command  line  are evaluated.  Without this flag, 1mcalc 22mwill exit
	      after they are evaluated.

	      For example:

		   calc 2+5

	      will print the value 7 and exit whereas:

		   calc -i 2+5

	      will print the value 7 and prompt the user for  more  1mcalc  22mcom-
	      mands.


       1m-m 22mmode
	      This  flag  sets	the  permission mode of 1mcalc22m.  It controls the
	      ability for 1mcalc 22mto open files and execute programs.   4mMode24m  may
	      be a number from 0 to 7.

	      The  mode  value	is interpreted in a way similar to that of the
	      1mchmod22m(1) octal mode:

		   0  do not open any file, do not execute progs
		   1  do not open any file
		   2  do not open files for reading, do not execute progs
		   3  do not open files for reading
		   4  do not open files for writing, do not execute progs
		   5  do not open files for writing
		   6  do not execute any program
		   7  allow everything (default mode)

	      If one wished to run 1mcalc 22mfrom a privileged user, one might want
	      to use 1m-m 22m0 in an effort to make 1mcalc 22msomewhat more secure.

	      Mode  bits for reading and writing apply only on an open.  Files
	      already open are not effected.  Thus if one wanted to use the 1m-m0m
	      0  in  an  effort  to  make 1mcalc 22msomewhat more secure, but still
	      wanted to read and write a specific file, one might want	to  do
	      in 1msh22m(1), 1mksh22m(1), 1mbash22m(1)-like shells:

		   calc -m 0 3<a.file

	      Files  presented	to  1mcalc  22min this way are opened in an unknown
	      mode.  1mCalc 22mwill attempt to read or write them if directed.

	      If the mode disables opening of  files  for  reading,  then  the
	      startup  resource  files	are  disabled as if 1m-q 22mwas given.	The
	      reading of key bindings is also disabled when the mode  disables
	      opening of files for reading.


       1m-O	22mUse	the  old classic defaults instead of the default configura-
	      tion.  This flag as the same effect as  executing  1mconfig("all",0m
	      1m"oldcfg") 22mat startup time.

	      NOTE: Older versions of calc used 1m-n 22mto setup a modified form of
	      the default calc configuration.	The  1m-n  22mflag  currently  does
	      nothing.	 Use  of the 1m-n 22mflag is now deprecated and may be used
	      for something else in the future.


       1m-p	22mPipe processing is enabled by use of 1m-p22m.  For example:

		   calc -p "2^21701-1" | fizzbin

	      In pipe mode, 1mcalc 22mdoes not prompt, does not print leading  tabs
	      and  does  not  print the initial header.  The 1m-p 22mflag overrides
	      1m-i22m.


       1m-q	22mDisable the reading of the startup scripts.


       1m-s	22mBy default, all 4mcalc_cmd24m args are evaluated and executed.   This
	      flag  will disable their evaluation and instead make them avail-
	      able as strings for the argv() builtin function.


       1m-u	22mDisable buffering of stdin and stdout.


       1m-v	22mPrint the 1mcalc 22mversion number and exit.


       1m--	22mThe double dash indicates to calc that no more  options  follow.
	      Thus  calc will ignore a later argument on the command line even
	      if it starts with a dash.  This is useful when entering negative
	      values on the command line as in:

		   calc -p -- -1 - -7





       CALC COMMAND LINE

       With  no  4mcalc_cmd24m	arguments,  1mcalc 22moperates interactively.  If one or
       more arguments are given on the command line and 1m-s 22mis NOT given,  then
       1mcalc  22mwill	read  and execute them and either attempt to go interactive
       according as the 1m-i 22mflag was present or absent.

       If 1m-s 22mis given, 1mcalc 22mwill  not  evaluate  any	4mcalc_cmd24m  arguments  but
       instead	make them available as strings to the argv() builtin function.

       Sufficiently simple  commands  with  no	characters  like  parentheses,
       brackets,  semicolons,  '*', which have special interpretations in UNIX
       shells may be entered, possibly with spaces, until the terminating new-
       line.  For example:

	    calc 23 + 47

       will print 70.  However, command lines will have problems:

	    calc 23 * 47

	    calc -23 + 47

       The first example above fails because the shell interprets the '*' as a
       file glob.  The second example fails because '-23' is viewed as a  calc
       option (which it is not) and do calc objects to that it thinks of as an
       unknown option.	These cases can usually be made to work as expected by
       enclosing the command between quotes:

	    calc '23 * 47'

	    calc "print sqrt(2), exp(1)"

       or in parentheses and quotes to avoid leading -'s as in:

	    calc '(-23 + 47)'

       One  may  also use a double dash to denote that calc options have ended
       as in:

	    calc -- -23 + 47

	    calc -q -- -23 + 47

       If '!' is to be used to indicate the  factorial	function,  for	shells
       like 1mcsh4m22m(1)24m for which '!' followed by a non-space character is used for
       history substitution, it may be necessary to include a space or	use  a
       backslash  to escape the special meaning of '!'.  For example, the com-
       mand:

	    print 27!^2

       may have to be replaced by:

	    print 27! ^2   or	print 27^2


       CALC STARTUP FILES

       Normally on startup, if the environment variable 1m$CALCRC  22mis  undefined
       and  1mcalc  22mis invoked without the 1m-q 22mflag, or if 1m$CALCRC 22mis defined and
       calc is invoked with 1m-e22m, 1mcalc 22mlooks for a file "startup"  in  the  calc
       resource  directory 1m.calcrc 22min the user's home directory, and 1m.calcinit0m
       1min the current directory.  22mIf one or more of these are found, they	are
       read  in  succession as 1mcalc 22mscripts and their commands executed.  When
       defined, 1m$CALCRC 22mis to contain a ':' separated list of names of  files,
       and  if	calc  is then invoked without either the 1m-q 22mor 1m-e 22mflags, these
       files are read in succession and their  commands  executed.   No  error
       condition is produced if a listed file is not found.

       If the mode specified by 1m-m 22mdisables opening of files for reading, then
       the reading of startup files is also disabled as if 1m-q 22mwas given.


       CALC FILE SEARCH PATH

       If the environment variable 1m$CALCPATH 22mis undefined, or if it is defined
       and  1mcalc  22mis  invoked with the 1m-e 22mflag, when a file name not beginning
       with 1m/22m, 1m~ 22mor 1m./22m, is specified as in:

	    calc read myfile

       1mcalc 22msearches in succession:

	    ./myfile
	    ./myfile.cal
	    /usr/lib/myfile
	    /usr/lib/myfile.cal
	    /usr/share/calc/custom/myfile
	    /usr/share/calc/custom/myfile.cal

       If the file is found, the search stops and the commands in the file are
       executed.   It  is an error if no readable file with the specified name
       is found.  An alternative search path  can  be  specified  by  defining
       1m$CALCPATH  22min  the same way as PATH is defined, as a ':' separated list
       of directories, and then invoking 1mcalc 22mwithout the 1m-e 22mflag.

       1mCalc 22mtreats all open files, other than  stdin,  stdout  and  stderr  as
       files  available for reading and writing.  One may present 1mcalc 22mwith an
       already open file using 1msh22m(1), 1mksh22m(1), 1mbash22m(1)-like shells is to:

	    calc 3<open_file 4<open_file2

       For more information use the following 1mcalc 22mcommands:

	    help help
	    help overview
	    help usage
	    help environment
	    help config




       SHELL SCRIPT MODE

       If the first line of an executable file	begins	1m#!   22mfollowed  by	the
       absolute pathname of the 1mcalc 22mprogram and the flag 1m-f 22mas in:

	    4m#!/usr/bin/calc24m [other_flags ...] 1m-f0m

       the rest of the file will be processed in 1mshell script mode22m.  Note that
       1m-f 22mmust be at the end of the initial ``#!'' line.  Any  other  optional
       1mother_flags 22mmust come before the 1m-f22m.

       In  1mshell script mode 22mthe contents of the file are read and executed as
       if they were in a file being processed by a read command, except that a
       "command"  beginning  with '#' followed by whitespace and ending at the
       next newline is treated as a comment.  Any optional 1mother_flags 22mwill be
       parsed first followed by the later lines within the script itself.

       In 1mshell script mode22m, 1m-s 22mis always assumed.  In addition, 1m-d 22mand 1m-p 22mare
       automatically set if 1m-i 22mis not given.

       For example, if the file 1m/tmp/mersenne22m:

	    4m#!/usr/bin/calc24m 1m-q -f0m
	    #
	    # mersenne - an example of a calc 1mshell script file0m

	    /* parse args */
	    if (argv() != 1) {
		fprintf(files(2), "usage: %s exp\n", config("program"));
		abort "must give one exponent arg";
	    }

	    /* print the mersenne number */
	    print "2^": argv(0) : "-1 =", 2^eval(argv(0))-1;

       is made an executable file by:

	    chmod +x /tmp/mersenne

       then the command line:

	    /tmp/mersenne 127

       will print:

	    2^127-1 = 170141183460469231731687303715884105727

       Note that because 1m-s 22mis assumed in 1mshell	script	mode  22mand  non-dashed
       args  are  made	available  as strings via the 1margv22m() builtin function.
       Therefore:

	    2^eval(argv(0))-1

       will print the decimal value of 2^n-1 but

	    2^argv(0)-1

       will not.



       DATA TYPES

       Fundamental builtin data types include integers, real numbers, rational
       numbers, complex numbers and strings.

       By  use of an object, one may define an arbitrarily complex data types.
       One may define how such objects behave a wide range of operations  such
       as addition, subtraction, multiplication, division, negation, squaring,
       modulus, rounding, exponentiation, equality, comparison,  printing  and
       so on.

       For more information use the following 1mcalc 22mcommands:

	  help types
	  help obj
	  show objfuncs



       VARIABLES

       Variables  in  4mcalc24m are typeless.  In other words, the fundamental type
       of a variable is determined by  its  content.   Before  a  variable  is
       assigned a value it has the value of zero.

       The  scope  of a variable may be global, local to a file, or local to a
       procedure.  Values may be grouped together in a matrix, or into a  list
       that permits stack and queue style operations.

       For more information use the following 1mcalc 22mcommands:

	  help variable
	  help mat
	  help list
	  show globals



       INPUT/OUTPUT

       A  leading ``0x'' implies a hexadecimal value, a leading ``0b'' implies
       a binary value, and a ``0'' followed by a digit implies an octal value.
       Complex	numbers are indicated by a trailing ``i'' such as in ``3+4i''.
       Strings may be delimited by either a pair of single or  double  quotes.
       By  default, 4mcalc24m prints values as if they were floating point numbers.
       One may change the default to print values in a number of modes includ-
       ing fractions, integers and exponentials.

       A number of stdio-like file I/O operations are provided.  One may open,
       read, write, seek and close files.  Filenames  are  subject  to	``  ''
       expansion  to  home directories in a way similar to that of the Korn or
       C-Shell.

       For example:

	  ~/.calcrc
	  ~chongo/lib/fft_multiply.cal

       For more information use the following 1mcalc 22mcommand:

	  help file



       CALC LANGUAGE

       The 4mcalc24m language is a C-like language.  The language includes commands
       such  as variable declarations, expressions, tests, labels, loops, file
       operations, function calls.  These commands are very similar  to  their
       counterparts in C.

       The  language  also  include  a	number	of commands particular to 4mcalc0m
       itself.	These include commands	such  as  function  definition,  help,
       reading	in  resource  files, dump files to a file, error notification,
       configuration control and status.

       For more information use the following 1mcalc 22mcommand:

	  help command
	  help statement
	  help expression
	  help operator
	  help config


1mFILES0m

       /usr/bin/calc
	    calc binary

       /usr/share/doc/apcalc/examples/*
	    calc shell scripts

       /usr/lib/*.cal
	    calc standard resource files

       /usr/lib/help/*
	    help files

       /usr/lib/bindings
	    non-GNU-readline command line editor bindings

       /usr/include/calc/*.h
	    include files for C interface use

       /usr/lib/libcalc.a
	    calc binary link library

       /usr/lib/libcustcalc.a
	    custom binary link library

       /usr/share/calc/custom/*.cal
	    custom resource files

       /usr/share/calc/custhelp/*
	    custom help files

1mENVIRONMENT0m

       CALCPATH
	    A :-separated list of directories used to search for calc resource
	    filenames that do not begin with /, ./ or ~.

	    Default	   value:	 .:./cal:~/.cal:/usr/local/share/calc:
	    /usr/share/calc:/usr/share/calc/custom

       CALCRC
	    On startup (unless -h or -q was given on the command  line),  1mcalc0m
	    searches for files along this :-separated environment variable.

	    Default		  value:	      /usr/share/calc/startup:
	    /usr/local/share/calc/startup:~/.calcrc:./.calcinit

       CALCBINDINGS
	    On startup (unless 4m-h24m or 4m-q24m was given on the command line,  or  4m-m0m
	    disallows opening files for reading), 1mcalc 22mreads key bindings from
	    the filename specified by  this  environment  variable.   The  key
	    binding  file is searched for along the $CALCPATH list of directo-
	    ries.

	    Default value: binding

	    This variable is not used if calc was compiled  with  GNU-readline
	    support.   In  that  case,	the  standard readline mechanisms (see
	    readline(3)) are used.

1mCREDIT0m

       The main chunk of 1mcalc 22mwas written by David I. Bell.

       The 1mcalc 22mprimary mirror, calc mailing list and calc bug report process-
       ing is performed by Landon Curt Noll.

       Landon  Curt  Noll  maintains  the  master  reference  source, performs
       release control functions as well as other calc maintenance  functions.

       Thanks  for  suggestions and encouragement from Peter Miller, Neil Jus-
       tusson, and Landon Noll.

       Thanks to Stephen Rothwell for writing the original version  of	hist.c
       which is used to do the command line editing.

       Thanks  to  Ernest W. Bowen for supplying many improvements in accuracy
       and generality for some numeric functions.  Much of this was  in  terms
       of  actual  code which I gratefully accepted.  Ernest also supplied the
       original text for many of the help files.

       Portions of this program are derived from  an  earlier  set  of	public
       domain  arbitrarily  precision  routines  which	was  posted to the net
       around 1984.  By now, there is almost no recognizable  code  left  from
       that original source.

1mCOPYING / CALC GNU LESSER GENERAL PUBLIC LICENSE0m


       Calc  is  open  software,  and  is covered under version 2.1 of the GNU
       Lesser General Public License.  You are welcome	to  change  it	and/or
       distribute copies of it under certain conditions.  The calc commands:

	    help copyright
	    help copying
	    help copying-lgpl

       should  display	the  contents  of  the COPYING and COPYING-LGPL files.
       Those files contain information about the  calc's  GNU  Lesser  General
       Public  License,  and  in particular the conditions under which you are
       allowed to change it and/or distribute copies of it.

       You should have received a copy of the version 2.1 of  the  GNU	Lesser
       General Public License.	If you do not have these files, write to:

	    Free Software Foundation, Inc.
	    51 Franklin Street
	    Fifth Floor
	    Boston, MA	02110-1301
	    USA

       Calc is copyrighted in several different ways.  These ways include:

	    Copyright (C) year	David I. Bell
	    Copyright (C) year	David I. Bell and Landon Curt Noll
	    Copyright (C) year	David I. Bell and Ernest Bowen
	    Copyright (C) year	David I. Bell, Landon Curt Noll and Ernest Bowen
	    Copyright (C) year	Landon Curt Noll
	    Copyright (C) year	Ernest Bowen and Landon Curt Noll
	    Copyright (C) year	Ernest Bowen

       This man page is:

	    Copyright (C) 1999	Landon Curt Noll

       and is covered under version 2.1 GNU Lesser General Public License.

1mCALC MAILING LIST / CALC UPDATES / ENHANCEMENTS0m


       To  contribute comments, suggestions, enhancements and interesting 1mcalc0m
       resource files, and shell scripts please join the low volume calc mail-
       ing list.

       To join the low volume calc mailing list, send EMail to:

	    calc-tester-request at asthe dot com

       Your subject must contain the words:

	    calc mailing list subscription

       You may have additional words in your subject line.

       Your message body must contain:

	    subscribe calc-tester address
	    end
	    name your_full_name

       where  1maddress  22ms  your  EMail  address and 1myour_full_name 22mis your full
       name.  Feel free to follow the 1mname 22mline with additional EMail text  as
       desired.

1mBUG REPORTS / BUG FIXES0m


       Send bug reports and bug fixes to:

	    calc-bugs at asthe dot com

	    [[ NOTE: Replace 'at' with @, 'dot' is with . and remove the spaces ]]
	    [[ NOTE: The EMail address uses 'asthe' and the web site URL uses 'isthe' ]]

       Your subject must contain the words:

	    calc bug report

       You may have additional words in your subject line.

       See the 4mBUGS24m source file or use the 4mcalc24m command:

	    help bugs

       for more information about bug reporting.

1mCALC WEB SITE0m

       Landon Noll maintains the 1mcalc 22mweb site is located at:

	    www.isthe.com/chongo/tech/comp/calc/

       Share and Enjoy! :-)



2007-02-06			     ^..^			       calc(1)
