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] [-h] [-i] [-m mode] [-O]
12            [-p] [-q] [-s] [-u] [-v] [[--] calc_cmd ...]
13
14       #!/usr/bin/calc [other_flags ...] -f
15

DESCRIPTION

17
18       CALC OPTIONS
19
20       -c     Continue reading command lines even after a scan/parse error has
21              caused the abandonment of a line.  Note that  this  option  only
22              deals  with  scanning and parsing of the calc language.  It does
23              not deal with execution or run-time errors.
24
25              For example:
26
27                   calc read many_errors.cal
28
29              will cause calc to abort on the first syntax error, whereas:
30
31                   calc -c read many_errors.cal
32
33              will cause calc to try to process each line being  read  despite
34              the scan/parse errors that it encounters.
35
36              By  default, calc startup resource files are silently ignored if
37              not found.  This flag will report missing startup resource files
38              unless -d is also given.
39
40
41       -C     Permit  the execution of custom builtin functions.  Without this
42              flag, calling the custom() builtin function will simply generate
43              an error.
44
45              Use  of  this  flag may cause calc to execute functions that are
46              non-standard and that are not portable.   Custom  builtin  func‐
47              tions are disabled by default for this reason.
48
49
50       -d     Disable  the  printing  of  the  opening title.  The printing of
51              resource file debug and informational messages is also  disabled
52              as if config("resource_debug", 0) had been executed.
53
54              For example:
55
56                   calc "read qtime; qtime(2)"
57
58              will output something like:
59
60                   qtime(utc_hr_offset) defined
61                   It's nearly ten past six.
62
63              whereas:
64
65                   calc -d "read qtime; qtime(2)"
66
67              will just say:
68
69                   It's nearly ten past six.
70
71              This  flag  disables  the  reporting  of  missing  calc  startup
72              resource files.
73
74
75       -D calc_debug[:resource_debug[:user_debug]]
76              Force  the   initial   value   of   config("calc_debug"),   con‐
77              fig("resource_debug") and config("user_debug").
78
79              The  :  separated strings are interpreted as signed 32 bit inte‐
80              gers.  After an optional leading sign a leading  zero  indicates
81              octal  conversion,  and  a  leading ``0x'' or ``0X'' hexadecimal
82              conversion.  Otherwise, decimal conversion is assumed.
83
84              By default, calc_debug is 0, resource_debug is 3 and  user_debug
85              is 0.
86
87              For more information use the following calc command:
88
89                   help config
90
91
92       -e     Ignore  any  environment  variables  on  startup.   The getenv()
93              builtin will still return values, however.
94
95
96       -f     This flag is required when using calc in shell script mode.   It
97              must be at the end of the initial #!  line of the script.
98
99              This  flag  is  normally only at the end of a calc shell script.
100              If the first line of an executable file begins #!   followed  by
101              the absolute pathname of the calc program and the flag -f as in:
102
103                   #!/usr/bin/calc [other_flags ...] -f
104
105              the  rest  of  the  file will be processed in shell script mode.
106              See SHELL SCRIPT  MODE  section  of  this  man  page  below  for
107              details.
108
109              The actual form of this flag is:
110
111                   -f filename
112
113              On  systems  that  treat an executable that begins with #!  as a
114              script, the path of the executable is appended by the kernel  as
115              the  final  argument to the exec() system call.  This is why the
116              -f flag at the very end of the #!  line.
117
118              It is possible use -f filename on the command line:
119
120                   calc [other_flags ...] -f filename
121
122              This will cause calc to  process  lines  in  filename  in  shell
123              script mode.
124
125              Use  of -f implies -s.  In addition, -d and -p are implied if -i
126              is not given.
127
128
129       -h     Print a help message.  This option implies -q.  This is  equiva‐
130              lent  to  the calc command help help.  The help facility is dis‐
131              abled unless the mode is 5 or 7.  See -m.
132
133
134       -i     Become interactive if possible.  This flag will  cause  calc  to
135              drop  into  interactive mode after the calc_cmd arguments on the
136              command line are evaluated.  Without this flag, calc  will  exit
137              after they are evaluated.
138
139              For example:
140
141                   calc 2+5
142
143              will print the value 7 and exit whereas:
144
145                   calc -i 2+5
146
147              will  print  the  value 7 and prompt the user for more calc com‐
148              mands.
149
150
151       -m mode
152              This flag sets the permission mode of  calc.   It  controls  the
153              ability  for  calc to open files and execute programs.  Mode may
154              be a number from 0 to 7.
155
156              The mode value is interpreted in a way similar to  that  of  the
157              chmod(1) octal mode:
158
159                   0  do not open any file, do not execute progs
160                   1  do not open any file
161                   2  do not open files for reading, do not execute progs
162                   3  do not open files for reading
163                   4  do not open files for writing, do not execute progs
164                   5  do not open files for writing
165                   6  do not execute any program
166                   7  allow everything (default mode)
167
168              If one wished to run calc from a privileged user, one might want
169              to use -m 0 in an effort to make calc somewhat more secure.
170
171              Mode bits for reading and writing apply only on an open.   Files
172              already open are not effected.  Thus if one wanted to use the -m
173              0 in an effort to make calc  somewhat  more  secure,  but  still
174              wanted  to  read and write a specific file, one might want to do
175              in sh(1), ksh(1), bash(1)-like shells:
176
177                   calc -m 0 3<a.file
178
179              Files presented to calc in this way are  opened  in  an  unknown
180              mode.  Calc will attempt to read or write them if directed.
181
182              If  the  mode  disables  opening  of files for reading, then the
183              startup resource files are disabled as if  -q  was  given.   The
184              reading  of key bindings is also disabled when the mode disables
185              opening of files for reading.
186
187
188       -O     Use the old classic defaults instead of the  default  configura‐
189              tion.   This  flag as the same effect as executing config("all",
190              "oldcfg") at startup time.
191
192              NOTE: Older versions of calc used -n to setup a modified form of
193              the  default  calc  configuration.   The  -n flag currently does
194              nothing.  Use of the -n flag is now deprecated and may  be  used
195              for something else in the future.
196
197
198       -p     Pipe processing is enabled by use of -p.  For example:
199
200                   calc -p "2^21701-1" | fizzbin
201
202              In  pipe mode, calc does not prompt, does not print leading tabs
203              and does not print the initial header.  The  -p  flag  overrides
204              -i.
205
206
207       -q     Disable the reading of the startup scripts.
208
209
210       -s     By  default, all calc_cmd args are evaluated and executed.  This
211              flag will disable their evaluation and instead make them  avail‐
212              able as strings for the argv() builtin function.
213
214
215       -u     Disable buffering of stdin and stdout.
216
217
218       -v     Print the calc version number and exit.
219
220
221       --     The  double  dash  indicates to calc that no more option follow.
222              Thus calc will ignore a later argument on the command line  even
223              if it starts with a dash.  This is useful when entering negative
224              values on the command line as in:
225
226                   calc -p -- -1 - -7
227
228
229
230
231       CALC COMMAND LINE
232
233       With no calc_cmd arguments, calc operates  interactively.   If  one  or
234       more  arguments are given on the command line and -s is NOT given, then
235       calc will read and execute them and either attempt  to  go  interactive
236       according as the -i flag was present or absent.
237
238       If  -s  is  given,  calc  will  not evaluate any calc_cmd arguments but
239       instead make them available as strings to the argv() builtin function.
240
241       Sufficiently simple commands with no no  characters  like  parentheses,
242       brackets,  semicolons,  '*', which have special interpretations in UNIX
243       shells may be entered, possibly with spaces, until the terminating new‐
244       line.  For example:
245
246            calc 23 + 47
247
248       will print 70.  However, command lines will have problems:
249
250            calc 23 * 47
251
252            calc -23 + 47
253
254       The first example above fails because the shell interprets the '*' as a
255       file glob.  The second example fails because '-23' is viewed as a  calc
256       option (which it is not) and do calc objects to that it thinks of as an
257       unknown option.  These cases can usually be made to work as expected by
258       enclosing the command between quotes:
259
260            calc '23 * 47'
261
262            calc "print sqrt(2), exp(1)"
263
264       or in parentheses and quotes to avoid leading -'s as in:
265
266            calc '(-23 + 47)'
267
268       One  may  also use a double dash to denote that calc options have ended
269       as in:
270
271            calc -- -23 + 47
272
273            calc -q -- -23 + 47
274
275       If '!' is to be used to indicate the  factorial  function,  for  shells
276       like csh(1) for which '!' followed by a non-space character is used for
277       history substitution, it may be necessary to include a space or  use  a
278       backslash  to escape the special meaning of '!'.  For example, the com‐
279       mand:
280
281            print 27!^2
282
283       may have to be replaced by:
284
285            print 27! ^2   or   print 27^2
286
287       Reading from standard input when calc is part of a pipe works  as  long
288       as  the  -p flag is given to calc.  For example, this will print chongo
289       was here:
290
291            echo chongo was here | calc -p 'print fgetline(files(0));'
292
293
294       while this does not:
295
296            echo chongo was here | calc 'print fgetline(files(0));'
297
298
299       nor will this print chongo was here:
300
301            echo chongo was here | calc -i 'print fgetline(files(0));'
302
303
304       This is because without -p, the interactive parser,  in  an  effort  to
305       parse interactive commands, flushes data on standard input.
306
307
308
309       CALC STARTUP FILES
310
311       Normally  on  startup, if the environment variable $CALCRC is undefined
312       and calc is invoked without the -q flag, or if $CALCRC is  defined  and
313       calc  is  invoked  with -e, calc looks for a file "startup" in the calc
314       resource directory .calcrc in the user's home directory, and  .calcinit
315       in  the current directory.  If one or more of these are found, they are
316       read in succession as calc scripts and their commands  executed.   When
317       defined,  $CALCRC is to contain a ':' separated list of names of files,
318       and if calc is then invoked without either the -q or  -e  flags,  these
319       files  are  read  in  succession and their commands executed.  No error
320       condition is produced if a listed file is not found.
321
322       If the mode specified by -m disables opening of files for reading, then
323       the reading of startup files is also disabled as if -q was given.
324
325
326       CALC FILE SEARCH PATH
327
328       If the environment variable $CALCPATH is undefined, or if it is defined
329       and calc is invoked with the -e flag, when a file  name  not  beginning
330       with /, ~ or ./, is specified as in:
331
332            calc read myfile
333
334       calc searches in succession:
335
336            ./myfile
337            ./myfile.cal
338            /usr/lib64/myfile
339            /usr/lib64/myfile.cal
340            /usr/share/calc/custom/myfile
341            /usr/share/calc/custom/myfile.cal
342
343       If the file is found, the search stops and the commands in the file are
344       executed.  It is an error if no readable file with the  specified  name
345       is  found.   An  alternative  search  path can be specified by defining
346       $CALCPATH in the same way as PATH is defined, as a ':'  separated  list
347       of directories, and then invoking calc without the -e flag.
348
349       Calc  treats  all  open  files,  other than stdin, stdout and stderr as
350       files available for reading and writing.  One may present calc with  an
351       already open file using sh(1), ksh(1), bash(1)-like shells is to:
352
353            calc 3<open_file 4<open_file2
354
355       For more information use the following calc commands:
356
357            help help
358            help overview
359            help usage
360            help environment
361            help config
362
363
364
365       SHELL SCRIPT MODE
366
367       If  the  first  line  of  an executable file begins #!  followed by the
368       absolute pathname of the calc program and the flag -f as in:
369
370            #!/usr/bin/calc [other_flags ...] -f
371
372       the rest of the file will be processed in shell script mode.  Note that
373       -f  must  at  the  end  of the initial ``#!'' line.  Any other optional
374       other_flags must come before the -f.
375
376       In shell script mode the contents of the file are read and executed  as
377       if they were in a file being processed by a read command, except that a
378       "command" beginning with '#' followed by whitespace and ending  at  the
379       next newline is treated as a comment.  Any optional other_flags will be
380       parsed first followed by the later lines within the script itself.
381
382       In shell script mode, -s is always assumed.  In addition, -d and -p are
383       automatically set if -i is not given.
384
385       For example, if the file /tmp/mersenne:
386
387            #!/usr/bin/calc -q -f
388            #
389            # mersenne - an example of a calc shell script file
390
391            /* parse args */
392            if (argv() != 1) {
393                fprintf(files(2), "usage: %s exp\n", config("program"));
394                abort "must give one exponent arg";
395            }
396
397            /* print the mersenne number */
398            print "2^": argv(0) : "-1 =", 2^eval(argv(0))-1;
399
400       is made an executable file by:
401
402            chmod +x /tmp/mersenne
403
404       then the command line:
405
406            /tmp/mersenne 127
407
408       will print:
409
410            2^127-1 = 170141183460469231731687303715884105727
411
412       Note  that  because  -s  is assumed in shell script mode and non-dashed
413       args are made available as strings via  the  argv()  builtin  function.
414       Therefore:
415
416            2^eval(argv(0))-1
417
418       will print the decimal value of 2^n-1 but
419
420            2^argv(0)-1
421
422       will not.
423
424
425       DATA TYPES
426
427       Fundamental builtin data types include integers, real numbers, rational
428       numbers, complex numbers and strings.
429
430       By use of an object, one may define an arbitrarily complex data  types.
431       One  may define how such objects behave a wide range of operations such
432       as addition, subtraction, multiplication, division, negation, squaring,
433       modulus,  rounding,  exponentiation, equality, comparison, printing and
434       so on.
435
436       For more information use the following calc commands:
437
438          help types
439          help obj
440          show objfuncs
441
442
443       VARIABLES
444
445       Variables in calc are typeless.  In other words, the  fundamental  type
446       of  a  variable  is  determined  by  its content.  Before a variable is
447       assigned a value it has the value of zero.
448
449       The scope of a variable may be global, local to a file, or local  to  a
450       procedure.   Values  may  be  grouped together in a matrix, or into a a
451       list that permits stack and queue style operations.
452
453       For more information use the following calc commands:
454
455          help variable
456          help mat
457          help list
458          show globals
459
460
461       INPUT/OUTPUT
462
463       A leading ``0x'' implies a hexadecimal value, a leading ``0b''  implies
464       a binary value, and a ``0'' followed by a digit implies an octal value.
465       Complex numbers are indicated by a trailing ``i'' such as in  ``3+4i''.
466       Strings  may  be delimited by either a pair of single or double quotes.
467       By default, calc prints values as if they were floating point  numbers.
468       One may change the default to print values in a number of modes includ‐
469       ing fractions, integers and exponentials.
470
471       A number of stdio-like file I/O operations are provided.  One may open,
472       read,  write,  seek  and  close  files.  Filenames are subject to `` ''
473       expansion to home directories in a way similar to that of the  Korn  or
474       C-Shell.
475
476       For example:
477
478          ~/.calcrc
479          ~chongo/lib/fft_multiply.cal
480
481       For more information use the following calc command:
482
483          help file
484
485
486       CALC LANGUAGE
487
488       The calc language is a C-like language.  The language includes commands
489       such as variable declarations, expressions, tests, labels, loops,  file
490       operations,  function  calls.  These commands are very similar to their
491       counterparts in C.
492
493       The language also include a  number  of  commands  particular  to  calc
494       itself.   These  include  commands  such  as function definition, help,
495       reading in resource files, dump files to a  file,  error  notification,
496       configuration control and status.
497
498       For more information use the following calc command:
499
500          help command
501          help statement
502          help expression
503          help operator
504          help config
505

