calc(1)                     General Commands Manual                    calc(1)

NNAAMMEE
       calc - arbitrary precision calculator

SSYYNNOOPPSSIISS
       _c_a_l_c [--cc] [--CC] [--dd]
            [--DD ccaallcc__ddeebbuugg[[::rreessoouurrccee__ddeebbuugg[[::uusseerr__ddeebbuugg]]]]]
            [--ee] [--ff ffiilleennaammee] [--hh] [--ii] [--mm mmooddee] [--OO]
            [--pp] [--qq] [--ss] [--uu] [--vv] [[[----]] ccaallcc__ccmmdd ......]

       _#_!_/_o_p_t_/_l_o_c_a_l_/_b_i_n_/_c_a_l_c [optional_other_flags ...] --ff


DDEESSCCRRIIPPTTIIOONN
       --cc     Continue 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 ccaallcc to abort on the first syntax error, whereas:

                   calc -c read many_errors.cal

              will cause ccaallcc to 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 --dd is also given.


       --CC     Permit 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 ccaallcc to execute functions that are
              non-standard and that are not portable.  Custom builtin
              functions are disabled by default for this reason.


       --dd     Disable the printing of the opening title.  The printing of
              resource file debug and informational messages is also disabled
              as if ccoonnffiigg((""rreessoouurrccee__ddeebbuugg"",, 00)) had 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
              resource files.


              This flag also disables the printing the leading tilde. For
              example:

                   calc 2/3

              will print:

                   ~0.66666666666666666667


              whereas:

                   calc -d 2/3

              will just print:

                   0.66666666666666666667



       --DD calc_debug[:resource_debug[:user_debug]]
              Force the initial value of config("calc_debug"),
              config("resource_debug") and config("user_debug").

              The : separated strings are interpreted as signed 32 bit
              integers.  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, _c_a_l_c___d_e_b_u_g is 0, _r_e_s_o_u_r_c_e___d_e_b_u_g is 3 and _u_s_e_r___d_e_b_u_g
              is 0.

              For more information use the following ccaallcc command:

                   help config


       --ee     Ignore any environment variables on startup.  The getenv()
              builtin will still return values, however.


       --ff filename
              This flag is normally only with calc shell scripts.

              This flag is required when using calc in sshheellll ssccrriipptt mmooddee.  It
              must be at the end of the initial ##!!  line of the script, as in:

                   _#_!_/_o_p_t_/_l_o_c_a_l_/_b_i_n_/_c_a_l_c [optional_other_flags ...] --ff

              the rest of the file will be processed in sshheellll ssccrriipptt mmooddee.


              A common flag to use, prior to the --ff on the #! line is the --qq
              flag.  For example:

                   _#_!_/_o_p_t_/_l_o_c_a_l_/_b_i_n_/_c_a_l_c --qq --ff


              See SSHHEELLLL SSCCRRIIPPTT MMOODDEE section of this man page below for
              details.

              While the actual form of this flag is:

                   --ff filename

              for systems that treat an executable that begins with ##!!  as 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
              --ff flag at the very end of the ##!!  line.

              It is possible use --ff filename on the command line:

                   _c_a_l_c [optional_other_flags ...] --ff filename

              This will cause calc to process lines in ffiilleennaammee in sshheellll
              ssccrriipptt mmooddee.


              NNOOTTEE:: The use of --ff does NNOOTT imply --qq and thus one would need to
              use

                   _c_a_l_c [optional_other_flags ...] --qq --ff filename

              to disable the use of calc startup files as well.


              The use of --ff filename implies the --ss flag.


       --hh     Print a help message.  This option implies --qq.  This is
              equivalent to the ccaallcc command help help.  The help facility is
              disabled unless the mode is 5 or 7.  See --mm.


       --ii     Become interactive if possible.  This flag will cause ccaallcc to
              drop into interactive mode after the _c_a_l_c___c_m_d arguments on the
              command line are evaluated.  Without this flag, ccaallcc will 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 ccaallcc
              commands.


       --mm mode
              This flag sets the permission mode of ccaallcc.  It controls the
              ability for ccaallcc to open files and execute programs.  _M_o_d_e may
              be a number from 0 to 7.

              The mode value is interpreted in a way similar to that of the
              cchhmmoodd(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 ccaallcc from a privileged user, one might want
              to use --mm 0 in an effort to make ccaallcc somewhat 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 --mm
              0 in an effort to make ccaallcc somewhat more secure, but still
              wanted to read and write a specific file, one might want to do
              in sshh(1), kksshh(1), bbaasshh(1)-like shells:

                   calc -m 0 3<a.file

              Files presented to ccaallcc in this way are opened in an unknown
              mode.  CCaallcc will 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 --qq was given.  The
              reading of key bindings is also disabled when the mode disables
              opening of files for reading.


       --OO     Use the old classic defaults instead of the default
              configuration.  This flag as the same effect as executing
              ccoonnffiigg((""aallll"",, ""oollddccffgg"")) at startup time.

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


       --pp     Pipe processing is enabled by use of --pp.  For example:

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

              In pipe mode, ccaallcc does not prompt, does not print leading tabs
              and does not print the initial header.  The --pp flag overrides
              --ii.


              When running calc as a shell script (see SSHHEELLLL SSCCRRIIPPTT MMOODDEE for
              details), calc will close standard input (stdin) during startup
              UUNNLLEESSSS the --pp flag is given on the command line.  When calc is
              running in sshheellll ssccrriipptt mmooddee, shell scripts that call the
              pprroommpptt(str) builtin will not work properly (the prompt builtin
              will always fail) unless the --pp flag is given on the command
              line.


       --qq     Disable the reading of the startup scripts.

              This allows the script to run independently of startup scripts
              such those managed by the $$CCAALLCCRRCC environment variable.  For
              example, this will disable the use of the common calcrc file
              (usually ~/.calcrc).


       --ss     By default, all _c_a_l_c___c_m_d args are evaluated and executed.  This
              flag will disable their evaluation and instead make them
              available as strings for the argv() builtin function.


       --uu     Disable buffering of stdin and stdout.


       --vv     Print the ccaallcc version number and exit.


       ----     The 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



CCAALLCC CCOOMMMMAANNDD LLIINNEE
       With no _c_a_l_c___c_m_d arguments, ccaallcc operates interactively.  If one or
       more arguments are given on the command line and --ss is NOT given, then
       ccaallcc will read and execute them and either attempt to go interactive
       according as the --ii flag was present or absent.

       If --ss is given, ccaallcc will not evaluate any _c_a_l_c___c_m_d 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
       newline.  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 ccsshh_(_1_) 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
       command:

            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p, the interactive parser, in an effort to
       parse interactive commands, flushes data on standard input.


CCAALLCC SSTTAARRTTUUPP FFIILLEESS
       Normally on startup, if the environment variable $$CCAALLCCRRCC is undefined
       and ccaallcc is invoked without the --qq flag, or if $$CCAALLCCRRCC is defined and
       calc is invoked with --ee, ccaallcc looks for a file "startup" in the calc
       resource directory ..ccaallccrrcc in the user's home directory, and ..ccaallcciinniitt
       iinn tthhee ccuurrrreenntt ddiirreeccttoorryy..  If one or more of these are found, they are
       read in succession as ccaallcc scripts and their commands executed.  When
       defined, $$CCAALLCCRRCC is to contain a ':' separated list of names of files,
       and if calc is then invoked without either the --qq or --ee flags, 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 --mm disables opening of files for reading, then
       the reading of startup files is also disabled as if --qq was given.


CCAALLCC FFIILLEE SSEEAARRCCHH PPAATTHH
       If the environment variable $$CCAALLCCPPAATTHH is undefined, or if it is defined
       and ccaallcc is invoked with the --ee flag, when a file name not beginning
       with //, ~~ or ..//, is specified as in:

            calc read myfile

       ccaallcc searches in succession:

            ./myfile
            ./myfile.cal
            /opt/local/lib/myfile
            /opt/local/lib/myfile.cal
            /opt/local/share/calc/custom/myfile
            /opt/local/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
       $$CCAALLCCPPAATTHH in the same way as PATH is defined, as a ':' separated list
       of directories, and then invoking ccaallcc without the --ee flag.

       CCaallcc treats all open files, other than stdin, stdout and stderr as
       files available for reading and writing.  One may present ccaallcc with an
       already open file using sshh(1), kksshh(1), bbaasshh(1)-like shells is to:

            calc 3<open_file 4<open_file2

       For more information use the following ccaallcc commands:

            help help
            help overview
            help usage
            help environment
            help config



SSHHEELLLL SSCCRRIIPPTT MMOODDEE
       If the first line of an executable file begins ##!!  followed by the
       absolute pathname of the ccaallcc program and the first line ends with the
       flag --ff as in:

            _#_!_/_o_p_t_/_l_o_c_a_l_/_b_i_n_/_c_a_l_c [optional_other_flags ...] --ff

       the rest of the file will be processed in sshheellll ssccrriipptt mmooddee.  Note that
       --ss --ff must be at the end of the initial ``#!'' line.  Any other
       optional ooppttiioonnaall__ootthheerr__ffllaaggss must come before the --ff flag.

       In sshheellll ssccrriipptt mmooddee the 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
       ooppttiioonnaall__ootthheerr__ffllaaggss will be parsed first followed by the later lines
       within the script itself.

       In sshheellll ssccrriipptt mmooddee, --ss is always assumed.  In addition, --dd and --pp are
       automatically set if --ii is not given.

       NNOOTTEE:: The use of --ff does NNOOTT imply --qq and thus one would need to use

            _c_a_l_c [optional_other_flags ...] --qq --ff filename

       to disable the use of calc startup files as well.

       For example, if the file //ttmmpp//mmeerrsseennnnee:

            _#_!_/_o_p_t_/_l_o_c_a_l_/_b_i_n_/_c_a_l_c --qq --ff

            /* 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 --ss is required in sshheellll ssccrriipptt mmooddee non-dashed args
       are made available as strings via the aarrggvv() builtin function.
       Therefore:

            2^eval(argv(1))-1

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

            2^argv(1)-1

       will not.


       By default, using calc startup in sshheellll ssccrriipptt mmooddee, calc will close
       standard input (stdin).  Thus builtin functions such as pprroommpptt(str)
       will fail (return a null value).  Calc shell scripts that call the
       pprroommpptt(str) builtin will not work properly (the prompt builtin will
       always fail and return a null value) unless the --pp flag is given on the
       command line.


       The following shell script will always print "got null" because stdin
       will be closed by calc during startup:


            #!/usr/bin/calc -q -f

            # The prompt will ALWAYS FAIL and return a null value
            n = prompt("Test> ");
            if (isnull(n)) {
              print("got null");
            } else {
              print("got " + n);
            }

       However the following shell script (note the --pp before the --ff in the
       1st line) will be interactive, prompt with "Test> " and print the "got"
       result as expected:


            #!/usr/bin/calc -q -p -f

            n = prompt("Test> ");
            if (isnull(n)) {
              print("got null");
            } else {
              print("got " + n);
            }


       Note in the example above, the --pp flag must be given before the final
       --ff command line flag.


DDAATTAA TTYYPPEESS
       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 ccaallcc commands:

          help types
          help obj
          show objfuncs


VVAARRIIAABBLLEESS
       Variables in _c_a_l_c 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 ccaallcc commands:

          help variable
          help mat
          help list
          show globals


IINNPPUUTT//OOUUTTPPUUTT
       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, _c_a_l_c prints values as if they were floating point numbers.
       One may change the default to print values in a number of modes
       including 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 ccaallcc command:

          help file


CCAALLCC LLAANNGGUUAAGGEE
       The _c_a_l_c 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 _c_a_l_c
       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 ccaallcc command:

          help command
          help statement
          help expression
          help operator
          help config


FFIILLEESS
       /opt/local/bin/calc
            calc binary

       /opt/local/bin/cscript/*
            calc shell scripts

       /opt/local/lib/*.cal
            calc standard resource files

       /opt/local/lib/help/*
            help files

       /opt/local/lib/bindings
            non-GNU-readline command line editor bindings

       /opt/local/include/calc/*.h
            include files for C interface use

       /opt/local/lib/libcalc.a
            calc binary link library

       /opt/local/lib/libcustcalc.a
            custom binary link library

       /opt/local/share/calc/custom/*.cal
            custom resource files

       /opt/local/share/calc/custhelp/*
            custom help files


EENNVVIIRROONNMMEENNTT
       CALCPATH
            A :-separated list of directories used to search for calc resource
            filenames that do not begin with /, ./ or ~.

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

       CALCRC
            On startup (unless -h or -q was given on the command line), ccaallcc
            searches for files along this :-separated environment variable.

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

       CALCBINDINGS
            On startup (unless _-_h or _-_q was given on the command line, or _-_m
            disallows opening files for reading), ccaallcc reads key bindings from
            the filename specified by this environment variable.  The key
            binding file is searched for along the $CALCPATH list of
            directories.

            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: /opt/local/share/calc/help

       CALCCUSTOMHELP
            Location of the calc custom help directory.

            Default value: /opt/local/share/calc/custhelp


CCRREEDDIITT
       The main chunk of ccaallcc was written by David I. Bell.

       The ccaallcc primary mirror, and calc bug report processing 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
       Justusson, 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.

CCOOPPYYIINNGG // CCAALLCC GGNNUU LLEESSSSEERR GGEENNEERRAALL PPUUBBLLIICC LLIICCEENNSSEE
       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
       information on this license, see:

            https://unlicense.org


CCAALLCC QQUUEESSTTIIOONNSS
       To ask the calc maintainers a general question about calc, see the
       output 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


BBUUGG RREEPPOORRTTSS // BBUUGG FFIIXXEESS
       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


CCOONNTTRRIIBBUUTTIINNGG CCOODDEE TTOO CCAALLCC
       _C_a_l_c 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


CCAALLCC WWEEBB SSIITTEE
       Landon Noll maintains the ccaallcc web site is located at:

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

CCAALLCC SSOOUURRCCEE CCOODDEE
       The latest version of calc source code may be obtained at the following
       GitHub repo:

            https://github.com/lcn2/calc

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

       You may download the calc releases from:

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

       The releases marked ((((PPrree--rreelleeaassee)))) are beta releases.

       The most recent release that is NOT  marked ((((PPrree--rreelleeaassee)))) is the
       recommended stable release.

Share and enjoy!  :-)                ^..^                              calc(1)
