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