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