1calc(1) General Commands Manual calc(1)
2
3
4
6 calc - arbitrary precision calculator
7
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
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 This allows the script to run independely of startup scripts.
131
132
133 See SHELL SCRIPT MODE section of this man page below for de‐
134 tails.
135
136 While the actual form of this flag is:
137
138 -f filename
139
140 for systems that treat an executable that begins with #! as a
141 script, the path of the executable is appended by the kernel as
142 the final argument to the exec() system call. This is why the
143 -f flag at the very end of the #! line.
144
145 It is possible use -f filename on the command line:
146
147 calc [optional_other_flags ...] -f filename
148
149 This will cause calc to process lines in filename in shell
150 script mode.
151
152
153 The use of -f filename implies the -s flag.
154
155
156 -h Print a help message. This option implies -q. This is equiva‐
157 lent to the calc command help help. The help facility is dis‐
158 abled unless the mode is 5 or 7. See -m.
159
160
161 -i Become interactive if possible. This flag will cause calc to
162 drop into interactive mode after the calc_cmd arguments on the
163 command line are evaluated. Without this flag, calc will exit
164 after they are evaluated.
165
166 For example:
167
168 calc 2+5
169
170 will print the value 7 and exit whereas:
171
172 calc -i 2+5
173
174 will print the value 7 and prompt the user for more calc com‐
175 mands.
176
177
178 -m mode
179 This flag sets the permission mode of calc. It controls the
180 ability for calc to open files and execute programs. Mode may
181 be a number from 0 to 7.
182
183 The mode value is interpreted in a way similar to that of the
184 chmod(1) octal mode:
185
186 0 do not open any file, do not execute progs
187 1 do not open any file
188 2 do not open files for reading, do not execute progs
189 3 do not open files for reading
190 4 do not open files for writing, do not execute progs
191 5 do not open files for writing
192 6 do not execute any program
193 7 allow everything (default mode)
194
195 If one wished to run calc from a privileged user, one might want
196 to use -m 0 in an effort to make calc somewhat more secure.
197
198 Mode bits for reading and writing apply only on an open. Files
199 already open are not effected. Thus if one wanted to use the -m
200 0 in an effort to make calc somewhat more secure, but still
201 wanted to read and write a specific file, one might want to do
202 in sh(1), ksh(1), bash(1)-like shells:
203
204 calc -m 0 3<a.file
205
206 Files presented to calc in this way are opened in an unknown
207 mode. Calc will attempt to read or write them if directed.
208
209 If the mode disables opening of files for reading, then the
210 startup resource files are disabled as if -q was given. The
211 reading of key bindings is also disabled when the mode disables
212 opening of files for reading.
213
214
215 -O Use the old classic defaults instead of the default configura‐
216 tion. This flag as the same effect as executing config("all",
217 "oldcfg") at startup time.
218
219 NOTE: Older versions of calc used -n to setup a modified form of
220 the default calc configuration. The -n flag currently does
221 nothing. Use of the -n flag is now deprecated and may be used
222 for something else in the future.
223
224
225 -p Pipe processing is enabled by use of -p. For example:
226
227 calc -p "2^21701-1" | fizzbin
228
229 In pipe mode, calc does not prompt, does not print leading tabs
230 and does not print the initial header. The -p flag overrides
231 -i.
232
233
234 -q Disable the reading of the startup scripts.
235
236
237 -s By default, all calc_cmd args are evaluated and executed. This
238 flag will disable their evaluation and instead make them avail‐
239 able as strings for the argv() builtin function.
240
241
242 -u Disable buffering of stdin and stdout.
243
244
245 -v Print the calc version number and exit.
246
247
248 -- The double dash indicates to calc that no more option follow.
249 Thus calc will ignore a later argument on the command line even
250 if it starts with a dash. This is useful when entering negative
251 values on the command line as in:
252
253 calc -p -- -1 - -7
254
255
256
258 With no calc_cmd arguments, calc operates interactively. If one or
259 more arguments are given on the command line and -s is NOT given, then
260 calc will read and execute them and either attempt to go interactive
261 according as the -i flag was present or absent.
262
263 If -s is given, calc will not evaluate any calc_cmd arguments but in‐
264 stead make them available as strings to the argv() builtin function.
265
266 Sufficiently simple commands with no no characters like parentheses,
267 brackets, semicolons, '*', which have special interpretations in UNIX
268 shells may be entered, possibly with spaces, until the terminating new‐
269 line. For example:
270
271 calc 23 + 47
272
273 will print 70. However, command lines will have problems:
274
275 calc 23 * 47
276
277 calc -23 + 47
278
279 The first example above fails because the shell interprets the '*' as a
280 file glob. The second example fails because '-23' is viewed as a calc
281 option (which it is not) and do calc objects to that it thinks of as an
282 unknown option. These cases can usually be made to work as expected by
283 enclosing the command between quotes:
284
285 calc '23 * 47'
286
287 calc "print sqrt(2), exp(1)"
288
289 or in parentheses and quotes to avoid leading -'s as in:
290
291 calc '(-23 + 47)'
292
293 One may also use a double dash to denote that calc options have ended
294 as in:
295
296 calc -- -23 + 47
297
298 calc -q -- -23 + 47
299
300 If '!' is to be used to indicate the factorial function, for shells
301 like csh[4m(1) for which '!' followed by a non-space character is used for
302 history substitution, it may be necessary to include a space or use a
303 backslash to escape the special meaning of '!'. For example, the com‐
304 mand:
305
306 print 27!^2
307
308 may have to be replaced by:
309
310 print 27! ^2 or print 27^2
311
312 Reading from standard input when calc is part of a pipe works as long
313 as the -p flag is given to calc. For example, this will print chongo
314 was here:
315
316 echo chongo was here | calc -p 'print fgetline(files(0));'
317
318
319 while this does not:
320
321 echo chongo was here | calc 'print fgetline(files(0));'
322
323
324 nor will this print chongo was here:
325
326 echo chongo was here | calc -i 'print fgetline(files(0));'
327
328
329 This is because without -p, the interactive parser, in an effort to
330 parse interactive commands, flushes data on standard input.
331
332
334 Normally on startup, if the environment variable $CALCRC is undefined
335 and calc is invoked without the -q flag, or if $CALCRC is defined and
336 calc is invoked with -e, calc looks for a file "startup" in the calc
337 resource directory .calcrc in the user's home directory, and .calcinit
338 in the current directory. If one or more of these are found, they are
339 read in succession as calc scripts and their commands executed. When
340 defined, $CALCRC is to contain a ':' separated list of names of files,
341 and if calc is then invoked without either the -q or -e flags, these
342 files are read in succession and their commands executed. No error
343 condition is produced if a listed file is not found.
344
345 If the mode specified by -m disables opening of files for reading, then
346 the reading of startup files is also disabled as if -q was given.
347
348
350 If the environment variable $CALCPATH is undefined, or if it is defined
351 and calc is invoked with the -e flag, when a file name not beginning
352 with /, ~ or ./, is specified as in:
353
354 calc read myfile
355
356 calc searches in succession:
357
358 ./myfile
359 ./myfile.cal
360 /usr/lib64/myfile
361 /usr/lib64/myfile.cal
362 /usr/share/calc/custom/myfile
363 /usr/share/calc/custom/myfile.cal
364
365 If the file is found, the search stops and the commands in the file are
366 executed. It is an error if no readable file with the specified name
367 is found. An alternative search path can be specified by defining
368 $CALCPATH in the same way as PATH is defined, as a ':' separated list
369 of directories, and then invoking calc without the -e flag.
370
371 Calc treats all open files, other than stdin, stdout and stderr as
372 files available for reading and writing. One may present calc with an
373 already open file using sh(1), ksh(1), bash(1)-like shells is to:
374
375 calc 3<open_file 4<open_file2
376
377 For more information use the following calc commands:
378
379 help help
380 help overview
381 help usage
382 help environment
383 help config
384
385
386
388 If the first line of an executable file begins #! followed by the ab‐
389 solute pathname of the calc program and the first line ends with the
390 flag -f as in:
391
392 #!/usr/bin/calc [optional_other_flags ...] -f
393
394 the rest of the file will be processed in shell script mode. Note that
395 -s -f must at the end of the initial ``#!'' line. Any other optional
396 optional_other_flags must come before the -f flag.
397
398 In shell script mode the contents of the file are read and executed as
399 if they were in a file being processed by a read command, except that a
400 "command" beginning with '#' followed by whitespace and ending at the
401 next newline is treated as a comment. Any optional op‐
402 tional_other_flags will be parsed first followed by the later lines
403 within the script itself.
404
405 In shell script mode, -s is always assumed. In addition, -d and -p are
406 automatically set if -i is not given.
407
408 For example, if the file /tmp/mersenne:
409
410 #!/usr/bin/calc -q -f
411
412 /* setup */
413 argc = argv();
414 program = argv(0);
415 stderr = files(2);
416
417 /* parse args */
418 if (argc != 2) {
419 fprintf(stderr, "usage: %s exp0, program);
420 abort "must give one exponent arg";
421 }
422 exp = eval(argv(1));
423 if (!isint(exp) || exp < 0) {
424 fprintf(stderr, "%s: exp must be non-negative integer0, program);
425 abort "must give one exponent arg";
426 }
427
428 /* print the mersenne number */
429 print "2^": exp : "-1 =", 2^exp-1;
430
431 is made an executable file by:
432
433 chmod +x /tmp/mersenne
434
435 then the command line:
436
437 /tmp/mersenne 127
438
439 will print:
440
441 2^127-1 = 170141183460469231731687303715884105727
442
443 Note that because -s is required in shell script mode non-dashed args
444 are made available as strings via the argv() builtin function. There‐
445 fore:
446
447 2^eval(argv(1))-1
448
449 will print the decimal value of 2^n-1 whereas
450
451 2^argv(1)-1
452
453 will not.
454
455
457 Fundamental builtin data types include integers, real numbers, rational
458 numbers, complex numbers and strings.
459
460 By use of an object, one may define an arbitrarily complex data types.
461 One may define how such objects behave a wide range of operations such
462 as addition, subtraction, multiplication, division, negation, squaring,
463 modulus, rounding, exponentiation, equality, comparison, printing and
464 so on.
465
466 For more information use the following calc commands:
467
468 help types
469 help obj
470 show objfuncs
471
472
474 Variables in calc are typeless. In other words, the fundamental type
475 of a variable is determined by its content. Before a variable is as‐
476 signed a value it has the value of zero.
477
478 The scope of a variable may be global, local to a file, or local to a
479 procedure. Values may be grouped together in a matrix, or into a a
480 list that permits stack and queue style operations.
481
482 For more information use the following calc commands:
483
484 help variable
485 help mat
486 help list
487 show globals
488
489
491 A leading ``0x'' implies a hexadecimal value, a leading ``0b'' implies
492 a binary value, and a ``0'' followed by a digit implies an octal value.
493 Complex numbers are indicated by a trailing ``i'' such as in ``3+4i''.
494 Strings may be delimited by either a pair of single or double quotes.
495 By default, calc prints values as if they were floating point numbers.
496 One may change the default to print values in a number of modes includ‐
497 ing fractions, integers and exponentials.
498
499 A number of stdio-like file I/O operations are provided. One may open,
500 read, write, seek and close files. Filenames are subject to `` '' ex‐
501 pansion to home directories in a way similar to that of the Korn or C-
502 Shell.
503
504 For example:
505
506 ~/.calcrc
507 ~chongo/lib/fft_multiply.cal
508
509 For more information use the following calc command:
510
511 help file
512
513
515 The calc language is a C-like language. The language includes commands
516 such as variable declarations, expressions, tests, labels, loops, file
517 operations, function calls. These commands are very similar to their
518 counterparts in C.
519
520 The language also include a number of commands particular to calc it‐
521 self. These include commands such as function definition, help, read‐
522 ing in resource files, dump files to a file, error notification, con‐
523 figuration control and status.
524
525 For more information use the following calc command:
526
527 help command
528 help statement
529 help expression
530 help operator
531 help config
532
533
535 /usr/bin/calc
536 calc binary
537
538 /usr/bin/cscript/*
539 calc shell scripts
540
541 /usr/lib64/*.cal
542 calc standard resource files
543
544 /usr/lib64/help/*
545 help files
546
547 /usr/lib64/bindings
548 non-GNU-readline command line editor bindings
549
550 /usr/include/calc/*.h
551 include files for C interface use
552
553 /usr/lib64/libcalc.a
554 calc binary link library
555
556 /usr/lib64/libcustcalc.a
557 custom binary link library
558
559 /usr/share/calc/custom/*.cal
560 custom resource files
561
562 /usr/share/calc/custhelp/*
563 custom help files
564
565
567 CALCPATH
568 A :-separated list of directories used to search for calc resource
569 filenames that do not begin with /, ./ or ~.
570
571 Default value: .:./cal:~/.cal:/usr/share/calc
572
573 CALCRC
574 On startup (unless -h or -q was given on the command line), calc
575 searches for files along this :-separated environment variable.
576
577 Default value: /usr/share/calc/startup:~/.calcrc:./.calcinit
578
579 CALCBINDINGS
580 On startup (unless -h or -q was given on the command line, or -m
581 disallows opening files for reading), calc reads key bindings from
582 the filename specified by this environment variable. The key
583 binding file is searched for along the $CALCPATH list of directo‐
584 ries.
585
586 Default value: binding
587
588 This variable is not used if calc was compiled with GNU-readline
589 support. In that case, the standard readline mechanisms (see
590 readline(3)) are used.
591
592 CALCHISTFILE
593 Location of the calc history file.
594
595 Default value: ~/.calc_history
596
597 This variable is not used if calc was compiled with GNU-readline
598 support.
599
600 CALCHELP
601 Location of the calc help directory.
602
603 Default value: /usr/share/calc/help
604
605 CALCCUSTOMHELP
606 Location of the calc custom help directory.
607
608 Default value: /usr/share/calc/custhelp
609
610
612 The main chunk of calc was written by David I. Bell.
613
614 The calc primary mirror, and calc bug report processing is performed by
615 Landon Curt Noll.
616
617 Landon Curt Noll maintains the master reference source, performs re‐
618 lease control functions as well as other calc maintenance functions.
619
620 Thanks for suggestions and encouragement from Peter Miller, Neil Jus‐
621 tusson, and Landon Noll.
622
623 Thanks to Stephen Rothwell for writing the original version of hist.c
624 which is used to do the command line editing.
625
626 Thanks to Ernest W. Bowen for supplying many improvements in accuracy
627 and generality for some numeric functions. Much of this was in terms
628 of actual code which I gratefully accepted. Ernest also supplied the
629 original text for many of the help files.
630
631 Portions of this program are derived from an earlier set of public do‐
632 main arbitrarily precision routines which was posted to the net around
633 1984. By now, there is almost no recognizable code left from that
634 original source.
635
637 Calc is open software, and is covered under version 2 of the GNU Gen‐
638 eral Public License. You are welcome to change it and/or distribute
639 copies of it under certain conditions. The calc commands:
640
641 help copyright
642 help copying
643 help copying-gpl
644
645 should display the contents of the COPYING and COPYING-GPL files.
646 Those files contain information about the calc's GNU General Public Li‐
647 cense, and in particular the conditions under which you are allowed to
648 change it and/or distribute copies of it.
649
650 You should have received a copy of the version 2 of the GNU General
651 Public License. If you do not have these files, write to:
652
653 Free Software Foundation, Inc.
654 51 Franklin Street
655 Fifth Floor
656 Boston, MA 02110-1301
657 USA
658
659 Calc is copyrighted in several different ways. These ways include:
660
661 Copyright (C) year David I. Bell
662 Copyright (C) year David I. Bell and Landon Curt Noll
663 Copyright (C) year David I. Bell and Ernest Bowen
664 Copyright (C) year David I. Bell, Landon Curt Noll and Ernest Bowen
665 Copyright (C) year Landon Curt Noll
666 Copyright (C) year Ernest Bowen and Landon Curt Noll
667 Copyright (C) year Ernest Bowen
668
669 This man page is:
670
671 Copyright (C) 1999-2021 Landon Curt Noll
672
673 and is covered under version 2 GNU General Public License.
674
676 If you have a simple general question about calc, send Email to:
677
678 calc-quest-mail at asthe dot com
679
680 NOTE: Remove spaces and replace 'at' with @, and 'dot' with .
681
682 NOTE: Yes, the Email address uses 'asthe', while the web site uses
683 'isthe'.
684
685 PLEASE put following the SPECIAL PHRASE somewhere in your Email Subject
686 line:
687
688 calc question
689
690 You may add additional words to your subject line.
691
692 IMPORTANT: If your Email doesn't contain the above phrase, then we WILL
693 NOT SEE your Email.
694
695 PLEASE BE SURE you have that SPECIAL PHRASE somewhere in the subject
696 line!
697
698 Suggestion:
699
700 From time to time, the Email address and Subject SPECIAL PHRASE
701 may change so verify you have the current info by visiting:
702
703 http://www.isthe.com/chongo/tech/comp/calc/calc-question.html
704
705 Please limit your questions to general questions about calc. We cannot
706 go into great detail in our answers, nor can we do your homework, nor
707 can we do much more than answer short general questions about calc.
708
709 Please be patient as we cannot always respond to Email messages
710 quickly.
711
713 Send bug reports and bug fixes to:
714
715 calc-bugrept at asthe dot com
716
717 NOTE: Remove spaces and replace 'at' with @, 'dot' with .
718
719 NOTE: Yes, the Email address uses 'asthe', while the web site uses
720 'isthe'.
721
722 You MUST use following SPECIAL PHRASE in your Email Subject line:
723
724 calc bug report
725
726 You may add additional words to your subject line.
727
728 Suggestion:
729
730 From time to time, the Email address and Subject SPECIAL PHRASE
731 may change so verify you have the current info by visiting:
732
733 http://www.isthe.com/chongo/tech/comp/calc/calc-bugrept.html
734
735 IMPORTANT: If your Email doesn't contain the above phrase, then we WILL
736 NOT SEE your Email.
737
738 PLEASE BE SURE you have that SPECIAL PHRASE somewhere in the subject
739 line!
740
741 See the BUGS source file or use the calc command:
742
743 help bugs
744
745 for more information about bug reporting.
746
747 Please be patient as we cannot always respond to Email messages
748 quickly.
749
751 Calc is open source. Contributions of code are welcome.
752
753 We welcome and encourage you to send us:
754
755
756 * calc resource files (cal/*.cal)
757 * calc shell scripts (cscript/*.calc)
758 * builtin functions that you have modified or written, i.e.:
759 assocfunc.c comfunc.c func.c func.h
760 listfunc.c matfunc.c qfunc.c zfunc.c
761 * custom functions that you have modified or written (custom/*)
762 * help files modified or written (help/*)
763 * brief description of you added, fixed, improved in CHANGES
764 * regression test cases (cal/regress.cal)
765 * Makefile improvements (Makefile, */Makefile)
766 * other source code modifications (*.c, *.h)
767 * etc. (* */* :) )
768
769 If you add functionality to calc, please be sure to modify/patch/add
770 Makefiles, help files, cal/regress.cal test code as well. Regression
771 test cases are vital to maintaining calc's level of correctness and
772 helps us avoid code bug regression.
773
774 In order to consider integrating your code, we need:
775
776 * calc version you are working with (please try use the latest version)
777 * new help files or help file patches, if applicable (documentation)
778 * proposed text for the CHANGES file (brief description of what it does)
779 * regress.cal test patch as needed
780 * your source code and/or source code changes (:-))
781
782 The best way to send us new code, if your changes are small, is via a
783 patch (diff -c from the latest alpha code to your code). If your
784 change is large, you should send entire files (either as a diff -c
785 /dev/null your-file patch, or as a uuencoded and gziped (or compressed)
786 tar file).
787
788 Please try to generate a patch against the most recent version of calc,
789 and if you use GitHub, the top of the master branch:
790
791 https://github.com/lcn2/calc
792
793
794 The best way contribute to calc bug is to generate calc GitHub pull re‐
795 quest:
796
797 https://github.com/lcn2/calc/pulls
798
799 Your code needs to be contributed under either the 2.1 of the GNU Gen‐
800 eral Public License (LGPL 2.1) or be in the public domain.
801
802 If you do not want to use calc GitHub, then send EMail to:
803
804 calc-contrib at asthe dot com
805
806 NOTE: Remove spaces and replace 'at' with @, 'dot' with .
807
808 NOTE: Yes, the Email address uses 'asthe', while the web site uses
809 'isthe'.
810
811 You MUST use following SPECIAL PHRASE in your Email Subject line:
812
813 calc contribution
814
815 You may add additional words to your subject line.
816
817 Suggestion:
818
819 From time to time, the Email address and Subject SPECIAL PHRASE
820 may change so verify you have the current info by visiting:
821
822 http://www.isthe.com/chongo/tech/comp/calc/calc-contrib.html
823
824 IMPORTANT: If your Email doesn't contain the above phrase, then we WILL
825 NOT SEE your Email.
826
827 PLEASE BE SURE you have that SPECIAL PHRASE somewhere in the subject
828 line!
829
830 Please be patient as we cannot always respond to Email messages
831 quickly.
832
834 Landon Noll maintains the calc web site is located at:
835
836 www.isthe.com/chongo/tech/comp/calc/
837
838 Share and Enjoy! :-)
839
840
841
842Share and enjoy! :-) ^..^ calc(1)