4mcalc24m(1)			 General Commands Manual		    4mcalc24m(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-f filename22m] [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 [optional_other_flags ...] 1m-f0m


1mDESCRIPTION0m
       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	re‐
	      source 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 print:

		   It's nearly ten past six.

	      This  flag  disables  the	 reporting of missing calc startup re‐
	      source files.


	      This flag also disables the printing the leading tilde. For  ex‐
	      ample:

		   calc 2/3

	      will print:

		   ~0.66666666666666666667


	      whereas:

		   calc -d 2/3

	      will just print:

		   0.66666666666666666667



       1m-D 22mcalc_debug[:resource_debug[:user_debug]]
	      Force  the  initial  value  of config("calc_debug"), config("re‐
	      source_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 22mfilename
	      This flag is normally only with calc shell scripts.

	      This  flag is required when using calc in 1mshell script mode22m.	 It
	      must be at the end of the initial 1m#!  22mline of the script, as in:

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

	      the rest of the file will be processed in 1mshell script mode22m.


	      A common flag to use, prior to the 1m-f 22mon the #! line is  the	 1m-q0m
	      flag.  For example:

		   4m#!/usr/bin/calc24m 1m-q -f0m


	      See  1mSHELL  SCRIPT  MODE  22msection of this man page below for de‐
	      tails.

	      While the actual form of this flag is:

		   1m-f 22mfilename

	      for 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 22mfilename on the command line:

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

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


	      1mNOTE: 22mThe use of 1m-f 22mdoes 1mNOT 22mimply 1m-q 22mand thus one would need to
	      use

		   4mcalc24m [optional_other_flags ...] 1m-q -f 22mfilename

	      to disable the use of calc startup files as well.


	      The use of 1m-f 22mfilename implies the 1m-s 22mflag.


       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.

	      This allows the script to run independently of  startup  scripts
	      such those managed by the 1m$CALCRC 22menvironment variable.  For ex‐
	      ample, this will disable the use of the common calcrc file (usu‐
	      ally ~/.calcrc).


       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



1mCALC COMMAND LINE0m
       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  in‐
       stead 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

       Reading from standard input when calc is part of a pipe works  as  long
       as  the	-p flag is given to calc.  For example, this will print chongo
       was here:

	    echo chongo was here | calc -p 'print fgetline(files(0));'


       while this does not:

	    echo chongo was here | calc 'print fgetline(files(0));'


       nor will this print chongo was here:

	    echo chongo was here | calc -i 'print fgetline(files(0));'


       This is because without -p, the interactive parser,  in	an  effort  to
       parse interactive commands, flushes data on standard input.


1mCALC STARTUP FILES0m
       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.


1mCALC FILE SEARCH PATH0m
       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



1mSHELL SCRIPT MODE0m
       If the first line of an executable file begins 1m#!  22mfollowed by the	ab‐
       solute  pathname	 of  the 1mcalc 22mprogram and the first line ends with the
       flag 1m-f 22mas in:

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

       the rest of the file will be processed in 1mshell script mode22m.  Note that
       1m-s -f 22mmust be at the end of the initial ``#!''  line.   Any	 other	op‐
       tional 1moptional_other_flags 22mmust come before the 1m-f 22mflag.

       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  1mop‐0m
       1mtional_other_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.

       1mNOTE: 22mThe use of 1m-f 22mdoes 1mNOT 22mimply 1m-q 22mand thus one would need to use

	    4mcalc24m [optional_other_flags ...] 1m-q -f 22mfilename

       to disable the use of calc startup files as well.

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

	    4m#!/usr/bin/calc24m 1m-q -f0m

	    /* setup */
	    argc = argv();
	    program = argv(0);
	    stderr = files(2);

	    /* parse args */
	    if (argc != 2) {
		fprintf(stderr, "usage: %s exp0, program);
		abort "must give one exponent arg";
	    }
	    exp = eval(argv(1));
	    if (!isint(exp) || exp < 0) {
		fprintf(stderr, "%s: exp must be non-negative integer0, program);
		abort "must give one exponent arg";
	    }

	    /* print the mersenne number */
	    print "2^": exp : "-1 =", 2^exp-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 required in 1mshell script mode 22mnon-dashed args
       are made available as strings via the 1margv22m() builtin function.   There‐
       fore:

	    2^eval(argv(1))-1

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

	    2^argv(1)-1

       will not.


1mDATA TYPES0m
       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


1mVARIABLES0m
       Variables in 4mcalc24m are typeless.  In other words, the  fundamental  type
       of  a  variable is determined by its content.  Before a variable is as‐
       signed 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


1mINPUT/OUTPUT0m
       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 `` ''  ex‐
       pansion	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


1mCALC LANGUAGE0m
       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  4mcalc24m	it‐
       self.   These include commands such as function definition, help, read‐
       ing in resource files, dump files to a file, error  notification,  con‐
       figuration 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/calc/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:		./.calcinit:~/.calcrc:/usr/lo‐
	    cal/share/calc/startup:/usr/share/calc/startup

       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.

       CALCHISTFILE
	    Location of the calc history file.

	    Default value: ~/.calc_history

	    This  variable  is not used if calc was compiled with GNU-readline
	    support.

       CALCHELP
	    Location of the calc help directory.

	    Default value: /usr/share/calc/help

       CALCCUSTOMHELP
	    Location of the calc custom help directory.

	    Default value: /usr/share/calc/custhelp


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

       The 1mcalc 22mprimary mirror, and calc bug report processing is performed by
       Landon Curt Noll.

       Landon Curt Noll maintains the master reference	source,	 performs  re‐
       lease 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  do‐
       main  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
	    help credit

       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
	    Copyright (C) year	Petteri Kettunen and Landon Curt Noll
	    Copyright (C) year	Christoph Zurnieden
	    Copyright (C) year	Landon Curt Noll and Thomas Jones-Low
	    Copyright (C) year	Klaus Alexander Seistrup and Landon Curt Noll

       This man page is:

	    Copyright (C) 1999-2023  Landon Curt Noll

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

       A few files in calc are covered under "The Unlicense".  For more infor‐
       mation on this license, see:

	    https://unlicense.org


1mCALC QUESTIONS0m
       To ask the calc maintainers a general question about calc, see the out‐
       put of the following calc command:


	    ; help question

       or read the source file:


	    QUESTIONS


       or visit the following URL:

	    https://github.com/lcn2/calc/blob/master/QUESTIONS


1mBUG REPORTS / BUG FIXES0m
       To inform the calc maintainers about a bug, or to submit a bug fix, see
       the output of the following calc command:


	    ; help bugs

       or read the source file:


	    BUGS

       or visit the following URL:

	    https://github.com/lcn2/calc/blob/master/BUGS


1mCONTRIBUTING CODE TO CALC0m
       4mCalc24m  is  open  source.  You are welcome to contribute code to calc, or
       submit bug fixes to to calc.

       To contribute code to calc, please see see the output of the  following
       calc command:


	    ; help contrib


       or read the source file:


	    CONTRIB-CODE

       or visit the following URL:

	    https://github.com/lcn2/calc/blob/master/CONTRIB-CODE


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

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

1mCALC SOURCE CODE0m
       The latest version of calc source code may be obtained at the following
       GitHub repo:

	    https://github.com/lcn2/calc

       The very latest calc souece is found at the top master branch.

       You may download the calc releases from:

	    https://github.com/lcn2/calc/releases

       The releases marked 1m((Pre-release)) 22mare beta releases.

       The most recent release that is NOT  marked 1m((Pre-release)) 22mis the rec‐
       ommended stable release.

Share and enjoy!  :-)		     ^..^			       4mcalc24m(1)
