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.  If  calc_cmd  args  are  given,
135              calc  by  default,  calc  will execute them and exit.  This flag
136              causes calc to drop into interactive mode after the commands are
137              executed.   This  flag  will cause calc to drop into interactive
138              mode after the commands are executed.
139
140              For example:
141
142                   calc 2+5
143
144              will print the value 7 and exit whereas:
145
146                   calc -i 2+5
147
148              will print the value 7 and prompt the user for  more  calc  com‐
149              mands.
150
151
152       -m mode
153              This  flag  sets  the  permission mode of calc.  It controls the
154              ability for calc to open files and execute programs.   Mode  may
155              be a number from 0 to 7.
156
157              The  mode  value  is interpreted in a way similar to that of the
158              chmod(1) octal mode:
159
160                   0  do not open any file, do not execute progs
161                   1  do not open any file
162                   2  do not open files for reading, do not execute progs
163                   3  do not open files for reading
164                   4  do not open files for writing, do not execute progs
165                   5  do not open files for writing
166                   6  do not execute any program
167                   7  allow everything (default mode)
168
169              If one wished to run calc from a privileged user, one might want
170              to use -m 0 in an effort to make calc somewhat more secure.
171
172              Mode  bits for reading and writing apply only on an open.  Files
173              already open are not effected.  Thus if one wanted to use the -m
174              0  in  an  effort  to  make calc somewhat more secure, but still
175              wanted to read and write a specific file, one might want  to  do
176              in sh(1), ksh(1), bash(1)-like shells:
177
178                   calc -m 0 3<a.file
179
180              Files  presented  to  calc  in this way are opened in an unknown
181              mode.  Calc will attempt to read or write them if directed.
182
183              If the mode disables opening of  files  for  reading,  then  the
184              startup  resource  files  are  disabled as if -q was given.  The
185              reading of key bindings is also disabled when the mode  disables
186              opening of files for reading.
187
188
189       -O     Use  the  old classic defaults instead of the default configura‐
190              tion.  This flag as the same effect as  executing  config("all",
191              "oldcfg") at startup time.
192
193              NOTE: Older versions of calc used -n to setup a modified form of
194              the default calc configuration.   The  -n  flag  currently  does
195              nothing.   Use  of the -n flag is now deprecated and may be used
196              for something else in the future.
197
198
199       -p     Pipe processing is enabled by use of -p.  For example:
200
201                   calc -p "2^21701-1" | fizzbin
202
203              In pipe mode, calc does not prompt, does not print leading  tabs
204              and  does  not  print the initial header.  The -p flag overrides
205              -i.
206
207
208       -q     Disable the reading of the startup scripts.
209
210
211       -s     By default, all calc_cmd args are evaluated and executed.   This
212              flag  will disable their evaluation and instead make them avail‐
213              able as strings for the argv() builtin function.
214
215
216       -u     Disable buffering of stdin and stdout.
217
218
219       -v     Print the calc version number and exit.
220
221
222       --     The double dash indicates to calc that no  more  option  follow.
223              Thus  calc will ignore a later argument on the command line even
224              if it starts with a dash.  This is useful when entering negative
225              values on the command line as in:
226
227                   calc -p -- -1 - -7
228
229
230
231
232       CALC COMMAND LINE
233
234       With  no  calc_cmd  arguments,  calc operates interactively.  If one or
235       more arguments are given on the command line and -s is NOT given,  then
236       calc  will  read  and execute them and either attempt to go interactive
237       according as the -i flag was present or absent.
238
239       If -s is given, calc will  not  evaluate  any  calc_cmd  arguments  but
240       instead make them available as strings to the argv() builtin function.
241
242       Sufficiently  simple  commands  with no no characters like parentheses,
243       brackets, semicolons, '*', which have special interpretations  in  UNIX
244       shells may be entered, possibly with spaces, until the terminating new‐
245       line.  For example:
246
247            calc 23 + 47
248
249       will print 70.  However, command lines will have problems:
250
251            calc 23 * 47
252
253            calc -23 + 47
254
255       The first example above fails because the shell interprets the '*' as a
256       file  glob.  The second example fails because '-23' is viewed as a calc
257       option (which it is not) and do calc objects to that it thinks of as an
258       unknown option.  These cases can usually be made to work as expected by
259       enclosing the command between quotes:
260
261            calc '23 * 47'
262
263            calc "print sqrt(2), exp(1)"
264
265       or in parentheses and quotes to avoid leading -'s as in:
266
267            calc '(-23 + 47)'
268
269       One may also use a double dash to denote that calc options  have  ended
270       as in:
271
272            calc -- -23 + 47
273
274            calc -q -- -23 + 47
275
276       If  '!'  is  to  be used to indicate the factorial function, for shells
277       like csh(1) for which '!' followed by a non-space character is used for
278       history  substitution,  it may be necessary to include a space or use a
279       backslash to escape the special meaning of '!'.  For example, the  com‐
280       mand:
281
282            print 27!^2
283
284       may have to be replaced by:
285
286            print 27! ^2   or   print 27^2
287
288
289       CALC STARTUP FILES
290
291       Normally  on  startup, if the environment variable $CALCRC is undefined
292       and calc is invoked without the -q flag, or if $CALCRC is  defined  and
293       calc  is  invoked  with -e, calc looks for a file "startup" in the calc
294       resource directory .calcrc in the user's home directory, and  .calcinit
295       in  the current directory.  If one or more of these are found, they are
296       read in succession as calc scripts and their commands  executed.   When
297       defined,  $CALCRC is to contain a ':' separated list of names of files,
298       and if calc is then invoked without either the -q or  -e  flags,  these
299       files  are  read  in  succession and their commands executed.  No error
300       condition is produced if a listed file is not found.
301
302       If the mode specified by -m disables opening of files for reading, then
303       the reading of startup files is also disabled as if -q was given.
304
305
306       CALC FILE SEARCH PATH
307
308       If the environment variable $CALCPATH is undefined, or if it is defined
309       and calc is invoked with the -e flag, when a file  name  not  beginning
310       with /, ~ or ./, is specified as in:
311
312            calc read myfile
313
314       calc searches in succession:
315
316            /usr/lib64/myfile
317            /usr/lib64/myfile.cal
318            /usr/share/calc/custom/myfile
319            /usr/share/calc/custom/myfile.cal
320
321       If the file is found, the search stops and the commands in the file are
322       executed.  It is an error if no readable file with the  specified  name
323       is  found.   An  alternative  search  path can be specified by defining
324       $CALCPATH in the same way as PATH is defined, as a ':'  separated  list
325       of directories, and then invoking calc without the -e flag.
326
327       Calc  treats  all  open  files,  other than stdin, stdout and stderr as
328       files available for reading and writing.  One may present calc with  an
329       already open file using sh(1), ksh(1), bash(1)-like shells is to:
330
331            calc 3<open_file 4<open_file2
332
333       For more information use the following calc commands:
334
335            help help
336            help overview
337            help usage
338            help environment
339            help config
340
341
342
343       SHELL SCRIPT MODE
344
345       If  the  first  line  of  an executable file begins #!  followed by the
346       absolute pathname of the calc program and the flag -f as in:
347
348            #!/usr/bin/calc [other_flags ...] -f
349
350       the rest of the file will be processed in shell script mode.  Note that
351       -f  must  at  the  end  of the initial ``#!'' line.  Any other optional
352       other_flags must come before the -f.
353
354       In shell script mode the contents of the file are read and executed  as
355       if they were in a file being processed by a read command, except that a
356       "command" beginning with '#' followed by whitespace and ending  at  the
357       next newline is treated as a comment.  Any optional other_flags will be
358       parsed first followed by the later lines within the script itself.
359
360       In shell script mode, -s is always assumed.  In addition, -d and -p are
361       automatically set if -i is not given.
362
363       For example, if the file /tmp/mersenne:
364
365            #!/usr/bin/calc -q -f
366            #
367            # mersenne - an example of a calc shell script file
368
369            /* parse args */
370            if (argv() != 1) {
371                fprintf(files(2), "usage: %s exp\n", config("program"));
372                abort "must give one exponent arg";
373            }
374
375            /* print the mersenne number */
376            print "2^": argv(0) : "-1 =", 2^eval(argv(0))-1;
377
378       is made an executable file by:
379
380            chmod +x /tmp/mersenne
381
382       then the command line:
383
384            /tmp/mersenne 127
385
386       will print:
387
388            2^127-1 = 170141183460469231731687303715884105727
389
390       Note  that  because  -s  is assumed in shell script mode and non-dashed
391       args are made available as strings via  the  argv()  builtin  function.
392       Therefore:
393
394            2^eval(argv(0))-1
395
396       will print the decimal value of 2^n-1 but
397
398            2^argv(0)-1
399
400       will not.
401
402
403       DATA TYPES
404
405       Fundamental builtin data types include integers, real numbers, rational
406       numbers, complex numbers and strings.
407
408       By use of an object, one may define an arbitrarily complex data  types.
409       One  may define how such objects behave a wide range of operations such
410       as addition, subtraction, multiplication, division, negation, squaring,
411       modulus,  rounding,  exponentiation, equality, comparison, printing and
412       so on.
413
414       For more information use the following calc commands:
415
416          help types
417          help obj
418          show objfuncs
419
420
421       VARIABLES
422
423       Variables in calc are typeless.  In other words, the  fundamental  type
424       of  a  variable  is  determined  by  its content.  Before a variable is
425       assigned a value it has the value of zero.
426
427       The scope of a variable may be global, local to a file, or local  to  a
428       procedure.   Values  may  be  grouped together in a matrix, or into a a
429       list that permits stack and queue style operations.
430
431       For more information use the following calc commands:
432
433          help variable
434          help mat
435          help list
436          show globals
437
438
439       INPUT/OUTPUT
440
441       A leading ``0x'' implies a hexadecimal value, a leading ``0b''  implies
442       a binary value, and a ``0'' followed by a digit implies an octal value.
443       Complex numbers are indicated by a trailing ``i'' such as in  ``3+4i''.
444       Strings  may  be delimited by either a pair of single or double quotes.
445       By default, calc prints values as if they were floating point  numbers.
446       One may change the default to print values in a number of modes includ‐
447       ing fractions, integers and exponentials.
448
449       A number of stdio-like file I/O operations are provided.  One may open,
450       read,  write,  seek  and  close  files.  Filenames are subject to `` ''
451       expansion to home directories in a way similar to that of the  Korn  or
452       C-Shell.
453
454       For example:
455
456          ~/.calcrc
457          ~chongo/lib/fft_multiply.cal
458
459       For more information use the following calc command:
460
461          help file
462
463
464       CALC LANGUAGE
465
466       The calc language is a C-like language.  The language includes commands
467       such as variable declarations, expressions, tests, labels, loops,  file
468       operations,  function  calls.  These commands are very similar to their
469       counterparts in C.
470
471       The language also include a  number  of  commands  particular  to  calc
472       itself.   These  include  commands  such  as function definition, help,
473       reading in resource files, dump files to a  file,  error  notification,
474       configuration control and status.
475
476       For more information use the following calc command:
477
478          help command
479          help statement
480          help expression
481          help operator
482          help config
483