FILES

507
508       /usr/bin/calc
509            calc binary
510
511       /usr/share/calc/cscript/*
512            calc shell scripts
513
514       /usr/lib64/*.cal
515            calc standard resource files
516
517       /usr/lib64/help/*
518            help files
519
520       /usr/lib64/bindings
521            non-GNU-readline command line editor bindings
522
523       /usr/include/calc/*.h
524            include files for C interface use
525
526       /usr/lib64/libcalc.a
527            calc binary link library
528
529       /usr/lib64/libcustcalc.a
530            custom binary link library
531
532       /usr/share/calc/custom/*.cal
533            custom resource files
534
535       /usr/share/calc/custhelp/*
536            custom help files
537

ENVIRONMENT

539
540       CALCPATH
541            A :-separated list of directories used to search for calc resource
542            filenames that do not begin with /, ./ or ~.
543
544            Default value: .:./cal:~/.cal:/usr/share/calc
545
546       CALCRC
547            On startup (unless -h or -q was given on the command  line),  calc
548            searches for files along this :-separated environment variable.
549
550            Default value: /usr/share/calc/startup:~/.calcrc:./.calcinit
551
552       CALCBINDINGS
553            On  startup  (unless -h or -q was given on the command line, or -m
554            disallows opening files for reading), calc reads key bindings from
555            the  filename  specified  by  this  environment variable.  The key
556            binding file is searched for along the $CALCPATH list of  directo‐
557            ries.
558
559            Default value: binding
560
561            This  variable  is not used if calc was compiled with GNU-readline
562            support.  In that case,  the  standard  readline  mechanisms  (see
563            readline(3)) are used.
564

CREDIT

566
567       The main chunk of calc was written by David I. Bell.
568
569       The calc primary mirror, calc mailing list and calc bug report process‐
570       ing is performed by Landon Curt Noll.
571
572       Landon Curt  Noll  maintains  the  master  reference  source,  performs
573       release control functions as well as other calc maintenance functions.
574
575       Thanks  for  suggestions and encouragement from Peter Miller, Neil Jus‐
576       tusson, and Landon Noll.
577
578       Thanks to Stephen Rothwell for writing the original version  of  hist.c
579       which is used to do the command line editing.
580
581       Thanks  to  Ernest W. Bowen for supplying many improvements in accuracy
582       and generality for some numeric functions.  Much of this was  in  terms
583       of  actual  code which I gratefully accepted.  Ernest also supplied the
584       original text for many of the help files.
585
586       Portions of this program are derived from  an  earlier  set  of  public
587       domain  arbitrarily  precision  routines  which  was  posted to the net
588       around 1984.  By now, there is almost no recognizable  code  left  from
589       that original source.
590

COPYING / CALC GNU LESSER GENERAL PUBLIC LICENSE

592
593
594       Calc  is  open software, and is covered under version 2 of the GNU Gen‐
595       eral Public License.  You are welcome to change  it  and/or  distribute
596       copies of it under certain conditions.  The calc commands:
597
598            help copyright
599            help copying
600            help copying-gpl
601
602       should  display  the  contents  of  the  COPYING and COPYING-GPL files.
603       Those files contain information about the  calc's  GNU  General  Public
604       License,  and  in particular the conditions under which you are allowed
605       to change it and/or distribute copies of it.
606
607       You should have received a copy of the version 2  of  the  GNU  General
608       Public License.  If you do not have these files, write to:
609
610            Free Software Foundation, Inc.
611            51 Franklin Street
612            Fifth Floor
613            Boston, MA  02110-1301
614            USA
615
616       Calc is copyrighted in several different ways.  These ways include:
617
618            Copyright (C) year  David I. Bell
619            Copyright (C) year  David I. Bell and Landon Curt Noll
620            Copyright (C) year  David I. Bell and Ernest Bowen
621            Copyright (C) year  David I. Bell, Landon Curt Noll and Ernest Bowen
622            Copyright (C) year  Landon Curt Noll
623            Copyright (C) year  Ernest Bowen and Landon Curt Noll
624            Copyright (C) year  Ernest Bowen
625
626       This man page is:
627
628            Copyright (C) 1999  Landon Curt Noll
629
630       and is covered under version 2 GNU General Public License.
631

CALC MAILING LIST / CALC UPDATES / ENHANCEMENTS

633
634
635       To  contribute comments, suggestions, enhancements and interesting calc
636       resource files, and shell scripts please join the calc-tester low  vol‐
637       ume moderated calc mailing list.
638
639       To the calc-tester mailing list, visit the following URL:
640
641            https://www.listbox.com/subscribe/?list_id=239342
642
643       To  help determine you are a human and not just a spam bot, you will be
644       required to provide the following additional information:
645
646            Your Name
647
648            Calc Version
649               For example, the current version is: 2.12.6.7
650
651            Operating System
652               If you don't know your operating system, enter: unknown
653
654            The date 7 days ago
655               Consult a calendar :-)
656
657       If you need a human to help you with your mailing list subscription, or
658       if you have problems with the above procedure, please send EMail to our
659       special address:
660
661            calc-tester-maillist-help at asthe dot com
662
663            NOTE: Remove spaces and replace 'at' with @, 'dot' with .
664
665            NOTE: Yes, the EMail address uses 'asthe',
666                  while the web site uses 'isthe'.
667
668       To be sure we see your EMail asking for help  with  your  mailing  list
669       subscription, please use the following phase in your EMail Subject line
670       your subject must contain the words:
671
672            calc tester mailing list help
673
674       You may have additional words in your subject line.
675

BUG REPORTS / BUG FIXES

677
678
679       Send bug reports and bug fixes to:
680
681            calc-bug-report at asthe dot com
682
683            NOTE: Remove spaces and replace 'at' with @, 'dot' with .
684
685            NOTE: Yes, the EMail address uses 'asthe',
686                  while the web site uses 'isthe'.
687
688       Your subject must contain the words:
689
690            calc bug report
691
692       You may have additional words in your subject line.
693            However, you may find it more helpful to simply subscribe  to  the
694            calc-tester  mailing list (see above) and then to send your report
695            to that mailing list as a wider set calc testers may  be  able  to
696            help you.
697
698            See the BUGS source file or use the calc command:
699
700                 help bugs
701
702            for more information about bug reporting.
703

CALC WEB SITE

705
706       Landon Noll maintains the calc web site is located at:
707
708            www.isthe.com/chongo/tech/comp/calc/
709
710       Share and Enjoy! :-)
711
712
713
7142007-02-06                           ^..^                              calc(1)
Impressum