13aa99676SStefan Eßer<!--- 23aa99676SStefan Eßer 33aa99676SStefan EßerSPDX-License-Identifier: BSD-2-Clause 43aa99676SStefan Eßer 5d101cdd6SStefan EßerCopyright (c) 2018-2023 Gavin D. Howard and contributors. 63aa99676SStefan Eßer 73aa99676SStefan EßerRedistribution and use in source and binary forms, with or without 83aa99676SStefan Eßermodification, are permitted provided that the following conditions are met: 93aa99676SStefan Eßer 103aa99676SStefan Eßer* Redistributions of source code must retain the above copyright notice, this 113aa99676SStefan Eßer list of conditions and the following disclaimer. 123aa99676SStefan Eßer 133aa99676SStefan Eßer* Redistributions in binary form must reproduce the above copyright notice, 143aa99676SStefan Eßer this list of conditions and the following disclaimer in the documentation 153aa99676SStefan Eßer and/or other materials provided with the distribution. 163aa99676SStefan Eßer 173aa99676SStefan EßerTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 183aa99676SStefan EßerAND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 193aa99676SStefan EßerIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 203aa99676SStefan EßerARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 213aa99676SStefan EßerLIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 223aa99676SStefan EßerCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 233aa99676SStefan EßerSUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 243aa99676SStefan EßerINTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 253aa99676SStefan EßerCONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 263aa99676SStefan EßerARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 273aa99676SStefan EßerPOSSIBILITY OF SUCH DAMAGE. 283aa99676SStefan Eßer 293aa99676SStefan Eßer--> 303aa99676SStefan Eßer 313aa99676SStefan Eßer# NAME 323aa99676SStefan Eßer 3350696a6eSStefan Eßerbc - arbitrary-precision decimal arithmetic language and calculator 343aa99676SStefan Eßer 353aa99676SStefan Eßer# SYNOPSIS 363aa99676SStefan Eßer 37d101cdd6SStefan Eßer**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*...] 383aa99676SStefan Eßer 393aa99676SStefan Eßer# DESCRIPTION 403aa99676SStefan Eßer 413aa99676SStefan Eßerbc(1) is an interactive processor for a language first standardized in 1991 by 42d101cdd6SStefan EßerPOSIX. (See the **STANDARDS** section.) The language provides unlimited 43d101cdd6SStefan Eßerprecision decimal arithmetic and is somewhat C-like, but there are differences. 44d101cdd6SStefan EßerSuch differences will be noted in this document. 453aa99676SStefan Eßer 463aa99676SStefan EßerAfter parsing and handling options, this bc(1) reads any files given on the 473aa99676SStefan Eßercommand line and executes them before reading from **stdin**. 483aa99676SStefan Eßer 49d101cdd6SStefan EßerThis bc(1) is a drop-in replacement for *any* bc(1), including (and 50d101cdd6SStefan Eßerespecially) the GNU bc(1). It also has many extensions and extra features beyond 51d101cdd6SStefan Eßerother implementations. 5244d4804dSStefan Eßer 5344d4804dSStefan Eßer**Note**: If running this bc(1) on *any* script meant for another bc(1) gives a 5444d4804dSStefan Eßerparse error, it is probably because a word this bc(1) reserves as a keyword is 5544d4804dSStefan Eßerused as the name of a function, variable, or array. To fix that, use the 5644d4804dSStefan Eßercommand-line option **-r** *keyword*, where *keyword* is the keyword that is 5744d4804dSStefan Eßerused as a name in the script. For more information, see the **OPTIONS** section. 5844d4804dSStefan Eßer 5944d4804dSStefan EßerIf parsing scripts meant for other bc(1) implementations still does not work, 6044d4804dSStefan Eßerthat is a bug and should be reported. See the **BUGS** section. 613aa99676SStefan Eßer 623aa99676SStefan Eßer# OPTIONS 633aa99676SStefan Eßer 643aa99676SStefan EßerThe following are the options that bc(1) accepts. 653aa99676SStefan Eßer 66d101cdd6SStefan Eßer**-C**, **-\-no-digit-clamp** 67d101cdd6SStefan Eßer 68d101cdd6SStefan Eßer: Disables clamping of digits greater than or equal to the current **ibase** 69d101cdd6SStefan Eßer when parsing numbers. 70d101cdd6SStefan Eßer 71d101cdd6SStefan Eßer This means that the value added to a number from a digit is always that 72d101cdd6SStefan Eßer digit's value multiplied by the value of ibase raised to the power of the 73d101cdd6SStefan Eßer digit's position, which starts from 0 at the least significant digit. 74d101cdd6SStefan Eßer 75d101cdd6SStefan Eßer If this and/or the **-c** or **-\-digit-clamp** options are given multiple 76d101cdd6SStefan Eßer times, the last one given is used. 77d101cdd6SStefan Eßer 78d101cdd6SStefan Eßer This option overrides the **BC_DIGIT_CLAMP** environment variable (see the 79d101cdd6SStefan Eßer **ENVIRONMENT VARIABLES** section) and the default, which can be queried 80d101cdd6SStefan Eßer with the **-h** or **-\-help** options. 81d101cdd6SStefan Eßer 82d101cdd6SStefan Eßer This is a **non-portable extension**. 83d101cdd6SStefan Eßer 84d101cdd6SStefan Eßer**-c**, **-\-digit-clamp** 85d101cdd6SStefan Eßer 86d101cdd6SStefan Eßer: Enables clamping of digits greater than or equal to the current **ibase** 87d101cdd6SStefan Eßer when parsing numbers. 88d101cdd6SStefan Eßer 89d101cdd6SStefan Eßer This means that digits that the value added to a number from a digit that is 90d101cdd6SStefan Eßer greater than or equal to the ibase is the value of ibase minus 1 all 91d101cdd6SStefan Eßer multiplied by the value of ibase raised to the power of the digit's 92d101cdd6SStefan Eßer position, which starts from 0 at the least significant digit. 93d101cdd6SStefan Eßer 94d101cdd6SStefan Eßer If this and/or the **-C** or **-\-no-digit-clamp** options are given 95d101cdd6SStefan Eßer multiple times, the last one given is used. 96d101cdd6SStefan Eßer 97d101cdd6SStefan Eßer This option overrides the **BC_DIGIT_CLAMP** environment variable (see the 98d101cdd6SStefan Eßer **ENVIRONMENT VARIABLES** section) and the default, which can be queried 99d101cdd6SStefan Eßer with the **-h** or **-\-help** options. 100d101cdd6SStefan Eßer 101d101cdd6SStefan Eßer This is a **non-portable extension**. 102d101cdd6SStefan Eßer 103d101cdd6SStefan Eßer**-e** *expr*, **-\-expression**=*expr* 104d101cdd6SStefan Eßer 105d101cdd6SStefan Eßer: Evaluates *expr*. If multiple expressions are given, they are evaluated in 106d101cdd6SStefan Eßer order. If files are given as well (see the **-f** and **-\-file** options), 107d101cdd6SStefan Eßer the expressions and files are evaluated in the order given. This means that 108d101cdd6SStefan Eßer if a file is given before an expression, the file is read in and evaluated 109d101cdd6SStefan Eßer first. 110d101cdd6SStefan Eßer 111d101cdd6SStefan Eßer If this option is given on the command-line (i.e., not in **BC_ENV_ARGS**, 112d101cdd6SStefan Eßer see the **ENVIRONMENT VARIABLES** section), then after processing all 113d101cdd6SStefan Eßer expressions and files, bc(1) will exit, unless **-** (**stdin**) was given 114d101cdd6SStefan Eßer as an argument at least once to **-f** or **-\-file**, whether on the 115d101cdd6SStefan Eßer command-line or in **BC_ENV_ARGS**. However, if any other **-e**, 116d101cdd6SStefan Eßer **-\-expression**, **-f**, or **-\-file** arguments are given after **-f-** 117d101cdd6SStefan Eßer or equivalent is given, bc(1) will give a fatal error and exit. 118d101cdd6SStefan Eßer 119d101cdd6SStefan Eßer This is a **non-portable extension**. 120d101cdd6SStefan Eßer 121d101cdd6SStefan Eßer**-f** *file*, **-\-file**=*file* 122d101cdd6SStefan Eßer 123d101cdd6SStefan Eßer: Reads in *file* and evaluates it, line by line, as though it were read 124d101cdd6SStefan Eßer through **stdin**. If expressions are also given (see the **-e** and 125d101cdd6SStefan Eßer **-\-expression** options), the expressions are evaluated in the order 126d101cdd6SStefan Eßer given. 127d101cdd6SStefan Eßer 128d101cdd6SStefan Eßer If this option is given on the command-line (i.e., not in **BC_ENV_ARGS**, 129d101cdd6SStefan Eßer see the **ENVIRONMENT VARIABLES** section), then after processing all 130d101cdd6SStefan Eßer expressions and files, bc(1) will exit, unless **-** (**stdin**) was given 131d101cdd6SStefan Eßer as an argument at least once to **-f** or **-\-file**. However, if any other 132d101cdd6SStefan Eßer **-e**, **-\-expression**, **-f**, or **-\-file** arguments are given after 133d101cdd6SStefan Eßer **-f-** or equivalent is given, bc(1) will give a fatal error and exit. 134d101cdd6SStefan Eßer 135d101cdd6SStefan Eßer This is a **non-portable extension**. 136d101cdd6SStefan Eßer 137028616d0SStefan Eßer**-g**, **-\-global-stacks** 1383aa99676SStefan Eßer 13944d4804dSStefan Eßer: Turns the globals **ibase**, **obase**, and **scale** into stacks. 1403aa99676SStefan Eßer 1413aa99676SStefan Eßer This has the effect that a copy of the current value of all three are pushed 1423aa99676SStefan Eßer onto a stack for every function call, as well as popped when every function 1433aa99676SStefan Eßer returns. This means that functions can assign to any and all of those 1443aa99676SStefan Eßer globals without worrying that the change will affect other functions. 1453aa99676SStefan Eßer Thus, a hypothetical function named **output(x,b)** that simply printed 1463aa99676SStefan Eßer **x** in base **b** could be written like this: 1473aa99676SStefan Eßer 1483aa99676SStefan Eßer define void output(x, b) { 1493aa99676SStefan Eßer obase=b 1503aa99676SStefan Eßer x 1513aa99676SStefan Eßer } 1523aa99676SStefan Eßer 1533aa99676SStefan Eßer instead of like this: 1543aa99676SStefan Eßer 1553aa99676SStefan Eßer define void output(x, b) { 1563aa99676SStefan Eßer auto c 1573aa99676SStefan Eßer c=obase 1583aa99676SStefan Eßer obase=b 1593aa99676SStefan Eßer x 1603aa99676SStefan Eßer obase=c 1613aa99676SStefan Eßer } 1623aa99676SStefan Eßer 1633aa99676SStefan Eßer This makes writing functions much easier. 1643aa99676SStefan Eßer 1653aa99676SStefan Eßer However, since using this flag means that functions cannot set **ibase**, 1663aa99676SStefan Eßer **obase**, or **scale** globally, functions that are made to do so cannot 1673aa99676SStefan Eßer work anymore. There are two possible use cases for that, and each has a 1683aa99676SStefan Eßer solution. 1693aa99676SStefan Eßer 1703aa99676SStefan Eßer First, if a function is called on startup to turn bc(1) into a number 1713aa99676SStefan Eßer converter, it is possible to replace that capability with various shell 1723aa99676SStefan Eßer aliases. Examples: 1733aa99676SStefan Eßer 1743aa99676SStefan Eßer alias d2o="bc -e ibase=A -e obase=8" 1753aa99676SStefan Eßer alias h2b="bc -e ibase=G -e obase=2" 1763aa99676SStefan Eßer 1773aa99676SStefan Eßer Second, if the purpose of a function is to set **ibase**, **obase**, or 1783aa99676SStefan Eßer **scale** globally for any other purpose, it could be split into one to 1793aa99676SStefan Eßer three functions (based on how many globals it sets) and each of those 1803aa99676SStefan Eßer functions could return the desired value for a global. 1813aa99676SStefan Eßer 1823aa99676SStefan Eßer If the behavior of this option is desired for every run of bc(1), then users 1833aa99676SStefan Eßer could make sure to define **BC_ENV_ARGS** and include this option (see the 1843aa99676SStefan Eßer **ENVIRONMENT VARIABLES** section for more details). 1853aa99676SStefan Eßer 1863aa99676SStefan Eßer If **-s**, **-w**, or any equivalents are used, this option is ignored. 1873aa99676SStefan Eßer 1883aa99676SStefan Eßer This is a **non-portable extension**. 1893aa99676SStefan Eßer 190028616d0SStefan Eßer**-h**, **-\-help** 1913aa99676SStefan Eßer 192d101cdd6SStefan Eßer: Prints a usage message and exits. 193d101cdd6SStefan Eßer 194d101cdd6SStefan Eßer**-I** *ibase*, **-\-ibase**=*ibase* 195d101cdd6SStefan Eßer 196d101cdd6SStefan Eßer: Sets the builtin variable **ibase** to the value *ibase* assuming that 197d101cdd6SStefan Eßer *ibase* is in base 10. It is a fatal error if *ibase* is not a valid number. 198d101cdd6SStefan Eßer 199d101cdd6SStefan Eßer If multiple instances of this option are given, the last is used. 200d101cdd6SStefan Eßer 201d101cdd6SStefan Eßer This is a **non-portable extension**. 2023aa99676SStefan Eßer 203028616d0SStefan Eßer**-i**, **-\-interactive** 2043aa99676SStefan Eßer 2053aa99676SStefan Eßer: Forces interactive mode. (See the **INTERACTIVE MODE** section.) 2063aa99676SStefan Eßer 2073aa99676SStefan Eßer This is a **non-portable extension**. 2083aa99676SStefan Eßer 209d43fa8efSStefan Eßer**-L**, **-\-no-line-length** 210d43fa8efSStefan Eßer 211d43fa8efSStefan Eßer: Disables line length checking and prints numbers without backslashes and 212d43fa8efSStefan Eßer newlines. In other words, this option sets **BC_LINE_LENGTH** to **0** (see 213d43fa8efSStefan Eßer the **ENVIRONMENT VARIABLES** section). 214d43fa8efSStefan Eßer 215d43fa8efSStefan Eßer This is a **non-portable extension**. 216d43fa8efSStefan Eßer 217028616d0SStefan Eßer**-l**, **-\-mathlib** 2183aa99676SStefan Eßer 2193aa99676SStefan Eßer: Sets **scale** (see the **SYNTAX** section) to **20** and loads the included 2203aa99676SStefan Eßer math library before running any code, including any expressions or files 2213aa99676SStefan Eßer specified on the command line. 2223aa99676SStefan Eßer 2233aa99676SStefan Eßer To learn what is in the library, see the **LIBRARY** section. 2243aa99676SStefan Eßer 225d101cdd6SStefan Eßer**-O** *obase*, **-\-obase**=*obase* 226d101cdd6SStefan Eßer 227d101cdd6SStefan Eßer: Sets the builtin variable **obase** to the value *obase* assuming that 228d101cdd6SStefan Eßer *obase* is in base 10. It is a fatal error if *obase* is not a valid number. 229d101cdd6SStefan Eßer 230d101cdd6SStefan Eßer If multiple instances of this option are given, the last is used. 231d101cdd6SStefan Eßer 232d101cdd6SStefan Eßer This is a **non-portable extension**. 233d101cdd6SStefan Eßer 234028616d0SStefan Eßer**-P**, **-\-no-prompt** 2353aa99676SStefan Eßer 2363aa99676SStefan Eßer: Disables the prompt in TTY mode. (The prompt is only enabled in TTY mode. 2377e5c51e5SStefan Eßer See the **TTY MODE** section.) This is mostly for those users that do not 2383aa99676SStefan Eßer want a prompt or are not used to having them in bc(1). Most of those users 2393aa99676SStefan Eßer would want to put this option in **BC_ENV_ARGS** (see the 2403aa99676SStefan Eßer **ENVIRONMENT VARIABLES** section). 2413aa99676SStefan Eßer 24244d4804dSStefan Eßer These options override the **BC_PROMPT** and **BC_TTY_MODE** environment 24344d4804dSStefan Eßer variables (see the **ENVIRONMENT VARIABLES** section). 24444d4804dSStefan Eßer 2453aa99676SStefan Eßer This is a **non-portable extension**. 2463aa99676SStefan Eßer 247d101cdd6SStefan Eßer**-q**, **-\-quiet** 248d101cdd6SStefan Eßer 249d101cdd6SStefan Eßer: This option is for compatibility with the GNU bc(1) 250d101cdd6SStefan Eßer (https://www.gnu.org/software/bc/); it is a no-op. Without this option, GNU 251d101cdd6SStefan Eßer bc(1) prints a copyright header. This bc(1) only prints the copyright header 252d101cdd6SStefan Eßer if one or more of the **-v**, **-V**, or **-\-version** options are given 253d101cdd6SStefan Eßer unless the **BC_BANNER** environment variable is set and contains a non-zero 254d101cdd6SStefan Eßer integer or if this bc(1) was built with the header displayed by default. If 255d101cdd6SStefan Eßer *any* of that is the case, then this option *does* prevent bc(1) from 256d101cdd6SStefan Eßer printing the header. 257d101cdd6SStefan Eßer 258d101cdd6SStefan Eßer This is a **non-portable extension**. 259d101cdd6SStefan Eßer 2607e5c51e5SStefan Eßer**-R**, **-\-no-read-prompt** 2617e5c51e5SStefan Eßer 2627e5c51e5SStefan Eßer: Disables the read prompt in TTY mode. (The read prompt is only enabled in 2637e5c51e5SStefan Eßer TTY mode. See the **TTY MODE** section.) This is mostly for those users that 2647e5c51e5SStefan Eßer do not want a read prompt or are not used to having them in bc(1). Most of 2657e5c51e5SStefan Eßer those users would want to put this option in **BC_ENV_ARGS** (see the 2667e5c51e5SStefan Eßer **ENVIRONMENT VARIABLES** section). This option is also useful in hash bang 2677e5c51e5SStefan Eßer lines of bc(1) scripts that prompt for user input. 2687e5c51e5SStefan Eßer 2697e5c51e5SStefan Eßer This option does not disable the regular prompt because the read prompt is 2707e5c51e5SStefan Eßer only used when the **read()** built-in function is called. 2717e5c51e5SStefan Eßer 27244d4804dSStefan Eßer These options *do* override the **BC_PROMPT** and **BC_TTY_MODE** 27344d4804dSStefan Eßer environment variables (see the **ENVIRONMENT VARIABLES** section), but only 27444d4804dSStefan Eßer for the read prompt. 27544d4804dSStefan Eßer 2767e5c51e5SStefan Eßer This is a **non-portable extension**. 2777e5c51e5SStefan Eßer 27844d4804dSStefan Eßer**-r** *keyword*, **-\-redefine**=*keyword* 27944d4804dSStefan Eßer 28044d4804dSStefan Eßer: Redefines *keyword* in order to allow it to be used as a function, variable, 28144d4804dSStefan Eßer or array name. This is useful when this bc(1) gives parse errors when 28244d4804dSStefan Eßer parsing scripts meant for other bc(1) implementations. 28344d4804dSStefan Eßer 28444d4804dSStefan Eßer The keywords this bc(1) allows to be redefined are: 28544d4804dSStefan Eßer 28644d4804dSStefan Eßer * **abs** 28744d4804dSStefan Eßer * **asciify** 28844d4804dSStefan Eßer * **continue** 28944d4804dSStefan Eßer * **divmod** 29044d4804dSStefan Eßer * **else** 29144d4804dSStefan Eßer * **halt** 29244d4804dSStefan Eßer * **last** 29344d4804dSStefan Eßer * **limits** 29444d4804dSStefan Eßer * **maxibase** 29544d4804dSStefan Eßer * **maxobase** 29644d4804dSStefan Eßer * **maxscale** 29744d4804dSStefan Eßer * **modexp** 29844d4804dSStefan Eßer * **print** 29944d4804dSStefan Eßer * **read** 30044d4804dSStefan Eßer * **stream** 30144d4804dSStefan Eßer 30244d4804dSStefan Eßer If any of those keywords are used as a function, variable, or array name in 30344d4804dSStefan Eßer a script, use this option with the keyword as the argument. If multiple are 30444d4804dSStefan Eßer used, use this option for all of them; it can be used multiple times. 30544d4804dSStefan Eßer 30644d4804dSStefan Eßer Keywords are *not* redefined when parsing the builtin math library (see the 30744d4804dSStefan Eßer **LIBRARY** section). 30844d4804dSStefan Eßer 309d101cdd6SStefan Eßer It is a fatal error to redefine keywords mandated by the POSIX standard (see 310d101cdd6SStefan Eßer the **STANDARDS** section). It is a fatal error to attempt to redefine words 311d101cdd6SStefan Eßer that this bc(1) does not reserve as keywords. 31244d4804dSStefan Eßer 313d101cdd6SStefan Eßer**-S** *scale*, **-\-scale**=*scale* 3143aa99676SStefan Eßer 315d101cdd6SStefan Eßer: Sets the builtin variable **scale** to the value *scale* assuming that 316d101cdd6SStefan Eßer *scale* is in base 10. It is a fatal error if *scale* is not a valid number. 317d101cdd6SStefan Eßer 318d101cdd6SStefan Eßer If multiple instances of this option are given, the last is used. 3193aa99676SStefan Eßer 3203aa99676SStefan Eßer This is a **non-portable extension**. 3213aa99676SStefan Eßer 322028616d0SStefan Eßer**-s**, **-\-standard** 3233aa99676SStefan Eßer 324d101cdd6SStefan Eßer: Process exactly the language defined by the standard (see the **STANDARDS** 325d101cdd6SStefan Eßer section) and error if any extensions are used. 3263aa99676SStefan Eßer 3273aa99676SStefan Eßer This is a **non-portable extension**. 3283aa99676SStefan Eßer 329028616d0SStefan Eßer**-v**, **-V**, **-\-version** 3303aa99676SStefan Eßer 331d101cdd6SStefan Eßer: Print the version information (copyright header) and exits. 3323aa99676SStefan Eßer 3333aa99676SStefan Eßer This is a **non-portable extension**. 3343aa99676SStefan Eßer 335028616d0SStefan Eßer**-w**, **-\-warn** 3363aa99676SStefan Eßer 337028616d0SStefan Eßer: Like **-s** and **-\-standard**, except that warnings (and not errors) are 3383aa99676SStefan Eßer printed for non-standard extensions and execution continues normally. 3393aa99676SStefan Eßer 3403aa99676SStefan Eßer This is a **non-portable extension**. 3413aa99676SStefan Eßer 342d43fa8efSStefan Eßer**-z**, **-\-leading-zeroes** 343d43fa8efSStefan Eßer 344d43fa8efSStefan Eßer: Makes bc(1) print all numbers greater than **-1** and less than **1**, and 345d43fa8efSStefan Eßer not equal to **0**, with a leading zero. 346d43fa8efSStefan Eßer 347103d7cdfSStefan Eßer This can be set for individual numbers with the **plz(x)**, **plznl(x)**, 348d43fa8efSStefan Eßer **pnlz(x)**, and **pnlznl(x)** functions in the extended math library (see 349d43fa8efSStefan Eßer the **LIBRARY** section). 350d43fa8efSStefan Eßer 351d43fa8efSStefan Eßer This is a **non-portable extension**. 352d43fa8efSStefan Eßer 3533aa99676SStefan EßerAll long options are **non-portable extensions**. 3543aa99676SStefan Eßer 35544d4804dSStefan Eßer# STDIN 35644d4804dSStefan Eßer 35744d4804dSStefan EßerIf no files or expressions are given by the **-f**, **-\-file**, **-e**, or 35878bc019dSStefan Eßer**-\-expression** options, then bc(1) reads from **stdin**. 35944d4804dSStefan Eßer 36044d4804dSStefan EßerHowever, there are a few caveats to this. 36144d4804dSStefan Eßer 36244d4804dSStefan EßerFirst, **stdin** is evaluated a line at a time. The only exception to this is if 36344d4804dSStefan Eßerthe parse cannot complete. That means that starting a string without ending it 36444d4804dSStefan Eßeror starting a function, **if** statement, or loop without ending it will also 36544d4804dSStefan Eßercause bc(1) to not execute. 36644d4804dSStefan Eßer 36744d4804dSStefan EßerSecond, after an **if** statement, bc(1) doesn't know if an **else** statement 36844d4804dSStefan Eßerwill follow, so it will not execute until it knows there will not be an **else** 36944d4804dSStefan Eßerstatement. 37044d4804dSStefan Eßer 3713aa99676SStefan Eßer# STDOUT 3723aa99676SStefan Eßer 3739a995fe1SStefan EßerAny non-error output is written to **stdout**. In addition, if history (see the 3749a995fe1SStefan Eßer**HISTORY** section) and the prompt (see the **TTY MODE** section) are enabled, 3759a995fe1SStefan Eßerboth are output to **stdout**. 3763aa99676SStefan Eßer 3773aa99676SStefan Eßer**Note**: Unlike other bc(1) implementations, this bc(1) will issue a fatal 3783aa99676SStefan Eßererror (see the **EXIT STATUS** section) if it cannot write to **stdout**, so if 3793aa99676SStefan Eßer**stdout** is closed, as in **bc <file> >&-**, it will quit with an error. This 3803aa99676SStefan Eßeris done so that bc(1) can report problems when **stdout** is redirected to a 3813aa99676SStefan Eßerfile. 3823aa99676SStefan Eßer 3833aa99676SStefan EßerIf there are scripts that depend on the behavior of other bc(1) implementations, 3843aa99676SStefan Eßerit is recommended that those scripts be changed to redirect **stdout** to 3853aa99676SStefan Eßer**/dev/null**. 3863aa99676SStefan Eßer 3873aa99676SStefan Eßer# STDERR 3883aa99676SStefan Eßer 3893aa99676SStefan EßerAny error output is written to **stderr**. 3903aa99676SStefan Eßer 3913aa99676SStefan Eßer**Note**: Unlike other bc(1) implementations, this bc(1) will issue a fatal 3923aa99676SStefan Eßererror (see the **EXIT STATUS** section) if it cannot write to **stderr**, so if 3933aa99676SStefan Eßer**stderr** is closed, as in **bc <file> 2>&-**, it will quit with an error. This 3943aa99676SStefan Eßeris done so that bc(1) can exit with an error code when **stderr** is redirected 3953aa99676SStefan Eßerto a file. 3963aa99676SStefan Eßer 3973aa99676SStefan EßerIf there are scripts that depend on the behavior of other bc(1) implementations, 3983aa99676SStefan Eßerit is recommended that those scripts be changed to redirect **stderr** to 3993aa99676SStefan Eßer**/dev/null**. 4003aa99676SStefan Eßer 4013aa99676SStefan Eßer# SYNTAX 4023aa99676SStefan Eßer 4033aa99676SStefan EßerThe syntax for bc(1) programs is mostly C-like, with some differences. This 404d101cdd6SStefan Eßerbc(1) follows the POSIX standard (see the **STANDARDS** section), which is a 40578bc019dSStefan Eßermuch more thorough resource for the language this bc(1) accepts. This section is 40678bc019dSStefan Eßermeant to be a summary and a listing of all the extensions to the standard. 4073aa99676SStefan Eßer 4083aa99676SStefan EßerIn the sections below, **E** means expression, **S** means statement, and **I** 4093aa99676SStefan Eßermeans identifier. 4103aa99676SStefan Eßer 4113aa99676SStefan EßerIdentifiers (**I**) start with a lowercase letter and can be followed by any 4123aa99676SStefan Eßernumber (up to **BC_NAME_MAX-1**) of lowercase letters (**a-z**), digits 4133aa99676SStefan Eßer(**0-9**), and underscores (**\_**). The regex is **\[a-z\]\[a-z0-9\_\]\***. 4143aa99676SStefan EßerIdentifiers with more than one character (letter) are a 4153aa99676SStefan Eßer**non-portable extension**. 4163aa99676SStefan Eßer 4173aa99676SStefan Eßer**ibase** is a global variable determining how to interpret constant numbers. It 4183aa99676SStefan Eßeris the "input" base, or the number base used for interpreting input numbers. 419028616d0SStefan Eßer**ibase** is initially **10**. If the **-s** (**-\-standard**) and **-w** 420028616d0SStefan Eßer(**-\-warn**) flags were not given on the command line, the max allowable value 4213aa99676SStefan Eßerfor **ibase** is **36**. Otherwise, it is **16**. The min allowable value for 4223aa99676SStefan Eßer**ibase** is **2**. The max allowable value for **ibase** can be queried in 4233aa99676SStefan Eßerbc(1) programs with the **maxibase()** built-in function. 4243aa99676SStefan Eßer 4253aa99676SStefan Eßer**obase** is a global variable determining how to output results. It is the 4263aa99676SStefan Eßer"output" base, or the number base used for outputting numbers. **obase** is 4273aa99676SStefan Eßerinitially **10**. The max allowable value for **obase** is **BC_BASE_MAX** and 4283aa99676SStefan Eßercan be queried in bc(1) programs with the **maxobase()** built-in function. The 4293aa99676SStefan Eßermin allowable value for **obase** is **2**. Values are output in the specified 4303aa99676SStefan Eßerbase. 4313aa99676SStefan Eßer 4323aa99676SStefan EßerThe *scale* of an expression is the number of digits in the result of the 4333aa99676SStefan Eßerexpression right of the decimal point, and **scale** is a global variable that 4343aa99676SStefan Eßersets the precision of any operations, with exceptions. **scale** is initially 4353aa99676SStefan Eßer**0**. **scale** cannot be negative. The max allowable value for **scale** is 4363aa99676SStefan Eßer**BC_SCALE_MAX** and can be queried in bc(1) programs with the **maxscale()** 4373aa99676SStefan Eßerbuilt-in function. 4383aa99676SStefan Eßer 4393aa99676SStefan Eßerbc(1) has both *global* variables and *local* variables. All *local* 4403aa99676SStefan Eßervariables are local to the function; they are parameters or are introduced in 4413aa99676SStefan Eßerthe **auto** list of a function (see the **FUNCTIONS** section). If a variable 4423aa99676SStefan Eßeris accessed which is not a parameter or in the **auto** list, it is assumed to 4433aa99676SStefan Eßerbe *global*. If a parent function has a *local* variable version of a variable 4443aa99676SStefan Eßerthat a child function considers *global*, the value of that *global* variable in 4453aa99676SStefan Eßerthe child function is the value of the variable in the parent function, not the 4463aa99676SStefan Eßervalue of the actual *global* variable. 4473aa99676SStefan Eßer 4483aa99676SStefan EßerAll of the above applies to arrays as well. 4493aa99676SStefan Eßer 4503aa99676SStefan EßerThe value of a statement that is an expression (i.e., any of the named 4513aa99676SStefan Eßerexpressions or operands) is printed unless the lowest precedence operator is an 4523aa99676SStefan Eßerassignment operator *and* the expression is notsurrounded by parentheses. 4533aa99676SStefan Eßer 4543aa99676SStefan EßerThe value that is printed is also assigned to the special variable **last**. A 4553aa99676SStefan Eßersingle dot (**.**) may also be used as a synonym for **last**. These are 4563aa99676SStefan Eßer**non-portable extensions**. 4573aa99676SStefan Eßer 4583aa99676SStefan EßerEither semicolons or newlines may separate statements. 4593aa99676SStefan Eßer 4603aa99676SStefan Eßer## Comments 4613aa99676SStefan Eßer 4623aa99676SStefan EßerThere are two kinds of comments: 4633aa99676SStefan Eßer 4643aa99676SStefan Eßer1. Block comments are enclosed in **/\*** and **\*/**. 4653aa99676SStefan Eßer2. Line comments go from **#** until, and not including, the next newline. This 4663aa99676SStefan Eßer is a **non-portable extension**. 4673aa99676SStefan Eßer 4683aa99676SStefan Eßer## Named Expressions 4693aa99676SStefan Eßer 4703aa99676SStefan EßerThe following are named expressions in bc(1): 4713aa99676SStefan Eßer 4723aa99676SStefan Eßer1. Variables: **I** 4733aa99676SStefan Eßer2. Array Elements: **I[E]** 4743aa99676SStefan Eßer3. **ibase** 4753aa99676SStefan Eßer4. **obase** 4763aa99676SStefan Eßer5. **scale** 4773aa99676SStefan Eßer6. **last** or a single dot (**.**) 4783aa99676SStefan Eßer 4793aa99676SStefan EßerNumber 6 is a **non-portable extension**. 4803aa99676SStefan Eßer 4813aa99676SStefan EßerVariables and arrays do not interfere; users can have arrays named the same as 4823aa99676SStefan Eßervariables. This also applies to functions (see the **FUNCTIONS** section), so a 4833aa99676SStefan Eßeruser can have a variable, array, and function that all have the same name, and 4843aa99676SStefan Eßerthey will not shadow each other, whether inside of functions or not. 4853aa99676SStefan Eßer 4863aa99676SStefan EßerNamed expressions are required as the operand of **increment**/**decrement** 4873aa99676SStefan Eßeroperators and as the left side of **assignment** operators (see the *Operators* 4883aa99676SStefan Eßersubsection). 4893aa99676SStefan Eßer 4903aa99676SStefan Eßer## Operands 4913aa99676SStefan Eßer 4923aa99676SStefan EßerThe following are valid operands in bc(1): 4933aa99676SStefan Eßer 4943aa99676SStefan Eßer1. Numbers (see the *Numbers* subsection below). 4953aa99676SStefan Eßer2. Array indices (**I[E]**). 4963aa99676SStefan Eßer3. **(E)**: The value of **E** (used to change precedence). 4973aa99676SStefan Eßer4. **sqrt(E)**: The square root of **E**. **E** must be non-negative. 49844d4804dSStefan Eßer5. **length(E)**: The number of significant decimal digits in **E**. Returns 49944d4804dSStefan Eßer **1** for **0** with no decimal places. If given a string, the length of the 50044d4804dSStefan Eßer string is returned. Passing a string to **length(E)** is a **non-portable 50144d4804dSStefan Eßer extension**. 5023aa99676SStefan Eßer6. **length(I[])**: The number of elements in the array **I**. This is a 5033aa99676SStefan Eßer **non-portable extension**. 5043aa99676SStefan Eßer7. **scale(E)**: The *scale* of **E**. 5053aa99676SStefan Eßer8. **abs(E)**: The absolute value of **E**. This is a **non-portable 5063aa99676SStefan Eßer extension**. 507d101cdd6SStefan Eßer9. **is_number(E)**: **1** if the given argument is a number, **0** if it is a 508d101cdd6SStefan Eßer string. This is a **non-portable extension**. 509d101cdd6SStefan Eßer10. **is_string(E)**: **1** if the given argument is a string, **0** if it is a 510d101cdd6SStefan Eßer number. This is a **non-portable extension**. 511d101cdd6SStefan Eßer11. **modexp(E, E, E)**: Modular exponentiation, where the first expression is 51244d4804dSStefan Eßer the base, the second is the exponent, and the third is the modulus. All 51344d4804dSStefan Eßer three values must be integers. The second argument must be non-negative. The 51444d4804dSStefan Eßer third argument must be non-zero. This is a **non-portable extension**. 515d101cdd6SStefan Eßer11. **divmod(E, E, I[])**: Division and modulus in one operation. This is for 51644d4804dSStefan Eßer optimization. The first expression is the dividend, and the second is the 51744d4804dSStefan Eßer divisor, which must be non-zero. The return value is the quotient, and the 51844d4804dSStefan Eßer modulus is stored in index **0** of the provided array (the last argument). 51944d4804dSStefan Eßer This is a **non-portable extension**. 520d101cdd6SStefan Eßer12. **asciify(E)**: If **E** is a string, returns a string that is the first 52144d4804dSStefan Eßer letter of its argument. If it is a number, calculates the number mod **256** 52244d4804dSStefan Eßer and returns that number as a one-character string. This is a **non-portable 52344d4804dSStefan Eßer extension**. 524d101cdd6SStefan Eßer13. **asciify(I[])**: A string that is made up of the characters that would 525d101cdd6SStefan Eßer result from running **asciify(E)** on each element of the array identified 526d101cdd6SStefan Eßer by the argument. This allows creating multi-character strings and storing 527d101cdd6SStefan Eßer them. This is a **non-portable extension**. 528d101cdd6SStefan Eßer14. **I()**, **I(E)**, **I(E, E)**, and so on, where **I** is an identifier for 5293aa99676SStefan Eßer a non-**void** function (see the *Void Functions* subsection of the 5303aa99676SStefan Eßer **FUNCTIONS** section). The **E** argument(s) may also be arrays of the form 5313aa99676SStefan Eßer **I[]**, which will automatically be turned into array references (see the 5323aa99676SStefan Eßer *Array References* subsection of the **FUNCTIONS** section) if the 5333aa99676SStefan Eßer corresponding parameter in the function definition is an array reference. 534d101cdd6SStefan Eßer15. **read()**: Reads a line from **stdin** and uses that as an expression. The 5353aa99676SStefan Eßer result of that expression is the result of the **read()** operand. This is a 5363aa99676SStefan Eßer **non-portable extension**. 537d101cdd6SStefan Eßer16. **maxibase()**: The max allowable **ibase**. This is a **non-portable 5383aa99676SStefan Eßer extension**. 539d101cdd6SStefan Eßer17. **maxobase()**: The max allowable **obase**. This is a **non-portable 5403aa99676SStefan Eßer extension**. 541d101cdd6SStefan Eßer18. **maxscale()**: The max allowable **scale**. This is a **non-portable 5423aa99676SStefan Eßer extension**. 543d101cdd6SStefan Eßer19. **line_length()**: The line length set with **BC_LINE_LENGTH** (see the 544d43fa8efSStefan Eßer **ENVIRONMENT VARIABLES** section). This is a **non-portable extension**. 545d101cdd6SStefan Eßer20. **global_stacks()**: **0** if global stacks are not enabled with the **-g** 546d43fa8efSStefan Eßer or **-\-global-stacks** options, non-zero otherwise. See the **OPTIONS** 547d43fa8efSStefan Eßer section. This is a **non-portable extension**. 548d101cdd6SStefan Eßer21. **leading_zero()**: **0** if leading zeroes are not enabled with the **-z** 549d43fa8efSStefan Eßer or **--leading-zeroes** options, non-zero otherwise. See the **OPTIONS** 550d43fa8efSStefan Eßer section. This is a **non-portable extension**. 5513aa99676SStefan Eßer 5523aa99676SStefan Eßer## Numbers 5533aa99676SStefan Eßer 5543aa99676SStefan EßerNumbers are strings made up of digits, uppercase letters, and at most **1** 5553aa99676SStefan Eßerperiod for a radix. Numbers can have up to **BC_NUM_MAX** digits. Uppercase 556d101cdd6SStefan Eßerletters are equal to **9** plus their position in the alphabet, starting from 557d101cdd6SStefan Eßer**1** (i.e., **A** equals **10**, or **9+1**). 5583aa99676SStefan Eßer 559d101cdd6SStefan EßerIf a digit or letter makes no sense with the current value of **ibase** (i.e., 560d101cdd6SStefan Eßerthey are greater than or equal to the current value of **ibase**), then the 561d101cdd6SStefan Eßerbehavior depends on the existence of the **-c**/**-\-digit-clamp** or 562d101cdd6SStefan Eßer**-C**/**-\-no-digit-clamp** options (see the **OPTIONS** section), the 563d101cdd6SStefan Eßerexistence and setting of the **BC_DIGIT_CLAMP** environment variable (see the 564d101cdd6SStefan Eßer**ENVIRONMENT VARIABLES** section), or the default, which can be queried with 565d101cdd6SStefan Eßerthe **-h**/**-\-help** option. 566d101cdd6SStefan Eßer 567d101cdd6SStefan EßerIf clamping is off, then digits or letters that are greater than or equal to the 568d101cdd6SStefan Eßercurrent value of **ibase** are not changed. Instead, their given value is 569d101cdd6SStefan Eßermultiplied by the appropriate power of **ibase** and added into the number. This 570d101cdd6SStefan Eßermeans that, with an **ibase** of **3**, the number **AB** is equal to 571d101cdd6SStefan Eßer**3\^1\*A+3\^0\*B**, which is **3** times **10** plus **11**, or **41**. 572d101cdd6SStefan Eßer 573d101cdd6SStefan EßerIf clamping is on, then digits or letters that are greater than or equal to the 574d101cdd6SStefan Eßercurrent value of **ibase** are set to the value of the highest valid digit in 575d101cdd6SStefan Eßer**ibase** before being multiplied by the appropriate power of **ibase** and 576d101cdd6SStefan Eßeradded into the number. This means that, with an **ibase** of **3**, the number 577d101cdd6SStefan Eßer**AB** is equal to **3\^1\*2+3\^0\*2**, which is **3** times **2** plus **2**, 578d101cdd6SStefan Eßeror **8**. 579d101cdd6SStefan Eßer 580d101cdd6SStefan EßerThere is one exception to clamping: single-character numbers (i.e., **A** 581d101cdd6SStefan Eßeralone). Such numbers are never clamped and always take the value they would have 582d101cdd6SStefan Eßerin the highest possible **ibase**. This means that **A** alone always equals 583d101cdd6SStefan Eßerdecimal **10** and **Z** alone always equals decimal **35**. This behavior is 584d101cdd6SStefan Eßermandated by the standard (see the STANDARDS section) and is meant to provide an 585d101cdd6SStefan Eßereasy way to set the current **ibase** (with the **i** command) regardless of the 586d101cdd6SStefan Eßercurrent value of **ibase**. 587d101cdd6SStefan Eßer 588d101cdd6SStefan EßerIf clamping is on, and the clamped value of a character is needed, use a leading 589d101cdd6SStefan Eßerzero, i.e., for **A**, use **0A**. 5903aa99676SStefan Eßer 5913aa99676SStefan Eßer## Operators 5923aa99676SStefan Eßer 5933aa99676SStefan EßerThe following arithmetic and logical operators can be used. They are listed in 5943aa99676SStefan Eßerorder of decreasing precedence. Operators in the same group have the same 5953aa99676SStefan Eßerprecedence. 5963aa99676SStefan Eßer 597028616d0SStefan Eßer**++** **-\-** 5983aa99676SStefan Eßer 5993aa99676SStefan Eßer: Type: Prefix and Postfix 6003aa99676SStefan Eßer 6013aa99676SStefan Eßer Associativity: None 6023aa99676SStefan Eßer 6033aa99676SStefan Eßer Description: **increment**, **decrement** 6043aa99676SStefan Eßer 6053aa99676SStefan Eßer**-** **!** 6063aa99676SStefan Eßer 6073aa99676SStefan Eßer: Type: Prefix 6083aa99676SStefan Eßer 6093aa99676SStefan Eßer Associativity: None 6103aa99676SStefan Eßer 6113aa99676SStefan Eßer Description: **negation**, **boolean not** 6123aa99676SStefan Eßer 6133aa99676SStefan Eßer**\^** 6143aa99676SStefan Eßer 6153aa99676SStefan Eßer: Type: Binary 6163aa99676SStefan Eßer 6173aa99676SStefan Eßer Associativity: Right 6183aa99676SStefan Eßer 6193aa99676SStefan Eßer Description: **power** 6203aa99676SStefan Eßer 6213aa99676SStefan Eßer**\*** **/** **%** 6223aa99676SStefan Eßer 6233aa99676SStefan Eßer: Type: Binary 6243aa99676SStefan Eßer 6253aa99676SStefan Eßer Associativity: Left 6263aa99676SStefan Eßer 6273aa99676SStefan Eßer Description: **multiply**, **divide**, **modulus** 6283aa99676SStefan Eßer 6293aa99676SStefan Eßer**+** **-** 6303aa99676SStefan Eßer 6313aa99676SStefan Eßer: Type: Binary 6323aa99676SStefan Eßer 6333aa99676SStefan Eßer Associativity: Left 6343aa99676SStefan Eßer 6353aa99676SStefan Eßer Description: **add**, **subtract** 6363aa99676SStefan Eßer 6373aa99676SStefan Eßer**=** **+=** **-=** **\*=** **/=** **%=** **\^=** 6383aa99676SStefan Eßer 6393aa99676SStefan Eßer: Type: Binary 6403aa99676SStefan Eßer 6413aa99676SStefan Eßer Associativity: Right 6423aa99676SStefan Eßer 6433aa99676SStefan Eßer Description: **assignment** 6443aa99676SStefan Eßer 6453aa99676SStefan Eßer**==** **\<=** **\>=** **!=** **\<** **\>** 6463aa99676SStefan Eßer 6473aa99676SStefan Eßer: Type: Binary 6483aa99676SStefan Eßer 6493aa99676SStefan Eßer Associativity: Left 6503aa99676SStefan Eßer 6513aa99676SStefan Eßer Description: **relational** 6523aa99676SStefan Eßer 6533aa99676SStefan Eßer**&&** 6543aa99676SStefan Eßer 6553aa99676SStefan Eßer: Type: Binary 6563aa99676SStefan Eßer 6573aa99676SStefan Eßer Associativity: Left 6583aa99676SStefan Eßer 6593aa99676SStefan Eßer Description: **boolean and** 6603aa99676SStefan Eßer 6613aa99676SStefan Eßer**||** 6623aa99676SStefan Eßer 6633aa99676SStefan Eßer: Type: Binary 6643aa99676SStefan Eßer 6653aa99676SStefan Eßer Associativity: Left 6663aa99676SStefan Eßer 6673aa99676SStefan Eßer Description: **boolean or** 6683aa99676SStefan Eßer 6693aa99676SStefan EßerThe operators will be described in more detail below. 6703aa99676SStefan Eßer 671028616d0SStefan Eßer**++** **-\-** 6723aa99676SStefan Eßer 6733aa99676SStefan Eßer: The prefix and postfix **increment** and **decrement** operators behave 6743aa99676SStefan Eßer exactly like they would in C. They require a named expression (see the 6753aa99676SStefan Eßer *Named Expressions* subsection) as an operand. 6763aa99676SStefan Eßer 6773aa99676SStefan Eßer The prefix versions of these operators are more efficient; use them where 6783aa99676SStefan Eßer possible. 6793aa99676SStefan Eßer 6803aa99676SStefan Eßer**-** 6813aa99676SStefan Eßer 6823aa99676SStefan Eßer: The **negation** operator returns **0** if a user attempts to negate any 6833aa99676SStefan Eßer expression with the value **0**. Otherwise, a copy of the expression with 6843aa99676SStefan Eßer its sign flipped is returned. 6853aa99676SStefan Eßer 6863aa99676SStefan Eßer**!** 6873aa99676SStefan Eßer 6883aa99676SStefan Eßer: The **boolean not** operator returns **1** if the expression is **0**, or 6893aa99676SStefan Eßer **0** otherwise. 6903aa99676SStefan Eßer 6913aa99676SStefan Eßer This is a **non-portable extension**. 6923aa99676SStefan Eßer 6933aa99676SStefan Eßer**\^** 6943aa99676SStefan Eßer 6953aa99676SStefan Eßer: The **power** operator (not the **exclusive or** operator, as it would be in 6963aa99676SStefan Eßer C) takes two expressions and raises the first to the power of the value of 69750696a6eSStefan Eßer the second. The *scale* of the result is equal to **scale**. 6983aa99676SStefan Eßer 6993aa99676SStefan Eßer The second expression must be an integer (no *scale*), and if it is 7003aa99676SStefan Eßer negative, the first value must be non-zero. 7013aa99676SStefan Eßer 7023aa99676SStefan Eßer**\*** 7033aa99676SStefan Eßer 7043aa99676SStefan Eßer: The **multiply** operator takes two expressions, multiplies them, and 7053aa99676SStefan Eßer returns the product. If **a** is the *scale* of the first expression and 7063aa99676SStefan Eßer **b** is the *scale* of the second expression, the *scale* of the result is 7073aa99676SStefan Eßer equal to **min(a+b,max(scale,a,b))** where **min()** and **max()** return 7083aa99676SStefan Eßer the obvious values. 7093aa99676SStefan Eßer 7103aa99676SStefan Eßer**/** 7113aa99676SStefan Eßer 7123aa99676SStefan Eßer: The **divide** operator takes two expressions, divides them, and returns the 7133aa99676SStefan Eßer quotient. The *scale* of the result shall be the value of **scale**. 7143aa99676SStefan Eßer 7153aa99676SStefan Eßer The second expression must be non-zero. 7163aa99676SStefan Eßer 7173aa99676SStefan Eßer**%** 7183aa99676SStefan Eßer 7193aa99676SStefan Eßer: The **modulus** operator takes two expressions, **a** and **b**, and 7203aa99676SStefan Eßer evaluates them by 1) Computing **a/b** to current **scale** and 2) Using the 7213aa99676SStefan Eßer result of step 1 to calculate **a-(a/b)\*b** to *scale* 7223aa99676SStefan Eßer **max(scale+scale(b),scale(a))**. 7233aa99676SStefan Eßer 7243aa99676SStefan Eßer The second expression must be non-zero. 7253aa99676SStefan Eßer 7263aa99676SStefan Eßer**+** 7273aa99676SStefan Eßer 7283aa99676SStefan Eßer: The **add** operator takes two expressions, **a** and **b**, and returns the 7293aa99676SStefan Eßer sum, with a *scale* equal to the max of the *scale*s of **a** and **b**. 7303aa99676SStefan Eßer 7313aa99676SStefan Eßer**-** 7323aa99676SStefan Eßer 7333aa99676SStefan Eßer: The **subtract** operator takes two expressions, **a** and **b**, and 7343aa99676SStefan Eßer returns the difference, with a *scale* equal to the max of the *scale*s of 7353aa99676SStefan Eßer **a** and **b**. 7363aa99676SStefan Eßer 7373aa99676SStefan Eßer**=** **+=** **-=** **\*=** **/=** **%=** **\^=** 7383aa99676SStefan Eßer 7393aa99676SStefan Eßer: The **assignment** operators take two expressions, **a** and **b** where 7403aa99676SStefan Eßer **a** is a named expression (see the *Named Expressions* subsection). 7413aa99676SStefan Eßer 7423aa99676SStefan Eßer For **=**, **b** is copied and the result is assigned to **a**. For all 7433aa99676SStefan Eßer others, **a** and **b** are applied as operands to the corresponding 7443aa99676SStefan Eßer arithmetic operator and the result is assigned to **a**. 7453aa99676SStefan Eßer 7463aa99676SStefan Eßer**==** **\<=** **\>=** **!=** **\<** **\>** 7473aa99676SStefan Eßer 7483aa99676SStefan Eßer: The **relational** operators compare two expressions, **a** and **b**, and 7493aa99676SStefan Eßer if the relation holds, according to C language semantics, the result is 7503aa99676SStefan Eßer **1**. Otherwise, it is **0**. 7513aa99676SStefan Eßer 7523aa99676SStefan Eßer Note that unlike in C, these operators have a lower precedence than the 7533aa99676SStefan Eßer **assignment** operators, which means that **a=b\>c** is interpreted as 7543aa99676SStefan Eßer **(a=b)\>c**. 7553aa99676SStefan Eßer 756d101cdd6SStefan Eßer Also, unlike the standard (see the **STANDARDS** section) requires, these 757d101cdd6SStefan Eßer operators can appear anywhere any other expressions can be used. This 758d101cdd6SStefan Eßer allowance is a **non-portable extension**. 7593aa99676SStefan Eßer 7603aa99676SStefan Eßer**&&** 7613aa99676SStefan Eßer 7623aa99676SStefan Eßer: The **boolean and** operator takes two expressions and returns **1** if both 7633aa99676SStefan Eßer expressions are non-zero, **0** otherwise. 7643aa99676SStefan Eßer 7653aa99676SStefan Eßer This is *not* a short-circuit operator. 7663aa99676SStefan Eßer 7673aa99676SStefan Eßer This is a **non-portable extension**. 7683aa99676SStefan Eßer 7693aa99676SStefan Eßer**||** 7703aa99676SStefan Eßer 7713aa99676SStefan Eßer: The **boolean or** operator takes two expressions and returns **1** if one 7723aa99676SStefan Eßer of the expressions is non-zero, **0** otherwise. 7733aa99676SStefan Eßer 7743aa99676SStefan Eßer This is *not* a short-circuit operator. 7753aa99676SStefan Eßer 7763aa99676SStefan Eßer This is a **non-portable extension**. 7773aa99676SStefan Eßer 7783aa99676SStefan Eßer## Statements 7793aa99676SStefan Eßer 7803aa99676SStefan EßerThe following items are statements: 7813aa99676SStefan Eßer 7823aa99676SStefan Eßer1. **E** 7833aa99676SStefan Eßer2. **{** **S** **;** ... **;** **S** **}** 7843aa99676SStefan Eßer3. **if** **(** **E** **)** **S** 7853aa99676SStefan Eßer4. **if** **(** **E** **)** **S** **else** **S** 7863aa99676SStefan Eßer5. **while** **(** **E** **)** **S** 7873aa99676SStefan Eßer6. **for** **(** **E** **;** **E** **;** **E** **)** **S** 7883aa99676SStefan Eßer7. An empty statement 7893aa99676SStefan Eßer8. **break** 7903aa99676SStefan Eßer9. **continue** 7913aa99676SStefan Eßer10. **quit** 7923aa99676SStefan Eßer11. **halt** 7933aa99676SStefan Eßer12. **limits** 7943aa99676SStefan Eßer13. A string of characters, enclosed in double quotes 7953aa99676SStefan Eßer14. **print** **E** **,** ... **,** **E** 79644d4804dSStefan Eßer15. **stream** **E** **,** ... **,** **E** 79744d4804dSStefan Eßer16. **I()**, **I(E)**, **I(E, E)**, and so on, where **I** is an identifier for 7983aa99676SStefan Eßer a **void** function (see the *Void Functions* subsection of the 7993aa99676SStefan Eßer **FUNCTIONS** section). The **E** argument(s) may also be arrays of the form 8003aa99676SStefan Eßer **I[]**, which will automatically be turned into array references (see the 8013aa99676SStefan Eßer *Array References* subsection of the **FUNCTIONS** section) if the 8023aa99676SStefan Eßer corresponding parameter in the function definition is an array reference. 8033aa99676SStefan Eßer 80444d4804dSStefan EßerNumbers 4, 9, 11, 12, 14, 15, and 16 are **non-portable extensions**. 8053aa99676SStefan Eßer 8063aa99676SStefan EßerAlso, as a **non-portable extension**, any or all of the expressions in the 8073aa99676SStefan Eßerheader of a for loop may be omitted. If the condition (second expression) is 8083aa99676SStefan Eßeromitted, it is assumed to be a constant **1**. 8093aa99676SStefan Eßer 8103aa99676SStefan EßerThe **break** statement causes a loop to stop iterating and resume execution 8113aa99676SStefan Eßerimmediately following a loop. This is only allowed in loops. 8123aa99676SStefan Eßer 8133aa99676SStefan EßerThe **continue** statement causes a loop iteration to stop early and returns to 8143aa99676SStefan Eßerthe start of the loop, including testing the loop condition. This is only 8153aa99676SStefan Eßerallowed in loops. 8163aa99676SStefan Eßer 8173aa99676SStefan EßerThe **if** **else** statement does the same thing as in C. 8183aa99676SStefan Eßer 8193aa99676SStefan EßerThe **quit** statement causes bc(1) to quit, even if it is on a branch that will 8203aa99676SStefan Eßernot be executed (it is a compile-time command). 8213aa99676SStefan Eßer 822d101cdd6SStefan Eßer**Warning**: The behavior of this bc(1) on **quit** is slightly different from 823d101cdd6SStefan Eßerother bc(1) implementations. Other bc(1) implementations will exit as soon as 824d101cdd6SStefan Eßerthey finish parsing the line that a **quit** command is on. This bc(1) will 825d101cdd6SStefan Eßerexecute any completed and executable statements that occur before the **quit** 826d101cdd6SStefan Eßerstatement before exiting. 827d101cdd6SStefan Eßer 828d101cdd6SStefan EßerIn other words, for the bc(1) code below: 829d101cdd6SStefan Eßer 830d101cdd6SStefan Eßer for (i = 0; i < 3; ++i) i; quit 831d101cdd6SStefan Eßer 832d101cdd6SStefan EßerOther bc(1) implementations will print nothing, and this bc(1) will print **0**, 833d101cdd6SStefan Eßer**1**, and **2** on successive lines before exiting. 834d101cdd6SStefan Eßer 8353aa99676SStefan EßerThe **halt** statement causes bc(1) to quit, if it is executed. (Unlike **quit** 8363aa99676SStefan Eßerif it is on a branch of an **if** statement that is not executed, bc(1) does not 8373aa99676SStefan Eßerquit.) 8383aa99676SStefan Eßer 8393aa99676SStefan EßerThe **limits** statement prints the limits that this bc(1) is subject to. This 8403aa99676SStefan Eßeris like the **quit** statement in that it is a compile-time command. 8413aa99676SStefan Eßer 8423aa99676SStefan EßerAn expression by itself is evaluated and printed, followed by a newline. 8433aa99676SStefan Eßer 84444d4804dSStefan Eßer## Strings 84544d4804dSStefan Eßer 84644d4804dSStefan EßerIf strings appear as a statement by themselves, they are printed without a 84744d4804dSStefan Eßertrailing newline. 84844d4804dSStefan Eßer 84944d4804dSStefan EßerIn addition to appearing as a lone statement by themselves, strings can be 85044d4804dSStefan Eßerassigned to variables and array elements. They can also be passed to functions 85144d4804dSStefan Eßerin variable parameters. 85244d4804dSStefan Eßer 85344d4804dSStefan EßerIf any statement that expects a string is given a variable that had a string 85444d4804dSStefan Eßerassigned to it, the statement acts as though it had received a string. 85544d4804dSStefan Eßer 85644d4804dSStefan EßerIf any math operation is attempted on a string or a variable or array element 85744d4804dSStefan Eßerthat has been assigned a string, an error is raised, and bc(1) resets (see the 85844d4804dSStefan Eßer**RESET** section). 85944d4804dSStefan Eßer 86044d4804dSStefan EßerAssigning strings to variables and array elements and passing them to functions 86144d4804dSStefan Eßerare **non-portable extensions**. 86244d4804dSStefan Eßer 8633aa99676SStefan Eßer## Print Statement 8643aa99676SStefan Eßer 8653aa99676SStefan EßerThe "expressions" in a **print** statement may also be strings. If they are, there 8663aa99676SStefan Eßerare backslash escape sequences that are interpreted specially. What those 8673aa99676SStefan Eßersequences are, and what they cause to be printed, are shown below: 8683aa99676SStefan Eßer 86944d4804dSStefan Eßer**\\a**: **\\a** 87044d4804dSStefan Eßer 87144d4804dSStefan Eßer**\\b**: **\\b** 87244d4804dSStefan Eßer 87344d4804dSStefan Eßer**\\\\**: **\\** 87444d4804dSStefan Eßer 87544d4804dSStefan Eßer**\\e**: **\\** 87644d4804dSStefan Eßer 87744d4804dSStefan Eßer**\\f**: **\\f** 87844d4804dSStefan Eßer 87944d4804dSStefan Eßer**\\n**: **\\n** 88044d4804dSStefan Eßer 88144d4804dSStefan Eßer**\\q**: **"** 88244d4804dSStefan Eßer 88344d4804dSStefan Eßer**\\r**: **\\r** 88444d4804dSStefan Eßer 88544d4804dSStefan Eßer**\\t**: **\\t** 8863aa99676SStefan Eßer 8873aa99676SStefan EßerAny other character following a backslash causes the backslash and character to 8883aa99676SStefan Eßerbe printed as-is. 8893aa99676SStefan Eßer 8903aa99676SStefan EßerAny non-string expression in a print statement shall be assigned to **last**, 8913aa99676SStefan Eßerlike any other expression that is printed. 8923aa99676SStefan Eßer 89344d4804dSStefan Eßer## Stream Statement 89444d4804dSStefan Eßer 895aa339f1dSStefan EßerThe expressions in a **stream** statement may also be strings. 89644d4804dSStefan Eßer 89744d4804dSStefan EßerIf a **stream** statement is given a string, it prints the string as though the 89844d4804dSStefan Eßerstring had appeared as its own statement. In other words, the **stream** 89944d4804dSStefan Eßerstatement prints strings normally, without a newline. 90044d4804dSStefan Eßer 90144d4804dSStefan EßerIf a **stream** statement is given a number, a copy of it is truncated and its 90244d4804dSStefan Eßerabsolute value is calculated. The result is then printed as though **obase** is 90344d4804dSStefan Eßer**256** and each digit is interpreted as an 8-bit ASCII character, making it a 90444d4804dSStefan Eßerbyte stream. 90544d4804dSStefan Eßer 9063aa99676SStefan Eßer## Order of Evaluation 9073aa99676SStefan Eßer 9083aa99676SStefan EßerAll expressions in a statment are evaluated left to right, except as necessary 9093aa99676SStefan Eßerto maintain order of operations. This means, for example, assuming that **i** is 9103aa99676SStefan Eßerequal to **0**, in the expression 9113aa99676SStefan Eßer 9123aa99676SStefan Eßer a[i++] = i++ 9133aa99676SStefan Eßer 9143aa99676SStefan Eßerthe first (or 0th) element of **a** is set to **1**, and **i** is equal to **2** 9153aa99676SStefan Eßerat the end of the expression. 9163aa99676SStefan Eßer 9173aa99676SStefan EßerThis includes function arguments. Thus, assuming **i** is equal to **0**, this 9183aa99676SStefan Eßermeans that in the expression 9193aa99676SStefan Eßer 9203aa99676SStefan Eßer x(i++, i++) 9213aa99676SStefan Eßer 9223aa99676SStefan Eßerthe first argument passed to **x()** is **0**, and the second argument is **1**, 9233aa99676SStefan Eßerwhile **i** is equal to **2** before the function starts executing. 9243aa99676SStefan Eßer 9253aa99676SStefan Eßer# FUNCTIONS 9263aa99676SStefan Eßer 9273aa99676SStefan EßerFunction definitions are as follows: 9283aa99676SStefan Eßer 9293aa99676SStefan Eßer``` 9303aa99676SStefan Eßerdefine I(I,...,I){ 9313aa99676SStefan Eßer auto I,...,I 9323aa99676SStefan Eßer S;...;S 9333aa99676SStefan Eßer return(E) 9343aa99676SStefan Eßer} 9353aa99676SStefan Eßer``` 9363aa99676SStefan Eßer 9373aa99676SStefan EßerAny **I** in the parameter list or **auto** list may be replaced with **I[]** to 9383aa99676SStefan Eßermake a parameter or **auto** var an array, and any **I** in the parameter list 9393aa99676SStefan Eßermay be replaced with **\*I[]** to make a parameter an array reference. Callers 9403aa99676SStefan Eßerof functions that take array references should not put an asterisk in the call; 9413aa99676SStefan Eßerthey must be called with just **I[]** like normal array parameters and will be 9423aa99676SStefan Eßerautomatically converted into references. 9433aa99676SStefan Eßer 9443aa99676SStefan EßerAs a **non-portable extension**, the opening brace of a **define** statement may 9453aa99676SStefan Eßerappear on the next line. 9463aa99676SStefan Eßer 9473aa99676SStefan EßerAs a **non-portable extension**, the return statement may also be in one of the 9483aa99676SStefan Eßerfollowing forms: 9493aa99676SStefan Eßer 9503aa99676SStefan Eßer1. **return** 9513aa99676SStefan Eßer2. **return** **(** **)** 9523aa99676SStefan Eßer3. **return** **E** 9533aa99676SStefan Eßer 9543aa99676SStefan EßerThe first two, or not specifying a **return** statement, is equivalent to 9553aa99676SStefan Eßer**return (0)**, unless the function is a **void** function (see the *Void 9563aa99676SStefan EßerFunctions* subsection below). 9573aa99676SStefan Eßer 9583aa99676SStefan Eßer## Void Functions 9593aa99676SStefan Eßer 9603aa99676SStefan EßerFunctions can also be **void** functions, defined as follows: 9613aa99676SStefan Eßer 9623aa99676SStefan Eßer``` 9633aa99676SStefan Eßerdefine void I(I,...,I){ 9643aa99676SStefan Eßer auto I,...,I 9653aa99676SStefan Eßer S;...;S 9663aa99676SStefan Eßer return 9673aa99676SStefan Eßer} 9683aa99676SStefan Eßer``` 9693aa99676SStefan Eßer 9703aa99676SStefan EßerThey can only be used as standalone expressions, where such an expression would 9713aa99676SStefan Eßerbe printed alone, except in a print statement. 9723aa99676SStefan Eßer 9733aa99676SStefan EßerVoid functions can only use the first two **return** statements listed above. 9743aa99676SStefan EßerThey can also omit the return statement entirely. 9753aa99676SStefan Eßer 9763aa99676SStefan EßerThe word "void" is not treated as a keyword; it is still possible to have 9773aa99676SStefan Eßervariables, arrays, and functions named **void**. The word "void" is only 9783aa99676SStefan Eßertreated specially right after the **define** keyword. 9793aa99676SStefan Eßer 9803aa99676SStefan EßerThis is a **non-portable extension**. 9813aa99676SStefan Eßer 9823aa99676SStefan Eßer## Array References 9833aa99676SStefan Eßer 9843aa99676SStefan EßerFor any array in the parameter list, if the array is declared in the form 9853aa99676SStefan Eßer 9863aa99676SStefan Eßer``` 9873aa99676SStefan Eßer*I[] 9883aa99676SStefan Eßer``` 9893aa99676SStefan Eßer 9903aa99676SStefan Eßerit is a **reference**. Any changes to the array in the function are reflected, 9913aa99676SStefan Eßerwhen the function returns, to the array that was passed in. 9923aa99676SStefan Eßer 9933aa99676SStefan EßerOther than this, all function arguments are passed by value. 9943aa99676SStefan Eßer 9953aa99676SStefan EßerThis is a **non-portable extension**. 9963aa99676SStefan Eßer 9973aa99676SStefan Eßer# LIBRARY 9983aa99676SStefan Eßer 999028616d0SStefan EßerAll of the functions below are available when the **-l** or **-\-mathlib** 10003aa99676SStefan Eßercommand-line flags are given. 10013aa99676SStefan Eßer 10023aa99676SStefan Eßer## Standard Library 10033aa99676SStefan Eßer 1004d101cdd6SStefan EßerThe standard (see the **STANDARDS** section) defines the following functions for 1005d101cdd6SStefan Eßerthe math library: 10063aa99676SStefan Eßer 10073aa99676SStefan Eßer**s(x)** 10083aa99676SStefan Eßer 10093aa99676SStefan Eßer: Returns the sine of **x**, which is assumed to be in radians. 10103aa99676SStefan Eßer 10113aa99676SStefan Eßer This is a transcendental function (see the *Transcendental Functions* 10123aa99676SStefan Eßer subsection below). 10133aa99676SStefan Eßer 10143aa99676SStefan Eßer**c(x)** 10153aa99676SStefan Eßer 10163aa99676SStefan Eßer: Returns the cosine of **x**, which is assumed to be in radians. 10173aa99676SStefan Eßer 10183aa99676SStefan Eßer This is a transcendental function (see the *Transcendental Functions* 10193aa99676SStefan Eßer subsection below). 10203aa99676SStefan Eßer 10213aa99676SStefan Eßer**a(x)** 10223aa99676SStefan Eßer 10233aa99676SStefan Eßer: Returns the arctangent of **x**, in radians. 10243aa99676SStefan Eßer 10253aa99676SStefan Eßer This is a transcendental function (see the *Transcendental Functions* 10263aa99676SStefan Eßer subsection below). 10273aa99676SStefan Eßer 10283aa99676SStefan Eßer**l(x)** 10293aa99676SStefan Eßer 10303aa99676SStefan Eßer: Returns the natural logarithm of **x**. 10313aa99676SStefan Eßer 10323aa99676SStefan Eßer This is a transcendental function (see the *Transcendental Functions* 10333aa99676SStefan Eßer subsection below). 10343aa99676SStefan Eßer 10353aa99676SStefan Eßer**e(x)** 10363aa99676SStefan Eßer 10373aa99676SStefan Eßer: Returns the mathematical constant **e** raised to the power of **x**. 10383aa99676SStefan Eßer 10393aa99676SStefan Eßer This is a transcendental function (see the *Transcendental Functions* 10403aa99676SStefan Eßer subsection below). 10413aa99676SStefan Eßer 10423aa99676SStefan Eßer**j(x, n)** 10433aa99676SStefan Eßer 10443aa99676SStefan Eßer: Returns the bessel integer order **n** (truncated) of **x**. 10453aa99676SStefan Eßer 10463aa99676SStefan Eßer This is a transcendental function (see the *Transcendental Functions* 10473aa99676SStefan Eßer subsection below). 10483aa99676SStefan Eßer 10493aa99676SStefan Eßer## Transcendental Functions 10503aa99676SStefan Eßer 105178bc019dSStefan EßerAll transcendental functions can return slightly inaccurate results, up to 1 ULP 105278bc019dSStefan Eßer(https://en.wikipedia.org/wiki/Unit_in_the_last_place). This is unavoidable, and 105378bc019dSStefan Eßerthe article at https://people.eecs.berkeley.edu/~wkahan/LOG10HAF.TXT explains 105478bc019dSStefan Eßerwhy it is impossible and unnecessary to calculate exact results for the 105578bc019dSStefan Eßertranscendental functions. 10563aa99676SStefan Eßer 10573aa99676SStefan EßerBecause of the possible inaccuracy, I recommend that users call those functions 10583aa99676SStefan Eßerwith the precision (**scale**) set to at least 1 higher than is necessary. If 10593aa99676SStefan Eßerexact results are *absolutely* required, users can double the precision 10603aa99676SStefan Eßer(**scale**) and then truncate. 10613aa99676SStefan Eßer 10623aa99676SStefan EßerThe transcendental functions in the standard math library are: 10633aa99676SStefan Eßer 10643aa99676SStefan Eßer* **s(x)** 10653aa99676SStefan Eßer* **c(x)** 10663aa99676SStefan Eßer* **a(x)** 10673aa99676SStefan Eßer* **l(x)** 10683aa99676SStefan Eßer* **e(x)** 10693aa99676SStefan Eßer* **j(x, n)** 10703aa99676SStefan Eßer 10713aa99676SStefan Eßer# RESET 10723aa99676SStefan Eßer 10733aa99676SStefan EßerWhen bc(1) encounters an error or a signal that it has a non-default handler 10743aa99676SStefan Eßerfor, it resets. This means that several things happen. 10753aa99676SStefan Eßer 10763aa99676SStefan EßerFirst, any functions that are executing are stopped and popped off the stack. 10773aa99676SStefan EßerThe behavior is not unlike that of exceptions in programming languages. Then 10783aa99676SStefan Eßerthe execution point is set so that any code waiting to execute (after all 10793aa99676SStefan Eßerfunctions returned) is skipped. 10803aa99676SStefan Eßer 10813aa99676SStefan EßerThus, when bc(1) resets, it skips any remaining code waiting to be executed. 10823aa99676SStefan EßerThen, if it is interactive mode, and the error was not a fatal error (see the 10833aa99676SStefan Eßer**EXIT STATUS** section), it asks for more input; otherwise, it exits with the 10843aa99676SStefan Eßerappropriate return code. 10853aa99676SStefan Eßer 10863aa99676SStefan EßerNote that this reset behavior is different from the GNU bc(1), which attempts to 10873aa99676SStefan Eßerstart executing the statement right after the one that caused an error. 10883aa99676SStefan Eßer 10893aa99676SStefan Eßer# PERFORMANCE 10903aa99676SStefan Eßer 10913aa99676SStefan EßerMost bc(1) implementations use **char** types to calculate the value of **1** 10923aa99676SStefan Eßerdecimal digit at a time, but that can be slow. This bc(1) does something 10933aa99676SStefan Eßerdifferent. 10943aa99676SStefan Eßer 10953aa99676SStefan EßerIt uses large integers to calculate more than **1** decimal digit at a time. If 10963aa99676SStefan Eßerbuilt in a environment where **BC_LONG_BIT** (see the **LIMITS** section) is 10973aa99676SStefan Eßer**64**, then each integer has **9** decimal digits. If built in an environment 10983aa99676SStefan Eßerwhere **BC_LONG_BIT** is **32** then each integer has **4** decimal digits. This 10993aa99676SStefan Eßervalue (the number of decimal digits per large integer) is called 11003aa99676SStefan Eßer**BC_BASE_DIGS**. 11013aa99676SStefan Eßer 11023aa99676SStefan EßerThe actual values of **BC_LONG_BIT** and **BC_BASE_DIGS** can be queried with 11033aa99676SStefan Eßerthe **limits** statement. 11043aa99676SStefan Eßer 11053aa99676SStefan EßerIn addition, this bc(1) uses an even larger integer for overflow checking. This 11063aa99676SStefan Eßerinteger type depends on the value of **BC_LONG_BIT**, but is always at least 11073aa99676SStefan Eßertwice as large as the integer type used to store digits. 11083aa99676SStefan Eßer 11093aa99676SStefan Eßer# LIMITS 11103aa99676SStefan Eßer 11113aa99676SStefan EßerThe following are the limits on bc(1): 11123aa99676SStefan Eßer 11133aa99676SStefan Eßer**BC_LONG_BIT** 11143aa99676SStefan Eßer 11153aa99676SStefan Eßer: The number of bits in the **long** type in the environment where bc(1) was 11163aa99676SStefan Eßer built. This determines how many decimal digits can be stored in a single 11173aa99676SStefan Eßer large integer (see the **PERFORMANCE** section). 11183aa99676SStefan Eßer 11193aa99676SStefan Eßer**BC_BASE_DIGS** 11203aa99676SStefan Eßer 11213aa99676SStefan Eßer: The number of decimal digits per large integer (see the **PERFORMANCE** 11223aa99676SStefan Eßer section). Depends on **BC_LONG_BIT**. 11233aa99676SStefan Eßer 11243aa99676SStefan Eßer**BC_BASE_POW** 11253aa99676SStefan Eßer 11263aa99676SStefan Eßer: The max decimal number that each large integer can store (see 11273aa99676SStefan Eßer **BC_BASE_DIGS**) plus **1**. Depends on **BC_BASE_DIGS**. 11283aa99676SStefan Eßer 11293aa99676SStefan Eßer**BC_OVERFLOW_MAX** 11303aa99676SStefan Eßer 11313aa99676SStefan Eßer: The max number that the overflow type (see the **PERFORMANCE** section) can 11323aa99676SStefan Eßer hold. Depends on **BC_LONG_BIT**. 11333aa99676SStefan Eßer 11343aa99676SStefan Eßer**BC_BASE_MAX** 11353aa99676SStefan Eßer 11363aa99676SStefan Eßer: The maximum output base. Set at **BC_BASE_POW**. 11373aa99676SStefan Eßer 11383aa99676SStefan Eßer**BC_DIM_MAX** 11393aa99676SStefan Eßer 11403aa99676SStefan Eßer: The maximum size of arrays. Set at **SIZE_MAX-1**. 11413aa99676SStefan Eßer 11423aa99676SStefan Eßer**BC_SCALE_MAX** 11433aa99676SStefan Eßer 11443aa99676SStefan Eßer: The maximum **scale**. Set at **BC_OVERFLOW_MAX-1**. 11453aa99676SStefan Eßer 11463aa99676SStefan Eßer**BC_STRING_MAX** 11473aa99676SStefan Eßer 11483aa99676SStefan Eßer: The maximum length of strings. Set at **BC_OVERFLOW_MAX-1**. 11493aa99676SStefan Eßer 11503aa99676SStefan Eßer**BC_NAME_MAX** 11513aa99676SStefan Eßer 11523aa99676SStefan Eßer: The maximum length of identifiers. Set at **BC_OVERFLOW_MAX-1**. 11533aa99676SStefan Eßer 11543aa99676SStefan Eßer**BC_NUM_MAX** 11553aa99676SStefan Eßer 11563aa99676SStefan Eßer: The maximum length of a number (in decimal digits), which includes digits 11573aa99676SStefan Eßer after the decimal point. Set at **BC_OVERFLOW_MAX-1**. 11583aa99676SStefan Eßer 11593aa99676SStefan EßerExponent 11603aa99676SStefan Eßer 11613aa99676SStefan Eßer: The maximum allowable exponent (positive or negative). Set at 11623aa99676SStefan Eßer **BC_OVERFLOW_MAX**. 11633aa99676SStefan Eßer 11643aa99676SStefan EßerNumber of vars 11653aa99676SStefan Eßer 11663aa99676SStefan Eßer: The maximum number of vars/arrays. Set at **SIZE_MAX-1**. 11673aa99676SStefan Eßer 11683aa99676SStefan EßerThe actual values can be queried with the **limits** statement. 11693aa99676SStefan Eßer 11703aa99676SStefan EßerThese limits are meant to be effectively non-existent; the limits are so large 11713aa99676SStefan Eßer(at least on 64-bit machines) that there should not be any point at which they 11723aa99676SStefan Eßerbecome a problem. In fact, memory should be exhausted before these limits should 11733aa99676SStefan Eßerbe hit. 11743aa99676SStefan Eßer 11753aa99676SStefan Eßer# ENVIRONMENT VARIABLES 11763aa99676SStefan Eßer 1177d101cdd6SStefan EßerAs **non-portable extensions**, bc(1) recognizes the following environment 1178d101cdd6SStefan Eßervariables: 11793aa99676SStefan Eßer 11803aa99676SStefan Eßer**POSIXLY_CORRECT** 11813aa99676SStefan Eßer 11823aa99676SStefan Eßer: If this variable exists (no matter the contents), bc(1) behaves as if 11833aa99676SStefan Eßer the **-s** option was given. 11843aa99676SStefan Eßer 11853aa99676SStefan Eßer**BC_ENV_ARGS** 11863aa99676SStefan Eßer 11873aa99676SStefan Eßer: This is another way to give command-line arguments to bc(1). They should be 11883aa99676SStefan Eßer in the same format as all other command-line arguments. These are always 11893aa99676SStefan Eßer processed first, so any files given in **BC_ENV_ARGS** will be processed 11903aa99676SStefan Eßer before arguments and files given on the command-line. This gives the user 11913aa99676SStefan Eßer the ability to set up "standard" options and files to be used at every 11923aa99676SStefan Eßer invocation. The most useful thing for such files to contain would be useful 11933aa99676SStefan Eßer functions that the user might want every time bc(1) runs. 11943aa99676SStefan Eßer 11953aa99676SStefan Eßer The code that parses **BC_ENV_ARGS** will correctly handle quoted arguments, 11963aa99676SStefan Eßer but it does not understand escape sequences. For example, the string 11973aa99676SStefan Eßer **"/home/gavin/some bc file.bc"** will be correctly parsed, but the string 11983aa99676SStefan Eßer **"/home/gavin/some \"bc\" file.bc"** will include the backslashes. 11993aa99676SStefan Eßer 12003aa99676SStefan Eßer The quote parsing will handle either kind of quotes, **'** or **"**. Thus, 12013aa99676SStefan Eßer if you have a file with any number of single quotes in the name, you can use 12023aa99676SStefan Eßer double quotes as the outside quotes, as in **"some 'bc' file.bc"**, and vice 12033aa99676SStefan Eßer versa if you have a file with double quotes. However, handling a file with 12043aa99676SStefan Eßer both kinds of quotes in **BC_ENV_ARGS** is not supported due to the 12053aa99676SStefan Eßer complexity of the parsing, though such files are still supported on the 12063aa99676SStefan Eßer command-line where the parsing is done by the shell. 12073aa99676SStefan Eßer 12083aa99676SStefan Eßer**BC_LINE_LENGTH** 12093aa99676SStefan Eßer 12103aa99676SStefan Eßer: If this environment variable exists and contains an integer that is greater 12113aa99676SStefan Eßer than **1** and is less than **UINT16_MAX** (**2\^16-1**), bc(1) will output 12123aa99676SStefan Eßer lines to that length, including the backslash (**\\**). The default line 12133aa99676SStefan Eßer length is **70**. 12143aa99676SStefan Eßer 1215d43fa8efSStefan Eßer The special value of **0** will disable line length checking and print 1216d43fa8efSStefan Eßer numbers without regard to line length and without backslashes and newlines. 1217d43fa8efSStefan Eßer 121844d4804dSStefan Eßer**BC_BANNER** 121944d4804dSStefan Eßer 122044d4804dSStefan Eßer: If this environment variable exists and contains an integer, then a non-zero 122144d4804dSStefan Eßer value activates the copyright banner when bc(1) is in interactive mode, 122244d4804dSStefan Eßer while zero deactivates it. 122344d4804dSStefan Eßer 122444d4804dSStefan Eßer If bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section), 122544d4804dSStefan Eßer then this environment variable has no effect because bc(1) does not print 122644d4804dSStefan Eßer the banner when not in interactive mode. 122744d4804dSStefan Eßer 122844d4804dSStefan Eßer This environment variable overrides the default, which can be queried with 122944d4804dSStefan Eßer the **-h** or **-\-help** options. 123044d4804dSStefan Eßer 123144d4804dSStefan Eßer**BC_SIGINT_RESET** 123244d4804dSStefan Eßer 123344d4804dSStefan Eßer: If bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section), 123444d4804dSStefan Eßer then this environment variable has no effect because bc(1) exits on 123544d4804dSStefan Eßer **SIGINT** when not in interactive mode. 123644d4804dSStefan Eßer 123744d4804dSStefan Eßer However, when bc(1) is in interactive mode, then if this environment 123844d4804dSStefan Eßer variable exists and contains an integer, a non-zero value makes bc(1) reset 123944d4804dSStefan Eßer on **SIGINT**, rather than exit, and zero makes bc(1) exit. If this 124044d4804dSStefan Eßer environment variable exists and is *not* an integer, then bc(1) will exit on 124144d4804dSStefan Eßer **SIGINT**. 124244d4804dSStefan Eßer 124344d4804dSStefan Eßer This environment variable overrides the default, which can be queried with 124444d4804dSStefan Eßer the **-h** or **-\-help** options. 124544d4804dSStefan Eßer 124644d4804dSStefan Eßer**BC_TTY_MODE** 124744d4804dSStefan Eßer 124844d4804dSStefan Eßer: If TTY mode is *not* available (see the **TTY MODE** section), then this 124944d4804dSStefan Eßer environment variable has no effect. 125044d4804dSStefan Eßer 125144d4804dSStefan Eßer However, when TTY mode is available, then if this environment variable 125244d4804dSStefan Eßer exists and contains an integer, then a non-zero value makes bc(1) use TTY 125344d4804dSStefan Eßer mode, and zero makes bc(1) not use TTY mode. 125444d4804dSStefan Eßer 125544d4804dSStefan Eßer This environment variable overrides the default, which can be queried with 125644d4804dSStefan Eßer the **-h** or **-\-help** options. 125744d4804dSStefan Eßer 125844d4804dSStefan Eßer**BC_PROMPT** 125944d4804dSStefan Eßer 126044d4804dSStefan Eßer: If TTY mode is *not* available (see the **TTY MODE** section), then this 126144d4804dSStefan Eßer environment variable has no effect. 126244d4804dSStefan Eßer 126344d4804dSStefan Eßer However, when TTY mode is available, then if this environment variable 126444d4804dSStefan Eßer exists and contains an integer, a non-zero value makes bc(1) use a prompt, 126544d4804dSStefan Eßer and zero or a non-integer makes bc(1) not use a prompt. If this environment 126644d4804dSStefan Eßer variable does not exist and **BC_TTY_MODE** does, then the value of the 126744d4804dSStefan Eßer **BC_TTY_MODE** environment variable is used. 126844d4804dSStefan Eßer 126944d4804dSStefan Eßer This environment variable and the **BC_TTY_MODE** environment variable 127044d4804dSStefan Eßer override the default, which can be queried with the **-h** or **-\-help** 127144d4804dSStefan Eßer options. 127244d4804dSStefan Eßer 127310041e99SStefan Eßer**BC_EXPR_EXIT** 127410041e99SStefan Eßer 127510041e99SStefan Eßer: If any expressions or expression files are given on the command-line with 127610041e99SStefan Eßer **-e**, **-\-expression**, **-f**, or **-\-file**, then if this environment 127710041e99SStefan Eßer variable exists and contains an integer, a non-zero value makes bc(1) exit 127878bc019dSStefan Eßer after executing the expressions and expression files, and a zero value makes 127978bc019dSStefan Eßer bc(1) not exit. 128010041e99SStefan Eßer 128110041e99SStefan Eßer This environment variable overrides the default, which can be queried with 128210041e99SStefan Eßer the **-h** or **-\-help** options. 128310041e99SStefan Eßer 1284d101cdd6SStefan Eßer**BC_DIGIT_CLAMP** 1285d101cdd6SStefan Eßer 1286d101cdd6SStefan Eßer: When parsing numbers and if this environment variable exists and contains an 1287d101cdd6SStefan Eßer integer, a non-zero value makes bc(1) clamp digits that are greater than or 1288d101cdd6SStefan Eßer equal to the current **ibase** so that all such digits are considered equal 1289d101cdd6SStefan Eßer to the **ibase** minus 1, and a zero value disables such clamping so that 1290d101cdd6SStefan Eßer those digits are always equal to their value, which is multiplied by the 1291d101cdd6SStefan Eßer power of the **ibase**. 1292d101cdd6SStefan Eßer 1293d101cdd6SStefan Eßer This never applies to single-digit numbers, as per the standard (see the 1294d101cdd6SStefan Eßer **STANDARDS** section). 1295d101cdd6SStefan Eßer 1296d101cdd6SStefan Eßer This environment variable overrides the default, which can be queried with 1297d101cdd6SStefan Eßer the **-h** or **-\-help** options. 1298d101cdd6SStefan Eßer 12993aa99676SStefan Eßer# EXIT STATUS 13003aa99676SStefan Eßer 13013aa99676SStefan Eßerbc(1) returns the following exit statuses: 13023aa99676SStefan Eßer 13033aa99676SStefan Eßer**0** 13043aa99676SStefan Eßer 13053aa99676SStefan Eßer: No error. 13063aa99676SStefan Eßer 13073aa99676SStefan Eßer**1** 13083aa99676SStefan Eßer 13093aa99676SStefan Eßer: A math error occurred. This follows standard practice of using **1** for 13103aa99676SStefan Eßer expected errors, since math errors will happen in the process of normal 13113aa99676SStefan Eßer execution. 13123aa99676SStefan Eßer 13133aa99676SStefan Eßer Math errors include divide by **0**, taking the square root of a negative 13143aa99676SStefan Eßer number, attempting to convert a negative number to a hardware integer, 131544d4804dSStefan Eßer overflow when converting a number to a hardware integer, overflow when 131644d4804dSStefan Eßer calculating the size of a number, and attempting to use a non-integer where 131744d4804dSStefan Eßer an integer is required. 13183aa99676SStefan Eßer 13193aa99676SStefan Eßer Converting to a hardware integer happens for the second operand of the power 13203aa99676SStefan Eßer (**\^**) operator and the corresponding assignment operator. 13213aa99676SStefan Eßer 13223aa99676SStefan Eßer**2** 13233aa99676SStefan Eßer 13243aa99676SStefan Eßer: A parse error occurred. 13253aa99676SStefan Eßer 13263aa99676SStefan Eßer Parse errors include unexpected **EOF**, using an invalid character, failing 13273aa99676SStefan Eßer to find the end of a string or comment, using a token where it is invalid, 13283aa99676SStefan Eßer giving an invalid expression, giving an invalid print statement, giving an 13293aa99676SStefan Eßer invalid function definition, attempting to assign to an expression that is 13303aa99676SStefan Eßer not a named expression (see the *Named Expressions* subsection of the 13313aa99676SStefan Eßer **SYNTAX** section), giving an invalid **auto** list, having a duplicate 13323aa99676SStefan Eßer **auto**/function parameter, failing to find the end of a code block, 13333aa99676SStefan Eßer attempting to return a value from a **void** function, attempting to use a 13343aa99676SStefan Eßer variable as a reference, and using any extensions when the option **-s** or 13353aa99676SStefan Eßer any equivalents were given. 13363aa99676SStefan Eßer 13373aa99676SStefan Eßer**3** 13383aa99676SStefan Eßer 13393aa99676SStefan Eßer: A runtime error occurred. 13403aa99676SStefan Eßer 134144d4804dSStefan Eßer Runtime errors include assigning an invalid number to any global (**ibase**, 134244d4804dSStefan Eßer **obase**, or **scale**), giving a bad expression to a **read()** call, 134344d4804dSStefan Eßer calling **read()** inside of a **read()** call, type errors, passing the 134444d4804dSStefan Eßer wrong number of arguments to functions, attempting to call an undefined 134544d4804dSStefan Eßer function, and attempting to use a **void** function call as a value in an 134644d4804dSStefan Eßer expression. 13473aa99676SStefan Eßer 13483aa99676SStefan Eßer**4** 13493aa99676SStefan Eßer 13503aa99676SStefan Eßer: A fatal error occurred. 13513aa99676SStefan Eßer 13523aa99676SStefan Eßer Fatal errors include memory allocation errors, I/O errors, failing to open 13533aa99676SStefan Eßer files, attempting to use files that do not have only ASCII characters (bc(1) 13543aa99676SStefan Eßer only accepts ASCII characters), attempting to open a directory as a file, 13553aa99676SStefan Eßer and giving invalid command-line options. 13563aa99676SStefan Eßer 13573aa99676SStefan EßerThe exit status **4** is special; when a fatal error occurs, bc(1) always exits 13583aa99676SStefan Eßerand returns **4**, no matter what mode bc(1) is in. 13593aa99676SStefan Eßer 13603aa99676SStefan EßerThe other statuses will only be returned when bc(1) is not in interactive mode 13613aa99676SStefan Eßer(see the **INTERACTIVE MODE** section), since bc(1) resets its state (see the 13623aa99676SStefan Eßer**RESET** section) and accepts more input when one of those errors occurs in 13633aa99676SStefan Eßerinteractive mode. This is also the case when interactive mode is forced by the 1364028616d0SStefan Eßer**-i** flag or **-\-interactive** option. 13653aa99676SStefan Eßer 13663aa99676SStefan EßerThese exit statuses allow bc(1) to be used in shell scripting with error 13673aa99676SStefan Eßerchecking, and its normal behavior can be forced by using the **-i** flag or 1368028616d0SStefan Eßer**-\-interactive** option. 13693aa99676SStefan Eßer 13703aa99676SStefan Eßer# INTERACTIVE MODE 13713aa99676SStefan Eßer 1372d101cdd6SStefan EßerPer the standard (see the **STANDARDS** section), bc(1) has an interactive mode 1373d101cdd6SStefan Eßerand a non-interactive mode. Interactive mode is turned on automatically when 1374d101cdd6SStefan Eßerboth **stdin** and **stdout** are hooked to a terminal, but the **-i** flag and 1375d101cdd6SStefan Eßer**-\-interactive** option can turn it on in other situations. 13763aa99676SStefan Eßer 13773aa99676SStefan EßerIn interactive mode, bc(1) attempts to recover from errors (see the **RESET** 13783aa99676SStefan Eßersection), and in normal execution, flushes **stdout** as soon as execution is 137944d4804dSStefan Eßerdone for the current input. bc(1) may also reset on **SIGINT** instead of exit, 138044d4804dSStefan Eßerdepending on the contents of, or default for, the **BC_SIGINT_RESET** 138144d4804dSStefan Eßerenvironment variable (see the **ENVIRONMENT VARIABLES** section). 13823aa99676SStefan Eßer 13833aa99676SStefan Eßer# TTY MODE 13843aa99676SStefan Eßer 138544d4804dSStefan EßerIf **stdin**, **stdout**, and **stderr** are all connected to a TTY, then "TTY 138644d4804dSStefan Eßermode" is considered to be available, and thus, bc(1) can turn on TTY mode, 138744d4804dSStefan Eßersubject to some settings. 13883aa99676SStefan Eßer 138944d4804dSStefan EßerIf there is the environment variable **BC_TTY_MODE** in the environment (see the 139044d4804dSStefan Eßer**ENVIRONMENT VARIABLES** section), then if that environment variable contains a 139144d4804dSStefan Eßernon-zero integer, bc(1) will turn on TTY mode when **stdin**, **stdout**, and 139244d4804dSStefan Eßer**stderr** are all connected to a TTY. If the **BC_TTY_MODE** environment 139344d4804dSStefan Eßervariable exists but is *not* a non-zero integer, then bc(1) will not turn TTY 139444d4804dSStefan Eßermode on. 13953aa99676SStefan Eßer 139644d4804dSStefan EßerIf the environment variable **BC_TTY_MODE** does *not* exist, the default 139744d4804dSStefan Eßersetting is used. The default setting can be queried with the **-h** or 139844d4804dSStefan Eßer**-\-help** options. 13993aa99676SStefan Eßer 14003aa99676SStefan EßerTTY mode is different from interactive mode because interactive mode is required 1401d101cdd6SStefan Eßerin the bc(1) standard (see the **STANDARDS** section), and interactive mode 1402d101cdd6SStefan Eßerrequires only **stdin** and **stdout** to be connected to a terminal. 14033aa99676SStefan Eßer 140444d4804dSStefan Eßer## Command-Line History 140544d4804dSStefan Eßer 140644d4804dSStefan EßerCommand-line history is only enabled if TTY mode is, i.e., that **stdin**, 140744d4804dSStefan Eßer**stdout**, and **stderr** are connected to a TTY and the **BC_TTY_MODE** 140844d4804dSStefan Eßerenvironment variable (see the **ENVIRONMENT VARIABLES** section) and its default 140944d4804dSStefan Eßerdo not disable TTY mode. See the **COMMAND LINE HISTORY** section for more 141044d4804dSStefan Eßerinformation. 141144d4804dSStefan Eßer 141244d4804dSStefan Eßer## Prompt 141344d4804dSStefan Eßer 141444d4804dSStefan EßerIf TTY mode is available, then a prompt can be enabled. Like TTY mode itself, it 141544d4804dSStefan Eßercan be turned on or off with an environment variable: **BC_PROMPT** (see the 141644d4804dSStefan Eßer**ENVIRONMENT VARIABLES** section). 141744d4804dSStefan Eßer 141844d4804dSStefan EßerIf the environment variable **BC_PROMPT** exists and is a non-zero integer, then 141944d4804dSStefan Eßerthe prompt is turned on when **stdin**, **stdout**, and **stderr** are connected 142044d4804dSStefan Eßerto a TTY and the **-P** and **-\-no-prompt** options were not used. The read 142144d4804dSStefan Eßerprompt will be turned on under the same conditions, except that the **-R** and 142244d4804dSStefan Eßer**-\-no-read-prompt** options must also not be used. 142344d4804dSStefan Eßer 142444d4804dSStefan EßerHowever, if **BC_PROMPT** does not exist, the prompt can be enabled or disabled 142544d4804dSStefan Eßerwith the **BC_TTY_MODE** environment variable, the **-P** and **-\-no-prompt** 142644d4804dSStefan Eßeroptions, and the **-R** and **-\-no-read-prompt** options. See the **ENVIRONMENT 142744d4804dSStefan EßerVARIABLES** and **OPTIONS** sections for more details. 142844d4804dSStefan Eßer 14293aa99676SStefan Eßer# SIGNAL HANDLING 14303aa99676SStefan Eßer 143144d4804dSStefan EßerSending a **SIGINT** will cause bc(1) to do one of two things. 143244d4804dSStefan Eßer 143344d4804dSStefan EßerIf bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section), or 143444d4804dSStefan Eßerthe **BC_SIGINT_RESET** environment variable (see the **ENVIRONMENT VARIABLES** 143544d4804dSStefan Eßersection), or its default, is either not an integer or it is zero, bc(1) will 143644d4804dSStefan Eßerexit. 143744d4804dSStefan Eßer 143844d4804dSStefan EßerHowever, if bc(1) is in interactive mode, and the **BC_SIGINT_RESET** or its 143944d4804dSStefan Eßerdefault is an integer and non-zero, then bc(1) will stop executing the current 144044d4804dSStefan Eßerinput and reset (see the **RESET** section) upon receiving a **SIGINT**. 14413aa99676SStefan Eßer 14423aa99676SStefan EßerNote that "current input" can mean one of two things. If bc(1) is processing 144344d4804dSStefan Eßerinput from **stdin** in interactive mode, it will ask for more input. If bc(1) 144444d4804dSStefan Eßeris processing input from a file in interactive mode, it will stop processing the 144544d4804dSStefan Eßerfile and start processing the next file, if one exists, or ask for input from 144644d4804dSStefan Eßer**stdin** if no other file exists. 14473aa99676SStefan Eßer 14483aa99676SStefan EßerThis means that if a **SIGINT** is sent to bc(1) as it is executing a file, it 14493aa99676SStefan Eßercan seem as though bc(1) did not respond to the signal since it will immediately 14503aa99676SStefan Eßerstart executing the next file. This is by design; most files that users execute 14513aa99676SStefan Eßerwhen interacting with bc(1) have function definitions, which are quick to parse. 14523aa99676SStefan EßerIf a file takes a long time to execute, there may be a bug in that file. The 14533aa99676SStefan Eßerrest of the files could still be executed without problem, allowing the user to 14543aa99676SStefan Eßercontinue. 14553aa99676SStefan Eßer 14563aa99676SStefan Eßer**SIGTERM** and **SIGQUIT** cause bc(1) to clean up and exit, and it uses the 14573aa99676SStefan Eßerdefault handler for all other signals. The one exception is **SIGHUP**; in that 145844d4804dSStefan Eßercase, and only when bc(1) is in TTY mode (see the **TTY MODE** section), a 145944d4804dSStefan Eßer**SIGHUP** will cause bc(1) to clean up and exit. 14603aa99676SStefan Eßer 14613aa99676SStefan Eßer# COMMAND LINE HISTORY 14623aa99676SStefan Eßer 146344d4804dSStefan Eßerbc(1) supports interactive command-line editing. 146444d4804dSStefan Eßer 146544d4804dSStefan EßerIf bc(1) can be in TTY mode (see the **TTY MODE** section), history can be 146644d4804dSStefan Eßerenabled. This means that command-line history can only be enabled when 146744d4804dSStefan Eßer**stdin**, **stdout**, and **stderr** are all connected to a TTY. 146844d4804dSStefan Eßer 146944d4804dSStefan EßerLike TTY mode itself, it can be turned on or off with the environment variable 147044d4804dSStefan Eßer**BC_TTY_MODE** (see the **ENVIRONMENT VARIABLES** section). 147144d4804dSStefan Eßer 147244d4804dSStefan EßerIf history is enabled, previous lines can be recalled and edited with the arrow 147344d4804dSStefan Eßerkeys. 14743aa99676SStefan Eßer 14753aa99676SStefan Eßer**Note**: tabs are converted to 8 spaces. 14763aa99676SStefan Eßer 14773aa99676SStefan Eßer# LOCALES 14783aa99676SStefan Eßer 14793aa99676SStefan EßerThis bc(1) ships with support for adding error messages for different locales 14803aa99676SStefan Eßerand thus, supports **LC_MESSAGES**. 14813aa99676SStefan Eßer 14823aa99676SStefan Eßer# SEE ALSO 14833aa99676SStefan Eßer 14843aa99676SStefan Eßerdc(1) 14853aa99676SStefan Eßer 14863aa99676SStefan Eßer# STANDARDS 14873aa99676SStefan Eßer 148878bc019dSStefan Eßerbc(1) is compliant with the IEEE Std 1003.1-2017 (“POSIX.1-2017”) specification 148978bc019dSStefan Eßerat https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html . The 149078bc019dSStefan Eßerflags **-efghiqsvVw**, all long options, and the extensions noted above are 149178bc019dSStefan Eßerextensions to that specification. 14923aa99676SStefan Eßer 1493d101cdd6SStefan EßerIn addition, the behavior of the **quit** implements an interpretation of that 1494d101cdd6SStefan Eßerspecification that is different from all known implementations. For more 1495d101cdd6SStefan Eßerinformation see the **Statements** subsection of the **SYNTAX** section. 1496d101cdd6SStefan Eßer 14973aa99676SStefan EßerNote that the specification explicitly says that bc(1) only accepts numbers that 14983aa99676SStefan Eßeruse a period (**.**) as a radix point, regardless of the value of 14993aa99676SStefan Eßer**LC_NUMERIC**. 15003aa99676SStefan Eßer 15013aa99676SStefan EßerThis bc(1) supports error messages for different locales, and thus, it supports 15023aa99676SStefan Eßer**LC_MESSAGES**. 15033aa99676SStefan Eßer 15043aa99676SStefan Eßer# BUGS 15053aa99676SStefan Eßer 1506d101cdd6SStefan EßerBefore version **6.1.0**, this bc(1) had incorrect behavior for the **quit** 1507d101cdd6SStefan Eßerstatement. 1508d101cdd6SStefan Eßer 15094fca8e0fSStefan EßerNo other bugs are known. Report bugs at https://git.gavinhoward.com/gavin/bc . 15103aa99676SStefan Eßer 15113aa99676SStefan Eßer# AUTHORS 15123aa99676SStefan Eßer 15134fca8e0fSStefan EßerGavin D. Howard <gavin@gavinhoward.com> and contributors. 1514