xref: /freebsd/contrib/bc/manuals/bc/HN.1.md (revision 4d3fc8b0)
1<!---
2
3SPDX-License-Identifier: BSD-2-Clause
4
5Copyright (c) 2018-2023 Gavin D. Howard and contributors.
6
7Redistribution and use in source and binary forms, with or without
8modification, are permitted provided that the following conditions are met:
9
10* Redistributions of source code must retain the above copyright notice, this
11  list of conditions and the following disclaimer.
12
13* Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27POSSIBILITY OF SUCH DAMAGE.
28
29-->
30
31# NAME
32
33bc - arbitrary-precision decimal arithmetic language and calculator
34
35# SYNOPSIS
36
37**bc** [**-cCghilPqRsvVw**] [**-\-digit-clamp**] [**-\-no-digit-clamp**] [**-\-global-stacks**] [**-\-help**] [**-\-interactive**] [**-\-mathlib**] [**-\-no-prompt**] [**-\-no-read-prompt**] [**-\-quiet**] [**-\-standard**] [**-\-warn**] [**-\-version**] [**-e** *expr*] [**-\-expression**=*expr*...] [**-f** *file*...] [**-\-file**=*file*...] [*file*...] [**-I** *ibase*] [**-\-ibase**=*ibase*] [**-O** *obase*] [**-\-obase**=*obase*] [**-S** *scale*] [**-\-scale**=*scale*] [**-E** *seed*] [**-\-seed**=*seed*]
38
39# DESCRIPTION
40
41bc(1) is an interactive processor for a language first standardized in 1991 by
42POSIX. (See the **STANDARDS** section.) The language provides unlimited
43precision decimal arithmetic and is somewhat C-like, but there are differences.
44Such differences will be noted in this document.
45
46After parsing and handling options, this bc(1) reads any files given on the
47command line and executes them before reading from **stdin**.
48
49This bc(1) is a drop-in replacement for *any* bc(1), including (and
50especially) the GNU bc(1). It also has many extensions and extra features beyond
51other implementations.
52
53**Note**: If running this bc(1) on *any* script meant for another bc(1) gives a
54parse error, it is probably because a word this bc(1) reserves as a keyword is
55used as the name of a function, variable, or array. To fix that, use the
56command-line option **-r** *keyword*, where *keyword* is the keyword that is
57used as a name in the script. For more information, see the **OPTIONS** section.
58
59If parsing scripts meant for other bc(1) implementations still does not work,
60that is a bug and should be reported. See the **BUGS** section.
61
62# OPTIONS
63
64The following are the options that bc(1) accepts.
65
66**-C**, **-\-no-digit-clamp**
67
68:   Disables clamping of digits greater than or equal to the current **ibase**
69    when parsing numbers.
70
71    This means that the value added to a number from a digit is always that
72    digit's value multiplied by the value of ibase raised to the power of the
73    digit's position, which starts from 0 at the least significant digit.
74
75    If this and/or the **-c** or **-\-digit-clamp** options are given multiple
76    times, the last one given is used.
77
78    This option overrides the **BC_DIGIT_CLAMP** environment variable (see the
79    **ENVIRONMENT VARIABLES** section) and the default, which can be queried
80    with the **-h** or **-\-help** options.
81
82    This is a **non-portable extension**.
83
84**-c**, **-\-digit-clamp**
85
86:   Enables clamping of digits greater than or equal to the current **ibase**
87    when parsing numbers.
88
89    This means that digits that the value added to a number from a digit that is
90    greater than or equal to the ibase is the value of ibase minus 1 all
91    multiplied by the value of ibase raised to the power of the digit's
92    position, which starts from 0 at the least significant digit.
93
94    If this and/or the **-C** or **-\-no-digit-clamp** options are given
95    multiple times, the last one given is used.
96
97    This option overrides the **BC_DIGIT_CLAMP** environment variable (see the
98    **ENVIRONMENT VARIABLES** section) and the default, which can be queried
99    with the **-h** or **-\-help** options.
100
101    This is a **non-portable extension**.
102
103**-E** *seed*, **-\-seed**=*seed*
104
105:   Sets the builtin variable **seed** to the value *seed* assuming that *seed*
106    is in base 10. It is a fatal error if *seed* is not a valid number.
107
108    If multiple instances of this option are given, the last is used.
109
110    This is a **non-portable extension**.
111
112**-e** *expr*, **-\-expression**=*expr*
113
114:   Evaluates *expr*. If multiple expressions are given, they are evaluated in
115    order. If files are given as well (see the **-f** and **-\-file** options),
116    the expressions and files are evaluated in the order given. This means that
117    if a file is given before an expression, the file is read in and evaluated
118    first.
119
120    If this option is given on the command-line (i.e., not in **BC_ENV_ARGS**,
121    see the **ENVIRONMENT VARIABLES** section), then after processing all
122    expressions and files, bc(1) will exit, unless **-** (**stdin**) was given
123    as an argument at least once to **-f** or **-\-file**, whether on the
124    command-line or in **BC_ENV_ARGS**. However, if any other **-e**,
125    **-\-expression**, **-f**, or **-\-file** arguments are given after **-f-**
126    or equivalent is given, bc(1) will give a fatal error and exit.
127
128    This is a **non-portable extension**.
129
130**-f** *file*, **-\-file**=*file*
131
132:   Reads in *file* and evaluates it, line by line, as though it were read
133    through **stdin**. If expressions are also given (see the **-e** and
134    **-\-expression** options), the expressions are evaluated in the order
135    given.
136
137    If this option is given on the command-line (i.e., not in **BC_ENV_ARGS**,
138    see the **ENVIRONMENT VARIABLES** section), then after processing all
139    expressions and files, bc(1) will exit, unless **-** (**stdin**) was given
140    as an argument at least once to **-f** or **-\-file**. However, if any other
141    **-e**, **-\-expression**, **-f**, or **-\-file** arguments are given after
142    **-f-** or equivalent is given, bc(1) will give a fatal error and exit.
143
144    This is a **non-portable extension**.
145
146**-g**, **-\-global-stacks**
147
148:   Turns the globals **ibase**, **obase**, **scale**, and **seed** into stacks.
149
150    This has the effect that a copy of the current value of all four are pushed
151    onto a stack for every function call, as well as popped when every function
152    returns. This means that functions can assign to any and all of those
153    globals without worrying that the change will affect other functions.
154    Thus, a hypothetical function named **output(x,b)** that simply printed
155    **x** in base **b** could be written like this:
156
157        define void output(x, b) {
158            obase=b
159            x
160        }
161
162    instead of like this:
163
164        define void output(x, b) {
165            auto c
166            c=obase
167            obase=b
168            x
169            obase=c
170        }
171
172    This makes writing functions much easier.
173
174    (**Note**: the function **output(x,b)** exists in the extended math library.
175     See the **LIBRARY** section.)
176
177    However, since using this flag means that functions cannot set **ibase**,
178    **obase**, **scale**, or **seed** globally, functions that are made to do so
179    cannot work anymore. There are two possible use cases for that, and each has
180    a solution.
181
182    First, if a function is called on startup to turn bc(1) into a number
183    converter, it is possible to replace that capability with various shell
184    aliases. Examples:
185
186        alias d2o="bc -e ibase=A -e obase=8"
187        alias h2b="bc -e ibase=G -e obase=2"
188
189    Second, if the purpose of a function is to set **ibase**, **obase**,
190    **scale**, or **seed** globally for any other purpose, it could be split
191    into one to four functions (based on how many globals it sets) and each of
192    those functions could return the desired value for a global.
193
194    For functions that set **seed**, the value assigned to **seed** is not
195    propagated to parent functions. This means that the sequence of
196    pseudo-random numbers that they see will not be the same sequence of
197    pseudo-random numbers that any parent sees. This is only the case once
198    **seed** has been set.
199
200    If a function desires to not affect the sequence of pseudo-random numbers
201    of its parents, but wants to use the same **seed**, it can use the following
202    line:
203
204        seed = seed
205
206    If the behavior of this option is desired for every run of bc(1), then users
207    could make sure to define **BC_ENV_ARGS** and include this option (see the
208    **ENVIRONMENT VARIABLES** section for more details).
209
210    If **-s**, **-w**, or any equivalents are used, this option is ignored.
211
212    This is a **non-portable extension**.
213
214**-h**, **-\-help**
215
216:   Prints a usage message and exits.
217
218**-I** *ibase*, **-\-ibase**=*ibase*
219
220:   Sets the builtin variable **ibase** to the value *ibase* assuming that
221    *ibase* is in base 10. It is a fatal error if *ibase* is not a valid number.
222
223    If multiple instances of this option are given, the last is used.
224
225    This is a **non-portable extension**.
226
227**-i**, **-\-interactive**
228
229:   Forces interactive mode. (See the **INTERACTIVE MODE** section.)
230
231    This is a **non-portable extension**.
232
233**-L**, **-\-no-line-length**
234
235:   Disables line length checking and prints numbers without backslashes and
236    newlines. In other words, this option sets **BC_LINE_LENGTH** to **0** (see
237    the **ENVIRONMENT VARIABLES** section).
238
239    This is a **non-portable extension**.
240
241**-l**, **-\-mathlib**
242
243:   Sets **scale** (see the **SYNTAX** section) to **20** and loads the included
244    math library and the extended math library before running any code,
245    including any expressions or files specified on the command line.
246
247    To learn what is in the libraries, see the **LIBRARY** section.
248
249**-O** *obase*, **-\-obase**=*obase*
250
251:   Sets the builtin variable **obase** to the value *obase* assuming that
252    *obase* is in base 10. It is a fatal error if *obase* is not a valid number.
253
254    If multiple instances of this option are given, the last is used.
255
256    This is a **non-portable extension**.
257
258**-P**, **-\-no-prompt**
259
260:   Disables the prompt in TTY mode. (The prompt is only enabled in TTY mode.
261    See the **TTY MODE** section.) This is mostly for those users that do not
262    want a prompt or are not used to having them in bc(1). Most of those users
263    would want to put this option in **BC_ENV_ARGS** (see the
264    **ENVIRONMENT VARIABLES** section).
265
266    These options override the **BC_PROMPT** and **BC_TTY_MODE** environment
267    variables (see the **ENVIRONMENT VARIABLES** section).
268
269    This is a **non-portable extension**.
270
271**-q**, **-\-quiet**
272
273:   This option is for compatibility with the GNU bc(1)
274    (https://www.gnu.org/software/bc/); it is a no-op. Without this option, GNU
275    bc(1) prints a copyright header. This bc(1) only prints the copyright header
276    if one or more of the **-v**, **-V**, or **-\-version** options are given
277    unless the **BC_BANNER** environment variable is set and contains a non-zero
278    integer or if this bc(1) was built with the header displayed by default. If
279    *any* of that is the case, then this option *does* prevent bc(1) from
280    printing the header.
281
282    This is a **non-portable extension**.
283
284**-R**, **-\-no-read-prompt**
285
286:   Disables the read prompt in TTY mode. (The read prompt is only enabled in
287    TTY mode. See the **TTY MODE** section.) This is mostly for those users that
288    do not want a read prompt or are not used to having them in bc(1). Most of
289    those users would want to put this option in **BC_ENV_ARGS** (see the
290    **ENVIRONMENT VARIABLES** section). This option is also useful in hash bang
291    lines of bc(1) scripts that prompt for user input.
292
293    This option does not disable the regular prompt because the read prompt is
294    only used when the **read()** built-in function is called.
295
296    These options *do* override the **BC_PROMPT** and **BC_TTY_MODE**
297    environment variables (see the **ENVIRONMENT VARIABLES** section), but only
298    for the read prompt.
299
300    This is a **non-portable extension**.
301
302**-r** *keyword*, **-\-redefine**=*keyword*
303
304:   Redefines *keyword* in order to allow it to be used as a function, variable,
305    or array name. This is useful when this bc(1) gives parse errors when
306    parsing scripts meant for other bc(1) implementations.
307
308    The keywords this bc(1) allows to be redefined are:
309
310    * **abs**
311    * **asciify**
312    * **continue**
313    * **divmod**
314    * **else**
315    * **halt**
316    * **irand**
317    * **last**
318    * **limits**
319    * **maxibase**
320    * **maxobase**
321    * **maxrand**
322    * **maxscale**
323    * **modexp**
324    * **print**
325    * **rand**
326    * **read**
327    * **seed**
328	* **stream**
329
330    If any of those keywords are used as a function, variable, or array name in
331    a script, use this option with the keyword as the argument. If multiple are
332    used, use this option for all of them; it can be used multiple times.
333
334    Keywords are *not* redefined when parsing the builtin math library (see the
335    **LIBRARY** section).
336
337    It is a fatal error to redefine keywords mandated by the POSIX standard (see
338    the **STANDARDS** section). It is a fatal error to attempt to redefine words
339    that this bc(1) does not reserve as keywords.
340
341**-S** *scale*, **-\-scale**=*scale*
342
343:   Sets the builtin variable **scale** to the value *scale* assuming that
344    *scale* is in base 10. It is a fatal error if *scale* is not a valid number.
345
346    If multiple instances of this option are given, the last is used.
347
348    This is a **non-portable extension**.
349
350**-s**, **-\-standard**
351
352:   Process exactly the language defined by the standard (see the **STANDARDS**
353    section) and error if any extensions are used.
354
355    This is a **non-portable extension**.
356
357**-v**, **-V**, **-\-version**
358
359:   Print the version information (copyright header) and exits.
360
361    This is a **non-portable extension**.
362
363**-w**, **-\-warn**
364
365:   Like **-s** and **-\-standard**, except that warnings (and not errors) are
366    printed for non-standard extensions and execution continues normally.
367
368    This is a **non-portable extension**.
369
370**-z**, **-\-leading-zeroes**
371
372:   Makes bc(1) print all numbers greater than **-1** and less than **1**, and
373    not equal to **0**, with a leading zero.
374
375    This can be set for individual numbers with the **plz(x)**, **plznl(x)**,
376    **pnlz(x)**, and **pnlznl(x)** functions in the extended math library (see
377    the **LIBRARY** section).
378
379    This is a **non-portable extension**.
380
381All long options are **non-portable extensions**.
382
383# STDIN
384
385If no files or expressions are given by the **-f**, **-\-file**, **-e**, or
386**-\-expression** options, then bc(1) reads from **stdin**.
387
388However, there are a few caveats to this.
389
390First, **stdin** is evaluated a line at a time. The only exception to this is if
391the parse cannot complete. That means that starting a string without ending it
392or starting a function, **if** statement, or loop without ending it will also
393cause bc(1) to not execute.
394
395Second, after an **if** statement, bc(1) doesn't know if an **else** statement
396will follow, so it will not execute until it knows there will not be an **else**
397statement.
398
399# STDOUT
400
401Any non-error output is written to **stdout**. In addition, if history (see the
402**HISTORY** section) and the prompt (see the **TTY MODE** section) are enabled,
403both are output to **stdout**.
404
405**Note**: Unlike other bc(1) implementations, this bc(1) will issue a fatal
406error (see the **EXIT STATUS** section) if it cannot write to **stdout**, so if
407**stdout** is closed, as in **bc <file> >&-**, it will quit with an error. This
408is done so that bc(1) can report problems when **stdout** is redirected to a
409file.
410
411If there are scripts that depend on the behavior of other bc(1) implementations,
412it is recommended that those scripts be changed to redirect **stdout** to
413**/dev/null**.
414
415# STDERR
416
417Any error output is written to **stderr**.
418
419**Note**: Unlike other bc(1) implementations, this bc(1) will issue a fatal
420error (see the **EXIT STATUS** section) if it cannot write to **stderr**, so if
421**stderr** is closed, as in **bc <file> 2>&-**, it will quit with an error. This
422is done so that bc(1) can exit with an error code when **stderr** is redirected
423to a file.
424
425If there are scripts that depend on the behavior of other bc(1) implementations,
426it is recommended that those scripts be changed to redirect **stderr** to
427**/dev/null**.
428
429# SYNTAX
430
431The syntax for bc(1) programs is mostly C-like, with some differences. This
432bc(1) follows the POSIX standard (see the **STANDARDS** section), which is a
433much more thorough resource for the language this bc(1) accepts. This section is
434meant to be a summary and a listing of all the extensions to the standard.
435
436In the sections below, **E** means expression, **S** means statement, and **I**
437means identifier.
438
439Identifiers (**I**) start with a lowercase letter and can be followed by any
440number (up to **BC_NAME_MAX-1**) of lowercase letters (**a-z**), digits
441(**0-9**), and underscores (**\_**). The regex is **\[a-z\]\[a-z0-9\_\]\***.
442Identifiers with more than one character (letter) are a
443**non-portable extension**.
444
445**ibase** is a global variable determining how to interpret constant numbers. It
446is the "input" base, or the number base used for interpreting input numbers.
447**ibase** is initially **10**. If the **-s** (**-\-standard**) and **-w**
448(**-\-warn**) flags were not given on the command line, the max allowable value
449for **ibase** is **36**. Otherwise, it is **16**. The min allowable value for
450**ibase** is **2**. The max allowable value for **ibase** can be queried in
451bc(1) programs with the **maxibase()** built-in function.
452
453**obase** is a global variable determining how to output results. It is the
454"output" base, or the number base used for outputting numbers. **obase** is
455initially **10**. The max allowable value for **obase** is **BC_BASE_MAX** and
456can be queried in bc(1) programs with the **maxobase()** built-in function. The
457min allowable value for **obase** is **0**. If **obase** is **0**, values are
458output in scientific notation, and if **obase** is **1**, values are output in
459engineering notation. Otherwise, values are output in the specified base.
460
461Outputting in scientific and engineering notations are **non-portable
462extensions**.
463
464The *scale* of an expression is the number of digits in the result of the
465expression right of the decimal point, and **scale** is a global variable that
466sets the precision of any operations, with exceptions. **scale** is initially
467**0**. **scale** cannot be negative. The max allowable value for **scale** is
468**BC_SCALE_MAX** and can be queried in bc(1) programs with the **maxscale()**
469built-in function.
470
471bc(1) has both *global* variables and *local* variables. All *local*
472variables are local to the function; they are parameters or are introduced in
473the **auto** list of a function (see the **FUNCTIONS** section). If a variable
474is accessed which is not a parameter or in the **auto** list, it is assumed to
475be *global*. If a parent function has a *local* variable version of a variable
476that a child function considers *global*, the value of that *global* variable in
477the child function is the value of the variable in the parent function, not the
478value of the actual *global* variable.
479
480All of the above applies to arrays as well.
481
482The value of a statement that is an expression (i.e., any of the named
483expressions or operands) is printed unless the lowest precedence operator is an
484assignment operator *and* the expression is notsurrounded by parentheses.
485
486The value that is printed is also assigned to the special variable **last**. A
487single dot (**.**) may also be used as a synonym for **last**. These are
488**non-portable extensions**.
489
490Either semicolons or newlines may separate statements.
491
492## Comments
493
494There are two kinds of comments:
495
4961.	Block comments are enclosed in **/\*** and **\*/**.
4972.	Line comments go from **#** until, and not including, the next newline. This
498	is a **non-portable extension**.
499
500## Named Expressions
501
502The following are named expressions in bc(1):
503
5041.	Variables: **I**
5052.	Array Elements: **I[E]**
5063.	**ibase**
5074.	**obase**
5085.	**scale**
5096.	**seed**
5107.	**last** or a single dot (**.**)
511
512Numbers 6 and 7 are **non-portable extensions**.
513
514The meaning of **seed** is dependent on the current pseudo-random number
515generator but is guaranteed to not change except for new major versions.
516
517The *scale* and sign of the value may be significant.
518
519If a previously used **seed** value is assigned to **seed** and used again, the
520pseudo-random number generator is guaranteed to produce the same sequence of
521pseudo-random numbers as it did when the **seed** value was previously used.
522
523The exact value assigned to **seed** is not guaranteed to be returned if
524**seed** is queried again immediately. However, if **seed** *does* return a
525different value, both values, when assigned to **seed**, are guaranteed to
526produce the same sequence of pseudo-random numbers. This means that certain
527values assigned to **seed** will *not* produce unique sequences of pseudo-random
528numbers. The value of **seed** will change after any use of the **rand()** and
529**irand(E)** operands (see the *Operands* subsection below), except if the
530parameter passed to **irand(E)** is **0**, **1**, or negative.
531
532There is no limit to the length (number of significant decimal digits) or
533*scale* of the value that can be assigned to **seed**.
534
535Variables and arrays do not interfere; users can have arrays named the same as
536variables. This also applies to functions (see the **FUNCTIONS** section), so a
537user can have a variable, array, and function that all have the same name, and
538they will not shadow each other, whether inside of functions or not.
539
540Named expressions are required as the operand of **increment**/**decrement**
541operators  and as the left side of **assignment** operators (see the *Operators*
542subsection).
543
544## Operands
545
546The following are valid operands in bc(1):
547
5481.	Numbers (see the *Numbers* subsection below).
5492.	Array indices (**I[E]**).
5503.	**(E)**: The value of **E** (used to change precedence).
5514.	**sqrt(E)**: The square root of **E**. **E** must be non-negative.
5525.	**length(E)**: The number of significant decimal digits in **E**. Returns
553	**1** for **0** with no decimal places. If given a string, the length of the
554	string is returned. Passing a string to **length(E)** is a **non-portable
555	extension**.
5566.	**length(I[])**: The number of elements in the array **I**. This is a
557	**non-portable extension**.
5587.	**scale(E)**: The *scale* of **E**.
5598.	**abs(E)**: The absolute value of **E**. This is a **non-portable
560	extension**.
5619.	**is_number(E)**: **1** if the given argument is a number, **0** if it is a
562	string. This is a **non-portable extension**.
56310.	**is_string(E)**: **1** if the given argument is a string, **0** if it is a
564	number. This is a **non-portable extension**.
56511.	**modexp(E, E, E)**: Modular exponentiation, where the first expression is
566	the base, the second is the exponent, and the third is the modulus. All
567	three values must be integers. The second argument must be non-negative. The
568	third argument must be non-zero. This is a **non-portable extension**.
56911.	**divmod(E, E, I[])**: Division and modulus in one operation. This is for
570	optimization. The first expression is the dividend, and the second is the
571	divisor, which must be non-zero. The return value is the quotient, and the
572	modulus is stored in index **0** of the provided array (the last argument).
573	This is a **non-portable extension**.
57412.	**asciify(E)**: If **E** is a string, returns a string that is the first
575	letter of its argument. If it is a number, calculates the number mod **256**
576	and returns that number as a one-character string. This is a **non-portable
577	extension**.
57813.	**asciify(I[])**: A string that is made up of the characters that would
579	result from running **asciify(E)** on each element of the array identified
580	by the argument. This allows creating multi-character strings and storing
581	them. This is a **non-portable extension**.
58214.	**I()**, **I(E)**, **I(E, E)**, and so on, where **I** is an identifier for
583	a non-**void** function (see the *Void Functions* subsection of the
584	**FUNCTIONS** section). The **E** argument(s) may also be arrays of the form
585	**I[]**, which will automatically be turned into array references (see the
586	*Array References* subsection of the **FUNCTIONS** section) if the
587	corresponding parameter in the function definition is an array reference.
58815.	**read()**: Reads a line from **stdin** and uses that as an expression. The
589	result of that expression is the result of the **read()** operand. This is a
590	**non-portable extension**.
59116.	**maxibase()**: The max allowable **ibase**. This is a **non-portable
592	extension**.
59317.	**maxobase()**: The max allowable **obase**. This is a **non-portable
594	extension**.
59518.	**maxscale()**: The max allowable **scale**. This is a **non-portable
596	extension**.
59719.	**line_length()**: The line length set with **BC_LINE_LENGTH** (see the
598	**ENVIRONMENT VARIABLES** section). This is a **non-portable extension**.
59920.	**global_stacks()**: **0** if global stacks are not enabled with the **-g**
600	or **-\-global-stacks** options, non-zero otherwise. See the **OPTIONS**
601	section. This is a **non-portable extension**.
60221.	**leading_zero()**: **0** if leading zeroes are not enabled with the **-z**
603	or **--leading-zeroes** options, non-zero otherwise. See the **OPTIONS**
604	section. This is a **non-portable extension**.
60522.	**rand()**: A pseudo-random integer between **0** (inclusive) and
606	**BC_RAND_MAX** (inclusive). Using this operand will change the value of
607	**seed**. This is a **non-portable extension**.
60823.	**irand(E)**: A pseudo-random integer between **0** (inclusive) and the
609	value of **E** (exclusive). If **E** is negative or is a non-integer
610	(**E**'s *scale* is not **0**), an error is raised, and bc(1) resets (see
611	the **RESET** section) while **seed** remains unchanged. If **E** is larger
612	than **BC_RAND_MAX**, the higher bound is honored by generating several
613	pseudo-random integers, multiplying them by appropriate powers of
614	**BC_RAND_MAX+1**, and adding them together. Thus, the size of integer that
615	can be generated with this operand is unbounded. Using this operand will
616	change the value of **seed**, unless the value of **E** is **0** or **1**.
617	In that case, **0** is returned, and **seed** is *not* changed. This is a
618	**non-portable extension**.
61924.	**maxrand()**: The max integer returned by **rand()**. This is a
620	**non-portable extension**.
621
622The integers generated by **rand()** and **irand(E)** are guaranteed to be as
623unbiased as possible, subject to the limitations of the pseudo-random number
624generator.
625
626**Note**: The values returned by the pseudo-random number generator with
627**rand()** and **irand(E)** are guaranteed to *NOT* be cryptographically secure.
628This is a consequence of using a seeded pseudo-random number generator. However,
629they *are* guaranteed to be reproducible with identical **seed** values. This
630means that the pseudo-random values from bc(1) should only be used where a
631reproducible stream of pseudo-random numbers is *ESSENTIAL*. In any other case,
632use a non-seeded pseudo-random number generator.
633
634## Numbers
635
636Numbers are strings made up of digits, uppercase letters, and at most **1**
637period for a radix. Numbers can have up to **BC_NUM_MAX** digits. Uppercase
638letters are equal to **9** plus their position in the alphabet, starting from
639**1** (i.e., **A** equals **10**, or **9+1**).
640
641If a digit or letter makes no sense with the current value of **ibase** (i.e.,
642they are greater than or equal to the current value of **ibase**), then the
643behavior depends on the existence of the **-c**/**-\-digit-clamp** or
644**-C**/**-\-no-digit-clamp** options (see the **OPTIONS** section), the
645existence and setting of the **BC_DIGIT_CLAMP** environment variable (see the
646**ENVIRONMENT VARIABLES** section), or the default, which can be queried with
647the **-h**/**-\-help** option.
648
649If clamping is off, then digits or letters that are greater than or equal to the
650current value of **ibase** are not changed. Instead, their given value is
651multiplied by the appropriate power of **ibase** and added into the number. This
652means that, with an **ibase** of **3**, the number **AB** is equal to
653**3\^1\*A+3\^0\*B**, which is **3** times **10** plus **11**, or **41**.
654
655If clamping is on, then digits or letters that are greater than or equal to the
656current value of **ibase** are set to the value of the highest valid digit in
657**ibase** before being multiplied by the appropriate power of **ibase** and
658added into the number. This means that, with an **ibase** of **3**, the number
659**AB** is equal to **3\^1\*2+3\^0\*2**, which is **3** times **2** plus **2**,
660or **8**.
661
662There is one exception to clamping: single-character numbers (i.e., **A**
663alone). Such numbers are never clamped and always take the value they would have
664in the highest possible **ibase**. This means that **A** alone always equals
665decimal **10** and **Z** alone always equals decimal **35**. This behavior is
666mandated by the standard (see the STANDARDS section) and is meant to provide an
667easy way to set the current **ibase** (with the **i** command) regardless of the
668current value of **ibase**.
669
670If clamping is on, and the clamped value of a character is needed, use a leading
671zero, i.e., for **A**, use **0A**.
672
673In addition, bc(1) accepts numbers in scientific notation. These have the form
674**\<number\>e\<integer\>**. The exponent (the portion after the **e**) must be
675an integer. An example is **1.89237e9**, which is equal to **1892370000**.
676Negative exponents are also allowed, so **4.2890e-3** is equal to **0.0042890**.
677
678Using scientific notation is an error or warning if the **-s** or **-w**,
679respectively, command-line options (or equivalents) are given.
680
681**WARNING**: Both the number and the exponent in scientific notation are
682interpreted according to the current **ibase**, but the number is still
683multiplied by **10\^exponent** regardless of the current **ibase**. For example,
684if **ibase** is **16** and bc(1) is given the number string **FFeA**, the
685resulting decimal number will be **2550000000000**, and if bc(1) is given the
686number string **10e-4**, the resulting decimal number will be **0.0016**.
687
688Accepting input as scientific notation is a **non-portable extension**.
689
690## Operators
691
692The following arithmetic and logical operators can be used. They are listed in
693order of decreasing precedence. Operators in the same group have the same
694precedence.
695
696**++** **-\-**
697
698:   Type: Prefix and Postfix
699
700    Associativity: None
701
702    Description: **increment**, **decrement**
703
704**-** **!**
705
706:   Type: Prefix
707
708    Associativity: None
709
710    Description: **negation**, **boolean not**
711
712**\$**
713
714:   Type: Postfix
715
716    Associativity: None
717
718    Description: **truncation**
719
720**\@**
721
722:   Type: Binary
723
724    Associativity: Right
725
726    Description: **set precision**
727
728**\^**
729
730:   Type: Binary
731
732    Associativity: Right
733
734    Description: **power**
735
736**\*** **/** **%**
737
738:   Type: Binary
739
740    Associativity: Left
741
742    Description: **multiply**, **divide**, **modulus**
743
744**+** **-**
745
746:   Type: Binary
747
748    Associativity: Left
749
750    Description: **add**, **subtract**
751
752**\<\<** **\>\>**
753
754:   Type: Binary
755
756    Associativity: Left
757
758    Description: **shift left**, **shift right**
759
760**=** **\<\<=** **\>\>=** **+=** **-=** **\*=** **/=** **%=** **\^=** **\@=**
761
762:   Type: Binary
763
764    Associativity: Right
765
766    Description: **assignment**
767
768**==** **\<=** **\>=** **!=** **\<** **\>**
769
770:   Type: Binary
771
772    Associativity: Left
773
774    Description: **relational**
775
776**&&**
777
778:   Type: Binary
779
780    Associativity: Left
781
782    Description: **boolean and**
783
784**||**
785
786:   Type: Binary
787
788    Associativity: Left
789
790    Description: **boolean or**
791
792The operators will be described in more detail below.
793
794**++** **-\-**
795
796:   The prefix and postfix **increment** and **decrement** operators behave
797    exactly like they would in C. They require a named expression (see the
798    *Named Expressions* subsection) as an operand.
799
800    The prefix versions of these operators are more efficient; use them where
801    possible.
802
803**-**
804
805:   The **negation** operator returns **0** if a user attempts to negate any
806    expression with the value **0**. Otherwise, a copy of the expression with
807    its sign flipped is returned.
808
809**!**
810
811:   The **boolean not** operator returns **1** if the expression is **0**, or
812    **0** otherwise.
813
814    This is a **non-portable extension**.
815
816**\$**
817
818:   The **truncation** operator returns a copy of the given expression with all
819    of its *scale* removed.
820
821    This is a **non-portable extension**.
822
823**\@**
824
825:   The **set precision** operator takes two expressions and returns a copy of
826    the first with its *scale* equal to the value of the second expression. That
827    could either mean that the number is returned without change (if the
828    *scale* of the first expression matches the value of the second
829    expression), extended (if it is less), or truncated (if it is more).
830
831    The second expression must be an integer (no *scale*) and non-negative.
832
833    This is a **non-portable extension**.
834
835**\^**
836
837:   The **power** operator (not the **exclusive or** operator, as it would be in
838    C) takes two expressions and raises the first to the power of the value of
839    the second. The *scale* of the result is equal to **scale**.
840
841    The second expression must be an integer (no *scale*), and if it is
842    negative, the first value must be non-zero.
843
844**\***
845
846:   The **multiply** operator takes two expressions, multiplies them, and
847    returns the product. If **a** is the *scale* of the first expression and
848    **b** is the *scale* of the second expression, the *scale* of the result is
849    equal to **min(a+b,max(scale,a,b))** where **min()** and **max()** return
850    the obvious values.
851
852**/**
853
854:   The **divide** operator takes two expressions, divides them, and returns the
855    quotient. The *scale* of the result shall be the value of **scale**.
856
857    The second expression must be non-zero.
858
859**%**
860
861:   The **modulus** operator takes two expressions, **a** and **b**, and
862    evaluates them by 1) Computing **a/b** to current **scale** and 2) Using the
863    result of step 1 to calculate **a-(a/b)\*b** to *scale*
864    **max(scale+scale(b),scale(a))**.
865
866    The second expression must be non-zero.
867
868**+**
869
870:   The **add** operator takes two expressions, **a** and **b**, and returns the
871    sum, with a *scale* equal to the max of the *scale*s of **a** and **b**.
872
873**-**
874
875:   The **subtract** operator takes two expressions, **a** and **b**, and
876    returns the difference, with a *scale* equal to the max of the *scale*s of
877    **a** and **b**.
878
879**\<\<**
880
881:   The **left shift** operator takes two expressions, **a** and **b**, and
882    returns a copy of the value of **a** with its decimal point moved **b**
883    places to the right.
884
885    The second expression must be an integer (no *scale*) and non-negative.
886
887    This is a **non-portable extension**.
888
889**\>\>**
890
891:   The **right shift** operator takes two expressions, **a** and **b**, and
892    returns a copy of the value of **a** with its decimal point moved **b**
893    places to the left.
894
895    The second expression must be an integer (no *scale*) and non-negative.
896
897    This is a **non-portable extension**.
898
899**=** **\<\<=** **\>\>=** **+=** **-=** **\*=** **/=** **%=** **\^=** **\@=**
900
901:   The **assignment** operators take two expressions, **a** and **b** where
902    **a** is a named expression (see the *Named Expressions* subsection).
903
904    For **=**, **b** is copied and the result is assigned to **a**. For all
905    others, **a** and **b** are applied as operands to the corresponding
906    arithmetic operator and the result is assigned to **a**.
907
908    The **assignment** operators that correspond to operators that are
909    extensions are themselves **non-portable extensions**.
910
911**==** **\<=** **\>=** **!=** **\<** **\>**
912
913:   The **relational** operators compare two expressions, **a** and **b**, and
914    if the relation holds, according to C language semantics, the result is
915    **1**. Otherwise, it is **0**.
916
917    Note that unlike in C, these operators have a lower precedence than the
918    **assignment** operators, which means that **a=b\>c** is interpreted as
919    **(a=b)\>c**.
920
921    Also, unlike the standard (see the **STANDARDS** section) requires, these
922    operators can appear anywhere any other expressions can be used. This
923    allowance is a **non-portable extension**.
924
925**&&**
926
927:   The **boolean and** operator takes two expressions and returns **1** if both
928    expressions are non-zero, **0** otherwise.
929
930    This is *not* a short-circuit operator.
931
932    This is a **non-portable extension**.
933
934**||**
935
936:   The **boolean or** operator takes two expressions and returns **1** if one
937    of the expressions is non-zero, **0** otherwise.
938
939    This is *not* a short-circuit operator.
940
941    This is a **non-portable extension**.
942
943## Statements
944
945The following items are statements:
946
9471.	**E**
9482.	**{** **S** **;** ... **;** **S** **}**
9493.	**if** **(** **E** **)** **S**
9504.	**if** **(** **E** **)** **S** **else** **S**
9515.	**while** **(** **E** **)** **S**
9526.	**for** **(** **E** **;** **E** **;** **E** **)** **S**
9537.	An empty statement
9548.	**break**
9559.	**continue**
95610.	**quit**
95711.	**halt**
95812.	**limits**
95913.	A string of characters, enclosed in double quotes
96014.	**print** **E** **,** ... **,** **E**
96115.	**stream** **E** **,** ... **,** **E**
96216.	**I()**, **I(E)**, **I(E, E)**, and so on, where **I** is an identifier for
963	a **void** function (see the *Void Functions* subsection of the
964	**FUNCTIONS** section). The **E** argument(s) may also be arrays of the form
965	**I[]**, which will automatically be turned into array references (see the
966	*Array References* subsection of the **FUNCTIONS** section) if the
967	corresponding parameter in the function definition is an array reference.
968
969Numbers 4, 9, 11, 12, 14, 15, and 16 are **non-portable extensions**.
970
971Also, as a **non-portable extension**, any or all of the expressions in the
972header of a for loop may be omitted. If the condition (second expression) is
973omitted, it is assumed to be a constant **1**.
974
975The **break** statement causes a loop to stop iterating and resume execution
976immediately following a loop. This is only allowed in loops.
977
978The **continue** statement causes a loop iteration to stop early and returns to
979the start of the loop, including testing the loop condition. This is only
980allowed in loops.
981
982The **if** **else** statement does the same thing as in C.
983
984The **quit** statement causes bc(1) to quit, even if it is on a branch that will
985not be executed (it is a compile-time command).
986
987**Warning**: The behavior of this bc(1) on **quit** is slightly different from
988other bc(1) implementations. Other bc(1) implementations will exit as soon as
989they finish parsing the line that a **quit** command is on. This bc(1) will
990execute any completed and executable statements that occur before the **quit**
991statement before exiting.
992
993In other words, for the bc(1) code below:
994
995    for (i = 0; i < 3; ++i) i; quit
996
997Other bc(1) implementations will print nothing, and this bc(1) will print **0**,
998**1**, and **2** on successive lines before exiting.
999
1000The **halt** statement causes bc(1) to quit, if it is executed. (Unlike **quit**
1001if it is on a branch of an **if** statement that is not executed, bc(1) does not
1002quit.)
1003
1004The **limits** statement prints the limits that this bc(1) is subject to. This
1005is like the **quit** statement in that it is a compile-time command.
1006
1007An expression by itself is evaluated and printed, followed by a newline.
1008
1009Both scientific notation and engineering notation are available for printing the
1010results of expressions. Scientific notation is activated by assigning **0** to
1011**obase**, and engineering notation is activated by assigning **1** to
1012**obase**. To deactivate them, just assign a different value to **obase**.
1013
1014Scientific notation and engineering notation are disabled if bc(1) is run with
1015either the **-s** or **-w** command-line options (or equivalents).
1016
1017Printing numbers in scientific notation and/or engineering notation is a
1018**non-portable extension**.
1019
1020## Strings
1021
1022If strings appear as a statement by themselves, they are printed without a
1023trailing newline.
1024
1025In addition to appearing as a lone statement by themselves, strings can be
1026assigned to variables and array elements. They can also be passed to functions
1027in variable parameters.
1028
1029If any statement that expects a string is given a variable that had a string
1030assigned to it, the statement acts as though it had received a string.
1031
1032If any math operation is attempted on a string or a variable or array element
1033that has been assigned a string, an error is raised, and bc(1) resets (see the
1034**RESET** section).
1035
1036Assigning strings to variables and array elements and passing them to functions
1037are **non-portable extensions**.
1038
1039## Print Statement
1040
1041The "expressions" in a **print** statement may also be strings. If they are, there
1042are backslash escape sequences that are interpreted specially. What those
1043sequences are, and what they cause to be printed, are shown below:
1044
1045**\\a**:   **\\a**
1046
1047**\\b**:   **\\b**
1048
1049**\\\\**:   **\\**
1050
1051**\\e**:   **\\**
1052
1053**\\f**:   **\\f**
1054
1055**\\n**:   **\\n**
1056
1057**\\q**:   **"**
1058
1059**\\r**:   **\\r**
1060
1061**\\t**:   **\\t**
1062
1063Any other character following a backslash causes the backslash and character to
1064be printed as-is.
1065
1066Any non-string expression in a print statement shall be assigned to **last**,
1067like any other expression that is printed.
1068
1069## Stream Statement
1070
1071The "expressions in a **stream** statement may also be strings.
1072
1073If a **stream** statement is given a string, it prints the string as though the
1074string had appeared as its own statement. In other words, the **stream**
1075statement prints strings normally, without a newline.
1076
1077If a **stream** statement is given a number, a copy of it is truncated and its
1078absolute value is calculated. The result is then printed as though **obase** is
1079**256** and each digit is interpreted as an 8-bit ASCII character, making it a
1080byte stream.
1081
1082## Order of Evaluation
1083
1084All expressions in a statment are evaluated left to right, except as necessary
1085to maintain order of operations. This means, for example, assuming that **i** is
1086equal to **0**, in the expression
1087
1088    a[i++] = i++
1089
1090the first (or 0th) element of **a** is set to **1**, and **i** is equal to **2**
1091at the end of the expression.
1092
1093This includes function arguments. Thus, assuming **i** is equal to **0**, this
1094means that in the expression
1095
1096    x(i++, i++)
1097
1098the first argument passed to **x()** is **0**, and the second argument is **1**,
1099while **i** is equal to **2** before the function starts executing.
1100
1101# FUNCTIONS
1102
1103Function definitions are as follows:
1104
1105```
1106define I(I,...,I){
1107	auto I,...,I
1108	S;...;S
1109	return(E)
1110}
1111```
1112
1113Any **I** in the parameter list or **auto** list may be replaced with **I[]** to
1114make a parameter or **auto** var an array, and any **I** in the parameter list
1115may be replaced with **\*I[]** to make a parameter an array reference. Callers
1116of functions that take array references should not put an asterisk in the call;
1117they must be called with just **I[]** like normal array parameters and will be
1118automatically converted into references.
1119
1120As a **non-portable extension**, the opening brace of a **define** statement may
1121appear on the next line.
1122
1123As a **non-portable extension**, the return statement may also be in one of the
1124following forms:
1125
11261.	**return**
11272.	**return** **(** **)**
11283.	**return** **E**
1129
1130The first two, or not specifying a **return** statement, is equivalent to
1131**return (0)**, unless the function is a **void** function (see the *Void
1132Functions* subsection below).
1133
1134## Void Functions
1135
1136Functions can also be **void** functions, defined as follows:
1137
1138```
1139define void I(I,...,I){
1140	auto I,...,I
1141	S;...;S
1142	return
1143}
1144```
1145
1146They can only be used as standalone expressions, where such an expression would
1147be printed alone, except in a print statement.
1148
1149Void functions can only use the first two **return** statements listed above.
1150They can also omit the return statement entirely.
1151
1152The word "void" is not treated as a keyword; it is still possible to have
1153variables, arrays, and functions named **void**. The word "void" is only
1154treated specially right after the **define** keyword.
1155
1156This is a **non-portable extension**.
1157
1158## Array References
1159
1160For any array in the parameter list, if the array is declared in the form
1161
1162```
1163*I[]
1164```
1165
1166it is a **reference**. Any changes to the array in the function are reflected,
1167when the function returns, to the array that was passed in.
1168
1169Other than this, all function arguments are passed by value.
1170
1171This is a **non-portable extension**.
1172
1173# LIBRARY
1174
1175All of the functions below, including the functions in the extended math
1176library (see the *Extended Library* subsection below), are available when the
1177**-l** or **-\-mathlib** command-line flags are given, except that the extended
1178math library is not available when the **-s** option, the **-w** option, or
1179equivalents are given.
1180
1181## Standard Library
1182
1183The standard (see the **STANDARDS** section) defines the following functions for
1184the math library:
1185
1186**s(x)**
1187
1188:   Returns the sine of **x**, which is assumed to be in radians.
1189
1190    This is a transcendental function (see the *Transcendental Functions*
1191    subsection below).
1192
1193**c(x)**
1194
1195:   Returns the cosine of **x**, which is assumed to be in radians.
1196
1197    This is a transcendental function (see the *Transcendental Functions*
1198    subsection below).
1199
1200**a(x)**
1201
1202:   Returns the arctangent of **x**, in radians.
1203
1204    This is a transcendental function (see the *Transcendental Functions*
1205    subsection below).
1206
1207**l(x)**
1208
1209:   Returns the natural logarithm of **x**.
1210
1211    This is a transcendental function (see the *Transcendental Functions*
1212    subsection below).
1213
1214**e(x)**
1215
1216:   Returns the mathematical constant **e** raised to the power of **x**.
1217
1218    This is a transcendental function (see the *Transcendental Functions*
1219    subsection below).
1220
1221**j(x, n)**
1222
1223:   Returns the bessel integer order **n** (truncated) of **x**.
1224
1225    This is a transcendental function (see the *Transcendental Functions*
1226    subsection below).
1227
1228## Extended Library
1229
1230The extended library is *not* loaded when the **-s**/**-\-standard** or
1231**-w**/**-\-warn** options are given since they are not part of the library
1232defined by the standard (see the **STANDARDS** section).
1233
1234The extended library is a **non-portable extension**.
1235
1236**p(x, y)**
1237
1238:   Calculates **x** to the power of **y**, even if **y** is not an integer, and
1239    returns the result to the current **scale**.
1240
1241    It is an error if **y** is negative and **x** is **0**.
1242
1243    This is a transcendental function (see the *Transcendental Functions*
1244    subsection below).
1245
1246**r(x, p)**
1247
1248:   Returns **x** rounded to **p** decimal places according to the rounding mode
1249    round half away from **0**
1250    (https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero).
1251
1252**ceil(x, p)**
1253
1254:   Returns **x** rounded to **p** decimal places according to the rounding mode
1255    round away from **0**
1256    (https://en.wikipedia.org/wiki/Rounding#Rounding_away_from_zero).
1257
1258**f(x)**
1259
1260:   Returns the factorial of the truncated absolute value of **x**.
1261
1262**perm(n, k)**
1263
1264:   Returns the permutation of the truncated absolute value of **n** of the
1265    truncated absolute value of **k**, if **k \<= n**. If not, it returns **0**.
1266
1267**comb(n, k)**
1268
1269:   Returns the combination of the truncated absolute value of **n** of the
1270    truncated absolute value of **k**, if **k \<= n**. If not, it returns **0**.
1271
1272**l2(x)**
1273
1274:   Returns the logarithm base **2** of **x**.
1275
1276    This is a transcendental function (see the *Transcendental Functions*
1277    subsection below).
1278
1279**l10(x)**
1280
1281:   Returns the logarithm base **10** of **x**.
1282
1283    This is a transcendental function (see the *Transcendental Functions*
1284    subsection below).
1285
1286**log(x, b)**
1287
1288:   Returns the logarithm base **b** of **x**.
1289
1290    This is a transcendental function (see the *Transcendental Functions*
1291    subsection below).
1292
1293**cbrt(x)**
1294
1295:   Returns the cube root of **x**.
1296
1297**root(x, n)**
1298
1299:   Calculates the truncated value of **n**, **r**, and returns the **r**th root
1300    of **x** to the current **scale**.
1301
1302    If **r** is **0** or negative, this raises an error and causes bc(1) to
1303    reset (see the **RESET** section). It also raises an error and causes bc(1)
1304    to reset if **r** is even and **x** is negative.
1305
1306**gcd(a, b)**
1307
1308:   Returns the greatest common divisor (factor) of the truncated absolute value
1309    of **a** and the truncated absolute value of **b**.
1310
1311**lcm(a, b)**
1312
1313:   Returns the least common multiple of the truncated absolute value of **a**
1314    and the truncated absolute value of **b**.
1315
1316**pi(p)**
1317
1318:   Returns **pi** to **p** decimal places.
1319
1320    This is a transcendental function (see the *Transcendental Functions*
1321    subsection below).
1322
1323**t(x)**
1324
1325:   Returns the tangent of **x**, which is assumed to be in radians.
1326
1327    This is a transcendental function (see the *Transcendental Functions*
1328    subsection below).
1329
1330**a2(y, x)**
1331
1332:   Returns the arctangent of **y/x**, in radians. If both **y** and **x** are
1333    equal to **0**, it raises an error and causes bc(1) to reset (see the
1334    **RESET** section). Otherwise, if **x** is greater than **0**, it returns
1335    **a(y/x)**. If **x** is less than **0**, and **y** is greater than or equal
1336    to **0**, it returns **a(y/x)+pi**. If **x** is less than **0**, and **y**
1337    is less than **0**, it returns **a(y/x)-pi**. If **x** is equal to **0**,
1338    and **y** is greater than **0**, it returns **pi/2**. If **x** is equal to
1339    **0**, and **y** is less than **0**, it returns **-pi/2**.
1340
1341    This function is the same as the **atan2()** function in many programming
1342    languages.
1343
1344    This is a transcendental function (see the *Transcendental Functions*
1345    subsection below).
1346
1347**sin(x)**
1348
1349:   Returns the sine of **x**, which is assumed to be in radians.
1350
1351    This is an alias of **s(x)**.
1352
1353    This is a transcendental function (see the *Transcendental Functions*
1354    subsection below).
1355
1356**cos(x)**
1357
1358:   Returns the cosine of **x**, which is assumed to be in radians.
1359
1360    This is an alias of **c(x)**.
1361
1362    This is a transcendental function (see the *Transcendental Functions*
1363    subsection below).
1364
1365**tan(x)**
1366
1367:   Returns the tangent of **x**, which is assumed to be in radians.
1368
1369    If **x** is equal to **1** or **-1**, this raises an error and causes bc(1)
1370    to reset (see the **RESET** section).
1371
1372    This is an alias of **t(x)**.
1373
1374    This is a transcendental function (see the *Transcendental Functions*
1375    subsection below).
1376
1377**atan(x)**
1378
1379:   Returns the arctangent of **x**, in radians.
1380
1381    This is an alias of **a(x)**.
1382
1383    This is a transcendental function (see the *Transcendental Functions*
1384    subsection below).
1385
1386**atan2(y, x)**
1387
1388:   Returns the arctangent of **y/x**, in radians. If both **y** and **x** are
1389    equal to **0**, it raises an error and causes bc(1) to reset (see the
1390    **RESET** section). Otherwise, if **x** is greater than **0**, it returns
1391    **a(y/x)**. If **x** is less than **0**, and **y** is greater than or equal
1392    to **0**, it returns **a(y/x)+pi**. If **x** is less than **0**, and **y**
1393    is less than **0**, it returns **a(y/x)-pi**. If **x** is equal to **0**,
1394    and **y** is greater than **0**, it returns **pi/2**. If **x** is equal to
1395    **0**, and **y** is less than **0**, it returns **-pi/2**.
1396
1397    This function is the same as the **atan2()** function in many programming
1398    languages.
1399
1400    This is an alias of **a2(y, x)**.
1401
1402    This is a transcendental function (see the *Transcendental Functions*
1403    subsection below).
1404
1405**r2d(x)**
1406
1407:   Converts **x** from radians to degrees and returns the result.
1408
1409    This is a transcendental function (see the *Transcendental Functions*
1410    subsection below).
1411
1412**d2r(x)**
1413
1414:   Converts **x** from degrees to radians and returns the result.
1415
1416    This is a transcendental function (see the *Transcendental Functions*
1417    subsection below).
1418
1419**frand(p)**
1420
1421:   Generates a pseudo-random number between **0** (inclusive) and **1**
1422    (exclusive) with the number of decimal digits after the decimal point equal
1423    to the truncated absolute value of **p**. If **p** is not **0**, then
1424    calling this function will change the value of **seed**. If **p** is **0**,
1425    then **0** is returned, and **seed** is *not* changed.
1426
1427**ifrand(i, p)**
1428
1429:   Generates a pseudo-random number that is between **0** (inclusive) and the
1430    truncated absolute value of **i** (exclusive) with the number of decimal
1431    digits after the decimal point equal to the truncated absolute value of
1432    **p**. If the absolute value of **i** is greater than or equal to **2**, and
1433    **p** is not **0**, then calling this function will change the value of
1434    **seed**; otherwise, **0** is returned and **seed** is not changed.
1435
1436**srand(x)**
1437
1438:   Returns **x** with its sign flipped with probability **0.5**. In other
1439    words, it randomizes the sign of **x**.
1440
1441**brand()**
1442
1443:   Returns a random boolean value (either **0** or **1**).
1444
1445**band(a, b)**
1446
1447:   Takes the truncated absolute value of both **a** and **b** and calculates
1448    and returns the result of the bitwise **and** operation between them.
1449
1450    If you want to use signed two's complement arguments, use **s2u(x)** to
1451    convert.
1452
1453**bor(a, b)**
1454
1455:   Takes the truncated absolute value of both **a** and **b** and calculates
1456    and returns the result of the bitwise **or** operation between them.
1457
1458    If you want to use signed two's complement arguments, use **s2u(x)** to
1459    convert.
1460
1461**bxor(a, b)**
1462
1463:   Takes the truncated absolute value of both **a** and **b** and calculates
1464    and returns the result of the bitwise **xor** operation between them.
1465
1466    If you want to use signed two's complement arguments, use **s2u(x)** to
1467    convert.
1468
1469**bshl(a, b)**
1470
1471:   Takes the truncated absolute value of both **a** and **b** and calculates
1472    and returns the result of **a** bit-shifted left by **b** places.
1473
1474    If you want to use signed two's complement arguments, use **s2u(x)** to
1475    convert.
1476
1477**bshr(a, b)**
1478
1479:   Takes the truncated absolute value of both **a** and **b** and calculates
1480    and returns the truncated result of **a** bit-shifted right by **b** places.
1481
1482    If you want to use signed two's complement arguments, use **s2u(x)** to
1483    convert.
1484
1485**bnotn(x, n)**
1486
1487:   Takes the truncated absolute value of **x** and does a bitwise not as though
1488    it has the same number of bytes as the truncated absolute value of **n**.
1489
1490    If you want to a use signed two's complement argument, use **s2u(x)** to
1491    convert.
1492
1493**bnot8(x)**
1494
1495:   Does a bitwise not of the truncated absolute value of **x** as though it has
1496    **8** binary digits (1 unsigned byte).
1497
1498    If you want to a use signed two's complement argument, use **s2u(x)** to
1499    convert.
1500
1501**bnot16(x)**
1502
1503:   Does a bitwise not of the truncated absolute value of **x** as though it has
1504    **16** binary digits (2 unsigned bytes).
1505
1506    If you want to a use signed two's complement argument, use **s2u(x)** to
1507    convert.
1508
1509**bnot32(x)**
1510
1511:   Does a bitwise not of the truncated absolute value of **x** as though it has
1512    **32** binary digits (4 unsigned bytes).
1513
1514    If you want to a use signed two's complement argument, use **s2u(x)** to
1515    convert.
1516
1517**bnot64(x)**
1518
1519:   Does a bitwise not of the truncated absolute value of **x** as though it has
1520    **64** binary digits (8 unsigned bytes).
1521
1522    If you want to a use signed two's complement argument, use **s2u(x)** to
1523    convert.
1524
1525**bnot(x)**
1526
1527:   Does a bitwise not of the truncated absolute value of **x** as though it has
1528    the minimum number of power of two unsigned bytes.
1529
1530    If you want to a use signed two's complement argument, use **s2u(x)** to
1531    convert.
1532
1533**brevn(x, n)**
1534
1535:   Runs a bit reversal on the truncated absolute value of **x** as though it
1536    has the same number of 8-bit bytes as the truncated absolute value of **n**.
1537
1538    If you want to a use signed two's complement argument, use **s2u(x)** to
1539    convert.
1540
1541**brev8(x)**
1542
1543:   Runs a bit reversal on the truncated absolute value of **x** as though it
1544    has 8 binary digits (1 unsigned byte).
1545
1546    If you want to a use signed two's complement argument, use **s2u(x)** to
1547    convert.
1548
1549**brev16(x)**
1550
1551:   Runs a bit reversal on the truncated absolute value of **x** as though it
1552    has 16 binary digits (2 unsigned bytes).
1553
1554    If you want to a use signed two's complement argument, use **s2u(x)** to
1555    convert.
1556
1557**brev32(x)**
1558
1559:   Runs a bit reversal on the truncated absolute value of **x** as though it
1560    has 32 binary digits (4 unsigned bytes).
1561
1562    If you want to a use signed two's complement argument, use **s2u(x)** to
1563    convert.
1564
1565**brev64(x)**
1566
1567:   Runs a bit reversal on the truncated absolute value of **x** as though it
1568    has 64 binary digits (8 unsigned bytes).
1569
1570    If you want to a use signed two's complement argument, use **s2u(x)** to
1571    convert.
1572
1573**brev(x)**
1574
1575:   Runs a bit reversal on the truncated absolute value of **x** as though it
1576    has the minimum number of power of two unsigned bytes.
1577
1578    If you want to a use signed two's complement argument, use **s2u(x)** to
1579    convert.
1580
1581**broln(x, p, n)**
1582
1583:   Does a left bitwise rotatation of the truncated absolute value of **x**, as
1584    though it has the same number of unsigned 8-bit bytes as the truncated
1585    absolute value of **n**, by the number of places equal to the truncated
1586    absolute value of **p** modded by the **2** to the power of the number of
1587    binary digits in **n** 8-bit bytes.
1588
1589    If you want to a use signed two's complement argument, use **s2u(x)** to
1590    convert.
1591
1592**brol8(x, p)**
1593
1594:   Does a left bitwise rotatation of the truncated absolute value of **x**, as
1595    though it has **8** binary digits (**1** unsigned byte), by the number of
1596    places equal to the truncated absolute value of **p** modded by **2** to the
1597    power of **8**.
1598
1599    If you want to a use signed two's complement argument, use **s2u(x)** to
1600    convert.
1601
1602**brol16(x, p)**
1603
1604:   Does a left bitwise rotatation of the truncated absolute value of **x**, as
1605    though it has **16** binary digits (**2** unsigned bytes), by the number of
1606    places equal to the truncated absolute value of **p** modded by **2** to the
1607    power of **16**.
1608
1609    If you want to a use signed two's complement argument, use **s2u(x)** to
1610    convert.
1611
1612**brol32(x, p)**
1613
1614:   Does a left bitwise rotatation of the truncated absolute value of **x**, as
1615    though it has **32** binary digits (**2** unsigned bytes), by the number of
1616    places equal to the truncated absolute value of **p** modded by **2** to the
1617    power of **32**.
1618
1619    If you want to a use signed two's complement argument, use **s2u(x)** to
1620    convert.
1621
1622**brol64(x, p)**
1623
1624:   Does a left bitwise rotatation of the truncated absolute value of **x**, as
1625    though it has **64** binary digits (**2** unsigned bytes), by the number of
1626    places equal to the truncated absolute value of **p** modded by **2** to the
1627    power of **64**.
1628
1629    If you want to a use signed two's complement argument, use **s2u(x)** to
1630    convert.
1631
1632**brol(x, p)**
1633
1634:   Does a left bitwise rotatation of the truncated absolute value of **x**, as
1635    though it has the minimum number of power of two unsigned 8-bit bytes, by
1636    the number of places equal to the truncated absolute value of **p** modded
1637    by 2 to the power of the number of binary digits in the minimum number of
1638    8-bit bytes.
1639
1640    If you want to a use signed two's complement argument, use **s2u(x)** to
1641    convert.
1642
1643**brorn(x, p, n)**
1644
1645:   Does a right bitwise rotatation of the truncated absolute value of **x**, as
1646    though it has the same number of unsigned 8-bit bytes as the truncated
1647    absolute value of **n**, by the number of places equal to the truncated
1648    absolute value of **p** modded by the **2** to the power of the number of
1649    binary digits in **n** 8-bit bytes.
1650
1651    If you want to a use signed two's complement argument, use **s2u(x)** to
1652    convert.
1653
1654**bror8(x, p)**
1655
1656:   Does a right bitwise rotatation of the truncated absolute value of **x**, as
1657    though it has **8** binary digits (**1** unsigned byte), by the number of
1658    places equal to the truncated absolute value of **p** modded by **2** to the
1659    power of **8**.
1660
1661    If you want to a use signed two's complement argument, use **s2u(x)** to
1662    convert.
1663
1664**bror16(x, p)**
1665
1666:   Does a right bitwise rotatation of the truncated absolute value of **x**, as
1667    though it has **16** binary digits (**2** unsigned bytes), by the number of
1668    places equal to the truncated absolute value of **p** modded by **2** to the
1669    power of **16**.
1670
1671    If you want to a use signed two's complement argument, use **s2u(x)** to
1672    convert.
1673
1674**bror32(x, p)**
1675
1676:   Does a right bitwise rotatation of the truncated absolute value of **x**, as
1677    though it has **32** binary digits (**2** unsigned bytes), by the number of
1678    places equal to the truncated absolute value of **p** modded by **2** to the
1679    power of **32**.
1680
1681    If you want to a use signed two's complement argument, use **s2u(x)** to
1682    convert.
1683
1684**bror64(x, p)**
1685
1686:   Does a right bitwise rotatation of the truncated absolute value of **x**, as
1687    though it has **64** binary digits (**2** unsigned bytes), by the number of
1688    places equal to the truncated absolute value of **p** modded by **2** to the
1689    power of **64**.
1690
1691    If you want to a use signed two's complement argument, use **s2u(x)** to
1692    convert.
1693
1694**bror(x, p)**
1695
1696:   Does a right bitwise rotatation of the truncated absolute value of **x**, as
1697    though it has the minimum number of power of two unsigned 8-bit bytes, by
1698    the number of places equal to the truncated absolute value of **p** modded
1699    by 2 to the power of the number of binary digits in the minimum number of
1700    8-bit bytes.
1701
1702    If you want to a use signed two's complement argument, use **s2u(x)** to
1703    convert.
1704
1705**bmodn(x, n)**
1706
1707:   Returns the modulus of the truncated absolute value of **x** by **2** to the
1708    power of the multiplication of the truncated absolute value of **n** and
1709    **8**.
1710
1711    If you want to a use signed two's complement argument, use **s2u(x)** to
1712    convert.
1713
1714**bmod8(x, n)**
1715
1716:   Returns the modulus of the truncated absolute value of **x** by **2** to the
1717    power of **8**.
1718
1719    If you want to a use signed two's complement argument, use **s2u(x)** to
1720    convert.
1721
1722**bmod16(x, n)**
1723
1724:   Returns the modulus of the truncated absolute value of **x** by **2** to the
1725    power of **16**.
1726
1727    If you want to a use signed two's complement argument, use **s2u(x)** to
1728    convert.
1729
1730**bmod32(x, n)**
1731
1732:   Returns the modulus of the truncated absolute value of **x** by **2** to the
1733    power of **32**.
1734
1735    If you want to a use signed two's complement argument, use **s2u(x)** to
1736    convert.
1737
1738**bmod64(x, n)**
1739
1740:   Returns the modulus of the truncated absolute value of **x** by **2** to the
1741    power of **64**.
1742
1743    If you want to a use signed two's complement argument, use **s2u(x)** to
1744    convert.
1745
1746**bunrev(t)**
1747
1748:   Assumes **t** is a bitwise-reversed number with an extra set bit one place
1749    more significant than the real most significant bit (which was the least
1750    significant bit in the original number). This number is reversed and
1751    returned without the extra set bit.
1752
1753    This function is used to implement other bitwise functions; it is not meant
1754    to be used by users, but it can be.
1755
1756**plz(x)**
1757
1758:   If **x** is not equal to **0** and greater that **-1** and less than **1**,
1759    it is printed with a leading zero, regardless of the use of the **-z**
1760    option (see the **OPTIONS** section) and without a trailing newline.
1761
1762    Otherwise, **x** is printed normally, without a trailing newline.
1763
1764**plznl(x)**
1765
1766:   If **x** is not equal to **0** and greater that **-1** and less than **1**,
1767    it is printed with a leading zero, regardless of the use of the **-z**
1768    option (see the **OPTIONS** section) and with a trailing newline.
1769
1770    Otherwise, **x** is printed normally, with a trailing newline.
1771
1772**pnlz(x)**
1773
1774:   If **x** is not equal to **0** and greater that **-1** and less than **1**,
1775    it is printed without a leading zero, regardless of the use of the **-z**
1776    option (see the **OPTIONS** section) and without a trailing newline.
1777
1778    Otherwise, **x** is printed normally, without a trailing newline.
1779
1780**pnlznl(x)**
1781
1782:   If **x** is not equal to **0** and greater that **-1** and less than **1**,
1783    it is printed without a leading zero, regardless of the use of the **-z**
1784    option (see the **OPTIONS** section) and with a trailing newline.
1785
1786    Otherwise, **x** is printed normally, with a trailing newline.
1787
1788**ubytes(x)**
1789
1790:   Returns the numbers of unsigned integer bytes required to hold the truncated
1791    absolute value of **x**.
1792
1793**sbytes(x)**
1794
1795:   Returns the numbers of signed, two's-complement integer bytes required to
1796    hold the truncated value of **x**.
1797
1798**s2u(x)**
1799
1800:   Returns **x** if it is non-negative. If it *is* negative, then it calculates
1801    what **x** would be as a 2's-complement signed integer and returns the
1802    non-negative integer that would have the same representation in binary.
1803
1804**s2un(x,n)**
1805
1806:   Returns **x** if it is non-negative. If it *is* negative, then it calculates
1807    what **x** would be as a 2's-complement signed integer with **n** bytes and
1808    returns the non-negative integer that would have the same representation in
1809    binary. If **x** cannot fit into **n** 2's-complement signed bytes, it is
1810    truncated to fit.
1811
1812**hex(x)**
1813
1814:   Outputs the hexadecimal (base **16**) representation of **x**.
1815
1816    This is a **void** function (see the *Void Functions* subsection of the
1817    **FUNCTIONS** section).
1818
1819**binary(x)**
1820
1821:   Outputs the binary (base **2**) representation of **x**.
1822
1823    This is a **void** function (see the *Void Functions* subsection of the
1824    **FUNCTIONS** section).
1825
1826**output(x, b)**
1827
1828:   Outputs the base **b** representation of **x**.
1829
1830    This is a **void** function (see the *Void Functions* subsection of the
1831    **FUNCTIONS** section).
1832
1833**uint(x)**
1834
1835:   Outputs the representation, in binary and hexadecimal, of **x** as an
1836    unsigned integer in as few power of two bytes as possible. Both outputs are
1837    split into bytes separated by spaces.
1838
1839    If **x** is not an integer or is negative, an error message is printed
1840    instead, but bc(1) is not reset (see the **RESET** section).
1841
1842    This is a **void** function (see the *Void Functions* subsection of the
1843    **FUNCTIONS** section).
1844
1845**int(x)**
1846
1847:   Outputs the representation, in binary and hexadecimal, of **x** as a signed,
1848    two's-complement integer in as few power of two bytes as possible. Both
1849    outputs are split into bytes separated by spaces.
1850
1851    If **x** is not an integer, an error message is printed instead, but bc(1)
1852    is not reset (see the **RESET** section).
1853
1854    This is a **void** function (see the *Void Functions* subsection of the
1855    **FUNCTIONS** section).
1856
1857**uintn(x, n)**
1858
1859:   Outputs the representation, in binary and hexadecimal, of **x** as an
1860    unsigned integer in **n** bytes. Both outputs are split into bytes separated
1861    by spaces.
1862
1863    If **x** is not an integer, is negative, or cannot fit into **n** bytes, an
1864    error message is printed instead, but bc(1) is not reset (see the **RESET**
1865    section).
1866
1867    This is a **void** function (see the *Void Functions* subsection of the
1868    **FUNCTIONS** section).
1869
1870**intn(x, n)**
1871
1872:   Outputs the representation, in binary and hexadecimal, of **x** as a signed,
1873    two's-complement integer in **n** bytes. Both outputs are split into bytes
1874    separated by spaces.
1875
1876    If **x** is not an integer or cannot fit into **n** bytes, an error message
1877    is printed instead, but bc(1) is not reset (see the **RESET** section).
1878
1879    This is a **void** function (see the *Void Functions* subsection of the
1880    **FUNCTIONS** section).
1881
1882**uint8(x)**
1883
1884:   Outputs the representation, in binary and hexadecimal, of **x** as an
1885    unsigned integer in **1** byte. Both outputs are split into bytes separated
1886    by spaces.
1887
1888    If **x** is not an integer, is negative, or cannot fit into **1** byte, an
1889    error message is printed instead, but bc(1) is not reset (see the **RESET**
1890    section).
1891
1892    This is a **void** function (see the *Void Functions* subsection of the
1893    **FUNCTIONS** section).
1894
1895**int8(x)**
1896
1897:   Outputs the representation, in binary and hexadecimal, of **x** as a signed,
1898    two's-complement integer in **1** byte. Both outputs are split into bytes
1899    separated by spaces.
1900
1901    If **x** is not an integer or cannot fit into **1** byte, an error message
1902    is printed instead, but bc(1) is not reset (see the **RESET** section).
1903
1904    This is a **void** function (see the *Void Functions* subsection of the
1905    **FUNCTIONS** section).
1906
1907**uint16(x)**
1908
1909:   Outputs the representation, in binary and hexadecimal, of **x** as an
1910    unsigned integer in **2** bytes. Both outputs are split into bytes separated
1911    by spaces.
1912
1913    If **x** is not an integer, is negative, or cannot fit into **2** bytes, an
1914    error message is printed instead, but bc(1) is not reset (see the **RESET**
1915    section).
1916
1917    This is a **void** function (see the *Void Functions* subsection of the
1918    **FUNCTIONS** section).
1919
1920**int16(x)**
1921
1922:   Outputs the representation, in binary and hexadecimal, of **x** as a signed,
1923    two's-complement integer in **2** bytes. Both outputs are split into bytes
1924    separated by spaces.
1925
1926    If **x** is not an integer or cannot fit into **2** bytes, an error message
1927    is printed instead, but bc(1) is not reset (see the **RESET** section).
1928
1929    This is a **void** function (see the *Void Functions* subsection of the
1930    **FUNCTIONS** section).
1931
1932**uint32(x)**
1933
1934:   Outputs the representation, in binary and hexadecimal, of **x** as an
1935    unsigned integer in **4** bytes. Both outputs are split into bytes separated
1936    by spaces.
1937
1938    If **x** is not an integer, is negative, or cannot fit into **4** bytes, an
1939    error message is printed instead, but bc(1) is not reset (see the **RESET**
1940    section).
1941
1942    This is a **void** function (see the *Void Functions* subsection of the
1943    **FUNCTIONS** section).
1944
1945**int32(x)**
1946
1947:   Outputs the representation, in binary and hexadecimal, of **x** as a signed,
1948    two's-complement integer in **4** bytes. Both outputs are split into bytes
1949    separated by spaces.
1950
1951    If **x** is not an integer or cannot fit into **4** bytes, an error message
1952    is printed instead, but bc(1) is not reset (see the **RESET** section).
1953
1954    This is a **void** function (see the *Void Functions* subsection of the
1955    **FUNCTIONS** section).
1956
1957**uint64(x)**
1958
1959:   Outputs the representation, in binary and hexadecimal, of **x** as an
1960    unsigned integer in **8** bytes. Both outputs are split into bytes separated
1961    by spaces.
1962
1963    If **x** is not an integer, is negative, or cannot fit into **8** bytes, an
1964    error message is printed instead, but bc(1) is not reset (see the **RESET**
1965    section).
1966
1967    This is a **void** function (see the *Void Functions* subsection of the
1968    **FUNCTIONS** section).
1969
1970**int64(x)**
1971
1972:   Outputs the representation, in binary and hexadecimal, of **x** as a signed,
1973    two's-complement integer in **8** bytes. Both outputs are split into bytes
1974    separated by spaces.
1975
1976    If **x** is not an integer or cannot fit into **8** bytes, an error message
1977    is printed instead, but bc(1) is not reset (see the **RESET** section).
1978
1979    This is a **void** function (see the *Void Functions* subsection of the
1980    **FUNCTIONS** section).
1981
1982**hex_uint(x, n)**
1983
1984:   Outputs the representation of the truncated absolute value of **x** as an
1985    unsigned integer in hexadecimal using **n** bytes. Not all of the value will
1986    be output if **n** is too small.
1987
1988    This is a **void** function (see the *Void Functions* subsection of the
1989    **FUNCTIONS** section).
1990
1991**binary_uint(x, n)**
1992
1993:   Outputs the representation of the truncated absolute value of **x** as an
1994    unsigned integer in binary using **n** bytes. Not all of the value will be
1995    output if **n** is too small.
1996
1997    This is a **void** function (see the *Void Functions* subsection of the
1998    **FUNCTIONS** section).
1999
2000**output_uint(x, n)**
2001
2002:   Outputs the representation of the truncated absolute value of **x** as an
2003    unsigned integer in the current **obase** (see the **SYNTAX** section) using
2004    **n** bytes. Not all of the value will be output if **n** is too small.
2005
2006    This is a **void** function (see the *Void Functions* subsection of the
2007    **FUNCTIONS** section).
2008
2009**output_byte(x, i)**
2010
2011:   Outputs byte **i** of the truncated absolute value of **x**, where **0** is
2012    the least significant byte and **number_of_bytes - 1** is the most
2013    significant byte.
2014
2015    This is a **void** function (see the *Void Functions* subsection of the
2016    **FUNCTIONS** section).
2017
2018## Transcendental Functions
2019
2020All transcendental functions can return slightly inaccurate results, up to 1 ULP
2021(https://en.wikipedia.org/wiki/Unit_in_the_last_place). This is unavoidable, and
2022the  article at https://people.eecs.berkeley.edu/~wkahan/LOG10HAF.TXT explains
2023why it is impossible and unnecessary to calculate exact results for the
2024transcendental functions.
2025
2026Because of the possible inaccuracy, I recommend that users call those functions
2027with the precision (**scale**) set to at least 1 higher than is necessary. If
2028exact results are *absolutely* required, users can double the precision
2029(**scale**) and then truncate.
2030
2031The transcendental functions in the standard math library are:
2032
2033* **s(x)**
2034* **c(x)**
2035* **a(x)**
2036* **l(x)**
2037* **e(x)**
2038* **j(x, n)**
2039
2040The transcendental functions in the extended math library are:
2041
2042* **l2(x)**
2043* **l10(x)**
2044* **log(x, b)**
2045* **pi(p)**
2046* **t(x)**
2047* **a2(y, x)**
2048* **sin(x)**
2049* **cos(x)**
2050* **tan(x)**
2051* **atan(x)**
2052* **atan2(y, x)**
2053* **r2d(x)**
2054* **d2r(x)**
2055
2056# RESET
2057
2058When bc(1) encounters an error or a signal that it has a non-default handler
2059for, it resets. This means that several things happen.
2060
2061First, any functions that are executing are stopped and popped off the stack.
2062The behavior is not unlike that of exceptions in programming languages. Then
2063the execution point is set so that any code waiting to execute (after all
2064functions returned) is skipped.
2065
2066Thus, when bc(1) resets, it skips any remaining code waiting to be executed.
2067Then, if it is interactive mode, and the error was not a fatal error (see the
2068**EXIT STATUS** section), it asks for more input; otherwise, it exits with the
2069appropriate return code.
2070
2071Note that this reset behavior is different from the GNU bc(1), which attempts to
2072start executing the statement right after the one that caused an error.
2073
2074# PERFORMANCE
2075
2076Most bc(1) implementations use **char** types to calculate the value of **1**
2077decimal digit at a time, but that can be slow. This bc(1) does something
2078different.
2079
2080It uses large integers to calculate more than **1** decimal digit at a time. If
2081built in a environment where **BC_LONG_BIT** (see the **LIMITS** section) is
2082**64**, then each integer has **9** decimal digits. If built in an environment
2083where **BC_LONG_BIT** is **32** then each integer has **4** decimal digits. This
2084value (the number of decimal digits per large integer) is called
2085**BC_BASE_DIGS**.
2086
2087The actual values of **BC_LONG_BIT** and **BC_BASE_DIGS** can be queried with
2088the **limits** statement.
2089
2090In addition, this bc(1) uses an even larger integer for overflow checking. This
2091integer type depends on the value of **BC_LONG_BIT**, but is always at least
2092twice as large as the integer type used to store digits.
2093
2094# LIMITS
2095
2096The following are the limits on bc(1):
2097
2098**BC_LONG_BIT**
2099
2100:   The number of bits in the **long** type in the environment where bc(1) was
2101    built. This determines how many decimal digits can be stored in a single
2102    large integer (see the **PERFORMANCE** section).
2103
2104**BC_BASE_DIGS**
2105
2106:   The number of decimal digits per large integer (see the **PERFORMANCE**
2107    section). Depends on **BC_LONG_BIT**.
2108
2109**BC_BASE_POW**
2110
2111:   The max decimal number that each large integer can store (see
2112    **BC_BASE_DIGS**) plus **1**. Depends on **BC_BASE_DIGS**.
2113
2114**BC_OVERFLOW_MAX**
2115
2116:   The max number that the overflow type (see the **PERFORMANCE** section) can
2117    hold. Depends on **BC_LONG_BIT**.
2118
2119**BC_BASE_MAX**
2120
2121:   The maximum output base. Set at **BC_BASE_POW**.
2122
2123**BC_DIM_MAX**
2124
2125:   The maximum size of arrays. Set at **SIZE_MAX-1**.
2126
2127**BC_SCALE_MAX**
2128
2129:   The maximum **scale**. Set at **BC_OVERFLOW_MAX-1**.
2130
2131**BC_STRING_MAX**
2132
2133:   The maximum length of strings. Set at **BC_OVERFLOW_MAX-1**.
2134
2135**BC_NAME_MAX**
2136
2137:   The maximum length of identifiers. Set at **BC_OVERFLOW_MAX-1**.
2138
2139**BC_NUM_MAX**
2140
2141:   The maximum length of a number (in decimal digits), which includes digits
2142    after the decimal point. Set at **BC_OVERFLOW_MAX-1**.
2143
2144**BC_RAND_MAX**
2145
2146:   The maximum integer (inclusive) returned by the **rand()** operand. Set at
2147    **2\^BC_LONG_BIT-1**.
2148
2149Exponent
2150
2151:   The maximum allowable exponent (positive or negative). Set at
2152    **BC_OVERFLOW_MAX**.
2153
2154Number of vars
2155
2156:   The maximum number of vars/arrays. Set at **SIZE_MAX-1**.
2157
2158The actual values can be queried with the **limits** statement.
2159
2160These limits are meant to be effectively non-existent; the limits are so large
2161(at least on 64-bit machines) that there should not be any point at which they
2162become a problem. In fact, memory should be exhausted before these limits should
2163be hit.
2164
2165# ENVIRONMENT VARIABLES
2166
2167As **non-portable extensions**, bc(1) recognizes the following environment
2168variables:
2169
2170**POSIXLY_CORRECT**
2171
2172:   If this variable exists (no matter the contents), bc(1) behaves as if
2173    the **-s** option was given.
2174
2175**BC_ENV_ARGS**
2176
2177:   This is another way to give command-line arguments to bc(1). They should be
2178    in the same format as all other command-line arguments. These are always
2179    processed first, so any files given in **BC_ENV_ARGS** will be processed
2180    before arguments and files given on the command-line. This gives the user
2181    the ability to set up "standard" options and files to be used at every
2182    invocation. The most useful thing for such files to contain would be useful
2183    functions that the user might want every time bc(1) runs.
2184
2185    The code that parses **BC_ENV_ARGS** will correctly handle quoted arguments,
2186    but it does not understand escape sequences. For example, the string
2187    **"/home/gavin/some bc file.bc"** will be correctly parsed, but the string
2188    **"/home/gavin/some \"bc\" file.bc"** will include the backslashes.
2189
2190    The quote parsing will handle either kind of quotes, **'** or **"**. Thus,
2191    if you have a file with any number of single quotes in the name, you can use
2192    double quotes as the outside quotes, as in **"some 'bc' file.bc"**, and vice
2193    versa if you have a file with double quotes. However, handling a file with
2194    both kinds of quotes in **BC_ENV_ARGS** is not supported due to the
2195    complexity of the parsing, though such files are still supported on the
2196    command-line where the parsing is done by the shell.
2197
2198**BC_LINE_LENGTH**
2199
2200:   If this environment variable exists and contains an integer that is greater
2201    than **1** and is less than **UINT16_MAX** (**2\^16-1**), bc(1) will output
2202    lines to that length, including the backslash (**\\**). The default line
2203    length is **70**.
2204
2205    The special value of **0** will disable line length checking and print
2206    numbers without regard to line length and without backslashes and newlines.
2207
2208**BC_BANNER**
2209
2210:   If this environment variable exists and contains an integer, then a non-zero
2211    value activates the copyright banner when bc(1) is in interactive mode,
2212    while zero deactivates it.
2213
2214    If bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section),
2215    then this environment variable has no effect because bc(1) does not print
2216    the banner when not in interactive mode.
2217
2218    This environment variable overrides the default, which can be queried with
2219    the **-h** or **-\-help** options.
2220
2221**BC_SIGINT_RESET**
2222
2223:   If bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section),
2224    then this environment variable has no effect because bc(1) exits on
2225    **SIGINT** when not in interactive mode.
2226
2227    However, when bc(1) is in interactive mode, then if this environment
2228    variable exists and contains an integer, a non-zero value makes bc(1) reset
2229    on **SIGINT**, rather than exit, and zero makes bc(1) exit. If this
2230    environment variable exists and is *not* an integer, then bc(1) will exit on
2231    **SIGINT**.
2232
2233    This environment variable overrides the default, which can be queried with
2234    the **-h** or **-\-help** options.
2235
2236**BC_TTY_MODE**
2237
2238:   If TTY mode is *not* available (see the **TTY MODE** section), then this
2239    environment variable has no effect.
2240
2241    However, when TTY mode is available, then if this environment variable
2242    exists and contains an integer, then a non-zero value makes bc(1) use TTY
2243    mode, and zero makes bc(1) not use TTY mode.
2244
2245    This environment variable overrides the default, which can be queried with
2246    the **-h** or **-\-help** options.
2247
2248**BC_PROMPT**
2249
2250:   If TTY mode is *not* available (see the **TTY MODE** section), then this
2251    environment variable has no effect.
2252
2253    However, when TTY mode is available, then if this environment variable
2254    exists and contains an integer, a non-zero value makes bc(1) use a prompt,
2255    and zero or a non-integer makes bc(1) not use a prompt. If this environment
2256    variable does not exist and **BC_TTY_MODE** does, then the value of the
2257    **BC_TTY_MODE** environment variable is used.
2258
2259    This environment variable and the **BC_TTY_MODE** environment variable
2260    override the default, which can be queried with the **-h** or **-\-help**
2261    options.
2262
2263**BC_EXPR_EXIT**
2264
2265:   If any expressions or expression files are given on the command-line with
2266    **-e**, **-\-expression**, **-f**, or **-\-file**, then if this environment
2267    variable exists and contains an integer, a non-zero value makes bc(1) exit
2268    after executing the expressions and expression files, and a zero value makes
2269    bc(1) not exit.
2270
2271    This environment variable overrides the default, which can be queried with
2272    the **-h** or **-\-help** options.
2273
2274**BC_DIGIT_CLAMP**
2275
2276:   When parsing numbers and if this environment variable exists and contains an
2277    integer, a non-zero value makes bc(1) clamp digits that are greater than or
2278    equal to the current **ibase** so that all such digits are considered equal
2279    to the **ibase** minus 1, and a zero value disables such clamping so that
2280    those digits are always equal to their value, which is multiplied by the
2281    power of the **ibase**.
2282
2283    This never applies to single-digit numbers, as per the standard (see the
2284    **STANDARDS** section).
2285
2286    This environment variable overrides the default, which can be queried with
2287    the **-h** or **-\-help** options.
2288
2289# EXIT STATUS
2290
2291bc(1) returns the following exit statuses:
2292
2293**0**
2294
2295:   No error.
2296
2297**1**
2298
2299:   A math error occurred. This follows standard practice of using **1** for
2300    expected errors, since math errors will happen in the process of normal
2301    execution.
2302
2303    Math errors include divide by **0**, taking the square root of a negative
2304    number, using a negative number as a bound for the pseudo-random number
2305    generator, attempting to convert a negative number to a hardware integer,
2306    overflow when converting a number to a hardware integer, overflow when
2307    calculating the size of a number, and attempting to use a non-integer where
2308    an integer is required.
2309
2310    Converting to a hardware integer happens for the second operand of the power
2311    (**\^**), places (**\@**), left shift (**\<\<**), and right shift (**\>\>**)
2312    operators and their corresponding assignment operators.
2313
2314**2**
2315
2316:   A parse error occurred.
2317
2318    Parse errors include unexpected **EOF**, using an invalid character, failing
2319    to find the end of a string or comment, using a token where it is invalid,
2320    giving an invalid expression, giving an invalid print statement, giving an
2321    invalid function definition, attempting to assign to an expression that is
2322    not a named expression (see the *Named Expressions* subsection of the
2323    **SYNTAX** section), giving an invalid **auto** list, having a duplicate
2324    **auto**/function parameter, failing to find the end of a code block,
2325    attempting to return a value from a **void** function, attempting to use a
2326    variable as a reference, and using any extensions when the option **-s** or
2327    any equivalents were given.
2328
2329**3**
2330
2331:   A runtime error occurred.
2332
2333    Runtime errors include assigning an invalid number to any global (**ibase**,
2334    **obase**, or **scale**), giving a bad expression to a **read()** call,
2335    calling **read()** inside of a **read()** call, type errors, passing the
2336    wrong number of arguments to functions, attempting to call an undefined
2337    function, and attempting to use a **void** function call as a value in an
2338    expression.
2339
2340**4**
2341
2342:   A fatal error occurred.
2343
2344    Fatal errors include memory allocation errors, I/O errors, failing to open
2345    files, attempting to use files that do not have only ASCII characters (bc(1)
2346    only accepts ASCII characters), attempting to open a directory as a file,
2347    and giving invalid command-line options.
2348
2349The exit status **4** is special; when a fatal error occurs, bc(1) always exits
2350and returns **4**, no matter what mode bc(1) is in.
2351
2352The other statuses will only be returned when bc(1) is not in interactive mode
2353(see the **INTERACTIVE MODE** section), since bc(1) resets its state (see the
2354**RESET** section) and accepts more input when one of those errors occurs in
2355interactive mode. This is also the case when interactive mode is forced by the
2356**-i** flag or **-\-interactive** option.
2357
2358These exit statuses allow bc(1) to be used in shell scripting with error
2359checking, and its normal behavior can be forced by using the **-i** flag or
2360**-\-interactive** option.
2361
2362# INTERACTIVE MODE
2363
2364Per the standard (see the **STANDARDS** section), bc(1) has an interactive mode
2365and a non-interactive mode. Interactive mode is turned on automatically when
2366both **stdin** and **stdout** are hooked to a terminal, but the **-i** flag and
2367**-\-interactive** option can turn it on in other situations.
2368
2369In interactive mode, bc(1) attempts to recover from errors (see the **RESET**
2370section), and in normal execution, flushes **stdout** as soon as execution is
2371done for the current input. bc(1) may also reset on **SIGINT** instead of exit,
2372depending on the contents of, or default for, the **BC_SIGINT_RESET**
2373environment variable (see the **ENVIRONMENT VARIABLES** section).
2374
2375# TTY MODE
2376
2377If **stdin**, **stdout**, and **stderr** are all connected to a TTY, then "TTY
2378mode" is considered to be available, and thus, bc(1) can turn on TTY mode,
2379subject to some settings.
2380
2381If there is the environment variable **BC_TTY_MODE** in the environment (see the
2382**ENVIRONMENT VARIABLES** section), then if that environment variable contains a
2383non-zero integer, bc(1) will turn on TTY mode when **stdin**, **stdout**, and
2384**stderr** are all connected to a TTY. If the **BC_TTY_MODE** environment
2385variable exists but is *not* a non-zero integer, then bc(1) will not turn TTY
2386mode on.
2387
2388If the environment variable **BC_TTY_MODE** does *not* exist, the default
2389setting is used. The default setting can be queried with the **-h** or
2390**-\-help** options.
2391
2392TTY mode is different from interactive mode because interactive mode is required
2393in the bc(1) standard (see the **STANDARDS** section), and interactive mode
2394requires only **stdin** and **stdout** to be connected to a terminal.
2395
2396## Prompt
2397
2398If TTY mode is available, then a prompt can be enabled. Like TTY mode itself, it
2399can be turned on or off with an environment variable: **BC_PROMPT** (see the
2400**ENVIRONMENT VARIABLES** section).
2401
2402If the environment variable **BC_PROMPT** exists and is a non-zero integer, then
2403the prompt is turned on when **stdin**, **stdout**, and **stderr** are connected
2404to a TTY and the **-P** and **-\-no-prompt** options were not used. The read
2405prompt will be turned on under the same conditions, except that the **-R** and
2406**-\-no-read-prompt** options must also not be used.
2407
2408However, if **BC_PROMPT** does not exist, the prompt can be enabled or disabled
2409with the **BC_TTY_MODE** environment variable, the **-P** and **-\-no-prompt**
2410options, and the **-R** and **-\-no-read-prompt** options. See the **ENVIRONMENT
2411VARIABLES** and **OPTIONS** sections for more details.
2412
2413# SIGNAL HANDLING
2414
2415Sending a **SIGINT** will cause bc(1) to do one of two things.
2416
2417If bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section), or
2418the **BC_SIGINT_RESET** environment variable (see the **ENVIRONMENT VARIABLES**
2419section), or its default, is either not an integer or it is zero, bc(1) will
2420exit.
2421
2422However, if bc(1) is in interactive mode, and the **BC_SIGINT_RESET** or its
2423default is an integer and non-zero, then bc(1) will stop executing the current
2424input and reset (see the **RESET** section) upon receiving a **SIGINT**.
2425
2426Note that "current input" can mean one of two things. If bc(1) is processing
2427input from **stdin** in interactive mode, it will ask for more input. If bc(1)
2428is processing input from a file in interactive mode, it will stop processing the
2429file and start processing the next file, if one exists, or ask for input from
2430**stdin** if no other file exists.
2431
2432This means that if a **SIGINT** is sent to bc(1) as it is executing a file, it
2433can seem as though bc(1) did not respond to the signal since it will immediately
2434start executing the next file. This is by design; most files that users execute
2435when interacting with bc(1) have function definitions, which are quick to parse.
2436If a file takes a long time to execute, there may be a bug in that file. The
2437rest of the files could still be executed without problem, allowing the user to
2438continue.
2439
2440**SIGTERM** and **SIGQUIT** cause bc(1) to clean up and exit, and it uses the
2441default handler for all other signals.
2442
2443# SEE ALSO
2444
2445dc(1)
2446
2447# STANDARDS
2448
2449bc(1) is compliant with the IEEE Std 1003.1-2017 (“POSIX.1-2017”) specification
2450at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html . The
2451flags **-efghiqsvVw**, all long options, and the extensions noted above are
2452extensions to that specification.
2453
2454In addition, the behavior of the **quit** implements an interpretation of that
2455specification that is different from all known implementations. For more
2456information see the **Statements** subsection of the **SYNTAX** section.
2457
2458Note that the specification explicitly says that bc(1) only accepts numbers that
2459use a period (**.**) as a radix point, regardless of the value of
2460**LC_NUMERIC**.
2461
2462# BUGS
2463
2464Before version **6.1.0**, this bc(1) had incorrect behavior for the **quit**
2465statement.
2466
2467No other bugs are known. Report bugs at https://git.gavinhoward.com/gavin/bc .
2468
2469# AUTHORS
2470
2471Gavin D. Howard <gavin@gavinhoward.com> and contributors.
2472