1calc(1)                     General Commands Manual                    calc(1)
2
3
4

NAME

6       calc - arbitrary precision calculator
7

SYNOPSIS

9       calc [-c] [-C] [-d]
10            [-D calc_debug[:resource_debug[:user_debug]]]
11            [-e] [-f filename] [-h] [-i] [-m mode] [-O]
12            [-p] [-q] [-s] [-u] [-v] [[--] calc_cmd ...]
13
14       #!/usr/bin/calc [optional_other_flags ...] -f
15
16

DESCRIPTION

18       -c     Continue reading command lines even after a scan/parse error has
19              caused the abandonment of a line.  Note that  this  option  only
20              deals  with  scanning and parsing of the calc language.  It does
21              not deal with execution or run-time errors.
22
23              For example:
24
25                   calc read many_errors.cal
26
27              will cause calc to abort on the first syntax error, whereas:
28
29                   calc -c read many_errors.cal
30
31              will cause calc to try to process each line being  read  despite
32              the scan/parse errors that it encounters.
33
34              By  default, calc startup resource files are silently ignored if
35              not found.  This flag will report missing startup resource files
36              unless -d is also given.
37
38
39       -C     Permit  the execution of custom builtin functions.  Without this
40              flag, calling the custom() builtin function will simply generate
41              an error.
42
43              Use  of  this  flag may cause calc to execute functions that are
44              non-standard and that are not portable.   Custom  builtin  func‐
45              tions are disabled by default for this reason.
46
47
48       -d     Disable  the printing of the opening title.  The printing of re‐
49              source file debug and informational messages is also disabled as
50              if config("resource_debug", 0) had been executed.
51
52              For example:
53
54                   calc "read qtime; qtime(2)"
55
56              will output something like:
57
58                   qtime(utc_hr_offset) defined
59                   It's nearly ten past six.
60
61              whereas:
62
63                   calc -d "read qtime; qtime(2)"
64
65              will just print:
66
67                   It's nearly ten past six.
68
69              This  flag  disables  the  reporting of missing calc startup re‐
70              source files.
71
72
73              This flag also disables the printing the leading tilde. For  ex‐
74              ample:
75
76                   calc 2/3
77
78              will print:
79
80                   ~0.66666666666666666667
81
82
83              whereas:
84
85                   calc -d 2/3
86
87              will just print:
88
89                   0.66666666666666666667
90
91
92
93       -D calc_debug[:resource_debug[:user_debug]]
94              Force  the  initial  value  of config("calc_debug"), config("re‐
95              source_debug") and config("user_debug").
96
97              The : separated strings are interpreted as signed 32  bit  inte‐
98              gers.   After  an optional leading sign a leading zero indicates
99              octal conversion, and a leading  ``0x''  or  ``0X''  hexadecimal
100              conversion.  Otherwise, decimal conversion is assumed.
101
102              By  default, calc_debug is 0, resource_debug is 3 and user_debug
103              is 0.
104
105              For more information use the following calc command:
106
107                   help config
108
109
110       -e     Ignore any  environment  variables  on  startup.   The  getenv()
111              builtin will still return values, however.
112
113
114       -f filename
115              This flag is normally only with calc shell scripts.
116
117              This  flag is required when using calc in shell script mode.  It
118              must be at the end of the initial #!  line of the script, as in:
119
120                   #!/usr/bin/calc [optional_other_flags ...] -f
121
122              the rest of the file will be processed in shell script mode.
123
124
125              A common flag to use, prior to the -f on the #! line is  the  -q
126              flag.  For example:
127
128                   #!/usr/bin/calc -q -f
129
130
131              See  SHELL  SCRIPT  MODE  section of this man page below for de‐
132              tails.
133
134              While the actual form of this flag is:
135
136                   -f filename
137
138              for systems that treat an executable that begins with #!   as  a
139              script,  the path of the executable is appended by the kernel as
140              the final argument to the exec() system call.  This is  why  the
141              -f flag at the very end of the #!  line.
142
143              It is possible use -f filename on the command line:
144
145                   calc [optional_other_flags ...] -f filename
146
147              This  will  cause  calc  to  process  lines in filename in shell
148              script mode.
149
150
151              NOTE: The use of -f does NOT imply -q and thus one would need to
152              use
153
154                   calc [optional_other_flags ...] -q -f filename
155
156              to disable the use of calc startup files as well.
157
158
159              The use of -f filename implies the -s flag.
160
161
162       -h     Print  a help message.  This option implies -q.  This is equiva‐
163              lent to the calc command help help.  The help facility  is  dis‐
164              abled unless the mode is 5 or 7.  See -m.
165
166
167       -i     Become  interactive  if  possible.  This flag will cause calc to
168              drop into interactive mode after the calc_cmd arguments  on  the
169              command  line  are evaluated.  Without this flag, calc will exit
170              after they are evaluated.
171
172              For example:
173
174                   calc 2+5
175
176              will print the value 7 and exit whereas:
177
178                   calc -i 2+5
179
180              will print the value 7 and prompt the user for  more  calc  com‐
181              mands.
182
183
184       -m mode
185              This  flag  sets  the  permission mode of calc.  It controls the
186              ability for calc to open files and execute programs.   Mode  may
187              be a number from 0 to 7.
188
189              The  mode  value  is interpreted in a way similar to that of the
190              chmod(1) octal mode:
191
192                   0  do not open any file, do not execute progs
193                   1  do not open any file
194                   2  do not open files for reading, do not execute progs
195                   3  do not open files for reading
196                   4  do not open files for writing, do not execute progs
197                   5  do not open files for writing
198                   6  do not execute any program
199                   7  allow everything (default mode)
200
201              If one wished to run calc from a privileged user, one might want
202              to use -m 0 in an effort to make calc somewhat more secure.
203
204              Mode  bits for reading and writing apply only on an open.  Files
205              already open are not effected.  Thus if one wanted to use the -m
206              0  in  an  effort  to  make calc somewhat more secure, but still
207              wanted to read and write a specific file, one might want  to  do
208              in sh(1), ksh(1), bash(1)-like shells:
209
210                   calc -m 0 3<a.file
211
212              Files  presented  to  calc  in this way are opened in an unknown
213              mode.  Calc will attempt to read or write them if directed.
214
215              If the mode disables opening of  files  for  reading,  then  the
216              startup  resource  files  are  disabled as if -q was given.  The
217              reading of key bindings is also disabled when the mode  disables
218              opening of files for reading.
219
220
221       -O     Use  the  old classic defaults instead of the default configura‐
222              tion.  This flag as the same effect as  executing  config("all",
223              "oldcfg") at startup time.
224
225              NOTE: Older versions of calc used -n to setup a modified form of
226              the default calc configuration.   The  -n  flag  currently  does
227              nothing.   Use  of the -n flag is now deprecated and may be used
228              for something else in the future.
229
230
231       -p     Pipe processing is enabled by use of -p.  For example:
232
233                   calc -p "2^21701-1" | fizzbin
234
235              In pipe mode, calc does not prompt, does not print leading  tabs
236              and  does  not  print the initial header.  The -p flag overrides
237              -i.
238
239
240       -q     Disable the reading of the startup scripts.
241
242              This allows the script to run independently of  startup  scripts
243              such those managed by the $CALCRC environment variable.  For ex‐
244              ample, this will disable the use of the common calcrc file (usu‐
245              ally ~/.calcrc).
246
247
248       -s     By  default, all calc_cmd args are evaluated and executed.  This
249              flag will disable their evaluation and instead make them  avail‐
250              able as strings for the argv() builtin function.
251
252
253       -u     Disable buffering of stdin and stdout.
254
255
256       -v     Print the calc version number and exit.
257
258
259       --     The  double  dash  indicates to calc that no more option follow.
260              Thus calc will ignore a later argument on the command line  even
261              if it starts with a dash.  This is useful when entering negative
262              values on the command line as in:
263
264                   calc -p -- -1 - -7
265
266
267

CALC COMMAND LINE

269       With no calc_cmd arguments, calc operates  interactively.   If  one  or
270       more  arguments are given on the command line and -s is NOT given, then
271       calc will read and execute them and either attempt  to  go  interactive
272       according as the -i flag was present or absent.
273
274       If  -s  is given, calc will not evaluate any calc_cmd arguments but in‐
275       stead make them available as strings to the argv() builtin function.
276
277       Sufficiently simple commands with no no  characters  like  parentheses,
278       brackets,  semicolons,  '*', which have special interpretations in UNIX
279       shells may be entered, possibly with spaces, until the terminating new‐
280       line.  For example:
281
282            calc 23 + 47
283
284       will print 70.  However, command lines will have problems:
285
286            calc 23 * 47
287
288            calc -23 + 47
289
290       The first example above fails because the shell interprets the '*' as a
291       file glob.  The second example fails because '-23' is viewed as a  calc
292       option (which it is not) and do calc objects to that it thinks of as an
293       unknown option.  These cases can usually be made to work as expected by
294       enclosing the command between quotes:
295
296            calc '23 * 47'
297
298            calc "print sqrt(2), exp(1)"
299
300       or in parentheses and quotes to avoid leading -'s as in:
301
302            calc '(-23 + 47)'
303
304       One  may  also use a double dash to denote that calc options have ended
305       as in:
306
307            calc -- -23 + 47
308
309            calc -q -- -23 + 47
310
311       If '!' is to be used to indicate the  factorial  function,  for  shells
312       like csh(1) for which '!' followed by a non-space character is used for
313       history substitution, it may be necessary to include a space or  use  a
314       backslash  to escape the special meaning of '!'.  For example, the com‐
315       mand:
316
317            print 27!^2
318
319       may have to be replaced by:
320
321            print 27! ^2   or   print 27\!^2
322
323       Reading from standard input when calc is part of a pipe works  as  long
324       as  the  -p flag is given to calc.  For example, this will print chongo
325       was here:
326
327            echo chongo was here | calc -p 'print fgetline(files(0));'
328
329
330       while this does not:
331
332            echo chongo was here | calc 'print fgetline(files(0));'
333
334
335       nor will this print chongo was here:
336
337            echo chongo was here | calc -i 'print fgetline(files(0));'
338
339
340       This is because without -p, the interactive parser,  in  an  effort  to
341       parse interactive commands, flushes data on standard input.
342
343

CALC STARTUP FILES

345       Normally  on  startup, if the environment variable $CALCRC is undefined
346       and calc is invoked without the -q flag, or if $CALCRC is  defined  and
347       calc  is  invoked  with -e, calc looks for a file "startup" in the calc
348       resource directory .calcrc in the user's home directory, and  .calcinit
349       in  the current directory.  If one or more of these are found, they are
350       read in succession as calc scripts and their commands  executed.   When
351       defined,  $CALCRC is to contain a ':' separated list of names of files,
352       and if calc is then invoked without either the -q or  -e  flags,  these
353       files  are  read  in  succession and their commands executed.  No error
354       condition is produced if a listed file is not found.
355
356       If the mode specified by -m disables opening of files for reading, then
357       the reading of startup files is also disabled as if -q was given.
358
359

CALC FILE SEARCH PATH

361       If the environment variable $CALCPATH is undefined, or if it is defined
362       and calc is invoked with the -e flag, when a file  name  not  beginning
363       with /, ~ or ./, is specified as in:
364
365            calc read myfile
366
367       calc searches in succession:
368
369            ./myfile
370            ./myfile.cal
371            /usr/lib64/myfile
372            /usr/lib64/myfile.cal
373            /usr/share/calc/custom/myfile
374            /usr/share/calc/custom/myfile.cal
375
376       If the file is found, the search stops and the commands in the file are
377       executed.  It is an error if no readable file with the  specified  name
378       is  found.   An  alternative  search  path can be specified by defining
379       $CALCPATH in the same way as PATH is defined, as a ':'  separated  list
380       of directories, and then invoking calc without the -e flag.
381
382       Calc  treats  all  open  files,  other than stdin, stdout and stderr as
383       files available for reading and writing.  One may present calc with  an
384       already open file using sh(1), ksh(1), bash(1)-like shells is to:
385
386            calc 3<open_file 4<open_file2
387
388       For more information use the following calc commands:
389
390            help help
391            help overview
392            help usage
393            help environment
394            help config
395
396
397

SHELL SCRIPT MODE

399       If  the first line of an executable file begins #!  followed by the ab‐
400       solute pathname of the calc program and the first line  ends  with  the
401       flag -f as in:
402
403            #!/usr/bin/calc [optional_other_flags ...] -f
404
405       the rest of the file will be processed in shell script mode.  Note that
406       -s -f must at the end of the initial ``#!'' line.  Any  other  optional
407       optional_other_flags must come before the -f flag.
408
409       In  shell script mode the contents of the file are read and executed as
410       if they were in a file being processed by a read command, except that a
411       "command"  beginning  with '#' followed by whitespace and ending at the
412       next  newline  is   treated   as   a   comment.    Any   optional   op‐
413       tional_other_flags  will  be  parsed  first followed by the later lines
414       within the script itself.
415
416       In shell script mode, -s is always assumed.  In addition, -d and -p are
417       automatically set if -i is not given.
418
419       NOTE: The use of -f does NOT imply -q and thus one would need to use
420
421            calc [optional_other_flags ...] -q -f filename
422
423       to disable the use of calc startup files as well.
424
425       For example, if the file /tmp/mersenne:
426
427            #!/usr/bin/calc -q -f
428
429            /* setup */
430            argc = argv();
431            program = argv(0);
432            stderr = files(2);
433
434            /* parse args */
435            if (argc != 2) {
436                fprintf(stderr, "usage: %s exp0, program);
437                abort "must give one exponent arg";
438            }
439            exp = eval(argv(1));
440            if (!isint(exp) || exp < 0) {
441                fprintf(stderr, "%s: exp must be non-negative integer0, program);
442                abort "must give one exponent arg";
443            }
444
445            /* print the mersenne number */
446            print "2^": exp : "-1 =", 2^exp-1;
447
448       is made an executable file by:
449
450            chmod +x /tmp/mersenne
451
452       then the command line:
453
454            /tmp/mersenne 127
455
456       will print:
457
458            2^127-1 = 170141183460469231731687303715884105727
459
460       Note  that  because -s is required in shell script mode non-dashed args
461       are made available as strings via the argv() builtin function.   There‐
462       fore:
463
464            2^eval(argv(1))-1
465
466       will print the decimal value of 2^n-1 whereas
467
468            2^argv(1)-1
469
470       will not.
471
472

DATA TYPES

474       Fundamental builtin data types include integers, real numbers, rational
475       numbers, complex numbers and strings.
476
477       By use of an object, one may define an arbitrarily complex data  types.
478       One  may define how such objects behave a wide range of operations such
479       as addition, subtraction, multiplication, division, negation, squaring,
480       modulus,  rounding,  exponentiation, equality, comparison, printing and
481       so on.
482
483       For more information use the following calc commands:
484
485          help types
486          help obj
487          show objfuncs
488
489

VARIABLES

491       Variables in calc are typeless.  In other words, the  fundamental  type
492       of  a  variable is determined by its content.  Before a variable is as‐
493       signed a value it has the value of zero.
494
495       The scope of a variable may be global, local to a file, or local  to  a
496       procedure.   Values  may  be  grouped together in a matrix, or into a a
497       list that permits stack and queue style operations.
498
499       For more information use the following calc commands:
500
501          help variable
502          help mat
503          help list
504          show globals
505
506

INPUT/OUTPUT

508       A leading ``0x'' implies a hexadecimal value, a leading ``0b''  implies
509       a binary value, and a ``0'' followed by a digit implies an octal value.
510       Complex numbers are indicated by a trailing ``i'' such as in  ``3+4i''.
511       Strings  may  be delimited by either a pair of single or double quotes.
512       By default, calc prints values as if they were floating point  numbers.
513       One may change the default to print values in a number of modes includ‐
514       ing fractions, integers and exponentials.
515
516       A number of stdio-like file I/O operations are provided.  One may open,
517       read,  write, seek and close files.  Filenames are subject to `` '' ex‐
518       pansion to home directories in a way similar to that of the Korn or  C-
519       Shell.
520
521       For example:
522
523          ~/.calcrc
524          ~chongo/lib/fft_multiply.cal
525
526       For more information use the following calc command:
527
528          help file
529
530

CALC LANGUAGE

532       The calc language is a C-like language.  The language includes commands
533       such as variable declarations, expressions, tests, labels, loops,  file
534       operations,  function  calls.  These commands are very similar to their
535       counterparts in C.
536
537       The language also include a number of commands particular to  calc  it‐
538       self.   These include commands such as function definition, help, read‐
539       ing in resource files, dump files to a file, error  notification,  con‐
540       figuration control and status.
541
542       For more information use the following calc command:
543
544          help command
545          help statement
546          help expression
547          help operator
548          help config
549
550

FILES

552       /usr/bin/calc
553            calc binary
554
555       /usr/bin/cscript/*
556            calc shell scripts
557
558       /usr/lib64/*.cal
559            calc standard resource files
560
561       /usr/lib64/help/*
562            help files
563
564       /usr/lib64/bindings
565            non-GNU-readline command line editor bindings
566
567       /usr/include/calc/*.h
568            include files for C interface use
569
570       /usr/lib64/libcalc.a
571            calc binary link library
572
573       /usr/lib64/libcustcalc.a
574            custom binary link library
575
576       /usr/share/calc/custom/*.cal
577            custom resource files
578
579       /usr/share/calc/custhelp/*
580            custom help files
581
582

ENVIRONMENT

584       CALCPATH
585            A :-separated list of directories used to search for calc resource
586            filenames that do not begin with /, ./ or ~.
587
588            Default value: .:./cal:~/.cal:/usr/share/calc
589
590       CALCRC
591            On startup (unless -h or -q was given on the command  line),  calc
592            searches for files along this :-separated environment variable.
593
594            Default value: ./.calcinit:~/.calcrc:/usr/share/calc/startup
595
596       CALCBINDINGS
597            On  startup  (unless -h or -q was given on the command line, or -m
598            disallows opening files for reading), calc reads key bindings from
599            the  filename  specified  by  this  environment variable.  The key
600            binding file is searched for along the $CALCPATH list of  directo‐
601            ries.
602
603            Default value: binding
604
605            This  variable  is not used if calc was compiled with GNU-readline
606            support.  In that case,  the  standard  readline  mechanisms  (see
607            readline(3)) are used.
608
609       CALCHISTFILE
610            Location of the calc history file.
611
612            Default value: ~/.calc_history
613
614            This  variable  is not used if calc was compiled with GNU-readline
615            support.
616
617       CALCHELP
618            Location of the calc help directory.
619
620            Default value: /usr/share/calc/help
621
622       CALCCUSTOMHELP
623            Location of the calc custom help directory.
624
625            Default value: /usr/share/calc/custhelp
626
627

CREDIT

629       The main chunk of calc was written by David I. Bell.
630
631       The calc primary mirror, and calc bug report processing is performed by
632       Landon Curt Noll.
633
634       Landon  Curt  Noll  maintains the master reference source, performs re‐
635       lease control functions as well as other calc maintenance functions.
636
637       Thanks for suggestions and encouragement from Peter Miller,  Neil  Jus‐
638       tusson, and Landon Noll.
639
640       Thanks  to  Stephen Rothwell for writing the original version of hist.c
641       which is used to do the command line editing.
642
643       Thanks to Ernest W. Bowen for supplying many improvements  in  accuracy
644       and  generality  for some numeric functions.  Much of this was in terms
645       of actual code which I gratefully accepted.  Ernest also  supplied  the
646       original text for many of the help files.
647
648       Portions  of this program are derived from an earlier set of public do‐
649       main arbitrarily precision routines which was posted to the net  around
650       1984.   By  now,  there  is  almost no recognizable code left from that
651       original source.
652

COPYING / CALC GNU LESSER GENERAL PUBLIC LICENSE

654       Calc is open software, and is covered under  version  2.1  of  the  GNU
655       Lesser  General  Public  License.   You are welcome to change it and/or
656       distribute copies of it under certain conditions.  The calc commands:
657
658            help copyright
659            help copying
660            help copying-lgpl
661            help credit
662
663       should display the contents of  the  COPYING  and  COPYING-LGPL  files.
664       Those  files  contain  information  about the calc's GNU Lesser General
665       Public License, and in particular the conditions under  which  you  are
666       allowed to change it and/or distribute copies of it.
667
668       You  should  have  received a copy of the version 2.1 of the GNU Lesser
669       General Public License.  If you do not have these files, write to:
670
671            Free Software Foundation, Inc.
672            51 Franklin Street
673            Fifth Floor
674            Boston, MA  02110-1301
675            USA
676
677       Calc is copyrighted in several different ways.  These ways include:
678
679            Copyright (C) year  David I. Bell
680            Copyright (C) year  David I. Bell and Landon Curt Noll
681            Copyright (C) year  David I. Bell and Ernest Bowen
682            Copyright (C) year  David I. Bell, Landon Curt Noll and Ernest Bowen
683            Copyright (C) year  Landon Curt Noll
684            Copyright (C) year  Ernest Bowen and Landon Curt Noll
685            Copyright (C) year  Ernest Bowen
686            Copyright (C) year  Petteri Kettunen and Landon Curt Noll
687            Copyright (C) year  Christoph Zurnieden
688            Copyright (C) year  Landon Curt Noll and Thomas Jones-Low
689            Copyright (C) year  Klaus Alexander Seistrup and Landon Curt Noll
690
691       This man page is:
692
693            Copyright (C) 1999-2023  Landon Curt Noll
694
695       and is covered under version 2.1 GNU Lesser General Public License.
696
697       A few files in calc are covered under "The Unlicense".  For more infor‐
698       mation on this license, see:
699
700            https://unlicense.org
701
702

CALC QUESTIONS

704       To ask the calc maintainers a general question about calc, see the out‐
705       put of the following calc command:
706
707
708            ; help question
709
710       or read the source file:
711
712
713            QUESTIONS
714
715
716       or visit the following URL:
717
718            https://github.com/lcn2/calc/blob/master/QUESTIONS
719
720

BUG REPORTS / BUG FIXES

722       To inform the calc maintainers about a bug, or to submit a bug fix, see
723       the output of the following calc command:
724
725
726            ; help bugs
727
728       or read the source file:
729
730
731            BUGS
732
733       or visit the following URL:
734
735            https://github.com/lcn2/calc/blob/master/BUGS
736
737

CONTRIBUTING CODE TO CALC

739       Calc  is  open  source.  You are welcome to contribute code to calc, or
740       submit bug fixes to to calc.
741
742       To contribute code to calc, please see see the output of the  following
743       calc command:
744
745
746            ; help contrib
747
748
749       or read the source file:
750
751
752            CONTRIB-CODE
753
754       or visit the following URL:
755
756            https://github.com/lcn2/calc/blob/master/CONTRIB-CODE
757
758

CALC WEB SITE

760       Landon Noll maintains the calc web site is located at:
761
762            www.isthe.com/chongo/tech/comp/calc/
763

CALC SOURCE CODE

765       The latest version of calc source code may be obtained at the following
766       GitHub repo:
767
768            https://github.com/lcn2/calc
769
770       The very latest calc souece is found at the top master branch.
771
772       You may download the calc releases from:
773
774            https://github.com/lcn2/calc/releases
775
776       The releases marked ((Pre-release)) are beta releases.
777
778       The most recent release that is NOT  marked ((Pre-release)) is the rec‐
779       ommended stable release.
780
781
782
783Share and enjoy!  :-)                ^..^                              calc(1)
Impressum