forked from gavinhoward/bc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbc.1
1454 lines (1451 loc) · 51.2 KB
/
bc.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
.\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3
.
.TH "BC" "1" "November 2019" "Gavin D. Howard" "General Commands Manual"
.
.SH "NAME"
\fBbc\fR \- arbitrary\-precision arithmetic language and calculator
.
.SH "SYNOPSIS"
\fBbc\fR [\fB\-ghilPqsvVw\fR] [\fB\-\-global\-stacks\fR] [\fB\-\-help\fR] [\fB\-\-interactive\fR] [\fB\-\-mathlib\fR] [\fB\-\-no\-prompt\fR] [\fB\-\-quiet\fR] [\fB\-\-standard\fR] [\fB\-\-warn\fR] [\fB\-\-version\fR] [\fB\-e\fR \fIexpr\fR] [\fB\-\-expression=\fR\fIexpr\fR\.\.\.] [\fB\-f\fR \fIfile\fR\.\.\.] [\fB\-file=\fR\fIfile\fR\.\.\.] [\fIfile\fR\.\.\.]
.
.SH "DESCRIPTION"
bc(1) is an interactive processor for a language first standardized in 1991 by POSIX\. (The current standard is here \fIhttps://pubs\.opengroup\.org/onlinepubs/9699919799/utilities/bc\.html\fR\.) The language provides unlimited precision decimal arithmetic and is somewhat C\-like, but there are differences\. Such differences will be noted in this document\.
.
.P
After parsing and handling options, this bc(1) reads any files given on the command line and executes them before reading from \fBstdin\fR\.
.
.P
With all build options, except for extra math, enabled this bc(1) is a drop\-in replacement for \fB\fIany\fR\fR bc(1), including (and especially) the GNU bc(1)\. It is also a drop\-in replacement for any bc(1) if extra math is enabled, but it will have extra features not found in other bc(1) implementations\.
.
.SH "OPTIONS"
The following are the options that bc(1) accepts\.
.
.TP
\fB\-g\fR, \fB\-\-global\-stacks\fR
Turns the globals \fBibase\fR, \fBobase\fR, and \fBscale\fR into stacks\.
.
.IP
This has the effect that a copy of the current value of all three are pushed onto a stack for every function call, as well as popped when every function returns\. This means that functions can assign to any and all of those globals without worrying that the change will affect other functions\. Thus, \fBoutput(x,b)\fR (in the \fIextended library\fR) could have been written like this:
.
.IP
\fBdefine void output(x, b) { obase=b; x }\fR
.
.IP
instead of like this:
.
.IP
\fBdefine void output(x, b) { auto c; c=obase; obase=b; x; obase=c }\fR
.
.IP
This makes writing functions much easier\.
.
.IP
However, since using this flag means that functions cannot set \fBibase\fR, \fBobase\fR, or \fBscale\fR globally, functions that are made to do so cannot work anymore\. There are two possible use cases for that, and each has a solution\.
.
.IP
First, if a function is called on startup to turn bc(1) into a number converter, it is possible to replace that capability with various shell aliases\. Examples:
.
.IP
\fBalias d2o="bc \-e ibase=A \-e obase=8"; alias h2b="bc \-e ibase=G \-e obase=2"\fR
.
.IP
Second, if the purpose of a function is to set \fBibase\fR, \fBobase\fR, or \fBscale\fR globally for any other purpose, it could be split into one to three functions (based on how many globals it sets) and each of those functions could return the desired value for a global\.
.
.IP
If this behavior is desired for every run of bc(1), then users could make sure to define \fBBC_ENV_ARGS\fR and include this option (see the ENVIRONMENT VARIABLES section for more details)\.
.
.IP
If \fB\-s\fR, \fB\-w\fR, or any equivalents are used, this option is ignored\.
.
.IP
This is a \fBnon\-portable extension\fR\.
.
.TP
\fB\-h\fR, \fB\-\-help\fR
Prints a usage message and quits\.
.
.TP
\fB\-i\fR, \fB\-\-interactive\fR
Forces interactive mode\.
.
.IP
Per the standard \fIhttps://pubs\.opengroup\.org/onlinepubs/9699919799/utilities/bc\.html\fR, bc(1) has an interactive mode and a non\-interactive mode\. The interactive mode is turned on automatically when both \fBstdin\fR and \fBstdout\fR are hooked to a terminal, but this flag can turn it on in other cases\. In interactive mode, bc(1) attempts to recover from errors (see the RESET section), and in normal execution, flushes \fBstdout\fR as soon as execution is done for the current input\.
.
.IP
This is a \fBnon\-portable extension\fR\.
.
.TP
\fB\-l\fR, \fB\-\-mathlib\fR
Sets \fBscale\fR (see the Scale section) to \fB20\fR and loads the included math library before running any code, including any expressions or files specified on the command line\.
.
.IP
To learn what is in the library, see the LIBRARY section\.
.
.TP
\fB\-P\fR, \fB\-\-no\-prompt\fR
Disables the prompt in interactive mode\. This is mostly for those users that do not want a prompt or are not used to having them in \fBbc\fR\. Most of those users would want to put this option in \fBBC_ENV_ARGS\fR\.
.
.IP
If the prompt has been disabled while building bc(1), this option is a no\-op\.
.
.IP
This is a \fBnon\-portable extension\fR\.
.
.TP
\fB\-q\fR, \fB\-\-quiet\fR
Do not print copyright header\. bc(1) will also suppress the header in non\-interactive mode\.
.
.IP
This is mostly for compatibility with the GNU bc(1) \fIhttps://www\.gnu\.org/software/bc/\fR\.
.
.IP
This is a \fBnon\-portable extension\fR\.
.
.TP
\fB\-s\fR, \fB\-\-standard\fR
Process exactly the language defined by the standard \fIhttps://pubs\.opengroup\.org/onlinepubs/9699919799/utilities/bc\.html\fR and error if any extensions are used\.
.
.IP
This is a \fBnon\-portable extension\fR\.
.
.TP
\fB\-v\fR, \fB\-V\fR, \fB\-\-version\fR
Print the version information (copyright header) and exit\.
.
.IP
This is a \fBnon\-portable extension\fR\.
.
.TP
\fB\-w\fR, \fB\-\-warn\fR
Like \fB\-s\fR and \fB\-\-standard\fR, except that warnings (and not errors) are given for non\-standard extensions\.
.
.IP
This is a \fBnon\-portable extension\fR\.
.
.TP
\fB\-e\fR \fIexpr\fR, \fB\-\-expression\fR=\fIexpr\fR
Evaluates \fBexpr\fR\. If multiple expressions are given, they are evaluated in order\. If files are given as well (see below), the expressions and files are evaluated in the order given\. This means that if a file is given before an expression, the file is read in and evaluated first\.
.
.IP
In other bc(1) implementations, this option causes the program to execute the expressions and then exit\. This bc(1) does not, unless the \fBBC_EXPR_EXIT\fR is defined (see the ENVIRONMENT VARIABLES section)\.
.
.IP
This is a \fBnon\-portable extension\fR\.
.
.TP
\fB\-f\fR \fIfile\fR, \fB\-\-file\fR=\fIfile\fR
Reads in \fBfile\fR and evaluates it\. If expressions are also given (see above), the expressions are evaluated in the order given\.
.
.IP
In other bc(1) implementations, this option causes the program to execute the files and then exit\. This bc(1) does not, unless the \fBBC_EXPR_EXIT\fR is defined (see the ENVIRONMENT VARIABLES section)\.
.
.IP
This is a \fBnon\-portable extension\fR\.
.
.SH "STDOUT"
Any non\-error output is written to \fBstdout\fR\.
.
.P
\fBNote\fR: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the EXIT STATUS section) if it cannot write to \fBstdout\fR, so if \fBstdout\fR is closed, as in \fBbc <file> >&\-\fR, it will quit with an error\. This is done so that bc(1) can report problems when \fBstdout\fR is redirected to a file\.
.
.P
If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect \fBstdout\fR to \fB/dev/null\fR\.
.
.SH "STDERR"
Any error output is written to \fBstderr\fR\.
.
.P
\fBNote\fR: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the EXIT STATUS section) if it cannot write to \fBstderr\fR, so if \fBstderr\fR is closed, as in \fBbc <file> 2>&\-\fR, it will quit with an error\. This is done so that bc(1) can report problems when \fBstderr\fR is redirected to a file\.
.
.P
If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect \fBstderr\fR to \fB/dev/null\fR\.
.
.SH "SYNTAX"
The syntax for bc(1) programs is mostly C\-like, with some differences\. This bc(1) follows the POSIX standard \fIhttps://pubs\.opengroup\.org/onlinepubs/9699919799/utilities/bc\.html\fR, which is a much more thorough resource for the language this bc(1) accepts\. This section is meant to be a summary and a listing of all the extensions to the standard \fIhttps://pubs\.opengroup\.org/onlinepubs/9699919799/utilities/bc\.html\fR\.
.
.P
In the sections below, \fBE\fR means expression, \fBS\fR means statement, and \fBI\fR means identifier\.
.
.P
Identifiers (\fBI\fR) start with a lowercase letter and can be followed by any number (up to \fBBC_NAME_MAX\-1\fR) of lowercase letters (\fBa\-z\fR), digits (\fB0\-9\fR), and underscores (\fB_\fR)\. The regex is \fB[a\-z][a\-z0\-9_]*\fR Identifiers with more than one character (letter) are a \fBnon\-portable extension\fR\.
.
.P
\fBibase\fR is a global variable determining how to interpret constant numbers\. It is the "input" base, or the number base used for interpreting input numbers\. \fBibase\fR is initially \fB10\fR\. If the \fB\-s\fR (\fB\-\-standard\fR) and \fB\-w\fR (\fB\-\-warn\fR) flags were not given on the command line, the max allowable value for \fBibase\fR is \fB36\fR\. Otherwise, it is \fB16\fR\. The min allowable value for \fBibase\fR is \fB2\fR\. The max allowable value for \fBibase\fR can be queried in bc(1) programs with the \fBmaxibase()\fR built in function\.
.
.P
\fBobase\fR is a global variable determining how to output results\. It is the "output" base, or the number base used for outputting numbers\. \fBobase\fR is initially \fB10\fR\. The max allowable value for \fBobase\fR is \fBBC_BASE_MAX\fR\. The min allowable value for \fBobase\fR is \fB2\fR, unless bc(1) was built with the extra math option\. If it was, then the min allowable value is \fB0\fR\. In this case, if \fBobase\fR is \fB0\fR, values are output in scientific notation, and if \fBobase\fR is \fB1\fR, values are output in engineering notation\. (Outputting in scientific or engineering notation are \fBnon\-portable extensions\fR\.) The max allowable value for \fBobase\fR can be queried in bc(1) programs with the \fBmaxobase()\fR built in function\.
.
.P
The \fBscale\fR of an expression is the number of digits in the result of the expression right of the decimal point, and \fBscale\fR is a global variable that sets the precision of any operations, with exceptions\. \fBscale\fR is initially \fB0\fR\. \fBscale\fR cannot be negative\. The max allowable value for \fBscale\fR can be queried in bc(1) programs with the \fBmaxscale()\fR built in function\.
.
.P
bc(1) has both \fBglobal\fR variables and \fBlocal\fR variables\. All \fBlocal\fR variables are local to the function; they are parameters or are introduced in the \fBauto\fR list of a function (see FUNCTIONS)\. If a variable is accessed which is not a parameter or in the \fBauto\fR list, it is assumed to be \fBglobal\fR\. If a parent function has a \fBlocal\fR variable version of a \fBglobal\fR variable that is accessed by a function that it calls, the value of that \fBglobal\fR variable in the child function is the value of the variable in the parent function, not the value of the actual \fBglobal\fR variable\.
.
.P
All of the above applies to arrays as well\.
.
.P
The value of a statement that is an expression (i\.e\., any of the \fINamed Expressions\fR or \fIOperands\fR) is printed unless the lowest precedence operator is an \fI\fBassignment\fR\fR operator \fB\fIand\fR\fR the expression is not surrounded by parentheses\.
.
.P
The value that is printed is also assigned to the special variable \fBlast\fR\. A single dot (\fB\.\fR) may also be used as a synonym for \fBlast\fR\. These are \fBnon\-portable extensions\fR\.
.
.P
Either semicolons or newlines may separate statements\.
.
.SS "Comments"
There are two kinds of comments:
.
.IP "1." 4
Block comments are enclosed in \fB/*\fR and \fB*/\fR\.
.
.IP "2." 4
Line comments go from \fB#\fR until, and not including, the next newline\. This is a \fBnon\-portable extension\fR\.
.
.IP "" 0
.
.P
\fI\fR
.
.SS "Named Expressions"
The following are named expressions in bc(1):
.
.IP "1." 4
Variables: \fBI\fR
.
.IP "2." 4
Array Elements: \fBI[E]\fR
.
.IP "3." 4
\fBibase\fR
.
.IP "4." 4
\fBobase\fR
.
.IP "5." 4
\fBscale\fR
.
.IP "6." 4
\fBlast\fR or a single dot (\fB\.\fR)
.
.IP "" 0
.
.P
Number 6 is a \fBnon\-portable extension\fR\.
.
.P
Variables and arrays do not interfere; users can have arrays named the same as variables\. This also applies to functions (see the FUNCTIONS section), so a user can have a variable, array, and function that all have the same name, and they will not shadow each other\.
.
.P
Named expressions are required as the operand of \fI\fBincrement\fR/\fBdecrement\fR operators\fR and as the left side of \fI\fBassignment\fR operators\fR\.
.
.P
\fI\fR
.
.SS "Operands"
The following are valid operands in bc(1):
.
.IP "1." 4
Numbers (see \fINumbers\fR below)\.
.
.IP "2." 4
Array indices (\fBI[E]\fR)\.
.
.IP "3." 4
\fB(E)\fR: The value of \fBE\fR (used to change precedence)\.
.
.IP "4." 4
\fBsqrt(E)\fR: The square root of \fBE\fR\. \fBE\fR must be non\-negative\.
.
.IP "5." 4
\fBlength(E)\fR: The number of significant decimal digits in \fBE\fR\.
.
.IP "6." 4
\fBlength(I[])\fR: The number of elements in the array \fBI\fR\. This is a \fBnon\-portable extension\fR\.
.
.IP "7." 4
\fBscale(E)\fR: The \fBscale\fR of \fBE\fR\.
.
.IP "8." 4
\fBabs(E)\fR: The absolute value of \fBE\fR\. This is a \fBnon\-portable extension\fR\.
.
.IP "9." 4
\fBI()\fR, \fBI(E)\fR, \fBI(E, E)\fR, and so on, where \fBI\fR is an identifier for a non\-\fIvoid function\fR\. The \fBE\fR parameters may also be arrays and \fIarray references\fR\.
.
.IP "10." 4
\fBread()\fR: Reads a line from \fBstdin\fR and uses that as an expression\. The result of that expression is the result of the \fBread()\fR operand\. This is a \fBnon\-portable extension\fR\.
.
.IP "11." 4
\fBmaxibase()\fR: The max allowable \fBibase\fR\. This is a \fBnon\-portable extension\fR\.
.
.IP "12." 4
\fBmaxobase()\fR: The max allowable \fBobase\fR\. This is a \fBnon\-portable extension\fR\.
.
.IP "13." 4
\fBmaxscale()\fR: The max allowable \fBscale\fR\. This is a \fBnon\-portable extension\fR\.
.
.IP "" 0
.
.P
\fI\fR
.
.SS "Numbers"
Numbers are strings made up of digits, uppercase letters, and at most \fB1\fR period for a radix\. Numbers can have up to \fBBC_NUM_MAX\fR digits\. Uppercase letters equal \fB9\fR + their position in the alphabet (i\.e\., \fBA\fR equals \fB10\fR, or \fB9 + 1\fR)\. If a digit or letter makes no sense with the current value of \fBibase\fR, they are set to the value of the highest valid digit in \fBibase\fR\.
.
.P
Single\-character numbers (i\.e\., \fBA\fR) take the value that they would have if they were valid digits, regardless of the value of \fBibase\fR\. This means that \fBA\fR always equals decimal \fB10\fR and \fBZ\fR always equals decimal \fB35\fR\.
.
.P
In addition, if bc(1) was built with the extra math option, it accepts numbers in scientific notation\. For bc(1), an example is \fB1\.89237e9\fR, which is equal to \fB1892370000\fR\. Negative exponents are also allowed, so \fB4\.2890e\-3\fR is equal to \fB0\.0042890\fR\.
.
.P
Using scientific notation is an error or warning if the \fB\-s\fR or \fB\-w\fR, respectively, command\-line options (or equivalents) are given\.
.
.P
\fBWARNING\fR: Both the number and the exponent in scientific notation are interpreted according to the current \fBibase\fR, but the number is still multiplied by \fB10^exponent\fR regardless of the current \fBibase\fR\. For example, if \fBibase\fR is \fB16\fR and bc(1) is given the number string \fB"FFeA"\fR, the resulting decimal number will be \fB2550000000000\fR, and if bc(1) is given the number string \fB"10e\-4"\fR, the resulting decimal number will be \fB0\.0016\fR\.
.
.P
Accepting input as scientific notation is a \fBnon\-portable extension\fR\.
.
.SS "Operators"
The following arithmetic and logical operators can be used\. They are listed in order of decreasing precedence\. Operators in the same group have the same precedence\.
.
.TP
\fB++\fR \fB\-\-\fR
Type: Prefix and Postfix
.
.IP
Associativity: None
.
.IP
Description: \fBincrement\fR, \fBdecrement\fR
.
.TP
\fB\-\fR \fB!\fR
Type: Prefix
.
.IP
Associativity: None
.
.IP
Description: \fBnegation\fR, \fBboolean not\fR
.
.TP
\fB$\fR
Type: Postfix
.
.IP
Associativity: None
.
.IP
Description: \fBtruncation\fR
.
.TP
\fB@\fR
Type: Binary
.
.IP
Associativity: Right
.
.IP
Description: \fBset precision\fR
.
.TP
\fB^\fR
Type: Binary
.
.IP
Associativity: Right
.
.IP
Description: \fBpower\fR
.
.TP
\fB*\fR \fB/\fR \fB%\fR
Type: Binary
.
.IP
Associativity: Left
.
.IP
Description: \fBmultiply\fR, \fBdivide\fR, \fBmodulus\fR
.
.TP
\fB+\fR \fB\-\fR
Type: Binary
.
.IP
Associativity: Left
.
.IP
Description: \fBadd\fR, \fBsubtract\fR
.
.TP
\fB<<\fR \fB>>\fR
Type: Binary
.
.IP
Associativity: Left
.
.IP
Description: \fBshift left\fR, \fBshift right\fR
.
.TP
\fB=\fR \fB<<=\fR \fB>>=\fR \fB+=\fR \fB\-=\fR \fB*=\fR \fB/=\fR \fB%=\fR \fB^=\fR \fB@=\fR
Type: Binary
.
.IP
Associativity: Right
.
.IP
Description: \fBassignment\fR
.
.TP
\fB==\fR \fB<=\fR \fB>=\fR \fB!=\fR \fB<\fR \fB>\fR
Type: Binary
.
.IP
Associativity: Left
.
.IP
Description: \fBrelational\fR
.
.TP
\fB&&\fR
Type: Binary
.
.IP
Associativity: Left
.
.IP
Description: \fBboolean and\fR
.
.TP
\fB||\fR
Type: Binary
.
.IP
Associativity: Left
.
.IP
Description: \fBboolean or\fR
.
.P
The operators will be described in more detail below\.
.
.P
\fI\fR
.
.TP
\fB++\fR \fB\-\-\fR
The prefix and postfix \fBincrement\fR and \fBdecrement\fR operators behave exactly like they would in C\. They require a \fInamed expression\fR as an operand\.
.
.TP
\fB\-\fR
The \fBnegation\fR operator returns \fB0\fR if a user attempts to negate any expression with the value \fB0\fR\. Otherwise, a copy of the expression with its sign flipped is returned\.
.
.TP
\fB!\fR
The \fBboolean not\fR operator returns \fB1\fR if the expression is \fB0\fR, or \fB0\fR otherwise\.
.
.IP
This is a \fBnon\-portable extension\fR\.
.
.TP
\fB$\fR
The \fBtruncation\fR operator returns a copy of the given expression with all of its \fBscale\fR removed\.
.
.IP
This is a \fBnon\-portable extension\fR\.
.
.IP
This is only available if bc(1) has been compiled with the extra math option enabled\.
.
.TP
\fB@\fR
The \fBset precision\fR operator takes two expressions and returns a copy of the first with its \fBscale\fR equal to the value of the second expression\. That could either mean that the number is returned without change (if the \fBscale\fR of the first expression matches the value of the second expression), extended (if it is less), or truncated (if it is more)\.
.
.IP
The second expression must be an integer (no \fBscale\fR) and non\-negative\.
.
.IP
This is a \fBnon\-portable extension\fR\.
.
.IP
This is only available if bc(1) has been compiled with the extra math option enabled\.
.
.TP
\fB^\fR
The \fBpower\fR operator (not the \fBexclusive or\fR operator, as it would be in C) takes two expressions and raises the first to the power of the value of the second\.
.
.IP
The second expression must be an integer (no \fBscale\fR), and if it is negative, the first value must be non\-zero\.
.
.TP
\fB*\fR
The \fBmultiply\fR operator takes two expressions, multiplies them, and returns the product\. If \fBa\fR is the \fBscale\fR of the first expression and \fBb\fR is the \fBscale\fR of the second expression, the scale of the result is equal to \fBmin(a+b,max(scale,a,b))\fR where \fBmin\fR and \fBmax\fR return the obvious values\.
.
.TP
\fB/\fR
The \fBdivide\fR operator takes two expressions, divides them, and returns the quotient\. The scale of the result shall be the value of \fBscale\fR\.
.
.IP
The second expression must be non\-zero\.
.
.TP
\fB%\fR
The \fBmodulus\fR operator takes two expressions, \fBa\fR and \fBb\fR, and evaluates them by 1) Computing \fBa/b\fR to current \fBscale\fR and 2) Using the result of step 1 to calculate \fBa\-(a/b)*b\fR to scale \fBmax(scale+scale(b),scale(a))\fR\.
.
.IP
The second expression must be non\-zero\.
.
.TP
\fB+\fR
The \fBadd\fR operator takes two expressions, \fBa\fR and \fBb\fR, and returns the sum, with a \fBscale\fR equal to the max of the \fBscale\fRs of \fBa\fR and \fBb\fR\.
.
.TP
\fB\-\fR
The \fBsubtract\fR operator takes two expressions, \fBa\fR and \fBb\fR, and returns the difference, with a \fBscale\fR equal to the max of the \fBscale\fRs of \fBa\fR and \fBb\fR\.
.
.TP
\fB<<\fR
The \fBleft shift\fR operator takes two expressions, \fBa\fR and \fBb\fR, and returns a copy of the value of \fBa\fR with its decimal point moved \fBb\fR places to the right\.
.
.IP
The second expression must be an integer (no \fBscale\fR) and non\-negative\.
.
.IP
This is a \fBnon\-portable extension\fR\.
.
.IP
This is only available if bc(1) has been compiled with the extra math option enabled\.
.
.TP
\fB>>\fR
The \fBright shift\fR operator takes two expressions, \fBa\fR and \fBb\fR, and returns a copy of the value of \fBa\fR with its decimal point moved \fBb\fR places to the left\.
.
.IP
The second expression must be an integer (no \fBscale\fR) and non\-negative\.
.
.IP
This is a \fBnon\-portable extension\fR\.
.
.IP
This is only available if bc(1) has been compiled with the extra math option enabled\.
.
.P
\fI\fR
.
.TP
\fB=\fR \fB<<=\fR \fB>>=\fR \fB+=\fR \fB\-=\fR \fB*=\fR \fB/=\fR \fB%=\fR \fB^=\fR \fB@=\fR
The \fBassignment\fR operators take two expressions, \fBa\fR and \fBb\fR where \fBa\fR is a \fInamed expression\fR\.
.
.IP
For \fB=\fR, \fBb\fR is copied and the result is assigned to \fBa\fR\. For all others, \fBa\fR and \fBb\fR are applied as operands to the corresponding arithmetic operator and the result is assigned to \fBa\fR\.
.
.IP
The \fBassignment\fR operators that correspond to operators that are extensions are themselves extensions\.
.
.IP
Also, those \fBassignment\fR operators that are extensions are only available if bc(1) has been compiled with the extra math option enabled\.
.
.TP
\fB==\fR \fB<=\fR \fB>=\fR \fB!=\fR \fB<\fR \fB>\fR
The \fBrelational\fR operators compare two expressions, \fBa\fR and \fBb\fR, and if the relation holds, according to C language semantics, the result is \fB1\fR\. Otherwise, it is \fB0\fR\.
.
.IP
Note that unlike in C, these operators have a lower precedence than the \fBassignment\fR operators, which means that \fBa=b>c\fR is interpreted as \fB(a=b)>c\fR\.
.
.IP
Also, unlike the standard \fIhttps://pubs\.opengroup\.org/onlinepubs/9699919799/utilities/bc\.html\fR requires, these operators can appear anywhere any other expressions can be used\. This allowance is a \fBnon\-portable extension\fR\.
.
.TP
\fB&&\fR
The \fBboolean and\fR operator takes two expressions and returns \fB1\fR if both expressions are non\-zero, \fB0\fR otherwise\.
.
.IP
This is \fB\fInot\fR\fR a short\-circuit operator\.
.
.IP
This is a \fBnon\-portable extension\fR\.
.
.TP
\fB||\fR
The \fBboolean or\fR operator takes two expressions and returns \fB1\fR if one of the expressions is non\-zero, \fB0\fR otherwise\.
.
.IP
This is \fB\fInot\fR\fR a short\-circuit operator\.
.
.IP
This is a \fBnon\-portable extension\fR\.
.
.SS "Statements"
The following items are statements:
.
.IP "1." 4
\fBE\fR
.
.IP "2." 4
\fB{\fR \fBS\fR \fB;\fR \.\.\. \fB;\fR \fBS\fR \fB}\fR
.
.IP "3." 4
\fBif\fR \fB(\fR \fBE\fR \fB)\fR \fBS\fR
.
.IP "4." 4
\fBif\fR \fB(\fR \fBE\fR \fB)\fR \fBS\fR \fBelse\fR \fBS\fR
.
.IP "5." 4
\fBwhile\fR \fB(\fR \fBE\fR \fB)\fR \fBS\fR
.
.IP "6." 4
\fBfor\fR \fB(\fR \fBE\fR \fB;\fR \fBE\fR \fB;\fR \fBE\fR \fB)\fR \fBS\fR
.
.IP "7." 4
An empty statement
.
.IP "8." 4
\fBbreak\fR
.
.IP "9." 4
\fBcontinue\fR
.
.IP "10." 4
\fBquit\fR
.
.IP "11." 4
\fBhalt\fR
.
.IP "12." 4
\fBlimits\fR
.
.IP "13." 4
A string of characters, enclosed in double quotes
.
.IP "14." 4
\fBprint\fR \fBE\fR \fB,\fR \.\.\. \fB,\fR \fBE\fR
.
.IP "15." 4
\fBI()\fR, \fBI(E)\fR, \fBI(E, E)\fR, and so on, where \fBI\fR is an identifier for a \fIvoid function\fR\. The \fBE\fR parameters may also be arrays and \fIarray references\fR\.
.
.IP "" 0
.
.P
Numbers 4, 9, 11, 12, 14, and 15 are \fBnon\-portable extensions\fR\.
.
.P
Also, as a \fBnon\-portable extension\fR, any or all of the expressions in the header of a for loop may be omitted\. If the condition (second expression) is omitted, it is assumed to be a constant \fB1\fR\.
.
.P
The \fBbreak\fR statement causes a loop to stop iterating and resume execution immediately following a loop\. This is only allowed in loops\.
.
.P
The \fBcontinue\fR statement causes a loop iteration to stop early and returns to the start of the loop, including testing the loop condition\. This is only allowed in loops\.
.
.P
The \fBif\fR \fBelse\fR statement does the same thing as in C\.
.
.P
The \fBquit\fR statement causes bc(1) to quit, even if it is on a branch that will not be executed (it is a compile\-time command)\.
.
.P
The \fBhalt\fR statement causes bc(1) to quit, if it is executed\. (Unlike \fBquit\fR if it is on a branch of an \fBif\fR statement that is not executed, bc(1) does not quit\.)
.
.P
The \fBlimits\fR statement prints the limits that this bc(1) is subject to\. This is like the \fBquit\fR statement in that it is a compile\-time command\.
.
.P
An expression by itself is evaluated and printed, followed by a newline\. If bc(1) has been built with the extra math option enabled, both scientific notation and engineering notation are available for printing the results of expressions\. Scientific notation is activated by assigning \fB0\fR to \fBobase\fR (in any other context, an \fBobase\fR of \fB0\fR is invalid), and engineering notation is activated by assigning \fB1\fR to \fBobase\fR (which is also invalid in any other context)\. To deactivate them, just assign a different value to \fBobase\fR\.
.
.P
Scientific notation and engineering notation are disabled if bc(1) is run with either the \fB\-s\fR or \fB\-w\fR command\-line options (or equivalents)\.
.
.P
Printing numbers in scientific notation and/or engineering notation is a \fBnon\-portable extension\fR\.
.
.SS "Print Statement"
The "expressions" in a \fBprint\fR statement may also be strings\. If they are, there are backslash escape sequences that are interpreted specially\. What those sequences are, and what they cause to be printed, are shown below:
.
.TP
\fB\ea\fR
\fB\ea\fR
.
.TP
\fB\eb\fR
\fB\eb\fR
.
.TP
\fB\e\e\fR
\fB\e\fR
.
.TP
\fB\ee\fR
\fB\e\fR
.
.TP
\fB\ef\fR
\fB\ef\fR
.
.TP
\fB\en\fR
\fB\en\fR
.
.TP
\fB\eq\fR
\fB"\fR
.
.TP
\fB\er\fR
\fB\er\fR
.
.TP
\fB\et\fR
\fB\et\fR
.
.P
Any other character following a backslash causes the backslash and character to be printed as\-is\.
.
.P
Any non\-string expression in a print statement shall be assigned to \fBlast\fR, like any other expression that is printed\.
.
.SS "Order of Evaluation"
All expressions in a statment are evaluated left to right, except as necessary to maintain order of operations\. This means, for example, that in the expression \fBi = 0; a[i++] = i++\fR, the first (or 0th) element of \fBa\fR is set to \fB1\fR, and \fBi\fR is equal to \fB2\fR at the end of the expression\.
.
.P
This includes function arguments\. Thus, this means that in the expression \fBi = 0; x(i++, i++)\fR, the first argument passed to \fBx()\fR is \fB0\fR, and the second argument is \fB1\fR, while \fBi\fR is equal to \fB2\fR before the function starts executing\.
.
.SH "FUNCTIONS"
Function definitions are as follows:
.
.IP "" 4
.
.nf
define I(I,\.\.\.,I){
auto I,\.\.\.,I
S;\.\.\.;S
return(E)
}
.
.fi
.
.IP "" 0
.
.P
Any \fBI\fR in the parameter list or \fBauto\fR list may be replaced with \fBI[]\fR to make a parameter or \fBauto\fR var an array\.
.
.P
As a \fBnon\-portable extension\fR, the opening brace of a \fBdefine\fR statement may appear on the next line\.
.
.P
The return statement may also be in the following forms:
.
.IP "1." 4
\fBreturn\fR
.
.IP "2." 4
\fBreturn\fR \fB(\fR \fB)\fR
.
.IP "3." 4
\fBreturn\fR \fBE\fR
.
.IP "" 0
.
.P
The first two, or not specifying a \fBreturn\fR statement, is equivalent to \fBreturn (0)\fR, unless the function is a \fIvoid function\fR\.
.
.P
\fI\fR
.
.SS "Void Functions"
Functions can also be void functions, defined as follows:
.
.IP "" 4
.
.nf
define void I(I,\.\.\.,I){
auto I,\.\.\.,I
S;\.\.\.;S
return
}
.
.fi
.
.IP "" 0
.
.P
They can only be used as standalone expressions, where such an expression would be printed alone, except in a print statement\.
.
.P
Void functions can only use the first two \fBreturn\fR statements listed above\. They can also omit the return statement entirely\.
.
.P
The word \fBvoid\fR is not treated as a keyword; it is still possible to have variables, arrays, and functions named \fBvoid\fR\. The word \fBvoid\fR is only treated specially right after the \fBdefine\fR keyword\.
.
.P
This is a \fBnon\-portable extension\fR\.
.
.P
\fI\fR
.
.SS "Array References"
For any array in the parameter list, if the array is declared in the form
.
.IP "" 4
.
.nf
*I[]
.
.fi
.
.IP "" 0
.
.P
it is a \fBreference\fR\. Any changes to the array in the function are reflected, when the function returns, to the array that was passed in\.
.
.P
Other than this, all function arguments are passed by value\.
.
.P
This is a \fBnon\-portable extension\fR\.
.
.SH "LIBRARY"
All of the functions below, including the functions in the \fIextended library\fR if bc(1) has been compiled with the extra math option enabled, are available when the \fB\-l\fR or \fB\-\-mathlib\fR command\-line flags are given\.
.
.P
\fI\fR
.
.SS "Standard Library"
The standard \fIhttps://pubs\.opengroup\.org/onlinepubs/9699919799/utilities/bc\.html\fR defines the following functions for the math library:
.
.TP
\fBs(x)\fR
Returns the sine of \fBx\fR, which is assumed to be in radians\.
.
.IP
This is a \fItranscendental function\fR\.
.
.TP
\fBc(x)\fR
Returns the cosine of \fBx\fR, which is assumed to be in radians\.
.
.IP
This is a \fItranscendental function\fR\.
.
.TP
\fBa(x)\fR
Returns the arctangent of \fBx\fR, in radians\.
.
.IP
This is a \fItranscendental function\fR\.
.
.TP
\fBl(x)\fR
Returns the natural logarithm of \fBx\fR\.
.
.IP
This is a \fItranscendental function\fR\.
.
.TP
\fBe(x)\fR
Returns the mathematical constant \fBe\fR raised to the power of \fBx\fR\.
.
.IP
This is a \fItranscendental function\fR\.
.
.TP
\fBj(x, n)\fR
Returns the bessel integer order \fBn\fR (truncated) of \fBx\fR\.
.
.IP
This is a \fItranscendental function\fR\.
.
.P
\fI\fR
.
.SS "Extended Library"
In addition to the \fIstandard library\fR, if bc(1) has been built with the extra math option, the following functions are available when either the \fB\-l\fR or \fB\-\-mathlib\fR options are given\.
.
.P
However, the extended library is \fB\fInot\fR\fR loaded when the \fB\-s\fR/\fB\-\-standard\fR or \fB\-w\fR/\fB\-\-warn\fR options are given since they are not part of the library defined by the standard \fIhttps://pubs\.opengroup\.org/onlinepubs/9699919799/utilities/bc\.html\fR\.
.
.P
The extended library is a \fBnon\-portable extension\fR\.
.
.TP
\fBr(x, p)\fR
Returns \fBx\fR rounded to \fBp\fR decimal places according to the rounding mode round half away from \fB0\fR \fIhttps://en\.wikipedia\.org/wiki/Rounding#Round_half_away_from_zero\fR\.
.
.TP
\fBceil(x, p)\fR
Returns \fBx\fR rounded to \fBp\fR decimal places according to the rounding mode round away from \fB0\fR \fIhttps://en\.wikipedia\.org/wiki/Rounding#Rounding_away_from_zero\fR\.
.
.TP
\fBf(x)\fR
Returns the factorial of the truncated absolute value of \fBx\fR\.
.
.TP
\fBperm(n, k)\fR
Returns the permutation of the truncated absolute value of \fBn\fR of the truncated absolute value of \fBk\fR, if \fBk <= n\fR\. If not, it returns \fB0\fR\.
.
.TP
\fBcomb(n, k)\fR
Returns the combination of the truncated absolute value of \fBn\fR of the truncated absolute value of \fBk\fR, if \fBk <= n\fR\. If not, it returns \fB0\fR\.
.
.TP
\fBl2(x)\fR
Returns the logarithm base \fB2\fR of \fBx\fR\.
.
.IP
This is a \fItranscendental function\fR\.
.
.TP
\fBl10(x)\fR
Returns the logarithm base \fB10\fR of \fBx\fR\.
.
.IP
This is a \fItranscendental function\fR\.
.
.TP
\fBlog(x, b)\fR
Returns the logarithm base \fBb\fR of \fBx\fR\.
.
.IP
This is a \fItranscendental function\fR\.
.
.TP
\fBcbrt(x)\fR
Returns the cube root of \fBx\fR\.
.
.TP
\fBroot(x, n)\fR
Calculates the truncated value of \fBn\fR, \fBr\fR, and returns the \fBr\fRth root of \fBx\fR to the current \fBscale\fR\.
.
.IP
If \fBr\fR is \fB0\fR or negative, this raises an error and causes bc(1) to reset (see the RESET section)\. It also raises an error and causes bc(1) to reset if \fBr\fR is even and \fBx\fR is negative\.
.
.TP
\fBpi(p)\fR
Returns \fBpi\fR to \fBp\fR decimal places\.
.
.IP
This is a \fItranscendental function\fR\.
.
.TP
\fBt(x)\fR
Returns the tangent of \fBx\fR, which is assumed to be in radians\.
.
.IP
This is a \fItranscendental function\fR\.
.
.TP
\fBa2(y, x)\fR
Returns the arctangent of \fBy/x\fR, in radians\. If both \fBy\fR and \fBx\fR are equal to \fB0\fR, it raises an error and causes bc(1) to reset (see the RESET section)\. Otherwise, if \fBx\fR is greater than \fB0\fR, it returns \fBa(y/x)\fR\. If \fBx\fR is less than \fB0\fR, and \fBy\fR is greater than or equal to \fB0\fR, it returns \fBa(y/x) + pi\fR\. If \fBx\fR is less than \fB0\fR, and \fBy\fR is less than \fB0\fR, it returns \fBa(y/x) \- pi\fR\. If \fBx\fR is equal to \fB0\fR, and \fBy\fR is greater than \fB0\fR, it returns \fBpi/2\fR\. If \fBx\fR is equal to \fB0\fR, and \fBy\fR is less than \fB0\fR, it returns \fB\-pi/2\fR\.
.
.IP
This function is the same as the \fBatan2()\fR function in many programming languages\.
.
.IP
This is a \fItranscendental function\fR\.
.
.TP
\fBsin(x)\fR
Returns the sine of \fBx\fR, which is assumed to be in radians\.
.
.IP
This is an alias of \fBs(x)\fR\.
.
.IP
This is a \fItranscendental function\fR\.
.
.TP
\fBcos(x)\fR
Returns the cosine of \fBx\fR, which is assumed to be in radians\.
.
.IP
This is an alias of \fBc(x)\fR\.
.
.IP
This is a \fItranscendental function\fR\.
.
.TP
\fBtan(x)\fR
Returns the tangent of \fBx\fR, which is assumed to be in radians\.
.
.IP
If \fBx\fR is equal to \fB1\fR or \fB\-1\fR, this raises an error and causes bc(1) to reset (see the RESET section)\.
.
.IP
This is an alias of \fBt(x)\fR\.
.
.IP
This is a \fItranscendental function\fR\.
.
.TP
\fBatan(x)\fR
Returns the arctangent of \fBx\fR, in radians\.
.
.IP
This is an alias of \fBa(x)\fR\.
.
.IP
This is a \fItranscendental function\fR\.
.
.TP
\fBatan2(y, x)\fR
Returns the arctangent of \fBy/x\fR, in radians\. If both \fBy\fR and \fBx\fR are equal to \fB0\fR, it raises an error and causes bc(1) to reset (see the RESET section)\. Otherwise, if \fBx\fR is greater than \fB0\fR, it returns \fBa(y/x)\fR\. If \fBx\fR is less than \fB0\fR, and \fBy\fR is greater than or equal to \fB0\fR, it returns \fBa(y/x) + pi\fR\. If \fBx\fR is less than \fB0\fR, and \fBy\fR is less than \fB0\fR, it returns \fBa(y/x) \- pi\fR\. If \fBx\fR is equal to \fB0\fR, and \fBy\fR is greater than \fB0\fR, it returns \fBpi/2\fR\. If \fBx\fR is equal to \fB0\fR, and \fBy\fR is less than \fB0\fR, it returns \fB\-pi/2\fR\.
.
.IP