FILES

485
486       /usr/bin/calc
487            calc binary
488
489       /usr/share/calc/cscript/*
490            calc shell scripts
491
492       /usr/lib64/*.cal
493            calc standard resource files
494
495       /usr/lib64/help/*
496            help files
497
498       /usr/lib64/bindings
499            non-GNU-readline command line editor bindings
500
501       /usr/include/calc/*.h
502            include files for C interface use
503
504       /usr/lib64/libcalc.a
505            calc binary link library
506
507       /usr/lib64/libcustcalc.a
508            custom binary link library
509
510       /usr/share/calc/custom/*.cal
511            custom resource files
512
513       /usr/share/calc/custhelp/*
514            custom help files
515

ENVIRONMENT

517
518       CALCPATH
519            A :-separated list of directories used to search for calc resource
520            filenames that do not begin with /, ./ or ~.
521
522            Default value: .:./cal:~/.cal:/usr/share/calc
523
524       CALCRC
525            On startup (unless -h or -q was given on the command  line),  calc
526            searches for files along this :-separated environment variable.
527
528            Default value: /usr/share/calc/startup:~/.calcrc:./.calcinit
529
530       CALCBINDINGS
531            On  startup  (unless -h or -q was given on the command line, or -m
532            disallows opening files for reading), calc reads key bindings from
533            the  filename  specified  by  this  environment variable.  The key
534            binding file is searched for along the $CALCPATH list of  directo‐
535            ries.
536
537            Default value: binding
538
539            This  variable  is not used if calc was compiled with GNU-readline
540            support.  In that case,  the  standard  readline  mechanisms  (see
541            readline(3)) are used.
542

CREDIT

544
545       The main chunk of calc was written by David I. Bell.
546
547       The calc primary mirror, calc mailing list and calc bug report process‐
548       ing is performed by Landon Curt Noll.
549
550       Landon Curt  Noll  maintains  the  master  reference  source,  performs
551       release control functions as well as other calc maintenance functions.
552
553       Thanks  for  suggestions and encouragement from Peter Miller, Neil Jus‐
554       tusson, and Landon Noll.
555
556       Thanks to Stephen Rothwell for writing the original version  of  hist.c
557       which is used to do the command line editing.
558
559       Thanks  to  Ernest W. Bowen for supplying many improvements in accuracy
560       and generality for some numeric functions.  Much of this was  in  terms
561       of  actual  code which I gratefully accepted.  Ernest also supplied the
562       original text for many of the help files.
563
564       Portions of this program are derived from  an  earlier  set  of  public
565       domain  arbitrarily  precision  routines  which  was  posted to the net
566       around 1984.  By now, there is almost no recognizable  code  left  from
567       that original source.
568

COPYING / CALC GNU LESSER GENERAL PUBLIC LICENSE

570
571
572       Calc  is  open software, and is covered under version 2 of the GNU Gen‐
573       eral Public License.  You are welcome to change  it  and/or  distribute
574       copies of it under certain conditions.  The calc commands:
575
576            help copyright
577            help copying
578            help copying-gpl
579
580       should  display  the  contents  of  the  COPYING and COPYING-GPL files.
581       Those files contain information about the  calc's  GNU  General  Public
582       License,  and  in particular the conditions under which you are allowed
583       to change it and/or distribute copies of it.
584
585       You should have received a copy of the version 2  of  the  GNU  General
586       Public License.  If you do not have these files, write to:
587
588            Free Software Foundation, Inc.
589            51 Franklin Street
590            Fifth Floor
591            Boston, MA  02110-1301
592            USA
593
594       Calc is copyrighted in several different ways.  These ways include:
595
596            Copyright (C) year  David I. Bell
597            Copyright (C) year  David I. Bell and Landon Curt Noll
598            Copyright (C) year  David I. Bell and Ernest Bowen
599            Copyright (C) year  David I. Bell, Landon Curt Noll and Ernest Bowen
600            Copyright (C) year  Landon Curt Noll
601            Copyright (C) year  Ernest Bowen and Landon Curt Noll
602            Copyright (C) year  Ernest Bowen
603
604       This man page is:
605
606            Copyright (C) 1999  Landon Curt Noll
607
608       and is covered under version 2 GNU General Public License.
609

CALC MAILING LIST / CALC UPDATES / ENHANCEMENTS

611
612
613       To  contribute comments, suggestions, enhancements and interesting calc
614       resource files, and shell scripts please join the low volume calc mail‐
615       ing list.
616
617       To join the low volume calc mailing list, send EMail to:
618
619            calc-tester-request at asthe dot com
620
621       Your subject must contain the words:
622
623            calc mailing list subscription
624
625       You may have additional words in your subject line.
626
627       Your message body must contain:
628
629            subscribe calc-tester address
630            end
631            name your_full_name
632
633       where  address  s  your  EMail  address and your_full_name is your full
634       name.  Feel free to follow the name line with additional EMail text  as
635       desired.
636

BUG REPORTS / BUG FIXES

638
639
640       Send bug reports and bug fixes to:
641
642            calc-bugs at asthe dot com
643
644            [[ NOTE: Replace 'at' with @, 'dot' is with . and remove the spaces ]]
645            [[ NOTE: The EMail address uses 'asthe' and the web site URL uses 'isthe' ]]
646
647       Your subject must contain the words:
648
649            calc bug report
650
651       You may have additional words in your subject line.
652
653       See the BUGS source file or use the calc command:
654
655            help bugs
656
657       for more information about bug reporting.
658

CALC WEB SITE

660
661       Landon Noll maintains the the calc web site is located at:
662
663            www.isthe.com/chongo/tech/comp/calc/
664
665       Share and Enjoy! :-)
666
667
668
6692007-02-06                           ^..^                              calc(1)
Impressum