Try to quiet Python syntax warnings
[gh-bc.git] / manuals / bc / A.1.md
blob56f7c52fb2cdea1c516d8b88df26a32a08571e63
1 <!---
3 SPDX-License-Identifier: BSD-2-Clause
5 Copyright (c) 2018-2024 Gavin D. Howard and contributors.
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
10 * Redistributions of source code must retain the above copyright notice, this
11   list of conditions and the following disclaimer.
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.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
29 -->
31 # NAME
33 bc - arbitrary-precision decimal arithmetic language and calculator
35 # SYNOPSIS
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*]
39 # DESCRIPTION
41 bc(1) is an interactive processor for a language first standardized in 1991 by
42 POSIX. (See the **STANDARDS** section.) The language provides unlimited
43 precision decimal arithmetic and is somewhat C-like, but there are differences.
44 Such differences will be noted in this document.
46 After parsing and handling options, this bc(1) reads any files given on the
47 command line and executes them before reading from **stdin**.
49 This bc(1) is a drop-in replacement for *any* bc(1), including (and
50 especially) the GNU bc(1). It also has many extensions and extra features beyond
51 other implementations.
53 **Note**: If running this bc(1) on *any* script meant for another bc(1) gives a
54 parse error, it is probably because a word this bc(1) reserves as a keyword is
55 used as the name of a function, variable, or array. To fix that, use the
56 command-line option **-r** *keyword*, where *keyword* is the keyword that is
57 used as a name in the script. For more information, see the **OPTIONS** section.
59 If parsing scripts meant for other bc(1) implementations still does not work,
60 that is a bug and should be reported. See the **BUGS** section.
62 # OPTIONS
64 The following are the options that bc(1) accepts.
66 **-C**, **-\-no-digit-clamp**
68 :   Disables clamping of digits greater than or equal to the current **ibase**
69     when parsing numbers.
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.
75     If this and/or the **-c** or **-\-digit-clamp** options are given multiple
76     times, the last one given is used.
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.
82     This is a **non-portable extension**.
84 **-c**, **-\-digit-clamp**
86 :   Enables clamping of digits greater than or equal to the current **ibase**
87     when parsing numbers.
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.
94     If this and/or the **-C** or **-\-no-digit-clamp** options are given
95     multiple times, the last one given is used.
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.
101     This is a **non-portable extension**.
103 **-E** *seed*, **-\-seed**=*seed*
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.
108     If multiple instances of this option are given, the last is used.
110     This is a **non-portable extension**.
112 **-e** *expr*, **-\-expression**=*expr*
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.
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.
128     This is a **non-portable extension**.
130 **-f** *file*, **-\-file**=*file*
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.
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.
144     This is a **non-portable extension**.
146 **-g**, **-\-global-stacks**
148 :   Turns the globals **ibase**, **obase**, **scale**, and **seed** into stacks.
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:
157         define void output(x, b) {
158             obase=b
159             x
160         }
162     instead of like this:
164         define void output(x, b) {
165             auto c
166             c=obase
167             obase=b
168             x
169             obase=c
170         }
172     This makes writing functions much easier.
174     (**Note**: the function **output(x,b)** exists in the extended math library.
175      See the **LIBRARY** section.)
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.
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:
186         alias d2o="bc -e ibase=A -e obase=8"
187         alias h2b="bc -e ibase=G -e obase=2"
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.
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.
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:
204         seed = seed
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).
210     If **-s**, **-w**, or any equivalents are used, this option is ignored.
212     This is a **non-portable extension**.
214 **-h**, **-\-help**
216 :   Prints a usage message and exits.
218 **-I** *ibase*, **-\-ibase**=*ibase*
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.
223     If multiple instances of this option are given, the last is used.
225     This is a **non-portable extension**.
227 **-i**, **-\-interactive**
229 :   Forces interactive mode. (See the **INTERACTIVE MODE** section.)
231     This is a **non-portable extension**.
233 **-L**, **-\-no-line-length**
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).
239     This is a **non-portable extension**.
241 **-l**, **-\-mathlib**
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.
247     To learn what is in the libraries, see the **LIBRARY** section.
249 **-O** *obase*, **-\-obase**=*obase*
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.
254     If multiple instances of this option are given, the last is used.
256     This is a **non-portable extension**.
258 **-P**, **-\-no-prompt**
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).
266     These options override the **BC_PROMPT** and **BC_TTY_MODE** environment
267     variables (see the **ENVIRONMENT VARIABLES** section).
269     This is a **non-portable extension**.
271 **-q**, **-\-quiet**
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.
282     This is a **non-portable extension**.
284 **-R**, **-\-no-read-prompt**
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.
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.
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.
300     This is a **non-portable extension**.
302 **-r** *keyword*, **-\-redefine**=*keyword*
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.
308     The keywords this bc(1) allows to be redefined are:
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**
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.
334     Keywords are *not* redefined when parsing the builtin math library (see the
335     **LIBRARY** section).
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.
341 **-S** *scale*, **-\-scale**=*scale*
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.
346     If multiple instances of this option are given, the last is used.
348     This is a **non-portable extension**.
350 **-s**, **-\-standard**
352 :   Process exactly the language defined by the standard (see the **STANDARDS**
353     section) and error if any extensions are used.
355     This is a **non-portable extension**.
357 **-v**, **-V**, **-\-version**
359 :   Print the version information (copyright header) and exits.
361     This is a **non-portable extension**.
363 **-w**, **-\-warn**
365 :   Like **-s** and **-\-standard**, except that warnings (and not errors) are
366     printed for non-standard extensions and execution continues normally.
368     This is a **non-portable extension**.
370 **-z**, **-\-leading-zeroes**
372 :   Makes bc(1) print all numbers greater than **-1** and less than **1**, and
373     not equal to **0**, with a leading zero.
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).
379     This is a **non-portable extension**.
381 All long options are **non-portable extensions**.
383 # STDIN
385 If no files or expressions are given by the **-f**, **-\-file**, **-e**, or
386 **-\-expression** options, then bc(1) reads from **stdin**.
388 However, there are a few caveats to this.
390 First, **stdin** is evaluated a line at a time. The only exception to this is if
391 the parse cannot complete. That means that starting a string without ending it
392 or starting a function, **if** statement, or loop without ending it will also
393 cause bc(1) to not execute.
395 Second, after an **if** statement, bc(1) doesn't know if an **else** statement
396 will follow, so it will not execute until it knows there will not be an **else**
397 statement.
399 # STDOUT
401 Any 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,
403 both are output to **stdout**.
405 **Note**: Unlike other bc(1) implementations, this bc(1) will issue a fatal
406 error (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
408 is done so that bc(1) can report problems when **stdout** is redirected to a
409 file.
411 If there are scripts that depend on the behavior of other bc(1) implementations,
412 it is recommended that those scripts be changed to redirect **stdout** to
413 **/dev/null**.
415 # STDERR
417 Any error output is written to **stderr**.
419 **Note**: Unlike other bc(1) implementations, this bc(1) will issue a fatal
420 error (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
422 is done so that bc(1) can exit with an error code when **stderr** is redirected
423 to a file.
425 If there are scripts that depend on the behavior of other bc(1) implementations,
426 it is recommended that those scripts be changed to redirect **stderr** to
427 **/dev/null**.
429 # SYNTAX
431 The syntax for bc(1) programs is mostly C-like, with some differences. This
432 bc(1) follows the POSIX standard (see the **STANDARDS** section), which is a
433 much more thorough resource for the language this bc(1) accepts. This section is
434 meant to be a summary and a listing of all the extensions to the standard.
436 In the sections below, **E** means expression, **S** means statement, and **I**
437 means identifier.
439 Identifiers (**I**) start with a lowercase letter and can be followed by any
440 number (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\_\]\***.
442 Identifiers with more than one character (letter) are a
443 **non-portable extension**.
445 **ibase** is a global variable determining how to interpret constant numbers. It
446 is 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
449 for **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
451 bc(1) programs with the **maxibase()** built-in function.
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
455 initially **10**. The max allowable value for **obase** is **BC_BASE_MAX** and
456 can be queried in bc(1) programs with the **maxobase()** built-in function. The
457 min allowable value for **obase** is **0**. If **obase** is **0**, values are
458 output in scientific notation, and if **obase** is **1**, values are output in
459 engineering notation. Otherwise, values are output in the specified base.
461 Outputting in scientific and engineering notations are **non-portable
462 extensions**.
464 The *scale* of an expression is the number of digits in the result of the
465 expression right of the decimal point, and **scale** is a global variable that
466 sets 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()**
469 built-in function.
471 bc(1) has both *global* variables and *local* variables. All *local*
472 variables are local to the function; they are parameters or are introduced in
473 the **auto** list of a function (see the **FUNCTIONS** section). If a variable
474 is accessed which is not a parameter or in the **auto** list, it is assumed to
475 be *global*. If a parent function has a *local* variable version of a variable
476 that a child function considers *global*, the value of that *global* variable in
477 the child function is the value of the variable in the parent function, not the
478 value of the actual *global* variable.
480 All of the above applies to arrays as well.
482 The value of a statement that is an expression (i.e., any of the named
483 expressions or operands) is printed unless the lowest precedence operator is an
484 assignment operator *and* the expression is notsurrounded by parentheses.
486 The value that is printed is also assigned to the special variable **last**. A
487 single dot (**.**) may also be used as a synonym for **last**. These are
488 **non-portable extensions**.
490 Either semicolons or newlines may separate statements.
492 ## Comments
494 There are two kinds of comments:
496 1.      Block comments are enclosed in **/\*** and **\*/**.
497 2.      Line comments go from **#** until, and not including, the next newline. This
498         is a **non-portable extension**.
500 ## Named Expressions
502 The following are named expressions in bc(1):
504 1.      Variables: **I**
505 2.      Array Elements: **I[E]**
506 3.      **ibase**
507 4.      **obase**
508 5.      **scale**
509 6.      **seed**
510 7.      **last** or a single dot (**.**)
512 Numbers 6 and 7 are **non-portable extensions**.
514 The meaning of **seed** is dependent on the current pseudo-random number
515 generator but is guaranteed to not change except for new major versions.
517 The *scale* and sign of the value may be significant.
519 If a previously used **seed** value is assigned to **seed** and used again, the
520 pseudo-random number generator is guaranteed to produce the same sequence of
521 pseudo-random numbers as it did when the **seed** value was previously used.
523 The exact value assigned to **seed** is not guaranteed to be returned if
524 **seed** is queried again immediately. However, if **seed** *does* return a
525 different value, both values, when assigned to **seed**, are guaranteed to
526 produce the same sequence of pseudo-random numbers. This means that certain
527 values assigned to **seed** will *not* produce unique sequences of pseudo-random
528 numbers. 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
530 parameter passed to **irand(E)** is **0**, **1**, or negative.
532 There is no limit to the length (number of significant decimal digits) or
533 *scale* of the value that can be assigned to **seed**.
535 Variables and arrays do not interfere; users can have arrays named the same as
536 variables. This also applies to functions (see the **FUNCTIONS** section), so a
537 user can have a variable, array, and function that all have the same name, and
538 they will not shadow each other, whether inside of functions or not.
540 Named expressions are required as the operand of **increment**/**decrement**
541 operators  and as the left side of **assignment** operators (see the *Operators*
542 subsection).
544 ## Operands
546 The following are valid operands in bc(1):
548 1.      Numbers (see the *Numbers* subsection below).
549 2.      Array indices (**I[E]**).
550 3.      **(E)**: The value of **E** (used to change precedence).
551 4.      **sqrt(E)**: The square root of **E**. **E** must be non-negative.
552 5.      **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**.
556 6.      **length(I[])**: The number of elements in the array **I**. This is a
557         **non-portable extension**.
558 7.      **scale(E)**: The *scale* of **E**.
559 8.      **abs(E)**: The absolute value of **E**. This is a **non-portable
560         extension**.
561 9.      **is_number(E)**: **1** if the given argument is a number, **0** if it is a
562         string. This is a **non-portable extension**.
563 10.     **is_string(E)**: **1** if the given argument is a string, **0** if it is a
564         number. This is a **non-portable extension**.
565 11.     **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**.
569 11.     **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**.
574 12.     **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**.
578 13.     **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**.
582 14.     **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.
588 15.     **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**.
591 16.     **maxibase()**: The max allowable **ibase**. This is a **non-portable
592         extension**.
593 17.     **maxobase()**: The max allowable **obase**. This is a **non-portable
594         extension**.
595 18.     **maxscale()**: The max allowable **scale**. This is a **non-portable
596         extension**.
597 19.     **line_length()**: The line length set with **BC_LINE_LENGTH** (see the
598         **ENVIRONMENT VARIABLES** section). This is a **non-portable extension**.
599 20.     **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**.
602 21.     **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**.
605 22.     **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**.
608 23.     **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**.
619 24.     **maxrand()**: The max integer returned by **rand()**. This is a
620         **non-portable extension**.
622 The integers generated by **rand()** and **irand(E)** are guaranteed to be as
623 unbiased as possible, subject to the limitations of the pseudo-random number
624 generator.
626 **Note**: The values returned by the pseudo-random number generator with
627 **rand()** and **irand(E)** are guaranteed to *NOT* be cryptographically secure.
628 This is a consequence of using a seeded pseudo-random number generator. However,
629 they *are* guaranteed to be reproducible with identical **seed** values. This
630 means that the pseudo-random values from bc(1) should only be used where a
631 reproducible stream of pseudo-random numbers is *ESSENTIAL*. In any other case,
632 use a non-seeded pseudo-random number generator.
634 ## Numbers
636 Numbers are strings made up of digits, uppercase letters, and at most **1**
637 period for a radix. Numbers can have up to **BC_NUM_MAX** digits. Uppercase
638 letters are equal to **9** plus their position in the alphabet, starting from
639 **1** (i.e., **A** equals **10**, or **9+1**).
641 If a digit or letter makes no sense with the current value of **ibase** (i.e.,
642 they are greater than or equal to the current value of **ibase**), then the
643 behavior depends on the existence of the **-c**/**-\-digit-clamp** or
644 **-C**/**-\-no-digit-clamp** options (see the **OPTIONS** section), the
645 existence and setting of the **BC_DIGIT_CLAMP** environment variable (see the
646 **ENVIRONMENT VARIABLES** section), or the default, which can be queried with
647 the **-h**/**-\-help** option.
649 If clamping is off, then digits or letters that are greater than or equal to the
650 current value of **ibase** are not changed. Instead, their given value is
651 multiplied by the appropriate power of **ibase** and added into the number. This
652 means 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**.
655 If clamping is on, then digits or letters that are greater than or equal to the
656 current 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
658 added 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**,
660 or **8**.
662 There is one exception to clamping: single-character numbers (i.e., **A**
663 alone). Such numbers are never clamped and always take the value they would have
664 in the highest possible **ibase**. This means that **A** alone always equals
665 decimal **10** and **Z** alone always equals decimal **35**. This behavior is
666 mandated by the standard (see the STANDARDS section) and is meant to provide an
667 easy way to set the current **ibase** (with the **i** command) regardless of the
668 current value of **ibase**.
670 If clamping is on, and the clamped value of a character is needed, use a leading
671 zero, i.e., for **A**, use **0A**.
673 In 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
675 an integer. An example is **1.89237e9**, which is equal to **1892370000**.
676 Negative exponents are also allowed, so **4.2890e-3** is equal to **0.0042890**.
678 Using scientific notation is an error or warning if the **-s** or **-w**,
679 respectively, command-line options (or equivalents) are given.
681 **WARNING**: Both the number and the exponent in scientific notation are
682 interpreted according to the current **ibase**, but the number is still
683 multiplied by **10\^exponent** regardless of the current **ibase**. For example,
684 if **ibase** is **16** and bc(1) is given the number string **FFeA**, the
685 resulting decimal number will be **2550000000000**, and if bc(1) is given the
686 number string **10e-4**, the resulting decimal number will be **0.0016**.
688 Accepting input as scientific notation is a **non-portable extension**.
690 ## Operators
692 The following arithmetic and logical operators can be used. They are listed in
693 order of decreasing precedence. Operators in the same group have the same
694 precedence.
696 **++** **-\-**
698 :   Type: Prefix and Postfix
700     Associativity: None
702     Description: **increment**, **decrement**
704 **-** **!**
706 :   Type: Prefix
708     Associativity: None
710     Description: **negation**, **boolean not**
712 **\$**
714 :   Type: Postfix
716     Associativity: None
718     Description: **truncation**
720 **\@**
722 :   Type: Binary
724     Associativity: Right
726     Description: **set precision**
728 **\^**
730 :   Type: Binary
732     Associativity: Right
734     Description: **power**
736 **\*** **/** **%**
738 :   Type: Binary
740     Associativity: Left
742     Description: **multiply**, **divide**, **modulus**
744 **+** **-**
746 :   Type: Binary
748     Associativity: Left
750     Description: **add**, **subtract**
752 **\<\<** **\>\>**
754 :   Type: Binary
756     Associativity: Left
758     Description: **shift left**, **shift right**
760 **=** **\<\<=** **\>\>=** **+=** **-=** **\*=** **/=** **%=** **\^=** **\@=**
762 :   Type: Binary
764     Associativity: Right
766     Description: **assignment**
768 **==** **\<=** **\>=** **!=** **\<** **\>**
770 :   Type: Binary
772     Associativity: Left
774     Description: **relational**
776 **&&**
778 :   Type: Binary
780     Associativity: Left
782     Description: **boolean and**
784 **||**
786 :   Type: Binary
788     Associativity: Left
790     Description: **boolean or**
792 The operators will be described in more detail below.
794 **++** **-\-**
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.
800     The prefix versions of these operators are more efficient; use them where
801     possible.
803 **-**
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.
809 **!**
811 :   The **boolean not** operator returns **1** if the expression is **0**, or
812     **0** otherwise.
814     **Warning**: This operator has a **different precedence** than the
815     equivalent operator in GNU bc(1) and other bc(1) implementations!
817     This is a **non-portable extension**.
819 **\$**
821 :   The **truncation** operator returns a copy of the given expression with all
822     of its *scale* removed.
824     This is a **non-portable extension**.
826 **\@**
828 :   The **set precision** operator takes two expressions and returns a copy of
829     the first with its *scale* equal to the value of the second expression. That
830     could either mean that the number is returned without change (if the
831     *scale* of the first expression matches the value of the second
832     expression), extended (if it is less), or truncated (if it is more).
834     The second expression must be an integer (no *scale*) and non-negative.
836     This is a **non-portable extension**.
838 **\^**
840 :   The **power** operator (not the **exclusive or** operator, as it would be in
841     C) takes two expressions and raises the first to the power of the value of
842     the second. The *scale* of the result is equal to **scale**.
844     The second expression must be an integer (no *scale*), and if it is
845     negative, the first value must be non-zero.
847 **\***
849 :   The **multiply** operator takes two expressions, multiplies them, and
850     returns the product. If **a** is the *scale* of the first expression and
851     **b** is the *scale* of the second expression, the *scale* of the result is
852     equal to **min(a+b,max(scale,a,b))** where **min()** and **max()** return
853     the obvious values.
855 **/**
857 :   The **divide** operator takes two expressions, divides them, and returns the
858     quotient. The *scale* of the result shall be the value of **scale**.
860     The second expression must be non-zero.
862 **%**
864 :   The **modulus** operator takes two expressions, **a** and **b**, and
865     evaluates them by 1) Computing **a/b** to current **scale** and 2) Using the
866     result of step 1 to calculate **a-(a/b)\*b** to *scale*
867     **max(scale+scale(b),scale(a))**.
869     The second expression must be non-zero.
871 **+**
873 :   The **add** operator takes two expressions, **a** and **b**, and returns the
874     sum, with a *scale* equal to the max of the *scale*s of **a** and **b**.
876 **-**
878 :   The **subtract** operator takes two expressions, **a** and **b**, and
879     returns the difference, with a *scale* equal to the max of the *scale*s of
880     **a** and **b**.
882 **\<\<**
884 :   The **left shift** operator takes two expressions, **a** and **b**, and
885     returns a copy of the value of **a** with its decimal point moved **b**
886     places to the right.
888     The second expression must be an integer (no *scale*) and non-negative.
890     This is a **non-portable extension**.
892 **\>\>**
894 :   The **right shift** operator takes two expressions, **a** and **b**, and
895     returns a copy of the value of **a** with its decimal point moved **b**
896     places to the left.
898     The second expression must be an integer (no *scale*) and non-negative.
900     This is a **non-portable extension**.
902 **=** **\<\<=** **\>\>=** **+=** **-=** **\*=** **/=** **%=** **\^=** **\@=**
904 :   The **assignment** operators take two expressions, **a** and **b** where
905     **a** is a named expression (see the *Named Expressions* subsection).
907     For **=**, **b** is copied and the result is assigned to **a**. For all
908     others, **a** and **b** are applied as operands to the corresponding
909     arithmetic operator and the result is assigned to **a**.
911     The **assignment** operators that correspond to operators that are
912     extensions are themselves **non-portable extensions**.
914 **==** **\<=** **\>=** **!=** **\<** **\>**
916 :   The **relational** operators compare two expressions, **a** and **b**, and
917     if the relation holds, according to C language semantics, the result is
918     **1**. Otherwise, it is **0**.
920     Note that unlike in C, these operators have a lower precedence than the
921     **assignment** operators, which means that **a=b\>c** is interpreted as
922     **(a=b)\>c**.
924     Also, unlike the standard (see the **STANDARDS** section) requires, these
925     operators can appear anywhere any other expressions can be used. This
926     allowance is a **non-portable extension**.
928 **&&**
930 :   The **boolean and** operator takes two expressions and returns **1** if both
931     expressions are non-zero, **0** otherwise.
933     This is *not* a short-circuit operator.
935     This is a **non-portable extension**.
937 **||**
939 :   The **boolean or** operator takes two expressions and returns **1** if one
940     of the expressions is non-zero, **0** otherwise.
942     This is *not* a short-circuit operator.
944     This is a **non-portable extension**.
946 ## Statements
948 The following items are statements:
950 1.      **E**
951 2.      **{** **S** **;** ... **;** **S** **}**
952 3.      **if** **(** **E** **)** **S**
953 4.      **if** **(** **E** **)** **S** **else** **S**
954 5.      **while** **(** **E** **)** **S**
955 6.      **for** **(** **E** **;** **E** **;** **E** **)** **S**
956 7.      An empty statement
957 8.      **break**
958 9.      **continue**
959 10.     **quit**
960 11.     **halt**
961 12.     **limits**
962 13.     A string of characters, enclosed in double quotes
963 14.     **print** **E** **,** ... **,** **E**
964 15.     **stream** **E** **,** ... **,** **E**
965 16.     **I()**, **I(E)**, **I(E, E)**, and so on, where **I** is an identifier for
966         a **void** function (see the *Void Functions* subsection of the
967         **FUNCTIONS** section). The **E** argument(s) may also be arrays of the form
968         **I[]**, which will automatically be turned into array references (see the
969         *Array References* subsection of the **FUNCTIONS** section) if the
970         corresponding parameter in the function definition is an array reference.
972 Numbers 4, 9, 11, 12, 14, 15, and 16 are **non-portable extensions**.
974 Also, as a **non-portable extension**, any or all of the expressions in the
975 header of a for loop may be omitted. If the condition (second expression) is
976 omitted, it is assumed to be a constant **1**.
978 The **break** statement causes a loop to stop iterating and resume execution
979 immediately following a loop. This is only allowed in loops.
981 The **continue** statement causes a loop iteration to stop early and returns to
982 the start of the loop, including testing the loop condition. This is only
983 allowed in loops.
985 The **if** **else** statement does the same thing as in C.
987 The **quit** statement causes bc(1) to quit, even if it is on a branch that will
988 not be executed (it is a compile-time command).
990 **Warning**: The behavior of this bc(1) on **quit** is slightly different from
991 other bc(1) implementations. Other bc(1) implementations will exit as soon as
992 they finish parsing the line that a **quit** command is on. This bc(1) will
993 execute any completed and executable statements that occur before the **quit**
994 statement before exiting.
996 In other words, for the bc(1) code below:
998     for (i = 0; i < 3; ++i) i; quit
1000 Other bc(1) implementations will print nothing, and this bc(1) will print **0**,
1001 **1**, and **2** on successive lines before exiting.
1003 The **halt** statement causes bc(1) to quit, if it is executed. (Unlike **quit**
1004 if it is on a branch of an **if** statement that is not executed, bc(1) does not
1005 quit.)
1007 The **limits** statement prints the limits that this bc(1) is subject to. This
1008 is like the **quit** statement in that it is a compile-time command.
1010 An expression by itself is evaluated and printed, followed by a newline.
1012 Both scientific notation and engineering notation are available for printing the
1013 results of expressions. Scientific notation is activated by assigning **0** to
1014 **obase**, and engineering notation is activated by assigning **1** to
1015 **obase**. To deactivate them, just assign a different value to **obase**.
1017 Scientific notation and engineering notation are disabled if bc(1) is run with
1018 either the **-s** or **-w** command-line options (or equivalents).
1020 Printing numbers in scientific notation and/or engineering notation is a
1021 **non-portable extension**.
1023 ## Strings
1025 If strings appear as a statement by themselves, they are printed without a
1026 trailing newline.
1028 In addition to appearing as a lone statement by themselves, strings can be
1029 assigned to variables and array elements. They can also be passed to functions
1030 in variable parameters.
1032 If any statement that expects a string is given a variable that had a string
1033 assigned to it, the statement acts as though it had received a string.
1035 If any math operation is attempted on a string or a variable or array element
1036 that has been assigned a string, an error is raised, and bc(1) resets (see the
1037 **RESET** section).
1039 Assigning strings to variables and array elements and passing them to functions
1040 are **non-portable extensions**.
1042 ## Print Statement
1044 The "expressions" in a **print** statement may also be strings. If they are, there
1045 are backslash escape sequences that are interpreted specially. What those
1046 sequences are, and what they cause to be printed, are shown below:
1048 **\\a**:   **\\a**
1050 **\\b**:   **\\b**
1052 **\\\\**:   **\\**
1054 **\\e**:   **\\**
1056 **\\f**:   **\\f**
1058 **\\n**:   **\\n**
1060 **\\q**:   **"**
1062 **\\r**:   **\\r**
1064 **\\t**:   **\\t**
1066 Any other character following a backslash causes the backslash and character to
1067 be printed as-is.
1069 Any non-string expression in a print statement shall be assigned to **last**,
1070 like any other expression that is printed.
1072 ## Stream Statement
1074 The expressions in a **stream** statement may also be strings.
1076 If a **stream** statement is given a string, it prints the string as though the
1077 string had appeared as its own statement. In other words, the **stream**
1078 statement prints strings normally, without a newline.
1080 If a **stream** statement is given a number, a copy of it is truncated and its
1081 absolute value is calculated. The result is then printed as though **obase** is
1082 **256** and each digit is interpreted as an 8-bit ASCII character, making it a
1083 byte stream.
1085 ## Order of Evaluation
1087 All expressions in a statment are evaluated left to right, except as necessary
1088 to maintain order of operations. This means, for example, assuming that **i** is
1089 equal to **0**, in the expression
1091     a[i++] = i++
1093 the first (or 0th) element of **a** is set to **1**, and **i** is equal to **2**
1094 at the end of the expression.
1096 This includes function arguments. Thus, assuming **i** is equal to **0**, this
1097 means that in the expression
1099     x(i++, i++)
1101 the first argument passed to **x()** is **0**, and the second argument is **1**,
1102 while **i** is equal to **2** before the function starts executing.
1104 # FUNCTIONS
1106 Function definitions are as follows:
1109 define I(I,...,I){
1110         auto I,...,I
1111         S;...;S
1112         return(E)
1116 Any **I** in the parameter list or **auto** list may be replaced with **I[]** to
1117 make a parameter or **auto** var an array, and any **I** in the parameter list
1118 may be replaced with **\*I[]** to make a parameter an array reference. Callers
1119 of functions that take array references should not put an asterisk in the call;
1120 they must be called with just **I[]** like normal array parameters and will be
1121 automatically converted into references.
1123 As a **non-portable extension**, the opening brace of a **define** statement may
1124 appear on the next line.
1126 As a **non-portable extension**, the return statement may also be in one of the
1127 following forms:
1129 1.      **return**
1130 2.      **return** **(** **)**
1131 3.      **return** **E**
1133 The first two, or not specifying a **return** statement, is equivalent to
1134 **return (0)**, unless the function is a **void** function (see the *Void
1135 Functions* subsection below).
1137 ## Void Functions
1139 Functions can also be **void** functions, defined as follows:
1142 define void I(I,...,I){
1143         auto I,...,I
1144         S;...;S
1145         return
1149 They can only be used as standalone expressions, where such an expression would
1150 be printed alone, except in a print statement.
1152 Void functions can only use the first two **return** statements listed above.
1153 They can also omit the return statement entirely.
1155 The word "void" is not treated as a keyword; it is still possible to have
1156 variables, arrays, and functions named **void**. The word "void" is only
1157 treated specially right after the **define** keyword.
1159 This is a **non-portable extension**.
1161 ## Array References
1163 For any array in the parameter list, if the array is declared in the form
1166 *I[]
1169 it is a **reference**. Any changes to the array in the function are reflected,
1170 when the function returns, to the array that was passed in.
1172 Other than this, all function arguments are passed by value.
1174 This is a **non-portable extension**.
1176 # LIBRARY
1178 All of the functions below, including the functions in the extended math
1179 library (see the *Extended Library* subsection below), are available when the
1180 **-l** or **-\-mathlib** command-line flags are given, except that the extended
1181 math library is not available when the **-s** option, the **-w** option, or
1182 equivalents are given.
1184 ## Standard Library
1186 The standard (see the **STANDARDS** section) defines the following functions for
1187 the math library:
1189 **s(x)**
1191 :   Returns the sine of **x**, which is assumed to be in radians.
1193     This is a transcendental function (see the *Transcendental Functions*
1194     subsection below).
1196 **c(x)**
1198 :   Returns the cosine of **x**, which is assumed to be in radians.
1200     This is a transcendental function (see the *Transcendental Functions*
1201     subsection below).
1203 **a(x)**
1205 :   Returns the arctangent of **x**, in radians.
1207     This is a transcendental function (see the *Transcendental Functions*
1208     subsection below).
1210 **l(x)**
1212 :   Returns the natural logarithm of **x**.
1214     This is a transcendental function (see the *Transcendental Functions*
1215     subsection below).
1217 **e(x)**
1219 :   Returns the mathematical constant **e** raised to the power of **x**.
1221     This is a transcendental function (see the *Transcendental Functions*
1222     subsection below).
1224 **j(x, n)**
1226 :   Returns the bessel integer order **n** (truncated) of **x**.
1228     This is a transcendental function (see the *Transcendental Functions*
1229     subsection below).
1231 ## Extended Library
1233 The extended library is *not* loaded when the **-s**/**-\-standard** or
1234 **-w**/**-\-warn** options are given since they are not part of the library
1235 defined by the standard (see the **STANDARDS** section).
1237 The extended library is a **non-portable extension**.
1239 **p(x, y)**
1241 :   Calculates **x** to the power of **y**, even if **y** is not an integer, and
1242     returns the result to the current **scale**.
1244     It is an error if **y** is negative and **x** is **0**.
1246     This is a transcendental function (see the *Transcendental Functions*
1247     subsection below).
1249 **r(x, p)**
1251 :   Returns **x** rounded to **p** decimal places according to the rounding mode
1252     round half away from **0**
1253     (https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero).
1255 **ceil(x, p)**
1257 :   Returns **x** rounded to **p** decimal places according to the rounding mode
1258     round away from **0**
1259     (https://en.wikipedia.org/wiki/Rounding#Rounding_away_from_zero).
1261 **f(x)**
1263 :   Returns the factorial of the truncated absolute value of **x**.
1265 **max(a, b)**
1267 :   Returns **a** if **a** is greater than **b**; otherwise, returns **b**.
1269 **min(a, b)**
1271 :   Returns **a** if **a** is less than **b**; otherwise, returns **b**.
1273 **perm(n, k)**
1275 :   Returns the permutation of the truncated absolute value of **n** of the
1276     truncated absolute value of **k**, if **k \<= n**. If not, it returns **0**.
1278 **comb(n, k)**
1280 :   Returns the combination of the truncated absolute value of **n** of the
1281     truncated absolute value of **k**, if **k \<= n**. If not, it returns **0**.
1283 **fib(n)**
1285 :   Returns the Fibonacci number of the truncated absolute value of **n**.
1287 **l2(x)**
1289 :   Returns the logarithm base **2** of **x**.
1291     This is a transcendental function (see the *Transcendental Functions*
1292     subsection below).
1294 **l10(x)**
1296 :   Returns the logarithm base **10** of **x**.
1298     This is a transcendental function (see the *Transcendental Functions*
1299     subsection below).
1301 **log(x, b)**
1303 :   Returns the logarithm base **b** of **x**.
1305     This is a transcendental function (see the *Transcendental Functions*
1306     subsection below).
1308 **cbrt(x)**
1310 :   Returns the cube root of **x**.
1312 **root(x, n)**
1314 :   Calculates the truncated value of **n**, **r**, and returns the **r**th root
1315     of **x** to the current **scale**.
1317     If **r** is **0** or negative, this raises an error and causes bc(1) to
1318     reset (see the **RESET** section). It also raises an error and causes bc(1)
1319     to reset if **r** is even and **x** is negative.
1321 **gcd(a, b)**
1323 :   Returns the greatest common divisor (factor) of the truncated absolute value
1324     of **a** and the truncated absolute value of **b**.
1326 **lcm(a, b)**
1328 :   Returns the least common multiple of the truncated absolute value of **a**
1329     and the truncated absolute value of **b**.
1331 **pi(p)**
1333 :   Returns **pi** to **p** decimal places.
1335     This is a transcendental function (see the *Transcendental Functions*
1336     subsection below).
1338 **t(x)**
1340 :   Returns the tangent of **x**, which is assumed to be in radians.
1342     This is a transcendental function (see the *Transcendental Functions*
1343     subsection below).
1345 **a2(y, x)**
1347 :   Returns the arctangent of **y/x**, in radians. If both **y** and **x** are
1348     equal to **0**, it raises an error and causes bc(1) to reset (see the
1349     **RESET** section). Otherwise, if **x** is greater than **0**, it returns
1350     **a(y/x)**. If **x** is less than **0**, and **y** is greater than or equal
1351     to **0**, it returns **a(y/x)+pi**. If **x** is less than **0**, and **y**
1352     is less than **0**, it returns **a(y/x)-pi**. If **x** is equal to **0**,
1353     and **y** is greater than **0**, it returns **pi/2**. If **x** is equal to
1354     **0**, and **y** is less than **0**, it returns **-pi/2**.
1356     This function is the same as the **atan2()** function in many programming
1357     languages.
1359     This is a transcendental function (see the *Transcendental Functions*
1360     subsection below).
1362 **sin(x)**
1364 :   Returns the sine of **x**, which is assumed to be in radians.
1366     This is an alias of **s(x)**.
1368     This is a transcendental function (see the *Transcendental Functions*
1369     subsection below).
1371 **cos(x)**
1373 :   Returns the cosine of **x**, which is assumed to be in radians.
1375     This is an alias of **c(x)**.
1377     This is a transcendental function (see the *Transcendental Functions*
1378     subsection below).
1380 **tan(x)**
1382 :   Returns the tangent of **x**, which is assumed to be in radians.
1384     If **x** is equal to **1** or **-1**, this raises an error and causes bc(1)
1385     to reset (see the **RESET** section).
1387     This is an alias of **t(x)**.
1389     This is a transcendental function (see the *Transcendental Functions*
1390     subsection below).
1392 **atan(x)**
1394 :   Returns the arctangent of **x**, in radians.
1396     This is an alias of **a(x)**.
1398     This is a transcendental function (see the *Transcendental Functions*
1399     subsection below).
1401 **atan2(y, x)**
1403 :   Returns the arctangent of **y/x**, in radians. If both **y** and **x** are
1404     equal to **0**, it raises an error and causes bc(1) to reset (see the
1405     **RESET** section). Otherwise, if **x** is greater than **0**, it returns
1406     **a(y/x)**. If **x** is less than **0**, and **y** is greater than or equal
1407     to **0**, it returns **a(y/x)+pi**. If **x** is less than **0**, and **y**
1408     is less than **0**, it returns **a(y/x)-pi**. If **x** is equal to **0**,
1409     and **y** is greater than **0**, it returns **pi/2**. If **x** is equal to
1410     **0**, and **y** is less than **0**, it returns **-pi/2**.
1412     This function is the same as the **atan2()** function in many programming
1413     languages.
1415     This is an alias of **a2(y, x)**.
1417     This is a transcendental function (see the *Transcendental Functions*
1418     subsection below).
1420 **r2d(x)**
1422 :   Converts **x** from radians to degrees and returns the result.
1424     This is a transcendental function (see the *Transcendental Functions*
1425     subsection below).
1427 **d2r(x)**
1429 :   Converts **x** from degrees to radians and returns the result.
1431     This is a transcendental function (see the *Transcendental Functions*
1432     subsection below).
1434 **frand(p)**
1436 :   Generates a pseudo-random integer between **0** (inclusive) and **1**
1437     (exclusive) with the number of decimal digits after the decimal point equal
1438     to the truncated absolute value of **p**. If **p** is not **0**, then
1439     calling this function will change the value of **seed**. If **p** is **0**,
1440     then **0** is returned, and **seed** is *not* changed.
1442 **ifrand(i, p)**
1444 :   Generates a pseudo-random integer that is between **0** (inclusive) and the
1445     truncated absolute value of **i** (exclusive) with the number of decimal
1446     digits after the decimal point equal to the truncated absolute value of
1447     **p**. If the absolute value of **i** is greater than or equal to **2**, and
1448     **p** is not **0**, then calling this function will change the value of
1449     **seed**; otherwise, **0** is returned, and **seed** is not changed.
1451 **i2rand(a, b)**
1453 :   Takes the truncated value of **a** and **b** and uses them as inclusive
1454     bounds to enerate a pseudo-random integer. If the difference of the
1455     truncated values of **a** and **b** is **0**, then the truncated value is
1456     returned, and **seed** is *not* changed. Otherwise, this function will
1457     change the value of **seed**.
1459 **srand(x)**
1461 :   Returns **x** with its sign flipped with probability **0.5**. In other
1462     words, it randomizes the sign of **x**.
1464 **brand()**
1466 :   Returns a random boolean value (either **0** or **1**).
1468 **band(a, b)**
1470 :   Takes the truncated absolute value of both **a** and **b** and calculates
1471     and returns the result of the bitwise **and** operation between them.
1473     If you want to use signed two's complement arguments, use **s2u(x)** to
1474     convert.
1476 **bor(a, b)**
1478 :   Takes the truncated absolute value of both **a** and **b** and calculates
1479     and returns the result of the bitwise **or** operation between them.
1481     If you want to use signed two's complement arguments, use **s2u(x)** to
1482     convert.
1484 **bxor(a, b)**
1486 :   Takes the truncated absolute value of both **a** and **b** and calculates
1487     and returns the result of the bitwise **xor** operation between them.
1489     If you want to use signed two's complement arguments, use **s2u(x)** to
1490     convert.
1492 **bshl(a, b)**
1494 :   Takes the truncated absolute value of both **a** and **b** and calculates
1495     and returns the result of **a** bit-shifted left by **b** places.
1497     If you want to use signed two's complement arguments, use **s2u(x)** to
1498     convert.
1500 **bshr(a, b)**
1502 :   Takes the truncated absolute value of both **a** and **b** and calculates
1503     and returns the truncated result of **a** bit-shifted right by **b** places.
1505     If you want to use signed two's complement arguments, use **s2u(x)** to
1506     convert.
1508 **bnotn(x, n)**
1510 :   Takes the truncated absolute value of **x** and does a bitwise not as though
1511     it has the same number of bytes as the truncated absolute value of **n**.
1513     If you want to a use signed two's complement argument, use **s2u(x)** to
1514     convert.
1516 **bnot8(x)**
1518 :   Does a bitwise not of the truncated absolute value of **x** as though it has
1519     **8** binary digits (**1** unsigned byte).
1521     If you want to a use signed two's complement argument, use **s2u(x)** to
1522     convert.
1524 **bnot16(x)**
1526 :   Does a bitwise not of the truncated absolute value of **x** as though it has
1527     **16** binary digits (**2** unsigned bytes).
1529     If you want to a use signed two's complement argument, use **s2u(x)** to
1530     convert.
1532 **bnot32(x)**
1534 :   Does a bitwise not of the truncated absolute value of **x** as though it has
1535     **32** binary digits (**4** unsigned bytes).
1537     If you want to a use signed two's complement argument, use **s2u(x)** to
1538     convert.
1540 **bnot64(x)**
1542 :   Does a bitwise not of the truncated absolute value of **x** as though it has
1543     **64** binary digits (**8** unsigned bytes).
1545     If you want to a use signed two's complement argument, use **s2u(x)** to
1546     convert.
1548 **bnot(x)**
1550 :   Does a bitwise not of the truncated absolute value of **x** as though it has
1551     the minimum number of power of two unsigned bytes.
1553     If you want to a use signed two's complement argument, use **s2u(x)** to
1554     convert.
1556 **brevn(x, n)**
1558 :   Runs a bit reversal on the truncated absolute value of **x** as though it
1559     has the same number of 8-bit bytes as the truncated absolute value of **n**.
1561     If you want to a use signed two's complement argument, use **s2u(x)** to
1562     convert.
1564 **brev8(x)**
1566 :   Runs a bit reversal on the truncated absolute value of **x** as though it
1567     has 8 binary digits (**1** unsigned byte).
1569     If you want to a use signed two's complement argument, use **s2u(x)** to
1570     convert.
1572 **brev16(x)**
1574 :   Runs a bit reversal on the truncated absolute value of **x** as though it
1575     has 16 binary digits (**2** unsigned bytes).
1577     If you want to a use signed two's complement argument, use **s2u(x)** to
1578     convert.
1580 **brev32(x)**
1582 :   Runs a bit reversal on the truncated absolute value of **x** as though it
1583     has 32 binary digits (**4** unsigned bytes).
1585     If you want to a use signed two's complement argument, use **s2u(x)** to
1586     convert.
1588 **brev64(x)**
1590 :   Runs a bit reversal on the truncated absolute value of **x** as though it
1591     has 64 binary digits (**8** unsigned bytes).
1593     If you want to a use signed two's complement argument, use **s2u(x)** to
1594     convert.
1596 **brev(x)**
1598 :   Runs a bit reversal on the truncated absolute value of **x** as though it
1599     has the minimum number of power of two unsigned bytes.
1601     If you want to a use signed two's complement argument, use **s2u(x)** to
1602     convert.
1604 **broln(x, p, n)**
1606 :   Does a left bitwise rotatation of the truncated absolute value of **x**, as
1607     though it has the same number of unsigned 8-bit bytes as the truncated
1608     absolute value of **n**, by the number of places equal to the truncated
1609     absolute value of **p** modded by the **2** to the power of the number of
1610     binary digits in **n** 8-bit bytes.
1612     If you want to a use signed two's complement argument, use **s2u(x)** to
1613     convert.
1615 **brol8(x, p)**
1617 :   Does a left bitwise rotatation of the truncated absolute value of **x**, as
1618     though it has **8** binary digits (**1** unsigned byte), by the number of
1619     places equal to the truncated absolute value of **p** modded by **2** to the
1620     power of **8**.
1622     If you want to a use signed two's complement argument, use **s2u(x)** to
1623     convert.
1625 **brol16(x, p)**
1627 :   Does a left bitwise rotatation of the truncated absolute value of **x**, as
1628     though it has **16** binary digits (**2** unsigned bytes), by the number of
1629     places equal to the truncated absolute value of **p** modded by **2** to the
1630     power of **16**.
1632     If you want to a use signed two's complement argument, use **s2u(x)** to
1633     convert.
1635 **brol32(x, p)**
1637 :   Does a left bitwise rotatation of the truncated absolute value of **x**, as
1638     though it has **32** binary digits (**4** unsigned bytes), by the number of
1639     places equal to the truncated absolute value of **p** modded by **2** to the
1640     power of **32**.
1642     If you want to a use signed two's complement argument, use **s2u(x)** to
1643     convert.
1645 **brol64(x, p)**
1647 :   Does a left bitwise rotatation of the truncated absolute value of **x**, as
1648     though it has **64** binary digits (**8** unsigned bytes), by the number of
1649     places equal to the truncated absolute value of **p** modded by **2** to the
1650     power of **64**.
1652     If you want to a use signed two's complement argument, use **s2u(x)** to
1653     convert.
1655 **brol(x, p)**
1657 :   Does a left bitwise rotatation of the truncated absolute value of **x**, as
1658     though it has the minimum number of power of two unsigned 8-bit bytes, by
1659     the number of places equal to the truncated absolute value of **p** modded
1660     by 2 to the power of the number of binary digits in the minimum number of
1661     8-bit bytes.
1663     If you want to a use signed two's complement argument, use **s2u(x)** to
1664     convert.
1666 **brorn(x, p, n)**
1668 :   Does a right bitwise rotatation of the truncated absolute value of **x**, as
1669     though it has the same number of unsigned 8-bit bytes as the truncated
1670     absolute value of **n**, by the number of places equal to the truncated
1671     absolute value of **p** modded by the **2** to the power of the number of
1672     binary digits in **n** 8-bit bytes.
1674     If you want to a use signed two's complement argument, use **s2u(x)** to
1675     convert.
1677 **bror8(x, p)**
1679 :   Does a right bitwise rotatation of the truncated absolute value of **x**, as
1680     though it has **8** binary digits (**1** unsigned byte), by the number of
1681     places equal to the truncated absolute value of **p** modded by **2** to the
1682     power of **8**.
1684     If you want to a use signed two's complement argument, use **s2u(x)** to
1685     convert.
1687 **bror16(x, p)**
1689 :   Does a right bitwise rotatation of the truncated absolute value of **x**, as
1690     though it has **16** binary digits (**2** unsigned bytes), by the number of
1691     places equal to the truncated absolute value of **p** modded by **2** to the
1692     power of **16**.
1694     If you want to a use signed two's complement argument, use **s2u(x)** to
1695     convert.
1697 **bror32(x, p)**
1699 :   Does a right bitwise rotatation of the truncated absolute value of **x**, as
1700     though it has **32** binary digits (**2** unsigned bytes), by the number of
1701     places equal to the truncated absolute value of **p** modded by **2** to the
1702     power of **32**.
1704     If you want to a use signed two's complement argument, use **s2u(x)** to
1705     convert.
1707 **bror64(x, p)**
1709 :   Does a right bitwise rotatation of the truncated absolute value of **x**, as
1710     though it has **64** binary digits (**2** unsigned bytes), by the number of
1711     places equal to the truncated absolute value of **p** modded by **2** to the
1712     power of **64**.
1714     If you want to a use signed two's complement argument, use **s2u(x)** to
1715     convert.
1717 **bror(x, p)**
1719 :   Does a right bitwise rotatation of the truncated absolute value of **x**, as
1720     though it has the minimum number of power of two unsigned 8-bit bytes, by
1721     the number of places equal to the truncated absolute value of **p** modded
1722     by 2 to the power of the number of binary digits in the minimum number of
1723     8-bit bytes.
1725     If you want to a use signed two's complement argument, use **s2u(x)** to
1726     convert.
1728 **bmodn(x, n)**
1730 :   Returns the modulus of the truncated absolute value of **x** by **2** to the
1731     power of the multiplication of the truncated absolute value of **n** and
1732     **8**.
1734     If you want to a use signed two's complement argument, use **s2u(x)** to
1735     convert.
1737 **bmod8(x, n)**
1739 :   Returns the modulus of the truncated absolute value of **x** by **2** to the
1740     power of **8**.
1742     If you want to a use signed two's complement argument, use **s2u(x)** to
1743     convert.
1745 **bmod16(x, n)**
1747 :   Returns the modulus of the truncated absolute value of **x** by **2** to the
1748     power of **16**.
1750     If you want to a use signed two's complement argument, use **s2u(x)** to
1751     convert.
1753 **bmod32(x, n)**
1755 :   Returns the modulus of the truncated absolute value of **x** by **2** to the
1756     power of **32**.
1758     If you want to a use signed two's complement argument, use **s2u(x)** to
1759     convert.
1761 **bmod64(x, n)**
1763 :   Returns the modulus of the truncated absolute value of **x** by **2** to the
1764     power of **64**.
1766     If you want to a use signed two's complement argument, use **s2u(x)** to
1767     convert.
1769 **bunrev(t)**
1771 :   Assumes **t** is a bitwise-reversed number with an extra set bit one place
1772     more significant than the real most significant bit (which was the least
1773     significant bit in the original number). This number is reversed and
1774     returned without the extra set bit.
1776     This function is used to implement other bitwise functions; it is not meant
1777     to be used by users, but it can be.
1779 **plz(x)**
1781 :   If **x** is not equal to **0** and greater that **-1** and less than **1**,
1782     it is printed with a leading zero, regardless of the use of the **-z**
1783     option (see the **OPTIONS** section) and without a trailing newline.
1785     Otherwise, **x** is printed normally, without a trailing newline.
1787 **plznl(x)**
1789 :   If **x** is not equal to **0** and greater that **-1** and less than **1**,
1790     it is printed with a leading zero, regardless of the use of the **-z**
1791     option (see the **OPTIONS** section) and with a trailing newline.
1793     Otherwise, **x** is printed normally, with a trailing newline.
1795 **pnlz(x)**
1797 :   If **x** is not equal to **0** and greater that **-1** and less than **1**,
1798     it is printed without a leading zero, regardless of the use of the **-z**
1799     option (see the **OPTIONS** section) and without a trailing newline.
1801     Otherwise, **x** is printed normally, without a trailing newline.
1803 **pnlznl(x)**
1805 :   If **x** is not equal to **0** and greater that **-1** and less than **1**,
1806     it is printed without a leading zero, regardless of the use of the **-z**
1807     option (see the **OPTIONS** section) and with a trailing newline.
1809     Otherwise, **x** is printed normally, with a trailing newline.
1811 **ubytes(x)**
1813 :   Returns the numbers of unsigned integer bytes required to hold the truncated
1814     absolute value of **x**.
1816 **sbytes(x)**
1818 :   Returns the numbers of signed, two's-complement integer bytes required to
1819     hold the truncated value of **x**.
1821 **s2u(x)**
1823 :   Returns **x** if it is non-negative. If it *is* negative, then it calculates
1824     what **x** would be as a 2's-complement signed integer and returns the
1825     non-negative integer that would have the same representation in binary.
1827 **s2un(x,n)**
1829 :   Returns **x** if it is non-negative. If it *is* negative, then it calculates
1830     what **x** would be as a 2's-complement signed integer with **n** bytes and
1831     returns the non-negative integer that would have the same representation in
1832     binary. If **x** cannot fit into **n** 2's-complement signed bytes, it is
1833     truncated to fit.
1835 **hex(x)**
1837 :   Outputs the hexadecimal (base **16**) representation of **x**.
1839     This is a **void** function (see the *Void Functions* subsection of the
1840     **FUNCTIONS** section).
1842 **binary(x)**
1844 :   Outputs the binary (base **2**) representation of **x**.
1846     This is a **void** function (see the *Void Functions* subsection of the
1847     **FUNCTIONS** section).
1849 **output(x, b)**
1851 :   Outputs the base **b** representation of **x**.
1853     This is a **void** function (see the *Void Functions* subsection of the
1854     **FUNCTIONS** section).
1856 **uint(x)**
1858 :   Outputs the representation, in binary and hexadecimal, of **x** as an
1859     unsigned integer in as few power of two bytes as possible. Both outputs are
1860     split into bytes separated by spaces.
1862     If **x** is not an integer or is negative, an error message is printed
1863     instead, but bc(1) is not reset (see the **RESET** section).
1865     This is a **void** function (see the *Void Functions* subsection of the
1866     **FUNCTIONS** section).
1868 **int(x)**
1870 :   Outputs the representation, in binary and hexadecimal, of **x** as a signed,
1871     two's-complement integer in as few power of two bytes as possible. Both
1872     outputs are split into bytes separated by spaces.
1874     If **x** is not an integer, an error message is printed instead, but bc(1)
1875     is not reset (see the **RESET** section).
1877     This is a **void** function (see the *Void Functions* subsection of the
1878     **FUNCTIONS** section).
1880 **uintn(x, n)**
1882 :   Outputs the representation, in binary and hexadecimal, of **x** as an
1883     unsigned integer in **n** bytes. Both outputs are split into bytes separated
1884     by spaces.
1886     If **x** is not an integer, is negative, or cannot fit into **n** bytes, an
1887     error message is printed instead, but bc(1) is not reset (see the **RESET**
1888     section).
1890     This is a **void** function (see the *Void Functions* subsection of the
1891     **FUNCTIONS** section).
1893 **intn(x, n)**
1895 :   Outputs the representation, in binary and hexadecimal, of **x** as a signed,
1896     two's-complement integer in **n** bytes. Both outputs are split into bytes
1897     separated by spaces.
1899     If **x** is not an integer or cannot fit into **n** bytes, an error message
1900     is printed instead, but bc(1) is not reset (see the **RESET** section).
1902     This is a **void** function (see the *Void Functions* subsection of the
1903     **FUNCTIONS** section).
1905 **uint8(x)**
1907 :   Outputs the representation, in binary and hexadecimal, of **x** as an
1908     unsigned integer in **1** byte. Both outputs are split into bytes separated
1909     by spaces.
1911     If **x** is not an integer, is negative, or cannot fit into **1** byte, an
1912     error message is printed instead, but bc(1) is not reset (see the **RESET**
1913     section).
1915     This is a **void** function (see the *Void Functions* subsection of the
1916     **FUNCTIONS** section).
1918 **int8(x)**
1920 :   Outputs the representation, in binary and hexadecimal, of **x** as a signed,
1921     two's-complement integer in **1** byte. Both outputs are split into bytes
1922     separated by spaces.
1924     If **x** is not an integer or cannot fit into **1** byte, an error message
1925     is printed instead, but bc(1) is not reset (see the **RESET** section).
1927     This is a **void** function (see the *Void Functions* subsection of the
1928     **FUNCTIONS** section).
1930 **uint16(x)**
1932 :   Outputs the representation, in binary and hexadecimal, of **x** as an
1933     unsigned integer in **2** bytes. Both outputs are split into bytes separated
1934     by spaces.
1936     If **x** is not an integer, is negative, or cannot fit into **2** bytes, an
1937     error message is printed instead, but bc(1) is not reset (see the **RESET**
1938     section).
1940     This is a **void** function (see the *Void Functions* subsection of the
1941     **FUNCTIONS** section).
1943 **int16(x)**
1945 :   Outputs the representation, in binary and hexadecimal, of **x** as a signed,
1946     two's-complement integer in **2** bytes. Both outputs are split into bytes
1947     separated by spaces.
1949     If **x** is not an integer or cannot fit into **2** bytes, an error message
1950     is printed instead, but bc(1) is not reset (see the **RESET** section).
1952     This is a **void** function (see the *Void Functions* subsection of the
1953     **FUNCTIONS** section).
1955 **uint32(x)**
1957 :   Outputs the representation, in binary and hexadecimal, of **x** as an
1958     unsigned integer in **4** bytes. Both outputs are split into bytes separated
1959     by spaces.
1961     If **x** is not an integer, is negative, or cannot fit into **4** bytes, an
1962     error message is printed instead, but bc(1) is not reset (see the **RESET**
1963     section).
1965     This is a **void** function (see the *Void Functions* subsection of the
1966     **FUNCTIONS** section).
1968 **int32(x)**
1970 :   Outputs the representation, in binary and hexadecimal, of **x** as a signed,
1971     two's-complement integer in **4** bytes. Both outputs are split into bytes
1972     separated by spaces.
1974     If **x** is not an integer or cannot fit into **4** bytes, an error message
1975     is printed instead, but bc(1) is not reset (see the **RESET** section).
1977     This is a **void** function (see the *Void Functions* subsection of the
1978     **FUNCTIONS** section).
1980 **uint64(x)**
1982 :   Outputs the representation, in binary and hexadecimal, of **x** as an
1983     unsigned integer in **8** bytes. Both outputs are split into bytes separated
1984     by spaces.
1986     If **x** is not an integer, is negative, or cannot fit into **8** bytes, an
1987     error message is printed instead, but bc(1) is not reset (see the **RESET**
1988     section).
1990     This is a **void** function (see the *Void Functions* subsection of the
1991     **FUNCTIONS** section).
1993 **int64(x)**
1995 :   Outputs the representation, in binary and hexadecimal, of **x** as a signed,
1996     two's-complement integer in **8** bytes. Both outputs are split into bytes
1997     separated by spaces.
1999     If **x** is not an integer or cannot fit into **8** bytes, an error message
2000     is printed instead, but bc(1) is not reset (see the **RESET** section).
2002     This is a **void** function (see the *Void Functions* subsection of the
2003     **FUNCTIONS** section).
2005 **hex_uint(x, n)**
2007 :   Outputs the representation of the truncated absolute value of **x** as an
2008     unsigned integer in hexadecimal using **n** bytes. Not all of the value will
2009     be output if **n** is too small.
2011     This is a **void** function (see the *Void Functions* subsection of the
2012     **FUNCTIONS** section).
2014 **binary_uint(x, n)**
2016 :   Outputs the representation of the truncated absolute value of **x** as an
2017     unsigned integer in binary using **n** bytes. Not all of the value will be
2018     output if **n** is too small.
2020     This is a **void** function (see the *Void Functions* subsection of the
2021     **FUNCTIONS** section).
2023 **output_uint(x, n)**
2025 :   Outputs the representation of the truncated absolute value of **x** as an
2026     unsigned integer in the current **obase** (see the **SYNTAX** section) using
2027     **n** bytes. Not all of the value will be output if **n** is too small.
2029     This is a **void** function (see the *Void Functions* subsection of the
2030     **FUNCTIONS** section).
2032 **output_byte(x, i)**
2034 :   Outputs byte **i** of the truncated absolute value of **x**, where **0** is
2035     the least significant byte and **number_of_bytes - 1** is the most
2036     significant byte.
2038     This is a **void** function (see the *Void Functions* subsection of the
2039     **FUNCTIONS** section).
2041 ## Transcendental Functions
2043 All transcendental functions can return slightly inaccurate results, up to 1 ULP
2044 (https://en.wikipedia.org/wiki/Unit_in_the_last_place). This is unavoidable, and
2045 the  article at https://people.eecs.berkeley.edu/~wkahan/LOG10HAF.TXT explains
2046 why it is impossible and unnecessary to calculate exact results for the
2047 transcendental functions.
2049 Because of the possible inaccuracy, I recommend that users call those functions
2050 with the precision (**scale**) set to at least 1 higher than is necessary. If
2051 exact results are *absolutely* required, users can double the precision
2052 (**scale**) and then truncate.
2054 The transcendental functions in the standard math library are:
2056 * **s(x)**
2057 * **c(x)**
2058 * **a(x)**
2059 * **l(x)**
2060 * **e(x)**
2061 * **j(x, n)**
2063 The transcendental functions in the extended math library are:
2065 * **l2(x)**
2066 * **l10(x)**
2067 * **log(x, b)**
2068 * **pi(p)**
2069 * **t(x)**
2070 * **a2(y, x)**
2071 * **sin(x)**
2072 * **cos(x)**
2073 * **tan(x)**
2074 * **atan(x)**
2075 * **atan2(y, x)**
2076 * **r2d(x)**
2077 * **d2r(x)**
2079 # RESET
2081 When bc(1) encounters an error or a signal that it has a non-default handler
2082 for, it resets. This means that several things happen.
2084 First, any functions that are executing are stopped and popped off the stack.
2085 The behavior is not unlike that of exceptions in programming languages. Then
2086 the execution point is set so that any code waiting to execute (after all
2087 functions returned) is skipped.
2089 Thus, when bc(1) resets, it skips any remaining code waiting to be executed.
2090 Then, if it is interactive mode, and the error was not a fatal error (see the
2091 **EXIT STATUS** section), it asks for more input; otherwise, it exits with the
2092 appropriate return code.
2094 Note that this reset behavior is different from the GNU bc(1), which attempts to
2095 start executing the statement right after the one that caused an error.
2097 # PERFORMANCE
2099 Most bc(1) implementations use **char** types to calculate the value of **1**
2100 decimal digit at a time, but that can be slow. This bc(1) does something
2101 different.
2103 It uses large integers to calculate more than **1** decimal digit at a time. If
2104 built in a environment where **BC_LONG_BIT** (see the **LIMITS** section) is
2105 **64**, then each integer has **9** decimal digits. If built in an environment
2106 where **BC_LONG_BIT** is **32** then each integer has **4** decimal digits. This
2107 value (the number of decimal digits per large integer) is called
2108 **BC_BASE_DIGS**.
2110 The actual values of **BC_LONG_BIT** and **BC_BASE_DIGS** can be queried with
2111 the **limits** statement.
2113 In addition, this bc(1) uses an even larger integer for overflow checking. This
2114 integer type depends on the value of **BC_LONG_BIT**, but is always at least
2115 twice as large as the integer type used to store digits.
2117 # LIMITS
2119 The following are the limits on bc(1):
2121 **BC_LONG_BIT**
2123 :   The number of bits in the **long** type in the environment where bc(1) was
2124     built. This determines how many decimal digits can be stored in a single
2125     large integer (see the **PERFORMANCE** section).
2127 **BC_BASE_DIGS**
2129 :   The number of decimal digits per large integer (see the **PERFORMANCE**
2130     section). Depends on **BC_LONG_BIT**.
2132 **BC_BASE_POW**
2134 :   The max decimal number that each large integer can store (see
2135     **BC_BASE_DIGS**) plus **1**. Depends on **BC_BASE_DIGS**.
2137 **BC_OVERFLOW_MAX**
2139 :   The max number that the overflow type (see the **PERFORMANCE** section) can
2140     hold. Depends on **BC_LONG_BIT**.
2142 **BC_BASE_MAX**
2144 :   The maximum output base. Set at **BC_BASE_POW**.
2146 **BC_DIM_MAX**
2148 :   The maximum size of arrays. Set at **SIZE_MAX-1**.
2150 **BC_SCALE_MAX**
2152 :   The maximum **scale**. Set at **BC_OVERFLOW_MAX-1**.
2154 **BC_STRING_MAX**
2156 :   The maximum length of strings. Set at **BC_OVERFLOW_MAX-1**.
2158 **BC_NAME_MAX**
2160 :   The maximum length of identifiers. Set at **BC_OVERFLOW_MAX-1**.
2162 **BC_NUM_MAX**
2164 :   The maximum length of a number (in decimal digits), which includes digits
2165     after the decimal point. Set at **BC_OVERFLOW_MAX-1**.
2167 **BC_RAND_MAX**
2169 :   The maximum integer (inclusive) returned by the **rand()** operand. Set at
2170     **2\^BC_LONG_BIT-1**.
2172 Exponent
2174 :   The maximum allowable exponent (positive or negative). Set at
2175     **BC_OVERFLOW_MAX**.
2177 Number of vars
2179 :   The maximum number of vars/arrays. Set at **SIZE_MAX-1**.
2181 The actual values can be queried with the **limits** statement.
2183 These limits are meant to be effectively non-existent; the limits are so large
2184 (at least on 64-bit machines) that there should not be any point at which they
2185 become a problem. In fact, memory should be exhausted before these limits should
2186 be hit.
2188 # ENVIRONMENT VARIABLES
2190 As **non-portable extensions**, bc(1) recognizes the following environment
2191 variables:
2193 **POSIXLY_CORRECT**
2195 :   If this variable exists (no matter the contents), bc(1) behaves as if
2196     the **-s** option was given.
2198 **BC_ENV_ARGS**
2200 :   This is another way to give command-line arguments to bc(1). They should be
2201     in the same format as all other command-line arguments. These are always
2202     processed first, so any files given in **BC_ENV_ARGS** will be processed
2203     before arguments and files given on the command-line. This gives the user
2204     the ability to set up "standard" options and files to be used at every
2205     invocation. The most useful thing for such files to contain would be useful
2206     functions that the user might want every time bc(1) runs.
2208     The code that parses **BC_ENV_ARGS** will correctly handle quoted arguments,
2209     but it does not understand escape sequences. For example, the string
2210     **"/home/gavin/some bc file.bc"** will be correctly parsed, but the string
2211     **"/home/gavin/some \"bc\" file.bc"** will include the backslashes.
2213     The quote parsing will handle either kind of quotes, **'** or **"**. Thus,
2214     if you have a file with any number of single quotes in the name, you can use
2215     double quotes as the outside quotes, as in **"some 'bc' file.bc"**, and vice
2216     versa if you have a file with double quotes. However, handling a file with
2217     both kinds of quotes in **BC_ENV_ARGS** is not supported due to the
2218     complexity of the parsing, though such files are still supported on the
2219     command-line where the parsing is done by the shell.
2221 **BC_LINE_LENGTH**
2223 :   If this environment variable exists and contains an integer that is greater
2224     than **1** and is less than **UINT16_MAX** (**2\^16-1**), bc(1) will output
2225     lines to that length, including the backslash (**\\**). The default line
2226     length is **70**.
2228     The special value of **0** will disable line length checking and print
2229     numbers without regard to line length and without backslashes and newlines.
2231 **BC_BANNER**
2233 :   If this environment variable exists and contains an integer, then a non-zero
2234     value activates the copyright banner when bc(1) is in interactive mode,
2235     while zero deactivates it.
2237     If bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section),
2238     then this environment variable has no effect because bc(1) does not print
2239     the banner when not in interactive mode.
2241     This environment variable overrides the default, which can be queried with
2242     the **-h** or **-\-help** options.
2244 **BC_SIGINT_RESET**
2246 :   If bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section),
2247     then this environment variable has no effect because bc(1) exits on
2248     **SIGINT** when not in interactive mode.
2250     However, when bc(1) is in interactive mode, then if this environment
2251     variable exists and contains an integer, a non-zero value makes bc(1) reset
2252     on **SIGINT**, rather than exit, and zero makes bc(1) exit. If this
2253     environment variable exists and is *not* an integer, then bc(1) will exit on
2254     **SIGINT**.
2256     This environment variable overrides the default, which can be queried with
2257     the **-h** or **-\-help** options.
2259 **BC_TTY_MODE**
2261 :   If TTY mode is *not* available (see the **TTY MODE** section), then this
2262     environment variable has no effect.
2264     However, when TTY mode is available, then if this environment variable
2265     exists and contains an integer, then a non-zero value makes bc(1) use TTY
2266     mode, and zero makes bc(1) not use TTY mode.
2268     This environment variable overrides the default, which can be queried with
2269     the **-h** or **-\-help** options.
2271 **BC_PROMPT**
2273 :   If TTY mode is *not* available (see the **TTY MODE** section), then this
2274     environment variable has no effect.
2276     However, when TTY mode is available, then if this environment variable
2277     exists and contains an integer, a non-zero value makes bc(1) use a prompt,
2278     and zero or a non-integer makes bc(1) not use a prompt. If this environment
2279     variable does not exist and **BC_TTY_MODE** does, then the value of the
2280     **BC_TTY_MODE** environment variable is used.
2282     This environment variable and the **BC_TTY_MODE** environment variable
2283     override the default, which can be queried with the **-h** or **-\-help**
2284     options.
2286 **BC_EXPR_EXIT**
2288 :   If any expressions or expression files are given on the command-line with
2289     **-e**, **-\-expression**, **-f**, or **-\-file**, then if this environment
2290     variable exists and contains an integer, a non-zero value makes bc(1) exit
2291     after executing the expressions and expression files, and a zero value makes
2292     bc(1) not exit.
2294     This environment variable overrides the default, which can be queried with
2295     the **-h** or **-\-help** options.
2297 **BC_DIGIT_CLAMP**
2299 :   When parsing numbers and if this environment variable exists and contains an
2300     integer, a non-zero value makes bc(1) clamp digits that are greater than or
2301     equal to the current **ibase** so that all such digits are considered equal
2302     to the **ibase** minus 1, and a zero value disables such clamping so that
2303     those digits are always equal to their value, which is multiplied by the
2304     power of the **ibase**.
2306     This never applies to single-digit numbers, as per the standard (see the
2307     **STANDARDS** section).
2309     This environment variable overrides the default, which can be queried with
2310     the **-h** or **-\-help** options.
2312 # EXIT STATUS
2314 bc(1) returns the following exit statuses:
2316 **0**
2318 :   No error.
2320 **1**
2322 :   A math error occurred. This follows standard practice of using **1** for
2323     expected errors, since math errors will happen in the process of normal
2324     execution.
2326     Math errors include divide by **0**, taking the square root of a negative
2327     number, using a negative number as a bound for the pseudo-random number
2328     generator, attempting to convert a negative number to a hardware integer,
2329     overflow when converting a number to a hardware integer, overflow when
2330     calculating the size of a number, and attempting to use a non-integer where
2331     an integer is required.
2333     Converting to a hardware integer happens for the second operand of the power
2334     (**\^**), places (**\@**), left shift (**\<\<**), and right shift (**\>\>**)
2335     operators and their corresponding assignment operators.
2337 **2**
2339 :   A parse error occurred.
2341     Parse errors include unexpected **EOF**, using an invalid character, failing
2342     to find the end of a string or comment, using a token where it is invalid,
2343     giving an invalid expression, giving an invalid print statement, giving an
2344     invalid function definition, attempting to assign to an expression that is
2345     not a named expression (see the *Named Expressions* subsection of the
2346     **SYNTAX** section), giving an invalid **auto** list, having a duplicate
2347     **auto**/function parameter, failing to find the end of a code block,
2348     attempting to return a value from a **void** function, attempting to use a
2349     variable as a reference, and using any extensions when the option **-s** or
2350     any equivalents were given.
2352 **3**
2354 :   A runtime error occurred.
2356     Runtime errors include assigning an invalid number to any global (**ibase**,
2357     **obase**, or **scale**), giving a bad expression to a **read()** call,
2358     calling **read()** inside of a **read()** call, type errors, passing the
2359     wrong number of arguments to functions, attempting to call an undefined
2360     function, and attempting to use a **void** function call as a value in an
2361     expression.
2363 **4**
2365 :   A fatal error occurred.
2367     Fatal errors include memory allocation errors, I/O errors, failing to open
2368     files, attempting to use files that do not have only ASCII characters (bc(1)
2369     only accepts ASCII characters), attempting to open a directory as a file,
2370     and giving invalid command-line options.
2372 The exit status **4** is special; when a fatal error occurs, bc(1) always exits
2373 and returns **4**, no matter what mode bc(1) is in.
2375 The other statuses will only be returned when bc(1) is not in interactive mode
2376 (see the **INTERACTIVE MODE** section), since bc(1) resets its state (see the
2377 **RESET** section) and accepts more input when one of those errors occurs in
2378 interactive mode. This is also the case when interactive mode is forced by the
2379 **-i** flag or **-\-interactive** option.
2381 These exit statuses allow bc(1) to be used in shell scripting with error
2382 checking, and its normal behavior can be forced by using the **-i** flag or
2383 **-\-interactive** option.
2385 # INTERACTIVE MODE
2387 Per the standard (see the **STANDARDS** section), bc(1) has an interactive mode
2388 and a non-interactive mode. Interactive mode is turned on automatically when
2389 both **stdin** and **stdout** are hooked to a terminal, but the **-i** flag and
2390 **-\-interactive** option can turn it on in other situations.
2392 In interactive mode, bc(1) attempts to recover from errors (see the **RESET**
2393 section), and in normal execution, flushes **stdout** as soon as execution is
2394 done for the current input. bc(1) may also reset on **SIGINT** instead of exit,
2395 depending on the contents of, or default for, the **BC_SIGINT_RESET**
2396 environment variable (see the **ENVIRONMENT VARIABLES** section).
2398 # TTY MODE
2400 If **stdin**, **stdout**, and **stderr** are all connected to a TTY, then "TTY
2401 mode" is considered to be available, and thus, bc(1) can turn on TTY mode,
2402 subject to some settings.
2404 If there is the environment variable **BC_TTY_MODE** in the environment (see the
2405 **ENVIRONMENT VARIABLES** section), then if that environment variable contains a
2406 non-zero integer, bc(1) will turn on TTY mode when **stdin**, **stdout**, and
2407 **stderr** are all connected to a TTY. If the **BC_TTY_MODE** environment
2408 variable exists but is *not* a non-zero integer, then bc(1) will not turn TTY
2409 mode on.
2411 If the environment variable **BC_TTY_MODE** does *not* exist, the default
2412 setting is used. The default setting can be queried with the **-h** or
2413 **-\-help** options.
2415 TTY mode is different from interactive mode because interactive mode is required
2416 in the bc(1) standard (see the **STANDARDS** section), and interactive mode
2417 requires only **stdin** and **stdout** to be connected to a terminal.
2419 ## Command-Line History
2421 Command-line history is only enabled if TTY mode is, i.e., that **stdin**,
2422 **stdout**, and **stderr** are connected to a TTY and the **BC_TTY_MODE**
2423 environment variable (see the **ENVIRONMENT VARIABLES** section) and its default
2424 do not disable TTY mode. See the **COMMAND LINE HISTORY** section for more
2425 information.
2427 ## Prompt
2429 If TTY mode is available, then a prompt can be enabled. Like TTY mode itself, it
2430 can be turned on or off with an environment variable: **BC_PROMPT** (see the
2431 **ENVIRONMENT VARIABLES** section).
2433 If the environment variable **BC_PROMPT** exists and is a non-zero integer, then
2434 the prompt is turned on when **stdin**, **stdout**, and **stderr** are connected
2435 to a TTY and the **-P** and **-\-no-prompt** options were not used. The read
2436 prompt will be turned on under the same conditions, except that the **-R** and
2437 **-\-no-read-prompt** options must also not be used.
2439 However, if **BC_PROMPT** does not exist, the prompt can be enabled or disabled
2440 with the **BC_TTY_MODE** environment variable, the **-P** and **-\-no-prompt**
2441 options, and the **-R** and **-\-no-read-prompt** options. See the **ENVIRONMENT
2442 VARIABLES** and **OPTIONS** sections for more details.
2444 # SIGNAL HANDLING
2446 Sending a **SIGINT** will cause bc(1) to do one of two things.
2448 If bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section), or
2449 the **BC_SIGINT_RESET** environment variable (see the **ENVIRONMENT VARIABLES**
2450 section), or its default, is either not an integer or it is zero, bc(1) will
2451 exit.
2453 However, if bc(1) is in interactive mode, and the **BC_SIGINT_RESET** or its
2454 default is an integer and non-zero, then bc(1) will stop executing the current
2455 input and reset (see the **RESET** section) upon receiving a **SIGINT**.
2457 Note that "current input" can mean one of two things. If bc(1) is processing
2458 input from **stdin** in interactive mode, it will ask for more input. If bc(1)
2459 is processing input from a file in interactive mode, it will stop processing the
2460 file and start processing the next file, if one exists, or ask for input from
2461 **stdin** if no other file exists.
2463 This means that if a **SIGINT** is sent to bc(1) as it is executing a file, it
2464 can seem as though bc(1) did not respond to the signal since it will immediately
2465 start executing the next file. This is by design; most files that users execute
2466 when interacting with bc(1) have function definitions, which are quick to parse.
2467 If a file takes a long time to execute, there may be a bug in that file. The
2468 rest of the files could still be executed without problem, allowing the user to
2469 continue.
2471 **SIGTERM** and **SIGQUIT** cause bc(1) to clean up and exit, and it uses the
2472 default handler for all other signals. The one exception is **SIGHUP**; in that
2473 case, and only when bc(1) is in TTY mode (see the **TTY MODE** section), a
2474 **SIGHUP** will cause bc(1) to clean up and exit.
2476 # COMMAND LINE HISTORY
2478 bc(1) supports interactive command-line editing.
2480 If bc(1) can be in TTY mode (see the **TTY MODE** section), history can be
2481 enabled. This means that command-line history can only be enabled when
2482 **stdin**, **stdout**, and **stderr** are all connected to a TTY.
2484 Like TTY mode itself, it can be turned on or off with the environment variable
2485 **BC_TTY_MODE** (see the **ENVIRONMENT VARIABLES** section).
2487 If history is enabled, previous lines can be recalled and edited with the arrow
2488 keys.
2490 **Note**: tabs are converted to 8 spaces.
2492 # LOCALES
2494 This bc(1) ships with support for adding error messages for different locales
2495 and thus, supports **LC_MESSAGES**.
2497 # SEE ALSO
2499 dc(1)
2501 # STANDARDS
2503 bc(1) is compliant with the IEEE Std 1003.1-2017 (“POSIX.1-2017”) specification
2504 at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html . The
2505 flags **-efghiqsvVw**, all long options, and the extensions noted above are
2506 extensions to that specification.
2508 In addition, the behavior of the **quit** implements an interpretation of that
2509 specification that is different from all known implementations. For more
2510 information see the **Statements** subsection of the **SYNTAX** section.
2512 Note that the specification explicitly says that bc(1) only accepts numbers that
2513 use a period (**.**) as a radix point, regardless of the value of
2514 **LC_NUMERIC**.
2516 This bc(1) supports error messages for different locales, and thus, it supports
2517 **LC_MESSAGES**.
2519 # BUGS
2521 Before version **6.1.0**, this bc(1) had incorrect behavior for the **quit**
2522 statement.
2524 No other bugs are known. Report bugs at https://git.gavinhoward.com/gavin/bc .
2526 # AUTHORS
2528 Gavin D. Howard <gavin@gavinhoward.com> and contributors.