diff --git a/contrib/bc/NEWS.md b/contrib/bc/NEWS.md index d4b160d74d35..240e02f11388 100644 --- a/contrib/bc/NEWS.md +++ b/contrib/bc/NEWS.md @@ -1,1534 +1,1546 @@ # News +## 6.7.4 + +This is a production release to fix problems in the `bc` manual. + +Users only need to update if desired. + +## 6.7.3 + +This is a production release to fix the library build on Mac OSX. + +Users on other platforms do *not* need to update. + ## 6.7.2 This is a production release to remove some debugging code that I accidentally committed. ## 6.7.1 This is a production release with a bug fix for `SIGINT` only being handled once. ## 6.7.0 This is a production release with three new functions in the [extended math library][16]: `min()`, `max()`, and `i2rand()`. ## 6.6.1 This is a production release with an improved `p()` function in the [extended math library][16]. Users who don't care do not need to upgrade. ## 6.6.0 This is a production release with two bug fixes and one change. The first bug fix is to fix the build on Mac OSX. The second bug was to remove printing a leading zero in scientific or engineering output modes. The change was that the implementation of `irand()` was improved to call the PRNG less. ## 6.5.0 This is a production release that fixes an infinite loop bug in `root()` and `cbrt()`, fixes a bug with `BC_LINE_LENGTH=0`, and adds the `fib()` function to the extended math library to calculate Fibonacci numbers. ## 6.4.0 This is a production release that fixes a `read()`/`?` bug and adds features to `bcl`. The bug was that multiple read calls could repeat old data. The new features in `bcl` are functions to preserve `BclNumber` arguments and not free them. ***WARNING for `bcl` Users***: The `bcl_rand_seedWithNum()` function used to not consume its arguments. Now it does. This change could have made this version `7.0.0`, but I'm 99.9% confident that there are no `bcl` users, or if there are, they probably don't use the PRNG. So I took a risk and didn't update the major version. `bcl` now includes more capacity to check for invalid numbers when built to run under Valgrind. ## 6.3.1 This is a production release that fixes a `bc` dependency loop for minimal environments and Linux from Scratch. ## 6.3.0 This is a production release with a couple of fixes for manuals and a new feature for `dc`: there is now a command to query whether extended registers are enabled or not. Users who don't care do not need to upgrade. ## 6.2.6 This is a production release that fixes an install bug that affected locale installation of all locales when using `mksh`. Users do ***NOT*** need to upgrade if they don't use `mksh` and/or don't need to install all locales. ## 6.2.5 This is a production release that fixes a test bug that affected Android and `mksh`. Users do ***NOT*** need to upgrade unless they use `mksh` or another affected shell and need to run the test suite. ## 6.2.4 This is a production release that fixes a test failure that happens when `tests/bc/scripts/timeconst.bc` doesn't exist. This should only affect packagers. This bug happened because I forgot something I added in the previous release: better error checking in tests to help packagers. Unfortunately, I was too zealous with the error checking. ## 6.2.3 This is a production release that moves `bc` to . That's all it does: update links. Users do ***NOT*** need to upgrade; there are redirects that will stay in place indefinitely. This release is only for new users. ## 6.2.2 This is a production release that fixes a bug. The bug was that if an array element was used as a parameter, and then a later parameter had the same name as the array whose element was used, `bc` would grab the element from the new array parameter, not the actual element from before the function call. ## 6.2.1 This is a production release with one bug fix for a memory bug in history. ## 6.2.0 This is a production release with a new feature and a few bug fixes. The bug fixes include: * A crash when `bc` and `dc` are built using editline, but history is not activated. * A missing local in the `uint*()` family of functions in the extended math library. * A failure to clear the tail call list in `dc` on error. * A crash when attempting to swap characters in command-line history when no characters exist. * `SIGWINCH` was activated even when history was not. The new feature is that stack traces are now given for runtime errors. In debug mode, the C source file and line of errors are given as well. ## 6.1.1 This is a production release that fixes a build issue with predefined builds and generated tests. ## 6.1.0 This is a production release that fixes a discrepancy from the `bc` standard, a couple of memory bugs, and adds new features. The discrepancy from the `bc` standard was with regards to the behavior of the `quit` command. This `bc` used to quit whenever it encountered `quit` during parsing, even if it was parsing a full file. Now, `bc` only quits when encountering `quit` *after* it has executed all executable statements up to that point. This behavior is slightly different from GNU `bc`, but users will only notice the difference if they put `quit` on the same line as other statements. The first memory bug could be reproduced by assigning a string to a non-local variable in a function, then redefining the function with use of the same non-local variable, which would still refer to a string in the previous version of the function. The second memory bug was caused by passing an array argument to the `asciify()` built-in function. In certain cases, that was wrongly allowed, and the interpreter just assumed everything was correct and accessed memory. Now that arrays are allowed as arguments (see below), this is not an issue. The first feature was the addition of the `is_number()` built-in function (`u` in `dc`) that returns 1 if the runtime argument is a number and 0 otherwise. The second feature was the addition of the `is_string()` built-in function (`t` in `dc`) that returns 1 if the runtime argument is a string and 0 otherwise. These features were added because I realized that type-checking is necessary now that strings can be assigned to variables in `bc` and because they've always been assignable to variables in `dc`. The last added feature is the ability of the `asciify()` built-in function in `bc` to convert a full array of numbers into a string. This means that character-by-character printing will not be necessary, and more strings than just single-character ones will be able to be created. ## 6.0.4 This is a production release that most users will not need to upgrade to. This fixes a build bug for `bcl` only on OpenBSD. Users that do not need `bcl` or have not run into build errors with `bcl` do ***NOT*** need to upgrade. ## 6.0.3 This is a production release that fixes a build bug for cross-compilation. Users that do not need cross-compilation do ***NOT*** need to upgrade. ## 6.0.2 This is a production release that fixes two bugs: * The `-l` option overrode the `-S` option. * A double-free and crash when sending a `SIGINT` while executing expressions given on the command-line. ## 6.0.1 This is a production release that fixes memory bugs and memory leaks in `bcl`. Users that do not use `bcl` (use only `bc` and/or `dc`) do ***NOT*** need to upgrade. These happened because I was unaware that the `bcl` test was not hooked into the Valgrind test infrastructure. Then, when I ran the release script, which tests everything under Valgrind (or so I thought), it caught nothing, and I thought it was safe. But it was not. Nevertheless, I have now run it under Valgrind and fixed all of the memory bugs (caused by not using `memset()` where I should have but previously didn't have to) and memory leaks. ## 6.0.0 This is a production release that fixes an oversight in the `bc` parser (that sometimes caused the wrong error message) and adds a feature for compatibility with the BSD `bc` and `dc`: turning off digit clamping when parsing numbers. The default for clamping can be set during the build (see the [build manual][13]), it can be set with the `BC_DIGIT_CLAMP` and `DC_DIGIT_CLAMP` environment variables, and it can be set with the `-c` and `-C` command-line options. Turning off clamping was also added to the `bcl` library. In addition, signal handling was removed from the `bcl` library in order to add the capability for multi-threading. This required a major version bump. I apologize to all library users (I don't know of any), but signals and threads do not play well together. To help with building, a convenience option (`-p`) to `configure.sh` was added to build a `bc` and `dc` that is by default compatible with either the BSD `bc` and `dc` or the GNU `bc` and `dc`. ## 5.3.3 This is a production release that fixes a build problem in the FreeBSD base system. All other users do **NOT** need to upgrade. ## 5.3.2 This is a production release that fixes prompt bugs with editline and readline where the `BC_PROMPT` environment variable was not being respected. This also fixes editline and readline output on `EOF`. ## 5.3.1 This is a production release that fixes a build problem in the FreeBSD base system, as well as a problem in the `en_US` locale. If you don't have problems with either, you do not need to upgrade. ## 5.3.0 This is a production release that adds features and has a few bug fixes. First, support for editline and readline history has been added. To use editline, pass `-e` to `configure.sh`, and to use readline, pass `-r`. Second, history support for Windows has been fixed and re-enabled. Third, command-line options to set `scale`, `ibase`, `obase`, and `seed` were added. This was requested long ago, and I originally disagreed with the idea. Fourth, the manuals had typos and were missing information. That has been fixed. Fifth, the manuals received different formatting to be more readable as manpages. ## 5.2.5 This is a production release that fixes this `bc`'s behavior on `^D` to match GNU `bc`. ## 5.2.4 This is a production release that fixes two bugs in history: * Without prompt, the cursor could not be placed on the first character in a line. * Home and End key handling in `tmux` was fixed. Any users that do not care about these improvements do not need to upgrade. ## 5.2.3 This is a production release that fixes one bug, a parse error when passing a file to `bc` using `-f` if that file had a multiline comment or string in it. ## 5.2.2 This is a production release that fixes one bug, a segmentation fault if `argv[0]` equals `NULL`. This is not a critical bug; there will be no vulnerability as far as I can tell. There is no need to update if you do not wish to. ## 5.2.1 This is a production release that fixes two parse bugs when in POSIX standard mode. One of these bugs was due to a quirk of the POSIX grammar, and the other was because `bc` was too strict. ## 5.2.0 This is a production release that adds a new feature, fixes some bugs, and adds out-of-source builds and a `pkg-config` file for `bcl`. The new feature is the ability to turn off exiting on expressions. It is also possible to set the default using `configure.sh`. This behavior used to exist with the `BC_EXPR_EXIT` environment variable, which is now used again. Bugs fixed include: * Some possible race conditions with error handling. * Install and uninstall targets for `bcl` did not work. ## 5.1.1 This is a production release that completes a bug fix from `5.1.0`. The bug exists in all versions of `bc`. The bug was that `if` statements without `else` statements would not be handled correctly at the end of files or right before a function definition. ## 5.1.0 This is a production release with some fixes and new features. * Fixed a bug where an `if` statement without an `else` before defining a function caused an error. * Fixed a bug with the `bc` banner and `-q`. * Fixed a bug on Windows where files were not read correctly. * Added a command-line flag (`-z`) to make `bc` and `dc` print leading zeroes on numbers `-1 < x < 1`. * Added four functions to `lib2.bc` (`plz()`, `plznl()`, `pnlz()`, and `pnlznl()`) to allow printing numbers with or without leading zeros, despite the use of `-z` or not. * Added builtin functions to query global state like line length, global stacks, and leading zeroes. * Added a command-line flag (`-L`) to disable wrapping when printing numbers. * Improved builds on Windows. ## 5.0.2 This is a production release with one fix for a flaky test. If you have not experienced problems with the test suite, you do ***NOT*** need to upgrade. The test was one that tested whether `bc` fails gracefully when it can't allocate memory. Unfortunately, there are cases when Linux and FreeBSD lie and pretend to allocate the memory. The reason they do this is because a lot of programs don't use all of the memory they allocate, so those OS's usually get away with it. However, this `bc` uses all of the memory it allocates (at least at page granularity), so when it tries to use the memory, FreeBSD and Linux kill it. This only happens sometimes, however. Other times (on my machine), they do, in fact, refuse the request. So I changed the test to not test for that because I think the graceful failure code won't really change much. ## 5.0.1 This is a production release with two fixes: * Fix for the build on Mac OSX. * Fix for the build on Android. Users that do not use those platforms do ***NOT*** need to update. ## 5.0.0 This is a major production release with several changes: * Added support for OpenBSD's `pledge()` and `unveil()`. * Fixed print bug where a backslash newline combo was printed even if only one digit was left, something I blindly copied from GNU `bc`, like a fool. * Fixed bugs in the manuals. * Fixed a possible multiplication overflow in power. * Temporary numbers are garbage collected if allocation fails, and the allocation is retried. This is to make `bc` and `dc` more resilient to running out of memory. * Limited the number of temporary numbers and made the space for them static so that allocating more space for them cannot fail. * Allowed integers with non-zero `scale` to be used with power, places, and shift operators. * Added greatest common divisor and least common multiple to `lib2.bc`. * Added `SIGQUIT` handling to history. * Added a command to `dc` (`y`) to get the length of register stacks. * Fixed multi-digit bugs in `lib2.bc`. * Removed the no prompt build option. * Created settings that builders can set defaults for and users can set their preferences for. This includes the `bc` banner, resetting on `SIGINT`, TTY mode, and prompt. * Added history support to Windows. * Fixed bugs with the handling of register names in `dc`. * Fixed bugs with multi-line comments and strings in both calculators. * Added a new error type and message for `dc` when register stacks don't have enough items. * Optimized string allocation. * Made `bc` and `dc` UTF-8 capable. * Fixed a bug with `void` functions. * Fixed a misspelled symbol in `bcl`. This is technically a breaking change, which requires this to be `5.0.0`. * Added the ability for users to get the copyright banner back. * Added the ability for users to have `bc` and `dc` quit on `SIGINT`. * Added the ability for users to disable prompt and TTY mode by environment variables. * Added the ability for users to redefine keywords. This is another reason this is `5.0.0`. * Added `dc`'s modular exponentiation and divmod to `bc`. * Added the ability to assign strings to variables and array elements and pass them to functions in `bc`. * Added `dc`'s asciify command and stream printing to `bc`. * Added a command to `dc` (`Y`) to get the length of an array. * Added a command to `dc` (`,`) to get the depth of the execution stack. * Added bitwise and, or, xor, left shift, right shift, reverse, left rotate, right rotate, and mod functions to `lib2.bc`. * Added the functions `s2u(x)` and `s2un(x,n)`, to `lib2.bc`. ## 4.0.2 This is a production release that fixes two bugs: 1. If no files are used and the first statement on `stdin` is invalid, `scale` would not be set to `20` even if `-l` was used. 2. When using history, `bc` failed to respond properly to `SIGSTOP` and `SIGTSTP`. ## 4.0.1 This is a production release that only adds one thing: flushing output when it is printed with a print statement. ## 4.0.0 This is a production release with many fixes, a new command-line option, and a big surprise: * A bug was fixed in `dc`'s `P` command where the item on the stack was *not* popped. * Various bugs in the manuals have been fixed. * A known bug was fixed where history did not interact well with prompts printed by user code without newlines. * A new command-line option, `-R` and `--no-read-prompt` was added to disable just the prompt when using `read()` (`bc`) or `?` (`dc`). * And finally, **official support for Windows was added**. The last item is why this is a major version bump. Currently, only one set of build options (extra math and prompt enabled, history and NLS/locale support disabled, both calculators enabled) is supported on Windows. However, both debug and release builds are supported. In addition, Windows builds are supported for the the library (`bcl`). For more details about how to build on Windows, see the [README][5] or the [build manual][13]. ## 3.3.4 This is a production release that fixes a small bug. The bug was that output was not flushed before a `read()` call, so prompts without a newline on the end were not flushed before the `read()` call. This is such a tiny bug that users only need to upgrade if they are affected. ## 3.3.3 This is a production release with one tweak and fixes for manuals. The tweak is that `length(0)` returns `1` instead of `0`. In `3.3.1`, I changed it so `length(0.x)`, where `x` could be any number of digits, returned the `scale`, but `length(0)` still returned `0` because I believe that `0` has `0` significant digits. After request of FreeBSD and considering the arguments of a mathematician, compatibility with other `bc`'s, and the expectations of users, I decided to make the change. The fixes for manuals fixed a bug where `--` was rendered as `-`. ## 3.3.2 This is a production release that fixes a divide-by-zero bug in `root()` in the [extended math library][16]. All previous versions with `root()` have the bug. ## 3.3.1 This is a production release that fixes a bug. The bug was in the reporting of number length when the value was 0. ## 3.3.0 This is a production release that changes one behavior and fixes documentation bugs. The changed behavior is the treatment of `-e` and `-f` when given through `BC_ENV_ARGS` or `DC_ENV_ARGS`. Now `bc` and `dc` do not exit when those options (or their equivalents) are given through those environment variables. However, `bc` and `dc` still exit when they or their equivalents are given on the command-line. ## 3.2.7 This is a production release that removes a small non-portable shell operation in `configure.sh`. This problem was only noticed on OpenBSD, not FreeBSD or Linux. Non-OpenBSD users do ***NOT*** need to upgrade, although NetBSD users may also need to upgrade. ## 3.2.6 This is a production release that fixes the build on FreeBSD. There was a syntax error in `configure.sh` that the Linux shell did not catch, and FreeBSD depends on the existence of `tests/all.sh`. All users that already upgraded to `3.2.5` should update to this release, with my apologies for the poor release of `3.2.5`. Other users should skip `3.2.5` in favor of this version. ## 3.2.5 This is a production release that fixes several bugs and adds a couple small things. The two most important bugs were bugs that causes `dc` to access memory out-of-bounds (crash in debug builds). This was found by upgrading to `afl++` from `afl`. Both were caused by a failure to distinguish between the same two cases. Another bug was the failure to put all of the licenses in the `LICENSE.md` file. Third, some warnings by `scan-build` were found and eliminated. This needed one big change: `bc` and `dc` now bail out as fast as possible on fatal errors instead of unwinding the stack. Fourth, the pseudo-random number now attempts to seed itself with `/dev/random` if `/dev/urandom` fails. Finally, this release has a few quality-of-life changes to the build system. The usage should not change at all; the only thing that changed was making sure the `Makefile.in` was written to rebuild properly when headers changed and to not rebuild when not necessary. ## 3.2.4 This is a production release that fixes a warning on `gcc` 6 or older, which does not have an attribute that is used. Users do ***NOT*** need to upgrade if they don't use `gcc` 6 or older. ## 3.2.3 This is a production release that fixes a bug in `gen/strgen.sh`. I recently changed `gen/strgen.c`, but I did not change `gen/strgen.sh`. Users that do not use `gen/strgen.sh` do not need to upgrade. ## 3.2.2 This is a production release that fixes a portability bug in `configure.sh`. The bug was using the GNU `find` extension `-wholename`. ## 3.2.1 This is a production release that has one fix for `bcl(3)`. It is technically not a bug fix since the behavior is undefined, but the `BclNumber`s that `bcl_divmod()` returns will be set to `BCL_ERROR_INVALID_NUM` if there is an error. Previously, they were not set. ## 3.2.0 This is a production release that has one bug fix and a major addition. The bug fix was a missing `auto` variable in the bessel `j()` function in the math library. The major addition is a way to build a version of `bc`'s math code as a library. This is done with the `-a` option to `configure.sh`. The API for the library can be read in `./manuals/bcl.3.md` or `man bcl` once the library is installed with `make install`. This library was requested by developers before I even finished version 1.0, but I could not figure out how to do it until now. If the library has API breaking changes, the major version of `bc` will be incremented. ## 3.1.6 This is a production release that fixes a new warning from Clang 12 for FreeBSD and also removes some possible undefined behavior found by UBSan that compilers did not seem to take advantage of. Users do ***NOT*** need to upgrade, if they do not want to. ## 3.1.5 This is a production release that fixes the Chinese locales (which caused `bc` to crash) and a crash caused by `bc` executing code when it should not have been able to. ***ALL USERS SHOULD UPGRADE.*** ## 3.1.4 This is a production release that fixes one bug, changes two behaviors, and removes one environment variable. The bug is like the one in the last release except it applies if files are being executed. I also made the fix more general. The behavior that was changed is that `bc` now exits when given `-e`, `-f`, `--expression` or `--file`. However, if the last one of those is `-f-` (using `stdin` as the file), `bc` does not exit. If `-f-` exists and is not the last of the `-e` and `-f` options (and equivalents), `bc` gives a fatal error and exits. Next, I removed the `BC_EXPR_EXIT` and `DC_EXPR_EXIT` environment variables since their use is not needed with the behavior change. Finally, I made it so `bc` does not print the header, though the `-q` and `--quiet` options were kept for compatibility with GNU `bc`. ## 3.1.3 This is a production release that fixes one minor bug: if `bc` was invoked like the following, it would error: ``` echo "if (1 < 3) 1" | bc ``` Unless users run into this bug, they do not need to upgrade, but it is suggested that they do. ## 3.1.2 This is a production release that adds a way to install *all* locales. Users do ***NOT*** need to upgrade. For package maintainers wishing to make use of the change, just pass `-l` to `configure.sh`. ## 3.1.1 This is a production release that adds two Spanish locales. Users do ***NOT*** need to upgrade, unless they want those locales. ## 3.1.0 This is a production release that adjusts one behavior, fixes eight bugs, and improves manpages for FreeBSD. Because this release fixes bugs, **users and package maintainers should update to this version as soon as possible**. The behavior that was adjusted was how code from the `-e` and `-f` arguments (and equivalents) were executed. They used to be executed as one big chunk, but in this release, they are now executed line-by-line. The first bug fix in how output to `stdout` was handled in `SIGINT`. If a `SIGINT` came in, the `stdout` buffer was not correctly flushed. In fact, a clean-up function was not getting called. This release fixes that bug. The second bug is in how `dc` handled input from `stdin`. This affected `bc` as well since it was a mishandling of the `stdin` buffer. The third fixed bug was that `bc` and `dc` could `abort()` (in debug mode) when receiving a `SIGTERM`. This one was a race condition with pushing and popping items onto and out of vectors. The fourth bug fixed was that `bc` could leave extra items on the stack and thus, not properly clean up some memory. (The memory would still get `free()`'ed, but it would not be `free()`'ed when it could have been.) The next two bugs were bugs in `bc`'s parser that caused crashes when executing the resulting code. The last two bugs were crashes in `dc` that resulted from mishandling of strings. The manpage improvement was done by switching from [ronn][20] to [Pandoc][21] to generate manpages. Pandoc generates much cleaner manpages and doesn't leave blank lines where they shouldn't be. ## 3.0.3 This is a production release that adds one new feature: specific manpages. Before this release, `bc` and `dc` only used one manpage each that referred to various build options. This release changes it so there is one manpage set per relevant build type. Each manual only has information about its particular build, and `configure.sh` selects the correct set for install. ## 3.0.2 This is a production release that adds `utf8` locale symlinks and removes an unused `auto` variable from the `ceil()` function in the [extended math library][16]. Users do ***NOT*** need to update unless they want the locales. ## 3.0.1 This is a production release with two small changes. Users do ***NOT*** need to upgrade to this release; however, if they haven't upgraded to `3.0.0` yet, it may be worthwhile to upgrade to this release. The first change is fixing a compiler warning on FreeBSD with strict warnings on. The second change is to make the new implementation of `ceil()` in `lib2.bc` much more efficient. ## 3.0.0 *Notes for package maintainers:* *First, the `2.7.0` release series saw a change in the option parsing. This made me change one error message and add a few others. The error message that was changed removed one format specifier. This means that `printf()` will seqfault on old locale files. Unfortunately, `bc` cannot use any locale files except the global ones that are already installed, so it will use the previous ones while running tests during install. **If `bc` segfaults while running arg tests when updating, it is because the global locale files have not been replaced. Make sure to either prevent the test suite from running on update or remove the old locale files before updating.** (Removing the locale files can be done with `make uninstall` or by running the [`locale_uninstall.sh`][22] script.) Once this is done, `bc` should install without problems.* *Second, **the option to build without signal support has been removed**. See below for the reasons why.* This is a production release with some small bug fixes, a few improvements, three major bug fixes, and a complete redesign of `bc`'s error and signal handling. **Users and package maintainers should update to this version as soon as possible.** The first major bug fix was in how `bc` executed files. Previously, a whole file was parsed before it was executed, but if a function is defined *after* code, especially if the function definition was actually a redefinition, and the code before the definition referred to the previous function, this `bc` would replace the function before executing any code. The fix was to make sure that all code that existed before a function definition was executed. The second major bug fix was in `bc`'s `lib2.bc`. The `ceil()` function had a bug where a `0` in the decimal place after the truncation position, caused it to output the wrong numbers if there was any non-zero digit after. The third major bug is that when passing parameters to functions, if an expression included an array (not an array element) as a parameter, it was accepted, when it should have been rejected. It is now correctly rejected. Beyond that, this `bc` got several improvements that both sped it up, improved the handling of signals, and improved the error handling. First, the requirements for `bc` were pushed back to POSIX 2008. `bc` uses one function, `strdup()`, which is not in POSIX 2001, and it is in the X/Open System Interfaces group 2001. It is, however, in POSIX 2008, and since POSIX 2008 is old enough to be supported anywhere that I care, that should be the requirement. -Second, the BcVm global variable was put into `bss`. This actually slightly +Second, the `BcVm` global variable was put into `bss`. This actually slightly reduces the size of the executable from a massive code shrink, and it will stop `bc` from allocating a large set of memory when `bc` starts. Third, the default Karatsuba length was updated from 64 to 32 after making the optimization changes below, since 32 is going to be better than 64 after the changes. Fourth, Spanish translations were added. Fifth, the interpreter received a speedup to make performance on non-math-heavy scripts more competitive with GNU `bc`. While improvements did, in fact, get it much closer (see the [benchmarks][19]), it isn't quite there. There were several things done to speed up the interpreter: First, several small inefficiencies were removed. These inefficiencies included calling the function `bc_vec_pop(v)` twice instead of calling `bc_vec_npop(v, 2)`. They also included an extra function call for checking the size of the stack and checking the size of the stack more than once on several operations. Second, since the current `bc` function is the one that stores constants and strings, the program caches pointers to the current function's vectors of constants and strings to prevent needing to grab the current function in order to grab a constant or a string. Third, `bc` tries to reuse `BcNum`'s (the internal representation of arbitary-precision numbers). If a `BcNum` has the default capacity of `BC_NUM_DEF_SIZE` (32 on 64-bit and 16 on 32-bit) when it is freed, it is added to a list of available `BcNum`'s. And then, when a `BcNum` is allocated with a capacity of `BC_NUM_DEF_SIZE` and any `BcNum`'s exist on the list of reusable ones, one of those ones is grabbed instead. In order to support these changes, the `BC_NUM_DEF_SIZE` was changed. It used to be 16 bytes on all systems, but it was changed to more closely align with the minimum allocation size on Linux, which is either 32 bytes (64-bit musl), 24 bytes (64-bit glibc), 16 bytes (32-bit musl), or 12 bytes (32-bit glibc). Since these are the minimum allocation sizes, these are the sizes that would be allocated anyway, making it worth it to just use the whole space, so the value of `BC_NUM_DEF_SIZE` on 64-bit systems was changed to 32 bytes. On top of that, at least on 64-bit, `BC_NUM_DEF_SIZE` supports numbers with either 72 integer digits or 45 integer digits and 27 fractional digits. This should be more than enough for most cases since `bc`'s default `scale` values are 0 or 20, meaning that, by default, it has at most 20 fractional digits. And 45 integer digits are *a lot*; it's enough to calculate the amount of mass in the Milky Way galaxy in kilograms. Also, 72 digits is enough to calculate the diameter of the universe in Planck lengths. (For 32-bit, these numbers are either 32 integer digits or 12 integer digits and 20 fractional digits. These are also quite big, and going much bigger on a 32-bit system seems a little pointless since 12 digits is just under a trillion and 20 fractional digits is still enough for about any use since `10^-20` light years is just under a millimeter.) All of this together means that for ordinary uses, and even uses in scientific work, the default number size will be all that is needed, which means that nearly all, if not all, numbers will be reused, relieving pressure on the system allocator. I did several experiments to find the changes that had the most impact, especially with regard to reusing `BcNum`'s. One was putting `BcNum`'s into buckets according to their capacity in powers of 2 up to 512. That performed worse than `bc` did in `2.7.2`. Another was putting any `BcNum` on the reuse list that had a capacity of `BC_NUM_DEF_SIZE * 2` and reusing them for `BcNum`'s that requested `BC_NUM_DEF_SIZE`. This did reduce the amount of time spent, but it also spent a lot of time in the system allocator for an unknown reason. (When using `strace`, a bunch more `brk` calls showed up.) Just reusing `BcNum`'s that had exactly `BC_NUM_DEF_SIZE` capacity spent the smallest amount of time in both user and system time. This makes sense, especially with the changes to make `BC_NUM_DEF_SIZE` bigger on 64-bit systems, since the vast majority of numbers will only ever use numbers with a size less than or equal to `BC_NUM_DEF_SIZE`. Last of all, `bc`'s signal handling underwent a complete redesign. (This is the reason that this version is `3.0.0` and not `2.8.0`.) The change was to move from a polling approach to signal handling to an interrupt-based approach. Previously, every single loop condition had a check for signals. I suspect that this could be expensive when in tight loops. Now, the signal handler just uses `longjmp()` (actually `siglongjmp()`) to start an unwinding of the stack until it is stopped or the stack is unwound to `main()`, which just returns. If `bc` is currently executing code that cannot be safely interrupted (according to POSIX), then signals are "locked." The signal handler checks if the lock is taken, and if it is, it just sets the status to indicate that a signal arrived. Later, when the signal lock is released, the status is checked to see if a signal came in. If so, the stack unwinding starts. This design eliminates polling in favor of maintaining a stack of `jmp_buf`'s. This has its own performance implications, but it gives better interaction. And the cost of pushing and popping a `jmp_buf` in a function is paid at most twice. Most functions do not pay that price, and most of the rest only pay it once. (There are only some 3 functions in `bc` that push and pop a `jmp_buf` twice.) As a side effect of this change, I had to eliminate the use of `stdio.h` in `bc` because `stdio` does not play nice with signals and `longjmp()`. I implemented custom I/O buffer code that takes a fraction of the size. This means that static builds will be smaller, but non-static builds will be bigger, though they will have less linking time. This change is also good because my history implementation was already bypassing `stdio` for good reasons, and unifying the architecture was a win. Another reason for this change is that my `bc` should *always* behave correctly in the presence of signals like `SIGINT`, `SIGTERM`, and `SIGQUIT`. With the addition of my own I/O buffering, I needed to also make sure that the buffers were correctly flushed even when such signals happened. For this reason, I **removed the option to build without signal support**. As a nice side effect of this change, the error handling code could be changed to take advantage of the stack unwinding that signals used. This means that signals and error handling use the same code paths, which means that the stack unwinding is well-tested. (Errors are tested heavily in the test suite.) It also means that functions do not need to return a status code that ***every*** caller needs to check. This eliminated over 100 branches that simply checked return codes and then passed that return code up the stack if necessary. The code bloat savings from this is at least 1700 bytes on `x86_64`, *before* taking into account the extra code from removing `stdio.h`. ## 2.7.2 This is a production release with one major bug fix. The `length()` built-in function can take either a number or an array. If it takes an array, it returns the length of the array. Arrays can be passed by reference. The bug is that the `length()` function would not properly dereference arrays that were references. This is a bug that affects all users. **ALL USERS SHOULD UPDATE `bc`**. ## 2.7.1 This is a production release with fixes for new locales and fixes for compiler warnings on FreeBSD. ## 2.7.0 This is a production release with a bug fix for Linux, new translations, and new features. Bug fixes: * Option parsing in `BC_ENV_ARGS` was broken on Linux in 2.6.1 because `glibc`'s `getopt_long()` is broken. To get around that, and to support long options on every platform, an adapted version of [`optparse`][17] was added. Now, `bc` does not even use `getopt()`. * Parsing `BC_ENV_ARGS` with quotes now works. It isn't the smartest, but it does the job if there are spaces in file names. The following new languages are supported: * Dutch * Polish * Russian * Japanes * Simplified Chinese All of these translations were generated using [DeepL][18], so improvements are welcome. There is only one new feature: **`bc` now has a built-in pseudo-random number generator** (PRNG). The PRNG is seeded, making it useful for applications where `/dev/urandom` does not work because output needs to be reproducible. However, it also uses `/dev/urandom` to seed itself by default, so it will start with a good seed by default. It also outputs 32 bits on 32-bit platforms and 64 bits on 64-bit platforms, far better than the 15 bits of C's `rand()` and `bash`'s `$RANDOM`. In addition, the PRNG can take a bound, and when it gets a bound, it automatically adjusts to remove bias. It can also generate numbers of arbitrary size. (As of the time of release, the largest pseudo-random number generated by this `bc` was generated with a bound of `2^(2^20)`.) ***IMPORTANT: read the [`bc` manual][9] and the [`dc` manual][10] to find out exactly what guarantees the PRNG provides. The underlying implementation is not guaranteed to stay the same, but the guarantees that it provides are guaranteed to stay the same regardless of the implementation.*** On top of that, four functions were added to `bc`'s [extended math library][16] to make using the PRNG easier: * `frand(p)`: Generates a number between `[0,1)` to `p` decimal places. * `ifrand(i, p)`: Generates an integer with bound `i` and adds it to `frand(p)`. * `srand(x)`: Randomizes the sign of `x`. In other words, it flips the sign of `x` with probability `0.5`. * `brand()`: Returns a random boolean value (either `0` or `1`). ## 2.6.1 This is a production release with a bug fix for FreeBSD. The bug was that when `bc` was built without long options, it would give a fatal error on every run. This was caused by a mishandling of `optind`. ## 2.6.0 This release is a production release ***with no bugfixes***. If you do not want to upgrade, you don't have to. No source code changed; the only thing that changed was `lib2.bc`. This release adds one function to the [extended math library][16]: `p(x, y)`, which calculates `x` to the power of `y`, whether or not `y` is an integer. (The `^` operator can only accept integer powers.) This release also includes a couple of small tweaks to the [extended math library][16], mostly to fix returning numbers with too high of `scale`. ## 2.5.3 This release is a production release which addresses inconsistencies in the Portuguese locales. No `bc` code was changed. The issues were that the ISO files used different naming, and also that the files that should have been symlinks were not. I did not catch that because GitHub rendered them the exact same way. ## 2.5.2 This release is a production release. No code was changed, but the build system was changed to allow `CFLAGS` to be given to `CC`, like this: ``` CC="gcc -O3 -march=native" ./configure.sh ``` If this happens, the flags are automatically put into `CFLAGS`, and the compiler is set appropriately. In the example above this means that `CC` will be "gcc" and `CFLAGS` will be "-O3 -march=native". This behavior was added to conform to GNU autotools practices. ## 2.5.1 This is a production release which addresses portability concerns discovered in the `bc` build system. No `bc` code was changed. * Support for Solaris SPARC and AIX were added. * Minor documentations edits were performed. * An option for `configure.sh` was added to disable long options if `getopt_long()` is missing. ## 2.5.0 This is a production release with new translations. No code changed. The translations were contributed by [bugcrazy][15], and they are for Portuguese, both Portugal and Brazil locales. ## 2.4.0 This is a production release primarily aimed at improving `dc`. * A couple of copy and paste errors in the [`dc` manual][10] were fixed. * `dc` startup was optimized by making sure it didn't have to set up `bc`-only things. * The `bc` `&&` and `||` operators were made available to `dc` through the `M` and `m` commands, respectively. * `dc` macros were changed to be tail call-optimized. The last item, tail call optimization, means that if the last thing in a macro is a call to another macro, then the old macro is popped before executing the new macro. This change was made to stop `dc` from consuming more and more memory as macros are executed in a loop. The `q` and `Q` commands still respect the "hidden" macros by way of recording how many macros were removed by tail call optimization. ## 2.3.2 This is a production release meant to fix warnings in the Gentoo `ebuild` by making it possible to disable binary stripping. Other users do *not* need to upgrade. ## 2.3.1 This is a production release. It fixes a bug that caused `-1000000000 < -1` to return `0`. This only happened with negative numbers and only if the value on the left was more negative by a certain amount. That said, this bug *is* a bad bug, and needs to be fixed. **ALL USERS SHOULD UPDATE `bc`**. ## 2.3.0 This is a production release with changes to the build system. ## 2.2.0 This release is a production release. It only has new features and performance improvements. 1. The performance of `sqrt(x)` was improved. 2. The new function `root(x, n)` was added to the extended math library to calculate `n`th roots. 3. The new function `cbrt(x)` was added to the extended math library to calculate cube roots. ## 2.1.3 This is a non-critical release; it just changes the build system, and in non-breaking ways: 1. Linked locale files were changed to link to their sources with a relative link. 2. A bug in `configure.sh` that caused long option parsing to fail under `bash` was fixed. ## 2.1.2 This release is not a critical release. 1. A few codes were added to history. 2. Multiplication was optimized a bit more. 3. Addition and subtraction were both optimized a bit more. ## 2.1.1 This release contains a fix for the test suite made for Linux from Scratch: now the test suite prints `pass` when a test is passed. Other than that, there is no change in this release, so distros and other users do not need to upgrade. ## 2.1.0 This release is a production release. The following bugs were fixed: 1. A `dc` bug that caused stack mishandling was fixed. 2. A warning on OpenBSD was fixed. 3. Bugs in `ctrl+arrow` operations in history were fixed. 4. The ability to paste multiple lines in history was added. 5. A `bc` bug, mishandling of array arguments to functions, was fixed. 6. A crash caused by freeing the wrong pointer was fixed. 7. A `dc` bug where strings, in a rare case, were mishandled in parsing was fixed. In addition, the following changes were made: 1. Division was slightly optimized. 2. An option was added to the build to disable printing of prompts. 3. The special case of empty arguments is now handled. This is to prevent errors in scripts that end up passing empty arguments. 4. A harmless bug was fixed. This bug was that, with the pop instructions (mostly) removed (see below), `bc` would leave extra values on its stack for `void` functions and in a few other cases. These extra items would not affect anything put on the stack and would not cause any sort of crash or even buggy behavior, but they would cause `bc` to take more memory than it needed. On top of the above changes, the following optimizations were added: 1. The need for pop instructions in `bc` was removed. 2. Extra tests on every iteration of the interpreter loop were removed. 3. Updating function and code pointers on every iteration of the interpreter loop was changed to only updating them when necessary. 4. Extra assignments to pointers were removed. Altogether, these changes sped up the interpreter by around 2x. ***NOTE***: This is the last release with new features because this `bc` is now considered complete. From now on, only bug fixes and new translations will be added to this `bc`. ## 2.0.3 This is a production, bug-fix release. Two bugs were fixed in this release: 1. A rare and subtle signal handling bug was fixed. 2. A misbehavior on `0` to a negative power was fixed. The last bug bears some mentioning. When I originally wrote power, I did not thoroughly check its error cases; instead, I had it check if the first number was `0` and then if so, just return `0`. However, `0` to a negative power means that `1` will be divided by `0`, which is an error. I caught this, but only after I stopped being cocky. You see, sometime later, I had noticed that GNU `bc` returned an error, correctly, but I thought it was wrong simply because that's not what my `bc` did. I saw it again later and had a double take. I checked for real, finally, and found out that my `bc` was wrong all along. That was bad on me. But the bug was easy to fix, so it is fixed now. There are two other things in this release: 1. Subtraction was optimized by [Stefan Eßer][14]. 2. Division was also optimized, also by Stefan Eßer. ## 2.0.2 This release contains a fix for a possible overflow in the signal handling. I would be surprised if any users ran into it because it would only happen after 2 billion (`2^31-1`) `SIGINT`'s, but I saw it and had to fix it. ## 2.0.1 This release contains very few things that will apply to any users. 1. A slight bug in `dc`'s interactive mode was fixed. 2. A bug in the test suite that was only triggered on NetBSD was fixed. 3. **The `-P`/`--no-prompt` option** was added for users that do not want a prompt. 4. A `make check` target was added as an alias for `make test`. 5. `dc` got its own read prompt: `?> `. ## 2.0.0 This release is a production release. This release is also a little different from previous releases. From here on out, I do not plan on adding any more features to this `bc`; I believe that it is complete. However, there may be bug fix releases in the future, if I or any others manage to find bugs. This release has only a few new features: 1. `atan2(y, x)` was added to the extended math library as both `a2(y, x)` and `atan2(y, x)`. 2. Locales were fixed. 3. A **POSIX shell-compatible script was added as an alternative to compiling `gen/strgen.c`** on a host machine. More details about making the choice between the two can be found by running `./configure.sh --help` or reading the [build manual][13]. 4. Multiplication was optimized by using **diagonal multiplication**, rather than straight brute force. 5. The `locale_install.sh` script was fixed. 6. `dc` was given the ability to **use the environment variable `DC_ENV_ARGS`**. 7. `dc` was also given the ability to **use the `-i` or `--interactive`** options. 8. Printing the prompt was fixed so that it did not print when it shouldn't. 9. Signal handling was fixed. 10. **Handling of `SIGTERM` and `SIGQUIT`** was fixed. 11. The **built-in functions `maxibase()`, `maxobase()`, and `maxscale()`** (the commands `T`, `U`, `V` in `dc`, respectively) were added to allow scripts to query for the max allowable values of those globals. 12. Some incompatibilities with POSIX were fixed. In addition, this release is `2.0.0` for a big reason: the internal format for numbers changed. They used to be a `char` array. Now, they are an array of larger integers, packing more decimal digits into each integer. This has delivered ***HUGE*** performance improvements, especially for multiplication, division, and power. This `bc` should now be the fastest `bc` available, but I may be wrong. ## 1.2.8 This release contains a fix for a harmless bug (it is harmless in that it still works, but it just copies extra data) in the [`locale_install.sh`][12] script. ## 1.2.7 This version contains fixes for the build on Arch Linux. ## 1.2.6 This release removes the use of `local` in shell scripts because it's not POSIX shell-compatible, and also updates a man page that should have been updated a long time ago but was missed. ## 1.2.5 This release contains some missing locale `*.msg` files. ## 1.2.4 This release contains a few bug fixes and new French translations. ## 1.2.3 This release contains a fix for a bug: use of uninitialized data. Such data was only used when outputting an error message, but I am striving for perfection. As Michelangelo said, "Trifles make perfection, and perfection is no trifle." ## 1.2.2 This release contains fixes for OpenBSD. ## 1.2.1 This release contains bug fixes for some rare bugs. ## 1.2.0 This is a production release. There have been several changes since `1.1.0`: 1. The build system had some changes. 2. Locale support has been added. (Patches welcome for translations.) 3. **The ability to turn `ibase`, `obase`, and `scale` into stacks** was added with the `-g` command-line option. (See the [`bc` manual][9] for more details.) 4. Support for compiling on Mac OSX out of the box was added. 5. The extended math library got `t(x)`, `ceil(x)`, and some aliases. 6. The extended math library also got `r2d(x)` (for converting from radians to degrees) and `d2r(x)` (for converting from degrees to radians). This is to allow using degrees with the standard library. 7. Both calculators now accept numbers in **scientific notation**. See the [`bc` manual][9] and the [`dc` manual][10] for details. 8. Both calculators can **output in either scientific or engineering notation**. See the [`bc` manual][9] and the [`dc` manual][10] for details. 9. Some inefficiencies were removed. 10. Some bugs were fixed. 11. Some bugs in the extended library were fixed. 12. Some defects from [Coverity Scan][11] were fixed. ## 1.1.4 This release contains a fix to the build system that allows it to build on older versions of `glibc`. ## 1.1.3 This release contains a fix for a bug in the test suite where `bc` tests and `dc` tests could not be run in parallel. ## 1.1.2 This release has a fix for a history bug; the down arrow did not work. ## 1.1.1 This release fixes a bug in the `1.1.0` build system. The source is exactly the same. The bug that was fixed was a failure to install if no `EXECSUFFIX` was used. ## 1.1.0 This is a production release. However, many new features were added since `1.0`. 1. **The build system has been changed** to use a custom, POSIX shell-compatible configure script ([`configure.sh`][6]) to generate a POSIX make-compatible `Makefile`, which means that `bc` and `dc` now build out of the box on any POSIX-compatible system. 2. Out-of-memory and output errors now cause the `bc` to report the error, clean up, and die, rather than just reporting and trying to continue. 3. **Strings and constants are now garbage collected** when possible. 4. Signal handling and checking has been made more simple and more thorough. 5. `BcGlobals` was refactored into `BcVm` and `BcVm` was made global. Some procedure names were changed to reflect its difference to everything else. 6. Addition got a speed improvement. 7. Some common code for addition and multiplication was refactored into its own procedure. 8. A bug was removed where `dc` could have been selected, but the internal `#define` that returned `true` for a query about `dc` would not have returned `true`. 9. Useless calls to `bc_num_zero()` were removed. 10. **History support was added.** The history support is based off of a [UTF-8 aware fork][7] of [`linenoise`][8], which has been customized with `bc`'s own data structures and signal handling. 11. Generating C source from the math library now removes tabs from the library, shrinking the size of the executable. 12. The math library was shrunk. 13. Error handling and reporting was improved. 14. Reallocations were reduced by giving access to the request size for each operation. 15. **`abs()` (`b` command for `dc`) was added as a builtin.** 16. Both calculators were tested on FreeBSD. 17. Many obscure parse bugs were fixed. 18. Markdown and man page manuals were added, and the man pages are installed by `make install`. 19. Executable size was reduced, though the added features probably made the executable end up bigger. 20. **GNU-style array references were added as a supported feature.** 21. Allocations were reduced. 22. **New operators were added**: `$` (`$` for `dc`), `@` (`@` for `dc`), `@=`, `<<` (`H` for `dc`), `<<=`, `>>` (`h` for `dc`), and `>>=`. See the [`bc` manual][9] and the [`dc` manual][10] for more details. 23. **An extended math library was added.** This library contains code that makes it so I can replace my desktop calculator with this `bc`. See the [`bc` manual][3] for more details. 24. Support for all capital letters as numbers was added. 25. **Support for GNU-style void functions was added.** 26. A bug fix for improper handling of function parameters was added. 27. Precedence for the or (`||`) operator was changed to match GNU `bc`. 28. `dc` was given an explicit negation command. 29. `dc` was changed to be able to handle strings in arrays. ## 1.1 Release Candidate 3 This release is the eighth release candidate for 1.1, though it is the third release candidate meant as a general release candidate. The new code has not been tested as thoroughly as it should for release. ## 1.1 Release Candidate 2 This release is the seventh release candidate for 1.1, though it is the second release candidate meant as a general release candidate. The new code has not been tested as thoroughly as it should for release. ## 1.1 FreeBSD Beta 5 This release is the sixth release candidate for 1.1, though it is the fifth release candidate meant specifically to test if `bc` works on FreeBSD. The new code has not been tested as thoroughly as it should for release. ## 1.1 FreeBSD Beta 4 This release is the fifth release candidate for 1.1, though it is the fourth release candidate meant specifically to test if `bc` works on FreeBSD. The new code has not been tested as thoroughly as it should for release. ## 1.1 FreeBSD Beta 3 This release is the fourth release candidate for 1.1, though it is the third release candidate meant specifically to test if `bc` works on FreeBSD. The new code has not been tested as thoroughly as it should for release. ## 1.1 FreeBSD Beta 2 This release is the third release candidate for 1.1, though it is the second release candidate meant specifically to test if `bc` works on FreeBSD. The new code has not been tested as thoroughly as it should for release. ## 1.1 FreeBSD Beta 1 This release is the second release candidate for 1.1, though it is meant specifically to test if `bc` works on FreeBSD. The new code has not been tested as thoroughly as it should for release. ## 1.1 Release Candidate 1 This is the first release candidate for 1.1. The new code has not been tested as thoroughly as it should for release. ## 1.0 This is the first non-beta release. `bc` is ready for production use. As such, a lot has changed since 0.5. 1. `dc` has been added. It has been tested even more thoroughly than `bc` was for `0.5`. It does not have the `!` command, and for security reasons, it never will, so it is complete. 2. `bc` has been more thoroughly tested. An entire section of the test suite (for both programs) has been added to test for errors. 3. A prompt (`>>> `) has been added for interactive mode, making it easier to see inputs and outputs. 4. Interrupt handling has been improved, including elimination of race conditions (as much as possible). 5. MinGW and [Windows Subsystem for Linux][1] support has been added (see [xstatic][2] for binaries). 6. Memory leaks and errors have been eliminated (as far as ASan and Valgrind can tell). 7. Crashes have been eliminated (as far as [afl][3] can tell). 8. Karatsuba multiplication was added (and thoroughly) tested, speeding up multiplication and power by orders of magnitude. 9. Performance was further enhanced by using a "divmod" function to reduce redundant divisions and by removing superfluous `memset()` calls. 10. To switch between Karatsuba and `O(n^2)` multiplication, the config variable `BC_NUM_KARATSUBA_LEN` was added. It is set to a sane default, but the optimal number can be found with [`karatsuba.py`][4] (requires Python 3) and then configured through `make`. 11. The random math test generator script was changed to Python 3 and improved. `bc` and `dc` have together been run through 30+ million random tests. 12. All known math bugs have been fixed, including out of control memory allocations in `sine` and `cosine` (that was actually a parse bug), certain cases of infinite loop on square root, and slight inaccuracies (as much as possible; see the [README][5]) in transcendental functions. 13. Parsing has been fixed as much as possible. 14. Test coverage was improved to 94.8%. The only paths not covered are ones that happen when `malloc()` or `realloc()` fails. 15. An extension to get the length of an array was added. 16. The boolean not (`!`) had its precedence change to match negation. 17. Data input was hardened. 18. `bc` was made fully compliant with POSIX when the `-s` flag is used or `POSIXLY_CORRECT` is defined. 19. Error handling was improved. 20. `bc` now checks that files it is given are not directories. ## 1.0 Release Candidate 7 This is the seventh release candidate for 1.0. It fixes a few bugs in 1.0 Release Candidate 6. ## 1.0 Release Candidate 6 This is the sixth release candidate for 1.0. It fixes a few bugs in 1.0 Release Candidate 5. ## 1.0 Release Candidate 5 This is the fifth release candidate for 1.0. It fixes a few bugs in 1.0 Release Candidate 4. ## 1.0 Release Candidate 4 This is the fourth release candidate for 1.0. It fixes a few bugs in 1.0 Release Candidate 3. ## 1.0 Release Candidate 3 This is the third release candidate for 1.0. It fixes a few bugs in 1.0 Release Candidate 2. ## 1.0 Release Candidate 2 This is the second release candidate for 1.0. It fixes a few bugs in 1.0 Release Candidate 1. ## 1.0 Release Candidate 1 This is the first Release Candidate for 1.0. `bc` is complete, with `dc`, but it is not tested. ## 0.5 This beta release completes more features, but it is still not complete nor tested as thoroughly as necessary. ## 0.4.1 This beta release fixes a few bugs in 0.4. ## 0.4 This is a beta release. It does not have the complete set of features, and it is not thoroughly tested. [1]: https://docs.microsoft.com/en-us/windows/wsl/install-win10 [2]: https://pkg.musl.cc/bc/ [3]: http://lcamtuf.coredump.cx/afl/ [4]: ./scripts/karatsuba.py [5]: ./README.md [6]: ./configure.sh [7]: https://github.com/rain-1/linenoise-mob [8]: https://github.com/antirez/linenoise [9]: ./manuals/bc/A.1.md [10]: ./manuals/dc/A.1.md [11]: https://scan.coverity.com/projects/gavinhoward-bc [12]: ./scripts/locale_install.sh [13]: ./manuals/build.md [14]: https://github.com/stesser [15]: https://github.com/bugcrazy [16]: ./manuals/bc/A.1.md#extended-library [17]: https://github.com/skeeto/optparse [18]: https://www.deepl.com/translator [19]: ./manuals/benchmarks.md [20]: https://github.com/apjanke/ronn-ng [21]: https://pandoc.org/ [22]: ./scripts/locale_uninstall.sh diff --git a/contrib/bc/configure.sh b/contrib/bc/configure.sh index 35d55058f8f1..9292f094bb17 100755 --- a/contrib/bc/configure.sh +++ b/contrib/bc/configure.sh @@ -1,2125 +1,2127 @@ #! /bin/sh # # SPDX-License-Identifier: BSD-2-Clause # # Copyright (c) 2018-2023 Gavin D. Howard and contributors. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # * Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # * Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # script="$0" scriptdir=$(dirname "$script") script=$(basename "$script") builddir=$(pwd) . "$scriptdir/scripts/functions.sh" # Simply prints the help message and quits based on the argument. # @param msg The help message to print. usage() { if [ $# -gt 0 ]; then _usage_val=1 printf '%s\n\n' "$1" else _usage_val=0 fi printf 'usage:\n' printf ' %s -h\n' "$script" printf ' %s --help\n' "$script" printf ' %s [-a|-bD|-dB|-c] [-CeEfgGHilmMNPrtTvz] [-O OPT_LEVEL] [-k KARATSUBA_LEN]\\\n' "$script" printf ' [-s SETTING] [-S SETTING] [-p TYPE]\n' printf ' %s \\\n' "$script" printf ' [--library|--bc-only --disable-dc|--dc-only --disable-bc|--coverage] \\\n' printf ' [--force --debug --disable-extra-math --disable-generated-tests] \\\n' printf ' [--disable-history --disable-man-pages --disable-nls --disable-strip] \\\n' printf ' [--enable-editline] [--enable-readline] [--enable-internal-history] \\\n' printf ' [--disable-problematic-tests] [--install-all-locales] \\\n' printf ' [--opt=OPT_LEVEL] [--karatsuba-len=KARATSUBA_LEN] \\\n' printf ' [--set-default-on=SETTING] [--set-default-off=SETTING] \\\n' printf ' [--predefined-build-type=TYPE] \\\n' printf ' [--prefix=PREFIX] [--bindir=BINDIR] [--datarootdir=DATAROOTDIR] \\\n' printf ' [--datadir=DATADIR] [--mandir=MANDIR] [--man1dir=MAN1DIR] \\\n' printf ' [--man3dir=MAN3DIR]\n' if [ "$_usage_val" -ne 0 ]; then exit fi printf '\n' printf ' -a, --library\n' printf ' Build the libbcl instead of the programs. This is meant to be used with\n' printf ' Other software like programming languages that want to make use of the\n' printf ' parsing and math capabilities. This option will install headers using\n' printf ' `make install`.\n' printf ' -b, --bc-only\n' printf ' Build bc only. It is an error if "-d", "--dc-only", "-B", or\n' printf ' "--disable-bc" are specified too.\n' printf ' -B, --disable-bc\n' printf ' Disable bc. It is an error if "-b", "--bc-only", "-D", or "--disable-dc"\n' printf ' are specified too.\n' printf ' -c, --coverage\n' printf ' Generate test coverage code. Requires gcov and gcovr.\n' printf ' It is an error if either "-b" ("-D") or "-d" ("-B") is specified.\n' printf ' Requires a compiler that use gcc-compatible coverage options\n' printf ' -C, --disable-clean\n' printf ' Disable the clean that configure.sh does before configure.\n' printf ' -d, --dc-only\n' printf ' Build dc only. It is an error if "-b", "--bc-only", "-D", or\n' printf ' "--disable-dc" are specified too.\n' printf ' -D, --disable-dc\n' printf ' Disable dc. It is an error if "-d", "--dc-only", "-B", or "--disable-bc"\n' printf ' are specified too.\n' printf ' -e, --enable-editline\n' printf ' Enable the use of libedit/editline. This is meant for those users that\n' printf ' want vi-like or Emacs-like behavior in history. This option is ignored\n' printf ' if history is disabled. If the -r or -i options are given with this\n' printf ' option, the last occurrence of all of the three is used.\n' printf ' -E, --disable-extra-math\n' printf ' Disable extra math. This includes: "$" operator (truncate to integer),\n' printf ' "@" operator (set number of decimal places), and r(x, p) (rounding\n' printf ' function). Additionally, this option disables the extra printing\n' printf ' functions in the math library.\n' printf ' -f, --force\n' printf ' Force use of all enabled options, even if they do not work. This\n' printf ' option is to allow the maintainer a way to test that certain options\n' printf ' are not failing invisibly. (Development only.)\n' printf ' -g, --debug\n' printf ' Build in debug mode. Adds the "-g" flag, and if there are no\n' printf ' other CFLAGS, and "-O" was not given, this also adds the "-O0"\n' printf ' flag. If this flag is *not* given, "-DNDEBUG" is added to CPPFLAGS\n' printf ' and a strip flag is added to the link stage.\n' printf ' -G, --disable-generated-tests\n' printf ' Disable generating tests. This is for platforms that do not have a\n' printf ' GNU bc-compatible bc to generate tests.\n' printf ' -h, --help\n' printf ' Print this help message and exit.\n' printf ' -H, --disable-history\n' printf ' Disable history.\n' printf ' -i, --enable-internal-history\n' printf ' Enable the internal history implementation and do not depend on either\n' printf ' editline or readline. This option is ignored if history is disabled.\n' printf ' If this option is given along with -e and -r, the last occurrence of\n' printf ' all of the three is used.\n' printf ' -k KARATSUBA_LEN, --karatsuba-len KARATSUBA_LEN\n' printf ' Set the karatsuba length to KARATSUBA_LEN (default is 64).\n' printf ' It is an error if KARATSUBA_LEN is not a number or is less than 16.\n' printf ' -l, --install-all-locales\n' printf ' Installs all locales, regardless of how many are on the system. This\n' printf ' option is useful for package maintainers who want to make sure that\n' printf ' a package contains all of the locales that end users might need.\n' printf ' -m, --enable-memcheck\n' printf ' Enable memcheck mode, to ensure no memory leaks. For development only.\n' printf ' -M, --disable-man-pages\n' printf ' Disable installing manpages.\n' printf ' -N, --disable-nls\n' printf ' Disable POSIX locale (NLS) support.\n' printf ' ***WARNING***: Locales ignore the prefix because they *must* be\n' printf ' installed at a fixed location to work at all. If you do not want that\n' printf ' to happen, you must disable locales (NLS) completely.\n' printf ' -O OPT_LEVEL, --opt OPT_LEVEL\n' printf ' Set the optimization level. This can also be included in the CFLAGS,\n' printf ' but it is provided, so maintainers can build optimized debug builds.\n' printf ' This is passed through to the compiler, so it must be supported.\n' printf ' -p TYPE, --predefined-build-type=TYPE\n' printf ' Sets a given predefined build type with specific defaults. This is for\n' printf ' easy setting of predefined builds. For example, to get a build that\n' printf ' acts like the GNU bc by default, TYPE should be "GNU" (without the\n' printf ' quotes) This option *must* come before any others that might change the\n' printf ' build options. Currently supported values for TYPE include: "BSD" (for\n' printf ' matching the BSD bc and BSD dc), "GNU" (for matching the GNU bc and\n' printf ' dc), "GDH" (for the preferred build of the author, Gavin D. Howard),\n' printf ' and "DBG" (for the preferred debug build of the author). This will\n' printf ' also automatically enable a release build (except for "DBG").\n' printf ' -P, --disable-problematic-tests\n' printf ' Disables problematic tests. These tests usually include tests that\n' printf ' can cause a SIGKILL because of too much memory usage.\n' printf ' -r, --enable-readline\n' printf ' Enable the use of libreadline/readline. This is meant for those users\n' printf ' that want vi-like or Emacs-like behavior in history. This option is\n' printf ' ignored if history is disabled. If this option is given along with -e\n' printf ' and -r, the last occurrence of all of the three is used.\n' printf ' -s SETTING, --set-default-on SETTING\n' printf ' Set the default named by SETTING to on. See below for possible values\n' printf ' for SETTING. For multiple instances of the -s or -S for the the same\n' printf ' setting, the last one is used.\n' printf ' -S SETTING, --set-default-off SETTING\n' printf ' Set the default named by SETTING to off. See below for possible values\n' printf ' for SETTING. For multiple instances of the -s or -S for the the same\n' printf ' setting, the last one is used.\n' printf ' -t, --enable-test-timing\n' printf ' Enable the timing of tests. This is for development only.\n' printf ' -T, --disable-strip\n' printf ' Disable stripping symbols from the compiled binary or binaries.\n' printf ' Stripping symbols only happens when debug mode is off.\n' printf ' -v, --enable-valgrind\n' printf ' Enable a build appropriate for valgrind. For development only.\n' printf ' -z, --enable-fuzz-mode\n' printf ' Enable fuzzing mode. THIS IS FOR DEVELOPMENT ONLY.\n' printf ' --prefix PREFIX\n' printf ' The prefix to install to. Overrides "$PREFIX" if it exists.\n' printf ' If PREFIX is "/usr", install path will be "/usr/bin".\n' printf ' Default is "/usr/local".\n' printf ' ***WARNING***: Locales ignore the prefix because they *must* be\n' printf ' installed at a fixed location to work at all. If you do not want that to\n' printf ' happen, you must disable locales (NLS) completely.\n' printf ' --bindir BINDIR\n' printf ' The directory to install binaries in. Overrides "$BINDIR" if it exists.\n' printf ' Default is "$PREFIX/bin".\n' printf ' --includedir INCLUDEDIR\n' printf ' The directory to install headers in. Overrides "$INCLUDEDIR" if it\n' printf ' exists. Default is "$PREFIX/include".\n' printf ' --libdir LIBDIR\n' printf ' The directory to install libraries in. Overrides "$LIBDIR" if it exists.\n' printf ' Default is "$PREFIX/lib".\n' printf ' --datarootdir DATAROOTDIR\n' printf ' The root location for data files. Overrides "$DATAROOTDIR" if it exists.\n' printf ' Default is "$PREFIX/share".\n' printf ' --datadir DATADIR\n' printf ' The location for data files. Overrides "$DATADIR" if it exists.\n' printf ' Default is "$DATAROOTDIR".\n' printf ' --mandir MANDIR\n' printf ' The location to install manpages to. Overrides "$MANDIR" if it exists.\n' printf ' Default is "$DATADIR/man".\n' printf ' --man1dir MAN1DIR\n' printf ' The location to install Section 1 manpages to. Overrides "$MAN1DIR" if\n' printf ' it exists. Default is "$MANDIR/man1".\n' printf ' --man3dir MAN3DIR\n' printf ' The location to install Section 3 manpages to. Overrides "$MAN3DIR" if\n' printf ' it exists. Default is "$MANDIR/man3".\n' printf '\n' printf 'In addition, the following environment variables are used:\n' printf '\n' printf ' CC C compiler. Must be compatible with POSIX c99. If there is a\n' printf ' space in the basename of the compiler, the items after the\n' printf ' first space are assumed to be compiler flags, and in that case,\n' printf ' the flags are automatically moved into CFLAGS. Default is\n' printf ' "c99".\n' printf ' HOSTCC Host C compiler. Must be compatible with POSIX c99. If there is\n' printf ' a space in the basename of the compiler, the items after the\n' printf ' first space are assumed to be compiler flags, and in the case,\n' printf ' the flags are automatically moved into HOSTCFLAGS. Default is\n' printf ' "$CC".\n' printf ' HOST_CC Same as HOSTCC. If HOSTCC also exists, it is used.\n' printf ' CFLAGS C compiler flags.\n' printf ' HOSTCFLAGS CFLAGS for HOSTCC. Default is "$CFLAGS".\n' printf ' HOST_CFLAGS Same as HOST_CFLAGS. If HOST_CFLAGS also exists, it is used.\n' printf ' CPPFLAGS C preprocessor flags. Default is "".\n' printf ' LDFLAGS Linker flags. Default is "".\n' printf ' PREFIX The prefix to install to. Default is "/usr/local".\n' printf ' If PREFIX is "/usr", install path will be "/usr/bin".\n' printf ' ***WARNING***: Locales ignore the prefix because they *must* be\n' printf ' installed at a fixed location to work at all. If you do not\n' printf ' want that to happen, you must disable locales (NLS) completely.\n' printf ' BINDIR The directory to install binaries in. Default is "$PREFIX/bin".\n' printf ' INCLUDEDIR The directory to install header files in. Default is\n' printf ' "$PREFIX/include".\n' printf ' LIBDIR The directory to install libraries in. Default is\n' printf ' "$PREFIX/lib".\n' printf ' DATAROOTDIR The root location for data files. Default is "$PREFIX/share".\n' printf ' DATADIR The location for data files. Default is "$DATAROOTDIR".\n' printf ' MANDIR The location to install manpages to. Default is "$DATADIR/man".\n' printf ' MAN1DIR The location to install Section 1 manpages to. Default is\n' printf ' "$MANDIR/man1".\n' printf ' MAN3DIR The location to install Section 3 manpages to. Default is\n' printf ' "$MANDIR/man3".\n' printf ' NLSPATH The location to install locale catalogs to. Must be an absolute\n' printf ' path (or contain one). This is treated the same as the POSIX\n' printf ' definition of $NLSPATH (see POSIX environment variables for\n' printf ' more information). Default is "/usr/share/locale/%%L/%%N".\n' printf ' PC_PATH The location to install pkg-config files to. Must be an\n' printf ' path or contain one. Default is the first path given by the\n' printf ' output of `pkg-config --variable=pc_path pkg-config`.\n' printf ' EXECSUFFIX The suffix to append to the executable names, used to not\n' printf ' interfere with other installed bc executables. Default is "".\n' printf ' EXECPREFIX The prefix to append to the executable names, used to not\n' printf ' interfere with other installed bc executables. Default is "".\n' printf ' DESTDIR For package creation. Default is "". If it is empty when\n' printf ' `%s` is run, it can also be passed to `make install`\n' "$script" printf ' later as an environment variable. If both are specified,\n' printf ' the one given to `%s` takes precedence.\n' "$script" printf ' LONG_BIT The number of bits in a C `long` type. This is mostly for the\n' printf ' embedded space since this `bc` uses `long`s internally for\n' printf ' overflow checking. In C99, a `long` is required to be 32 bits.\n' printf ' For most normal desktop systems, setting this is unnecessary,\n' printf ' except that 32-bit platforms with 64-bit longs may want to set\n' printf ' it to `32`. Default is the default of `LONG_BIT` for the target\n' printf ' platform. Minimum allowed is `32`. It is a build time error if\n' printf ' the specified value of `LONG_BIT` is greater than the default\n' printf ' value of `LONG_BIT` for the target platform.\n' printf ' GEN_HOST Whether to use `gen/strgen.c`, instead of `gen/strgen.sh`, to\n' printf ' produce the C files that contain the help texts as well as the\n' printf ' math libraries. By default, `gen/strgen.c` is used, compiled by\n' printf ' "$HOSTCC" and run on the host machine. Using `gen/strgen.sh`\n' printf ' removes the need to compile and run an executable on the host\n' printf ' machine since `gen/strgen.sh` is a POSIX shell script. However,\n' printf ' `gen/lib2.bc` is over 4095 characters, the max supported length\n' printf ' of a string literal in C99, and `gen/strgen.sh` generates a\n' printf ' string literal instead of an array, as `gen/strgen.c` does. For\n' printf ' most production-ready compilers, this limit probably is not\n' printf ' enforced, but it could be. Both options are still available for\n' printf ' this reason. If you are sure your compiler does not have the\n' printf ' limit and do not want to compile and run a binary on the host\n' printf ' machine, set this variable to "0". Any other value, or a\n' printf ' non-existent value, will cause the build system to compile and\n' printf ' run `gen/strgen.c`. Default is "".\n' printf ' GEN_EMU Emulator to run string generator code under (leave empty if not\n' printf ' necessary). This is not necessary when using `gen/strgen.sh`.\n' printf ' Default is "".\n' printf '\n' printf 'WARNING: even though `configure.sh` supports both option types, short and\n' printf 'long, it does not support handling both at the same time. Use only one type.\n' printf '\n' printf 'Settings\n' printf '========\n' printf '\n' printf 'bc and dc have some settings that, while they cannot be removed by build time\n' printf 'options, can have their defaults changed at build time by packagers. Users are\n' printf 'also able to change each setting with environment variables.\n' printf '\n' printf 'The following is a table of settings, along with their default values and the\n' printf 'environment variables users can use to change them. (For the defaults, non-zero\n' printf 'means on, and zero means off.)\n' printf '\n' printf '| Setting | Description | Default | Env Variable |\n' printf '| =============== | ==================== | ============ | ==================== |\n' printf '| bc.banner | Whether to display | 0 | BC_BANNER |\n' printf '| | the bc version | | |\n' printf '| | banner when in | | |\n' printf '| | interactive mode. | | |\n' printf '| --------------- | -------------------- | ------------ | -------------------- |\n' printf '| bc.sigint_reset | Whether SIGINT will | 1 | BC_SIGINT_RESET |\n' printf '| | reset bc, instead of | | |\n' printf '| | exiting, when in | | |\n' printf '| | interactive mode. | | |\n' printf '| --------------- | -------------------- | ------------ | -------------------- |\n' printf '| dc.sigint_reset | Whether SIGINT will | 1 | DC_SIGINT_RESET |\n' printf '| | reset dc, instead of | | |\n' printf '| | exiting, when in | | |\n' printf '| | interactive mode. | | |\n' printf '| --------------- | -------------------- | ------------ | -------------------- |\n' printf '| bc.tty_mode | Whether TTY mode for | 1 | BC_TTY_MODE |\n' printf '| | bc should be on when | | |\n' printf '| | available. | | |\n' printf '| --------------- | -------------------- | ------------ | -------------------- |\n' printf '| dc.tty_mode | Whether TTY mode for | 0 | BC_TTY_MODE |\n' printf '| | dc should be on when | | |\n' printf '| | available. | | |\n' printf '| --------------- | -------------------- | ------------ | -------------------- |\n' printf '| bc.prompt | Whether the prompt | $BC_TTY_MODE | BC_PROMPT |\n' printf '| | for bc should be on | | |\n' printf '| | in tty mode. | | |\n' printf '| --------------- | -------------------- | ------------ | -------------------- |\n' printf '| dc.prompt | Whether the prompt | $DC_TTY_MODE | DC_PROMPT |\n' printf '| | for dc should be on | | |\n' printf '| | in tty mode. | | |\n' printf '| --------------- | -------------------- | ------------ | -------------------- |\n' printf '| bc.expr_exit | Whether to exit bc | 1 | BC_EXPR_EXIT |\n' printf '| | if an expression or | | |\n' printf '| | expression file is | | |\n' printf '| | given with the -e or | | |\n' printf '| | -f options. | | |\n' printf '| --------------- | -------------------- | ------------ | -------------------- |\n' printf '| dc.expr_exit | Whether to exit dc | 1 | DC_EXPR_EXIT |\n' printf '| | if an expression or | | |\n' printf '| | expression file is | | |\n' printf '| | given with the -e or | | |\n' printf '| | -f options. | | |\n' printf '| --------------- | -------------------- | ------------ | -------------------- |\n' printf '| bc.digit_clamp | Whether to have bc | 0 | BC_DIGIT_CLAMP |\n' printf '| | clamp digits that | | |\n' printf '| | are greater than or | | |\n' printf '| | equal to the current | | |\n' printf '| | ibase when parsing | | |\n' printf '| | numbers. | | |\n' printf '| --------------- | -------------------- | ------------ | -------------------- |\n' printf '| dc.digit_clamp | Whether to have dc | 0 | DC_DIGIT_CLAMP |\n' printf '| | clamp digits that | | |\n' printf '| | are greater than or | | |\n' printf '| | equal to the current | | |\n' printf '| | ibase when parsing | | |\n' printf '| | numbers. | | |\n' printf '| --------------- | -------------------- | ------------ | -------------------- |\n' printf '\n' printf 'These settings are not meant to be changed on a whim. They are meant to ensure\n' printf 'that this bc and dc will conform to the expectations of the user on each\n' printf 'platform.\n' exit "$_usage_val" } # Replaces a file extension in a filename. This is used mostly to turn filenames # like `src/num.c` into `src/num.o`. In other words, it helps to link targets to # the files they depend on. # # @param file The filename. # @param ext1 The extension to replace. # @param ext2 The new extension. replace_ext() { if [ "$#" -ne 3 ]; then err_exit "Invalid number of args to $0" fi _replace_ext_file="$1" _replace_ext_ext1="$2" _replace_ext_ext2="$3" _replace_ext_result="${_replace_ext_file%.$_replace_ext_ext1}.$_replace_ext_ext2" printf '%s\n' "$_replace_ext_result" } # Replaces a file extension in every filename given in a list. The list is just # a space-separated list of words, so filenames are expected to *not* have # spaces in them. See the documentation for `replace_ext()`. # # @param files The list of space-separated filenames to replace extensions for. # @param ext1 The extension to replace. # @param ext2 The new extension. replace_exts() { if [ "$#" -ne 3 ]; then err_exit "Invalid number of args to $0" fi _replace_exts_files="$1" _replace_exts_ext1="$2" _replace_exts_ext2="$3" for _replace_exts_file in $_replace_exts_files; do _replace_exts_new_name=$(replace_ext "$_replace_exts_file" "$_replace_exts_ext1" "$_replace_exts_ext2") _replace_exts_result="$_replace_exts_result $_replace_exts_new_name" done printf '%s\n' "$_replace_exts_result" } # Finds a placeholder in @a str and replaces it. This is the workhorse of # configure.sh. It's what replaces placeholders in Makefile.in with the data # needed for the chosen build. Below, you will see a lot of calls to this # function. # # Note that needle can never contain an exclamation point. For more information, # see substring_replace() in scripts/functions.sh. # # @param str The string to find and replace placeholders in. # @param needle The placeholder name. # @param replacement The string to use to replace the placeholder. replace() { if [ "$#" -ne 3 ]; then err_exit "Invalid number of args to $0" fi _replace_str="$1" _replace_needle="$2" _replace_replacement="$3" substring_replace "$_replace_str" "%%$_replace_needle%%" "$_replace_replacement" } # This function finds all the source files that need to be built. If there is # only one argument and it is empty, then all source files are built. Otherwise, # the arguments are all assumed to be source files that should *not* be built. find_src_files() { _find_src_files_args="" if [ "$#" -ge 1 ] && [ "$1" != "" ]; then while [ "$#" -ge 1 ]; do _find_src_files_a="${1## }" shift _find_src_files_args=$(printf '%s\n%s/src/%s\n' "$_find_src_files_args" "$scriptdir" "${_find_src_files_a}") done fi - _find_src_files_files=$(find "$scriptdir/src/" -depth -name "*.c" -print | LC_ALL=C sort) + _find_src_files_files=$(find "$scriptdir/src" -depth -name "*.c" -print | LC_ALL=C sort) _find_src_files_result="" for _find_src_files_f in $_find_src_files_files; do # If this is true, the file is part of args, and therefore, unneeded. if [ "${_find_src_files_args##*$_find_src_files_f}" != "${_find_src_files_args}" ]; then continue fi _find_src_files_result=$(printf '%s\n%s\n' "$_find_src_files_result" "$_find_src_files_f") done printf '%s\n' "$_find_src_files_result" } # This function generates a list of files to go into the Makefile. It generates # the list of object files, as well as the list of test coverage files. # # @param contents The contents of the Makefile template to put the list of # files into. gen_file_list() { if [ "$#" -lt 1 ]; then err_exit "Invalid number of args to $0" fi _gen_file_list_contents="$1" shift if [ "$#" -ge 1 ]; then _gen_file_list_unneeded="$@" else _gen_file_list_unneeded="" fi _gen_file_list_needle_src="SRC" _gen_file_list_needle_obj="OBJ" _gen_file_list_needle_gcda="GCDA" _gen_file_list_needle_gcno="GCNO" _gen_file_list_replacement=$(find_src_files $_gen_file_list_unneeded | tr '\n' ' ') _gen_file_list_contents=$(replace "$_gen_file_list_contents" \ "$_gen_file_list_needle_src" "$_gen_file_list_replacement") _gen_file_list_cbases="" for _gen_file_list_f in $_gen_file_list_replacement; do _gen_file_list_b=$(basename "$_gen_file_list_f") _gen_file_list_cbases="$_gen_file_list_cbases src/$_gen_file_list_b" done _gen_file_list_replacement=$(replace_exts "$_gen_file_list_cbases" "c" "o") _gen_file_list_contents=$(replace "$_gen_file_list_contents" \ "$_gen_file_list_needle_obj" "$_gen_file_list_replacement") _gen_file_list_replacement=$(replace_exts "$_gen_file_list_replacement" "o" "gcda") _gen_file_list_contents=$(replace "$_gen_file_list_contents" \ "$_gen_file_list_needle_gcda" "$_gen_file_list_replacement") _gen_file_list_replacement=$(replace_exts "$_gen_file_list_replacement" "gcda" "gcno") _gen_file_list_contents=$(replace "$_gen_file_list_contents" \ "$_gen_file_list_needle_gcno" "$_gen_file_list_replacement") printf '%s\n' "$_gen_file_list_contents" } # Generates the proper test targets for each test to have its own target. This # allows `make test` to run in parallel. # # @param name Which calculator to generate tests for. # @param extra_math An integer that, if non-zero, activates extra math tests. # @param time_tests An integer that, if non-zero, tells the test suite to time # the execution of each test. gen_std_tests() { _gen_std_tests_name="$1" shift _gen_std_tests_extra_math="$1" shift _gen_std_tests_time_tests="$1" shift _gen_std_tests_extra_required=$(cat "$scriptdir/tests/extra_required.txt") for _gen_std_tests_t in $(cat "$scriptdir/tests/$_gen_std_tests_name/all.txt"); do if [ "$_gen_std_tests_extra_math" -eq 0 ]; then if [ -z "${_gen_std_tests_extra_required##*$_gen_std_tests_t*}" ]; then printf 'test_%s_%s:\n\t@printf "Skipping %s %s\\n"\n\n' \ "$_gen_std_tests_name" "$_gen_std_tests_t" "$_gen_std_tests_name" \ "$_gen_std_tests_t" >> "Makefile" continue fi fi printf 'test_%s_%s:\n\t@export BC_TEST_OUTPUT_DIR="%s/tests"; sh $(TESTSDIR)/test.sh %s %s %s %s %s\n\n' \ "$_gen_std_tests_name" "$_gen_std_tests_t" "$builddir" "$_gen_std_tests_name" \ "$_gen_std_tests_t" "$generate_tests" "$time_tests" \ "$*" >> "Makefile" done } # Generates a list of test targets that will be used as prerequisites for other # targets. # # @param name The name of the calculator to generate test targets for. gen_std_test_targets() { _gen_std_test_targets_name="$1" shift _gen_std_test_targets_tests=$(cat "$scriptdir/tests/${_gen_std_test_targets_name}/all.txt") for _gen_std_test_targets_t in $_gen_std_test_targets_tests; do printf ' test_%s_%s' "$_gen_std_test_targets_name" "$_gen_std_test_targets_t" done printf '\n' } # Generates the proper test targets for each error test to have its own target. # This allows `make test_bc_errors` and `make test_dc_errors` to run in # parallel. # # @param name Which calculator to generate tests for. gen_err_tests() { _gen_err_tests_name="$1" shift _gen_err_tests_fs=$(ls "$scriptdir/tests/$_gen_err_tests_name/errors/") for _gen_err_tests_t in $_gen_err_tests_fs; do printf 'test_%s_error_%s:\n\t@export BC_TEST_OUTPUT_DIR="%s/tests"; sh $(TESTSDIR)/error.sh %s %s %s %s\n\n' \ "$_gen_err_tests_name" "$_gen_err_tests_t" "$builddir" "$_gen_err_tests_name" \ "$_gen_err_tests_t" "$problematic_tests" "$*" >> "Makefile" done } # Generates a list of error test targets that will be used as prerequisites for # other targets. # # @param name The name of the calculator to generate test targets for. gen_err_test_targets() { _gen_err_test_targets_name="$1" shift _gen_err_test_targets_tests=$(ls "$scriptdir/tests/$_gen_err_test_targets_name/errors/") for _gen_err_test_targets_t in $_gen_err_test_targets_tests; do printf ' test_%s_error_%s' "$_gen_err_test_targets_name" "$_gen_err_test_targets_t" done printf '\n' } # Generates the proper script test targets for each script test to have its own # target. This allows `make test` to run in parallel. # # @param name Which calculator to generate tests for. # @param extra_math An integer that, if non-zero, activates extra math tests. # @param generate An integer that, if non-zero, activates generated tests. # @param time_tests An integer that, if non-zero, tells the test suite to time # the execution of each test. gen_script_tests() { _gen_script_tests_name="$1" shift _gen_script_tests_extra_math="$1" shift _gen_script_tests_generate="$1" shift _gen_script_tests_time="$1" shift _gen_script_tests_tests=$(cat "$scriptdir/tests/$_gen_script_tests_name/scripts/all.txt") for _gen_script_tests_f in $_gen_script_tests_tests; do _gen_script_tests_b=$(basename "$_gen_script_tests_f" ".${_gen_script_tests_name}") printf 'test_%s_script_%s:\n\t@export BC_TEST_OUTPUT_DIR="%s/tests"; sh $(TESTSDIR)/script.sh %s %s %s 1 %s %s %s\n\n' \ "$_gen_script_tests_name" "$_gen_script_tests_b" "$builddir" "$_gen_script_tests_name" \ "$_gen_script_tests_f" "$_gen_script_tests_extra_math" "$_gen_script_tests_generate" \ "$_gen_script_tests_time" "$*" >> "Makefile" done } set_default() { _set_default_on="$1" shift _set_default_name="$1" shift # The reason that the variables that are being set do not have the same # non-collision avoidance that the other variables do is that we *do* want # the settings of these variables to leak out of the function. They adjust # the settings outside of the function. case "$_set_default_name" in bc.banner) bc_default_banner="$_set_default_on" ;; bc.sigint_reset) bc_default_sigint_reset="$_set_default_on" ;; dc.sigint_reset) dc_default_sigint_reset="$_set_default_on" ;; bc.tty_mode) bc_default_tty_mode="$_set_default_on" ;; dc.tty_mode) dc_default_tty_mode="$_set_default_on" ;; bc.prompt) bc_default_prompt="$_set_default_on" ;; dc.prompt) dc_default_prompt="$_set_default_on" ;; bc.expr_exit) bc_default_expr_exit="$_set_default_on";; dc.expr_exit) dc_default_expr_exit="$_set_default_on";; bc.digit_clamp) bc_default_digit_clamp="$_set_default_on";; dc.digit_clamp) dc_default_digit_clamp="$_set_default_on";; ?) usage "Invalid setting: $_set_default_name" ;; esac } predefined_build() { _predefined_build_type="$1" shift # The reason that the variables that are being set do not have the same # non-collision avoidance that the other variables do is that we *do* want # the settings of these variables to leak out of the function. They adjust # the settings outside of the function. case "$_predefined_build_type" in BSD) bc_only=0 dc_only=0 coverage=0 debug=0 optimization="3" hist=1 hist_impl="editline" extra_math=1 generate_tests=$generate_tests install_manpages=0 nls=1 force=0 strip_bin=1 all_locales=0 library=0 fuzz=0 time_tests=0 vg=0 memcheck=0 clean=1 bc_default_banner=0 bc_default_sigint_reset=1 dc_default_sigint_reset=1 bc_default_tty_mode=1 dc_default_tty_mode=0 bc_default_prompt="" dc_default_prompt="" bc_default_expr_exit=1 dc_default_expr_exit=1 bc_default_digit_clamp=0 dc_default_digit_clamp=0;; GNU) bc_only=0 dc_only=0 coverage=0 debug=0 optimization="3" hist=1 hist_impl="internal" extra_math=1 generate_tests=$generate_tests install_manpages=1 nls=1 force=0 strip_bin=1 all_locales=0 library=0 fuzz=0 time_tests=0 vg=0 memcheck=0 clean=1 bc_default_banner=1 bc_default_sigint_reset=1 dc_default_sigint_reset=0 bc_default_tty_mode=1 dc_default_tty_mode=0 bc_default_prompt="" dc_default_prompt="" bc_default_expr_exit=1 dc_default_expr_exit=1 bc_default_digit_clamp=1 dc_default_digit_clamp=0;; GDH) - CFLAGS="-flto -Weverything -Wno-padded -Wno-unsafe-buffer-usage -Werror -pedantic -std=c11" + CFLAGS="-flto -Weverything -Wno-padded -Wno-unsafe-buffer-usage -Wno-poison-system-directories -Werror -pedantic -std=c11" bc_only=0 dc_only=0 coverage=0 debug=0 optimization="3" hist=1 hist_impl="internal" extra_math=1 generate_tests=1 install_manpages=1 nls=0 force=0 strip_bin=1 all_locales=0 library=0 fuzz=0 time_tests=0 vg=0 memcheck=0 clean=1 bc_default_banner=1 bc_default_sigint_reset=1 dc_default_sigint_reset=1 bc_default_tty_mode=1 dc_default_tty_mode=1 bc_default_prompt="" dc_default_prompt="" bc_default_expr_exit=0 dc_default_expr_exit=0 bc_default_digit_clamp=1 dc_default_digit_clamp=1;; DBG) - CFLAGS="-Weverything -Wno-padded -Wno-unsafe-buffer-usage -Werror -pedantic -std=c11" + CFLAGS="-Weverything -Wno-padded -Wno-unsafe-buffer-usage -Wno-poison-system-directories -Werror -pedantic -std=c11" bc_only=0 dc_only=0 coverage=0 debug=1 optimization="0" hist=1 hist_impl="internal" extra_math=1 generate_tests=1 install_manpages=1 nls=1 force=0 strip_bin=1 all_locales=0 library=0 fuzz=0 time_tests=0 vg=0 memcheck=1 clean=1 bc_default_banner=1 bc_default_sigint_reset=1 dc_default_sigint_reset=1 bc_default_tty_mode=1 dc_default_tty_mode=1 bc_default_prompt="" dc_default_prompt="" bc_default_expr_exit=0 dc_default_expr_exit=0 bc_default_digit_clamp=1 dc_default_digit_clamp=1;; ?|'') usage "Invalid user build: \"$_predefined_build_type\". Accepted types are BSD, GNU, GDH, DBG.";; esac } # Generates a list of script test targets that will be used as prerequisites for # other targets. # # @param name The name of the calculator to generate script test targets for. gen_script_test_targets() { _gen_script_test_targets_name="$1" shift _gen_script_test_targets_tests=$(cat "$scriptdir/tests/$_gen_script_test_targets_name/scripts/all.txt") for _gen_script_test_targets_f in $_gen_script_test_targets_tests; do _gen_script_test_targets_b=$(basename "$_gen_script_test_targets_f" \ ".$_gen_script_test_targets_name") printf ' test_%s_script_%s' "$_gen_script_test_targets_name" \ "$_gen_script_test_targets_b" done printf '\n' } # This is a list of defaults, but it is also the list of possible options for # users to change. # # The development options are: force (force options even if they fail), valgrind # (build in a way suitable for valgrind testing), memcheck (same as valgrind), # and fuzzing (build in a way suitable for fuzzing). bc_only=0 dc_only=0 coverage=0 karatsuba_len=32 debug=0 hist=1 hist_impl="internal" extra_math=1 optimization="" generate_tests=1 install_manpages=1 nls=1 force=0 strip_bin=1 all_locales=0 library=0 fuzz=0 time_tests=0 vg=0 memcheck=0 clean=1 problematic_tests=1 # The empty strings are because they depend on TTY mode. If they are directly # set, though, they will be integers. We test for empty strings later. bc_default_banner=0 bc_default_sigint_reset=1 dc_default_sigint_reset=1 bc_default_tty_mode=1 dc_default_tty_mode=0 bc_default_prompt="" dc_default_prompt="" bc_default_expr_exit=1 dc_default_expr_exit=1 bc_default_digit_clamp=0 dc_default_digit_clamp=0 # getopts is a POSIX utility, but it cannot handle long options. Thus, the # handling of long options is done by hand, and that's the reason that short and # long options cannot be mixed. while getopts "abBcdDeEfgGhHik:lMmNO:p:PrS:s:tTvz-" opt; do case "$opt" in a) library=1 ;; b) bc_only=1 ;; B) dc_only=1 ;; c) coverage=1 ;; C) clean=0 ;; d) dc_only=1 ;; D) bc_only=1 ;; e) hist_impl="editline" ;; E) extra_math=0 ;; f) force=1 ;; g) debug=1 ;; G) generate_tests=0 ;; h) usage ;; H) hist=0 ;; i) hist_impl="internal" ;; k) karatsuba_len="$OPTARG" ;; l) all_locales=1 ;; m) memcheck=1 ;; M) install_manpages=0 ;; N) nls=0 ;; O) optimization="$OPTARG" ;; p) predefined_build "$OPTARG" ;; P) problematic_tests=0 ;; r) hist_impl="readline" ;; S) set_default 0 "$OPTARG" ;; s) set_default 1 "$OPTARG" ;; t) time_tests=1 ;; T) strip_bin=0 ;; v) vg=1 ;; z) fuzz=1 ;; -) arg="$1" arg="${arg#--}" LONG_OPTARG="${arg#*=}" case $arg in help) usage ;; library) library=1 ;; bc-only) bc_only=1 ;; dc-only) dc_only=1 ;; coverage) coverage=1 ;; debug) debug=1 ;; force) force=1 ;; prefix=?*) PREFIX="$LONG_OPTARG" ;; prefix) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi PREFIX="$2" shift ;; bindir=?*) BINDIR="$LONG_OPTARG" ;; bindir) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi BINDIR="$2" shift ;; includedir=?*) INCLUDEDIR="$LONG_OPTARG" ;; includedir) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi INCLUDEDIR="$2" shift ;; libdir=?*) LIBDIR="$LONG_OPTARG" ;; libdir) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi LIBDIR="$2" shift ;; datarootdir=?*) DATAROOTDIR="$LONG_OPTARG" ;; datarootdir) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi DATAROOTDIR="$2" shift ;; datadir=?*) DATADIR="$LONG_OPTARG" ;; datadir) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi DATADIR="$2" shift ;; mandir=?*) MANDIR="$LONG_OPTARG" ;; mandir) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi MANDIR="$2" shift ;; man1dir=?*) MAN1DIR="$LONG_OPTARG" ;; man1dir) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi MAN1DIR="$2" shift ;; man3dir=?*) MAN3DIR="$LONG_OPTARG" ;; man3dir) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi MAN3DIR="$2" shift ;; karatsuba-len=?*) karatsuba_len="$LONG_OPTARG" ;; karatsuba-len) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi karatsuba_len="$1" shift ;; opt=?*) optimization="$LONG_OPTARG" ;; opt) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi optimization="$1" shift ;; set-default-on=?*) set_default 1 "$LONG_OPTARG" ;; set-default-on) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi set_default 1 "$1" shift ;; set-default-off=?*) set_default 0 "$LONG_OPTARG" ;; set-default-off) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi set_default 0 "$1" shift ;; predefined-build-type=?*) predefined_build "$LONG_OPTARG" ;; predefined-build-type) if [ "$#" -lt 2 ]; then usage "No argument given for '--$arg' option" fi predefined_build "$1" shift ;; disable-bc) dc_only=1 ;; disable-dc) bc_only=1 ;; disable-clean) clean=0 ;; disable-extra-math) extra_math=0 ;; disable-generated-tests) generate_tests=0 ;; disable-history) hist=0 ;; disable-man-pages) install_manpages=0 ;; disable-nls) nls=0 ;; disable-strip) strip_bin=0 ;; disable-problematic-tests) problematic_tests=0 ;; enable-editline) hist_impl="editline" ;; enable-readline) hist_impl="readline" ;; enable-internal-history) hist_impl="internal" ;; enable-test-timing) time_tests=1 ;; enable-valgrind) vg=1 ;; enable-fuzz-mode) fuzz=1 ;; enable-memcheck) memcheck=1 ;; install-all-locales) all_locales=1 ;; help* | bc-only* | dc-only* | coverage* | debug*) usage "No arg allowed for --$arg option" ;; disable-bc* | disable-dc* | disable-clean*) usage "No arg allowed for --$arg option" ;; disable-extra-math*) usage "No arg allowed for --$arg option" ;; disable-generated-tests* | disable-history*) usage "No arg allowed for --$arg option" ;; disable-man-pages* | disable-nls* | disable-strip*) usage "No arg allowed for --$arg option" ;; disable-problematic-tests*) usage "No arg allowed for --$arg option" ;; enable-fuzz-mode* | enable-test-timing* | enable-valgrind*) usage "No arg allowed for --$arg option" ;; enable-memcheck* | install-all-locales*) usage "No arg allowed for --$arg option" ;; enable-editline* | enable-readline*) usage "No arg allowed for --$arg option" ;; enable-internal-history*) usage "No arg allowed for --$arg option" ;; '') break ;; # "--" terminates argument processing * ) usage "Invalid option $LONG_OPTARG" ;; esac shift OPTIND=1 ;; ?) usage "Invalid option: $opt" ;; esac done # Sometimes, developers don't want configure.sh to do a config clean. But # sometimes they do. if [ "$clean" -ne 0 ]; then if [ -f ./Makefile ]; then make clean_config > /dev/null fi fi # It is an error to say that bc only should be built and likewise for dc. if [ "$bc_only" -eq 1 ] && [ "$dc_only" -eq 1 ]; then usage "Can only specify one of -b(-D) or -d(-B)" fi # The library is mutually exclusive to the calculators, so it's an error to # give an option for either of them. if [ "$library" -ne 0 ]; then if [ "$bc_only" -eq 1 ] || [ "$dc_only" -eq 1 ]; then usage "Must not specify -b(-D) or -d(-B) when building the library" fi fi # KARATSUBA_LEN must be an integer and must be 16 or greater. case $karatsuba_len in (*[!0-9]*|'') usage "KARATSUBA_LEN is not a number" ;; (*) ;; esac if [ "$karatsuba_len" -lt 16 ]; then usage "KARATSUBA_LEN is less than 16" fi set -e if [ -z "${LONG_BIT+set}" ]; then LONG_BIT_DEFINE="" elif [ "$LONG_BIT" -lt 32 ]; then usage "LONG_BIT is less than 32" else LONG_BIT_DEFINE="-DBC_LONG_BIT=$LONG_BIT" fi if [ -z "$CC" ]; then CC="c99" else # I had users complain that, if they gave CFLAGS as part of CC, which # autotools allows in its braindead way, the build would fail with an error. # I don't like adjusting for autotools, but oh well. These lines puts the # stuff after the first space into CFLAGS. ccbase=$(basename "$CC") suffix=" *" prefix="* " if [ "${ccbase%%$suffix}" != "$ccbase" ]; then ccflags="${ccbase#$prefix}" cc="${ccbase%%$suffix}" ccdir=$(dirname "$CC") if [ "$ccdir" = "." ] && [ "${CC#.}" = "$CC" ]; then ccdir="" else ccdir="$ccdir/" fi CC="${ccdir}${cc}" CFLAGS="$CFLAGS $ccflags" fi fi if [ -z "$HOSTCC" ] && [ -z "$HOST_CC" ]; then HOSTCC="$CC" elif [ -z "$HOSTCC" ]; then HOSTCC="$HOST_CC" fi if [ "$HOSTCC" != "$CC" ]; then # Like above, this splits HOSTCC and HOSTCFLAGS. ccbase=$(basename "$HOSTCC") suffix=" *" prefix="* " if [ "${ccbase%%$suffix}" != "$ccbase" ]; then ccflags="${ccbase#$prefix}" cc="${ccbase%%$suffix}" ccdir=$(dirname "$HOSTCC") if [ "$ccdir" = "." ] && [ "${HOSTCC#.}" = "$HOSTCC" ]; then ccdir="" else ccdir="$ccdir/" fi HOSTCC="${ccdir}${cc}" HOSTCFLAGS="$HOSTCFLAGS $ccflags" fi fi if [ -z "${HOSTCFLAGS+set}" ] && [ -z "${HOST_CFLAGS+set}" ]; then HOSTCFLAGS="$CFLAGS" elif [ -z "${HOSTCFLAGS+set}" ]; then HOSTCFLAGS="$HOST_CFLAGS" fi # Store these for the cross compilation detection later. OLDCFLAGS="$CFLAGS" OLDHOSTCFLAGS="$HOSTCFLAGS" link="@printf 'No link necessary\\\\n'" main_exec="BC" executable="BC_EXEC" tests="test_bc timeconst test_dc" bc_test="@export BC_TEST_OUTPUT_DIR=\"$builddir/tests\"; \$(TESTSDIR)/all.sh bc $extra_math 1 $generate_tests $problematic_tests $time_tests \$(BC_EXEC)" bc_test_np="@export BC_TEST_OUTPUT_DIR=\"$builddir/tests\"; \$(TESTSDIR)/all.sh -n bc $extra_math 1 $generate_tests $problematic_tests $time_tests \$(BC_EXEC)" dc_test="@export BC_TEST_OUTPUT_DIR=\"$builddir/tests\"; \$(TESTSDIR)/all.sh dc $extra_math 1 $generate_tests $problematic_tests $time_tests \$(DC_EXEC)" dc_test_np="@export BC_TEST_OUTPUT_DIR=\"$builddir/tests\"; \$(TESTSDIR)/all.sh -n dc $extra_math 1 $generate_tests $problematic_tests $time_tests \$(DC_EXEC)" timeconst="@export BC_TEST_OUTPUT_DIR=\"$builddir/tests\"; \$(TESTSDIR)/bc/timeconst.sh \$(TESTSDIR)/bc/scripts/timeconst.bc \$(BC_EXEC)" # In order to have cleanup at exit, we need to be in # debug mode, so don't run valgrind without that. if [ "$vg" -ne 0 ]; then debug=1 bc_test_exec='valgrind $(VALGRIND_ARGS) $(BC_EXEC)' dc_test_exec='valgrind $(VALGRIND_ARGS) $(DC_EXEC)' bcl_test_exec='valgrind $(VALGRIND_ARGS) $(BCL_TEST)' else bc_test_exec='$(BC_EXEC)' dc_test_exec='$(DC_EXEC)' bcl_test_exec='$(BCL_TEST)' fi test_bc_history_prereqs="test_bc_history_all" test_dc_history_prereqs="test_dc_history_all" karatsuba="@printf 'karatsuba cannot be run because one of bc or dc is not built\\\\n'" karatsuba_test="@printf 'karatsuba cannot be run because one of bc or dc is not built\\\\n'" bc_lib="\$(GEN_DIR)/lib.o" bc_help="\$(GEN_DIR)/bc_help.o" dc_help="\$(GEN_DIR)/dc_help.o" default_target_prereqs="\$(BIN) \$(OBJS)" default_target_cmd="\$(CC) \$(CFLAGS) \$(OBJS) \$(LDFLAGS) -o \$(EXEC)" default_target="\$(DC_EXEC)" second_target_prereqs="" second_target_cmd="$default_target_cmd" second_target="\$(BC_EXEC)" # This if/else if chain is for setting the defaults that change based on whether # the library is being built, bc only, dc only, or both calculators. if [ "$library" -ne 0 ]; then extra_math=1 nls=0 hist=0 bc=1 dc=1 default_target_prereqs="\$(BIN) \$(OBJ)" default_target_cmd="ar -r -cu \$(LIBBC) \$(OBJ)" default_target="\$(LIBBC)" tests="test_library" test_bc_history_prereqs=" test_bc_history_skip" test_dc_history_prereqs=" test_dc_history_skip" install_prereqs=" install_library" uninstall_prereqs=" uninstall_library" install_man_prereqs=" install_bcl_manpage" uninstall_man_prereqs=" uninstall_bcl_manpage" elif [ "$bc_only" -eq 1 ]; then bc=1 dc=0 dc_help="" executables="bc" dc_test="@printf 'No dc tests to run\\\\n'" dc_test_np="@printf 'No dc tests to run\\\\n'" test_dc_history_prereqs=" test_dc_history_skip" install_prereqs=" install_execs" install_man_prereqs=" install_bc_manpage" uninstall_prereqs=" uninstall_bc" uninstall_man_prereqs=" uninstall_bc_manpage" default_target="\$(BC_EXEC)" second_target="\$(DC_EXEC)" tests="test_bc timeconst" elif [ "$dc_only" -eq 1 ]; then bc=0 dc=1 bc_lib="" bc_help="" executables="dc" main_exec="DC" executable="DC_EXEC" bc_test="@printf 'No bc tests to run\\\\n'" bc_test_np="@printf 'No bc tests to run\\\\n'" test_bc_history_prereqs=" test_bc_history_skip" timeconst="@printf 'timeconst cannot be run because bc is not built\\\\n'" install_prereqs=" install_execs" install_man_prereqs=" install_dc_manpage" uninstall_prereqs=" uninstall_dc" uninstall_man_prereqs=" uninstall_dc_manpage" tests="test_dc" else bc=1 dc=1 executables="bc and dc" karatsuba="@\$(KARATSUBA) 30 0 \$(BC_EXEC)" karatsuba_test="@\$(KARATSUBA) 1 100 \$(BC_EXEC)" if [ "$library" -eq 0 ]; then install_prereqs=" install_execs" install_man_prereqs=" install_bc_manpage install_dc_manpage" uninstall_prereqs=" uninstall_bc uninstall_dc" uninstall_man_prereqs=" uninstall_bc_manpage uninstall_dc_manpage" else install_prereqs=" install_library install_bcl_header" install_man_prereqs=" install_bcl_manpage" uninstall_prereqs=" uninstall_library uninstall_bcl_header" uninstall_man_prereqs=" uninstall_bcl_manpage" tests="test_library" fi second_target_prereqs="$default_target_prereqs" default_target_prereqs="$second_target" default_target_cmd="\$(LINK) \$(BIN) \$(EXEC_PREFIX)\$(DC)" fi # We need specific stuff for fuzzing. if [ "$fuzz" -ne 0 ]; then debug=1 hist=0 nls=0 optimization="3" fi # This sets some necessary things for debug mode. if [ "$debug" -eq 1 ]; then if [ -z "$CFLAGS" ] && [ -z "$optimization" ]; then CFLAGS="-O0" fi CFLAGS="-g $CFLAGS" else - CPPFLAGS="-DNDEBUG $CPPFLAGS" - - if [ "$strip_bin" -ne 0 ]; then - LDFLAGS="-s $LDFLAGS" - fi fi # Set optimization CFLAGS. if [ -n "$optimization" ]; then CFLAGS="-O$optimization $CFLAGS" fi # Set test coverage defaults. if [ "$coverage" -eq 1 ]; then if [ "$bc_only" -eq 1 ] || [ "$dc_only" -eq 1 ]; then usage "Can only specify -c without -b or -d" fi CFLAGS="-fprofile-arcs -ftest-coverage -g -O0 $CFLAGS" CPPFLAGS="-DNDEBUG $CPPFLAGS" COVERAGE_OUTPUT="@gcov -pabcdf \$(GCDA) \$(BC_GCDA) \$(DC_GCDA) \$(HISTORY_GCDA) \$(RAND_GCDA)" COVERAGE_OUTPUT="$COVERAGE_OUTPUT;\$(RM) -f \$(GEN)*.gc*" COVERAGE_OUTPUT="$COVERAGE_OUTPUT;gcovr --exclude-unreachable-branches --exclude-throw-branches --html-details --output index.html" COVERAGE_PREREQS=" test coverage_output" else COVERAGE_OUTPUT="@printf 'Coverage not generated\\\\n'" COVERAGE_PREREQS="" fi # Set some defaults. if [ -z "${DESTDIR+set}" ]; then destdir="" else destdir="DESTDIR = $DESTDIR" fi # defprefix is for a warning about locales later. if [ -z "${PREFIX+set}" ]; then PREFIX="/usr/local" defprefix=1 else defprefix=0 fi if [ -z "${BINDIR+set}" ]; then BINDIR="$PREFIX/bin" fi if [ -z "${INCLUDEDIR+set}" ]; then INCLUDEDIR="$PREFIX/include" fi if [ -z "${LIBDIR+set}" ]; then LIBDIR="$PREFIX/lib" fi if [ -z "${PC_PATH+set}" ]; then set +e command -v pkg-config > /dev/null err=$? set -e if [ "$err" -eq 0 ]; then PC_PATH=$(pkg-config --variable=pc_path pkg-config) PC_PATH="${PC_PATH%%:*}" else PC_PATH="" fi fi # Set a default for the DATAROOTDIR. This is done if either manpages will be # installed, or locales are enabled because that's probably where NLSPATH # points. if [ "$install_manpages" -ne 0 ] || [ "$nls" -ne 0 ]; then if [ -z "${DATAROOTDIR+set}" ]; then DATAROOTDIR="$PREFIX/share" fi fi # Set defaults for manpage environment variables. if [ "$install_manpages" -ne 0 ]; then if [ -z "${DATADIR+set}" ]; then DATADIR="$DATAROOTDIR" fi if [ -z "${MANDIR+set}" ]; then MANDIR="$DATADIR/man" fi if [ -z "${MAN1DIR+set}" ]; then MAN1DIR="$MANDIR/man1" fi if [ -z "${MAN3DIR+set}" ]; then MAN3DIR="$MANDIR/man3" fi else install_man_prereqs="" uninstall_man_prereqs="" fi # Here is where we test NLS (the locale system). This is done by trying to # compile src/vm.c, which has the relevant code. If it fails, then it is # disabled. if [ "$nls" -ne 0 ]; then set +e printf 'Testing NLS...\n' flags="-DBC_ENABLE_NLS=1 -DBC_ENABLED=$bc -DDC_ENABLED=$dc" flags="$flags -DBC_ENABLE_HISTORY=$hist -DBC_ENABLE_LIBRARY=0 -DBC_ENABLE_AFL=0" flags="$flags -DBC_ENABLE_EXTRA_MATH=$extra_math -I$scriptdir/include/" flags="$flags -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700" ccbase=$(basename "$CC") if [ "$ccbase" = "clang" ]; then flags="$flags -Wno-unreachable-code" fi "$CC" $CPPFLAGS $CFLAGS $flags -c "$scriptdir/src/vm.c" -o "./vm.o" > /dev/null 2>&1 err="$?" rm -rf "./vm.o" # If this errors, it is probably because of building on Windows, # and NLS is not supported on Windows, so disable it. if [ "$err" -ne 0 ]; then printf 'NLS does not work.\n' if [ $force -eq 0 ]; then printf 'Disabling NLS...\n\n' nls=0 else printf 'Forcing NLS...\n\n' fi else printf 'NLS works.\n\n' printf 'Testing gencat...\n' gencat "./en_US.cat" "$scriptdir/locales/en_US.msg" > /dev/null 2>&1 err="$?" rm -rf "./en_US.cat" if [ "$err" -ne 0 ]; then printf 'gencat does not work.\n' if [ $force -eq 0 ]; then printf 'Disabling NLS...\n\n' nls=0 else printf 'Forcing NLS...\n\n' fi else printf 'gencat works.\n\n' # It turns out that POSIX locales are really terrible, and running # gencat on one machine is not guaranteed to make those cat files # portable to another machine, so we had better warn the user here. if [ "$HOSTCC" != "$CC" ] || [ "$OLDHOSTCFLAGS" != "$OLDCFLAGS" ]; then printf 'Cross-compile detected.\n\n' printf 'WARNING: Catalog files generated with gencat may not be portable\n' printf ' across different architectures.\n\n' fi if [ -z "$NLSPATH" ]; then NLSPATH="/usr/share/locale/%L/%N" fi install_locales_prereqs=" install_locales" uninstall_locales_prereqs=" uninstall_locales" fi fi set -e else install_locales_prereqs="" uninstall_locales_prereqs="" all_locales=0 fi if [ "$nls" -ne 0 ] && [ "$all_locales" -ne 0 ]; then install_locales="\$(LOCALE_INSTALL) -l \$(NLSPATH) \$(MAIN_EXEC) \$(DESTDIR)" else install_locales="\$(LOCALE_INSTALL) \$(NLSPATH) \$(MAIN_EXEC) \$(DESTDIR)" fi # Like the above tested locale support, this tests history. if [ "$hist" -eq 1 ]; then if [ "$hist_impl" = "editline" ]; then editline=1 readline=0 elif [ "$hist_impl" = "readline" ]; then editline=0 readline=1 else editline=0 readline=0 fi set +e printf 'Testing history...\n' flags="-DBC_ENABLE_HISTORY=1 -DBC_ENABLED=$bc -DDC_ENABLED=$dc" flags="$flags -DBC_ENABLE_NLS=$nls -DBC_ENABLE_LIBRARY=0 -DBC_ENABLE_AFL=0" flags="$flags -DBC_ENABLE_EDITLINE=$editline -DBC_ENABLE_READLINE=$readline" flags="$flags -DBC_ENABLE_EXTRA_MATH=$extra_math -I$scriptdir/include/" flags="$flags -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700" "$CC" $CPPFLAGS $CFLAGS $flags -c "$scriptdir/src/history.c" -o "./history.o" > /dev/null 2>&1 err="$?" rm -rf "./history.o" # If this errors, it is probably because of building on Windows, # and history is not supported on Windows, so disable it. if [ "$err" -ne 0 ]; then printf 'History does not work.\n' if [ $force -eq 0 ]; then printf 'Disabling history...\n\n' hist=0 else printf 'Forcing history...\n\n' fi else printf 'History works.\n\n' fi set -e else editline=0 readline=0 fi # We have to disable the history tests if it is disabled or valgrind is on. Or # if we are using editline or readline. if [ "$hist" -eq 0 ] || [ "$vg" -ne 0 ]; then test_bc_history_prereqs=" test_bc_history_skip" test_dc_history_prereqs=" test_dc_history_skip" history_tests="@printf 'Skipping history tests...\\\\n'" CFLAGS="$CFLAGS -DBC_ENABLE_EDITLINE=0 -DBC_ENABLE_READLINE=0" else if [ "$editline" -eq 0 ] && [ "$readline" -eq 0 ]; then history_tests="@printf '\$(TEST_STARS)\\\\n\\\\nRunning history tests...\\\\n\\\\n'" history_tests="$history_tests \&\& \$(TESTSDIR)/history.sh bc -a \&\&" history_tests="$history_tests \$(TESTSDIR)/history.sh dc -a \&\& printf" history_tests="$history_tests '\\\\nAll history tests passed.\\\\n\\\\n\$(TEST_STARS)\\\\n'" else test_bc_history_prereqs=" test_bc_history_skip" test_dc_history_prereqs=" test_dc_history_skip" history_tests="@printf 'Skipping history tests...\\\\n'" fi # We are also setting the CFLAGS and LDFLAGS here. if [ "$editline" -ne 0 ]; then LDFLAGS="$LDFLAGS -ledit" CPPFLAGS="$CPPFLAGS -DBC_ENABLE_EDITLINE=1 -DBC_ENABLE_READLINE=0" elif [ "$readline" -ne 0 ]; then LDFLAGS="$LDFLAGS -lreadline" CPPFLAGS="$CPPFLAGS -DBC_ENABLE_EDITLINE=0 -DBC_ENABLE_READLINE=1" else CPPFLAGS="$CPPFLAGS -DBC_ENABLE_EDITLINE=0 -DBC_ENABLE_READLINE=0" fi fi # Test FreeBSD. This is not in an if statement because regardless of whatever # the user says, we need to know if we are on FreeBSD. If we are, we cannot set # _POSIX_C_SOURCE and _XOPEN_SOURCE. The FreeBSD headers turn *off* stuff when # that is done. set +e printf 'Testing for FreeBSD...\n' flags="-DBC_TEST_FREEBSD -DBC_ENABLE_AFL=0" "$CC" $CPPFLAGS $CFLAGS $flags "-I$scriptdir/include" -E "$scriptdir/src/vm.c" > /dev/null 2>&1 err="$?" if [ "$err" -ne 0 ]; then printf 'On FreeBSD. Not using _POSIX_C_SOURCE and _XOPEN_SOURCE.\n\n' else printf 'Not on FreeBSD. Using _POSIX_C_SOURCE and _XOPEN_SOURCE.\n\n' CPPFLAGS="$CPPFLAGS -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700" fi # Test Mac OSX. This is not in an if statement because regardless of whatever # the user says, we need to know if we are on Mac OSX. If we are, we have to set # _DARWIN_C_SOURCE. printf 'Testing for Mac OSX...\n' flags="-DBC_TEST_APPLE -DBC_ENABLE_AFL=0" "$CC" $CPPFLAGS $CFLAGS $flags "-I$scriptdir/include" -E "$scriptdir/src/vm.c" > /dev/null 2>&1 err="$?" if [ "$err" -ne 0 ]; then printf 'On Mac OSX. Using _DARWIN_C_SOURCE.\n\n' apple="-D_DARWIN_C_SOURCE" else printf 'Not on Mac OSX.\n\n' apple="" fi +# We can't use the linker's strip flag on Mac OSX. +if [ "$debug" -eq 0 ] && [ "$apple" == "" ] && [ "$strip_bin" -ne 0 ]; then + LDFLAGS="-s $LDFLAGS" +fi + # Test OpenBSD. This is not in an if statement because regardless of whatever # the user says, we need to know if we are on OpenBSD to activate _BSD_SOURCE. # No, I cannot `#define _BSD_SOURCE` in a header because OpenBSD's patched GCC # and Clang complain that that is only allowed for system headers. Sigh....So we # have to check at configure time and set it on the compiler command-line. And # we have to set it because we also set _POSIX_C_SOURCE, which OpenBSD headers # detect, and when they detect it, they turn off _BSD_SOURCE unless it is # specifically requested. printf 'Testing for OpenBSD...\n' flags="-DBC_TEST_OPENBSD -DBC_ENABLE_AFL=0" "$CC" $CPPFLAGS $CFLAGS $flags "-I$scriptdir/include" -E "$scriptdir/src/vm.c" > /dev/null 2>&1 err="$?" if [ "$err" -ne 0 ]; then printf 'On OpenBSD. Using _BSD_SOURCE.\n\n' bsd="-D_BSD_SOURCE" # Readline errors on OpenBSD, for some weird reason. if [ "$readline" -ne 0 ]; then usage "Cannot use readline on OpenBSD" fi else printf 'Not on OpenBSD.\n\n' bsd="" fi set -e if [ "$library" -eq 1 ]; then bc_lib="" fi if [ "$extra_math" -eq 1 ] && [ "$bc" -ne 0 ] && [ "$library" -eq 0 ]; then BC_LIB2_O="\$(GEN_DIR)/lib2.o" else BC_LIB2_O="" fi GEN_DIR="$scriptdir/gen" # These lines set the appropriate targets based on whether `gen/strgen.c` or # `gen/strgen.sh` is used. GEN="strgen" GEN_EXEC_TARGET="\$(HOSTCC) -DBC_ENABLE_AFL=0 -I$scriptdir/include/ \$(HOSTCFLAGS) -o \$(GEN_EXEC) \$(GEN_C)" CLEAN_PREREQS=" clean_gen clean_coverage" if [ -z "${GEN_HOST+set}" ]; then GEN_HOST=1 else if [ "$GEN_HOST" -eq 0 ]; then GEN="strgen.sh" GEN_EXEC_TARGET="@printf 'Do not need to build gen/strgen.c\\\\n'" CLEAN_PREREQS=" clean_coverage" fi fi manpage_args="" unneeded="" headers="\$(HEADERS)" # This series of if statements figure out what source files are *not* needed. if [ "$extra_math" -eq 0 ]; then exclude_extra_math=1 manpage_args="E" unneeded="$unneeded rand.c" else exclude_extra_math=0 headers="$headers \$(EXTRA_MATH_HEADERS)" fi # All of these next if statements set the build type and mark certain source # files as unneeded so that they won't have targets generated for them. if [ "$hist" -eq 0 ]; then manpage_args="${manpage_args}H" unneeded="$unneeded history.c" else headers="$headers \$(HISTORY_HEADERS)" fi if [ "$nls" -eq 0 ]; then manpage_args="${manpage_args}N" fi if [ "$bc" -eq 0 ]; then unneeded="$unneeded bc.c bc_lex.c bc_parse.c" else headers="$headers \$(BC_HEADERS)" fi if [ "$dc" -eq 0 ]; then unneeded="$unneeded dc.c dc_lex.c dc_parse.c" else headers="$headers \$(DC_HEADERS)" fi # This convoluted mess does pull the version out. If you change the format of # include/version.h, you may have to change this line. version=$(cat "$scriptdir/include/version.h" | grep "VERSION " - | awk '{ print $3 }' -) if [ "$library" -ne 0 ]; then unneeded="$unneeded args.c opt.c read.c file.c main.c" unneeded="$unneeded lang.c lex.c parse.c program.c" unneeded="$unneeded bc.c bc_lex.c bc_parse.c" unneeded="$unneeded dc.c dc_lex.c dc_parse.c" headers="$headers \$(LIBRARY_HEADERS)" if [ "$PC_PATH" != "" ]; then contents=$(cat "$scriptdir/bcl.pc.in") contents=$(replace "$contents" "INCLUDEDIR" "$INCLUDEDIR") contents=$(replace "$contents" "LIBDIR" "$LIBDIR") contents=$(replace "$contents" "VERSION" "$version") printf '%s\n' "$contents" > "$scriptdir/bcl.pc" pkg_config_install="\$(SAFE_INSTALL) \$(PC_INSTALL_ARGS) \"\$(BCL_PC)\" \"\$(DESTDIR)\$(PC_PATH)/\$(BCL_PC)\"" pkg_config_uninstall="\$(RM) -f \"\$(DESTDIR)\$(PC_PATH)/\$(BCL_PC)\"" else pkg_config_install="" pkg_config_uninstall="" fi else unneeded="$unneeded library.c" PC_PATH="" pkg_config_install="" pkg_config_uninstall="" fi # library.c is not needed under normal circumstances. if [ "$unneeded" = "" ]; then unneeded="library.c" fi # This sets the appropriate manpage for a full build. if [ "$manpage_args" = "" ]; then manpage_args="A" fi if [ "$vg" -ne 0 ]; then memcheck=1 fi if [ "$bc_default_prompt" = "" ]; then bc_default_prompt="$bc_default_tty_mode" fi if [ "$dc_default_prompt" = "" ]; then dc_default_prompt="$dc_default_tty_mode" fi # Generate the test targets and prerequisites. bc_tests=$(gen_std_test_targets bc) bc_script_tests=$(gen_script_test_targets bc) bc_err_tests=$(gen_err_test_targets bc) dc_tests=$(gen_std_test_targets dc) dc_script_tests=$(gen_script_test_targets dc) dc_err_tests=$(gen_err_test_targets dc) +printf 'unneeded: %s\n' "$unneeded" + # Print out the values; this is for debugging. printf 'Version: %s\n' "$version" if [ "$bc" -ne 0 ]; then printf 'Building bc\n' else printf 'Not building bc\n' fi if [ "$dc" -ne 0 ]; then printf 'Building dc\n' else printf 'Not building dc\n' fi printf '\n' printf 'BC_ENABLE_LIBRARY=%s\n\n' "$library" printf 'BC_ENABLE_HISTORY=%s\n' "$hist" printf 'BC_ENABLE_EXTRA_MATH=%s\n' "$extra_math" printf 'BC_ENABLE_NLS=%s\n\n' "$nls" printf 'BC_ENABLE_AFL=%s\n' "$fuzz" printf '\n' printf 'BC_NUM_KARATSUBA_LEN=%s\n' "$karatsuba_len" printf '\n' printf 'CC=%s\n' "$CC" printf 'CFLAGS=%s\n' "$CFLAGS" printf 'HOSTCC=%s\n' "$HOSTCC" printf 'HOSTCFLAGS=%s\n' "$HOSTCFLAGS" printf 'CPPFLAGS=%s\n' "$CPPFLAGS" printf 'LDFLAGS=%s\n' "$LDFLAGS" printf 'PREFIX=%s\n' "$PREFIX" printf 'BINDIR=%s\n' "$BINDIR" printf 'INCLUDEDIR=%s\n' "$INCLUDEDIR" printf 'LIBDIR=%s\n' "$LIBDIR" printf 'DATAROOTDIR=%s\n' "$DATAROOTDIR" printf 'DATADIR=%s\n' "$DATADIR" printf 'MANDIR=%s\n' "$MANDIR" printf 'MAN1DIR=%s\n' "$MAN1DIR" printf 'MAN3DIR=%s\n' "$MAN3DIR" printf 'NLSPATH=%s\n' "$NLSPATH" printf 'PC_PATH=%s\n' "$PC_PATH" printf 'EXECSUFFIX=%s\n' "$EXECSUFFIX" printf 'EXECPREFIX=%s\n' "$EXECPREFIX" printf 'DESTDIR=%s\n' "$DESTDIR" printf 'LONG_BIT=%s\n' "$LONG_BIT" printf 'GEN_HOST=%s\n' "$GEN_HOST" printf 'GEN_EMU=%s\n' "$GEN_EMU" printf '\n' printf 'Setting Defaults\n' printf '================\n' printf 'bc.banner=%s\n' "$bc_default_banner" printf 'bc.sigint_reset=%s\n' "$bc_default_sigint_reset" printf 'dc.sigint_reset=%s\n' "$dc_default_sigint_reset" printf 'bc.tty_mode=%s\n' "$bc_default_tty_mode" printf 'dc.tty_mode=%s\n' "$dc_default_tty_mode" printf 'bc.prompt=%s\n' "$bc_default_prompt" printf 'dc.prompt=%s\n' "$dc_default_prompt" printf 'bc.expr_exit=%s\n' "$bc_default_expr_exit" printf 'dc.expr_exit=%s\n' "$dc_default_expr_exit" printf 'bc.digit_clamp=%s\n' "$bc_default_digit_clamp" printf 'dc.digit_clamp=%s\n' "$dc_default_digit_clamp" # This code outputs a warning. The warning is to not surprise users when locales # are installed outside of the prefix. This warning is suppressed when the # default prefix is used, as well, so as not to panic users just installing by # hand. I believe this will be okay because NLSPATH is usually in /usr and the # default prefix is /usr/local, so they'll be close that way. if [ "$nls" -ne 0 ] && [ "${NLSPATH#$PREFIX}" = "${NLSPATH}" ] && [ "$defprefix" -eq 0 ]; then printf '\n********************************************************************************\n\n' printf 'WARNING: Locales will *NOT* be installed in $PREFIX (%s).\n' "$PREFIX" printf '\n' printf ' This is because they *MUST* be installed at a fixed location to even\n' printf ' work, and that fixed location is $NLSPATH (%s).\n' "$NLSPATH" printf '\n' printf ' This location is *outside* of $PREFIX. If you do not wish to install\n' printf ' locales outside of $PREFIX, you must disable NLS with the -N or the\n' printf ' --disable-nls options.\n' printf '\n' printf ' The author apologizes for the inconvenience, but the need to install\n' printf ' the locales at a fixed location is mandated by POSIX, and it is not\n' printf ' possible for the author to change that requirement.\n' printf '\n********************************************************************************\n' fi # This is where the real work begins. This is the point at which the Makefile.in # template is edited and output to the Makefile. contents=$(cat "$scriptdir/Makefile.in") needle="WARNING" replacement='*** WARNING: Autogenerated from Makefile.in. DO NOT MODIFY ***' contents=$(replace "$contents" "$needle" "$replacement") # The contents are edited to have the list of files to build. contents=$(gen_file_list "$contents" $unneeded) SRC_TARGETS="" # This line and loop generates the individual targets for source files. I used # to just use an implicit target, but that was found to be inadequate when I # added the library. src_files=$(find_src_files $unneeded) for f in $src_files; do o=$(replace_ext "$f" "c" "o") o=$(basename "$o") SRC_TARGETS=$(printf '%s\n\nsrc/%s: src %s %s\n\t$(CC) $(CFLAGS) -o src/%s -c %s\n' \ "$SRC_TARGETS" "$o" "$headers" "$f" "$o" "$f") done # Replace all the placeholders. contents=$(replace "$contents" "ROOTDIR" "$scriptdir") contents=$(replace "$contents" "BUILDDIR" "$builddir") contents=$(replace "$contents" "HEADERS" "$headers") contents=$(replace "$contents" "BC_ENABLED" "$bc") contents=$(replace "$contents" "DC_ENABLED" "$dc") contents=$(replace "$contents" "BC_ALL_TESTS" "$bc_test") contents=$(replace "$contents" "BC_ALL_TESTS_NP" "$bc_test_np") contents=$(replace "$contents" "BC_TESTS" "$bc_tests") contents=$(replace "$contents" "BC_SCRIPT_TESTS" "$bc_script_tests") contents=$(replace "$contents" "BC_ERROR_TESTS" "$bc_err_tests") contents=$(replace "$contents" "BC_TEST_EXEC" "$bc_test_exec") contents=$(replace "$contents" "TIMECONST_ALL_TESTS" "$timeconst") contents=$(replace "$contents" "DC_ALL_TESTS" "$dc_test") contents=$(replace "$contents" "DC_ALL_TESTS_NP" "$dc_test_np") contents=$(replace "$contents" "DC_TESTS" "$dc_tests") contents=$(replace "$contents" "DC_SCRIPT_TESTS" "$dc_script_tests") contents=$(replace "$contents" "DC_ERROR_TESTS" "$dc_err_tests") contents=$(replace "$contents" "DC_TEST_EXEC" "$dc_test_exec") contents=$(replace "$contents" "BCL_TEST_EXEC" "$bcl_test_exec") contents=$(replace "$contents" "BUILD_TYPE" "$manpage_args") contents=$(replace "$contents" "EXCLUDE_EXTRA_MATH" "$exclude_extra_math") contents=$(replace "$contents" "LIBRARY" "$library") contents=$(replace "$contents" "HISTORY" "$hist") contents=$(replace "$contents" "EXTRA_MATH" "$extra_math") contents=$(replace "$contents" "NLS" "$nls") contents=$(replace "$contents" "FUZZ" "$fuzz") contents=$(replace "$contents" "MEMCHECK" "$memcheck") contents=$(replace "$contents" "BC_LIB_O" "$bc_lib") contents=$(replace "$contents" "BC_HELP_O" "$bc_help") contents=$(replace "$contents" "DC_HELP_O" "$dc_help") contents=$(replace "$contents" "BC_LIB2_O" "$BC_LIB2_O") contents=$(replace "$contents" "KARATSUBA_LEN" "$karatsuba_len") contents=$(replace "$contents" "NLSPATH" "$NLSPATH") contents=$(replace "$contents" "DESTDIR" "$destdir") contents=$(replace "$contents" "EXECSUFFIX" "$EXECSUFFIX") contents=$(replace "$contents" "EXECPREFIX" "$EXECPREFIX") contents=$(replace "$contents" "BINDIR" "$BINDIR") contents=$(replace "$contents" "INCLUDEDIR" "$INCLUDEDIR") contents=$(replace "$contents" "LIBDIR" "$LIBDIR") contents=$(replace "$contents" "MAN1DIR" "$MAN1DIR") contents=$(replace "$contents" "MAN3DIR" "$MAN3DIR") contents=$(replace "$contents" "CFLAGS" "$CFLAGS") contents=$(replace "$contents" "HOSTCFLAGS" "$HOSTCFLAGS") contents=$(replace "$contents" "CPPFLAGS" "$CPPFLAGS") contents=$(replace "$contents" "LDFLAGS" "$LDFLAGS") contents=$(replace "$contents" "CC" "$CC") contents=$(replace "$contents" "HOSTCC" "$HOSTCC") contents=$(replace "$contents" "COVERAGE_OUTPUT" "$COVERAGE_OUTPUT") contents=$(replace "$contents" "COVERAGE_PREREQS" "$COVERAGE_PREREQS") contents=$(replace "$contents" "INSTALL_PREREQS" "$install_prereqs") contents=$(replace "$contents" "INSTALL_MAN_PREREQS" "$install_man_prereqs") contents=$(replace "$contents" "INSTALL_LOCALES" "$install_locales") contents=$(replace "$contents" "INSTALL_LOCALES_PREREQS" "$install_locales_prereqs") contents=$(replace "$contents" "UNINSTALL_MAN_PREREQS" "$uninstall_man_prereqs") contents=$(replace "$contents" "UNINSTALL_PREREQS" "$uninstall_prereqs") contents=$(replace "$contents" "UNINSTALL_LOCALES_PREREQS" "$uninstall_locales_prereqs") contents=$(replace "$contents" "PC_PATH" "$PC_PATH") contents=$(replace "$contents" "PKG_CONFIG_INSTALL" "$pkg_config_install") contents=$(replace "$contents" "PKG_CONFIG_UNINSTALL" "$pkg_config_uninstall") contents=$(replace "$contents" "DEFAULT_TARGET" "$default_target") contents=$(replace "$contents" "DEFAULT_TARGET_PREREQS" "$default_target_prereqs") contents=$(replace "$contents" "DEFAULT_TARGET_CMD" "$default_target_cmd") contents=$(replace "$contents" "SECOND_TARGET" "$second_target") contents=$(replace "$contents" "SECOND_TARGET_PREREQS" "$second_target_prereqs") contents=$(replace "$contents" "SECOND_TARGET_CMD" "$second_target_cmd") contents=$(replace "$contents" "ALL_PREREQ" "$ALL_PREREQ") contents=$(replace "$contents" "BC_EXEC_PREREQ" "$bc_exec_prereq") contents=$(replace "$contents" "BC_EXEC_CMD" "$bc_exec_cmd") contents=$(replace "$contents" "DC_EXEC_PREREQ" "$dc_exec_prereq") contents=$(replace "$contents" "DC_EXEC_CMD" "$dc_exec_cmd") contents=$(replace "$contents" "EXECUTABLES" "$executables") contents=$(replace "$contents" "MAIN_EXEC" "$main_exec") contents=$(replace "$contents" "EXEC" "$executable") contents=$(replace "$contents" "TESTS" "$tests") contents=$(replace "$contents" "BC_HISTORY_TEST_PREREQS" "$test_bc_history_prereqs") contents=$(replace "$contents" "DC_HISTORY_TEST_PREREQS" "$test_dc_history_prereqs") contents=$(replace "$contents" "HISTORY_TESTS" "$history_tests") contents=$(replace "$contents" "VG_BC_TEST" "$vg_bc_test") contents=$(replace "$contents" "VG_DC_TEST" "$vg_dc_test") contents=$(replace "$contents" "TIMECONST" "$timeconst") contents=$(replace "$contents" "KARATSUBA" "$karatsuba") contents=$(replace "$contents" "KARATSUBA_TEST" "$karatsuba_test") contents=$(replace "$contents" "LONG_BIT_DEFINE" "$LONG_BIT_DEFINE") contents=$(replace "$contents" "GEN_DIR" "$GEN_DIR") contents=$(replace "$contents" "GEN" "$GEN") contents=$(replace "$contents" "GEN_EXEC_TARGET" "$GEN_EXEC_TARGET") contents=$(replace "$contents" "CLEAN_PREREQS" "$CLEAN_PREREQS") contents=$(replace "$contents" "GEN_EMU" "$GEN_EMU") contents=$(replace "$contents" "BSD" "$bsd") contents=$(replace "$contents" "APPLE" "$apple") contents=$(replace "$contents" "BC_DEFAULT_BANNER" "$bc_default_banner") contents=$(replace "$contents" "BC_DEFAULT_SIGINT_RESET" "$bc_default_sigint_reset") contents=$(replace "$contents" "DC_DEFAULT_SIGINT_RESET" "$dc_default_sigint_reset") contents=$(replace "$contents" "BC_DEFAULT_TTY_MODE" "$bc_default_tty_mode") contents=$(replace "$contents" "DC_DEFAULT_TTY_MODE" "$dc_default_tty_mode") contents=$(replace "$contents" "BC_DEFAULT_PROMPT" "$bc_default_prompt") contents=$(replace "$contents" "DC_DEFAULT_PROMPT" "$dc_default_prompt") contents=$(replace "$contents" "BC_DEFAULT_EXPR_EXIT" "$bc_default_expr_exit") contents=$(replace "$contents" "DC_DEFAULT_EXPR_EXIT" "$dc_default_expr_exit") contents=$(replace "$contents" "BC_DEFAULT_DIGIT_CLAMP" "$bc_default_digit_clamp") contents=$(replace "$contents" "DC_DEFAULT_DIGIT_CLAMP" "$dc_default_digit_clamp") # Do the first print to the Makefile. printf '%s\n%s\n\n' "$contents" "$SRC_TARGETS" > "Makefile" # Generate the individual test targets. if [ "$bc" -ne 0 ]; then gen_std_tests bc "$extra_math" "$time_tests" $bc_test_exec gen_script_tests bc "$extra_math" "$generate_tests" "$time_tests" $bc_test_exec gen_err_tests bc $bc_test_exec fi if [ "$dc" -ne 0 ]; then gen_std_tests dc "$extra_math" "$time_tests" $dc_test_exec gen_script_tests dc "$extra_math" "$generate_tests" "$time_tests" $dc_test_exec gen_err_tests dc $dc_test_exec fi # Copy the correct manuals to the expected places. mkdir -p manuals cp -f "$scriptdir/manuals/bc/$manpage_args.1.md" manuals/bc.1.md cp -f "$scriptdir/manuals/bc/$manpage_args.1" manuals/bc.1 cp -f "$scriptdir/manuals/dc/$manpage_args.1.md" manuals/dc.1.md cp -f "$scriptdir/manuals/dc/$manpage_args.1" manuals/dc.1 make clean > /dev/null diff --git a/contrib/bc/include/version.h b/contrib/bc/include/version.h index 1cd13e9878c1..d481cb10abf1 100644 --- a/contrib/bc/include/version.h +++ b/contrib/bc/include/version.h @@ -1,42 +1,42 @@ /* * ***************************************************************************** * * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2018-2023 Gavin D. Howard and contributors. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * ***************************************************************************** * * The version of bc. * */ #ifndef BC_VERSION_H #define BC_VERSION_H /// The current version. -#define VERSION 6.7.2 +#define VERSION 6.7.4 #endif // BC_VERSION_H diff --git a/contrib/bc/manuals/bc/A.1 b/contrib/bc/manuals/bc/A.1 index fc75b5c70ac3..1810beaf9291 100644 --- a/contrib/bc/manuals/bc/A.1 +++ b/contrib/bc/manuals/bc/A.1 @@ -1,3003 +1,2944 @@ .\" .\" SPDX-License-Identifier: BSD-2-Clause .\" .\" Copyright (c) 2018-2023 Gavin D. Howard and contributors. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions are met: .\" .\" * Redistributions of source code must retain the above copyright notice, .\" this list of conditions and the following disclaimer. .\" .\" * Redistributions in binary form must reproduce the above copyright notice, .\" this list of conditions and the following disclaimer in the documentation .\" and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.TH "BC" "1" "February 2023" "Gavin D. Howard" "General Commands Manual" +.TH "BC" "1" "November 2023" "Gavin D. Howard" "General Commands Manual" .nh .ad l .SH NAME -.PP bc - arbitrary-precision decimal arithmetic language and calculator .SH SYNOPSIS -.PP \f[B]bc\f[R] [\f[B]-cCghilPqRsvVw\f[R]] [\f[B]--digit-clamp\f[R]] [\f[B]--no-digit-clamp\f[R]] [\f[B]--global-stacks\f[R]] [\f[B]--help\f[R]] [\f[B]--interactive\f[R]] [\f[B]--mathlib\f[R]] [\f[B]--no-prompt\f[R]] [\f[B]--no-read-prompt\f[R]] [\f[B]--quiet\f[R]] [\f[B]--standard\f[R]] [\f[B]--warn\f[R]] [\f[B]--version\f[R]] [\f[B]-e\f[R] \f[I]expr\f[R]] [\f[B]--expression\f[R]=\f[I]expr\f[R]\&...] [\f[B]-f\f[R] \f[I]file\f[R]\&...] [\f[B]--file\f[R]=\f[I]file\f[R]\&...] [\f[I]file\f[R]\&...] [\f[B]-I\f[R] \f[I]ibase\f[R]] [\f[B]--ibase\f[R]=\f[I]ibase\f[R]] [\f[B]-O\f[R] \f[I]obase\f[R]] [\f[B]--obase\f[R]=\f[I]obase\f[R]] [\f[B]-S\f[R] \f[I]scale\f[R]] [\f[B]--scale\f[R]=\f[I]scale\f[R]] [\f[B]-E\f[R] \f[I]seed\f[R]] [\f[B]--seed\f[R]=\f[I]seed\f[R]] .SH DESCRIPTION -.PP bc(1) is an interactive processor for a language first standardized in 1991 by POSIX. (See the \f[B]STANDARDS\f[R] section.) The language provides unlimited precision decimal arithmetic and is somewhat C-like, but there are differences. Such differences will be noted in this document. .PP After parsing and handling options, this bc(1) reads any files given on the command line and executes them before reading from \f[B]stdin\f[R]. .PP This bc(1) is a drop-in replacement for \f[I]any\f[R] bc(1), including (and especially) the GNU bc(1). It also has many extensions and extra features beyond other implementations. .PP \f[B]Note\f[R]: If running this bc(1) on \f[I]any\f[R] script meant for another bc(1) gives a parse error, it is probably because a word this bc(1) reserves as a keyword is used as the name of a function, variable, or array. To fix that, use the command-line option \f[B]-r\f[R] \f[I]keyword\f[R], where \f[I]keyword\f[R] is the keyword that is used as a name in the script. For more information, see the \f[B]OPTIONS\f[R] section. .PP If parsing scripts meant for other bc(1) implementations still does not work, that is a bug and should be reported. See the \f[B]BUGS\f[R] section. .SH OPTIONS -.PP The following are the options that bc(1) accepts. .TP \f[B]-C\f[R], \f[B]--no-digit-clamp\f[R] Disables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that the value added to a number from a digit is always that digit\[cq]s value multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-c\f[R] or \f[B]--digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-c\f[R], \f[B]--digit-clamp\f[R] Enables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that digits that the value added to a number from a digit that is greater than or equal to the ibase is the value of ibase minus 1 all multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-C\f[R] or \f[B]--no-digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-E\f[R] \f[I]seed\f[R], \f[B]--seed\f[R]=\f[I]seed\f[R] Sets the builtin variable \f[B]seed\f[R] to the value \f[I]seed\f[R] assuming that \f[I]seed\f[R] is in base 10. It is a fatal error if \f[I]seed\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-e\f[R] \f[I]expr\f[R], \f[B]--expression\f[R]=\f[I]expr\f[R] Evaluates \f[I]expr\f[R]. If multiple expressions are given, they are evaluated in order. If files are given as well (see the \f[B]-f\f[R] and \f[B]--file\f[R] options), the expressions and files are evaluated in the order given. This means that if a file is given before an expression, the file is read in and evaluated first. .RS .PP If this option is given on the command-line (i.e., not in \f[B]BC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, bc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R], whether on the command-line or in \f[B]BC_ENV_ARGS\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, bc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-f\f[R] \f[I]file\f[R], \f[B]--file\f[R]=\f[I]file\f[R] Reads in \f[I]file\f[R] and evaluates it, line by line, as though it were read through \f[B]stdin\f[R]. If expressions are also given (see the \f[B]-e\f[R] and \f[B]--expression\f[R] options), the expressions are evaluated in the order given. .RS .PP If this option is given on the command-line (i.e., not in \f[B]BC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, bc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, bc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-g\f[R], \f[B]--global-stacks\f[R] Turns the globals \f[B]ibase\f[R], \f[B]obase\f[R], \f[B]scale\f[R], and \f[B]seed\f[R] into stacks. .RS .PP This has the effect that a copy of the current value of all four are pushed onto a stack for every function call, as well as popped when every function returns. This means that functions can assign to any and all of those globals without worrying that the change will affect other functions. Thus, a hypothetical function named \f[B]output(x,b)\f[R] that simply printed \f[B]x\f[R] in base \f[B]b\f[R] could be written like this: .IP -.nf -\f[C] +.EX define void output(x, b) { obase=b x } -\f[R] -.fi +.EE .PP instead of like this: .IP -.nf -\f[C] +.EX define void output(x, b) { auto c c=obase obase=b x obase=c } -\f[R] -.fi +.EE .PP This makes writing functions much easier. .PP (\f[B]Note\f[R]: the function \f[B]output(x,b)\f[R] exists in the extended math library. See the \f[B]LIBRARY\f[R] section.) .PP However, since using this flag means that functions cannot set \f[B]ibase\f[R], \f[B]obase\f[R], \f[B]scale\f[R], or \f[B]seed\f[R] globally, functions that are made to do so cannot work anymore. There are two possible use cases for that, and each has a solution. .PP First, if a function is called on startup to turn bc(1) into a number converter, it is possible to replace that capability with various shell aliases. Examples: .IP -.nf -\f[C] +.EX alias d2o=\[dq]bc -e ibase=A -e obase=8\[dq] alias h2b=\[dq]bc -e ibase=G -e obase=2\[dq] -\f[R] -.fi +.EE .PP Second, if the purpose of a function is to set \f[B]ibase\f[R], \f[B]obase\f[R], \f[B]scale\f[R], or \f[B]seed\f[R] globally for any other purpose, it could be split into one to four functions (based on how many globals it sets) and each of those functions could return the desired value for a global. .PP For functions that set \f[B]seed\f[R], the value assigned to \f[B]seed\f[R] is not propagated to parent functions. This means that the sequence of pseudo-random numbers that they see will not be the same sequence of pseudo-random numbers that any parent sees. This is only the case once \f[B]seed\f[R] has been set. .PP If a function desires to not affect the sequence of pseudo-random numbers of its parents, but wants to use the same \f[B]seed\f[R], it can use the following line: .IP -.nf -\f[C] +.EX seed = seed -\f[R] -.fi +.EE .PP If the behavior of this option is desired for every run of bc(1), then users could make sure to define \f[B]BC_ENV_ARGS\f[R] and include this option (see the \f[B]ENVIRONMENT VARIABLES\f[R] section for more details). .PP If \f[B]-s\f[R], \f[B]-w\f[R], or any equivalents are used, this option is ignored. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-h\f[R], \f[B]--help\f[R] Prints a usage message and exits. .TP \f[B]-I\f[R] \f[I]ibase\f[R], \f[B]--ibase\f[R]=\f[I]ibase\f[R] Sets the builtin variable \f[B]ibase\f[R] to the value \f[I]ibase\f[R] assuming that \f[I]ibase\f[R] is in base 10. It is a fatal error if \f[I]ibase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-i\f[R], \f[B]--interactive\f[R] Forces interactive mode. (See the \f[B]INTERACTIVE MODE\f[R] section.) .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-L\f[R], \f[B]--no-line-length\f[R] Disables line length checking and prints numbers without backslashes and newlines. In other words, this option sets \f[B]BC_LINE_LENGTH\f[R] to \f[B]0\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-l\f[R], \f[B]--mathlib\f[R] Sets \f[B]scale\f[R] (see the \f[B]SYNTAX\f[R] section) to \f[B]20\f[R] and loads the included math library and the extended math library before running any code, including any expressions or files specified on the command line. .RS .PP To learn what is in the libraries, see the \f[B]LIBRARY\f[R] section. .RE .TP \f[B]-O\f[R] \f[I]obase\f[R], \f[B]--obase\f[R]=\f[I]obase\f[R] Sets the builtin variable \f[B]obase\f[R] to the value \f[I]obase\f[R] assuming that \f[I]obase\f[R] is in base 10. It is a fatal error if \f[I]obase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-P\f[R], \f[B]--no-prompt\f[R] Disables the prompt in TTY mode. (The prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a prompt or are not used to having them in bc(1). Most of those users would want to put this option in \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .RS .PP These options override the \f[B]BC_PROMPT\f[R] and \f[B]BC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-q\f[R], \f[B]--quiet\f[R] This option is for compatibility with the GNU bc(1) (https://www.gnu.org/software/bc/); it is a no-op. Without this option, GNU bc(1) prints a copyright header. This bc(1) only prints the copyright header if one or more of the \f[B]-v\f[R], \f[B]-V\f[R], or \f[B]--version\f[R] options are given unless the \f[B]BC_BANNER\f[R] environment variable is set and contains a non-zero integer or if this bc(1) was built with the header displayed by default. If \f[I]any\f[R] of that is the case, then this option \f[I]does\f[R] prevent bc(1) from printing the header. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-R\f[R], \f[B]--no-read-prompt\f[R] Disables the read prompt in TTY mode. (The read prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a read prompt or are not used to having them in bc(1). Most of those users would want to put this option in \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). This option is also useful in hash bang lines of bc(1) scripts that prompt for user input. .RS .PP This option does not disable the regular prompt because the read prompt is only used when the \f[B]read()\f[R] built-in function is called. .PP These options \f[I]do\f[R] override the \f[B]BC_PROMPT\f[R] and \f[B]BC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), but only for the read prompt. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-r\f[R] \f[I]keyword\f[R], \f[B]--redefine\f[R]=\f[I]keyword\f[R] Redefines \f[I]keyword\f[R] in order to allow it to be used as a function, variable, or array name. This is useful when this bc(1) gives parse errors when parsing scripts meant for other bc(1) implementations. .RS .PP The keywords this bc(1) allows to be redefined are: .IP \[bu] 2 \f[B]abs\f[R] .IP \[bu] 2 \f[B]asciify\f[R] .IP \[bu] 2 \f[B]continue\f[R] .IP \[bu] 2 \f[B]divmod\f[R] .IP \[bu] 2 \f[B]else\f[R] .IP \[bu] 2 \f[B]halt\f[R] .IP \[bu] 2 \f[B]irand\f[R] .IP \[bu] 2 \f[B]last\f[R] .IP \[bu] 2 \f[B]limits\f[R] .IP \[bu] 2 \f[B]maxibase\f[R] .IP \[bu] 2 \f[B]maxobase\f[R] .IP \[bu] 2 \f[B]maxrand\f[R] .IP \[bu] 2 \f[B]maxscale\f[R] .IP \[bu] 2 \f[B]modexp\f[R] .IP \[bu] 2 \f[B]print\f[R] .IP \[bu] 2 \f[B]rand\f[R] .IP \[bu] 2 \f[B]read\f[R] .IP \[bu] 2 \f[B]seed\f[R] .IP \[bu] 2 \f[B]stream\f[R] .PP If any of those keywords are used as a function, variable, or array name in a script, use this option with the keyword as the argument. If multiple are used, use this option for all of them; it can be used multiple times. .PP Keywords are \f[I]not\f[R] redefined when parsing the builtin math library (see the \f[B]LIBRARY\f[R] section). .PP It is a fatal error to redefine keywords mandated by the POSIX standard (see the \f[B]STANDARDS\f[R] section). It is a fatal error to attempt to redefine words that this bc(1) does not reserve as keywords. .RE .TP \f[B]-S\f[R] \f[I]scale\f[R], \f[B]--scale\f[R]=\f[I]scale\f[R] Sets the builtin variable \f[B]scale\f[R] to the value \f[I]scale\f[R] assuming that \f[I]scale\f[R] is in base 10. It is a fatal error if \f[I]scale\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-s\f[R], \f[B]--standard\f[R] Process exactly the language defined by the standard (see the \f[B]STANDARDS\f[R] section) and error if any extensions are used. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-v\f[R], \f[B]-V\f[R], \f[B]--version\f[R] Print the version information (copyright header) and exits. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-w\f[R], \f[B]--warn\f[R] Like \f[B]-s\f[R] and \f[B]--standard\f[R], except that warnings (and not errors) are printed for non-standard extensions and execution continues normally. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-z\f[R], \f[B]--leading-zeroes\f[R] Makes bc(1) print all numbers greater than \f[B]-1\f[R] and less than \f[B]1\f[R], and not equal to \f[B]0\f[R], with a leading zero. .RS .PP This can be set for individual numbers with the \f[B]plz(x)\f[R], \f[B]plznl(x)\f[R], \f[B]pnlz(x)\f[R], and \f[B]pnlznl(x)\f[R] functions in the extended math library (see the \f[B]LIBRARY\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .PP All long options are \f[B]non-portable extensions\f[R]. .SH STDIN -.PP If no files or expressions are given by the \f[B]-f\f[R], \f[B]--file\f[R], \f[B]-e\f[R], or \f[B]--expression\f[R] options, then bc(1) reads from \f[B]stdin\f[R]. .PP However, there are a few caveats to this. .PP First, \f[B]stdin\f[R] is evaluated a line at a time. The only exception to this is if the parse cannot complete. That means that starting a string without ending it or starting a function, \f[B]if\f[R] statement, or loop without ending it will also cause bc(1) to not execute. .PP Second, after an \f[B]if\f[R] statement, bc(1) doesn\[cq]t know if an \f[B]else\f[R] statement will follow, so it will not execute until it knows there will not be an \f[B]else\f[R] statement. .SH STDOUT -.PP Any non-error output is written to \f[B]stdout\f[R]. In addition, if history (see the \f[B]HISTORY\f[R] section) and the prompt (see the \f[B]TTY MODE\f[R] section) are enabled, both are output to \f[B]stdout\f[R]. .PP \f[B]Note\f[R]: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stdout\f[R], so if \f[B]stdout\f[R] is closed, as in -\f[B]bc >&-\f[R], it will quit with an error. +\f[B]bc >&-\f[R], it will quit with an error. This is done so that bc(1) can report problems when \f[B]stdout\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stdout\f[R] to \f[B]/dev/null\f[R]. .SH STDERR -.PP Any error output is written to \f[B]stderr\f[R]. .PP \f[B]Note\f[R]: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stderr\f[R], so if \f[B]stderr\f[R] is closed, as in -\f[B]bc 2>&-\f[R], it will quit with an error. +\f[B]bc 2>&-\f[R], it will quit with an error. This is done so that bc(1) can exit with an error code when \f[B]stderr\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stderr\f[R] to \f[B]/dev/null\f[R]. .SH SYNTAX -.PP The syntax for bc(1) programs is mostly C-like, with some differences. This bc(1) follows the POSIX standard (see the \f[B]STANDARDS\f[R] section), which is a much more thorough resource for the language this bc(1) accepts. This section is meant to be a summary and a listing of all the extensions to the standard. .PP In the sections below, \f[B]E\f[R] means expression, \f[B]S\f[R] means statement, and \f[B]I\f[R] means identifier. .PP Identifiers (\f[B]I\f[R]) start with a lowercase letter and can be followed by any number (up to \f[B]BC_NAME_MAX-1\f[R]) of lowercase letters (\f[B]a-z\f[R]), digits (\f[B]0-9\f[R]), and underscores (\f[B]_\f[R]). The regex is \f[B][a-z][a-z0-9_]*\f[R]. Identifiers with more than one character (letter) are a \f[B]non-portable extension\f[R]. .PP \f[B]ibase\f[R] is a global variable determining how to interpret constant numbers. It is the \[lq]input\[rq] base, or the number base used for interpreting input numbers. \f[B]ibase\f[R] is initially \f[B]10\f[R]. If the \f[B]-s\f[R] (\f[B]--standard\f[R]) and \f[B]-w\f[R] (\f[B]--warn\f[R]) flags were not given on the command line, the max allowable value for \f[B]ibase\f[R] is \f[B]36\f[R]. Otherwise, it is \f[B]16\f[R]. The min allowable value for \f[B]ibase\f[R] is \f[B]2\f[R]. The max allowable value for \f[B]ibase\f[R] can be queried in bc(1) programs with the \f[B]maxibase()\f[R] built-in function. .PP \f[B]obase\f[R] is a global variable determining how to output results. It is the \[lq]output\[rq] base, or the number base used for outputting numbers. \f[B]obase\f[R] is initially \f[B]10\f[R]. The max allowable value for \f[B]obase\f[R] is \f[B]BC_BASE_MAX\f[R] and can be queried in bc(1) programs with the \f[B]maxobase()\f[R] built-in function. The min allowable value for \f[B]obase\f[R] is \f[B]0\f[R]. If \f[B]obase\f[R] is \f[B]0\f[R], values are output in scientific notation, and if \f[B]obase\f[R] is \f[B]1\f[R], values are output in engineering notation. Otherwise, values are output in the specified base. .PP Outputting in scientific and engineering notations are \f[B]non-portable extensions\f[R]. .PP The \f[I]scale\f[R] of an expression is the number of digits in the result of the expression right of the decimal point, and \f[B]scale\f[R] is a global variable that sets the precision of any operations, with exceptions. \f[B]scale\f[R] is initially \f[B]0\f[R]. \f[B]scale\f[R] cannot be negative. The max allowable value for \f[B]scale\f[R] is \f[B]BC_SCALE_MAX\f[R] and can be queried in bc(1) programs with the \f[B]maxscale()\f[R] built-in function. .PP bc(1) has both \f[I]global\f[R] variables and \f[I]local\f[R] variables. All \f[I]local\f[R] variables are local to the function; they are parameters or are introduced in the \f[B]auto\f[R] list of a function (see the \f[B]FUNCTIONS\f[R] section). If a variable is accessed which is not a parameter or in the \f[B]auto\f[R] list, it is assumed to be \f[I]global\f[R]. If a parent function has a \f[I]local\f[R] variable version of a variable that a child function considers \f[I]global\f[R], the value of that \f[I]global\f[R] variable in the child function is the value of the variable in the parent function, not the value of the actual \f[I]global\f[R] variable. .PP All of the above applies to arrays as well. .PP The value of a statement that is an expression (i.e., any of the named expressions or operands) is printed unless the lowest precedence operator is an assignment operator \f[I]and\f[R] the expression is notsurrounded by parentheses. .PP The value that is printed is also assigned to the special variable \f[B]last\f[R]. A single dot (\f[B].\f[R]) may also be used as a synonym for \f[B]last\f[R]. These are \f[B]non-portable extensions\f[R]. .PP Either semicolons or newlines may separate statements. .SS Comments -.PP There are two kinds of comments: .IP "1." 3 Block comments are enclosed in \f[B]/*\f[R] and \f[B]*/\f[R]. .IP "2." 3 Line comments go from \f[B]#\f[R] until, and not including, the next newline. This is a \f[B]non-portable extension\f[R]. .SS Named Expressions -.PP The following are named expressions in bc(1): .IP "1." 3 Variables: \f[B]I\f[R] .IP "2." 3 Array Elements: \f[B]I[E]\f[R] .IP "3." 3 \f[B]ibase\f[R] .IP "4." 3 \f[B]obase\f[R] .IP "5." 3 \f[B]scale\f[R] .IP "6." 3 \f[B]seed\f[R] .IP "7." 3 \f[B]last\f[R] or a single dot (\f[B].\f[R]) .PP Numbers 6 and 7 are \f[B]non-portable extensions\f[R]. .PP The meaning of \f[B]seed\f[R] is dependent on the current pseudo-random number generator but is guaranteed to not change except for new major versions. .PP The \f[I]scale\f[R] and sign of the value may be significant. .PP If a previously used \f[B]seed\f[R] value is assigned to \f[B]seed\f[R] and used again, the pseudo-random number generator is guaranteed to produce the same sequence of pseudo-random numbers as it did when the \f[B]seed\f[R] value was previously used. .PP The exact value assigned to \f[B]seed\f[R] is not guaranteed to be returned if \f[B]seed\f[R] is queried again immediately. However, if \f[B]seed\f[R] \f[I]does\f[R] return a different value, both values, when assigned to \f[B]seed\f[R], are guaranteed to produce the same sequence of pseudo-random numbers. This means that certain values assigned to \f[B]seed\f[R] will \f[I]not\f[R] produce unique sequences of pseudo-random numbers. The value of \f[B]seed\f[R] will change after any use of the \f[B]rand()\f[R] and \f[B]irand(E)\f[R] operands (see the \f[I]Operands\f[R] subsection below), except if the parameter passed to \f[B]irand(E)\f[R] is \f[B]0\f[R], \f[B]1\f[R], or negative. .PP There is no limit to the length (number of significant decimal digits) or \f[I]scale\f[R] of the value that can be assigned to \f[B]seed\f[R]. .PP Variables and arrays do not interfere; users can have arrays named the same as variables. This also applies to functions (see the \f[B]FUNCTIONS\f[R] section), so a user can have a variable, array, and function that all have the same name, and they will not shadow each other, whether inside of functions or not. .PP Named expressions are required as the operand of \f[B]increment\f[R]/\f[B]decrement\f[R] operators and as the left side of \f[B]assignment\f[R] operators (see the \f[I]Operators\f[R] subsection). .SS Operands -.PP The following are valid operands in bc(1): .IP " 1." 4 Numbers (see the \f[I]Numbers\f[R] subsection below). .IP " 2." 4 Array indices (\f[B]I[E]\f[R]). .IP " 3." 4 \f[B](E)\f[R]: The value of \f[B]E\f[R] (used to change precedence). .IP " 4." 4 \f[B]sqrt(E)\f[R]: The square root of \f[B]E\f[R]. \f[B]E\f[R] must be non-negative. .IP " 5." 4 \f[B]length(E)\f[R]: The number of significant decimal digits in \f[B]E\f[R]. Returns \f[B]1\f[R] for \f[B]0\f[R] with no decimal places. If given a string, the length of the string is returned. Passing a string to \f[B]length(E)\f[R] is a \f[B]non-portable extension\f[R]. .IP " 6." 4 \f[B]length(I[])\f[R]: The number of elements in the array \f[B]I\f[R]. This is a \f[B]non-portable extension\f[R]. .IP " 7." 4 \f[B]scale(E)\f[R]: The \f[I]scale\f[R] of \f[B]E\f[R]. .IP " 8." 4 \f[B]abs(E)\f[R]: The absolute value of \f[B]E\f[R]. This is a \f[B]non-portable extension\f[R]. .IP " 9." 4 \f[B]is_number(E)\f[R]: \f[B]1\f[R] if the given argument is a number, \f[B]0\f[R] if it is a string. This is a \f[B]non-portable extension\f[R]. .IP "10." 4 \f[B]is_string(E)\f[R]: \f[B]1\f[R] if the given argument is a string, \f[B]0\f[R] if it is a number. This is a \f[B]non-portable extension\f[R]. .IP "11." 4 \f[B]modexp(E, E, E)\f[R]: Modular exponentiation, where the first expression is the base, the second is the exponent, and the third is the modulus. All three values must be integers. The second argument must be non-negative. The third argument must be non-zero. This is a \f[B]non-portable extension\f[R]. .IP "12." 4 \f[B]divmod(E, E, I[])\f[R]: Division and modulus in one operation. This is for optimization. The first expression is the dividend, and the second is the divisor, which must be non-zero. The return value is the quotient, and the modulus is stored in index \f[B]0\f[R] of the provided array (the last argument). This is a \f[B]non-portable extension\f[R]. .IP "13." 4 \f[B]asciify(E)\f[R]: If \f[B]E\f[R] is a string, returns a string that is the first letter of its argument. If it is a number, calculates the number mod \f[B]256\f[R] and returns that number as a one-character string. This is a \f[B]non-portable extension\f[R]. .IP "14." 4 \f[B]asciify(I[])\f[R]: A string that is made up of the characters that would result from running \f[B]asciify(E)\f[R] on each element of the array identified by the argument. This allows creating multi-character strings and storing them. This is a \f[B]non-portable extension\f[R]. .IP "15." 4 \f[B]I()\f[R], \f[B]I(E)\f[R], \f[B]I(E, E)\f[R], and so on, where \f[B]I\f[R] is an identifier for a non-\f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). The \f[B]E\f[R] argument(s) may also be arrays of the form \f[B]I[]\f[R], which will automatically be turned into array references (see the \f[I]Array References\f[R] subsection of the \f[B]FUNCTIONS\f[R] section) if the corresponding parameter in the function definition is an array reference. .IP "16." 4 \f[B]read()\f[R]: Reads a line from \f[B]stdin\f[R] and uses that as an expression. The result of that expression is the result of the \f[B]read()\f[R] operand. This is a \f[B]non-portable extension\f[R]. .IP "17." 4 \f[B]maxibase()\f[R]: The max allowable \f[B]ibase\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "18." 4 \f[B]maxobase()\f[R]: The max allowable \f[B]obase\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "19." 4 \f[B]maxscale()\f[R]: The max allowable \f[B]scale\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "20." 4 \f[B]line_length()\f[R]: The line length set with \f[B]BC_LINE_LENGTH\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). This is a \f[B]non-portable extension\f[R]. .IP "21." 4 \f[B]global_stacks()\f[R]: \f[B]0\f[R] if global stacks are not enabled with the \f[B]-g\f[R] or \f[B]--global-stacks\f[R] options, non-zero otherwise. See the \f[B]OPTIONS\f[R] section. This is a \f[B]non-portable extension\f[R]. .IP "22." 4 \f[B]leading_zero()\f[R]: \f[B]0\f[R] if leading zeroes are not enabled with the \f[B]-z\f[R] or \f[B]\[en]leading-zeroes\f[R] options, non-zero otherwise. See the \f[B]OPTIONS\f[R] section. This is a \f[B]non-portable extension\f[R]. .IP "23." 4 \f[B]rand()\f[R]: A pseudo-random integer between \f[B]0\f[R] (inclusive) and \f[B]BC_RAND_MAX\f[R] (inclusive). Using this operand will change the value of \f[B]seed\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "24." 4 \f[B]irand(E)\f[R]: A pseudo-random integer between \f[B]0\f[R] (inclusive) and the value of \f[B]E\f[R] (exclusive). If \f[B]E\f[R] is negative or is a non-integer (\f[B]E\f[R]\[cq]s \f[I]scale\f[R] is not \f[B]0\f[R]), an error is raised, and bc(1) resets (see the \f[B]RESET\f[R] section) while \f[B]seed\f[R] remains unchanged. If \f[B]E\f[R] is larger than \f[B]BC_RAND_MAX\f[R], the higher bound is honored by generating several pseudo-random integers, multiplying them by appropriate powers of \f[B]BC_RAND_MAX+1\f[R], and adding them together. Thus, the size of integer that can be generated with this operand is unbounded. Using this operand will change the value of \f[B]seed\f[R], unless the value of \f[B]E\f[R] is \f[B]0\f[R] or \f[B]1\f[R]. In that case, \f[B]0\f[R] is returned, and \f[B]seed\f[R] is \f[I]not\f[R] changed. This is a \f[B]non-portable extension\f[R]. .IP "25." 4 \f[B]maxrand()\f[R]: The max integer returned by \f[B]rand()\f[R]. This is a \f[B]non-portable extension\f[R]. .PP The integers generated by \f[B]rand()\f[R] and \f[B]irand(E)\f[R] are guaranteed to be as unbiased as possible, subject to the limitations of the pseudo-random number generator. .PP \f[B]Note\f[R]: The values returned by the pseudo-random number generator with \f[B]rand()\f[R] and \f[B]irand(E)\f[R] are guaranteed to \f[I]NOT\f[R] be cryptographically secure. This is a consequence of using a seeded pseudo-random number generator. However, they \f[I]are\f[R] guaranteed to be reproducible with identical \f[B]seed\f[R] values. This means that the pseudo-random values from bc(1) should only be used where a reproducible stream of pseudo-random numbers is \f[I]ESSENTIAL\f[R]. In any other case, use a non-seeded pseudo-random number generator. .SS Numbers -.PP Numbers are strings made up of digits, uppercase letters, and at most \f[B]1\f[R] period for a radix. Numbers can have up to \f[B]BC_NUM_MAX\f[R] digits. Uppercase letters are equal to \f[B]9\f[R] plus their position in the alphabet, starting from \f[B]1\f[R] (i.e., \f[B]A\f[R] equals \f[B]10\f[R], or \f[B]9+1\f[R]). .PP If a digit or letter makes no sense with the current value of \f[B]ibase\f[R] (i.e., they are greater than or equal to the current value of \f[B]ibase\f[R]), then the behavior depends on the existence of the \f[B]-c\f[R]/\f[B]--digit-clamp\f[R] or \f[B]-C\f[R]/\f[B]--no-digit-clamp\f[R] options (see the \f[B]OPTIONS\f[R] section), the existence and setting of the \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or the default, which can be queried with the \f[B]-h\f[R]/\f[B]--help\f[R] option. .PP If clamping is off, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are not changed. Instead, their given value is multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*A+3\[ha]0*B\f[R], which is \f[B]3\f[R] times \f[B]10\f[R] plus \f[B]11\f[R], or \f[B]41\f[R]. .PP If clamping is on, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are set to the value of the highest valid digit in \f[B]ibase\f[R] before being multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*2+3\[ha]0*2\f[R], which is \f[B]3\f[R] times \f[B]2\f[R] plus \f[B]2\f[R], or \f[B]8\f[R]. .PP There is one exception to clamping: single-character numbers (i.e., \f[B]A\f[R] alone). Such numbers are never clamped and always take the value they would have in the highest possible \f[B]ibase\f[R]. This means that \f[B]A\f[R] alone always equals decimal \f[B]10\f[R] and \f[B]Z\f[R] alone always equals decimal \f[B]35\f[R]. This behavior is mandated by the standard (see the STANDARDS section) and is meant to provide an easy way to set the current \f[B]ibase\f[R] (with the \f[B]i\f[R] command) regardless of the current value of \f[B]ibase\f[R]. .PP If clamping is on, and the clamped value of a character is needed, use a leading zero, i.e., for \f[B]A\f[R], use \f[B]0A\f[R]. .PP In addition, bc(1) accepts numbers in scientific notation. These have the form \f[B]e\f[R]. The exponent (the portion after the \f[B]e\f[R]) must be an integer. An example is \f[B]1.89237e9\f[R], which is equal to \f[B]1892370000\f[R]. Negative exponents are also allowed, so \f[B]4.2890e-3\f[R] is equal to \f[B]0.0042890\f[R]. .PP Using scientific notation is an error or warning if the \f[B]-s\f[R] or \f[B]-w\f[R], respectively, command-line options (or equivalents) are given. .PP \f[B]WARNING\f[R]: Both the number and the exponent in scientific notation are interpreted according to the current \f[B]ibase\f[R], but the number is still multiplied by \f[B]10\[ha]exponent\f[R] regardless of the current \f[B]ibase\f[R]. For example, if \f[B]ibase\f[R] is \f[B]16\f[R] and bc(1) is given the number string \f[B]FFeA\f[R], the resulting decimal number will be \f[B]2550000000000\f[R], and if bc(1) is given the number string \f[B]10e-4\f[R], the resulting decimal number will be \f[B]0.0016\f[R]. .PP Accepting input as scientific notation is a \f[B]non-portable extension\f[R]. .SS Operators -.PP The following arithmetic and logical operators can be used. They are listed in order of decreasing precedence. Operators in the same group have the same precedence. .TP \f[B]++\f[R] \f[B]--\f[R] Type: Prefix and Postfix .RS .PP Associativity: None .PP Description: \f[B]increment\f[R], \f[B]decrement\f[R] .RE .TP \f[B]-\f[R] \f[B]!\f[R] Type: Prefix .RS .PP Associativity: None .PP Description: \f[B]negation\f[R], \f[B]boolean not\f[R] .RE .TP \f[B]$\f[R] Type: Postfix .RS .PP Associativity: None .PP Description: \f[B]truncation\f[R] .RE .TP \f[B]\[at]\f[R] Type: Binary .RS .PP Associativity: Right .PP Description: \f[B]set precision\f[R] .RE .TP \f[B]\[ha]\f[R] Type: Binary .RS .PP Associativity: Right .PP Description: \f[B]power\f[R] .RE .TP \f[B]*\f[R] \f[B]/\f[R] \f[B]%\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]multiply\f[R], \f[B]divide\f[R], \f[B]modulus\f[R] .RE .TP \f[B]+\f[R] \f[B]-\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]add\f[R], \f[B]subtract\f[R] .RE .TP \f[B]<<\f[R] \f[B]>>\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]shift left\f[R], \f[B]shift right\f[R] .RE .TP \f[B]=\f[R] \f[B]<<=\f[R] \f[B]>>=\f[R] \f[B]+=\f[R] \f[B]-=\f[R] \f[B]*=\f[R] \f[B]/=\f[R] \f[B]%=\f[R] \f[B]\[ha]=\f[R] \f[B]\[at]=\f[R] Type: Binary .RS .PP Associativity: Right .PP Description: \f[B]assignment\f[R] .RE .TP \f[B]==\f[R] \f[B]<=\f[R] \f[B]>=\f[R] \f[B]!=\f[R] \f[B]<\f[R] \f[B]>\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]relational\f[R] .RE .TP \f[B]&&\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]boolean and\f[R] .RE .TP \f[B]||\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]boolean or\f[R] .RE .PP The operators will be described in more detail below. .TP \f[B]++\f[R] \f[B]--\f[R] The prefix and postfix \f[B]increment\f[R] and \f[B]decrement\f[R] -operators behave exactly like they would in C. -They require a named expression (see the \f[I]Named Expressions\f[R] -subsection) as an operand. +operators behave exactly like they would in C. They require a named +expression (see the \f[I]Named Expressions\f[R] subsection) as an +operand. .RS .PP The prefix versions of these operators are more efficient; use them where possible. .RE .TP \f[B]-\f[R] The \f[B]negation\f[R] operator returns \f[B]0\f[R] if a user attempts to negate any expression with the value \f[B]0\f[R]. Otherwise, a copy of the expression with its sign flipped is returned. .TP \f[B]!\f[R] The \f[B]boolean not\f[R] operator returns \f[B]1\f[R] if the expression is \f[B]0\f[R], or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]$\f[R] The \f[B]truncation\f[R] operator returns a copy of the given expression with all of its \f[I]scale\f[R] removed. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[at]\f[R] The \f[B]set precision\f[R] operator takes two expressions and returns a copy of the first with its \f[I]scale\f[R] equal to the value of the second expression. That could either mean that the number is returned without change (if the \f[I]scale\f[R] of the first expression matches the value of the second expression), extended (if it is less), or truncated (if it is more). .RS .PP The second expression must be an integer (no \f[I]scale\f[R]) and non-negative. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[ha]\f[R] The \f[B]power\f[R] operator (not the \f[B]exclusive or\f[R] operator, as it would be in C) takes two expressions and raises the first to the power of the value of the second. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The second expression must be an integer (no \f[I]scale\f[R]), and if it is negative, the first value must be non-zero. .RE .TP \f[B]*\f[R] The \f[B]multiply\f[R] operator takes two expressions, multiplies them, and returns the product. If \f[B]a\f[R] is the \f[I]scale\f[R] of the first expression and \f[B]b\f[R] is the \f[I]scale\f[R] of the second expression, the \f[I]scale\f[R] of the result is equal to \f[B]min(a+b,max(scale,a,b))\f[R] where \f[B]min()\f[R] and \f[B]max()\f[R] return the obvious values. .TP \f[B]/\f[R] The \f[B]divide\f[R] operator takes two expressions, divides them, and returns the quotient. The \f[I]scale\f[R] of the result shall be the value of \f[B]scale\f[R]. .RS .PP The second expression must be non-zero. .RE .TP \f[B]%\f[R] The \f[B]modulus\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and evaluates them by 1) Computing \f[B]a/b\f[R] to current \f[B]scale\f[R] and 2) Using the result of step 1 to calculate \f[B]a-(a/b)*b\f[R] to \f[I]scale\f[R] \f[B]max(scale+scale(b),scale(a))\f[R]. .RS .PP The second expression must be non-zero. .RE .TP \f[B]+\f[R] The \f[B]add\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and returns the sum, with a \f[I]scale\f[R] equal to the max of the \f[I]scale\f[R]s of \f[B]a\f[R] and \f[B]b\f[R]. .TP \f[B]-\f[R] The \f[B]subtract\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and returns the difference, with a \f[I]scale\f[R] equal to the max of the \f[I]scale\f[R]s of \f[B]a\f[R] and \f[B]b\f[R]. .TP \f[B]<<\f[R] The \f[B]left shift\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and returns a copy of the value of \f[B]a\f[R] with its decimal point moved \f[B]b\f[R] places to the right. .RS .PP The second expression must be an integer (no \f[I]scale\f[R]) and non-negative. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]>>\f[R] The \f[B]right shift\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and returns a copy of the value of \f[B]a\f[R] with its decimal point moved \f[B]b\f[R] places to the left. .RS .PP The second expression must be an integer (no \f[I]scale\f[R]) and non-negative. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]=\f[R] \f[B]<<=\f[R] \f[B]>>=\f[R] \f[B]+=\f[R] \f[B]-=\f[R] \f[B]*=\f[R] \f[B]/=\f[R] \f[B]%=\f[R] \f[B]\[ha]=\f[R] \f[B]\[at]=\f[R] The \f[B]assignment\f[R] operators take two expressions, \f[B]a\f[R] and \f[B]b\f[R] where \f[B]a\f[R] is a named expression (see the \f[I]Named Expressions\f[R] subsection). .RS .PP For \f[B]=\f[R], \f[B]b\f[R] is copied and the result is assigned to \f[B]a\f[R]. For all others, \f[B]a\f[R] and \f[B]b\f[R] are applied as operands to the corresponding arithmetic operator and the result is assigned to \f[B]a\f[R]. .PP The \f[B]assignment\f[R] operators that correspond to operators that are extensions are themselves \f[B]non-portable extensions\f[R]. .RE .TP \f[B]==\f[R] \f[B]<=\f[R] \f[B]>=\f[R] \f[B]!=\f[R] \f[B]<\f[R] \f[B]>\f[R] The \f[B]relational\f[R] operators compare two expressions, \f[B]a\f[R] and \f[B]b\f[R], and if the relation holds, according to C language semantics, the result is \f[B]1\f[R]. Otherwise, it is \f[B]0\f[R]. .RS .PP Note that unlike in C, these operators have a lower precedence than the \f[B]assignment\f[R] operators, which means that \f[B]a=b>c\f[R] is interpreted as \f[B](a=b)>c\f[R]. .PP Also, unlike the standard (see the \f[B]STANDARDS\f[R] section) requires, these operators can appear anywhere any other expressions can be used. This allowance is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]&&\f[R] The \f[B]boolean and\f[R] operator takes two expressions and returns \f[B]1\f[R] if both expressions are non-zero, \f[B]0\f[R] otherwise. .RS .PP This is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]||\f[R] The \f[B]boolean or\f[R] operator takes two expressions and returns \f[B]1\f[R] if one of the expressions is non-zero, \f[B]0\f[R] otherwise. .RS .PP This is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Statements -.PP The following items are statements: .IP " 1." 4 \f[B]E\f[R] .IP " 2." 4 \f[B]{\f[R] \f[B]S\f[R] \f[B];\f[R] \&... \f[B];\f[R] \f[B]S\f[R] \f[B]}\f[R] .IP " 3." 4 \f[B]if\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] .IP " 4." 4 \f[B]if\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] \f[B]else\f[R] \f[B]S\f[R] .IP " 5." 4 \f[B]while\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] .IP " 6." 4 \f[B]for\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B];\f[R] \f[B]E\f[R] \f[B];\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] .IP " 7." 4 An empty statement .IP " 8." 4 \f[B]break\f[R] .IP " 9." 4 \f[B]continue\f[R] .IP "10." 4 \f[B]quit\f[R] .IP "11." 4 \f[B]halt\f[R] .IP "12." 4 \f[B]limits\f[R] .IP "13." 4 A string of characters, enclosed in double quotes .IP "14." 4 \f[B]print\f[R] \f[B]E\f[R] \f[B],\f[R] \&... \f[B],\f[R] \f[B]E\f[R] .IP "15." 4 \f[B]stream\f[R] \f[B]E\f[R] \f[B],\f[R] \&... \f[B],\f[R] \f[B]E\f[R] .IP "16." 4 \f[B]I()\f[R], \f[B]I(E)\f[R], \f[B]I(E, E)\f[R], and so on, where \f[B]I\f[R] is an identifier for a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). The \f[B]E\f[R] argument(s) may also be arrays of the form \f[B]I[]\f[R], which will automatically be turned into array references (see the \f[I]Array References\f[R] subsection of the \f[B]FUNCTIONS\f[R] section) if the corresponding parameter in the function definition is an array reference. .PP Numbers 4, 9, 11, 12, 14, 15, and 16 are \f[B]non-portable extensions\f[R]. .PP Also, as a \f[B]non-portable extension\f[R], any or all of the expressions in the header of a for loop may be omitted. If the condition (second expression) is omitted, it is assumed to be a constant \f[B]1\f[R]. .PP The \f[B]break\f[R] statement causes a loop to stop iterating and resume execution immediately following a loop. This is only allowed in loops. .PP The \f[B]continue\f[R] statement causes a loop iteration to stop early and returns to the start of the loop, including testing the loop condition. This is only allowed in loops. .PP The \f[B]if\f[R] \f[B]else\f[R] statement does the same thing as in C. .PP The \f[B]quit\f[R] statement causes bc(1) to quit, even if it is on a branch that will not be executed (it is a compile-time command). .PP \f[B]Warning\f[R]: The behavior of this bc(1) on \f[B]quit\f[R] is slightly different from other bc(1) implementations. Other bc(1) implementations will exit as soon as they finish parsing the line that a \f[B]quit\f[R] command is on. This bc(1) will execute any completed and executable statements that occur before the \f[B]quit\f[R] statement before exiting. .PP In other words, for the bc(1) code below: .IP -.nf -\f[C] +.EX for (i = 0; i < 3; ++i) i; quit -\f[R] -.fi +.EE .PP Other bc(1) implementations will print nothing, and this bc(1) will print \f[B]0\f[R], \f[B]1\f[R], and \f[B]2\f[R] on successive lines before exiting. .PP The \f[B]halt\f[R] statement causes bc(1) to quit, if it is executed. (Unlike \f[B]quit\f[R] if it is on a branch of an \f[B]if\f[R] statement that is not executed, bc(1) does not quit.) .PP The \f[B]limits\f[R] statement prints the limits that this bc(1) is subject to. This is like the \f[B]quit\f[R] statement in that it is a compile-time command. .PP An expression by itself is evaluated and printed, followed by a newline. .PP Both scientific notation and engineering notation are available for printing the results of expressions. Scientific notation is activated by assigning \f[B]0\f[R] to \f[B]obase\f[R], and engineering notation is activated by assigning \f[B]1\f[R] to \f[B]obase\f[R]. To deactivate them, just assign a different value to \f[B]obase\f[R]. .PP Scientific notation and engineering notation are disabled if bc(1) is run with either the \f[B]-s\f[R] or \f[B]-w\f[R] command-line options (or equivalents). .PP Printing numbers in scientific notation and/or engineering notation is a \f[B]non-portable extension\f[R]. .SS Strings -.PP If strings appear as a statement by themselves, they are printed without a trailing newline. .PP In addition to appearing as a lone statement by themselves, strings can be assigned to variables and array elements. They can also be passed to functions in variable parameters. .PP If any statement that expects a string is given a variable that had a string assigned to it, the statement acts as though it had received a string. .PP If any math operation is attempted on a string or a variable or array element that has been assigned a string, an error is raised, and bc(1) resets (see the \f[B]RESET\f[R] section). .PP Assigning strings to variables and array elements and passing them to functions are \f[B]non-portable extensions\f[R]. .SS Print Statement -.PP The \[lq]expressions\[rq] in a \f[B]print\f[R] statement may also be strings. If they are, there are backslash escape sequences that are interpreted specially. What those sequences are, and what they cause to be printed, are shown below: .PP \f[B]\[rs]a\f[R]: \f[B]\[rs]a\f[R] .PP \f[B]\[rs]b\f[R]: \f[B]\[rs]b\f[R] .PP \f[B]\[rs]\[rs]\f[R]: \f[B]\[rs]\f[R] .PP \f[B]\[rs]e\f[R]: \f[B]\[rs]\f[R] .PP \f[B]\[rs]f\f[R]: \f[B]\[rs]f\f[R] .PP \f[B]\[rs]n\f[R]: \f[B]\[rs]n\f[R] .PP \f[B]\[rs]q\f[R]: \f[B]\[lq]\f[R] .PP \f[B]\[rs]r\f[R]: \f[B]\[rs]r\f[R] .PP \f[B]\[rs]t\f[R]: \f[B]\[rs]t\f[R] .PP Any other character following a backslash causes the backslash and character to be printed as-is. .PP Any non-string expression in a print statement shall be assigned to \f[B]last\f[R], like any other expression that is printed. .SS Stream Statement -.PP The expressions in a \f[B]stream\f[R] statement may also be strings. .PP If a \f[B]stream\f[R] statement is given a string, it prints the string as though the string had appeared as its own statement. In other words, the \f[B]stream\f[R] statement prints strings normally, without a newline. .PP If a \f[B]stream\f[R] statement is given a number, a copy of it is truncated and its absolute value is calculated. The result is then printed as though \f[B]obase\f[R] is \f[B]256\f[R] and each digit is interpreted as an 8-bit ASCII character, making it a byte stream. .SS Order of Evaluation -.PP All expressions in a statment are evaluated left to right, except as necessary to maintain order of operations. This means, for example, assuming that \f[B]i\f[R] is equal to \f[B]0\f[R], in the expression .IP -.nf -\f[C] +.EX a[i++] = i++ -\f[R] -.fi +.EE .PP the first (or 0th) element of \f[B]a\f[R] is set to \f[B]1\f[R], and \f[B]i\f[R] is equal to \f[B]2\f[R] at the end of the expression. .PP This includes function arguments. Thus, assuming \f[B]i\f[R] is equal to \f[B]0\f[R], this means that in the expression .IP -.nf -\f[C] +.EX x(i++, i++) -\f[R] -.fi +.EE .PP the first argument passed to \f[B]x()\f[R] is \f[B]0\f[R], and the second argument is \f[B]1\f[R], while \f[B]i\f[R] is equal to \f[B]2\f[R] before the function starts executing. .SH FUNCTIONS -.PP Function definitions are as follows: .IP -.nf -\f[C] +.EX define I(I,...,I){ auto I,...,I S;...;S return(E) } -\f[R] -.fi +.EE .PP Any \f[B]I\f[R] in the parameter list or \f[B]auto\f[R] list may be replaced with \f[B]I[]\f[R] to make a parameter or \f[B]auto\f[R] var an array, and any \f[B]I\f[R] in the parameter list may be replaced with \f[B]*I[]\f[R] to make a parameter an array reference. Callers of functions that take array references should not put an asterisk in the call; they must be called with just \f[B]I[]\f[R] like normal array parameters and will be automatically converted into references. .PP As a \f[B]non-portable extension\f[R], the opening brace of a \f[B]define\f[R] statement may appear on the next line. .PP As a \f[B]non-portable extension\f[R], the return statement may also be in one of the following forms: .IP "1." 3 \f[B]return\f[R] .IP "2." 3 \f[B]return\f[R] \f[B](\f[R] \f[B])\f[R] .IP "3." 3 \f[B]return\f[R] \f[B]E\f[R] .PP The first two, or not specifying a \f[B]return\f[R] statement, is equivalent to \f[B]return (0)\f[R], unless the function is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection below). .SS Void Functions -.PP Functions can also be \f[B]void\f[R] functions, defined as follows: .IP -.nf -\f[C] +.EX define void I(I,...,I){ auto I,...,I S;...;S return } -\f[R] -.fi +.EE .PP They can only be used as standalone expressions, where such an expression would be printed alone, except in a print statement. .PP Void functions can only use the first two \f[B]return\f[R] statements listed above. They can also omit the return statement entirely. .PP The word \[lq]void\[rq] is not treated as a keyword; it is still possible to have variables, arrays, and functions named \f[B]void\f[R]. The word \[lq]void\[rq] is only treated specially right after the \f[B]define\f[R] keyword. .PP This is a \f[B]non-portable extension\f[R]. .SS Array References -.PP For any array in the parameter list, if the array is declared in the form .IP -.nf -\f[C] +.EX *I[] -\f[R] -.fi +.EE .PP it is a \f[B]reference\f[R]. Any changes to the array in the function are reflected, when the function returns, to the array that was passed in. .PP Other than this, all function arguments are passed by value. .PP This is a \f[B]non-portable extension\f[R]. .SH LIBRARY -.PP All of the functions below, including the functions in the extended math library (see the \f[I]Extended Library\f[R] subsection below), are available when the \f[B]-l\f[R] or \f[B]--mathlib\f[R] command-line flags are given, except that the extended math library is not available when the \f[B]-s\f[R] option, the \f[B]-w\f[R] option, or equivalents are given. .SS Standard Library -.PP The standard (see the \f[B]STANDARDS\f[R] section) defines the following functions for the math library: .TP \f[B]s(x)\f[R] Returns the sine of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]c(x)\f[R] Returns the cosine of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]a(x)\f[R] Returns the arctangent of \f[B]x\f[R], in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]l(x)\f[R] Returns the natural logarithm of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]e(x)\f[R] Returns the mathematical constant \f[B]e\f[R] raised to the power of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]j(x, n)\f[R] Returns the bessel integer order \f[B]n\f[R] (truncated) of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .SS Extended Library -.PP The extended library is \f[I]not\f[R] loaded when the \f[B]-s\f[R]/\f[B]--standard\f[R] or \f[B]-w\f[R]/\f[B]--warn\f[R] options are given since they are not part of the library defined by the standard (see the \f[B]STANDARDS\f[R] section). .PP The extended library is a \f[B]non-portable extension\f[R]. .TP \f[B]p(x, y)\f[R] Calculates \f[B]x\f[R] to the power of \f[B]y\f[R], even if \f[B]y\f[R] is not an integer, and returns the result to the current \f[B]scale\f[R]. .RS .PP It is an error if \f[B]y\f[R] is negative and \f[B]x\f[R] is \f[B]0\f[R]. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]r(x, p)\f[R] Returns \f[B]x\f[R] rounded to \f[B]p\f[R] decimal places according to the rounding mode round half away from \f[B]0\f[R] (https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero). .TP \f[B]ceil(x, p)\f[R] Returns \f[B]x\f[R] rounded to \f[B]p\f[R] decimal places according to the rounding mode round away from \f[B]0\f[R] (https://en.wikipedia.org/wiki/Rounding#Rounding_away_from_zero). .TP \f[B]f(x)\f[R] Returns the factorial of the truncated absolute value of \f[B]x\f[R]. .TP \f[B]max(a, b)\f[R] Returns \f[B]a\f[R] if \f[B]a\f[R] is greater than \f[B]b\f[R]; otherwise, returns \f[B]b\f[R]. .TP \f[B]min(a, b)\f[R] Returns \f[B]a\f[R] if \f[B]a\f[R] is less than \f[B]b\f[R]; otherwise, returns \f[B]b\f[R]. .TP \f[B]perm(n, k)\f[R] Returns the permutation of the truncated absolute value of \f[B]n\f[R] of the truncated absolute value of \f[B]k\f[R], if \f[B]k <= n\f[R]. If not, it returns \f[B]0\f[R]. .TP \f[B]comb(n, k)\f[R] Returns the combination of the truncated absolute value of \f[B]n\f[R] of the truncated absolute value of \f[B]k\f[R], if \f[B]k <= n\f[R]. If not, it returns \f[B]0\f[R]. .TP \f[B]fib(n)\f[R] Returns the Fibonacci number of the truncated absolute value of \f[B]n\f[R]. .TP \f[B]l2(x)\f[R] Returns the logarithm base \f[B]2\f[R] of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]l10(x)\f[R] Returns the logarithm base \f[B]10\f[R] of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]log(x, b)\f[R] Returns the logarithm base \f[B]b\f[R] of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]cbrt(x)\f[R] Returns the cube root of \f[B]x\f[R]. .TP \f[B]root(x, n)\f[R] Calculates the truncated value of \f[B]n\f[R], \f[B]r\f[R], and returns the \f[B]r\f[R]th root of \f[B]x\f[R] to the current \f[B]scale\f[R]. .RS .PP If \f[B]r\f[R] is \f[B]0\f[R] or negative, this raises an error and causes bc(1) to reset (see the \f[B]RESET\f[R] section). It also raises an error and causes bc(1) to reset if \f[B]r\f[R] is even and \f[B]x\f[R] is negative. .RE .TP \f[B]gcd(a, b)\f[R] Returns the greatest common divisor (factor) of the truncated absolute value of \f[B]a\f[R] and the truncated absolute value of \f[B]b\f[R]. .TP \f[B]lcm(a, b)\f[R] Returns the least common multiple of the truncated absolute value of \f[B]a\f[R] and the truncated absolute value of \f[B]b\f[R]. .TP \f[B]pi(p)\f[R] Returns \f[B]pi\f[R] to \f[B]p\f[R] decimal places. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]t(x)\f[R] Returns the tangent of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]a2(y, x)\f[R] Returns the arctangent of \f[B]y/x\f[R], in radians. If both \f[B]y\f[R] and \f[B]x\f[R] are equal to \f[B]0\f[R], it raises an error and causes bc(1) to reset (see the \f[B]RESET\f[R] section). Otherwise, if \f[B]x\f[R] is greater than \f[B]0\f[R], it returns \f[B]a(y/x)\f[R]. If \f[B]x\f[R] is less than \f[B]0\f[R], and \f[B]y\f[R] is greater than or equal to \f[B]0\f[R], it returns \f[B]a(y/x)+pi\f[R]. If \f[B]x\f[R] is less than \f[B]0\f[R], and \f[B]y\f[R] is less than \f[B]0\f[R], it returns \f[B]a(y/x)-pi\f[R]. If \f[B]x\f[R] is equal to \f[B]0\f[R], and \f[B]y\f[R] is greater than \f[B]0\f[R], it returns \f[B]pi/2\f[R]. If \f[B]x\f[R] is equal to \f[B]0\f[R], and \f[B]y\f[R] is less than \f[B]0\f[R], it returns \f[B]-pi/2\f[R]. .RS .PP This function is the same as the \f[B]atan2()\f[R] function in many programming languages. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]sin(x)\f[R] Returns the sine of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is an alias of \f[B]s(x)\f[R]. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]cos(x)\f[R] Returns the cosine of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is an alias of \f[B]c(x)\f[R]. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]tan(x)\f[R] Returns the tangent of \f[B]x\f[R], which is assumed to be in radians. .RS .PP If \f[B]x\f[R] is equal to \f[B]1\f[R] or \f[B]-1\f[R], this raises an error and causes bc(1) to reset (see the \f[B]RESET\f[R] section). .PP This is an alias of \f[B]t(x)\f[R]. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]atan(x)\f[R] Returns the arctangent of \f[B]x\f[R], in radians. .RS .PP This is an alias of \f[B]a(x)\f[R]. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]atan2(y, x)\f[R] Returns the arctangent of \f[B]y/x\f[R], in radians. If both \f[B]y\f[R] and \f[B]x\f[R] are equal to \f[B]0\f[R], it raises an error and causes bc(1) to reset (see the \f[B]RESET\f[R] section). Otherwise, if \f[B]x\f[R] is greater than \f[B]0\f[R], it returns \f[B]a(y/x)\f[R]. If \f[B]x\f[R] is less than \f[B]0\f[R], and \f[B]y\f[R] is greater than or equal to \f[B]0\f[R], it returns \f[B]a(y/x)+pi\f[R]. If \f[B]x\f[R] is less than \f[B]0\f[R], and \f[B]y\f[R] is less than \f[B]0\f[R], it returns \f[B]a(y/x)-pi\f[R]. If \f[B]x\f[R] is equal to \f[B]0\f[R], and \f[B]y\f[R] is greater than \f[B]0\f[R], it returns \f[B]pi/2\f[R]. If \f[B]x\f[R] is equal to \f[B]0\f[R], and \f[B]y\f[R] is less than \f[B]0\f[R], it returns \f[B]-pi/2\f[R]. .RS .PP This function is the same as the \f[B]atan2()\f[R] function in many programming languages. .PP This is an alias of \f[B]a2(y, x)\f[R]. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]r2d(x)\f[R] Converts \f[B]x\f[R] from radians to degrees and returns the result. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]d2r(x)\f[R] Converts \f[B]x\f[R] from degrees to radians and returns the result. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]frand(p)\f[R] Generates a pseudo-random integer between \f[B]0\f[R] (inclusive) and \f[B]1\f[R] (exclusive) with the number of decimal digits after the decimal point equal to the truncated absolute value of \f[B]p\f[R]. If \f[B]p\f[R] is not \f[B]0\f[R], then calling this function will change the value of \f[B]seed\f[R]. If \f[B]p\f[R] is \f[B]0\f[R], then \f[B]0\f[R] is returned, and \f[B]seed\f[R] is \f[I]not\f[R] changed. .TP \f[B]ifrand(i, p)\f[R] Generates a pseudo-random integer that is between \f[B]0\f[R] (inclusive) and the truncated absolute value of \f[B]i\f[R] (exclusive) with the number of decimal digits after the decimal point equal to the truncated absolute value of \f[B]p\f[R]. If the absolute value of \f[B]i\f[R] is greater than or equal to \f[B]2\f[R], and \f[B]p\f[R] is not \f[B]0\f[R], then calling this function will change the value of \f[B]seed\f[R]; otherwise, \f[B]0\f[R] is returned, and \f[B]seed\f[R] is not changed. .TP \f[B]i2rand(a, b)\f[R] Takes the truncated value of \f[B]a\f[R] and \f[B]b\f[R] and uses them as inclusive bounds to enerate a pseudo-random integer. If the difference of the truncated values of \f[B]a\f[R] and \f[B]b\f[R] is \f[B]0\f[R], then the truncated value is returned, and \f[B]seed\f[R] is \f[I]not\f[R] changed. Otherwise, this function will change the value of \f[B]seed\f[R]. .TP \f[B]srand(x)\f[R] Returns \f[B]x\f[R] with its sign flipped with probability \f[B]0.5\f[R]. In other words, it randomizes the sign of \f[B]x\f[R]. .TP \f[B]brand()\f[R] Returns a random boolean value (either \f[B]0\f[R] or \f[B]1\f[R]). .TP \f[B]band(a, b)\f[R] Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R] and calculates and returns the result of the bitwise \f[B]and\f[R] operation between them. .RS .PP If you want to use signed two\[cq]s complement arguments, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bor(a, b)\f[R] Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R] and calculates and returns the result of the bitwise \f[B]or\f[R] operation between them. .RS .PP If you want to use signed two\[cq]s complement arguments, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bxor(a, b)\f[R] Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R] and calculates and returns the result of the bitwise \f[B]xor\f[R] operation between them. .RS .PP If you want to use signed two\[cq]s complement arguments, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bshl(a, b)\f[R] Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R] and calculates and returns the result of \f[B]a\f[R] bit-shifted left by \f[B]b\f[R] places. .RS .PP If you want to use signed two\[cq]s complement arguments, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bshr(a, b)\f[R] Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R] and calculates and returns the truncated result of \f[B]a\f[R] bit-shifted right by \f[B]b\f[R] places. .RS .PP If you want to use signed two\[cq]s complement arguments, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bnotn(x, n)\f[R] Takes the truncated absolute value of \f[B]x\f[R] and does a bitwise not as though it has the same number of bytes as the truncated absolute value of \f[B]n\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bnot8(x)\f[R] Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as -though it has \f[B]8\f[R] binary digits (1 unsigned byte). +though it has \f[B]8\f[R] binary digits (\f[B]1\f[R] unsigned byte). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bnot16(x)\f[R] Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as -though it has \f[B]16\f[R] binary digits (2 unsigned bytes). +though it has \f[B]16\f[R] binary digits (\f[B]2\f[R] unsigned bytes). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bnot32(x)\f[R] Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as -though it has \f[B]32\f[R] binary digits (4 unsigned bytes). +though it has \f[B]32\f[R] binary digits (\f[B]4\f[R] unsigned bytes). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bnot64(x)\f[R] Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as -though it has \f[B]64\f[R] binary digits (8 unsigned bytes). +though it has \f[B]64\f[R] binary digits (\f[B]8\f[R] unsigned bytes). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bnot(x)\f[R] Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as though it has the minimum number of power of two unsigned bytes. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brevn(x, n)\f[R] Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as though it has the same number of 8-bit bytes as the truncated absolute value of \f[B]n\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brev8(x)\f[R] Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as -though it has 8 binary digits (1 unsigned byte). +though it has 8 binary digits (\f[B]1\f[R] unsigned byte). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brev16(x)\f[R] Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as -though it has 16 binary digits (2 unsigned bytes). +though it has 16 binary digits (\f[B]2\f[R] unsigned bytes). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brev32(x)\f[R] Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as -though it has 32 binary digits (4 unsigned bytes). +though it has 32 binary digits (\f[B]4\f[R] unsigned bytes). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brev64(x)\f[R] Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as -though it has 64 binary digits (8 unsigned bytes). +though it has 64 binary digits (\f[B]8\f[R] unsigned bytes). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brev(x)\f[R] Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as though it has the minimum number of power of two unsigned bytes. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]broln(x, p, n)\f[R] Does a left bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has the same number of unsigned 8-bit bytes as the truncated absolute value of \f[B]n\f[R], by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by the \f[B]2\f[R] to the power of the number of binary digits in \f[B]n\f[R] 8-bit bytes. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brol8(x, p)\f[R] Does a left bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has \f[B]8\f[R] binary digits (\f[B]1\f[R] unsigned byte), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]8\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brol16(x, p)\f[R] Does a left bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has \f[B]16\f[R] binary digits (\f[B]2\f[R] unsigned bytes), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]16\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brol32(x, p)\f[R] Does a left bitwise rotatation of the truncated absolute value of -\f[B]x\f[R], as though it has \f[B]32\f[R] binary digits (\f[B]2\f[R] +\f[B]x\f[R], as though it has \f[B]32\f[R] binary digits (\f[B]4\f[R] unsigned bytes), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]32\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brol64(x, p)\f[R] Does a left bitwise rotatation of the truncated absolute value of -\f[B]x\f[R], as though it has \f[B]64\f[R] binary digits (\f[B]2\f[R] +\f[B]x\f[R], as though it has \f[B]64\f[R] binary digits (\f[B]8\f[R] unsigned bytes), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]64\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brol(x, p)\f[R] Does a left bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has the minimum number of power of two unsigned 8-bit bytes, by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by 2 to the power of the number of binary digits in the minimum number of 8-bit bytes. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brorn(x, p, n)\f[R] Does a right bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has the same number of unsigned 8-bit bytes as the truncated absolute value of \f[B]n\f[R], by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by the \f[B]2\f[R] to the power of the number of binary digits in \f[B]n\f[R] 8-bit bytes. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bror8(x, p)\f[R] Does a right bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has \f[B]8\f[R] binary digits (\f[B]1\f[R] unsigned byte), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]8\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bror16(x, p)\f[R] Does a right bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has \f[B]16\f[R] binary digits (\f[B]2\f[R] unsigned bytes), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]16\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bror32(x, p)\f[R] Does a right bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has \f[B]32\f[R] binary digits (\f[B]2\f[R] unsigned bytes), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]32\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bror64(x, p)\f[R] Does a right bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has \f[B]64\f[R] binary digits (\f[B]2\f[R] unsigned bytes), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]64\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bror(x, p)\f[R] Does a right bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has the minimum number of power of two unsigned 8-bit bytes, by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by 2 to the power of the number of binary digits in the minimum number of 8-bit bytes. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bmodn(x, n)\f[R] Returns the modulus of the truncated absolute value of \f[B]x\f[R] by \f[B]2\f[R] to the power of the multiplication of the truncated absolute value of \f[B]n\f[R] and \f[B]8\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bmod8(x, n)\f[R] Returns the modulus of the truncated absolute value of \f[B]x\f[R] by \f[B]2\f[R] to the power of \f[B]8\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bmod16(x, n)\f[R] Returns the modulus of the truncated absolute value of \f[B]x\f[R] by \f[B]2\f[R] to the power of \f[B]16\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bmod32(x, n)\f[R] Returns the modulus of the truncated absolute value of \f[B]x\f[R] by \f[B]2\f[R] to the power of \f[B]32\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bmod64(x, n)\f[R] Returns the modulus of the truncated absolute value of \f[B]x\f[R] by \f[B]2\f[R] to the power of \f[B]64\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bunrev(t)\f[R] Assumes \f[B]t\f[R] is a bitwise-reversed number with an extra set bit one place more significant than the real most significant bit (which was the least significant bit in the original number). This number is reversed and returned without the extra set bit. .RS .PP This function is used to implement other bitwise functions; it is not meant to be used by users, but it can be. .RE .TP \f[B]plz(x)\f[R] If \f[B]x\f[R] is not equal to \f[B]0\f[R] and greater that \f[B]-1\f[R] and less than \f[B]1\f[R], it is printed with a leading zero, regardless of the use of the \f[B]-z\f[R] option (see the \f[B]OPTIONS\f[R] section) and without a trailing newline. .RS .PP Otherwise, \f[B]x\f[R] is printed normally, without a trailing newline. .RE .TP \f[B]plznl(x)\f[R] If \f[B]x\f[R] is not equal to \f[B]0\f[R] and greater that \f[B]-1\f[R] and less than \f[B]1\f[R], it is printed with a leading zero, regardless of the use of the \f[B]-z\f[R] option (see the \f[B]OPTIONS\f[R] section) and with a trailing newline. .RS .PP Otherwise, \f[B]x\f[R] is printed normally, with a trailing newline. .RE .TP \f[B]pnlz(x)\f[R] If \f[B]x\f[R] is not equal to \f[B]0\f[R] and greater that \f[B]-1\f[R] and less than \f[B]1\f[R], it is printed without a leading zero, regardless of the use of the \f[B]-z\f[R] option (see the \f[B]OPTIONS\f[R] section) and without a trailing newline. .RS .PP Otherwise, \f[B]x\f[R] is printed normally, without a trailing newline. .RE .TP \f[B]pnlznl(x)\f[R] If \f[B]x\f[R] is not equal to \f[B]0\f[R] and greater that \f[B]-1\f[R] and less than \f[B]1\f[R], it is printed without a leading zero, regardless of the use of the \f[B]-z\f[R] option (see the \f[B]OPTIONS\f[R] section) and with a trailing newline. .RS .PP Otherwise, \f[B]x\f[R] is printed normally, with a trailing newline. .RE .TP \f[B]ubytes(x)\f[R] Returns the numbers of unsigned integer bytes required to hold the truncated absolute value of \f[B]x\f[R]. .TP \f[B]sbytes(x)\f[R] Returns the numbers of signed, two\[cq]s-complement integer bytes required to hold the truncated value of \f[B]x\f[R]. .TP \f[B]s2u(x)\f[R] Returns \f[B]x\f[R] if it is non-negative. If it \f[I]is\f[R] negative, then it calculates what \f[B]x\f[R] would be as a 2\[cq]s-complement signed integer and returns the non-negative integer that would have the same representation in binary. .TP \f[B]s2un(x,n)\f[R] Returns \f[B]x\f[R] if it is non-negative. If it \f[I]is\f[R] negative, then it calculates what \f[B]x\f[R] would be as a 2\[cq]s-complement signed integer with \f[B]n\f[R] bytes and returns the non-negative integer that would have the same representation in binary. If \f[B]x\f[R] cannot fit into \f[B]n\f[R] 2\[cq]s-complement signed bytes, it is truncated to fit. .TP \f[B]hex(x)\f[R] Outputs the hexadecimal (base \f[B]16\f[R]) representation of \f[B]x\f[R]. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]binary(x)\f[R] Outputs the binary (base \f[B]2\f[R]) representation of \f[B]x\f[R]. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]output(x, b)\f[R] Outputs the base \f[B]b\f[R] representation of \f[B]x\f[R]. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]uint(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as an unsigned integer in as few power of two bytes as possible. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer or is negative, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]int(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as a signed, two\[cq]s-complement integer in as few power of two bytes as possible. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]uintn(x, n)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as an unsigned integer in \f[B]n\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer, is negative, or cannot fit into \f[B]n\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]intn(x, n)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as a signed, two\[cq]s-complement integer in \f[B]n\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer or cannot fit into \f[B]n\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]uint8(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as an unsigned integer in \f[B]1\f[R] byte. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer, is negative, or cannot fit into \f[B]1\f[R] byte, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]int8(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as a signed, two\[cq]s-complement integer in \f[B]1\f[R] byte. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer or cannot fit into \f[B]1\f[R] byte, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]uint16(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as an unsigned integer in \f[B]2\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer, is negative, or cannot fit into \f[B]2\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]int16(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as a signed, two\[cq]s-complement integer in \f[B]2\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer or cannot fit into \f[B]2\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]uint32(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as an unsigned integer in \f[B]4\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer, is negative, or cannot fit into \f[B]4\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]int32(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as a signed, two\[cq]s-complement integer in \f[B]4\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer or cannot fit into \f[B]4\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]uint64(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as an unsigned integer in \f[B]8\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer, is negative, or cannot fit into \f[B]8\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]int64(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as a signed, two\[cq]s-complement integer in \f[B]8\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer or cannot fit into \f[B]8\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]hex_uint(x, n)\f[R] Outputs the representation of the truncated absolute value of \f[B]x\f[R] as an unsigned integer in hexadecimal using \f[B]n\f[R] bytes. Not all of the value will be output if \f[B]n\f[R] is too small. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]binary_uint(x, n)\f[R] Outputs the representation of the truncated absolute value of \f[B]x\f[R] as an unsigned integer in binary using \f[B]n\f[R] bytes. Not all of the value will be output if \f[B]n\f[R] is too small. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]output_uint(x, n)\f[R] Outputs the representation of the truncated absolute value of \f[B]x\f[R] as an unsigned integer in the current \f[B]obase\f[R] (see the \f[B]SYNTAX\f[R] section) using \f[B]n\f[R] bytes. Not all of the value will be output if \f[B]n\f[R] is too small. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]output_byte(x, i)\f[R] Outputs byte \f[B]i\f[R] of the truncated absolute value of \f[B]x\f[R], where \f[B]0\f[R] is the least significant byte and \f[B]number_of_bytes - 1\f[R] is the most significant byte. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .SS Transcendental Functions -.PP All transcendental functions can return slightly inaccurate results, up to 1 ULP (https://en.wikipedia.org/wiki/Unit_in_the_last_place). This is unavoidable, and the article at https://people.eecs.berkeley.edu/\[ti]wkahan/LOG10HAF.TXT explains why it is impossible and unnecessary to calculate exact results for the transcendental functions. .PP Because of the possible inaccuracy, I recommend that users call those functions with the precision (\f[B]scale\f[R]) set to at least 1 higher than is necessary. If exact results are \f[I]absolutely\f[R] required, users can double the precision (\f[B]scale\f[R]) and then truncate. .PP The transcendental functions in the standard math library are: .IP \[bu] 2 \f[B]s(x)\f[R] .IP \[bu] 2 \f[B]c(x)\f[R] .IP \[bu] 2 \f[B]a(x)\f[R] .IP \[bu] 2 \f[B]l(x)\f[R] .IP \[bu] 2 \f[B]e(x)\f[R] .IP \[bu] 2 \f[B]j(x, n)\f[R] .PP The transcendental functions in the extended math library are: .IP \[bu] 2 \f[B]l2(x)\f[R] .IP \[bu] 2 \f[B]l10(x)\f[R] .IP \[bu] 2 \f[B]log(x, b)\f[R] .IP \[bu] 2 \f[B]pi(p)\f[R] .IP \[bu] 2 \f[B]t(x)\f[R] .IP \[bu] 2 \f[B]a2(y, x)\f[R] .IP \[bu] 2 \f[B]sin(x)\f[R] .IP \[bu] 2 \f[B]cos(x)\f[R] .IP \[bu] 2 \f[B]tan(x)\f[R] .IP \[bu] 2 \f[B]atan(x)\f[R] .IP \[bu] 2 \f[B]atan2(y, x)\f[R] .IP \[bu] 2 \f[B]r2d(x)\f[R] .IP \[bu] 2 \f[B]d2r(x)\f[R] .SH RESET -.PP When bc(1) encounters an error or a signal that it has a non-default handler for, it resets. This means that several things happen. .PP First, any functions that are executing are stopped and popped off the stack. The behavior is not unlike that of exceptions in programming languages. Then the execution point is set so that any code waiting to execute (after all functions returned) is skipped. .PP Thus, when bc(1) resets, it skips any remaining code waiting to be executed. Then, if it is interactive mode, and the error was not a fatal error (see the \f[B]EXIT STATUS\f[R] section), it asks for more input; otherwise, it exits with the appropriate return code. .PP Note that this reset behavior is different from the GNU bc(1), which attempts to start executing the statement right after the one that caused an error. .SH PERFORMANCE -.PP Most bc(1) implementations use \f[B]char\f[R] types to calculate the value of \f[B]1\f[R] decimal digit at a time, but that can be slow. This bc(1) does something different. .PP It uses large integers to calculate more than \f[B]1\f[R] decimal digit at a time. If built in a environment where \f[B]BC_LONG_BIT\f[R] (see the \f[B]LIMITS\f[R] section) is \f[B]64\f[R], then each integer has \f[B]9\f[R] decimal digits. If built in an environment where \f[B]BC_LONG_BIT\f[R] is \f[B]32\f[R] then each integer has \f[B]4\f[R] decimal digits. This value (the number of decimal digits per large integer) is called \f[B]BC_BASE_DIGS\f[R]. .PP The actual values of \f[B]BC_LONG_BIT\f[R] and \f[B]BC_BASE_DIGS\f[R] can be queried with the \f[B]limits\f[R] statement. .PP In addition, this bc(1) uses an even larger integer for overflow checking. This integer type depends on the value of \f[B]BC_LONG_BIT\f[R], but is always at least twice as large as the integer type used to store digits. .SH LIMITS -.PP The following are the limits on bc(1): .TP \f[B]BC_LONG_BIT\f[R] The number of bits in the \f[B]long\f[R] type in the environment where bc(1) was built. This determines how many decimal digits can be stored in a single large integer (see the \f[B]PERFORMANCE\f[R] section). .TP \f[B]BC_BASE_DIGS\f[R] The number of decimal digits per large integer (see the \f[B]PERFORMANCE\f[R] section). Depends on \f[B]BC_LONG_BIT\f[R]. .TP \f[B]BC_BASE_POW\f[R] The max decimal number that each large integer can store (see \f[B]BC_BASE_DIGS\f[R]) plus \f[B]1\f[R]. Depends on \f[B]BC_BASE_DIGS\f[R]. .TP \f[B]BC_OVERFLOW_MAX\f[R] The max number that the overflow type (see the \f[B]PERFORMANCE\f[R] section) can hold. Depends on \f[B]BC_LONG_BIT\f[R]. .TP \f[B]BC_BASE_MAX\f[R] The maximum output base. Set at \f[B]BC_BASE_POW\f[R]. .TP \f[B]BC_DIM_MAX\f[R] The maximum size of arrays. Set at \f[B]SIZE_MAX-1\f[R]. .TP \f[B]BC_SCALE_MAX\f[R] The maximum \f[B]scale\f[R]. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_STRING_MAX\f[R] The maximum length of strings. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_NAME_MAX\f[R] The maximum length of identifiers. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_NUM_MAX\f[R] The maximum length of a number (in decimal digits), which includes digits after the decimal point. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_RAND_MAX\f[R] The maximum integer (inclusive) returned by the \f[B]rand()\f[R] operand. Set at \f[B]2\[ha]BC_LONG_BIT-1\f[R]. .TP Exponent The maximum allowable exponent (positive or negative). Set at \f[B]BC_OVERFLOW_MAX\f[R]. .TP Number of vars The maximum number of vars/arrays. Set at \f[B]SIZE_MAX-1\f[R]. .PP The actual values can be queried with the \f[B]limits\f[R] statement. .PP These limits are meant to be effectively non-existent; the limits are so large (at least on 64-bit machines) that there should not be any point at which they become a problem. In fact, memory should be exhausted before these limits should be hit. .SH ENVIRONMENT VARIABLES -.PP As \f[B]non-portable extensions\f[R], bc(1) recognizes the following environment variables: .TP \f[B]POSIXLY_CORRECT\f[R] If this variable exists (no matter the contents), bc(1) behaves as if the \f[B]-s\f[R] option was given. .TP \f[B]BC_ENV_ARGS\f[R] This is another way to give command-line arguments to bc(1). They should be in the same format as all other command-line arguments. These are always processed first, so any files given in \f[B]BC_ENV_ARGS\f[R] will be processed before arguments and files given on the command-line. This gives the user the ability to set up \[lq]standard\[rq] options and files to be used at every invocation. The most useful thing for such files to contain would be useful functions that the user might want every time bc(1) runs. .RS .PP The code that parses \f[B]BC_ENV_ARGS\f[R] will correctly handle quoted arguments, but it does not understand escape sequences. For example, the string \f[B]\[lq]/home/gavin/some bc file.bc\[rq]\f[R] will be correctly parsed, but the string \f[B]\[lq]/home/gavin/some \[dq]bc\[dq] file.bc\[rq]\f[R] will include the backslashes. .PP The quote parsing will handle either kind of quotes, \f[B]\[cq]\f[R] or \f[B]\[lq]\f[R]. Thus, if you have a file with any number of single quotes in the name, you can use double quotes as the outside quotes, as in \f[B]\[lq]some `bc' file.bc\[rq]\f[R], and vice versa if you have a file with double quotes. However, handling a file with both kinds of quotes in \f[B]BC_ENV_ARGS\f[R] is not supported due to the complexity of the parsing, though such files are still supported on the command-line where the parsing is done by the shell. .RE .TP \f[B]BC_LINE_LENGTH\f[R] If this environment variable exists and contains an integer that is greater than \f[B]1\f[R] and is less than \f[B]UINT16_MAX\f[R] (\f[B]2\[ha]16-1\f[R]), bc(1) will output lines to that length, including the backslash (\f[B]\[rs]\f[R]). The default line length is \f[B]70\f[R]. .RS .PP The special value of \f[B]0\f[R] will disable line length checking and print numbers without regard to line length and without backslashes and newlines. .RE .TP \f[B]BC_BANNER\f[R] If this environment variable exists and contains an integer, then a non-zero value activates the copyright banner when bc(1) is in interactive mode, while zero deactivates it. .RS .PP If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), then this environment variable has no effect because bc(1) does not print the banner when not in interactive mode. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_SIGINT_RESET\f[R] If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), then this environment variable has no effect because bc(1) exits on \f[B]SIGINT\f[R] when not in interactive mode. .RS .PP However, when bc(1) is in interactive mode, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) reset on \f[B]SIGINT\f[R], rather than exit, and zero makes bc(1) exit. If this environment variable exists and is \f[I]not\f[R] an integer, then bc(1) will exit on \f[B]SIGINT\f[R]. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_TTY_MODE\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, then a non-zero value makes bc(1) use TTY mode, and zero makes bc(1) not use TTY mode. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_PROMPT\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) use a prompt, and zero or a non-integer makes bc(1) not use a prompt. If this environment variable does not exist and \f[B]BC_TTY_MODE\f[R] does, then the value of the \f[B]BC_TTY_MODE\f[R] environment variable is used. .PP This environment variable and the \f[B]BC_TTY_MODE\f[R] environment variable override the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_EXPR_EXIT\f[R] If any expressions or expression files are given on the command-line with \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R], then if this environment variable exists and contains an integer, a non-zero value makes bc(1) exit after executing the expressions and expression files, and a zero value makes bc(1) not exit. .RS .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_DIGIT_CLAMP\f[R] When parsing numbers and if this environment variable exists and contains an integer, a non-zero value makes bc(1) clamp digits that are greater than or equal to the current \f[B]ibase\f[R] so that all such digits are considered equal to the \f[B]ibase\f[R] minus 1, and a zero value disables such clamping so that those digits are always equal to their value, which is multiplied by the power of the \f[B]ibase\f[R]. .RS .PP This never applies to single-digit numbers, as per the standard (see the \f[B]STANDARDS\f[R] section). .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .SH EXIT STATUS -.PP bc(1) returns the following exit statuses: .TP \f[B]0\f[R] No error. .TP \f[B]1\f[R] A math error occurred. This follows standard practice of using \f[B]1\f[R] for expected errors, since math errors will happen in the process of normal execution. .RS .PP Math errors include divide by \f[B]0\f[R], taking the square root of a negative number, using a negative number as a bound for the pseudo-random number generator, attempting to convert a negative number to a hardware integer, overflow when converting a number to a hardware integer, overflow when calculating the size of a number, and attempting to use a non-integer where an integer is required. .PP Converting to a hardware integer happens for the second operand of the power (\f[B]\[ha]\f[R]), places (\f[B]\[at]\f[R]), left shift (\f[B]<<\f[R]), and right shift (\f[B]>>\f[R]) operators and their corresponding assignment operators. .RE .TP \f[B]2\f[R] A parse error occurred. .RS .PP Parse errors include unexpected \f[B]EOF\f[R], using an invalid character, failing to find the end of a string or comment, using a token where it is invalid, giving an invalid expression, giving an invalid print statement, giving an invalid function definition, attempting to assign to an expression that is not a named expression (see the \f[I]Named Expressions\f[R] subsection of the \f[B]SYNTAX\f[R] section), giving an invalid \f[B]auto\f[R] list, having a duplicate \f[B]auto\f[R]/function parameter, failing to find the end of a code block, attempting to return a value from a \f[B]void\f[R] function, attempting to use a variable as a reference, and using any extensions when the option \f[B]-s\f[R] or any equivalents were given. .RE .TP \f[B]3\f[R] A runtime error occurred. .RS .PP Runtime errors include assigning an invalid number to any global (\f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R]), giving a bad expression to a \f[B]read()\f[R] call, calling \f[B]read()\f[R] inside of a \f[B]read()\f[R] call, type errors, passing the wrong number of arguments to functions, attempting to call an undefined function, and attempting to use a \f[B]void\f[R] function call as a value in an expression. .RE .TP \f[B]4\f[R] A fatal error occurred. .RS .PP Fatal errors include memory allocation errors, I/O errors, failing to open files, attempting to use files that do not have only ASCII characters (bc(1) only accepts ASCII characters), attempting to open a directory as a file, and giving invalid command-line options. .RE .PP The exit status \f[B]4\f[R] is special; when a fatal error occurs, bc(1) always exits and returns \f[B]4\f[R], no matter what mode bc(1) is in. .PP The other statuses will only be returned when bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), since bc(1) resets its state (see the \f[B]RESET\f[R] section) and accepts more input when one of those errors occurs in interactive mode. This is also the case when interactive mode is forced by the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .PP These exit statuses allow bc(1) to be used in shell scripting with error checking, and its normal behavior can be forced by using the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .SH INTERACTIVE MODE -.PP Per the standard (see the \f[B]STANDARDS\f[R] section), bc(1) has an interactive mode and a non-interactive mode. Interactive mode is turned on automatically when both \f[B]stdin\f[R] and \f[B]stdout\f[R] are hooked to a terminal, but the \f[B]-i\f[R] flag and \f[B]--interactive\f[R] option can turn it on in other situations. .PP In interactive mode, bc(1) attempts to recover from errors (see the \f[B]RESET\f[R] section), and in normal execution, flushes \f[B]stdout\f[R] as soon as execution is done for the current input. bc(1) may also reset on \f[B]SIGINT\f[R] instead of exit, depending on the contents of, or default for, the \f[B]BC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .SH TTY MODE -.PP If \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY, then \[lq]TTY mode\[rq] is considered to be available, and thus, bc(1) can turn on TTY mode, subject to some settings. .PP If there is the environment variable \f[B]BC_TTY_MODE\f[R] in the environment (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then if that environment variable contains a non-zero integer, bc(1) will turn on TTY mode when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY. If the \f[B]BC_TTY_MODE\f[R] environment variable exists but is \f[I]not\f[R] a non-zero integer, then bc(1) will not turn TTY mode on. .PP If the environment variable \f[B]BC_TTY_MODE\f[R] does \f[I]not\f[R] exist, the default setting is used. The default setting can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP TTY mode is different from interactive mode because interactive mode is required in the bc(1) standard (see the \f[B]STANDARDS\f[R] section), and interactive mode requires only \f[B]stdin\f[R] and \f[B]stdout\f[R] to be connected to a terminal. .SS Command-Line History -.PP Command-line history is only enabled if TTY mode is, i.e., that \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the \f[B]BC_TTY_MODE\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and its default do not disable TTY mode. See the \f[B]COMMAND LINE HISTORY\f[R] section for more information. .SS Prompt -.PP If TTY mode is available, then a prompt can be enabled. Like TTY mode itself, it can be turned on or off with an environment variable: \f[B]BC_PROMPT\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP If the environment variable \f[B]BC_PROMPT\f[R] exists and is a non-zero integer, then the prompt is turned on when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options were not used. The read prompt will be turned on under the same conditions, except that the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options must also not be used. .PP However, if \f[B]BC_PROMPT\f[R] does not exist, the prompt can be enabled or disabled with the \f[B]BC_TTY_MODE\f[R] environment variable, the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options, and the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options. See the \f[B]ENVIRONMENT VARIABLES\f[R] and \f[B]OPTIONS\f[R] sections for more details. .SH SIGNAL HANDLING -.PP Sending a \f[B]SIGINT\f[R] will cause bc(1) to do one of two things. .PP If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), or the \f[B]BC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or its default, is either not an integer or it is zero, bc(1) will exit. .PP However, if bc(1) is in interactive mode, and the \f[B]BC_SIGINT_RESET\f[R] or its default is an integer and non-zero, then bc(1) will stop executing the current input and reset (see the \f[B]RESET\f[R] section) upon receiving a \f[B]SIGINT\f[R]. .PP Note that \[lq]current input\[rq] can mean one of two things. If bc(1) is processing input from \f[B]stdin\f[R] in interactive mode, it will ask for more input. If bc(1) is processing input from a file in interactive mode, it will stop processing the file and start processing the next file, if one exists, or ask for input from \f[B]stdin\f[R] if no other file exists. .PP This means that if a \f[B]SIGINT\f[R] is sent to bc(1) as it is executing a file, it can seem as though bc(1) did not respond to the signal since it will immediately start executing the next file. This is by design; most files that users execute when interacting with bc(1) have function definitions, which are quick to parse. If a file takes a long time to execute, there may be a bug in that file. The rest of the files could still be executed without problem, allowing the user to continue. .PP \f[B]SIGTERM\f[R] and \f[B]SIGQUIT\f[R] cause bc(1) to clean up and exit, and it uses the default handler for all other signals. The one exception is \f[B]SIGHUP\f[R]; in that case, and only when bc(1) is in TTY mode (see the \f[B]TTY MODE\f[R] section), a \f[B]SIGHUP\f[R] will cause bc(1) to clean up and exit. .SH COMMAND LINE HISTORY -.PP bc(1) supports interactive command-line editing. .PP If bc(1) can be in TTY mode (see the \f[B]TTY MODE\f[R] section), history can be enabled. This means that command-line history can only be enabled when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY. .PP Like TTY mode itself, it can be turned on or off with the environment variable \f[B]BC_TTY_MODE\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP If history is enabled, previous lines can be recalled and edited with the arrow keys. .PP \f[B]Note\f[R]: tabs are converted to 8 spaces. .SH LOCALES -.PP This bc(1) ships with support for adding error messages for different locales and thus, supports \f[B]LC_MESSAGES\f[R]. .SH SEE ALSO -.PP dc(1) .SH STANDARDS -.PP bc(1) is compliant with the IEEE Std 1003.1-2017 (\[lq]POSIX.1-2017\[rq]) specification at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html . The flags \f[B]-efghiqsvVw\f[R], all long options, and the extensions noted above are extensions to that specification. .PP In addition, the behavior of the \f[B]quit\f[R] implements an interpretation of that specification that is different from all known implementations. For more information see the \f[B]Statements\f[R] subsection of the \f[B]SYNTAX\f[R] section. .PP Note that the specification explicitly says that bc(1) only accepts numbers that use a period (\f[B].\f[R]) as a radix point, regardless of the value of \f[B]LC_NUMERIC\f[R]. .PP This bc(1) supports error messages for different locales, and thus, it supports \f[B]LC_MESSAGES\f[R]. .SH BUGS -.PP Before version \f[B]6.1.0\f[R], this bc(1) had incorrect behavior for the \f[B]quit\f[R] statement. .PP No other bugs are known. Report bugs at https://git.gavinhoward.com/gavin/bc . .SH AUTHORS -.PP -Gavin D. -Howard and contributors. +Gavin D. Howard \c +.MT gavin@gavinhoward.com +.ME \c +\ and contributors. diff --git a/contrib/bc/manuals/bc/A.1.md b/contrib/bc/manuals/bc/A.1.md index 3e593e16b481..77004822f73d 100644 --- a/contrib/bc/manuals/bc/A.1.md +++ b/contrib/bc/manuals/bc/A.1.md @@ -1,2525 +1,2525 @@ # NAME bc - arbitrary-precision decimal arithmetic language and calculator # SYNOPSIS **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*] # DESCRIPTION bc(1) is an interactive processor for a language first standardized in 1991 by POSIX. (See the **STANDARDS** section.) The language provides unlimited precision decimal arithmetic and is somewhat C-like, but there are differences. Such differences will be noted in this document. After parsing and handling options, this bc(1) reads any files given on the command line and executes them before reading from **stdin**. This bc(1) is a drop-in replacement for *any* bc(1), including (and especially) the GNU bc(1). It also has many extensions and extra features beyond other implementations. **Note**: If running this bc(1) on *any* script meant for another bc(1) gives a parse error, it is probably because a word this bc(1) reserves as a keyword is used as the name of a function, variable, or array. To fix that, use the command-line option **-r** *keyword*, where *keyword* is the keyword that is used as a name in the script. For more information, see the **OPTIONS** section. If parsing scripts meant for other bc(1) implementations still does not work, that is a bug and should be reported. See the **BUGS** section. # OPTIONS The following are the options that bc(1) accepts. **-C**, **-\-no-digit-clamp** : Disables clamping of digits greater than or equal to the current **ibase** when parsing numbers. This means that the value added to a number from a digit is always that digit's value multiplied by the value of ibase raised to the power of the digit's position, which starts from 0 at the least significant digit. If this and/or the **-c** or **-\-digit-clamp** options are given multiple times, the last one given is used. This option overrides the **BC_DIGIT_CLAMP** environment variable (see the **ENVIRONMENT VARIABLES** section) and the default, which can be queried with the **-h** or **-\-help** options. This is a **non-portable extension**. **-c**, **-\-digit-clamp** : Enables clamping of digits greater than or equal to the current **ibase** when parsing numbers. This means that digits that the value added to a number from a digit that is greater than or equal to the ibase is the value of ibase minus 1 all multiplied by the value of ibase raised to the power of the digit's position, which starts from 0 at the least significant digit. If this and/or the **-C** or **-\-no-digit-clamp** options are given multiple times, the last one given is used. This option overrides the **BC_DIGIT_CLAMP** environment variable (see the **ENVIRONMENT VARIABLES** section) and the default, which can be queried with the **-h** or **-\-help** options. This is a **non-portable extension**. **-E** *seed*, **-\-seed**=*seed* : Sets the builtin variable **seed** to the value *seed* assuming that *seed* is in base 10. It is a fatal error if *seed* is not a valid number. If multiple instances of this option are given, the last is used. This is a **non-portable extension**. **-e** *expr*, **-\-expression**=*expr* : Evaluates *expr*. If multiple expressions are given, they are evaluated in order. If files are given as well (see the **-f** and **-\-file** options), the expressions and files are evaluated in the order given. This means that if a file is given before an expression, the file is read in and evaluated first. If this option is given on the command-line (i.e., not in **BC_ENV_ARGS**, see the **ENVIRONMENT VARIABLES** section), then after processing all expressions and files, bc(1) will exit, unless **-** (**stdin**) was given as an argument at least once to **-f** or **-\-file**, whether on the command-line or in **BC_ENV_ARGS**. However, if any other **-e**, **-\-expression**, **-f**, or **-\-file** arguments are given after **-f-** or equivalent is given, bc(1) will give a fatal error and exit. This is a **non-portable extension**. **-f** *file*, **-\-file**=*file* : Reads in *file* and evaluates it, line by line, as though it were read through **stdin**. If expressions are also given (see the **-e** and **-\-expression** options), the expressions are evaluated in the order given. If this option is given on the command-line (i.e., not in **BC_ENV_ARGS**, see the **ENVIRONMENT VARIABLES** section), then after processing all expressions and files, bc(1) will exit, unless **-** (**stdin**) was given as an argument at least once to **-f** or **-\-file**. However, if any other **-e**, **-\-expression**, **-f**, or **-\-file** arguments are given after **-f-** or equivalent is given, bc(1) will give a fatal error and exit. This is a **non-portable extension**. **-g**, **-\-global-stacks** : Turns the globals **ibase**, **obase**, **scale**, and **seed** into stacks. This has the effect that a copy of the current value of all four are pushed onto a stack for every function call, as well as popped when every function returns. This means that functions can assign to any and all of those globals without worrying that the change will affect other functions. Thus, a hypothetical function named **output(x,b)** that simply printed **x** in base **b** could be written like this: define void output(x, b) { obase=b x } instead of like this: define void output(x, b) { auto c c=obase obase=b x obase=c } This makes writing functions much easier. (**Note**: the function **output(x,b)** exists in the extended math library. See the **LIBRARY** section.) However, since using this flag means that functions cannot set **ibase**, **obase**, **scale**, or **seed** globally, functions that are made to do so cannot work anymore. There are two possible use cases for that, and each has a solution. First, if a function is called on startup to turn bc(1) into a number converter, it is possible to replace that capability with various shell aliases. Examples: alias d2o="bc -e ibase=A -e obase=8" alias h2b="bc -e ibase=G -e obase=2" Second, if the purpose of a function is to set **ibase**, **obase**, **scale**, or **seed** globally for any other purpose, it could be split into one to four functions (based on how many globals it sets) and each of those functions could return the desired value for a global. For functions that set **seed**, the value assigned to **seed** is not propagated to parent functions. This means that the sequence of pseudo-random numbers that they see will not be the same sequence of pseudo-random numbers that any parent sees. This is only the case once **seed** has been set. If a function desires to not affect the sequence of pseudo-random numbers of its parents, but wants to use the same **seed**, it can use the following line: seed = seed If the behavior of this option is desired for every run of bc(1), then users could make sure to define **BC_ENV_ARGS** and include this option (see the **ENVIRONMENT VARIABLES** section for more details). If **-s**, **-w**, or any equivalents are used, this option is ignored. This is a **non-portable extension**. **-h**, **-\-help** : Prints a usage message and exits. **-I** *ibase*, **-\-ibase**=*ibase* : Sets the builtin variable **ibase** to the value *ibase* assuming that *ibase* is in base 10. It is a fatal error if *ibase* is not a valid number. If multiple instances of this option are given, the last is used. This is a **non-portable extension**. **-i**, **-\-interactive** : Forces interactive mode. (See the **INTERACTIVE MODE** section.) This is a **non-portable extension**. **-L**, **-\-no-line-length** : Disables line length checking and prints numbers without backslashes and newlines. In other words, this option sets **BC_LINE_LENGTH** to **0** (see the **ENVIRONMENT VARIABLES** section). This is a **non-portable extension**. **-l**, **-\-mathlib** : Sets **scale** (see the **SYNTAX** section) to **20** and loads the included math library and the extended math library before running any code, including any expressions or files specified on the command line. To learn what is in the libraries, see the **LIBRARY** section. **-O** *obase*, **-\-obase**=*obase* : Sets the builtin variable **obase** to the value *obase* assuming that *obase* is in base 10. It is a fatal error if *obase* is not a valid number. If multiple instances of this option are given, the last is used. This is a **non-portable extension**. **-P**, **-\-no-prompt** : Disables the prompt in TTY mode. (The prompt is only enabled in TTY mode. See the **TTY MODE** section.) This is mostly for those users that do not want a prompt or are not used to having them in bc(1). Most of those users would want to put this option in **BC_ENV_ARGS** (see the **ENVIRONMENT VARIABLES** section). These options override the **BC_PROMPT** and **BC_TTY_MODE** environment variables (see the **ENVIRONMENT VARIABLES** section). This is a **non-portable extension**. **-q**, **-\-quiet** : This option is for compatibility with the GNU bc(1) (https://www.gnu.org/software/bc/); it is a no-op. Without this option, GNU bc(1) prints a copyright header. This bc(1) only prints the copyright header if one or more of the **-v**, **-V**, or **-\-version** options are given unless the **BC_BANNER** environment variable is set and contains a non-zero integer or if this bc(1) was built with the header displayed by default. If *any* of that is the case, then this option *does* prevent bc(1) from printing the header. This is a **non-portable extension**. **-R**, **-\-no-read-prompt** : Disables the read prompt in TTY mode. (The read prompt is only enabled in TTY mode. See the **TTY MODE** section.) This is mostly for those users that do not want a read prompt or are not used to having them in bc(1). Most of those users would want to put this option in **BC_ENV_ARGS** (see the **ENVIRONMENT VARIABLES** section). This option is also useful in hash bang lines of bc(1) scripts that prompt for user input. This option does not disable the regular prompt because the read prompt is only used when the **read()** built-in function is called. These options *do* override the **BC_PROMPT** and **BC_TTY_MODE** environment variables (see the **ENVIRONMENT VARIABLES** section), but only for the read prompt. This is a **non-portable extension**. **-r** *keyword*, **-\-redefine**=*keyword* : Redefines *keyword* in order to allow it to be used as a function, variable, or array name. This is useful when this bc(1) gives parse errors when parsing scripts meant for other bc(1) implementations. The keywords this bc(1) allows to be redefined are: * **abs** * **asciify** * **continue** * **divmod** * **else** * **halt** * **irand** * **last** * **limits** * **maxibase** * **maxobase** * **maxrand** * **maxscale** * **modexp** * **print** * **rand** * **read** * **seed** * **stream** If any of those keywords are used as a function, variable, or array name in a script, use this option with the keyword as the argument. If multiple are used, use this option for all of them; it can be used multiple times. Keywords are *not* redefined when parsing the builtin math library (see the **LIBRARY** section). It is a fatal error to redefine keywords mandated by the POSIX standard (see the **STANDARDS** section). It is a fatal error to attempt to redefine words that this bc(1) does not reserve as keywords. **-S** *scale*, **-\-scale**=*scale* : Sets the builtin variable **scale** to the value *scale* assuming that *scale* is in base 10. It is a fatal error if *scale* is not a valid number. If multiple instances of this option are given, the last is used. This is a **non-portable extension**. **-s**, **-\-standard** : Process exactly the language defined by the standard (see the **STANDARDS** section) and error if any extensions are used. This is a **non-portable extension**. **-v**, **-V**, **-\-version** : Print the version information (copyright header) and exits. This is a **non-portable extension**. **-w**, **-\-warn** : Like **-s** and **-\-standard**, except that warnings (and not errors) are printed for non-standard extensions and execution continues normally. This is a **non-portable extension**. **-z**, **-\-leading-zeroes** : Makes bc(1) print all numbers greater than **-1** and less than **1**, and not equal to **0**, with a leading zero. This can be set for individual numbers with the **plz(x)**, **plznl(x)**, **pnlz(x)**, and **pnlznl(x)** functions in the extended math library (see the **LIBRARY** section). This is a **non-portable extension**. All long options are **non-portable extensions**. # STDIN If no files or expressions are given by the **-f**, **-\-file**, **-e**, or **-\-expression** options, then bc(1) reads from **stdin**. However, there are a few caveats to this. First, **stdin** is evaluated a line at a time. The only exception to this is if the parse cannot complete. That means that starting a string without ending it or starting a function, **if** statement, or loop without ending it will also cause bc(1) to not execute. Second, after an **if** statement, bc(1) doesn't know if an **else** statement will follow, so it will not execute until it knows there will not be an **else** statement. # STDOUT Any non-error output is written to **stdout**. In addition, if history (see the **HISTORY** section) and the prompt (see the **TTY MODE** section) are enabled, both are output to **stdout**. **Note**: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the **EXIT STATUS** section) if it cannot write to **stdout**, so if **stdout** is closed, as in **bc >&-**, it will quit with an error. This is done so that bc(1) can report problems when **stdout** is redirected to a file. If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect **stdout** to **/dev/null**. # STDERR Any error output is written to **stderr**. **Note**: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the **EXIT STATUS** section) if it cannot write to **stderr**, so if **stderr** is closed, as in **bc 2>&-**, it will quit with an error. This is done so that bc(1) can exit with an error code when **stderr** is redirected to a file. If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect **stderr** to **/dev/null**. # SYNTAX The syntax for bc(1) programs is mostly C-like, with some differences. This bc(1) follows the POSIX standard (see the **STANDARDS** section), which is a much more thorough resource for the language this bc(1) accepts. This section is meant to be a summary and a listing of all the extensions to the standard. In the sections below, **E** means expression, **S** means statement, and **I** means identifier. Identifiers (**I**) start with a lowercase letter and can be followed by any number (up to **BC_NAME_MAX-1**) of lowercase letters (**a-z**), digits (**0-9**), and underscores (**\_**). The regex is **\[a-z\]\[a-z0-9\_\]\***. Identifiers with more than one character (letter) are a **non-portable extension**. **ibase** is a global variable determining how to interpret constant numbers. It is the "input" base, or the number base used for interpreting input numbers. **ibase** is initially **10**. If the **-s** (**-\-standard**) and **-w** (**-\-warn**) flags were not given on the command line, the max allowable value for **ibase** is **36**. Otherwise, it is **16**. The min allowable value for **ibase** is **2**. The max allowable value for **ibase** can be queried in bc(1) programs with the **maxibase()** built-in function. **obase** is a global variable determining how to output results. It is the "output" base, or the number base used for outputting numbers. **obase** is initially **10**. The max allowable value for **obase** is **BC_BASE_MAX** and can be queried in bc(1) programs with the **maxobase()** built-in function. The min allowable value for **obase** is **0**. If **obase** is **0**, values are output in scientific notation, and if **obase** is **1**, values are output in engineering notation. Otherwise, values are output in the specified base. Outputting in scientific and engineering notations are **non-portable extensions**. The *scale* of an expression is the number of digits in the result of the expression right of the decimal point, and **scale** is a global variable that sets the precision of any operations, with exceptions. **scale** is initially **0**. **scale** cannot be negative. The max allowable value for **scale** is **BC_SCALE_MAX** and can be queried in bc(1) programs with the **maxscale()** built-in function. bc(1) has both *global* variables and *local* variables. All *local* variables are local to the function; they are parameters or are introduced in the **auto** list of a function (see the **FUNCTIONS** section). If a variable is accessed which is not a parameter or in the **auto** list, it is assumed to be *global*. If a parent function has a *local* variable version of a variable that a child function considers *global*, the value of that *global* variable in the child function is the value of the variable in the parent function, not the value of the actual *global* variable. All of the above applies to arrays as well. The value of a statement that is an expression (i.e., any of the named expressions or operands) is printed unless the lowest precedence operator is an assignment operator *and* the expression is notsurrounded by parentheses. The value that is printed is also assigned to the special variable **last**. A single dot (**.**) may also be used as a synonym for **last**. These are **non-portable extensions**. Either semicolons or newlines may separate statements. ## Comments There are two kinds of comments: 1. Block comments are enclosed in **/\*** and **\*/**. 2. Line comments go from **#** until, and not including, the next newline. This is a **non-portable extension**. ## Named Expressions The following are named expressions in bc(1): 1. Variables: **I** 2. Array Elements: **I[E]** 3. **ibase** 4. **obase** 5. **scale** 6. **seed** 7. **last** or a single dot (**.**) Numbers 6 and 7 are **non-portable extensions**. The meaning of **seed** is dependent on the current pseudo-random number generator but is guaranteed to not change except for new major versions. The *scale* and sign of the value may be significant. If a previously used **seed** value is assigned to **seed** and used again, the pseudo-random number generator is guaranteed to produce the same sequence of pseudo-random numbers as it did when the **seed** value was previously used. The exact value assigned to **seed** is not guaranteed to be returned if **seed** is queried again immediately. However, if **seed** *does* return a different value, both values, when assigned to **seed**, are guaranteed to produce the same sequence of pseudo-random numbers. This means that certain values assigned to **seed** will *not* produce unique sequences of pseudo-random numbers. The value of **seed** will change after any use of the **rand()** and **irand(E)** operands (see the *Operands* subsection below), except if the parameter passed to **irand(E)** is **0**, **1**, or negative. There is no limit to the length (number of significant decimal digits) or *scale* of the value that can be assigned to **seed**. Variables and arrays do not interfere; users can have arrays named the same as variables. This also applies to functions (see the **FUNCTIONS** section), so a user can have a variable, array, and function that all have the same name, and they will not shadow each other, whether inside of functions or not. Named expressions are required as the operand of **increment**/**decrement** operators and as the left side of **assignment** operators (see the *Operators* subsection). ## Operands The following are valid operands in bc(1): 1. Numbers (see the *Numbers* subsection below). 2. Array indices (**I[E]**). 3. **(E)**: The value of **E** (used to change precedence). 4. **sqrt(E)**: The square root of **E**. **E** must be non-negative. 5. **length(E)**: The number of significant decimal digits in **E**. Returns **1** for **0** with no decimal places. If given a string, the length of the string is returned. Passing a string to **length(E)** is a **non-portable extension**. 6. **length(I[])**: The number of elements in the array **I**. This is a **non-portable extension**. 7. **scale(E)**: The *scale* of **E**. 8. **abs(E)**: The absolute value of **E**. This is a **non-portable extension**. 9. **is_number(E)**: **1** if the given argument is a number, **0** if it is a string. This is a **non-portable extension**. 10. **is_string(E)**: **1** if the given argument is a string, **0** if it is a number. This is a **non-portable extension**. 11. **modexp(E, E, E)**: Modular exponentiation, where the first expression is the base, the second is the exponent, and the third is the modulus. All three values must be integers. The second argument must be non-negative. The third argument must be non-zero. This is a **non-portable extension**. 11. **divmod(E, E, I[])**: Division and modulus in one operation. This is for optimization. The first expression is the dividend, and the second is the divisor, which must be non-zero. The return value is the quotient, and the modulus is stored in index **0** of the provided array (the last argument). This is a **non-portable extension**. 12. **asciify(E)**: If **E** is a string, returns a string that is the first letter of its argument. If it is a number, calculates the number mod **256** and returns that number as a one-character string. This is a **non-portable extension**. 13. **asciify(I[])**: A string that is made up of the characters that would result from running **asciify(E)** on each element of the array identified by the argument. This allows creating multi-character strings and storing them. This is a **non-portable extension**. 14. **I()**, **I(E)**, **I(E, E)**, and so on, where **I** is an identifier for a non-**void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). The **E** argument(s) may also be arrays of the form **I[]**, which will automatically be turned into array references (see the *Array References* subsection of the **FUNCTIONS** section) if the corresponding parameter in the function definition is an array reference. 15. **read()**: Reads a line from **stdin** and uses that as an expression. The result of that expression is the result of the **read()** operand. This is a **non-portable extension**. 16. **maxibase()**: The max allowable **ibase**. This is a **non-portable extension**. 17. **maxobase()**: The max allowable **obase**. This is a **non-portable extension**. 18. **maxscale()**: The max allowable **scale**. This is a **non-portable extension**. 19. **line_length()**: The line length set with **BC_LINE_LENGTH** (see the **ENVIRONMENT VARIABLES** section). This is a **non-portable extension**. 20. **global_stacks()**: **0** if global stacks are not enabled with the **-g** or **-\-global-stacks** options, non-zero otherwise. See the **OPTIONS** section. This is a **non-portable extension**. 21. **leading_zero()**: **0** if leading zeroes are not enabled with the **-z** or **--leading-zeroes** options, non-zero otherwise. See the **OPTIONS** section. This is a **non-portable extension**. 22. **rand()**: A pseudo-random integer between **0** (inclusive) and **BC_RAND_MAX** (inclusive). Using this operand will change the value of **seed**. This is a **non-portable extension**. 23. **irand(E)**: A pseudo-random integer between **0** (inclusive) and the value of **E** (exclusive). If **E** is negative or is a non-integer (**E**'s *scale* is not **0**), an error is raised, and bc(1) resets (see the **RESET** section) while **seed** remains unchanged. If **E** is larger than **BC_RAND_MAX**, the higher bound is honored by generating several pseudo-random integers, multiplying them by appropriate powers of **BC_RAND_MAX+1**, and adding them together. Thus, the size of integer that can be generated with this operand is unbounded. Using this operand will change the value of **seed**, unless the value of **E** is **0** or **1**. In that case, **0** is returned, and **seed** is *not* changed. This is a **non-portable extension**. 24. **maxrand()**: The max integer returned by **rand()**. This is a **non-portable extension**. The integers generated by **rand()** and **irand(E)** are guaranteed to be as unbiased as possible, subject to the limitations of the pseudo-random number generator. **Note**: The values returned by the pseudo-random number generator with **rand()** and **irand(E)** are guaranteed to *NOT* be cryptographically secure. This is a consequence of using a seeded pseudo-random number generator. However, they *are* guaranteed to be reproducible with identical **seed** values. This means that the pseudo-random values from bc(1) should only be used where a reproducible stream of pseudo-random numbers is *ESSENTIAL*. In any other case, use a non-seeded pseudo-random number generator. ## Numbers Numbers are strings made up of digits, uppercase letters, and at most **1** period for a radix. Numbers can have up to **BC_NUM_MAX** digits. Uppercase letters are equal to **9** plus their position in the alphabet, starting from **1** (i.e., **A** equals **10**, or **9+1**). If a digit or letter makes no sense with the current value of **ibase** (i.e., they are greater than or equal to the current value of **ibase**), then the behavior depends on the existence of the **-c**/**-\-digit-clamp** or **-C**/**-\-no-digit-clamp** options (see the **OPTIONS** section), the existence and setting of the **BC_DIGIT_CLAMP** environment variable (see the **ENVIRONMENT VARIABLES** section), or the default, which can be queried with the **-h**/**-\-help** option. If clamping is off, then digits or letters that are greater than or equal to the current value of **ibase** are not changed. Instead, their given value is multiplied by the appropriate power of **ibase** and added into the number. This means that, with an **ibase** of **3**, the number **AB** is equal to **3\^1\*A+3\^0\*B**, which is **3** times **10** plus **11**, or **41**. If clamping is on, then digits or letters that are greater than or equal to the current value of **ibase** are set to the value of the highest valid digit in **ibase** before being multiplied by the appropriate power of **ibase** and added into the number. This means that, with an **ibase** of **3**, the number **AB** is equal to **3\^1\*2+3\^0\*2**, which is **3** times **2** plus **2**, or **8**. There is one exception to clamping: single-character numbers (i.e., **A** alone). Such numbers are never clamped and always take the value they would have in the highest possible **ibase**. This means that **A** alone always equals decimal **10** and **Z** alone always equals decimal **35**. This behavior is mandated by the standard (see the STANDARDS section) and is meant to provide an easy way to set the current **ibase** (with the **i** command) regardless of the current value of **ibase**. If clamping is on, and the clamped value of a character is needed, use a leading zero, i.e., for **A**, use **0A**. In addition, bc(1) accepts numbers in scientific notation. These have the form **\e\**. The exponent (the portion after the **e**) must be an integer. An example is **1.89237e9**, which is equal to **1892370000**. Negative exponents are also allowed, so **4.2890e-3** is equal to **0.0042890**. Using scientific notation is an error or warning if the **-s** or **-w**, respectively, command-line options (or equivalents) are given. **WARNING**: Both the number and the exponent in scientific notation are interpreted according to the current **ibase**, but the number is still multiplied by **10\^exponent** regardless of the current **ibase**. For example, if **ibase** is **16** and bc(1) is given the number string **FFeA**, the resulting decimal number will be **2550000000000**, and if bc(1) is given the number string **10e-4**, the resulting decimal number will be **0.0016**. Accepting input as scientific notation is a **non-portable extension**. ## Operators The following arithmetic and logical operators can be used. They are listed in order of decreasing precedence. Operators in the same group have the same precedence. **++** **-\-** : Type: Prefix and Postfix Associativity: None Description: **increment**, **decrement** **-** **!** : Type: Prefix Associativity: None Description: **negation**, **boolean not** **\$** : Type: Postfix Associativity: None Description: **truncation** **\@** : Type: Binary Associativity: Right Description: **set precision** **\^** : Type: Binary Associativity: Right Description: **power** **\*** **/** **%** : Type: Binary Associativity: Left Description: **multiply**, **divide**, **modulus** **+** **-** : Type: Binary Associativity: Left Description: **add**, **subtract** **\<\<** **\>\>** : Type: Binary Associativity: Left Description: **shift left**, **shift right** **=** **\<\<=** **\>\>=** **+=** **-=** **\*=** **/=** **%=** **\^=** **\@=** : Type: Binary Associativity: Right Description: **assignment** **==** **\<=** **\>=** **!=** **\<** **\>** : Type: Binary Associativity: Left Description: **relational** **&&** : Type: Binary Associativity: Left Description: **boolean and** **||** : Type: Binary Associativity: Left Description: **boolean or** The operators will be described in more detail below. **++** **-\-** : The prefix and postfix **increment** and **decrement** operators behave exactly like they would in C. They require a named expression (see the *Named Expressions* subsection) as an operand. The prefix versions of these operators are more efficient; use them where possible. **-** : The **negation** operator returns **0** if a user attempts to negate any expression with the value **0**. Otherwise, a copy of the expression with its sign flipped is returned. **!** : The **boolean not** operator returns **1** if the expression is **0**, or **0** otherwise. This is a **non-portable extension**. **\$** : The **truncation** operator returns a copy of the given expression with all of its *scale* removed. This is a **non-portable extension**. **\@** : The **set precision** operator takes two expressions and returns a copy of the first with its *scale* equal to the value of the second expression. That could either mean that the number is returned without change (if the *scale* of the first expression matches the value of the second expression), extended (if it is less), or truncated (if it is more). The second expression must be an integer (no *scale*) and non-negative. This is a **non-portable extension**. **\^** : The **power** operator (not the **exclusive or** operator, as it would be in C) takes two expressions and raises the first to the power of the value of the second. The *scale* of the result is equal to **scale**. The second expression must be an integer (no *scale*), and if it is negative, the first value must be non-zero. **\*** : The **multiply** operator takes two expressions, multiplies them, and returns the product. If **a** is the *scale* of the first expression and **b** is the *scale* of the second expression, the *scale* of the result is equal to **min(a+b,max(scale,a,b))** where **min()** and **max()** return the obvious values. **/** : The **divide** operator takes two expressions, divides them, and returns the quotient. The *scale* of the result shall be the value of **scale**. The second expression must be non-zero. **%** : The **modulus** operator takes two expressions, **a** and **b**, and evaluates them by 1) Computing **a/b** to current **scale** and 2) Using the result of step 1 to calculate **a-(a/b)\*b** to *scale* **max(scale+scale(b),scale(a))**. The second expression must be non-zero. **+** : The **add** operator takes two expressions, **a** and **b**, and returns the sum, with a *scale* equal to the max of the *scale*s of **a** and **b**. **-** : The **subtract** operator takes two expressions, **a** and **b**, and returns the difference, with a *scale* equal to the max of the *scale*s of **a** and **b**. **\<\<** : The **left shift** operator takes two expressions, **a** and **b**, and returns a copy of the value of **a** with its decimal point moved **b** places to the right. The second expression must be an integer (no *scale*) and non-negative. This is a **non-portable extension**. **\>\>** : The **right shift** operator takes two expressions, **a** and **b**, and returns a copy of the value of **a** with its decimal point moved **b** places to the left. The second expression must be an integer (no *scale*) and non-negative. This is a **non-portable extension**. **=** **\<\<=** **\>\>=** **+=** **-=** **\*=** **/=** **%=** **\^=** **\@=** : The **assignment** operators take two expressions, **a** and **b** where **a** is a named expression (see the *Named Expressions* subsection). For **=**, **b** is copied and the result is assigned to **a**. For all others, **a** and **b** are applied as operands to the corresponding arithmetic operator and the result is assigned to **a**. The **assignment** operators that correspond to operators that are extensions are themselves **non-portable extensions**. **==** **\<=** **\>=** **!=** **\<** **\>** : The **relational** operators compare two expressions, **a** and **b**, and if the relation holds, according to C language semantics, the result is **1**. Otherwise, it is **0**. Note that unlike in C, these operators have a lower precedence than the **assignment** operators, which means that **a=b\>c** is interpreted as **(a=b)\>c**. Also, unlike the standard (see the **STANDARDS** section) requires, these operators can appear anywhere any other expressions can be used. This allowance is a **non-portable extension**. **&&** : The **boolean and** operator takes two expressions and returns **1** if both expressions are non-zero, **0** otherwise. This is *not* a short-circuit operator. This is a **non-portable extension**. **||** : The **boolean or** operator takes two expressions and returns **1** if one of the expressions is non-zero, **0** otherwise. This is *not* a short-circuit operator. This is a **non-portable extension**. ## Statements The following items are statements: 1. **E** 2. **{** **S** **;** ... **;** **S** **}** 3. **if** **(** **E** **)** **S** 4. **if** **(** **E** **)** **S** **else** **S** 5. **while** **(** **E** **)** **S** 6. **for** **(** **E** **;** **E** **;** **E** **)** **S** 7. An empty statement 8. **break** 9. **continue** 10. **quit** 11. **halt** 12. **limits** 13. A string of characters, enclosed in double quotes 14. **print** **E** **,** ... **,** **E** 15. **stream** **E** **,** ... **,** **E** 16. **I()**, **I(E)**, **I(E, E)**, and so on, where **I** is an identifier for a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). The **E** argument(s) may also be arrays of the form **I[]**, which will automatically be turned into array references (see the *Array References* subsection of the **FUNCTIONS** section) if the corresponding parameter in the function definition is an array reference. Numbers 4, 9, 11, 12, 14, 15, and 16 are **non-portable extensions**. Also, as a **non-portable extension**, any or all of the expressions in the header of a for loop may be omitted. If the condition (second expression) is omitted, it is assumed to be a constant **1**. The **break** statement causes a loop to stop iterating and resume execution immediately following a loop. This is only allowed in loops. The **continue** statement causes a loop iteration to stop early and returns to the start of the loop, including testing the loop condition. This is only allowed in loops. The **if** **else** statement does the same thing as in C. The **quit** statement causes bc(1) to quit, even if it is on a branch that will not be executed (it is a compile-time command). **Warning**: The behavior of this bc(1) on **quit** is slightly different from other bc(1) implementations. Other bc(1) implementations will exit as soon as they finish parsing the line that a **quit** command is on. This bc(1) will execute any completed and executable statements that occur before the **quit** statement before exiting. In other words, for the bc(1) code below: for (i = 0; i < 3; ++i) i; quit Other bc(1) implementations will print nothing, and this bc(1) will print **0**, **1**, and **2** on successive lines before exiting. The **halt** statement causes bc(1) to quit, if it is executed. (Unlike **quit** if it is on a branch of an **if** statement that is not executed, bc(1) does not quit.) The **limits** statement prints the limits that this bc(1) is subject to. This is like the **quit** statement in that it is a compile-time command. An expression by itself is evaluated and printed, followed by a newline. Both scientific notation and engineering notation are available for printing the results of expressions. Scientific notation is activated by assigning **0** to **obase**, and engineering notation is activated by assigning **1** to **obase**. To deactivate them, just assign a different value to **obase**. Scientific notation and engineering notation are disabled if bc(1) is run with either the **-s** or **-w** command-line options (or equivalents). Printing numbers in scientific notation and/or engineering notation is a **non-portable extension**. ## Strings If strings appear as a statement by themselves, they are printed without a trailing newline. In addition to appearing as a lone statement by themselves, strings can be assigned to variables and array elements. They can also be passed to functions in variable parameters. If any statement that expects a string is given a variable that had a string assigned to it, the statement acts as though it had received a string. If any math operation is attempted on a string or a variable or array element that has been assigned a string, an error is raised, and bc(1) resets (see the **RESET** section). Assigning strings to variables and array elements and passing them to functions are **non-portable extensions**. ## Print Statement The "expressions" in a **print** statement may also be strings. If they are, there are backslash escape sequences that are interpreted specially. What those sequences are, and what they cause to be printed, are shown below: **\\a**: **\\a** **\\b**: **\\b** **\\\\**: **\\** **\\e**: **\\** **\\f**: **\\f** **\\n**: **\\n** **\\q**: **"** **\\r**: **\\r** **\\t**: **\\t** Any other character following a backslash causes the backslash and character to be printed as-is. Any non-string expression in a print statement shall be assigned to **last**, like any other expression that is printed. ## Stream Statement The expressions in a **stream** statement may also be strings. If a **stream** statement is given a string, it prints the string as though the string had appeared as its own statement. In other words, the **stream** statement prints strings normally, without a newline. If a **stream** statement is given a number, a copy of it is truncated and its absolute value is calculated. The result is then printed as though **obase** is **256** and each digit is interpreted as an 8-bit ASCII character, making it a byte stream. ## Order of Evaluation All expressions in a statment are evaluated left to right, except as necessary to maintain order of operations. This means, for example, assuming that **i** is equal to **0**, in the expression a[i++] = i++ the first (or 0th) element of **a** is set to **1**, and **i** is equal to **2** at the end of the expression. This includes function arguments. Thus, assuming **i** is equal to **0**, this means that in the expression x(i++, i++) the first argument passed to **x()** is **0**, and the second argument is **1**, while **i** is equal to **2** before the function starts executing. # FUNCTIONS Function definitions are as follows: ``` define I(I,...,I){ auto I,...,I S;...;S return(E) } ``` Any **I** in the parameter list or **auto** list may be replaced with **I[]** to make a parameter or **auto** var an array, and any **I** in the parameter list may be replaced with **\*I[]** to make a parameter an array reference. Callers of functions that take array references should not put an asterisk in the call; they must be called with just **I[]** like normal array parameters and will be automatically converted into references. As a **non-portable extension**, the opening brace of a **define** statement may appear on the next line. As a **non-portable extension**, the return statement may also be in one of the following forms: 1. **return** 2. **return** **(** **)** 3. **return** **E** The first two, or not specifying a **return** statement, is equivalent to **return (0)**, unless the function is a **void** function (see the *Void Functions* subsection below). ## Void Functions Functions can also be **void** functions, defined as follows: ``` define void I(I,...,I){ auto I,...,I S;...;S return } ``` They can only be used as standalone expressions, where such an expression would be printed alone, except in a print statement. Void functions can only use the first two **return** statements listed above. They can also omit the return statement entirely. The word "void" is not treated as a keyword; it is still possible to have variables, arrays, and functions named **void**. The word "void" is only treated specially right after the **define** keyword. This is a **non-portable extension**. ## Array References For any array in the parameter list, if the array is declared in the form ``` *I[] ``` it is a **reference**. Any changes to the array in the function are reflected, when the function returns, to the array that was passed in. Other than this, all function arguments are passed by value. This is a **non-portable extension**. # LIBRARY All of the functions below, including the functions in the extended math library (see the *Extended Library* subsection below), are available when the **-l** or **-\-mathlib** command-line flags are given, except that the extended math library is not available when the **-s** option, the **-w** option, or equivalents are given. ## Standard Library The standard (see the **STANDARDS** section) defines the following functions for the math library: **s(x)** : Returns the sine of **x**, which is assumed to be in radians. This is a transcendental function (see the *Transcendental Functions* subsection below). **c(x)** : Returns the cosine of **x**, which is assumed to be in radians. This is a transcendental function (see the *Transcendental Functions* subsection below). **a(x)** : Returns the arctangent of **x**, in radians. This is a transcendental function (see the *Transcendental Functions* subsection below). **l(x)** : Returns the natural logarithm of **x**. This is a transcendental function (see the *Transcendental Functions* subsection below). **e(x)** : Returns the mathematical constant **e** raised to the power of **x**. This is a transcendental function (see the *Transcendental Functions* subsection below). **j(x, n)** : Returns the bessel integer order **n** (truncated) of **x**. This is a transcendental function (see the *Transcendental Functions* subsection below). ## Extended Library The extended library is *not* loaded when the **-s**/**-\-standard** or **-w**/**-\-warn** options are given since they are not part of the library defined by the standard (see the **STANDARDS** section). The extended library is a **non-portable extension**. **p(x, y)** : Calculates **x** to the power of **y**, even if **y** is not an integer, and returns the result to the current **scale**. It is an error if **y** is negative and **x** is **0**. This is a transcendental function (see the *Transcendental Functions* subsection below). **r(x, p)** : Returns **x** rounded to **p** decimal places according to the rounding mode round half away from **0** (https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero). **ceil(x, p)** : Returns **x** rounded to **p** decimal places according to the rounding mode round away from **0** (https://en.wikipedia.org/wiki/Rounding#Rounding_away_from_zero). **f(x)** : Returns the factorial of the truncated absolute value of **x**. **max(a, b)** : Returns **a** if **a** is greater than **b**; otherwise, returns **b**. **min(a, b)** : Returns **a** if **a** is less than **b**; otherwise, returns **b**. **perm(n, k)** : Returns the permutation of the truncated absolute value of **n** of the truncated absolute value of **k**, if **k \<= n**. If not, it returns **0**. **comb(n, k)** : Returns the combination of the truncated absolute value of **n** of the truncated absolute value of **k**, if **k \<= n**. If not, it returns **0**. **fib(n)** : Returns the Fibonacci number of the truncated absolute value of **n**. **l2(x)** : Returns the logarithm base **2** of **x**. This is a transcendental function (see the *Transcendental Functions* subsection below). **l10(x)** : Returns the logarithm base **10** of **x**. This is a transcendental function (see the *Transcendental Functions* subsection below). **log(x, b)** : Returns the logarithm base **b** of **x**. This is a transcendental function (see the *Transcendental Functions* subsection below). **cbrt(x)** : Returns the cube root of **x**. **root(x, n)** : Calculates the truncated value of **n**, **r**, and returns the **r**th root of **x** to the current **scale**. If **r** is **0** or negative, this raises an error and causes bc(1) to reset (see the **RESET** section). It also raises an error and causes bc(1) to reset if **r** is even and **x** is negative. **gcd(a, b)** : Returns the greatest common divisor (factor) of the truncated absolute value of **a** and the truncated absolute value of **b**. **lcm(a, b)** : Returns the least common multiple of the truncated absolute value of **a** and the truncated absolute value of **b**. **pi(p)** : Returns **pi** to **p** decimal places. This is a transcendental function (see the *Transcendental Functions* subsection below). **t(x)** : Returns the tangent of **x**, which is assumed to be in radians. This is a transcendental function (see the *Transcendental Functions* subsection below). **a2(y, x)** : Returns the arctangent of **y/x**, in radians. If both **y** and **x** are equal to **0**, it raises an error and causes bc(1) to reset (see the **RESET** section). Otherwise, if **x** is greater than **0**, it returns **a(y/x)**. If **x** is less than **0**, and **y** is greater than or equal to **0**, it returns **a(y/x)+pi**. If **x** is less than **0**, and **y** is less than **0**, it returns **a(y/x)-pi**. If **x** is equal to **0**, and **y** is greater than **0**, it returns **pi/2**. If **x** is equal to **0**, and **y** is less than **0**, it returns **-pi/2**. This function is the same as the **atan2()** function in many programming languages. This is a transcendental function (see the *Transcendental Functions* subsection below). **sin(x)** : Returns the sine of **x**, which is assumed to be in radians. This is an alias of **s(x)**. This is a transcendental function (see the *Transcendental Functions* subsection below). **cos(x)** : Returns the cosine of **x**, which is assumed to be in radians. This is an alias of **c(x)**. This is a transcendental function (see the *Transcendental Functions* subsection below). **tan(x)** : Returns the tangent of **x**, which is assumed to be in radians. If **x** is equal to **1** or **-1**, this raises an error and causes bc(1) to reset (see the **RESET** section). This is an alias of **t(x)**. This is a transcendental function (see the *Transcendental Functions* subsection below). **atan(x)** : Returns the arctangent of **x**, in radians. This is an alias of **a(x)**. This is a transcendental function (see the *Transcendental Functions* subsection below). **atan2(y, x)** : Returns the arctangent of **y/x**, in radians. If both **y** and **x** are equal to **0**, it raises an error and causes bc(1) to reset (see the **RESET** section). Otherwise, if **x** is greater than **0**, it returns **a(y/x)**. If **x** is less than **0**, and **y** is greater than or equal to **0**, it returns **a(y/x)+pi**. If **x** is less than **0**, and **y** is less than **0**, it returns **a(y/x)-pi**. If **x** is equal to **0**, and **y** is greater than **0**, it returns **pi/2**. If **x** is equal to **0**, and **y** is less than **0**, it returns **-pi/2**. This function is the same as the **atan2()** function in many programming languages. This is an alias of **a2(y, x)**. This is a transcendental function (see the *Transcendental Functions* subsection below). **r2d(x)** : Converts **x** from radians to degrees and returns the result. This is a transcendental function (see the *Transcendental Functions* subsection below). **d2r(x)** : Converts **x** from degrees to radians and returns the result. This is a transcendental function (see the *Transcendental Functions* subsection below). **frand(p)** : Generates a pseudo-random integer between **0** (inclusive) and **1** (exclusive) with the number of decimal digits after the decimal point equal to the truncated absolute value of **p**. If **p** is not **0**, then calling this function will change the value of **seed**. If **p** is **0**, then **0** is returned, and **seed** is *not* changed. **ifrand(i, p)** : Generates a pseudo-random integer that is between **0** (inclusive) and the truncated absolute value of **i** (exclusive) with the number of decimal digits after the decimal point equal to the truncated absolute value of **p**. If the absolute value of **i** is greater than or equal to **2**, and **p** is not **0**, then calling this function will change the value of **seed**; otherwise, **0** is returned, and **seed** is not changed. **i2rand(a, b)** : Takes the truncated value of **a** and **b** and uses them as inclusive bounds to enerate a pseudo-random integer. If the difference of the truncated values of **a** and **b** is **0**, then the truncated value is returned, and **seed** is *not* changed. Otherwise, this function will change the value of **seed**. **srand(x)** : Returns **x** with its sign flipped with probability **0.5**. In other words, it randomizes the sign of **x**. **brand()** : Returns a random boolean value (either **0** or **1**). **band(a, b)** : Takes the truncated absolute value of both **a** and **b** and calculates and returns the result of the bitwise **and** operation between them. If you want to use signed two's complement arguments, use **s2u(x)** to convert. **bor(a, b)** : Takes the truncated absolute value of both **a** and **b** and calculates and returns the result of the bitwise **or** operation between them. If you want to use signed two's complement arguments, use **s2u(x)** to convert. **bxor(a, b)** : Takes the truncated absolute value of both **a** and **b** and calculates and returns the result of the bitwise **xor** operation between them. If you want to use signed two's complement arguments, use **s2u(x)** to convert. **bshl(a, b)** : Takes the truncated absolute value of both **a** and **b** and calculates and returns the result of **a** bit-shifted left by **b** places. If you want to use signed two's complement arguments, use **s2u(x)** to convert. **bshr(a, b)** : Takes the truncated absolute value of both **a** and **b** and calculates and returns the truncated result of **a** bit-shifted right by **b** places. If you want to use signed two's complement arguments, use **s2u(x)** to convert. **bnotn(x, n)** : Takes the truncated absolute value of **x** and does a bitwise not as though it has the same number of bytes as the truncated absolute value of **n**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bnot8(x)** : Does a bitwise not of the truncated absolute value of **x** as though it has - **8** binary digits (1 unsigned byte). + **8** binary digits (**1** unsigned byte). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bnot16(x)** : Does a bitwise not of the truncated absolute value of **x** as though it has - **16** binary digits (2 unsigned bytes). + **16** binary digits (**2** unsigned bytes). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bnot32(x)** : Does a bitwise not of the truncated absolute value of **x** as though it has - **32** binary digits (4 unsigned bytes). + **32** binary digits (**4** unsigned bytes). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bnot64(x)** : Does a bitwise not of the truncated absolute value of **x** as though it has - **64** binary digits (8 unsigned bytes). + **64** binary digits (**8** unsigned bytes). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bnot(x)** : Does a bitwise not of the truncated absolute value of **x** as though it has the minimum number of power of two unsigned bytes. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brevn(x, n)** : Runs a bit reversal on the truncated absolute value of **x** as though it has the same number of 8-bit bytes as the truncated absolute value of **n**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brev8(x)** : Runs a bit reversal on the truncated absolute value of **x** as though it - has 8 binary digits (1 unsigned byte). + has 8 binary digits (**1** unsigned byte). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brev16(x)** : Runs a bit reversal on the truncated absolute value of **x** as though it - has 16 binary digits (2 unsigned bytes). + has 16 binary digits (**2** unsigned bytes). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brev32(x)** : Runs a bit reversal on the truncated absolute value of **x** as though it - has 32 binary digits (4 unsigned bytes). + has 32 binary digits (**4** unsigned bytes). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brev64(x)** : Runs a bit reversal on the truncated absolute value of **x** as though it - has 64 binary digits (8 unsigned bytes). + has 64 binary digits (**8** unsigned bytes). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brev(x)** : Runs a bit reversal on the truncated absolute value of **x** as though it has the minimum number of power of two unsigned bytes. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **broln(x, p, n)** : Does a left bitwise rotatation of the truncated absolute value of **x**, as though it has the same number of unsigned 8-bit bytes as the truncated absolute value of **n**, by the number of places equal to the truncated absolute value of **p** modded by the **2** to the power of the number of binary digits in **n** 8-bit bytes. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brol8(x, p)** : Does a left bitwise rotatation of the truncated absolute value of **x**, as though it has **8** binary digits (**1** unsigned byte), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **8**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brol16(x, p)** : Does a left bitwise rotatation of the truncated absolute value of **x**, as though it has **16** binary digits (**2** unsigned bytes), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **16**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brol32(x, p)** : Does a left bitwise rotatation of the truncated absolute value of **x**, as - though it has **32** binary digits (**2** unsigned bytes), by the number of + though it has **32** binary digits (**4** unsigned bytes), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **32**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brol64(x, p)** : Does a left bitwise rotatation of the truncated absolute value of **x**, as - though it has **64** binary digits (**2** unsigned bytes), by the number of + though it has **64** binary digits (**8** unsigned bytes), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **64**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brol(x, p)** : Does a left bitwise rotatation of the truncated absolute value of **x**, as though it has the minimum number of power of two unsigned 8-bit bytes, by the number of places equal to the truncated absolute value of **p** modded by 2 to the power of the number of binary digits in the minimum number of 8-bit bytes. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brorn(x, p, n)** : Does a right bitwise rotatation of the truncated absolute value of **x**, as though it has the same number of unsigned 8-bit bytes as the truncated absolute value of **n**, by the number of places equal to the truncated absolute value of **p** modded by the **2** to the power of the number of binary digits in **n** 8-bit bytes. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bror8(x, p)** : Does a right bitwise rotatation of the truncated absolute value of **x**, as though it has **8** binary digits (**1** unsigned byte), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **8**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bror16(x, p)** : Does a right bitwise rotatation of the truncated absolute value of **x**, as though it has **16** binary digits (**2** unsigned bytes), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **16**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bror32(x, p)** : Does a right bitwise rotatation of the truncated absolute value of **x**, as though it has **32** binary digits (**2** unsigned bytes), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **32**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bror64(x, p)** : Does a right bitwise rotatation of the truncated absolute value of **x**, as though it has **64** binary digits (**2** unsigned bytes), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **64**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bror(x, p)** : Does a right bitwise rotatation of the truncated absolute value of **x**, as though it has the minimum number of power of two unsigned 8-bit bytes, by the number of places equal to the truncated absolute value of **p** modded by 2 to the power of the number of binary digits in the minimum number of 8-bit bytes. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bmodn(x, n)** : Returns the modulus of the truncated absolute value of **x** by **2** to the power of the multiplication of the truncated absolute value of **n** and **8**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bmod8(x, n)** : Returns the modulus of the truncated absolute value of **x** by **2** to the power of **8**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bmod16(x, n)** : Returns the modulus of the truncated absolute value of **x** by **2** to the power of **16**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bmod32(x, n)** : Returns the modulus of the truncated absolute value of **x** by **2** to the power of **32**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bmod64(x, n)** : Returns the modulus of the truncated absolute value of **x** by **2** to the power of **64**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bunrev(t)** : Assumes **t** is a bitwise-reversed number with an extra set bit one place more significant than the real most significant bit (which was the least significant bit in the original number). This number is reversed and returned without the extra set bit. This function is used to implement other bitwise functions; it is not meant to be used by users, but it can be. **plz(x)** : If **x** is not equal to **0** and greater that **-1** and less than **1**, it is printed with a leading zero, regardless of the use of the **-z** option (see the **OPTIONS** section) and without a trailing newline. Otherwise, **x** is printed normally, without a trailing newline. **plznl(x)** : If **x** is not equal to **0** and greater that **-1** and less than **1**, it is printed with a leading zero, regardless of the use of the **-z** option (see the **OPTIONS** section) and with a trailing newline. Otherwise, **x** is printed normally, with a trailing newline. **pnlz(x)** : If **x** is not equal to **0** and greater that **-1** and less than **1**, it is printed without a leading zero, regardless of the use of the **-z** option (see the **OPTIONS** section) and without a trailing newline. Otherwise, **x** is printed normally, without a trailing newline. **pnlznl(x)** : If **x** is not equal to **0** and greater that **-1** and less than **1**, it is printed without a leading zero, regardless of the use of the **-z** option (see the **OPTIONS** section) and with a trailing newline. Otherwise, **x** is printed normally, with a trailing newline. **ubytes(x)** : Returns the numbers of unsigned integer bytes required to hold the truncated absolute value of **x**. **sbytes(x)** : Returns the numbers of signed, two's-complement integer bytes required to hold the truncated value of **x**. **s2u(x)** : Returns **x** if it is non-negative. If it *is* negative, then it calculates what **x** would be as a 2's-complement signed integer and returns the non-negative integer that would have the same representation in binary. **s2un(x,n)** : Returns **x** if it is non-negative. If it *is* negative, then it calculates what **x** would be as a 2's-complement signed integer with **n** bytes and returns the non-negative integer that would have the same representation in binary. If **x** cannot fit into **n** 2's-complement signed bytes, it is truncated to fit. **hex(x)** : Outputs the hexadecimal (base **16**) representation of **x**. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **binary(x)** : Outputs the binary (base **2**) representation of **x**. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **output(x, b)** : Outputs the base **b** representation of **x**. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **uint(x)** : Outputs the representation, in binary and hexadecimal, of **x** as an unsigned integer in as few power of two bytes as possible. Both outputs are split into bytes separated by spaces. If **x** is not an integer or is negative, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **int(x)** : Outputs the representation, in binary and hexadecimal, of **x** as a signed, two's-complement integer in as few power of two bytes as possible. Both outputs are split into bytes separated by spaces. If **x** is not an integer, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **uintn(x, n)** : Outputs the representation, in binary and hexadecimal, of **x** as an unsigned integer in **n** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer, is negative, or cannot fit into **n** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **intn(x, n)** : Outputs the representation, in binary and hexadecimal, of **x** as a signed, two's-complement integer in **n** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer or cannot fit into **n** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **uint8(x)** : Outputs the representation, in binary and hexadecimal, of **x** as an unsigned integer in **1** byte. Both outputs are split into bytes separated by spaces. If **x** is not an integer, is negative, or cannot fit into **1** byte, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **int8(x)** : Outputs the representation, in binary and hexadecimal, of **x** as a signed, two's-complement integer in **1** byte. Both outputs are split into bytes separated by spaces. If **x** is not an integer or cannot fit into **1** byte, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **uint16(x)** : Outputs the representation, in binary and hexadecimal, of **x** as an unsigned integer in **2** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer, is negative, or cannot fit into **2** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **int16(x)** : Outputs the representation, in binary and hexadecimal, of **x** as a signed, two's-complement integer in **2** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer or cannot fit into **2** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **uint32(x)** : Outputs the representation, in binary and hexadecimal, of **x** as an unsigned integer in **4** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer, is negative, or cannot fit into **4** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **int32(x)** : Outputs the representation, in binary and hexadecimal, of **x** as a signed, two's-complement integer in **4** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer or cannot fit into **4** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **uint64(x)** : Outputs the representation, in binary and hexadecimal, of **x** as an unsigned integer in **8** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer, is negative, or cannot fit into **8** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **int64(x)** : Outputs the representation, in binary and hexadecimal, of **x** as a signed, two's-complement integer in **8** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer or cannot fit into **8** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **hex_uint(x, n)** : Outputs the representation of the truncated absolute value of **x** as an unsigned integer in hexadecimal using **n** bytes. Not all of the value will be output if **n** is too small. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **binary_uint(x, n)** : Outputs the representation of the truncated absolute value of **x** as an unsigned integer in binary using **n** bytes. Not all of the value will be output if **n** is too small. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **output_uint(x, n)** : Outputs the representation of the truncated absolute value of **x** as an unsigned integer in the current **obase** (see the **SYNTAX** section) using **n** bytes. Not all of the value will be output if **n** is too small. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **output_byte(x, i)** : Outputs byte **i** of the truncated absolute value of **x**, where **0** is the least significant byte and **number_of_bytes - 1** is the most significant byte. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). ## Transcendental Functions All transcendental functions can return slightly inaccurate results, up to 1 ULP (https://en.wikipedia.org/wiki/Unit_in_the_last_place). This is unavoidable, and the article at https://people.eecs.berkeley.edu/~wkahan/LOG10HAF.TXT explains why it is impossible and unnecessary to calculate exact results for the transcendental functions. Because of the possible inaccuracy, I recommend that users call those functions with the precision (**scale**) set to at least 1 higher than is necessary. If exact results are *absolutely* required, users can double the precision (**scale**) and then truncate. The transcendental functions in the standard math library are: * **s(x)** * **c(x)** * **a(x)** * **l(x)** * **e(x)** * **j(x, n)** The transcendental functions in the extended math library are: * **l2(x)** * **l10(x)** * **log(x, b)** * **pi(p)** * **t(x)** * **a2(y, x)** * **sin(x)** * **cos(x)** * **tan(x)** * **atan(x)** * **atan2(y, x)** * **r2d(x)** * **d2r(x)** # RESET When bc(1) encounters an error or a signal that it has a non-default handler for, it resets. This means that several things happen. First, any functions that are executing are stopped and popped off the stack. The behavior is not unlike that of exceptions in programming languages. Then the execution point is set so that any code waiting to execute (after all functions returned) is skipped. Thus, when bc(1) resets, it skips any remaining code waiting to be executed. Then, if it is interactive mode, and the error was not a fatal error (see the **EXIT STATUS** section), it asks for more input; otherwise, it exits with the appropriate return code. Note that this reset behavior is different from the GNU bc(1), which attempts to start executing the statement right after the one that caused an error. # PERFORMANCE Most bc(1) implementations use **char** types to calculate the value of **1** decimal digit at a time, but that can be slow. This bc(1) does something different. It uses large integers to calculate more than **1** decimal digit at a time. If built in a environment where **BC_LONG_BIT** (see the **LIMITS** section) is **64**, then each integer has **9** decimal digits. If built in an environment where **BC_LONG_BIT** is **32** then each integer has **4** decimal digits. This value (the number of decimal digits per large integer) is called **BC_BASE_DIGS**. The actual values of **BC_LONG_BIT** and **BC_BASE_DIGS** can be queried with the **limits** statement. In addition, this bc(1) uses an even larger integer for overflow checking. This integer type depends on the value of **BC_LONG_BIT**, but is always at least twice as large as the integer type used to store digits. # LIMITS The following are the limits on bc(1): **BC_LONG_BIT** : The number of bits in the **long** type in the environment where bc(1) was built. This determines how many decimal digits can be stored in a single large integer (see the **PERFORMANCE** section). **BC_BASE_DIGS** : The number of decimal digits per large integer (see the **PERFORMANCE** section). Depends on **BC_LONG_BIT**. **BC_BASE_POW** : The max decimal number that each large integer can store (see **BC_BASE_DIGS**) plus **1**. Depends on **BC_BASE_DIGS**. **BC_OVERFLOW_MAX** : The max number that the overflow type (see the **PERFORMANCE** section) can hold. Depends on **BC_LONG_BIT**. **BC_BASE_MAX** : The maximum output base. Set at **BC_BASE_POW**. **BC_DIM_MAX** : The maximum size of arrays. Set at **SIZE_MAX-1**. **BC_SCALE_MAX** : The maximum **scale**. Set at **BC_OVERFLOW_MAX-1**. **BC_STRING_MAX** : The maximum length of strings. Set at **BC_OVERFLOW_MAX-1**. **BC_NAME_MAX** : The maximum length of identifiers. Set at **BC_OVERFLOW_MAX-1**. **BC_NUM_MAX** : The maximum length of a number (in decimal digits), which includes digits after the decimal point. Set at **BC_OVERFLOW_MAX-1**. **BC_RAND_MAX** : The maximum integer (inclusive) returned by the **rand()** operand. Set at **2\^BC_LONG_BIT-1**. Exponent : The maximum allowable exponent (positive or negative). Set at **BC_OVERFLOW_MAX**. Number of vars : The maximum number of vars/arrays. Set at **SIZE_MAX-1**. The actual values can be queried with the **limits** statement. These limits are meant to be effectively non-existent; the limits are so large (at least on 64-bit machines) that there should not be any point at which they become a problem. In fact, memory should be exhausted before these limits should be hit. # ENVIRONMENT VARIABLES As **non-portable extensions**, bc(1) recognizes the following environment variables: **POSIXLY_CORRECT** : If this variable exists (no matter the contents), bc(1) behaves as if the **-s** option was given. **BC_ENV_ARGS** : This is another way to give command-line arguments to bc(1). They should be in the same format as all other command-line arguments. These are always processed first, so any files given in **BC_ENV_ARGS** will be processed before arguments and files given on the command-line. This gives the user the ability to set up "standard" options and files to be used at every invocation. The most useful thing for such files to contain would be useful functions that the user might want every time bc(1) runs. The code that parses **BC_ENV_ARGS** will correctly handle quoted arguments, but it does not understand escape sequences. For example, the string **"/home/gavin/some bc file.bc"** will be correctly parsed, but the string **"/home/gavin/some \"bc\" file.bc"** will include the backslashes. The quote parsing will handle either kind of quotes, **'** or **"**. Thus, if you have a file with any number of single quotes in the name, you can use double quotes as the outside quotes, as in **"some 'bc' file.bc"**, and vice versa if you have a file with double quotes. However, handling a file with both kinds of quotes in **BC_ENV_ARGS** is not supported due to the complexity of the parsing, though such files are still supported on the command-line where the parsing is done by the shell. **BC_LINE_LENGTH** : If this environment variable exists and contains an integer that is greater than **1** and is less than **UINT16_MAX** (**2\^16-1**), bc(1) will output lines to that length, including the backslash (**\\**). The default line length is **70**. The special value of **0** will disable line length checking and print numbers without regard to line length and without backslashes and newlines. **BC_BANNER** : If this environment variable exists and contains an integer, then a non-zero value activates the copyright banner when bc(1) is in interactive mode, while zero deactivates it. If bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section), then this environment variable has no effect because bc(1) does not print the banner when not in interactive mode. This environment variable overrides the default, which can be queried with the **-h** or **-\-help** options. **BC_SIGINT_RESET** : If bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section), then this environment variable has no effect because bc(1) exits on **SIGINT** when not in interactive mode. However, when bc(1) is in interactive mode, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) reset on **SIGINT**, rather than exit, and zero makes bc(1) exit. If this environment variable exists and is *not* an integer, then bc(1) will exit on **SIGINT**. This environment variable overrides the default, which can be queried with the **-h** or **-\-help** options. **BC_TTY_MODE** : If TTY mode is *not* available (see the **TTY MODE** section), then this environment variable has no effect. However, when TTY mode is available, then if this environment variable exists and contains an integer, then a non-zero value makes bc(1) use TTY mode, and zero makes bc(1) not use TTY mode. This environment variable overrides the default, which can be queried with the **-h** or **-\-help** options. **BC_PROMPT** : If TTY mode is *not* available (see the **TTY MODE** section), then this environment variable has no effect. However, when TTY mode is available, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) use a prompt, and zero or a non-integer makes bc(1) not use a prompt. If this environment variable does not exist and **BC_TTY_MODE** does, then the value of the **BC_TTY_MODE** environment variable is used. This environment variable and the **BC_TTY_MODE** environment variable override the default, which can be queried with the **-h** or **-\-help** options. **BC_EXPR_EXIT** : If any expressions or expression files are given on the command-line with **-e**, **-\-expression**, **-f**, or **-\-file**, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) exit after executing the expressions and expression files, and a zero value makes bc(1) not exit. This environment variable overrides the default, which can be queried with the **-h** or **-\-help** options. **BC_DIGIT_CLAMP** : When parsing numbers and if this environment variable exists and contains an integer, a non-zero value makes bc(1) clamp digits that are greater than or equal to the current **ibase** so that all such digits are considered equal to the **ibase** minus 1, and a zero value disables such clamping so that those digits are always equal to their value, which is multiplied by the power of the **ibase**. This never applies to single-digit numbers, as per the standard (see the **STANDARDS** section). This environment variable overrides the default, which can be queried with the **-h** or **-\-help** options. # EXIT STATUS bc(1) returns the following exit statuses: **0** : No error. **1** : A math error occurred. This follows standard practice of using **1** for expected errors, since math errors will happen in the process of normal execution. Math errors include divide by **0**, taking the square root of a negative number, using a negative number as a bound for the pseudo-random number generator, attempting to convert a negative number to a hardware integer, overflow when converting a number to a hardware integer, overflow when calculating the size of a number, and attempting to use a non-integer where an integer is required. Converting to a hardware integer happens for the second operand of the power (**\^**), places (**\@**), left shift (**\<\<**), and right shift (**\>\>**) operators and their corresponding assignment operators. **2** : A parse error occurred. Parse errors include unexpected **EOF**, using an invalid character, failing to find the end of a string or comment, using a token where it is invalid, giving an invalid expression, giving an invalid print statement, giving an invalid function definition, attempting to assign to an expression that is not a named expression (see the *Named Expressions* subsection of the **SYNTAX** section), giving an invalid **auto** list, having a duplicate **auto**/function parameter, failing to find the end of a code block, attempting to return a value from a **void** function, attempting to use a variable as a reference, and using any extensions when the option **-s** or any equivalents were given. **3** : A runtime error occurred. Runtime errors include assigning an invalid number to any global (**ibase**, **obase**, or **scale**), giving a bad expression to a **read()** call, calling **read()** inside of a **read()** call, type errors, passing the wrong number of arguments to functions, attempting to call an undefined function, and attempting to use a **void** function call as a value in an expression. **4** : A fatal error occurred. Fatal errors include memory allocation errors, I/O errors, failing to open files, attempting to use files that do not have only ASCII characters (bc(1) only accepts ASCII characters), attempting to open a directory as a file, and giving invalid command-line options. The exit status **4** is special; when a fatal error occurs, bc(1) always exits and returns **4**, no matter what mode bc(1) is in. The other statuses will only be returned when bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section), since bc(1) resets its state (see the **RESET** section) and accepts more input when one of those errors occurs in interactive mode. This is also the case when interactive mode is forced by the **-i** flag or **-\-interactive** option. These exit statuses allow bc(1) to be used in shell scripting with error checking, and its normal behavior can be forced by using the **-i** flag or **-\-interactive** option. # INTERACTIVE MODE Per the standard (see the **STANDARDS** section), bc(1) has an interactive mode and a non-interactive mode. Interactive mode is turned on automatically when both **stdin** and **stdout** are hooked to a terminal, but the **-i** flag and **-\-interactive** option can turn it on in other situations. In interactive mode, bc(1) attempts to recover from errors (see the **RESET** section), and in normal execution, flushes **stdout** as soon as execution is done for the current input. bc(1) may also reset on **SIGINT** instead of exit, depending on the contents of, or default for, the **BC_SIGINT_RESET** environment variable (see the **ENVIRONMENT VARIABLES** section). # TTY MODE If **stdin**, **stdout**, and **stderr** are all connected to a TTY, then "TTY mode" is considered to be available, and thus, bc(1) can turn on TTY mode, subject to some settings. If there is the environment variable **BC_TTY_MODE** in the environment (see the **ENVIRONMENT VARIABLES** section), then if that environment variable contains a non-zero integer, bc(1) will turn on TTY mode when **stdin**, **stdout**, and **stderr** are all connected to a TTY. If the **BC_TTY_MODE** environment variable exists but is *not* a non-zero integer, then bc(1) will not turn TTY mode on. If the environment variable **BC_TTY_MODE** does *not* exist, the default setting is used. The default setting can be queried with the **-h** or **-\-help** options. TTY mode is different from interactive mode because interactive mode is required in the bc(1) standard (see the **STANDARDS** section), and interactive mode requires only **stdin** and **stdout** to be connected to a terminal. ## Command-Line History Command-line history is only enabled if TTY mode is, i.e., that **stdin**, **stdout**, and **stderr** are connected to a TTY and the **BC_TTY_MODE** environment variable (see the **ENVIRONMENT VARIABLES** section) and its default do not disable TTY mode. See the **COMMAND LINE HISTORY** section for more information. ## Prompt If TTY mode is available, then a prompt can be enabled. Like TTY mode itself, it can be turned on or off with an environment variable: **BC_PROMPT** (see the **ENVIRONMENT VARIABLES** section). If the environment variable **BC_PROMPT** exists and is a non-zero integer, then the prompt is turned on when **stdin**, **stdout**, and **stderr** are connected to a TTY and the **-P** and **-\-no-prompt** options were not used. The read prompt will be turned on under the same conditions, except that the **-R** and **-\-no-read-prompt** options must also not be used. However, if **BC_PROMPT** does not exist, the prompt can be enabled or disabled with the **BC_TTY_MODE** environment variable, the **-P** and **-\-no-prompt** options, and the **-R** and **-\-no-read-prompt** options. See the **ENVIRONMENT VARIABLES** and **OPTIONS** sections for more details. # SIGNAL HANDLING Sending a **SIGINT** will cause bc(1) to do one of two things. If bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section), or the **BC_SIGINT_RESET** environment variable (see the **ENVIRONMENT VARIABLES** section), or its default, is either not an integer or it is zero, bc(1) will exit. However, if bc(1) is in interactive mode, and the **BC_SIGINT_RESET** or its default is an integer and non-zero, then bc(1) will stop executing the current input and reset (see the **RESET** section) upon receiving a **SIGINT**. Note that "current input" can mean one of two things. If bc(1) is processing input from **stdin** in interactive mode, it will ask for more input. If bc(1) is processing input from a file in interactive mode, it will stop processing the file and start processing the next file, if one exists, or ask for input from **stdin** if no other file exists. This means that if a **SIGINT** is sent to bc(1) as it is executing a file, it can seem as though bc(1) did not respond to the signal since it will immediately start executing the next file. This is by design; most files that users execute when interacting with bc(1) have function definitions, which are quick to parse. If a file takes a long time to execute, there may be a bug in that file. The rest of the files could still be executed without problem, allowing the user to continue. **SIGTERM** and **SIGQUIT** cause bc(1) to clean up and exit, and it uses the default handler for all other signals. The one exception is **SIGHUP**; in that case, and only when bc(1) is in TTY mode (see the **TTY MODE** section), a **SIGHUP** will cause bc(1) to clean up and exit. # COMMAND LINE HISTORY bc(1) supports interactive command-line editing. If bc(1) can be in TTY mode (see the **TTY MODE** section), history can be enabled. This means that command-line history can only be enabled when **stdin**, **stdout**, and **stderr** are all connected to a TTY. Like TTY mode itself, it can be turned on or off with the environment variable **BC_TTY_MODE** (see the **ENVIRONMENT VARIABLES** section). If history is enabled, previous lines can be recalled and edited with the arrow keys. **Note**: tabs are converted to 8 spaces. # LOCALES This bc(1) ships with support for adding error messages for different locales and thus, supports **LC_MESSAGES**. # SEE ALSO dc(1) # STANDARDS bc(1) is compliant with the IEEE Std 1003.1-2017 (“POSIX.1-2017”) specification at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html . The flags **-efghiqsvVw**, all long options, and the extensions noted above are extensions to that specification. In addition, the behavior of the **quit** implements an interpretation of that specification that is different from all known implementations. For more information see the **Statements** subsection of the **SYNTAX** section. Note that the specification explicitly says that bc(1) only accepts numbers that use a period (**.**) as a radix point, regardless of the value of **LC_NUMERIC**. This bc(1) supports error messages for different locales, and thus, it supports **LC_MESSAGES**. # BUGS Before version **6.1.0**, this bc(1) had incorrect behavior for the **quit** statement. No other bugs are known. Report bugs at https://git.gavinhoward.com/gavin/bc . # AUTHORS Gavin D. Howard and contributors. diff --git a/contrib/bc/manuals/bc/E.1 b/contrib/bc/manuals/bc/E.1 index 24f49c701b46..549c1adae8a1 100644 --- a/contrib/bc/manuals/bc/E.1 +++ b/contrib/bc/manuals/bc/E.1 @@ -1,1816 +1,1760 @@ .\" .\" SPDX-License-Identifier: BSD-2-Clause .\" .\" Copyright (c) 2018-2023 Gavin D. Howard and contributors. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions are met: .\" .\" * Redistributions of source code must retain the above copyright notice, .\" this list of conditions and the following disclaimer. .\" .\" * Redistributions in binary form must reproduce the above copyright notice, .\" this list of conditions and the following disclaimer in the documentation .\" and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.TH "BC" "1" "February 2023" "Gavin D. Howard" "General Commands Manual" +.TH "BC" "1" "November 2023" "Gavin D. Howard" "General Commands Manual" .nh .ad l .SH NAME -.PP bc - arbitrary-precision decimal arithmetic language and calculator .SH SYNOPSIS -.PP \f[B]bc\f[R] [\f[B]-cCghilPqRsvVw\f[R]] [\f[B]--digit-clamp\f[R]] [\f[B]--no-digit-clamp\f[R]] [\f[B]--global-stacks\f[R]] [\f[B]--help\f[R]] [\f[B]--interactive\f[R]] [\f[B]--mathlib\f[R]] [\f[B]--no-prompt\f[R]] [\f[B]--no-read-prompt\f[R]] [\f[B]--quiet\f[R]] [\f[B]--standard\f[R]] [\f[B]--warn\f[R]] [\f[B]--version\f[R]] [\f[B]-e\f[R] \f[I]expr\f[R]] [\f[B]--expression\f[R]=\f[I]expr\f[R]\&...] [\f[B]-f\f[R] \f[I]file\f[R]\&...] [\f[B]--file\f[R]=\f[I]file\f[R]\&...] [\f[I]file\f[R]\&...] .SH DESCRIPTION -.PP bc(1) is an interactive processor for a language first standardized in 1991 by POSIX. (See the \f[B]STANDARDS\f[R] section.) The language provides unlimited precision decimal arithmetic and is somewhat C-like, but there are differences. Such differences will be noted in this document. .PP After parsing and handling options, this bc(1) reads any files given on the command line and executes them before reading from \f[B]stdin\f[R]. .PP This bc(1) is a drop-in replacement for \f[I]any\f[R] bc(1), including (and especially) the GNU bc(1). It also has many extensions and extra features beyond other implementations. .PP \f[B]Note\f[R]: If running this bc(1) on \f[I]any\f[R] script meant for another bc(1) gives a parse error, it is probably because a word this bc(1) reserves as a keyword is used as the name of a function, variable, or array. To fix that, use the command-line option \f[B]-r\f[R] \f[I]keyword\f[R], where \f[I]keyword\f[R] is the keyword that is used as a name in the script. For more information, see the \f[B]OPTIONS\f[R] section. .PP If parsing scripts meant for other bc(1) implementations still does not work, that is a bug and should be reported. See the \f[B]BUGS\f[R] section. .SH OPTIONS -.PP The following are the options that bc(1) accepts. .TP \f[B]-C\f[R], \f[B]--no-digit-clamp\f[R] Disables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that the value added to a number from a digit is always that digit\[cq]s value multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-c\f[R] or \f[B]--digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-c\f[R], \f[B]--digit-clamp\f[R] Enables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that digits that the value added to a number from a digit that is greater than or equal to the ibase is the value of ibase minus 1 all multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-C\f[R] or \f[B]--no-digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-e\f[R] \f[I]expr\f[R], \f[B]--expression\f[R]=\f[I]expr\f[R] Evaluates \f[I]expr\f[R]. If multiple expressions are given, they are evaluated in order. If files are given as well (see the \f[B]-f\f[R] and \f[B]--file\f[R] options), the expressions and files are evaluated in the order given. This means that if a file is given before an expression, the file is read in and evaluated first. .RS .PP If this option is given on the command-line (i.e., not in \f[B]BC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, bc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R], whether on the command-line or in \f[B]BC_ENV_ARGS\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, bc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-f\f[R] \f[I]file\f[R], \f[B]--file\f[R]=\f[I]file\f[R] Reads in \f[I]file\f[R] and evaluates it, line by line, as though it were read through \f[B]stdin\f[R]. If expressions are also given (see the \f[B]-e\f[R] and \f[B]--expression\f[R] options), the expressions are evaluated in the order given. .RS .PP If this option is given on the command-line (i.e., not in \f[B]BC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, bc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, bc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-g\f[R], \f[B]--global-stacks\f[R] Turns the globals \f[B]ibase\f[R], \f[B]obase\f[R], and \f[B]scale\f[R] into stacks. .RS .PP This has the effect that a copy of the current value of all three are pushed onto a stack for every function call, as well as popped when every function returns. This means that functions can assign to any and all of those globals without worrying that the change will affect other functions. Thus, a hypothetical function named \f[B]output(x,b)\f[R] that simply printed \f[B]x\f[R] in base \f[B]b\f[R] could be written like this: .IP -.nf -\f[C] +.EX define void output(x, b) { obase=b x } -\f[R] -.fi +.EE .PP instead of like this: .IP -.nf -\f[C] +.EX define void output(x, b) { auto c c=obase obase=b x obase=c } -\f[R] -.fi +.EE .PP This makes writing functions much easier. .PP However, since using this flag means that functions cannot set \f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R] globally, functions that are made to do so cannot work anymore. There are two possible use cases for that, and each has a solution. .PP First, if a function is called on startup to turn bc(1) into a number converter, it is possible to replace that capability with various shell aliases. Examples: .IP -.nf -\f[C] +.EX alias d2o=\[dq]bc -e ibase=A -e obase=8\[dq] alias h2b=\[dq]bc -e ibase=G -e obase=2\[dq] -\f[R] -.fi +.EE .PP Second, if the purpose of a function is to set \f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R] globally for any other purpose, it could be split into one to three functions (based on how many globals it sets) and each of those functions could return the desired value for a global. .PP If the behavior of this option is desired for every run of bc(1), then users could make sure to define \f[B]BC_ENV_ARGS\f[R] and include this option (see the \f[B]ENVIRONMENT VARIABLES\f[R] section for more details). .PP If \f[B]-s\f[R], \f[B]-w\f[R], or any equivalents are used, this option is ignored. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-h\f[R], \f[B]--help\f[R] Prints a usage message and exits. .TP \f[B]-I\f[R] \f[I]ibase\f[R], \f[B]--ibase\f[R]=\f[I]ibase\f[R] Sets the builtin variable \f[B]ibase\f[R] to the value \f[I]ibase\f[R] assuming that \f[I]ibase\f[R] is in base 10. It is a fatal error if \f[I]ibase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-i\f[R], \f[B]--interactive\f[R] Forces interactive mode. (See the \f[B]INTERACTIVE MODE\f[R] section.) .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-L\f[R], \f[B]--no-line-length\f[R] Disables line length checking and prints numbers without backslashes and newlines. In other words, this option sets \f[B]BC_LINE_LENGTH\f[R] to \f[B]0\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-l\f[R], \f[B]--mathlib\f[R] Sets \f[B]scale\f[R] (see the \f[B]SYNTAX\f[R] section) to \f[B]20\f[R] and loads the included math library before running any code, including any expressions or files specified on the command line. .RS .PP To learn what is in the library, see the \f[B]LIBRARY\f[R] section. .RE .TP \f[B]-O\f[R] \f[I]obase\f[R], \f[B]--obase\f[R]=\f[I]obase\f[R] Sets the builtin variable \f[B]obase\f[R] to the value \f[I]obase\f[R] assuming that \f[I]obase\f[R] is in base 10. It is a fatal error if \f[I]obase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-P\f[R], \f[B]--no-prompt\f[R] Disables the prompt in TTY mode. (The prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a prompt or are not used to having them in bc(1). Most of those users would want to put this option in \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .RS .PP These options override the \f[B]BC_PROMPT\f[R] and \f[B]BC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-q\f[R], \f[B]--quiet\f[R] This option is for compatibility with the GNU bc(1) (https://www.gnu.org/software/bc/); it is a no-op. Without this option, GNU bc(1) prints a copyright header. This bc(1) only prints the copyright header if one or more of the \f[B]-v\f[R], \f[B]-V\f[R], or \f[B]--version\f[R] options are given unless the \f[B]BC_BANNER\f[R] environment variable is set and contains a non-zero integer or if this bc(1) was built with the header displayed by default. If \f[I]any\f[R] of that is the case, then this option \f[I]does\f[R] prevent bc(1) from printing the header. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-R\f[R], \f[B]--no-read-prompt\f[R] Disables the read prompt in TTY mode. (The read prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a read prompt or are not used to having them in bc(1). Most of those users would want to put this option in \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). This option is also useful in hash bang lines of bc(1) scripts that prompt for user input. .RS .PP This option does not disable the regular prompt because the read prompt is only used when the \f[B]read()\f[R] built-in function is called. .PP These options \f[I]do\f[R] override the \f[B]BC_PROMPT\f[R] and \f[B]BC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), but only for the read prompt. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-r\f[R] \f[I]keyword\f[R], \f[B]--redefine\f[R]=\f[I]keyword\f[R] Redefines \f[I]keyword\f[R] in order to allow it to be used as a function, variable, or array name. This is useful when this bc(1) gives parse errors when parsing scripts meant for other bc(1) implementations. .RS .PP The keywords this bc(1) allows to be redefined are: .IP \[bu] 2 \f[B]abs\f[R] .IP \[bu] 2 \f[B]asciify\f[R] .IP \[bu] 2 \f[B]continue\f[R] .IP \[bu] 2 \f[B]divmod\f[R] .IP \[bu] 2 \f[B]else\f[R] .IP \[bu] 2 \f[B]halt\f[R] .IP \[bu] 2 \f[B]last\f[R] .IP \[bu] 2 \f[B]limits\f[R] .IP \[bu] 2 \f[B]maxibase\f[R] .IP \[bu] 2 \f[B]maxobase\f[R] .IP \[bu] 2 \f[B]maxscale\f[R] .IP \[bu] 2 \f[B]modexp\f[R] .IP \[bu] 2 \f[B]print\f[R] .IP \[bu] 2 \f[B]read\f[R] .IP \[bu] 2 \f[B]stream\f[R] .PP If any of those keywords are used as a function, variable, or array name in a script, use this option with the keyword as the argument. If multiple are used, use this option for all of them; it can be used multiple times. .PP Keywords are \f[I]not\f[R] redefined when parsing the builtin math library (see the \f[B]LIBRARY\f[R] section). .PP It is a fatal error to redefine keywords mandated by the POSIX standard (see the \f[B]STANDARDS\f[R] section). It is a fatal error to attempt to redefine words that this bc(1) does not reserve as keywords. .RE .TP \f[B]-S\f[R] \f[I]scale\f[R], \f[B]--scale\f[R]=\f[I]scale\f[R] Sets the builtin variable \f[B]scale\f[R] to the value \f[I]scale\f[R] assuming that \f[I]scale\f[R] is in base 10. It is a fatal error if \f[I]scale\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-s\f[R], \f[B]--standard\f[R] Process exactly the language defined by the standard (see the \f[B]STANDARDS\f[R] section) and error if any extensions are used. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-v\f[R], \f[B]-V\f[R], \f[B]--version\f[R] Print the version information (copyright header) and exits. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-w\f[R], \f[B]--warn\f[R] Like \f[B]-s\f[R] and \f[B]--standard\f[R], except that warnings (and not errors) are printed for non-standard extensions and execution continues normally. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-z\f[R], \f[B]--leading-zeroes\f[R] Makes bc(1) print all numbers greater than \f[B]-1\f[R] and less than \f[B]1\f[R], and not equal to \f[B]0\f[R], with a leading zero. .RS .PP This can be set for individual numbers with the \f[B]plz(x)\f[R], \f[B]plznl(x)\f[R], \f[B]pnlz(x)\f[R], and \f[B]pnlznl(x)\f[R] functions in the extended math library (see the \f[B]LIBRARY\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .PP All long options are \f[B]non-portable extensions\f[R]. .SH STDIN -.PP If no files or expressions are given by the \f[B]-f\f[R], \f[B]--file\f[R], \f[B]-e\f[R], or \f[B]--expression\f[R] options, then bc(1) reads from \f[B]stdin\f[R]. .PP However, there are a few caveats to this. .PP First, \f[B]stdin\f[R] is evaluated a line at a time. The only exception to this is if the parse cannot complete. That means that starting a string without ending it or starting a function, \f[B]if\f[R] statement, or loop without ending it will also cause bc(1) to not execute. .PP Second, after an \f[B]if\f[R] statement, bc(1) doesn\[cq]t know if an \f[B]else\f[R] statement will follow, so it will not execute until it knows there will not be an \f[B]else\f[R] statement. .SH STDOUT -.PP Any non-error output is written to \f[B]stdout\f[R]. In addition, if history (see the \f[B]HISTORY\f[R] section) and the prompt (see the \f[B]TTY MODE\f[R] section) are enabled, both are output to \f[B]stdout\f[R]. .PP \f[B]Note\f[R]: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stdout\f[R], so if \f[B]stdout\f[R] is closed, as in -\f[B]bc >&-\f[R], it will quit with an error. +\f[B]bc >&-\f[R], it will quit with an error. This is done so that bc(1) can report problems when \f[B]stdout\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stdout\f[R] to \f[B]/dev/null\f[R]. .SH STDERR -.PP Any error output is written to \f[B]stderr\f[R]. .PP \f[B]Note\f[R]: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stderr\f[R], so if \f[B]stderr\f[R] is closed, as in -\f[B]bc 2>&-\f[R], it will quit with an error. +\f[B]bc 2>&-\f[R], it will quit with an error. This is done so that bc(1) can exit with an error code when \f[B]stderr\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stderr\f[R] to \f[B]/dev/null\f[R]. .SH SYNTAX -.PP The syntax for bc(1) programs is mostly C-like, with some differences. This bc(1) follows the POSIX standard (see the \f[B]STANDARDS\f[R] section), which is a much more thorough resource for the language this bc(1) accepts. This section is meant to be a summary and a listing of all the extensions to the standard. .PP In the sections below, \f[B]E\f[R] means expression, \f[B]S\f[R] means statement, and \f[B]I\f[R] means identifier. .PP Identifiers (\f[B]I\f[R]) start with a lowercase letter and can be followed by any number (up to \f[B]BC_NAME_MAX-1\f[R]) of lowercase letters (\f[B]a-z\f[R]), digits (\f[B]0-9\f[R]), and underscores (\f[B]_\f[R]). The regex is \f[B][a-z][a-z0-9_]*\f[R]. Identifiers with more than one character (letter) are a \f[B]non-portable extension\f[R]. .PP \f[B]ibase\f[R] is a global variable determining how to interpret constant numbers. It is the \[lq]input\[rq] base, or the number base used for interpreting input numbers. \f[B]ibase\f[R] is initially \f[B]10\f[R]. If the \f[B]-s\f[R] (\f[B]--standard\f[R]) and \f[B]-w\f[R] (\f[B]--warn\f[R]) flags were not given on the command line, the max allowable value for \f[B]ibase\f[R] is \f[B]36\f[R]. Otherwise, it is \f[B]16\f[R]. The min allowable value for \f[B]ibase\f[R] is \f[B]2\f[R]. The max allowable value for \f[B]ibase\f[R] can be queried in bc(1) programs with the \f[B]maxibase()\f[R] built-in function. .PP \f[B]obase\f[R] is a global variable determining how to output results. It is the \[lq]output\[rq] base, or the number base used for outputting numbers. \f[B]obase\f[R] is initially \f[B]10\f[R]. The max allowable value for \f[B]obase\f[R] is \f[B]BC_BASE_MAX\f[R] and can be queried in bc(1) programs with the \f[B]maxobase()\f[R] built-in function. The min allowable value for \f[B]obase\f[R] is \f[B]2\f[R]. Values are output in the specified base. .PP The \f[I]scale\f[R] of an expression is the number of digits in the result of the expression right of the decimal point, and \f[B]scale\f[R] is a global variable that sets the precision of any operations, with exceptions. \f[B]scale\f[R] is initially \f[B]0\f[R]. \f[B]scale\f[R] cannot be negative. The max allowable value for \f[B]scale\f[R] is \f[B]BC_SCALE_MAX\f[R] and can be queried in bc(1) programs with the \f[B]maxscale()\f[R] built-in function. .PP bc(1) has both \f[I]global\f[R] variables and \f[I]local\f[R] variables. All \f[I]local\f[R] variables are local to the function; they are parameters or are introduced in the \f[B]auto\f[R] list of a function (see the \f[B]FUNCTIONS\f[R] section). If a variable is accessed which is not a parameter or in the \f[B]auto\f[R] list, it is assumed to be \f[I]global\f[R]. If a parent function has a \f[I]local\f[R] variable version of a variable that a child function considers \f[I]global\f[R], the value of that \f[I]global\f[R] variable in the child function is the value of the variable in the parent function, not the value of the actual \f[I]global\f[R] variable. .PP All of the above applies to arrays as well. .PP The value of a statement that is an expression (i.e., any of the named expressions or operands) is printed unless the lowest precedence operator is an assignment operator \f[I]and\f[R] the expression is notsurrounded by parentheses. .PP The value that is printed is also assigned to the special variable \f[B]last\f[R]. A single dot (\f[B].\f[R]) may also be used as a synonym for \f[B]last\f[R]. These are \f[B]non-portable extensions\f[R]. .PP Either semicolons or newlines may separate statements. .SS Comments -.PP There are two kinds of comments: .IP "1." 3 Block comments are enclosed in \f[B]/*\f[R] and \f[B]*/\f[R]. .IP "2." 3 Line comments go from \f[B]#\f[R] until, and not including, the next newline. This is a \f[B]non-portable extension\f[R]. .SS Named Expressions -.PP The following are named expressions in bc(1): .IP "1." 3 Variables: \f[B]I\f[R] .IP "2." 3 Array Elements: \f[B]I[E]\f[R] .IP "3." 3 \f[B]ibase\f[R] .IP "4." 3 \f[B]obase\f[R] .IP "5." 3 \f[B]scale\f[R] .IP "6." 3 \f[B]last\f[R] or a single dot (\f[B].\f[R]) .PP Number 6 is a \f[B]non-portable extension\f[R]. .PP Variables and arrays do not interfere; users can have arrays named the same as variables. This also applies to functions (see the \f[B]FUNCTIONS\f[R] section), so a user can have a variable, array, and function that all have the same name, and they will not shadow each other, whether inside of functions or not. .PP Named expressions are required as the operand of \f[B]increment\f[R]/\f[B]decrement\f[R] operators and as the left side of \f[B]assignment\f[R] operators (see the \f[I]Operators\f[R] subsection). .SS Operands -.PP The following are valid operands in bc(1): .IP " 1." 4 Numbers (see the \f[I]Numbers\f[R] subsection below). .IP " 2." 4 Array indices (\f[B]I[E]\f[R]). .IP " 3." 4 \f[B](E)\f[R]: The value of \f[B]E\f[R] (used to change precedence). .IP " 4." 4 \f[B]sqrt(E)\f[R]: The square root of \f[B]E\f[R]. \f[B]E\f[R] must be non-negative. .IP " 5." 4 \f[B]length(E)\f[R]: The number of significant decimal digits in \f[B]E\f[R]. Returns \f[B]1\f[R] for \f[B]0\f[R] with no decimal places. If given a string, the length of the string is returned. Passing a string to \f[B]length(E)\f[R] is a \f[B]non-portable extension\f[R]. .IP " 6." 4 \f[B]length(I[])\f[R]: The number of elements in the array \f[B]I\f[R]. This is a \f[B]non-portable extension\f[R]. .IP " 7." 4 \f[B]scale(E)\f[R]: The \f[I]scale\f[R] of \f[B]E\f[R]. .IP " 8." 4 \f[B]abs(E)\f[R]: The absolute value of \f[B]E\f[R]. This is a \f[B]non-portable extension\f[R]. .IP " 9." 4 \f[B]is_number(E)\f[R]: \f[B]1\f[R] if the given argument is a number, \f[B]0\f[R] if it is a string. This is a \f[B]non-portable extension\f[R]. .IP "10." 4 \f[B]is_string(E)\f[R]: \f[B]1\f[R] if the given argument is a string, \f[B]0\f[R] if it is a number. This is a \f[B]non-portable extension\f[R]. .IP "11." 4 \f[B]modexp(E, E, E)\f[R]: Modular exponentiation, where the first expression is the base, the second is the exponent, and the third is the modulus. All three values must be integers. The second argument must be non-negative. The third argument must be non-zero. This is a \f[B]non-portable extension\f[R]. .IP "12." 4 \f[B]divmod(E, E, I[])\f[R]: Division and modulus in one operation. This is for optimization. The first expression is the dividend, and the second is the divisor, which must be non-zero. The return value is the quotient, and the modulus is stored in index \f[B]0\f[R] of the provided array (the last argument). This is a \f[B]non-portable extension\f[R]. .IP "13." 4 \f[B]asciify(E)\f[R]: If \f[B]E\f[R] is a string, returns a string that is the first letter of its argument. If it is a number, calculates the number mod \f[B]256\f[R] and returns that number as a one-character string. This is a \f[B]non-portable extension\f[R]. .IP "14." 4 \f[B]asciify(I[])\f[R]: A string that is made up of the characters that would result from running \f[B]asciify(E)\f[R] on each element of the array identified by the argument. This allows creating multi-character strings and storing them. This is a \f[B]non-portable extension\f[R]. .IP "15." 4 \f[B]I()\f[R], \f[B]I(E)\f[R], \f[B]I(E, E)\f[R], and so on, where \f[B]I\f[R] is an identifier for a non-\f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). The \f[B]E\f[R] argument(s) may also be arrays of the form \f[B]I[]\f[R], which will automatically be turned into array references (see the \f[I]Array References\f[R] subsection of the \f[B]FUNCTIONS\f[R] section) if the corresponding parameter in the function definition is an array reference. .IP "16." 4 \f[B]read()\f[R]: Reads a line from \f[B]stdin\f[R] and uses that as an expression. The result of that expression is the result of the \f[B]read()\f[R] operand. This is a \f[B]non-portable extension\f[R]. .IP "17." 4 \f[B]maxibase()\f[R]: The max allowable \f[B]ibase\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "18." 4 \f[B]maxobase()\f[R]: The max allowable \f[B]obase\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "19." 4 \f[B]maxscale()\f[R]: The max allowable \f[B]scale\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "20." 4 \f[B]line_length()\f[R]: The line length set with \f[B]BC_LINE_LENGTH\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). This is a \f[B]non-portable extension\f[R]. .IP "21." 4 \f[B]global_stacks()\f[R]: \f[B]0\f[R] if global stacks are not enabled with the \f[B]-g\f[R] or \f[B]--global-stacks\f[R] options, non-zero otherwise. See the \f[B]OPTIONS\f[R] section. This is a \f[B]non-portable extension\f[R]. .IP "22." 4 \f[B]leading_zero()\f[R]: \f[B]0\f[R] if leading zeroes are not enabled with the \f[B]-z\f[R] or \f[B]\[en]leading-zeroes\f[R] options, non-zero otherwise. See the \f[B]OPTIONS\f[R] section. This is a \f[B]non-portable extension\f[R]. .SS Numbers -.PP Numbers are strings made up of digits, uppercase letters, and at most \f[B]1\f[R] period for a radix. Numbers can have up to \f[B]BC_NUM_MAX\f[R] digits. Uppercase letters are equal to \f[B]9\f[R] plus their position in the alphabet, starting from \f[B]1\f[R] (i.e., \f[B]A\f[R] equals \f[B]10\f[R], or \f[B]9+1\f[R]). .PP If a digit or letter makes no sense with the current value of \f[B]ibase\f[R] (i.e., they are greater than or equal to the current value of \f[B]ibase\f[R]), then the behavior depends on the existence of the \f[B]-c\f[R]/\f[B]--digit-clamp\f[R] or \f[B]-C\f[R]/\f[B]--no-digit-clamp\f[R] options (see the \f[B]OPTIONS\f[R] section), the existence and setting of the \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or the default, which can be queried with the \f[B]-h\f[R]/\f[B]--help\f[R] option. .PP If clamping is off, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are not changed. Instead, their given value is multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*A+3\[ha]0*B\f[R], which is \f[B]3\f[R] times \f[B]10\f[R] plus \f[B]11\f[R], or \f[B]41\f[R]. .PP If clamping is on, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are set to the value of the highest valid digit in \f[B]ibase\f[R] before being multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*2+3\[ha]0*2\f[R], which is \f[B]3\f[R] times \f[B]2\f[R] plus \f[B]2\f[R], or \f[B]8\f[R]. .PP There is one exception to clamping: single-character numbers (i.e., \f[B]A\f[R] alone). Such numbers are never clamped and always take the value they would have in the highest possible \f[B]ibase\f[R]. This means that \f[B]A\f[R] alone always equals decimal \f[B]10\f[R] and \f[B]Z\f[R] alone always equals decimal \f[B]35\f[R]. This behavior is mandated by the standard (see the STANDARDS section) and is meant to provide an easy way to set the current \f[B]ibase\f[R] (with the \f[B]i\f[R] command) regardless of the current value of \f[B]ibase\f[R]. .PP If clamping is on, and the clamped value of a character is needed, use a leading zero, i.e., for \f[B]A\f[R], use \f[B]0A\f[R]. .SS Operators -.PP The following arithmetic and logical operators can be used. They are listed in order of decreasing precedence. Operators in the same group have the same precedence. .TP \f[B]++\f[R] \f[B]--\f[R] Type: Prefix and Postfix .RS .PP Associativity: None .PP Description: \f[B]increment\f[R], \f[B]decrement\f[R] .RE .TP \f[B]-\f[R] \f[B]!\f[R] Type: Prefix .RS .PP Associativity: None .PP Description: \f[B]negation\f[R], \f[B]boolean not\f[R] .RE .TP \f[B]\[ha]\f[R] Type: Binary .RS .PP Associativity: Right .PP Description: \f[B]power\f[R] .RE .TP \f[B]*\f[R] \f[B]/\f[R] \f[B]%\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]multiply\f[R], \f[B]divide\f[R], \f[B]modulus\f[R] .RE .TP \f[B]+\f[R] \f[B]-\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]add\f[R], \f[B]subtract\f[R] .RE .TP \f[B]=\f[R] \f[B]+=\f[R] \f[B]-=\f[R] \f[B]*=\f[R] \f[B]/=\f[R] \f[B]%=\f[R] \f[B]\[ha]=\f[R] Type: Binary .RS .PP Associativity: Right .PP Description: \f[B]assignment\f[R] .RE .TP \f[B]==\f[R] \f[B]<=\f[R] \f[B]>=\f[R] \f[B]!=\f[R] \f[B]<\f[R] \f[B]>\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]relational\f[R] .RE .TP \f[B]&&\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]boolean and\f[R] .RE .TP \f[B]||\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]boolean or\f[R] .RE .PP The operators will be described in more detail below. .TP \f[B]++\f[R] \f[B]--\f[R] The prefix and postfix \f[B]increment\f[R] and \f[B]decrement\f[R] -operators behave exactly like they would in C. -They require a named expression (see the \f[I]Named Expressions\f[R] -subsection) as an operand. +operators behave exactly like they would in C. They require a named +expression (see the \f[I]Named Expressions\f[R] subsection) as an +operand. .RS .PP The prefix versions of these operators are more efficient; use them where possible. .RE .TP \f[B]-\f[R] The \f[B]negation\f[R] operator returns \f[B]0\f[R] if a user attempts to negate any expression with the value \f[B]0\f[R]. Otherwise, a copy of the expression with its sign flipped is returned. .TP \f[B]!\f[R] The \f[B]boolean not\f[R] operator returns \f[B]1\f[R] if the expression is \f[B]0\f[R], or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[ha]\f[R] The \f[B]power\f[R] operator (not the \f[B]exclusive or\f[R] operator, as it would be in C) takes two expressions and raises the first to the power of the value of the second. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The second expression must be an integer (no \f[I]scale\f[R]), and if it is negative, the first value must be non-zero. .RE .TP \f[B]*\f[R] The \f[B]multiply\f[R] operator takes two expressions, multiplies them, and returns the product. If \f[B]a\f[R] is the \f[I]scale\f[R] of the first expression and \f[B]b\f[R] is the \f[I]scale\f[R] of the second expression, the \f[I]scale\f[R] of the result is equal to \f[B]min(a+b,max(scale,a,b))\f[R] where \f[B]min()\f[R] and \f[B]max()\f[R] return the obvious values. .TP \f[B]/\f[R] The \f[B]divide\f[R] operator takes two expressions, divides them, and returns the quotient. The \f[I]scale\f[R] of the result shall be the value of \f[B]scale\f[R]. .RS .PP The second expression must be non-zero. .RE .TP \f[B]%\f[R] The \f[B]modulus\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and evaluates them by 1) Computing \f[B]a/b\f[R] to current \f[B]scale\f[R] and 2) Using the result of step 1 to calculate \f[B]a-(a/b)*b\f[R] to \f[I]scale\f[R] \f[B]max(scale+scale(b),scale(a))\f[R]. .RS .PP The second expression must be non-zero. .RE .TP \f[B]+\f[R] The \f[B]add\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and returns the sum, with a \f[I]scale\f[R] equal to the max of the \f[I]scale\f[R]s of \f[B]a\f[R] and \f[B]b\f[R]. .TP \f[B]-\f[R] The \f[B]subtract\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and returns the difference, with a \f[I]scale\f[R] equal to the max of the \f[I]scale\f[R]s of \f[B]a\f[R] and \f[B]b\f[R]. .TP \f[B]=\f[R] \f[B]+=\f[R] \f[B]-=\f[R] \f[B]*=\f[R] \f[B]/=\f[R] \f[B]%=\f[R] \f[B]\[ha]=\f[R] The \f[B]assignment\f[R] operators take two expressions, \f[B]a\f[R] and \f[B]b\f[R] where \f[B]a\f[R] is a named expression (see the \f[I]Named Expressions\f[R] subsection). .RS .PP For \f[B]=\f[R], \f[B]b\f[R] is copied and the result is assigned to \f[B]a\f[R]. For all others, \f[B]a\f[R] and \f[B]b\f[R] are applied as operands to the corresponding arithmetic operator and the result is assigned to \f[B]a\f[R]. .RE .TP \f[B]==\f[R] \f[B]<=\f[R] \f[B]>=\f[R] \f[B]!=\f[R] \f[B]<\f[R] \f[B]>\f[R] The \f[B]relational\f[R] operators compare two expressions, \f[B]a\f[R] and \f[B]b\f[R], and if the relation holds, according to C language semantics, the result is \f[B]1\f[R]. Otherwise, it is \f[B]0\f[R]. .RS .PP Note that unlike in C, these operators have a lower precedence than the \f[B]assignment\f[R] operators, which means that \f[B]a=b>c\f[R] is interpreted as \f[B](a=b)>c\f[R]. .PP Also, unlike the standard (see the \f[B]STANDARDS\f[R] section) requires, these operators can appear anywhere any other expressions can be used. This allowance is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]&&\f[R] The \f[B]boolean and\f[R] operator takes two expressions and returns \f[B]1\f[R] if both expressions are non-zero, \f[B]0\f[R] otherwise. .RS .PP This is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]||\f[R] The \f[B]boolean or\f[R] operator takes two expressions and returns \f[B]1\f[R] if one of the expressions is non-zero, \f[B]0\f[R] otherwise. .RS .PP This is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Statements -.PP The following items are statements: .IP " 1." 4 \f[B]E\f[R] .IP " 2." 4 \f[B]{\f[R] \f[B]S\f[R] \f[B];\f[R] \&... \f[B];\f[R] \f[B]S\f[R] \f[B]}\f[R] .IP " 3." 4 \f[B]if\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] .IP " 4." 4 \f[B]if\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] \f[B]else\f[R] \f[B]S\f[R] .IP " 5." 4 \f[B]while\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] .IP " 6." 4 \f[B]for\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B];\f[R] \f[B]E\f[R] \f[B];\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] .IP " 7." 4 An empty statement .IP " 8." 4 \f[B]break\f[R] .IP " 9." 4 \f[B]continue\f[R] .IP "10." 4 \f[B]quit\f[R] .IP "11." 4 \f[B]halt\f[R] .IP "12." 4 \f[B]limits\f[R] .IP "13." 4 A string of characters, enclosed in double quotes .IP "14." 4 \f[B]print\f[R] \f[B]E\f[R] \f[B],\f[R] \&... \f[B],\f[R] \f[B]E\f[R] .IP "15." 4 \f[B]stream\f[R] \f[B]E\f[R] \f[B],\f[R] \&... \f[B],\f[R] \f[B]E\f[R] .IP "16." 4 \f[B]I()\f[R], \f[B]I(E)\f[R], \f[B]I(E, E)\f[R], and so on, where \f[B]I\f[R] is an identifier for a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). The \f[B]E\f[R] argument(s) may also be arrays of the form \f[B]I[]\f[R], which will automatically be turned into array references (see the \f[I]Array References\f[R] subsection of the \f[B]FUNCTIONS\f[R] section) if the corresponding parameter in the function definition is an array reference. .PP Numbers 4, 9, 11, 12, 14, 15, and 16 are \f[B]non-portable extensions\f[R]. .PP Also, as a \f[B]non-portable extension\f[R], any or all of the expressions in the header of a for loop may be omitted. If the condition (second expression) is omitted, it is assumed to be a constant \f[B]1\f[R]. .PP The \f[B]break\f[R] statement causes a loop to stop iterating and resume execution immediately following a loop. This is only allowed in loops. .PP The \f[B]continue\f[R] statement causes a loop iteration to stop early and returns to the start of the loop, including testing the loop condition. This is only allowed in loops. .PP The \f[B]if\f[R] \f[B]else\f[R] statement does the same thing as in C. .PP The \f[B]quit\f[R] statement causes bc(1) to quit, even if it is on a branch that will not be executed (it is a compile-time command). .PP \f[B]Warning\f[R]: The behavior of this bc(1) on \f[B]quit\f[R] is slightly different from other bc(1) implementations. Other bc(1) implementations will exit as soon as they finish parsing the line that a \f[B]quit\f[R] command is on. This bc(1) will execute any completed and executable statements that occur before the \f[B]quit\f[R] statement before exiting. .PP In other words, for the bc(1) code below: .IP -.nf -\f[C] +.EX for (i = 0; i < 3; ++i) i; quit -\f[R] -.fi +.EE .PP Other bc(1) implementations will print nothing, and this bc(1) will print \f[B]0\f[R], \f[B]1\f[R], and \f[B]2\f[R] on successive lines before exiting. .PP The \f[B]halt\f[R] statement causes bc(1) to quit, if it is executed. (Unlike \f[B]quit\f[R] if it is on a branch of an \f[B]if\f[R] statement that is not executed, bc(1) does not quit.) .PP The \f[B]limits\f[R] statement prints the limits that this bc(1) is subject to. This is like the \f[B]quit\f[R] statement in that it is a compile-time command. .PP An expression by itself is evaluated and printed, followed by a newline. .SS Strings -.PP If strings appear as a statement by themselves, they are printed without a trailing newline. .PP In addition to appearing as a lone statement by themselves, strings can be assigned to variables and array elements. They can also be passed to functions in variable parameters. .PP If any statement that expects a string is given a variable that had a string assigned to it, the statement acts as though it had received a string. .PP If any math operation is attempted on a string or a variable or array element that has been assigned a string, an error is raised, and bc(1) resets (see the \f[B]RESET\f[R] section). .PP Assigning strings to variables and array elements and passing them to functions are \f[B]non-portable extensions\f[R]. .SS Print Statement -.PP The \[lq]expressions\[rq] in a \f[B]print\f[R] statement may also be strings. If they are, there are backslash escape sequences that are interpreted specially. What those sequences are, and what they cause to be printed, are shown below: .PP \f[B]\[rs]a\f[R]: \f[B]\[rs]a\f[R] .PP \f[B]\[rs]b\f[R]: \f[B]\[rs]b\f[R] .PP \f[B]\[rs]\[rs]\f[R]: \f[B]\[rs]\f[R] .PP \f[B]\[rs]e\f[R]: \f[B]\[rs]\f[R] .PP \f[B]\[rs]f\f[R]: \f[B]\[rs]f\f[R] .PP \f[B]\[rs]n\f[R]: \f[B]\[rs]n\f[R] .PP \f[B]\[rs]q\f[R]: \f[B]\[lq]\f[R] .PP \f[B]\[rs]r\f[R]: \f[B]\[rs]r\f[R] .PP \f[B]\[rs]t\f[R]: \f[B]\[rs]t\f[R] .PP Any other character following a backslash causes the backslash and character to be printed as-is. .PP Any non-string expression in a print statement shall be assigned to \f[B]last\f[R], like any other expression that is printed. .SS Stream Statement -.PP The expressions in a \f[B]stream\f[R] statement may also be strings. .PP If a \f[B]stream\f[R] statement is given a string, it prints the string as though the string had appeared as its own statement. In other words, the \f[B]stream\f[R] statement prints strings normally, without a newline. .PP If a \f[B]stream\f[R] statement is given a number, a copy of it is truncated and its absolute value is calculated. The result is then printed as though \f[B]obase\f[R] is \f[B]256\f[R] and each digit is interpreted as an 8-bit ASCII character, making it a byte stream. .SS Order of Evaluation -.PP All expressions in a statment are evaluated left to right, except as necessary to maintain order of operations. This means, for example, assuming that \f[B]i\f[R] is equal to \f[B]0\f[R], in the expression .IP -.nf -\f[C] +.EX a[i++] = i++ -\f[R] -.fi +.EE .PP the first (or 0th) element of \f[B]a\f[R] is set to \f[B]1\f[R], and \f[B]i\f[R] is equal to \f[B]2\f[R] at the end of the expression. .PP This includes function arguments. Thus, assuming \f[B]i\f[R] is equal to \f[B]0\f[R], this means that in the expression .IP -.nf -\f[C] +.EX x(i++, i++) -\f[R] -.fi +.EE .PP the first argument passed to \f[B]x()\f[R] is \f[B]0\f[R], and the second argument is \f[B]1\f[R], while \f[B]i\f[R] is equal to \f[B]2\f[R] before the function starts executing. .SH FUNCTIONS -.PP Function definitions are as follows: .IP -.nf -\f[C] +.EX define I(I,...,I){ auto I,...,I S;...;S return(E) } -\f[R] -.fi +.EE .PP Any \f[B]I\f[R] in the parameter list or \f[B]auto\f[R] list may be replaced with \f[B]I[]\f[R] to make a parameter or \f[B]auto\f[R] var an array, and any \f[B]I\f[R] in the parameter list may be replaced with \f[B]*I[]\f[R] to make a parameter an array reference. Callers of functions that take array references should not put an asterisk in the call; they must be called with just \f[B]I[]\f[R] like normal array parameters and will be automatically converted into references. .PP As a \f[B]non-portable extension\f[R], the opening brace of a \f[B]define\f[R] statement may appear on the next line. .PP As a \f[B]non-portable extension\f[R], the return statement may also be in one of the following forms: .IP "1." 3 \f[B]return\f[R] .IP "2." 3 \f[B]return\f[R] \f[B](\f[R] \f[B])\f[R] .IP "3." 3 \f[B]return\f[R] \f[B]E\f[R] .PP The first two, or not specifying a \f[B]return\f[R] statement, is equivalent to \f[B]return (0)\f[R], unless the function is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection below). .SS Void Functions -.PP Functions can also be \f[B]void\f[R] functions, defined as follows: .IP -.nf -\f[C] +.EX define void I(I,...,I){ auto I,...,I S;...;S return } -\f[R] -.fi +.EE .PP They can only be used as standalone expressions, where such an expression would be printed alone, except in a print statement. .PP Void functions can only use the first two \f[B]return\f[R] statements listed above. They can also omit the return statement entirely. .PP The word \[lq]void\[rq] is not treated as a keyword; it is still possible to have variables, arrays, and functions named \f[B]void\f[R]. The word \[lq]void\[rq] is only treated specially right after the \f[B]define\f[R] keyword. .PP This is a \f[B]non-portable extension\f[R]. .SS Array References -.PP For any array in the parameter list, if the array is declared in the form .IP -.nf -\f[C] +.EX *I[] -\f[R] -.fi +.EE .PP it is a \f[B]reference\f[R]. Any changes to the array in the function are reflected, when the function returns, to the array that was passed in. .PP Other than this, all function arguments are passed by value. .PP This is a \f[B]non-portable extension\f[R]. .SH LIBRARY -.PP All of the functions below are available when the \f[B]-l\f[R] or \f[B]--mathlib\f[R] command-line flags are given. .SS Standard Library -.PP The standard (see the \f[B]STANDARDS\f[R] section) defines the following functions for the math library: .TP \f[B]s(x)\f[R] Returns the sine of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]c(x)\f[R] Returns the cosine of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]a(x)\f[R] Returns the arctangent of \f[B]x\f[R], in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]l(x)\f[R] Returns the natural logarithm of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]e(x)\f[R] Returns the mathematical constant \f[B]e\f[R] raised to the power of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]j(x, n)\f[R] Returns the bessel integer order \f[B]n\f[R] (truncated) of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .SS Transcendental Functions -.PP All transcendental functions can return slightly inaccurate results, up to 1 ULP (https://en.wikipedia.org/wiki/Unit_in_the_last_place). This is unavoidable, and the article at https://people.eecs.berkeley.edu/\[ti]wkahan/LOG10HAF.TXT explains why it is impossible and unnecessary to calculate exact results for the transcendental functions. .PP Because of the possible inaccuracy, I recommend that users call those functions with the precision (\f[B]scale\f[R]) set to at least 1 higher than is necessary. If exact results are \f[I]absolutely\f[R] required, users can double the precision (\f[B]scale\f[R]) and then truncate. .PP The transcendental functions in the standard math library are: .IP \[bu] 2 \f[B]s(x)\f[R] .IP \[bu] 2 \f[B]c(x)\f[R] .IP \[bu] 2 \f[B]a(x)\f[R] .IP \[bu] 2 \f[B]l(x)\f[R] .IP \[bu] 2 \f[B]e(x)\f[R] .IP \[bu] 2 \f[B]j(x, n)\f[R] .SH RESET -.PP When bc(1) encounters an error or a signal that it has a non-default handler for, it resets. This means that several things happen. .PP First, any functions that are executing are stopped and popped off the stack. The behavior is not unlike that of exceptions in programming languages. Then the execution point is set so that any code waiting to execute (after all functions returned) is skipped. .PP Thus, when bc(1) resets, it skips any remaining code waiting to be executed. Then, if it is interactive mode, and the error was not a fatal error (see the \f[B]EXIT STATUS\f[R] section), it asks for more input; otherwise, it exits with the appropriate return code. .PP Note that this reset behavior is different from the GNU bc(1), which attempts to start executing the statement right after the one that caused an error. .SH PERFORMANCE -.PP Most bc(1) implementations use \f[B]char\f[R] types to calculate the value of \f[B]1\f[R] decimal digit at a time, but that can be slow. This bc(1) does something different. .PP It uses large integers to calculate more than \f[B]1\f[R] decimal digit at a time. If built in a environment where \f[B]BC_LONG_BIT\f[R] (see the \f[B]LIMITS\f[R] section) is \f[B]64\f[R], then each integer has \f[B]9\f[R] decimal digits. If built in an environment where \f[B]BC_LONG_BIT\f[R] is \f[B]32\f[R] then each integer has \f[B]4\f[R] decimal digits. This value (the number of decimal digits per large integer) is called \f[B]BC_BASE_DIGS\f[R]. .PP The actual values of \f[B]BC_LONG_BIT\f[R] and \f[B]BC_BASE_DIGS\f[R] can be queried with the \f[B]limits\f[R] statement. .PP In addition, this bc(1) uses an even larger integer for overflow checking. This integer type depends on the value of \f[B]BC_LONG_BIT\f[R], but is always at least twice as large as the integer type used to store digits. .SH LIMITS -.PP The following are the limits on bc(1): .TP \f[B]BC_LONG_BIT\f[R] The number of bits in the \f[B]long\f[R] type in the environment where bc(1) was built. This determines how many decimal digits can be stored in a single large integer (see the \f[B]PERFORMANCE\f[R] section). .TP \f[B]BC_BASE_DIGS\f[R] The number of decimal digits per large integer (see the \f[B]PERFORMANCE\f[R] section). Depends on \f[B]BC_LONG_BIT\f[R]. .TP \f[B]BC_BASE_POW\f[R] The max decimal number that each large integer can store (see \f[B]BC_BASE_DIGS\f[R]) plus \f[B]1\f[R]. Depends on \f[B]BC_BASE_DIGS\f[R]. .TP \f[B]BC_OVERFLOW_MAX\f[R] The max number that the overflow type (see the \f[B]PERFORMANCE\f[R] section) can hold. Depends on \f[B]BC_LONG_BIT\f[R]. .TP \f[B]BC_BASE_MAX\f[R] The maximum output base. Set at \f[B]BC_BASE_POW\f[R]. .TP \f[B]BC_DIM_MAX\f[R] The maximum size of arrays. Set at \f[B]SIZE_MAX-1\f[R]. .TP \f[B]BC_SCALE_MAX\f[R] The maximum \f[B]scale\f[R]. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_STRING_MAX\f[R] The maximum length of strings. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_NAME_MAX\f[R] The maximum length of identifiers. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_NUM_MAX\f[R] The maximum length of a number (in decimal digits), which includes digits after the decimal point. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP Exponent The maximum allowable exponent (positive or negative). Set at \f[B]BC_OVERFLOW_MAX\f[R]. .TP Number of vars The maximum number of vars/arrays. Set at \f[B]SIZE_MAX-1\f[R]. .PP The actual values can be queried with the \f[B]limits\f[R] statement. .PP These limits are meant to be effectively non-existent; the limits are so large (at least on 64-bit machines) that there should not be any point at which they become a problem. In fact, memory should be exhausted before these limits should be hit. .SH ENVIRONMENT VARIABLES -.PP As \f[B]non-portable extensions\f[R], bc(1) recognizes the following environment variables: .TP \f[B]POSIXLY_CORRECT\f[R] If this variable exists (no matter the contents), bc(1) behaves as if the \f[B]-s\f[R] option was given. .TP \f[B]BC_ENV_ARGS\f[R] This is another way to give command-line arguments to bc(1). They should be in the same format as all other command-line arguments. These are always processed first, so any files given in \f[B]BC_ENV_ARGS\f[R] will be processed before arguments and files given on the command-line. This gives the user the ability to set up \[lq]standard\[rq] options and files to be used at every invocation. The most useful thing for such files to contain would be useful functions that the user might want every time bc(1) runs. .RS .PP The code that parses \f[B]BC_ENV_ARGS\f[R] will correctly handle quoted arguments, but it does not understand escape sequences. For example, the string \f[B]\[lq]/home/gavin/some bc file.bc\[rq]\f[R] will be correctly parsed, but the string \f[B]\[lq]/home/gavin/some \[dq]bc\[dq] file.bc\[rq]\f[R] will include the backslashes. .PP The quote parsing will handle either kind of quotes, \f[B]\[cq]\f[R] or \f[B]\[lq]\f[R]. Thus, if you have a file with any number of single quotes in the name, you can use double quotes as the outside quotes, as in \f[B]\[lq]some `bc' file.bc\[rq]\f[R], and vice versa if you have a file with double quotes. However, handling a file with both kinds of quotes in \f[B]BC_ENV_ARGS\f[R] is not supported due to the complexity of the parsing, though such files are still supported on the command-line where the parsing is done by the shell. .RE .TP \f[B]BC_LINE_LENGTH\f[R] If this environment variable exists and contains an integer that is greater than \f[B]1\f[R] and is less than \f[B]UINT16_MAX\f[R] (\f[B]2\[ha]16-1\f[R]), bc(1) will output lines to that length, including the backslash (\f[B]\[rs]\f[R]). The default line length is \f[B]70\f[R]. .RS .PP The special value of \f[B]0\f[R] will disable line length checking and print numbers without regard to line length and without backslashes and newlines. .RE .TP \f[B]BC_BANNER\f[R] If this environment variable exists and contains an integer, then a non-zero value activates the copyright banner when bc(1) is in interactive mode, while zero deactivates it. .RS .PP If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), then this environment variable has no effect because bc(1) does not print the banner when not in interactive mode. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_SIGINT_RESET\f[R] If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), then this environment variable has no effect because bc(1) exits on \f[B]SIGINT\f[R] when not in interactive mode. .RS .PP However, when bc(1) is in interactive mode, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) reset on \f[B]SIGINT\f[R], rather than exit, and zero makes bc(1) exit. If this environment variable exists and is \f[I]not\f[R] an integer, then bc(1) will exit on \f[B]SIGINT\f[R]. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_TTY_MODE\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, then a non-zero value makes bc(1) use TTY mode, and zero makes bc(1) not use TTY mode. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_PROMPT\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) use a prompt, and zero or a non-integer makes bc(1) not use a prompt. If this environment variable does not exist and \f[B]BC_TTY_MODE\f[R] does, then the value of the \f[B]BC_TTY_MODE\f[R] environment variable is used. .PP This environment variable and the \f[B]BC_TTY_MODE\f[R] environment variable override the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_EXPR_EXIT\f[R] If any expressions or expression files are given on the command-line with \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R], then if this environment variable exists and contains an integer, a non-zero value makes bc(1) exit after executing the expressions and expression files, and a zero value makes bc(1) not exit. .RS .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_DIGIT_CLAMP\f[R] When parsing numbers and if this environment variable exists and contains an integer, a non-zero value makes bc(1) clamp digits that are greater than or equal to the current \f[B]ibase\f[R] so that all such digits are considered equal to the \f[B]ibase\f[R] minus 1, and a zero value disables such clamping so that those digits are always equal to their value, which is multiplied by the power of the \f[B]ibase\f[R]. .RS .PP This never applies to single-digit numbers, as per the standard (see the \f[B]STANDARDS\f[R] section). .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .SH EXIT STATUS -.PP bc(1) returns the following exit statuses: .TP \f[B]0\f[R] No error. .TP \f[B]1\f[R] A math error occurred. This follows standard practice of using \f[B]1\f[R] for expected errors, since math errors will happen in the process of normal execution. .RS .PP Math errors include divide by \f[B]0\f[R], taking the square root of a negative number, attempting to convert a negative number to a hardware integer, overflow when converting a number to a hardware integer, overflow when calculating the size of a number, and attempting to use a non-integer where an integer is required. .PP Converting to a hardware integer happens for the second operand of the power (\f[B]\[ha]\f[R]) operator and the corresponding assignment operator. .RE .TP \f[B]2\f[R] A parse error occurred. .RS .PP Parse errors include unexpected \f[B]EOF\f[R], using an invalid character, failing to find the end of a string or comment, using a token where it is invalid, giving an invalid expression, giving an invalid print statement, giving an invalid function definition, attempting to assign to an expression that is not a named expression (see the \f[I]Named Expressions\f[R] subsection of the \f[B]SYNTAX\f[R] section), giving an invalid \f[B]auto\f[R] list, having a duplicate \f[B]auto\f[R]/function parameter, failing to find the end of a code block, attempting to return a value from a \f[B]void\f[R] function, attempting to use a variable as a reference, and using any extensions when the option \f[B]-s\f[R] or any equivalents were given. .RE .TP \f[B]3\f[R] A runtime error occurred. .RS .PP Runtime errors include assigning an invalid number to any global (\f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R]), giving a bad expression to a \f[B]read()\f[R] call, calling \f[B]read()\f[R] inside of a \f[B]read()\f[R] call, type errors, passing the wrong number of arguments to functions, attempting to call an undefined function, and attempting to use a \f[B]void\f[R] function call as a value in an expression. .RE .TP \f[B]4\f[R] A fatal error occurred. .RS .PP Fatal errors include memory allocation errors, I/O errors, failing to open files, attempting to use files that do not have only ASCII characters (bc(1) only accepts ASCII characters), attempting to open a directory as a file, and giving invalid command-line options. .RE .PP The exit status \f[B]4\f[R] is special; when a fatal error occurs, bc(1) always exits and returns \f[B]4\f[R], no matter what mode bc(1) is in. .PP The other statuses will only be returned when bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), since bc(1) resets its state (see the \f[B]RESET\f[R] section) and accepts more input when one of those errors occurs in interactive mode. This is also the case when interactive mode is forced by the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .PP These exit statuses allow bc(1) to be used in shell scripting with error checking, and its normal behavior can be forced by using the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .SH INTERACTIVE MODE -.PP Per the standard (see the \f[B]STANDARDS\f[R] section), bc(1) has an interactive mode and a non-interactive mode. Interactive mode is turned on automatically when both \f[B]stdin\f[R] and \f[B]stdout\f[R] are hooked to a terminal, but the \f[B]-i\f[R] flag and \f[B]--interactive\f[R] option can turn it on in other situations. .PP In interactive mode, bc(1) attempts to recover from errors (see the \f[B]RESET\f[R] section), and in normal execution, flushes \f[B]stdout\f[R] as soon as execution is done for the current input. bc(1) may also reset on \f[B]SIGINT\f[R] instead of exit, depending on the contents of, or default for, the \f[B]BC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .SH TTY MODE -.PP If \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY, then \[lq]TTY mode\[rq] is considered to be available, and thus, bc(1) can turn on TTY mode, subject to some settings. .PP If there is the environment variable \f[B]BC_TTY_MODE\f[R] in the environment (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then if that environment variable contains a non-zero integer, bc(1) will turn on TTY mode when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY. If the \f[B]BC_TTY_MODE\f[R] environment variable exists but is \f[I]not\f[R] a non-zero integer, then bc(1) will not turn TTY mode on. .PP If the environment variable \f[B]BC_TTY_MODE\f[R] does \f[I]not\f[R] exist, the default setting is used. The default setting can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP TTY mode is different from interactive mode because interactive mode is required in the bc(1) standard (see the \f[B]STANDARDS\f[R] section), and interactive mode requires only \f[B]stdin\f[R] and \f[B]stdout\f[R] to be connected to a terminal. .SS Command-Line History -.PP Command-line history is only enabled if TTY mode is, i.e., that \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the \f[B]BC_TTY_MODE\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and its default do not disable TTY mode. See the \f[B]COMMAND LINE HISTORY\f[R] section for more information. .SS Prompt -.PP If TTY mode is available, then a prompt can be enabled. Like TTY mode itself, it can be turned on or off with an environment variable: \f[B]BC_PROMPT\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP If the environment variable \f[B]BC_PROMPT\f[R] exists and is a non-zero integer, then the prompt is turned on when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options were not used. The read prompt will be turned on under the same conditions, except that the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options must also not be used. .PP However, if \f[B]BC_PROMPT\f[R] does not exist, the prompt can be enabled or disabled with the \f[B]BC_TTY_MODE\f[R] environment variable, the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options, and the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options. See the \f[B]ENVIRONMENT VARIABLES\f[R] and \f[B]OPTIONS\f[R] sections for more details. .SH SIGNAL HANDLING -.PP Sending a \f[B]SIGINT\f[R] will cause bc(1) to do one of two things. .PP If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), or the \f[B]BC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or its default, is either not an integer or it is zero, bc(1) will exit. .PP However, if bc(1) is in interactive mode, and the \f[B]BC_SIGINT_RESET\f[R] or its default is an integer and non-zero, then bc(1) will stop executing the current input and reset (see the \f[B]RESET\f[R] section) upon receiving a \f[B]SIGINT\f[R]. .PP Note that \[lq]current input\[rq] can mean one of two things. If bc(1) is processing input from \f[B]stdin\f[R] in interactive mode, it will ask for more input. If bc(1) is processing input from a file in interactive mode, it will stop processing the file and start processing the next file, if one exists, or ask for input from \f[B]stdin\f[R] if no other file exists. .PP This means that if a \f[B]SIGINT\f[R] is sent to bc(1) as it is executing a file, it can seem as though bc(1) did not respond to the signal since it will immediately start executing the next file. This is by design; most files that users execute when interacting with bc(1) have function definitions, which are quick to parse. If a file takes a long time to execute, there may be a bug in that file. The rest of the files could still be executed without problem, allowing the user to continue. .PP \f[B]SIGTERM\f[R] and \f[B]SIGQUIT\f[R] cause bc(1) to clean up and exit, and it uses the default handler for all other signals. The one exception is \f[B]SIGHUP\f[R]; in that case, and only when bc(1) is in TTY mode (see the \f[B]TTY MODE\f[R] section), a \f[B]SIGHUP\f[R] will cause bc(1) to clean up and exit. .SH COMMAND LINE HISTORY -.PP bc(1) supports interactive command-line editing. .PP If bc(1) can be in TTY mode (see the \f[B]TTY MODE\f[R] section), history can be enabled. This means that command-line history can only be enabled when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY. .PP Like TTY mode itself, it can be turned on or off with the environment variable \f[B]BC_TTY_MODE\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP If history is enabled, previous lines can be recalled and edited with the arrow keys. .PP \f[B]Note\f[R]: tabs are converted to 8 spaces. .SH LOCALES -.PP This bc(1) ships with support for adding error messages for different locales and thus, supports \f[B]LC_MESSAGES\f[R]. .SH SEE ALSO -.PP dc(1) .SH STANDARDS -.PP bc(1) is compliant with the IEEE Std 1003.1-2017 (\[lq]POSIX.1-2017\[rq]) specification at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html . The flags \f[B]-efghiqsvVw\f[R], all long options, and the extensions noted above are extensions to that specification. .PP In addition, the behavior of the \f[B]quit\f[R] implements an interpretation of that specification that is different from all known implementations. For more information see the \f[B]Statements\f[R] subsection of the \f[B]SYNTAX\f[R] section. .PP Note that the specification explicitly says that bc(1) only accepts numbers that use a period (\f[B].\f[R]) as a radix point, regardless of the value of \f[B]LC_NUMERIC\f[R]. .PP This bc(1) supports error messages for different locales, and thus, it supports \f[B]LC_MESSAGES\f[R]. .SH BUGS -.PP Before version \f[B]6.1.0\f[R], this bc(1) had incorrect behavior for the \f[B]quit\f[R] statement. .PP No other bugs are known. Report bugs at https://git.gavinhoward.com/gavin/bc . .SH AUTHORS -.PP -Gavin D. -Howard and contributors. +Gavin D. Howard \c +.MT gavin@gavinhoward.com +.ME \c +\ and contributors. diff --git a/contrib/bc/manuals/bc/EH.1 b/contrib/bc/manuals/bc/EH.1 index 35672fe5c4e6..e939df81cffd 100644 --- a/contrib/bc/manuals/bc/EH.1 +++ b/contrib/bc/manuals/bc/EH.1 @@ -1,1787 +1,1733 @@ .\" .\" SPDX-License-Identifier: BSD-2-Clause .\" .\" Copyright (c) 2018-2023 Gavin D. Howard and contributors. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions are met: .\" .\" * Redistributions of source code must retain the above copyright notice, .\" this list of conditions and the following disclaimer. .\" .\" * Redistributions in binary form must reproduce the above copyright notice, .\" this list of conditions and the following disclaimer in the documentation .\" and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.TH "BC" "1" "February 2023" "Gavin D. Howard" "General Commands Manual" +.TH "BC" "1" "November 2023" "Gavin D. Howard" "General Commands Manual" .nh .ad l .SH NAME -.PP bc - arbitrary-precision decimal arithmetic language and calculator .SH SYNOPSIS -.PP \f[B]bc\f[R] [\f[B]-cCghilPqRsvVw\f[R]] [\f[B]--digit-clamp\f[R]] [\f[B]--no-digit-clamp\f[R]] [\f[B]--global-stacks\f[R]] [\f[B]--help\f[R]] [\f[B]--interactive\f[R]] [\f[B]--mathlib\f[R]] [\f[B]--no-prompt\f[R]] [\f[B]--no-read-prompt\f[R]] [\f[B]--quiet\f[R]] [\f[B]--standard\f[R]] [\f[B]--warn\f[R]] [\f[B]--version\f[R]] [\f[B]-e\f[R] \f[I]expr\f[R]] [\f[B]--expression\f[R]=\f[I]expr\f[R]\&...] [\f[B]-f\f[R] \f[I]file\f[R]\&...] [\f[B]--file\f[R]=\f[I]file\f[R]\&...] [\f[I]file\f[R]\&...] .SH DESCRIPTION -.PP bc(1) is an interactive processor for a language first standardized in 1991 by POSIX. (See the \f[B]STANDARDS\f[R] section.) The language provides unlimited precision decimal arithmetic and is somewhat C-like, but there are differences. Such differences will be noted in this document. .PP After parsing and handling options, this bc(1) reads any files given on the command line and executes them before reading from \f[B]stdin\f[R]. .PP This bc(1) is a drop-in replacement for \f[I]any\f[R] bc(1), including (and especially) the GNU bc(1). It also has many extensions and extra features beyond other implementations. .PP \f[B]Note\f[R]: If running this bc(1) on \f[I]any\f[R] script meant for another bc(1) gives a parse error, it is probably because a word this bc(1) reserves as a keyword is used as the name of a function, variable, or array. To fix that, use the command-line option \f[B]-r\f[R] \f[I]keyword\f[R], where \f[I]keyword\f[R] is the keyword that is used as a name in the script. For more information, see the \f[B]OPTIONS\f[R] section. .PP If parsing scripts meant for other bc(1) implementations still does not work, that is a bug and should be reported. See the \f[B]BUGS\f[R] section. .SH OPTIONS -.PP The following are the options that bc(1) accepts. .TP \f[B]-C\f[R], \f[B]--no-digit-clamp\f[R] Disables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that the value added to a number from a digit is always that digit\[cq]s value multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-c\f[R] or \f[B]--digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-c\f[R], \f[B]--digit-clamp\f[R] Enables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that digits that the value added to a number from a digit that is greater than or equal to the ibase is the value of ibase minus 1 all multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-C\f[R] or \f[B]--no-digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-e\f[R] \f[I]expr\f[R], \f[B]--expression\f[R]=\f[I]expr\f[R] Evaluates \f[I]expr\f[R]. If multiple expressions are given, they are evaluated in order. If files are given as well (see the \f[B]-f\f[R] and \f[B]--file\f[R] options), the expressions and files are evaluated in the order given. This means that if a file is given before an expression, the file is read in and evaluated first. .RS .PP If this option is given on the command-line (i.e., not in \f[B]BC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, bc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R], whether on the command-line or in \f[B]BC_ENV_ARGS\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, bc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-f\f[R] \f[I]file\f[R], \f[B]--file\f[R]=\f[I]file\f[R] Reads in \f[I]file\f[R] and evaluates it, line by line, as though it were read through \f[B]stdin\f[R]. If expressions are also given (see the \f[B]-e\f[R] and \f[B]--expression\f[R] options), the expressions are evaluated in the order given. .RS .PP If this option is given on the command-line (i.e., not in \f[B]BC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, bc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, bc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-g\f[R], \f[B]--global-stacks\f[R] Turns the globals \f[B]ibase\f[R], \f[B]obase\f[R], and \f[B]scale\f[R] into stacks. .RS .PP This has the effect that a copy of the current value of all three are pushed onto a stack for every function call, as well as popped when every function returns. This means that functions can assign to any and all of those globals without worrying that the change will affect other functions. Thus, a hypothetical function named \f[B]output(x,b)\f[R] that simply printed \f[B]x\f[R] in base \f[B]b\f[R] could be written like this: .IP -.nf -\f[C] +.EX define void output(x, b) { obase=b x } -\f[R] -.fi +.EE .PP instead of like this: .IP -.nf -\f[C] +.EX define void output(x, b) { auto c c=obase obase=b x obase=c } -\f[R] -.fi +.EE .PP This makes writing functions much easier. .PP However, since using this flag means that functions cannot set \f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R] globally, functions that are made to do so cannot work anymore. There are two possible use cases for that, and each has a solution. .PP First, if a function is called on startup to turn bc(1) into a number converter, it is possible to replace that capability with various shell aliases. Examples: .IP -.nf -\f[C] +.EX alias d2o=\[dq]bc -e ibase=A -e obase=8\[dq] alias h2b=\[dq]bc -e ibase=G -e obase=2\[dq] -\f[R] -.fi +.EE .PP Second, if the purpose of a function is to set \f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R] globally for any other purpose, it could be split into one to three functions (based on how many globals it sets) and each of those functions could return the desired value for a global. .PP If the behavior of this option is desired for every run of bc(1), then users could make sure to define \f[B]BC_ENV_ARGS\f[R] and include this option (see the \f[B]ENVIRONMENT VARIABLES\f[R] section for more details). .PP If \f[B]-s\f[R], \f[B]-w\f[R], or any equivalents are used, this option is ignored. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-h\f[R], \f[B]--help\f[R] Prints a usage message and exits. .TP \f[B]-I\f[R] \f[I]ibase\f[R], \f[B]--ibase\f[R]=\f[I]ibase\f[R] Sets the builtin variable \f[B]ibase\f[R] to the value \f[I]ibase\f[R] assuming that \f[I]ibase\f[R] is in base 10. It is a fatal error if \f[I]ibase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-i\f[R], \f[B]--interactive\f[R] Forces interactive mode. (See the \f[B]INTERACTIVE MODE\f[R] section.) .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-L\f[R], \f[B]--no-line-length\f[R] Disables line length checking and prints numbers without backslashes and newlines. In other words, this option sets \f[B]BC_LINE_LENGTH\f[R] to \f[B]0\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-l\f[R], \f[B]--mathlib\f[R] Sets \f[B]scale\f[R] (see the \f[B]SYNTAX\f[R] section) to \f[B]20\f[R] and loads the included math library before running any code, including any expressions or files specified on the command line. .RS .PP To learn what is in the library, see the \f[B]LIBRARY\f[R] section. .RE .TP \f[B]-O\f[R] \f[I]obase\f[R], \f[B]--obase\f[R]=\f[I]obase\f[R] Sets the builtin variable \f[B]obase\f[R] to the value \f[I]obase\f[R] assuming that \f[I]obase\f[R] is in base 10. It is a fatal error if \f[I]obase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-P\f[R], \f[B]--no-prompt\f[R] Disables the prompt in TTY mode. (The prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a prompt or are not used to having them in bc(1). Most of those users would want to put this option in \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .RS .PP These options override the \f[B]BC_PROMPT\f[R] and \f[B]BC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-q\f[R], \f[B]--quiet\f[R] This option is for compatibility with the GNU bc(1) (https://www.gnu.org/software/bc/); it is a no-op. Without this option, GNU bc(1) prints a copyright header. This bc(1) only prints the copyright header if one or more of the \f[B]-v\f[R], \f[B]-V\f[R], or \f[B]--version\f[R] options are given unless the \f[B]BC_BANNER\f[R] environment variable is set and contains a non-zero integer or if this bc(1) was built with the header displayed by default. If \f[I]any\f[R] of that is the case, then this option \f[I]does\f[R] prevent bc(1) from printing the header. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-R\f[R], \f[B]--no-read-prompt\f[R] Disables the read prompt in TTY mode. (The read prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a read prompt or are not used to having them in bc(1). Most of those users would want to put this option in \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). This option is also useful in hash bang lines of bc(1) scripts that prompt for user input. .RS .PP This option does not disable the regular prompt because the read prompt is only used when the \f[B]read()\f[R] built-in function is called. .PP These options \f[I]do\f[R] override the \f[B]BC_PROMPT\f[R] and \f[B]BC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), but only for the read prompt. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-r\f[R] \f[I]keyword\f[R], \f[B]--redefine\f[R]=\f[I]keyword\f[R] Redefines \f[I]keyword\f[R] in order to allow it to be used as a function, variable, or array name. This is useful when this bc(1) gives parse errors when parsing scripts meant for other bc(1) implementations. .RS .PP The keywords this bc(1) allows to be redefined are: .IP \[bu] 2 \f[B]abs\f[R] .IP \[bu] 2 \f[B]asciify\f[R] .IP \[bu] 2 \f[B]continue\f[R] .IP \[bu] 2 \f[B]divmod\f[R] .IP \[bu] 2 \f[B]else\f[R] .IP \[bu] 2 \f[B]halt\f[R] .IP \[bu] 2 \f[B]last\f[R] .IP \[bu] 2 \f[B]limits\f[R] .IP \[bu] 2 \f[B]maxibase\f[R] .IP \[bu] 2 \f[B]maxobase\f[R] .IP \[bu] 2 \f[B]maxscale\f[R] .IP \[bu] 2 \f[B]modexp\f[R] .IP \[bu] 2 \f[B]print\f[R] .IP \[bu] 2 \f[B]read\f[R] .IP \[bu] 2 \f[B]stream\f[R] .PP If any of those keywords are used as a function, variable, or array name in a script, use this option with the keyword as the argument. If multiple are used, use this option for all of them; it can be used multiple times. .PP Keywords are \f[I]not\f[R] redefined when parsing the builtin math library (see the \f[B]LIBRARY\f[R] section). .PP It is a fatal error to redefine keywords mandated by the POSIX standard (see the \f[B]STANDARDS\f[R] section). It is a fatal error to attempt to redefine words that this bc(1) does not reserve as keywords. .RE .TP \f[B]-S\f[R] \f[I]scale\f[R], \f[B]--scale\f[R]=\f[I]scale\f[R] Sets the builtin variable \f[B]scale\f[R] to the value \f[I]scale\f[R] assuming that \f[I]scale\f[R] is in base 10. It is a fatal error if \f[I]scale\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-s\f[R], \f[B]--standard\f[R] Process exactly the language defined by the standard (see the \f[B]STANDARDS\f[R] section) and error if any extensions are used. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-v\f[R], \f[B]-V\f[R], \f[B]--version\f[R] Print the version information (copyright header) and exits. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-w\f[R], \f[B]--warn\f[R] Like \f[B]-s\f[R] and \f[B]--standard\f[R], except that warnings (and not errors) are printed for non-standard extensions and execution continues normally. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-z\f[R], \f[B]--leading-zeroes\f[R] Makes bc(1) print all numbers greater than \f[B]-1\f[R] and less than \f[B]1\f[R], and not equal to \f[B]0\f[R], with a leading zero. .RS .PP This can be set for individual numbers with the \f[B]plz(x)\f[R], \f[B]plznl(x)\f[R], \f[B]pnlz(x)\f[R], and \f[B]pnlznl(x)\f[R] functions in the extended math library (see the \f[B]LIBRARY\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .PP All long options are \f[B]non-portable extensions\f[R]. .SH STDIN -.PP If no files or expressions are given by the \f[B]-f\f[R], \f[B]--file\f[R], \f[B]-e\f[R], or \f[B]--expression\f[R] options, then bc(1) reads from \f[B]stdin\f[R]. .PP However, there are a few caveats to this. .PP First, \f[B]stdin\f[R] is evaluated a line at a time. The only exception to this is if the parse cannot complete. That means that starting a string without ending it or starting a function, \f[B]if\f[R] statement, or loop without ending it will also cause bc(1) to not execute. .PP Second, after an \f[B]if\f[R] statement, bc(1) doesn\[cq]t know if an \f[B]else\f[R] statement will follow, so it will not execute until it knows there will not be an \f[B]else\f[R] statement. .SH STDOUT -.PP Any non-error output is written to \f[B]stdout\f[R]. In addition, if history (see the \f[B]HISTORY\f[R] section) and the prompt (see the \f[B]TTY MODE\f[R] section) are enabled, both are output to \f[B]stdout\f[R]. .PP \f[B]Note\f[R]: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stdout\f[R], so if \f[B]stdout\f[R] is closed, as in -\f[B]bc >&-\f[R], it will quit with an error. +\f[B]bc >&-\f[R], it will quit with an error. This is done so that bc(1) can report problems when \f[B]stdout\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stdout\f[R] to \f[B]/dev/null\f[R]. .SH STDERR -.PP Any error output is written to \f[B]stderr\f[R]. .PP \f[B]Note\f[R]: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stderr\f[R], so if \f[B]stderr\f[R] is closed, as in -\f[B]bc 2>&-\f[R], it will quit with an error. +\f[B]bc 2>&-\f[R], it will quit with an error. This is done so that bc(1) can exit with an error code when \f[B]stderr\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stderr\f[R] to \f[B]/dev/null\f[R]. .SH SYNTAX -.PP The syntax for bc(1) programs is mostly C-like, with some differences. This bc(1) follows the POSIX standard (see the \f[B]STANDARDS\f[R] section), which is a much more thorough resource for the language this bc(1) accepts. This section is meant to be a summary and a listing of all the extensions to the standard. .PP In the sections below, \f[B]E\f[R] means expression, \f[B]S\f[R] means statement, and \f[B]I\f[R] means identifier. .PP Identifiers (\f[B]I\f[R]) start with a lowercase letter and can be followed by any number (up to \f[B]BC_NAME_MAX-1\f[R]) of lowercase letters (\f[B]a-z\f[R]), digits (\f[B]0-9\f[R]), and underscores (\f[B]_\f[R]). The regex is \f[B][a-z][a-z0-9_]*\f[R]. Identifiers with more than one character (letter) are a \f[B]non-portable extension\f[R]. .PP \f[B]ibase\f[R] is a global variable determining how to interpret constant numbers. It is the \[lq]input\[rq] base, or the number base used for interpreting input numbers. \f[B]ibase\f[R] is initially \f[B]10\f[R]. If the \f[B]-s\f[R] (\f[B]--standard\f[R]) and \f[B]-w\f[R] (\f[B]--warn\f[R]) flags were not given on the command line, the max allowable value for \f[B]ibase\f[R] is \f[B]36\f[R]. Otherwise, it is \f[B]16\f[R]. The min allowable value for \f[B]ibase\f[R] is \f[B]2\f[R]. The max allowable value for \f[B]ibase\f[R] can be queried in bc(1) programs with the \f[B]maxibase()\f[R] built-in function. .PP \f[B]obase\f[R] is a global variable determining how to output results. It is the \[lq]output\[rq] base, or the number base used for outputting numbers. \f[B]obase\f[R] is initially \f[B]10\f[R]. The max allowable value for \f[B]obase\f[R] is \f[B]BC_BASE_MAX\f[R] and can be queried in bc(1) programs with the \f[B]maxobase()\f[R] built-in function. The min allowable value for \f[B]obase\f[R] is \f[B]2\f[R]. Values are output in the specified base. .PP The \f[I]scale\f[R] of an expression is the number of digits in the result of the expression right of the decimal point, and \f[B]scale\f[R] is a global variable that sets the precision of any operations, with exceptions. \f[B]scale\f[R] is initially \f[B]0\f[R]. \f[B]scale\f[R] cannot be negative. The max allowable value for \f[B]scale\f[R] is \f[B]BC_SCALE_MAX\f[R] and can be queried in bc(1) programs with the \f[B]maxscale()\f[R] built-in function. .PP bc(1) has both \f[I]global\f[R] variables and \f[I]local\f[R] variables. All \f[I]local\f[R] variables are local to the function; they are parameters or are introduced in the \f[B]auto\f[R] list of a function (see the \f[B]FUNCTIONS\f[R] section). If a variable is accessed which is not a parameter or in the \f[B]auto\f[R] list, it is assumed to be \f[I]global\f[R]. If a parent function has a \f[I]local\f[R] variable version of a variable that a child function considers \f[I]global\f[R], the value of that \f[I]global\f[R] variable in the child function is the value of the variable in the parent function, not the value of the actual \f[I]global\f[R] variable. .PP All of the above applies to arrays as well. .PP The value of a statement that is an expression (i.e., any of the named expressions or operands) is printed unless the lowest precedence operator is an assignment operator \f[I]and\f[R] the expression is notsurrounded by parentheses. .PP The value that is printed is also assigned to the special variable \f[B]last\f[R]. A single dot (\f[B].\f[R]) may also be used as a synonym for \f[B]last\f[R]. These are \f[B]non-portable extensions\f[R]. .PP Either semicolons or newlines may separate statements. .SS Comments -.PP There are two kinds of comments: .IP "1." 3 Block comments are enclosed in \f[B]/*\f[R] and \f[B]*/\f[R]. .IP "2." 3 Line comments go from \f[B]#\f[R] until, and not including, the next newline. This is a \f[B]non-portable extension\f[R]. .SS Named Expressions -.PP The following are named expressions in bc(1): .IP "1." 3 Variables: \f[B]I\f[R] .IP "2." 3 Array Elements: \f[B]I[E]\f[R] .IP "3." 3 \f[B]ibase\f[R] .IP "4." 3 \f[B]obase\f[R] .IP "5." 3 \f[B]scale\f[R] .IP "6." 3 \f[B]last\f[R] or a single dot (\f[B].\f[R]) .PP Number 6 is a \f[B]non-portable extension\f[R]. .PP Variables and arrays do not interfere; users can have arrays named the same as variables. This also applies to functions (see the \f[B]FUNCTIONS\f[R] section), so a user can have a variable, array, and function that all have the same name, and they will not shadow each other, whether inside of functions or not. .PP Named expressions are required as the operand of \f[B]increment\f[R]/\f[B]decrement\f[R] operators and as the left side of \f[B]assignment\f[R] operators (see the \f[I]Operators\f[R] subsection). .SS Operands -.PP The following are valid operands in bc(1): .IP " 1." 4 Numbers (see the \f[I]Numbers\f[R] subsection below). .IP " 2." 4 Array indices (\f[B]I[E]\f[R]). .IP " 3." 4 \f[B](E)\f[R]: The value of \f[B]E\f[R] (used to change precedence). .IP " 4." 4 \f[B]sqrt(E)\f[R]: The square root of \f[B]E\f[R]. \f[B]E\f[R] must be non-negative. .IP " 5." 4 \f[B]length(E)\f[R]: The number of significant decimal digits in \f[B]E\f[R]. Returns \f[B]1\f[R] for \f[B]0\f[R] with no decimal places. If given a string, the length of the string is returned. Passing a string to \f[B]length(E)\f[R] is a \f[B]non-portable extension\f[R]. .IP " 6." 4 \f[B]length(I[])\f[R]: The number of elements in the array \f[B]I\f[R]. This is a \f[B]non-portable extension\f[R]. .IP " 7." 4 \f[B]scale(E)\f[R]: The \f[I]scale\f[R] of \f[B]E\f[R]. .IP " 8." 4 \f[B]abs(E)\f[R]: The absolute value of \f[B]E\f[R]. This is a \f[B]non-portable extension\f[R]. .IP " 9." 4 \f[B]is_number(E)\f[R]: \f[B]1\f[R] if the given argument is a number, \f[B]0\f[R] if it is a string. This is a \f[B]non-portable extension\f[R]. .IP "10." 4 \f[B]is_string(E)\f[R]: \f[B]1\f[R] if the given argument is a string, \f[B]0\f[R] if it is a number. This is a \f[B]non-portable extension\f[R]. .IP "11." 4 \f[B]modexp(E, E, E)\f[R]: Modular exponentiation, where the first expression is the base, the second is the exponent, and the third is the modulus. All three values must be integers. The second argument must be non-negative. The third argument must be non-zero. This is a \f[B]non-portable extension\f[R]. .IP "12." 4 \f[B]divmod(E, E, I[])\f[R]: Division and modulus in one operation. This is for optimization. The first expression is the dividend, and the second is the divisor, which must be non-zero. The return value is the quotient, and the modulus is stored in index \f[B]0\f[R] of the provided array (the last argument). This is a \f[B]non-portable extension\f[R]. .IP "13." 4 \f[B]asciify(E)\f[R]: If \f[B]E\f[R] is a string, returns a string that is the first letter of its argument. If it is a number, calculates the number mod \f[B]256\f[R] and returns that number as a one-character string. This is a \f[B]non-portable extension\f[R]. .IP "14." 4 \f[B]asciify(I[])\f[R]: A string that is made up of the characters that would result from running \f[B]asciify(E)\f[R] on each element of the array identified by the argument. This allows creating multi-character strings and storing them. This is a \f[B]non-portable extension\f[R]. .IP "15." 4 \f[B]I()\f[R], \f[B]I(E)\f[R], \f[B]I(E, E)\f[R], and so on, where \f[B]I\f[R] is an identifier for a non-\f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). The \f[B]E\f[R] argument(s) may also be arrays of the form \f[B]I[]\f[R], which will automatically be turned into array references (see the \f[I]Array References\f[R] subsection of the \f[B]FUNCTIONS\f[R] section) if the corresponding parameter in the function definition is an array reference. .IP "16." 4 \f[B]read()\f[R]: Reads a line from \f[B]stdin\f[R] and uses that as an expression. The result of that expression is the result of the \f[B]read()\f[R] operand. This is a \f[B]non-portable extension\f[R]. .IP "17." 4 \f[B]maxibase()\f[R]: The max allowable \f[B]ibase\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "18." 4 \f[B]maxobase()\f[R]: The max allowable \f[B]obase\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "19." 4 \f[B]maxscale()\f[R]: The max allowable \f[B]scale\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "20." 4 \f[B]line_length()\f[R]: The line length set with \f[B]BC_LINE_LENGTH\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). This is a \f[B]non-portable extension\f[R]. .IP "21." 4 \f[B]global_stacks()\f[R]: \f[B]0\f[R] if global stacks are not enabled with the \f[B]-g\f[R] or \f[B]--global-stacks\f[R] options, non-zero otherwise. See the \f[B]OPTIONS\f[R] section. This is a \f[B]non-portable extension\f[R]. .IP "22." 4 \f[B]leading_zero()\f[R]: \f[B]0\f[R] if leading zeroes are not enabled with the \f[B]-z\f[R] or \f[B]\[en]leading-zeroes\f[R] options, non-zero otherwise. See the \f[B]OPTIONS\f[R] section. This is a \f[B]non-portable extension\f[R]. .SS Numbers -.PP Numbers are strings made up of digits, uppercase letters, and at most \f[B]1\f[R] period for a radix. Numbers can have up to \f[B]BC_NUM_MAX\f[R] digits. Uppercase letters are equal to \f[B]9\f[R] plus their position in the alphabet, starting from \f[B]1\f[R] (i.e., \f[B]A\f[R] equals \f[B]10\f[R], or \f[B]9+1\f[R]). .PP If a digit or letter makes no sense with the current value of \f[B]ibase\f[R] (i.e., they are greater than or equal to the current value of \f[B]ibase\f[R]), then the behavior depends on the existence of the \f[B]-c\f[R]/\f[B]--digit-clamp\f[R] or \f[B]-C\f[R]/\f[B]--no-digit-clamp\f[R] options (see the \f[B]OPTIONS\f[R] section), the existence and setting of the \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or the default, which can be queried with the \f[B]-h\f[R]/\f[B]--help\f[R] option. .PP If clamping is off, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are not changed. Instead, their given value is multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*A+3\[ha]0*B\f[R], which is \f[B]3\f[R] times \f[B]10\f[R] plus \f[B]11\f[R], or \f[B]41\f[R]. .PP If clamping is on, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are set to the value of the highest valid digit in \f[B]ibase\f[R] before being multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*2+3\[ha]0*2\f[R], which is \f[B]3\f[R] times \f[B]2\f[R] plus \f[B]2\f[R], or \f[B]8\f[R]. .PP There is one exception to clamping: single-character numbers (i.e., \f[B]A\f[R] alone). Such numbers are never clamped and always take the value they would have in the highest possible \f[B]ibase\f[R]. This means that \f[B]A\f[R] alone always equals decimal \f[B]10\f[R] and \f[B]Z\f[R] alone always equals decimal \f[B]35\f[R]. This behavior is mandated by the standard (see the STANDARDS section) and is meant to provide an easy way to set the current \f[B]ibase\f[R] (with the \f[B]i\f[R] command) regardless of the current value of \f[B]ibase\f[R]. .PP If clamping is on, and the clamped value of a character is needed, use a leading zero, i.e., for \f[B]A\f[R], use \f[B]0A\f[R]. .SS Operators -.PP The following arithmetic and logical operators can be used. They are listed in order of decreasing precedence. Operators in the same group have the same precedence. .TP \f[B]++\f[R] \f[B]--\f[R] Type: Prefix and Postfix .RS .PP Associativity: None .PP Description: \f[B]increment\f[R], \f[B]decrement\f[R] .RE .TP \f[B]-\f[R] \f[B]!\f[R] Type: Prefix .RS .PP Associativity: None .PP Description: \f[B]negation\f[R], \f[B]boolean not\f[R] .RE .TP \f[B]\[ha]\f[R] Type: Binary .RS .PP Associativity: Right .PP Description: \f[B]power\f[R] .RE .TP \f[B]*\f[R] \f[B]/\f[R] \f[B]%\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]multiply\f[R], \f[B]divide\f[R], \f[B]modulus\f[R] .RE .TP \f[B]+\f[R] \f[B]-\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]add\f[R], \f[B]subtract\f[R] .RE .TP \f[B]=\f[R] \f[B]+=\f[R] \f[B]-=\f[R] \f[B]*=\f[R] \f[B]/=\f[R] \f[B]%=\f[R] \f[B]\[ha]=\f[R] Type: Binary .RS .PP Associativity: Right .PP Description: \f[B]assignment\f[R] .RE .TP \f[B]==\f[R] \f[B]<=\f[R] \f[B]>=\f[R] \f[B]!=\f[R] \f[B]<\f[R] \f[B]>\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]relational\f[R] .RE .TP \f[B]&&\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]boolean and\f[R] .RE .TP \f[B]||\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]boolean or\f[R] .RE .PP The operators will be described in more detail below. .TP \f[B]++\f[R] \f[B]--\f[R] The prefix and postfix \f[B]increment\f[R] and \f[B]decrement\f[R] -operators behave exactly like they would in C. -They require a named expression (see the \f[I]Named Expressions\f[R] -subsection) as an operand. +operators behave exactly like they would in C. They require a named +expression (see the \f[I]Named Expressions\f[R] subsection) as an +operand. .RS .PP The prefix versions of these operators are more efficient; use them where possible. .RE .TP \f[B]-\f[R] The \f[B]negation\f[R] operator returns \f[B]0\f[R] if a user attempts to negate any expression with the value \f[B]0\f[R]. Otherwise, a copy of the expression with its sign flipped is returned. .TP \f[B]!\f[R] The \f[B]boolean not\f[R] operator returns \f[B]1\f[R] if the expression is \f[B]0\f[R], or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[ha]\f[R] The \f[B]power\f[R] operator (not the \f[B]exclusive or\f[R] operator, as it would be in C) takes two expressions and raises the first to the power of the value of the second. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The second expression must be an integer (no \f[I]scale\f[R]), and if it is negative, the first value must be non-zero. .RE .TP \f[B]*\f[R] The \f[B]multiply\f[R] operator takes two expressions, multiplies them, and returns the product. If \f[B]a\f[R] is the \f[I]scale\f[R] of the first expression and \f[B]b\f[R] is the \f[I]scale\f[R] of the second expression, the \f[I]scale\f[R] of the result is equal to \f[B]min(a+b,max(scale,a,b))\f[R] where \f[B]min()\f[R] and \f[B]max()\f[R] return the obvious values. .TP \f[B]/\f[R] The \f[B]divide\f[R] operator takes two expressions, divides them, and returns the quotient. The \f[I]scale\f[R] of the result shall be the value of \f[B]scale\f[R]. .RS .PP The second expression must be non-zero. .RE .TP \f[B]%\f[R] The \f[B]modulus\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and evaluates them by 1) Computing \f[B]a/b\f[R] to current \f[B]scale\f[R] and 2) Using the result of step 1 to calculate \f[B]a-(a/b)*b\f[R] to \f[I]scale\f[R] \f[B]max(scale+scale(b),scale(a))\f[R]. .RS .PP The second expression must be non-zero. .RE .TP \f[B]+\f[R] The \f[B]add\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and returns the sum, with a \f[I]scale\f[R] equal to the max of the \f[I]scale\f[R]s of \f[B]a\f[R] and \f[B]b\f[R]. .TP \f[B]-\f[R] The \f[B]subtract\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and returns the difference, with a \f[I]scale\f[R] equal to the max of the \f[I]scale\f[R]s of \f[B]a\f[R] and \f[B]b\f[R]. .TP \f[B]=\f[R] \f[B]+=\f[R] \f[B]-=\f[R] \f[B]*=\f[R] \f[B]/=\f[R] \f[B]%=\f[R] \f[B]\[ha]=\f[R] The \f[B]assignment\f[R] operators take two expressions, \f[B]a\f[R] and \f[B]b\f[R] where \f[B]a\f[R] is a named expression (see the \f[I]Named Expressions\f[R] subsection). .RS .PP For \f[B]=\f[R], \f[B]b\f[R] is copied and the result is assigned to \f[B]a\f[R]. For all others, \f[B]a\f[R] and \f[B]b\f[R] are applied as operands to the corresponding arithmetic operator and the result is assigned to \f[B]a\f[R]. .RE .TP \f[B]==\f[R] \f[B]<=\f[R] \f[B]>=\f[R] \f[B]!=\f[R] \f[B]<\f[R] \f[B]>\f[R] The \f[B]relational\f[R] operators compare two expressions, \f[B]a\f[R] and \f[B]b\f[R], and if the relation holds, according to C language semantics, the result is \f[B]1\f[R]. Otherwise, it is \f[B]0\f[R]. .RS .PP Note that unlike in C, these operators have a lower precedence than the \f[B]assignment\f[R] operators, which means that \f[B]a=b>c\f[R] is interpreted as \f[B](a=b)>c\f[R]. .PP Also, unlike the standard (see the \f[B]STANDARDS\f[R] section) requires, these operators can appear anywhere any other expressions can be used. This allowance is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]&&\f[R] The \f[B]boolean and\f[R] operator takes two expressions and returns \f[B]1\f[R] if both expressions are non-zero, \f[B]0\f[R] otherwise. .RS .PP This is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]||\f[R] The \f[B]boolean or\f[R] operator takes two expressions and returns \f[B]1\f[R] if one of the expressions is non-zero, \f[B]0\f[R] otherwise. .RS .PP This is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Statements -.PP The following items are statements: .IP " 1." 4 \f[B]E\f[R] .IP " 2." 4 \f[B]{\f[R] \f[B]S\f[R] \f[B];\f[R] \&... \f[B];\f[R] \f[B]S\f[R] \f[B]}\f[R] .IP " 3." 4 \f[B]if\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] .IP " 4." 4 \f[B]if\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] \f[B]else\f[R] \f[B]S\f[R] .IP " 5." 4 \f[B]while\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] .IP " 6." 4 \f[B]for\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B];\f[R] \f[B]E\f[R] \f[B];\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] .IP " 7." 4 An empty statement .IP " 8." 4 \f[B]break\f[R] .IP " 9." 4 \f[B]continue\f[R] .IP "10." 4 \f[B]quit\f[R] .IP "11." 4 \f[B]halt\f[R] .IP "12." 4 \f[B]limits\f[R] .IP "13." 4 A string of characters, enclosed in double quotes .IP "14." 4 \f[B]print\f[R] \f[B]E\f[R] \f[B],\f[R] \&... \f[B],\f[R] \f[B]E\f[R] .IP "15." 4 \f[B]stream\f[R] \f[B]E\f[R] \f[B],\f[R] \&... \f[B],\f[R] \f[B]E\f[R] .IP "16." 4 \f[B]I()\f[R], \f[B]I(E)\f[R], \f[B]I(E, E)\f[R], and so on, where \f[B]I\f[R] is an identifier for a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). The \f[B]E\f[R] argument(s) may also be arrays of the form \f[B]I[]\f[R], which will automatically be turned into array references (see the \f[I]Array References\f[R] subsection of the \f[B]FUNCTIONS\f[R] section) if the corresponding parameter in the function definition is an array reference. .PP Numbers 4, 9, 11, 12, 14, 15, and 16 are \f[B]non-portable extensions\f[R]. .PP Also, as a \f[B]non-portable extension\f[R], any or all of the expressions in the header of a for loop may be omitted. If the condition (second expression) is omitted, it is assumed to be a constant \f[B]1\f[R]. .PP The \f[B]break\f[R] statement causes a loop to stop iterating and resume execution immediately following a loop. This is only allowed in loops. .PP The \f[B]continue\f[R] statement causes a loop iteration to stop early and returns to the start of the loop, including testing the loop condition. This is only allowed in loops. .PP The \f[B]if\f[R] \f[B]else\f[R] statement does the same thing as in C. .PP The \f[B]quit\f[R] statement causes bc(1) to quit, even if it is on a branch that will not be executed (it is a compile-time command). .PP \f[B]Warning\f[R]: The behavior of this bc(1) on \f[B]quit\f[R] is slightly different from other bc(1) implementations. Other bc(1) implementations will exit as soon as they finish parsing the line that a \f[B]quit\f[R] command is on. This bc(1) will execute any completed and executable statements that occur before the \f[B]quit\f[R] statement before exiting. .PP In other words, for the bc(1) code below: .IP -.nf -\f[C] +.EX for (i = 0; i < 3; ++i) i; quit -\f[R] -.fi +.EE .PP Other bc(1) implementations will print nothing, and this bc(1) will print \f[B]0\f[R], \f[B]1\f[R], and \f[B]2\f[R] on successive lines before exiting. .PP The \f[B]halt\f[R] statement causes bc(1) to quit, if it is executed. (Unlike \f[B]quit\f[R] if it is on a branch of an \f[B]if\f[R] statement that is not executed, bc(1) does not quit.) .PP The \f[B]limits\f[R] statement prints the limits that this bc(1) is subject to. This is like the \f[B]quit\f[R] statement in that it is a compile-time command. .PP An expression by itself is evaluated and printed, followed by a newline. .SS Strings -.PP If strings appear as a statement by themselves, they are printed without a trailing newline. .PP In addition to appearing as a lone statement by themselves, strings can be assigned to variables and array elements. They can also be passed to functions in variable parameters. .PP If any statement that expects a string is given a variable that had a string assigned to it, the statement acts as though it had received a string. .PP If any math operation is attempted on a string or a variable or array element that has been assigned a string, an error is raised, and bc(1) resets (see the \f[B]RESET\f[R] section). .PP Assigning strings to variables and array elements and passing them to functions are \f[B]non-portable extensions\f[R]. .SS Print Statement -.PP The \[lq]expressions\[rq] in a \f[B]print\f[R] statement may also be strings. If they are, there are backslash escape sequences that are interpreted specially. What those sequences are, and what they cause to be printed, are shown below: .PP \f[B]\[rs]a\f[R]: \f[B]\[rs]a\f[R] .PP \f[B]\[rs]b\f[R]: \f[B]\[rs]b\f[R] .PP \f[B]\[rs]\[rs]\f[R]: \f[B]\[rs]\f[R] .PP \f[B]\[rs]e\f[R]: \f[B]\[rs]\f[R] .PP \f[B]\[rs]f\f[R]: \f[B]\[rs]f\f[R] .PP \f[B]\[rs]n\f[R]: \f[B]\[rs]n\f[R] .PP \f[B]\[rs]q\f[R]: \f[B]\[lq]\f[R] .PP \f[B]\[rs]r\f[R]: \f[B]\[rs]r\f[R] .PP \f[B]\[rs]t\f[R]: \f[B]\[rs]t\f[R] .PP Any other character following a backslash causes the backslash and character to be printed as-is. .PP Any non-string expression in a print statement shall be assigned to \f[B]last\f[R], like any other expression that is printed. .SS Stream Statement -.PP The expressions in a \f[B]stream\f[R] statement may also be strings. .PP If a \f[B]stream\f[R] statement is given a string, it prints the string as though the string had appeared as its own statement. In other words, the \f[B]stream\f[R] statement prints strings normally, without a newline. .PP If a \f[B]stream\f[R] statement is given a number, a copy of it is truncated and its absolute value is calculated. The result is then printed as though \f[B]obase\f[R] is \f[B]256\f[R] and each digit is interpreted as an 8-bit ASCII character, making it a byte stream. .SS Order of Evaluation -.PP All expressions in a statment are evaluated left to right, except as necessary to maintain order of operations. This means, for example, assuming that \f[B]i\f[R] is equal to \f[B]0\f[R], in the expression .IP -.nf -\f[C] +.EX a[i++] = i++ -\f[R] -.fi +.EE .PP the first (or 0th) element of \f[B]a\f[R] is set to \f[B]1\f[R], and \f[B]i\f[R] is equal to \f[B]2\f[R] at the end of the expression. .PP This includes function arguments. Thus, assuming \f[B]i\f[R] is equal to \f[B]0\f[R], this means that in the expression .IP -.nf -\f[C] +.EX x(i++, i++) -\f[R] -.fi +.EE .PP the first argument passed to \f[B]x()\f[R] is \f[B]0\f[R], and the second argument is \f[B]1\f[R], while \f[B]i\f[R] is equal to \f[B]2\f[R] before the function starts executing. .SH FUNCTIONS -.PP Function definitions are as follows: .IP -.nf -\f[C] +.EX define I(I,...,I){ auto I,...,I S;...;S return(E) } -\f[R] -.fi +.EE .PP Any \f[B]I\f[R] in the parameter list or \f[B]auto\f[R] list may be replaced with \f[B]I[]\f[R] to make a parameter or \f[B]auto\f[R] var an array, and any \f[B]I\f[R] in the parameter list may be replaced with \f[B]*I[]\f[R] to make a parameter an array reference. Callers of functions that take array references should not put an asterisk in the call; they must be called with just \f[B]I[]\f[R] like normal array parameters and will be automatically converted into references. .PP As a \f[B]non-portable extension\f[R], the opening brace of a \f[B]define\f[R] statement may appear on the next line. .PP As a \f[B]non-portable extension\f[R], the return statement may also be in one of the following forms: .IP "1." 3 \f[B]return\f[R] .IP "2." 3 \f[B]return\f[R] \f[B](\f[R] \f[B])\f[R] .IP "3." 3 \f[B]return\f[R] \f[B]E\f[R] .PP The first two, or not specifying a \f[B]return\f[R] statement, is equivalent to \f[B]return (0)\f[R], unless the function is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection below). .SS Void Functions -.PP Functions can also be \f[B]void\f[R] functions, defined as follows: .IP -.nf -\f[C] +.EX define void I(I,...,I){ auto I,...,I S;...;S return } -\f[R] -.fi +.EE .PP They can only be used as standalone expressions, where such an expression would be printed alone, except in a print statement. .PP Void functions can only use the first two \f[B]return\f[R] statements listed above. They can also omit the return statement entirely. .PP The word \[lq]void\[rq] is not treated as a keyword; it is still possible to have variables, arrays, and functions named \f[B]void\f[R]. The word \[lq]void\[rq] is only treated specially right after the \f[B]define\f[R] keyword. .PP This is a \f[B]non-portable extension\f[R]. .SS Array References -.PP For any array in the parameter list, if the array is declared in the form .IP -.nf -\f[C] +.EX *I[] -\f[R] -.fi +.EE .PP it is a \f[B]reference\f[R]. Any changes to the array in the function are reflected, when the function returns, to the array that was passed in. .PP Other than this, all function arguments are passed by value. .PP This is a \f[B]non-portable extension\f[R]. .SH LIBRARY -.PP All of the functions below are available when the \f[B]-l\f[R] or \f[B]--mathlib\f[R] command-line flags are given. .SS Standard Library -.PP The standard (see the \f[B]STANDARDS\f[R] section) defines the following functions for the math library: .TP \f[B]s(x)\f[R] Returns the sine of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]c(x)\f[R] Returns the cosine of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]a(x)\f[R] Returns the arctangent of \f[B]x\f[R], in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]l(x)\f[R] Returns the natural logarithm of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]e(x)\f[R] Returns the mathematical constant \f[B]e\f[R] raised to the power of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]j(x, n)\f[R] Returns the bessel integer order \f[B]n\f[R] (truncated) of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .SS Transcendental Functions -.PP All transcendental functions can return slightly inaccurate results, up to 1 ULP (https://en.wikipedia.org/wiki/Unit_in_the_last_place). This is unavoidable, and the article at https://people.eecs.berkeley.edu/\[ti]wkahan/LOG10HAF.TXT explains why it is impossible and unnecessary to calculate exact results for the transcendental functions. .PP Because of the possible inaccuracy, I recommend that users call those functions with the precision (\f[B]scale\f[R]) set to at least 1 higher than is necessary. If exact results are \f[I]absolutely\f[R] required, users can double the precision (\f[B]scale\f[R]) and then truncate. .PP The transcendental functions in the standard math library are: .IP \[bu] 2 \f[B]s(x)\f[R] .IP \[bu] 2 \f[B]c(x)\f[R] .IP \[bu] 2 \f[B]a(x)\f[R] .IP \[bu] 2 \f[B]l(x)\f[R] .IP \[bu] 2 \f[B]e(x)\f[R] .IP \[bu] 2 \f[B]j(x, n)\f[R] .SH RESET -.PP When bc(1) encounters an error or a signal that it has a non-default handler for, it resets. This means that several things happen. .PP First, any functions that are executing are stopped and popped off the stack. The behavior is not unlike that of exceptions in programming languages. Then the execution point is set so that any code waiting to execute (after all functions returned) is skipped. .PP Thus, when bc(1) resets, it skips any remaining code waiting to be executed. Then, if it is interactive mode, and the error was not a fatal error (see the \f[B]EXIT STATUS\f[R] section), it asks for more input; otherwise, it exits with the appropriate return code. .PP Note that this reset behavior is different from the GNU bc(1), which attempts to start executing the statement right after the one that caused an error. .SH PERFORMANCE -.PP Most bc(1) implementations use \f[B]char\f[R] types to calculate the value of \f[B]1\f[R] decimal digit at a time, but that can be slow. This bc(1) does something different. .PP It uses large integers to calculate more than \f[B]1\f[R] decimal digit at a time. If built in a environment where \f[B]BC_LONG_BIT\f[R] (see the \f[B]LIMITS\f[R] section) is \f[B]64\f[R], then each integer has \f[B]9\f[R] decimal digits. If built in an environment where \f[B]BC_LONG_BIT\f[R] is \f[B]32\f[R] then each integer has \f[B]4\f[R] decimal digits. This value (the number of decimal digits per large integer) is called \f[B]BC_BASE_DIGS\f[R]. .PP The actual values of \f[B]BC_LONG_BIT\f[R] and \f[B]BC_BASE_DIGS\f[R] can be queried with the \f[B]limits\f[R] statement. .PP In addition, this bc(1) uses an even larger integer for overflow checking. This integer type depends on the value of \f[B]BC_LONG_BIT\f[R], but is always at least twice as large as the integer type used to store digits. .SH LIMITS -.PP The following are the limits on bc(1): .TP \f[B]BC_LONG_BIT\f[R] The number of bits in the \f[B]long\f[R] type in the environment where bc(1) was built. This determines how many decimal digits can be stored in a single large integer (see the \f[B]PERFORMANCE\f[R] section). .TP \f[B]BC_BASE_DIGS\f[R] The number of decimal digits per large integer (see the \f[B]PERFORMANCE\f[R] section). Depends on \f[B]BC_LONG_BIT\f[R]. .TP \f[B]BC_BASE_POW\f[R] The max decimal number that each large integer can store (see \f[B]BC_BASE_DIGS\f[R]) plus \f[B]1\f[R]. Depends on \f[B]BC_BASE_DIGS\f[R]. .TP \f[B]BC_OVERFLOW_MAX\f[R] The max number that the overflow type (see the \f[B]PERFORMANCE\f[R] section) can hold. Depends on \f[B]BC_LONG_BIT\f[R]. .TP \f[B]BC_BASE_MAX\f[R] The maximum output base. Set at \f[B]BC_BASE_POW\f[R]. .TP \f[B]BC_DIM_MAX\f[R] The maximum size of arrays. Set at \f[B]SIZE_MAX-1\f[R]. .TP \f[B]BC_SCALE_MAX\f[R] The maximum \f[B]scale\f[R]. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_STRING_MAX\f[R] The maximum length of strings. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_NAME_MAX\f[R] The maximum length of identifiers. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_NUM_MAX\f[R] The maximum length of a number (in decimal digits), which includes digits after the decimal point. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP Exponent The maximum allowable exponent (positive or negative). Set at \f[B]BC_OVERFLOW_MAX\f[R]. .TP Number of vars The maximum number of vars/arrays. Set at \f[B]SIZE_MAX-1\f[R]. .PP The actual values can be queried with the \f[B]limits\f[R] statement. .PP These limits are meant to be effectively non-existent; the limits are so large (at least on 64-bit machines) that there should not be any point at which they become a problem. In fact, memory should be exhausted before these limits should be hit. .SH ENVIRONMENT VARIABLES -.PP As \f[B]non-portable extensions\f[R], bc(1) recognizes the following environment variables: .TP \f[B]POSIXLY_CORRECT\f[R] If this variable exists (no matter the contents), bc(1) behaves as if the \f[B]-s\f[R] option was given. .TP \f[B]BC_ENV_ARGS\f[R] This is another way to give command-line arguments to bc(1). They should be in the same format as all other command-line arguments. These are always processed first, so any files given in \f[B]BC_ENV_ARGS\f[R] will be processed before arguments and files given on the command-line. This gives the user the ability to set up \[lq]standard\[rq] options and files to be used at every invocation. The most useful thing for such files to contain would be useful functions that the user might want every time bc(1) runs. .RS .PP The code that parses \f[B]BC_ENV_ARGS\f[R] will correctly handle quoted arguments, but it does not understand escape sequences. For example, the string \f[B]\[lq]/home/gavin/some bc file.bc\[rq]\f[R] will be correctly parsed, but the string \f[B]\[lq]/home/gavin/some \[dq]bc\[dq] file.bc\[rq]\f[R] will include the backslashes. .PP The quote parsing will handle either kind of quotes, \f[B]\[cq]\f[R] or \f[B]\[lq]\f[R]. Thus, if you have a file with any number of single quotes in the name, you can use double quotes as the outside quotes, as in \f[B]\[lq]some `bc' file.bc\[rq]\f[R], and vice versa if you have a file with double quotes. However, handling a file with both kinds of quotes in \f[B]BC_ENV_ARGS\f[R] is not supported due to the complexity of the parsing, though such files are still supported on the command-line where the parsing is done by the shell. .RE .TP \f[B]BC_LINE_LENGTH\f[R] If this environment variable exists and contains an integer that is greater than \f[B]1\f[R] and is less than \f[B]UINT16_MAX\f[R] (\f[B]2\[ha]16-1\f[R]), bc(1) will output lines to that length, including the backslash (\f[B]\[rs]\f[R]). The default line length is \f[B]70\f[R]. .RS .PP The special value of \f[B]0\f[R] will disable line length checking and print numbers without regard to line length and without backslashes and newlines. .RE .TP \f[B]BC_BANNER\f[R] If this environment variable exists and contains an integer, then a non-zero value activates the copyright banner when bc(1) is in interactive mode, while zero deactivates it. .RS .PP If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), then this environment variable has no effect because bc(1) does not print the banner when not in interactive mode. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_SIGINT_RESET\f[R] If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), then this environment variable has no effect because bc(1) exits on \f[B]SIGINT\f[R] when not in interactive mode. .RS .PP However, when bc(1) is in interactive mode, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) reset on \f[B]SIGINT\f[R], rather than exit, and zero makes bc(1) exit. If this environment variable exists and is \f[I]not\f[R] an integer, then bc(1) will exit on \f[B]SIGINT\f[R]. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_TTY_MODE\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, then a non-zero value makes bc(1) use TTY mode, and zero makes bc(1) not use TTY mode. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_PROMPT\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) use a prompt, and zero or a non-integer makes bc(1) not use a prompt. If this environment variable does not exist and \f[B]BC_TTY_MODE\f[R] does, then the value of the \f[B]BC_TTY_MODE\f[R] environment variable is used. .PP This environment variable and the \f[B]BC_TTY_MODE\f[R] environment variable override the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_EXPR_EXIT\f[R] If any expressions or expression files are given on the command-line with \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R], then if this environment variable exists and contains an integer, a non-zero value makes bc(1) exit after executing the expressions and expression files, and a zero value makes bc(1) not exit. .RS .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_DIGIT_CLAMP\f[R] When parsing numbers and if this environment variable exists and contains an integer, a non-zero value makes bc(1) clamp digits that are greater than or equal to the current \f[B]ibase\f[R] so that all such digits are considered equal to the \f[B]ibase\f[R] minus 1, and a zero value disables such clamping so that those digits are always equal to their value, which is multiplied by the power of the \f[B]ibase\f[R]. .RS .PP This never applies to single-digit numbers, as per the standard (see the \f[B]STANDARDS\f[R] section). .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .SH EXIT STATUS -.PP bc(1) returns the following exit statuses: .TP \f[B]0\f[R] No error. .TP \f[B]1\f[R] A math error occurred. This follows standard practice of using \f[B]1\f[R] for expected errors, since math errors will happen in the process of normal execution. .RS .PP Math errors include divide by \f[B]0\f[R], taking the square root of a negative number, attempting to convert a negative number to a hardware integer, overflow when converting a number to a hardware integer, overflow when calculating the size of a number, and attempting to use a non-integer where an integer is required. .PP Converting to a hardware integer happens for the second operand of the power (\f[B]\[ha]\f[R]) operator and the corresponding assignment operator. .RE .TP \f[B]2\f[R] A parse error occurred. .RS .PP Parse errors include unexpected \f[B]EOF\f[R], using an invalid character, failing to find the end of a string or comment, using a token where it is invalid, giving an invalid expression, giving an invalid print statement, giving an invalid function definition, attempting to assign to an expression that is not a named expression (see the \f[I]Named Expressions\f[R] subsection of the \f[B]SYNTAX\f[R] section), giving an invalid \f[B]auto\f[R] list, having a duplicate \f[B]auto\f[R]/function parameter, failing to find the end of a code block, attempting to return a value from a \f[B]void\f[R] function, attempting to use a variable as a reference, and using any extensions when the option \f[B]-s\f[R] or any equivalents were given. .RE .TP \f[B]3\f[R] A runtime error occurred. .RS .PP Runtime errors include assigning an invalid number to any global (\f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R]), giving a bad expression to a \f[B]read()\f[R] call, calling \f[B]read()\f[R] inside of a \f[B]read()\f[R] call, type errors, passing the wrong number of arguments to functions, attempting to call an undefined function, and attempting to use a \f[B]void\f[R] function call as a value in an expression. .RE .TP \f[B]4\f[R] A fatal error occurred. .RS .PP Fatal errors include memory allocation errors, I/O errors, failing to open files, attempting to use files that do not have only ASCII characters (bc(1) only accepts ASCII characters), attempting to open a directory as a file, and giving invalid command-line options. .RE .PP The exit status \f[B]4\f[R] is special; when a fatal error occurs, bc(1) always exits and returns \f[B]4\f[R], no matter what mode bc(1) is in. .PP The other statuses will only be returned when bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), since bc(1) resets its state (see the \f[B]RESET\f[R] section) and accepts more input when one of those errors occurs in interactive mode. This is also the case when interactive mode is forced by the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .PP These exit statuses allow bc(1) to be used in shell scripting with error checking, and its normal behavior can be forced by using the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .SH INTERACTIVE MODE -.PP Per the standard (see the \f[B]STANDARDS\f[R] section), bc(1) has an interactive mode and a non-interactive mode. Interactive mode is turned on automatically when both \f[B]stdin\f[R] and \f[B]stdout\f[R] are hooked to a terminal, but the \f[B]-i\f[R] flag and \f[B]--interactive\f[R] option can turn it on in other situations. .PP In interactive mode, bc(1) attempts to recover from errors (see the \f[B]RESET\f[R] section), and in normal execution, flushes \f[B]stdout\f[R] as soon as execution is done for the current input. bc(1) may also reset on \f[B]SIGINT\f[R] instead of exit, depending on the contents of, or default for, the \f[B]BC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .SH TTY MODE -.PP If \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY, then \[lq]TTY mode\[rq] is considered to be available, and thus, bc(1) can turn on TTY mode, subject to some settings. .PP If there is the environment variable \f[B]BC_TTY_MODE\f[R] in the environment (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then if that environment variable contains a non-zero integer, bc(1) will turn on TTY mode when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY. If the \f[B]BC_TTY_MODE\f[R] environment variable exists but is \f[I]not\f[R] a non-zero integer, then bc(1) will not turn TTY mode on. .PP If the environment variable \f[B]BC_TTY_MODE\f[R] does \f[I]not\f[R] exist, the default setting is used. The default setting can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP TTY mode is different from interactive mode because interactive mode is required in the bc(1) standard (see the \f[B]STANDARDS\f[R] section), and interactive mode requires only \f[B]stdin\f[R] and \f[B]stdout\f[R] to be connected to a terminal. .SS Prompt -.PP If TTY mode is available, then a prompt can be enabled. Like TTY mode itself, it can be turned on or off with an environment variable: \f[B]BC_PROMPT\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP If the environment variable \f[B]BC_PROMPT\f[R] exists and is a non-zero integer, then the prompt is turned on when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options were not used. The read prompt will be turned on under the same conditions, except that the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options must also not be used. .PP However, if \f[B]BC_PROMPT\f[R] does not exist, the prompt can be enabled or disabled with the \f[B]BC_TTY_MODE\f[R] environment variable, the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options, and the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options. See the \f[B]ENVIRONMENT VARIABLES\f[R] and \f[B]OPTIONS\f[R] sections for more details. .SH SIGNAL HANDLING -.PP Sending a \f[B]SIGINT\f[R] will cause bc(1) to do one of two things. .PP If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), or the \f[B]BC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or its default, is either not an integer or it is zero, bc(1) will exit. .PP However, if bc(1) is in interactive mode, and the \f[B]BC_SIGINT_RESET\f[R] or its default is an integer and non-zero, then bc(1) will stop executing the current input and reset (see the \f[B]RESET\f[R] section) upon receiving a \f[B]SIGINT\f[R]. .PP Note that \[lq]current input\[rq] can mean one of two things. If bc(1) is processing input from \f[B]stdin\f[R] in interactive mode, it will ask for more input. If bc(1) is processing input from a file in interactive mode, it will stop processing the file and start processing the next file, if one exists, or ask for input from \f[B]stdin\f[R] if no other file exists. .PP This means that if a \f[B]SIGINT\f[R] is sent to bc(1) as it is executing a file, it can seem as though bc(1) did not respond to the signal since it will immediately start executing the next file. This is by design; most files that users execute when interacting with bc(1) have function definitions, which are quick to parse. If a file takes a long time to execute, there may be a bug in that file. The rest of the files could still be executed without problem, allowing the user to continue. .PP \f[B]SIGTERM\f[R] and \f[B]SIGQUIT\f[R] cause bc(1) to clean up and exit, and it uses the default handler for all other signals. .SH LOCALES -.PP This bc(1) ships with support for adding error messages for different locales and thus, supports \f[B]LC_MESSAGES\f[R]. .SH SEE ALSO -.PP dc(1) .SH STANDARDS -.PP bc(1) is compliant with the IEEE Std 1003.1-2017 (\[lq]POSIX.1-2017\[rq]) specification at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html . The flags \f[B]-efghiqsvVw\f[R], all long options, and the extensions noted above are extensions to that specification. .PP In addition, the behavior of the \f[B]quit\f[R] implements an interpretation of that specification that is different from all known implementations. For more information see the \f[B]Statements\f[R] subsection of the \f[B]SYNTAX\f[R] section. .PP Note that the specification explicitly says that bc(1) only accepts numbers that use a period (\f[B].\f[R]) as a radix point, regardless of the value of \f[B]LC_NUMERIC\f[R]. .PP This bc(1) supports error messages for different locales, and thus, it supports \f[B]LC_MESSAGES\f[R]. .SH BUGS -.PP Before version \f[B]6.1.0\f[R], this bc(1) had incorrect behavior for the \f[B]quit\f[R] statement. .PP No other bugs are known. Report bugs at https://git.gavinhoward.com/gavin/bc . .SH AUTHORS -.PP -Gavin D. -Howard and contributors. +Gavin D. Howard \c +.MT gavin@gavinhoward.com +.ME \c +\ and contributors. diff --git a/contrib/bc/manuals/bc/EHN.1 b/contrib/bc/manuals/bc/EHN.1 index 72790726a4f4..290aec48a6e9 100644 --- a/contrib/bc/manuals/bc/EHN.1 +++ b/contrib/bc/manuals/bc/EHN.1 @@ -1,1780 +1,1727 @@ .\" .\" SPDX-License-Identifier: BSD-2-Clause .\" .\" Copyright (c) 2018-2023 Gavin D. Howard and contributors. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions are met: .\" .\" * Redistributions of source code must retain the above copyright notice, .\" this list of conditions and the following disclaimer. .\" .\" * Redistributions in binary form must reproduce the above copyright notice, .\" this list of conditions and the following disclaimer in the documentation .\" and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.TH "BC" "1" "February 2023" "Gavin D. Howard" "General Commands Manual" +.TH "BC" "1" "November 2023" "Gavin D. Howard" "General Commands Manual" .nh .ad l .SH NAME -.PP bc - arbitrary-precision decimal arithmetic language and calculator .SH SYNOPSIS -.PP \f[B]bc\f[R] [\f[B]-cCghilPqRsvVw\f[R]] [\f[B]--digit-clamp\f[R]] [\f[B]--no-digit-clamp\f[R]] [\f[B]--global-stacks\f[R]] [\f[B]--help\f[R]] [\f[B]--interactive\f[R]] [\f[B]--mathlib\f[R]] [\f[B]--no-prompt\f[R]] [\f[B]--no-read-prompt\f[R]] [\f[B]--quiet\f[R]] [\f[B]--standard\f[R]] [\f[B]--warn\f[R]] [\f[B]--version\f[R]] [\f[B]-e\f[R] \f[I]expr\f[R]] [\f[B]--expression\f[R]=\f[I]expr\f[R]\&...] [\f[B]-f\f[R] \f[I]file\f[R]\&...] [\f[B]--file\f[R]=\f[I]file\f[R]\&...] [\f[I]file\f[R]\&...] .SH DESCRIPTION -.PP bc(1) is an interactive processor for a language first standardized in 1991 by POSIX. (See the \f[B]STANDARDS\f[R] section.) The language provides unlimited precision decimal arithmetic and is somewhat C-like, but there are differences. Such differences will be noted in this document. .PP After parsing and handling options, this bc(1) reads any files given on the command line and executes them before reading from \f[B]stdin\f[R]. .PP This bc(1) is a drop-in replacement for \f[I]any\f[R] bc(1), including (and especially) the GNU bc(1). It also has many extensions and extra features beyond other implementations. .PP \f[B]Note\f[R]: If running this bc(1) on \f[I]any\f[R] script meant for another bc(1) gives a parse error, it is probably because a word this bc(1) reserves as a keyword is used as the name of a function, variable, or array. To fix that, use the command-line option \f[B]-r\f[R] \f[I]keyword\f[R], where \f[I]keyword\f[R] is the keyword that is used as a name in the script. For more information, see the \f[B]OPTIONS\f[R] section. .PP If parsing scripts meant for other bc(1) implementations still does not work, that is a bug and should be reported. See the \f[B]BUGS\f[R] section. .SH OPTIONS -.PP The following are the options that bc(1) accepts. .TP \f[B]-C\f[R], \f[B]--no-digit-clamp\f[R] Disables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that the value added to a number from a digit is always that digit\[cq]s value multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-c\f[R] or \f[B]--digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-c\f[R], \f[B]--digit-clamp\f[R] Enables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that digits that the value added to a number from a digit that is greater than or equal to the ibase is the value of ibase minus 1 all multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-C\f[R] or \f[B]--no-digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-e\f[R] \f[I]expr\f[R], \f[B]--expression\f[R]=\f[I]expr\f[R] Evaluates \f[I]expr\f[R]. If multiple expressions are given, they are evaluated in order. If files are given as well (see the \f[B]-f\f[R] and \f[B]--file\f[R] options), the expressions and files are evaluated in the order given. This means that if a file is given before an expression, the file is read in and evaluated first. .RS .PP If this option is given on the command-line (i.e., not in \f[B]BC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, bc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R], whether on the command-line or in \f[B]BC_ENV_ARGS\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, bc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-f\f[R] \f[I]file\f[R], \f[B]--file\f[R]=\f[I]file\f[R] Reads in \f[I]file\f[R] and evaluates it, line by line, as though it were read through \f[B]stdin\f[R]. If expressions are also given (see the \f[B]-e\f[R] and \f[B]--expression\f[R] options), the expressions are evaluated in the order given. .RS .PP If this option is given on the command-line (i.e., not in \f[B]BC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, bc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, bc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-g\f[R], \f[B]--global-stacks\f[R] Turns the globals \f[B]ibase\f[R], \f[B]obase\f[R], and \f[B]scale\f[R] into stacks. .RS .PP This has the effect that a copy of the current value of all three are pushed onto a stack for every function call, as well as popped when every function returns. This means that functions can assign to any and all of those globals without worrying that the change will affect other functions. Thus, a hypothetical function named \f[B]output(x,b)\f[R] that simply printed \f[B]x\f[R] in base \f[B]b\f[R] could be written like this: .IP -.nf -\f[C] +.EX define void output(x, b) { obase=b x } -\f[R] -.fi +.EE .PP instead of like this: .IP -.nf -\f[C] +.EX define void output(x, b) { auto c c=obase obase=b x obase=c } -\f[R] -.fi +.EE .PP This makes writing functions much easier. .PP However, since using this flag means that functions cannot set \f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R] globally, functions that are made to do so cannot work anymore. There are two possible use cases for that, and each has a solution. .PP First, if a function is called on startup to turn bc(1) into a number converter, it is possible to replace that capability with various shell aliases. Examples: .IP -.nf -\f[C] +.EX alias d2o=\[dq]bc -e ibase=A -e obase=8\[dq] alias h2b=\[dq]bc -e ibase=G -e obase=2\[dq] -\f[R] -.fi +.EE .PP Second, if the purpose of a function is to set \f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R] globally for any other purpose, it could be split into one to three functions (based on how many globals it sets) and each of those functions could return the desired value for a global. .PP If the behavior of this option is desired for every run of bc(1), then users could make sure to define \f[B]BC_ENV_ARGS\f[R] and include this option (see the \f[B]ENVIRONMENT VARIABLES\f[R] section for more details). .PP If \f[B]-s\f[R], \f[B]-w\f[R], or any equivalents are used, this option is ignored. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-h\f[R], \f[B]--help\f[R] Prints a usage message and exits. .TP \f[B]-I\f[R] \f[I]ibase\f[R], \f[B]--ibase\f[R]=\f[I]ibase\f[R] Sets the builtin variable \f[B]ibase\f[R] to the value \f[I]ibase\f[R] assuming that \f[I]ibase\f[R] is in base 10. It is a fatal error if \f[I]ibase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-i\f[R], \f[B]--interactive\f[R] Forces interactive mode. (See the \f[B]INTERACTIVE MODE\f[R] section.) .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-L\f[R], \f[B]--no-line-length\f[R] Disables line length checking and prints numbers without backslashes and newlines. In other words, this option sets \f[B]BC_LINE_LENGTH\f[R] to \f[B]0\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-l\f[R], \f[B]--mathlib\f[R] Sets \f[B]scale\f[R] (see the \f[B]SYNTAX\f[R] section) to \f[B]20\f[R] and loads the included math library before running any code, including any expressions or files specified on the command line. .RS .PP To learn what is in the library, see the \f[B]LIBRARY\f[R] section. .RE .TP \f[B]-O\f[R] \f[I]obase\f[R], \f[B]--obase\f[R]=\f[I]obase\f[R] Sets the builtin variable \f[B]obase\f[R] to the value \f[I]obase\f[R] assuming that \f[I]obase\f[R] is in base 10. It is a fatal error if \f[I]obase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-P\f[R], \f[B]--no-prompt\f[R] Disables the prompt in TTY mode. (The prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a prompt or are not used to having them in bc(1). Most of those users would want to put this option in \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .RS .PP These options override the \f[B]BC_PROMPT\f[R] and \f[B]BC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-q\f[R], \f[B]--quiet\f[R] This option is for compatibility with the GNU bc(1) (https://www.gnu.org/software/bc/); it is a no-op. Without this option, GNU bc(1) prints a copyright header. This bc(1) only prints the copyright header if one or more of the \f[B]-v\f[R], \f[B]-V\f[R], or \f[B]--version\f[R] options are given unless the \f[B]BC_BANNER\f[R] environment variable is set and contains a non-zero integer or if this bc(1) was built with the header displayed by default. If \f[I]any\f[R] of that is the case, then this option \f[I]does\f[R] prevent bc(1) from printing the header. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-R\f[R], \f[B]--no-read-prompt\f[R] Disables the read prompt in TTY mode. (The read prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a read prompt or are not used to having them in bc(1). Most of those users would want to put this option in \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). This option is also useful in hash bang lines of bc(1) scripts that prompt for user input. .RS .PP This option does not disable the regular prompt because the read prompt is only used when the \f[B]read()\f[R] built-in function is called. .PP These options \f[I]do\f[R] override the \f[B]BC_PROMPT\f[R] and \f[B]BC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), but only for the read prompt. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-r\f[R] \f[I]keyword\f[R], \f[B]--redefine\f[R]=\f[I]keyword\f[R] Redefines \f[I]keyword\f[R] in order to allow it to be used as a function, variable, or array name. This is useful when this bc(1) gives parse errors when parsing scripts meant for other bc(1) implementations. .RS .PP The keywords this bc(1) allows to be redefined are: .IP \[bu] 2 \f[B]abs\f[R] .IP \[bu] 2 \f[B]asciify\f[R] .IP \[bu] 2 \f[B]continue\f[R] .IP \[bu] 2 \f[B]divmod\f[R] .IP \[bu] 2 \f[B]else\f[R] .IP \[bu] 2 \f[B]halt\f[R] .IP \[bu] 2 \f[B]last\f[R] .IP \[bu] 2 \f[B]limits\f[R] .IP \[bu] 2 \f[B]maxibase\f[R] .IP \[bu] 2 \f[B]maxobase\f[R] .IP \[bu] 2 \f[B]maxscale\f[R] .IP \[bu] 2 \f[B]modexp\f[R] .IP \[bu] 2 \f[B]print\f[R] .IP \[bu] 2 \f[B]read\f[R] .IP \[bu] 2 \f[B]stream\f[R] .PP If any of those keywords are used as a function, variable, or array name in a script, use this option with the keyword as the argument. If multiple are used, use this option for all of them; it can be used multiple times. .PP Keywords are \f[I]not\f[R] redefined when parsing the builtin math library (see the \f[B]LIBRARY\f[R] section). .PP It is a fatal error to redefine keywords mandated by the POSIX standard (see the \f[B]STANDARDS\f[R] section). It is a fatal error to attempt to redefine words that this bc(1) does not reserve as keywords. .RE .TP \f[B]-S\f[R] \f[I]scale\f[R], \f[B]--scale\f[R]=\f[I]scale\f[R] Sets the builtin variable \f[B]scale\f[R] to the value \f[I]scale\f[R] assuming that \f[I]scale\f[R] is in base 10. It is a fatal error if \f[I]scale\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-s\f[R], \f[B]--standard\f[R] Process exactly the language defined by the standard (see the \f[B]STANDARDS\f[R] section) and error if any extensions are used. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-v\f[R], \f[B]-V\f[R], \f[B]--version\f[R] Print the version information (copyright header) and exits. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-w\f[R], \f[B]--warn\f[R] Like \f[B]-s\f[R] and \f[B]--standard\f[R], except that warnings (and not errors) are printed for non-standard extensions and execution continues normally. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-z\f[R], \f[B]--leading-zeroes\f[R] Makes bc(1) print all numbers greater than \f[B]-1\f[R] and less than \f[B]1\f[R], and not equal to \f[B]0\f[R], with a leading zero. .RS .PP This can be set for individual numbers with the \f[B]plz(x)\f[R], \f[B]plznl(x)\f[R], \f[B]pnlz(x)\f[R], and \f[B]pnlznl(x)\f[R] functions in the extended math library (see the \f[B]LIBRARY\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .PP All long options are \f[B]non-portable extensions\f[R]. .SH STDIN -.PP If no files or expressions are given by the \f[B]-f\f[R], \f[B]--file\f[R], \f[B]-e\f[R], or \f[B]--expression\f[R] options, then bc(1) reads from \f[B]stdin\f[R]. .PP However, there are a few caveats to this. .PP First, \f[B]stdin\f[R] is evaluated a line at a time. The only exception to this is if the parse cannot complete. That means that starting a string without ending it or starting a function, \f[B]if\f[R] statement, or loop without ending it will also cause bc(1) to not execute. .PP Second, after an \f[B]if\f[R] statement, bc(1) doesn\[cq]t know if an \f[B]else\f[R] statement will follow, so it will not execute until it knows there will not be an \f[B]else\f[R] statement. .SH STDOUT -.PP Any non-error output is written to \f[B]stdout\f[R]. In addition, if history (see the \f[B]HISTORY\f[R] section) and the prompt (see the \f[B]TTY MODE\f[R] section) are enabled, both are output to \f[B]stdout\f[R]. .PP \f[B]Note\f[R]: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stdout\f[R], so if \f[B]stdout\f[R] is closed, as in -\f[B]bc >&-\f[R], it will quit with an error. +\f[B]bc >&-\f[R], it will quit with an error. This is done so that bc(1) can report problems when \f[B]stdout\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stdout\f[R] to \f[B]/dev/null\f[R]. .SH STDERR -.PP Any error output is written to \f[B]stderr\f[R]. .PP \f[B]Note\f[R]: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stderr\f[R], so if \f[B]stderr\f[R] is closed, as in -\f[B]bc 2>&-\f[R], it will quit with an error. +\f[B]bc 2>&-\f[R], it will quit with an error. This is done so that bc(1) can exit with an error code when \f[B]stderr\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stderr\f[R] to \f[B]/dev/null\f[R]. .SH SYNTAX -.PP The syntax for bc(1) programs is mostly C-like, with some differences. This bc(1) follows the POSIX standard (see the \f[B]STANDARDS\f[R] section), which is a much more thorough resource for the language this bc(1) accepts. This section is meant to be a summary and a listing of all the extensions to the standard. .PP In the sections below, \f[B]E\f[R] means expression, \f[B]S\f[R] means statement, and \f[B]I\f[R] means identifier. .PP Identifiers (\f[B]I\f[R]) start with a lowercase letter and can be followed by any number (up to \f[B]BC_NAME_MAX-1\f[R]) of lowercase letters (\f[B]a-z\f[R]), digits (\f[B]0-9\f[R]), and underscores (\f[B]_\f[R]). The regex is \f[B][a-z][a-z0-9_]*\f[R]. Identifiers with more than one character (letter) are a \f[B]non-portable extension\f[R]. .PP \f[B]ibase\f[R] is a global variable determining how to interpret constant numbers. It is the \[lq]input\[rq] base, or the number base used for interpreting input numbers. \f[B]ibase\f[R] is initially \f[B]10\f[R]. If the \f[B]-s\f[R] (\f[B]--standard\f[R]) and \f[B]-w\f[R] (\f[B]--warn\f[R]) flags were not given on the command line, the max allowable value for \f[B]ibase\f[R] is \f[B]36\f[R]. Otherwise, it is \f[B]16\f[R]. The min allowable value for \f[B]ibase\f[R] is \f[B]2\f[R]. The max allowable value for \f[B]ibase\f[R] can be queried in bc(1) programs with the \f[B]maxibase()\f[R] built-in function. .PP \f[B]obase\f[R] is a global variable determining how to output results. It is the \[lq]output\[rq] base, or the number base used for outputting numbers. \f[B]obase\f[R] is initially \f[B]10\f[R]. The max allowable value for \f[B]obase\f[R] is \f[B]BC_BASE_MAX\f[R] and can be queried in bc(1) programs with the \f[B]maxobase()\f[R] built-in function. The min allowable value for \f[B]obase\f[R] is \f[B]2\f[R]. Values are output in the specified base. .PP The \f[I]scale\f[R] of an expression is the number of digits in the result of the expression right of the decimal point, and \f[B]scale\f[R] is a global variable that sets the precision of any operations, with exceptions. \f[B]scale\f[R] is initially \f[B]0\f[R]. \f[B]scale\f[R] cannot be negative. The max allowable value for \f[B]scale\f[R] is \f[B]BC_SCALE_MAX\f[R] and can be queried in bc(1) programs with the \f[B]maxscale()\f[R] built-in function. .PP bc(1) has both \f[I]global\f[R] variables and \f[I]local\f[R] variables. All \f[I]local\f[R] variables are local to the function; they are parameters or are introduced in the \f[B]auto\f[R] list of a function (see the \f[B]FUNCTIONS\f[R] section). If a variable is accessed which is not a parameter or in the \f[B]auto\f[R] list, it is assumed to be \f[I]global\f[R]. If a parent function has a \f[I]local\f[R] variable version of a variable that a child function considers \f[I]global\f[R], the value of that \f[I]global\f[R] variable in the child function is the value of the variable in the parent function, not the value of the actual \f[I]global\f[R] variable. .PP All of the above applies to arrays as well. .PP The value of a statement that is an expression (i.e., any of the named expressions or operands) is printed unless the lowest precedence operator is an assignment operator \f[I]and\f[R] the expression is notsurrounded by parentheses. .PP The value that is printed is also assigned to the special variable \f[B]last\f[R]. A single dot (\f[B].\f[R]) may also be used as a synonym for \f[B]last\f[R]. These are \f[B]non-portable extensions\f[R]. .PP Either semicolons or newlines may separate statements. .SS Comments -.PP There are two kinds of comments: .IP "1." 3 Block comments are enclosed in \f[B]/*\f[R] and \f[B]*/\f[R]. .IP "2." 3 Line comments go from \f[B]#\f[R] until, and not including, the next newline. This is a \f[B]non-portable extension\f[R]. .SS Named Expressions -.PP The following are named expressions in bc(1): .IP "1." 3 Variables: \f[B]I\f[R] .IP "2." 3 Array Elements: \f[B]I[E]\f[R] .IP "3." 3 \f[B]ibase\f[R] .IP "4." 3 \f[B]obase\f[R] .IP "5." 3 \f[B]scale\f[R] .IP "6." 3 \f[B]last\f[R] or a single dot (\f[B].\f[R]) .PP Number 6 is a \f[B]non-portable extension\f[R]. .PP Variables and arrays do not interfere; users can have arrays named the same as variables. This also applies to functions (see the \f[B]FUNCTIONS\f[R] section), so a user can have a variable, array, and function that all have the same name, and they will not shadow each other, whether inside of functions or not. .PP Named expressions are required as the operand of \f[B]increment\f[R]/\f[B]decrement\f[R] operators and as the left side of \f[B]assignment\f[R] operators (see the \f[I]Operators\f[R] subsection). .SS Operands -.PP The following are valid operands in bc(1): .IP " 1." 4 Numbers (see the \f[I]Numbers\f[R] subsection below). .IP " 2." 4 Array indices (\f[B]I[E]\f[R]). .IP " 3." 4 \f[B](E)\f[R]: The value of \f[B]E\f[R] (used to change precedence). .IP " 4." 4 \f[B]sqrt(E)\f[R]: The square root of \f[B]E\f[R]. \f[B]E\f[R] must be non-negative. .IP " 5." 4 \f[B]length(E)\f[R]: The number of significant decimal digits in \f[B]E\f[R]. Returns \f[B]1\f[R] for \f[B]0\f[R] with no decimal places. If given a string, the length of the string is returned. Passing a string to \f[B]length(E)\f[R] is a \f[B]non-portable extension\f[R]. .IP " 6." 4 \f[B]length(I[])\f[R]: The number of elements in the array \f[B]I\f[R]. This is a \f[B]non-portable extension\f[R]. .IP " 7." 4 \f[B]scale(E)\f[R]: The \f[I]scale\f[R] of \f[B]E\f[R]. .IP " 8." 4 \f[B]abs(E)\f[R]: The absolute value of \f[B]E\f[R]. This is a \f[B]non-portable extension\f[R]. .IP " 9." 4 \f[B]is_number(E)\f[R]: \f[B]1\f[R] if the given argument is a number, \f[B]0\f[R] if it is a string. This is a \f[B]non-portable extension\f[R]. .IP "10." 4 \f[B]is_string(E)\f[R]: \f[B]1\f[R] if the given argument is a string, \f[B]0\f[R] if it is a number. This is a \f[B]non-portable extension\f[R]. .IP "11." 4 \f[B]modexp(E, E, E)\f[R]: Modular exponentiation, where the first expression is the base, the second is the exponent, and the third is the modulus. All three values must be integers. The second argument must be non-negative. The third argument must be non-zero. This is a \f[B]non-portable extension\f[R]. .IP "12." 4 \f[B]divmod(E, E, I[])\f[R]: Division and modulus in one operation. This is for optimization. The first expression is the dividend, and the second is the divisor, which must be non-zero. The return value is the quotient, and the modulus is stored in index \f[B]0\f[R] of the provided array (the last argument). This is a \f[B]non-portable extension\f[R]. .IP "13." 4 \f[B]asciify(E)\f[R]: If \f[B]E\f[R] is a string, returns a string that is the first letter of its argument. If it is a number, calculates the number mod \f[B]256\f[R] and returns that number as a one-character string. This is a \f[B]non-portable extension\f[R]. .IP "14." 4 \f[B]asciify(I[])\f[R]: A string that is made up of the characters that would result from running \f[B]asciify(E)\f[R] on each element of the array identified by the argument. This allows creating multi-character strings and storing them. This is a \f[B]non-portable extension\f[R]. .IP "15." 4 \f[B]I()\f[R], \f[B]I(E)\f[R], \f[B]I(E, E)\f[R], and so on, where \f[B]I\f[R] is an identifier for a non-\f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). The \f[B]E\f[R] argument(s) may also be arrays of the form \f[B]I[]\f[R], which will automatically be turned into array references (see the \f[I]Array References\f[R] subsection of the \f[B]FUNCTIONS\f[R] section) if the corresponding parameter in the function definition is an array reference. .IP "16." 4 \f[B]read()\f[R]: Reads a line from \f[B]stdin\f[R] and uses that as an expression. The result of that expression is the result of the \f[B]read()\f[R] operand. This is a \f[B]non-portable extension\f[R]. .IP "17." 4 \f[B]maxibase()\f[R]: The max allowable \f[B]ibase\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "18." 4 \f[B]maxobase()\f[R]: The max allowable \f[B]obase\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "19." 4 \f[B]maxscale()\f[R]: The max allowable \f[B]scale\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "20." 4 \f[B]line_length()\f[R]: The line length set with \f[B]BC_LINE_LENGTH\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). This is a \f[B]non-portable extension\f[R]. .IP "21." 4 \f[B]global_stacks()\f[R]: \f[B]0\f[R] if global stacks are not enabled with the \f[B]-g\f[R] or \f[B]--global-stacks\f[R] options, non-zero otherwise. See the \f[B]OPTIONS\f[R] section. This is a \f[B]non-portable extension\f[R]. .IP "22." 4 \f[B]leading_zero()\f[R]: \f[B]0\f[R] if leading zeroes are not enabled with the \f[B]-z\f[R] or \f[B]\[en]leading-zeroes\f[R] options, non-zero otherwise. See the \f[B]OPTIONS\f[R] section. This is a \f[B]non-portable extension\f[R]. .SS Numbers -.PP Numbers are strings made up of digits, uppercase letters, and at most \f[B]1\f[R] period for a radix. Numbers can have up to \f[B]BC_NUM_MAX\f[R] digits. Uppercase letters are equal to \f[B]9\f[R] plus their position in the alphabet, starting from \f[B]1\f[R] (i.e., \f[B]A\f[R] equals \f[B]10\f[R], or \f[B]9+1\f[R]). .PP If a digit or letter makes no sense with the current value of \f[B]ibase\f[R] (i.e., they are greater than or equal to the current value of \f[B]ibase\f[R]), then the behavior depends on the existence of the \f[B]-c\f[R]/\f[B]--digit-clamp\f[R] or \f[B]-C\f[R]/\f[B]--no-digit-clamp\f[R] options (see the \f[B]OPTIONS\f[R] section), the existence and setting of the \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or the default, which can be queried with the \f[B]-h\f[R]/\f[B]--help\f[R] option. .PP If clamping is off, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are not changed. Instead, their given value is multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*A+3\[ha]0*B\f[R], which is \f[B]3\f[R] times \f[B]10\f[R] plus \f[B]11\f[R], or \f[B]41\f[R]. .PP If clamping is on, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are set to the value of the highest valid digit in \f[B]ibase\f[R] before being multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*2+3\[ha]0*2\f[R], which is \f[B]3\f[R] times \f[B]2\f[R] plus \f[B]2\f[R], or \f[B]8\f[R]. .PP There is one exception to clamping: single-character numbers (i.e., \f[B]A\f[R] alone). Such numbers are never clamped and always take the value they would have in the highest possible \f[B]ibase\f[R]. This means that \f[B]A\f[R] alone always equals decimal \f[B]10\f[R] and \f[B]Z\f[R] alone always equals decimal \f[B]35\f[R]. This behavior is mandated by the standard (see the STANDARDS section) and is meant to provide an easy way to set the current \f[B]ibase\f[R] (with the \f[B]i\f[R] command) regardless of the current value of \f[B]ibase\f[R]. .PP If clamping is on, and the clamped value of a character is needed, use a leading zero, i.e., for \f[B]A\f[R], use \f[B]0A\f[R]. .SS Operators -.PP The following arithmetic and logical operators can be used. They are listed in order of decreasing precedence. Operators in the same group have the same precedence. .TP \f[B]++\f[R] \f[B]--\f[R] Type: Prefix and Postfix .RS .PP Associativity: None .PP Description: \f[B]increment\f[R], \f[B]decrement\f[R] .RE .TP \f[B]-\f[R] \f[B]!\f[R] Type: Prefix .RS .PP Associativity: None .PP Description: \f[B]negation\f[R], \f[B]boolean not\f[R] .RE .TP \f[B]\[ha]\f[R] Type: Binary .RS .PP Associativity: Right .PP Description: \f[B]power\f[R] .RE .TP \f[B]*\f[R] \f[B]/\f[R] \f[B]%\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]multiply\f[R], \f[B]divide\f[R], \f[B]modulus\f[R] .RE .TP \f[B]+\f[R] \f[B]-\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]add\f[R], \f[B]subtract\f[R] .RE .TP \f[B]=\f[R] \f[B]+=\f[R] \f[B]-=\f[R] \f[B]*=\f[R] \f[B]/=\f[R] \f[B]%=\f[R] \f[B]\[ha]=\f[R] Type: Binary .RS .PP Associativity: Right .PP Description: \f[B]assignment\f[R] .RE .TP \f[B]==\f[R] \f[B]<=\f[R] \f[B]>=\f[R] \f[B]!=\f[R] \f[B]<\f[R] \f[B]>\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]relational\f[R] .RE .TP \f[B]&&\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]boolean and\f[R] .RE .TP \f[B]||\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]boolean or\f[R] .RE .PP The operators will be described in more detail below. .TP \f[B]++\f[R] \f[B]--\f[R] The prefix and postfix \f[B]increment\f[R] and \f[B]decrement\f[R] -operators behave exactly like they would in C. -They require a named expression (see the \f[I]Named Expressions\f[R] -subsection) as an operand. +operators behave exactly like they would in C. They require a named +expression (see the \f[I]Named Expressions\f[R] subsection) as an +operand. .RS .PP The prefix versions of these operators are more efficient; use them where possible. .RE .TP \f[B]-\f[R] The \f[B]negation\f[R] operator returns \f[B]0\f[R] if a user attempts to negate any expression with the value \f[B]0\f[R]. Otherwise, a copy of the expression with its sign flipped is returned. .TP \f[B]!\f[R] The \f[B]boolean not\f[R] operator returns \f[B]1\f[R] if the expression is \f[B]0\f[R], or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[ha]\f[R] The \f[B]power\f[R] operator (not the \f[B]exclusive or\f[R] operator, as it would be in C) takes two expressions and raises the first to the power of the value of the second. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The second expression must be an integer (no \f[I]scale\f[R]), and if it is negative, the first value must be non-zero. .RE .TP \f[B]*\f[R] The \f[B]multiply\f[R] operator takes two expressions, multiplies them, and returns the product. If \f[B]a\f[R] is the \f[I]scale\f[R] of the first expression and \f[B]b\f[R] is the \f[I]scale\f[R] of the second expression, the \f[I]scale\f[R] of the result is equal to \f[B]min(a+b,max(scale,a,b))\f[R] where \f[B]min()\f[R] and \f[B]max()\f[R] return the obvious values. .TP \f[B]/\f[R] The \f[B]divide\f[R] operator takes two expressions, divides them, and returns the quotient. The \f[I]scale\f[R] of the result shall be the value of \f[B]scale\f[R]. .RS .PP The second expression must be non-zero. .RE .TP \f[B]%\f[R] The \f[B]modulus\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and evaluates them by 1) Computing \f[B]a/b\f[R] to current \f[B]scale\f[R] and 2) Using the result of step 1 to calculate \f[B]a-(a/b)*b\f[R] to \f[I]scale\f[R] \f[B]max(scale+scale(b),scale(a))\f[R]. .RS .PP The second expression must be non-zero. .RE .TP \f[B]+\f[R] The \f[B]add\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and returns the sum, with a \f[I]scale\f[R] equal to the max of the \f[I]scale\f[R]s of \f[B]a\f[R] and \f[B]b\f[R]. .TP \f[B]-\f[R] The \f[B]subtract\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and returns the difference, with a \f[I]scale\f[R] equal to the max of the \f[I]scale\f[R]s of \f[B]a\f[R] and \f[B]b\f[R]. .TP \f[B]=\f[R] \f[B]+=\f[R] \f[B]-=\f[R] \f[B]*=\f[R] \f[B]/=\f[R] \f[B]%=\f[R] \f[B]\[ha]=\f[R] The \f[B]assignment\f[R] operators take two expressions, \f[B]a\f[R] and \f[B]b\f[R] where \f[B]a\f[R] is a named expression (see the \f[I]Named Expressions\f[R] subsection). .RS .PP For \f[B]=\f[R], \f[B]b\f[R] is copied and the result is assigned to \f[B]a\f[R]. For all others, \f[B]a\f[R] and \f[B]b\f[R] are applied as operands to the corresponding arithmetic operator and the result is assigned to \f[B]a\f[R]. .RE .TP \f[B]==\f[R] \f[B]<=\f[R] \f[B]>=\f[R] \f[B]!=\f[R] \f[B]<\f[R] \f[B]>\f[R] The \f[B]relational\f[R] operators compare two expressions, \f[B]a\f[R] and \f[B]b\f[R], and if the relation holds, according to C language semantics, the result is \f[B]1\f[R]. Otherwise, it is \f[B]0\f[R]. .RS .PP Note that unlike in C, these operators have a lower precedence than the \f[B]assignment\f[R] operators, which means that \f[B]a=b>c\f[R] is interpreted as \f[B](a=b)>c\f[R]. .PP Also, unlike the standard (see the \f[B]STANDARDS\f[R] section) requires, these operators can appear anywhere any other expressions can be used. This allowance is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]&&\f[R] The \f[B]boolean and\f[R] operator takes two expressions and returns \f[B]1\f[R] if both expressions are non-zero, \f[B]0\f[R] otherwise. .RS .PP This is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]||\f[R] The \f[B]boolean or\f[R] operator takes two expressions and returns \f[B]1\f[R] if one of the expressions is non-zero, \f[B]0\f[R] otherwise. .RS .PP This is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Statements -.PP The following items are statements: .IP " 1." 4 \f[B]E\f[R] .IP " 2." 4 \f[B]{\f[R] \f[B]S\f[R] \f[B];\f[R] \&... \f[B];\f[R] \f[B]S\f[R] \f[B]}\f[R] .IP " 3." 4 \f[B]if\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] .IP " 4." 4 \f[B]if\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] \f[B]else\f[R] \f[B]S\f[R] .IP " 5." 4 \f[B]while\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] .IP " 6." 4 \f[B]for\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B];\f[R] \f[B]E\f[R] \f[B];\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] .IP " 7." 4 An empty statement .IP " 8." 4 \f[B]break\f[R] .IP " 9." 4 \f[B]continue\f[R] .IP "10." 4 \f[B]quit\f[R] .IP "11." 4 \f[B]halt\f[R] .IP "12." 4 \f[B]limits\f[R] .IP "13." 4 A string of characters, enclosed in double quotes .IP "14." 4 \f[B]print\f[R] \f[B]E\f[R] \f[B],\f[R] \&... \f[B],\f[R] \f[B]E\f[R] .IP "15." 4 \f[B]stream\f[R] \f[B]E\f[R] \f[B],\f[R] \&... \f[B],\f[R] \f[B]E\f[R] .IP "16." 4 \f[B]I()\f[R], \f[B]I(E)\f[R], \f[B]I(E, E)\f[R], and so on, where \f[B]I\f[R] is an identifier for a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). The \f[B]E\f[R] argument(s) may also be arrays of the form \f[B]I[]\f[R], which will automatically be turned into array references (see the \f[I]Array References\f[R] subsection of the \f[B]FUNCTIONS\f[R] section) if the corresponding parameter in the function definition is an array reference. .PP Numbers 4, 9, 11, 12, 14, 15, and 16 are \f[B]non-portable extensions\f[R]. .PP Also, as a \f[B]non-portable extension\f[R], any or all of the expressions in the header of a for loop may be omitted. If the condition (second expression) is omitted, it is assumed to be a constant \f[B]1\f[R]. .PP The \f[B]break\f[R] statement causes a loop to stop iterating and resume execution immediately following a loop. This is only allowed in loops. .PP The \f[B]continue\f[R] statement causes a loop iteration to stop early and returns to the start of the loop, including testing the loop condition. This is only allowed in loops. .PP The \f[B]if\f[R] \f[B]else\f[R] statement does the same thing as in C. .PP The \f[B]quit\f[R] statement causes bc(1) to quit, even if it is on a branch that will not be executed (it is a compile-time command). .PP \f[B]Warning\f[R]: The behavior of this bc(1) on \f[B]quit\f[R] is slightly different from other bc(1) implementations. Other bc(1) implementations will exit as soon as they finish parsing the line that a \f[B]quit\f[R] command is on. This bc(1) will execute any completed and executable statements that occur before the \f[B]quit\f[R] statement before exiting. .PP In other words, for the bc(1) code below: .IP -.nf -\f[C] +.EX for (i = 0; i < 3; ++i) i; quit -\f[R] -.fi +.EE .PP Other bc(1) implementations will print nothing, and this bc(1) will print \f[B]0\f[R], \f[B]1\f[R], and \f[B]2\f[R] on successive lines before exiting. .PP The \f[B]halt\f[R] statement causes bc(1) to quit, if it is executed. (Unlike \f[B]quit\f[R] if it is on a branch of an \f[B]if\f[R] statement that is not executed, bc(1) does not quit.) .PP The \f[B]limits\f[R] statement prints the limits that this bc(1) is subject to. This is like the \f[B]quit\f[R] statement in that it is a compile-time command. .PP An expression by itself is evaluated and printed, followed by a newline. .SS Strings -.PP If strings appear as a statement by themselves, they are printed without a trailing newline. .PP In addition to appearing as a lone statement by themselves, strings can be assigned to variables and array elements. They can also be passed to functions in variable parameters. .PP If any statement that expects a string is given a variable that had a string assigned to it, the statement acts as though it had received a string. .PP If any math operation is attempted on a string or a variable or array element that has been assigned a string, an error is raised, and bc(1) resets (see the \f[B]RESET\f[R] section). .PP Assigning strings to variables and array elements and passing them to functions are \f[B]non-portable extensions\f[R]. .SS Print Statement -.PP The \[lq]expressions\[rq] in a \f[B]print\f[R] statement may also be strings. If they are, there are backslash escape sequences that are interpreted specially. What those sequences are, and what they cause to be printed, are shown below: .PP \f[B]\[rs]a\f[R]: \f[B]\[rs]a\f[R] .PP \f[B]\[rs]b\f[R]: \f[B]\[rs]b\f[R] .PP \f[B]\[rs]\[rs]\f[R]: \f[B]\[rs]\f[R] .PP \f[B]\[rs]e\f[R]: \f[B]\[rs]\f[R] .PP \f[B]\[rs]f\f[R]: \f[B]\[rs]f\f[R] .PP \f[B]\[rs]n\f[R]: \f[B]\[rs]n\f[R] .PP \f[B]\[rs]q\f[R]: \f[B]\[lq]\f[R] .PP \f[B]\[rs]r\f[R]: \f[B]\[rs]r\f[R] .PP \f[B]\[rs]t\f[R]: \f[B]\[rs]t\f[R] .PP Any other character following a backslash causes the backslash and character to be printed as-is. .PP Any non-string expression in a print statement shall be assigned to \f[B]last\f[R], like any other expression that is printed. .SS Stream Statement -.PP The expressions in a \f[B]stream\f[R] statement may also be strings. .PP If a \f[B]stream\f[R] statement is given a string, it prints the string as though the string had appeared as its own statement. In other words, the \f[B]stream\f[R] statement prints strings normally, without a newline. .PP If a \f[B]stream\f[R] statement is given a number, a copy of it is truncated and its absolute value is calculated. The result is then printed as though \f[B]obase\f[R] is \f[B]256\f[R] and each digit is interpreted as an 8-bit ASCII character, making it a byte stream. .SS Order of Evaluation -.PP All expressions in a statment are evaluated left to right, except as necessary to maintain order of operations. This means, for example, assuming that \f[B]i\f[R] is equal to \f[B]0\f[R], in the expression .IP -.nf -\f[C] +.EX a[i++] = i++ -\f[R] -.fi +.EE .PP the first (or 0th) element of \f[B]a\f[R] is set to \f[B]1\f[R], and \f[B]i\f[R] is equal to \f[B]2\f[R] at the end of the expression. .PP This includes function arguments. Thus, assuming \f[B]i\f[R] is equal to \f[B]0\f[R], this means that in the expression .IP -.nf -\f[C] +.EX x(i++, i++) -\f[R] -.fi +.EE .PP the first argument passed to \f[B]x()\f[R] is \f[B]0\f[R], and the second argument is \f[B]1\f[R], while \f[B]i\f[R] is equal to \f[B]2\f[R] before the function starts executing. .SH FUNCTIONS -.PP Function definitions are as follows: .IP -.nf -\f[C] +.EX define I(I,...,I){ auto I,...,I S;...;S return(E) } -\f[R] -.fi +.EE .PP Any \f[B]I\f[R] in the parameter list or \f[B]auto\f[R] list may be replaced with \f[B]I[]\f[R] to make a parameter or \f[B]auto\f[R] var an array, and any \f[B]I\f[R] in the parameter list may be replaced with \f[B]*I[]\f[R] to make a parameter an array reference. Callers of functions that take array references should not put an asterisk in the call; they must be called with just \f[B]I[]\f[R] like normal array parameters and will be automatically converted into references. .PP As a \f[B]non-portable extension\f[R], the opening brace of a \f[B]define\f[R] statement may appear on the next line. .PP As a \f[B]non-portable extension\f[R], the return statement may also be in one of the following forms: .IP "1." 3 \f[B]return\f[R] .IP "2." 3 \f[B]return\f[R] \f[B](\f[R] \f[B])\f[R] .IP "3." 3 \f[B]return\f[R] \f[B]E\f[R] .PP The first two, or not specifying a \f[B]return\f[R] statement, is equivalent to \f[B]return (0)\f[R], unless the function is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection below). .SS Void Functions -.PP Functions can also be \f[B]void\f[R] functions, defined as follows: .IP -.nf -\f[C] +.EX define void I(I,...,I){ auto I,...,I S;...;S return } -\f[R] -.fi +.EE .PP They can only be used as standalone expressions, where such an expression would be printed alone, except in a print statement. .PP Void functions can only use the first two \f[B]return\f[R] statements listed above. They can also omit the return statement entirely. .PP The word \[lq]void\[rq] is not treated as a keyword; it is still possible to have variables, arrays, and functions named \f[B]void\f[R]. The word \[lq]void\[rq] is only treated specially right after the \f[B]define\f[R] keyword. .PP This is a \f[B]non-portable extension\f[R]. .SS Array References -.PP For any array in the parameter list, if the array is declared in the form .IP -.nf -\f[C] +.EX *I[] -\f[R] -.fi +.EE .PP it is a \f[B]reference\f[R]. Any changes to the array in the function are reflected, when the function returns, to the array that was passed in. .PP Other than this, all function arguments are passed by value. .PP This is a \f[B]non-portable extension\f[R]. .SH LIBRARY -.PP All of the functions below are available when the \f[B]-l\f[R] or \f[B]--mathlib\f[R] command-line flags are given. .SS Standard Library -.PP The standard (see the \f[B]STANDARDS\f[R] section) defines the following functions for the math library: .TP \f[B]s(x)\f[R] Returns the sine of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]c(x)\f[R] Returns the cosine of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]a(x)\f[R] Returns the arctangent of \f[B]x\f[R], in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]l(x)\f[R] Returns the natural logarithm of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]e(x)\f[R] Returns the mathematical constant \f[B]e\f[R] raised to the power of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]j(x, n)\f[R] Returns the bessel integer order \f[B]n\f[R] (truncated) of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .SS Transcendental Functions -.PP All transcendental functions can return slightly inaccurate results, up to 1 ULP (https://en.wikipedia.org/wiki/Unit_in_the_last_place). This is unavoidable, and the article at https://people.eecs.berkeley.edu/\[ti]wkahan/LOG10HAF.TXT explains why it is impossible and unnecessary to calculate exact results for the transcendental functions. .PP Because of the possible inaccuracy, I recommend that users call those functions with the precision (\f[B]scale\f[R]) set to at least 1 higher than is necessary. If exact results are \f[I]absolutely\f[R] required, users can double the precision (\f[B]scale\f[R]) and then truncate. .PP The transcendental functions in the standard math library are: .IP \[bu] 2 \f[B]s(x)\f[R] .IP \[bu] 2 \f[B]c(x)\f[R] .IP \[bu] 2 \f[B]a(x)\f[R] .IP \[bu] 2 \f[B]l(x)\f[R] .IP \[bu] 2 \f[B]e(x)\f[R] .IP \[bu] 2 \f[B]j(x, n)\f[R] .SH RESET -.PP When bc(1) encounters an error or a signal that it has a non-default handler for, it resets. This means that several things happen. .PP First, any functions that are executing are stopped and popped off the stack. The behavior is not unlike that of exceptions in programming languages. Then the execution point is set so that any code waiting to execute (after all functions returned) is skipped. .PP Thus, when bc(1) resets, it skips any remaining code waiting to be executed. Then, if it is interactive mode, and the error was not a fatal error (see the \f[B]EXIT STATUS\f[R] section), it asks for more input; otherwise, it exits with the appropriate return code. .PP Note that this reset behavior is different from the GNU bc(1), which attempts to start executing the statement right after the one that caused an error. .SH PERFORMANCE -.PP Most bc(1) implementations use \f[B]char\f[R] types to calculate the value of \f[B]1\f[R] decimal digit at a time, but that can be slow. This bc(1) does something different. .PP It uses large integers to calculate more than \f[B]1\f[R] decimal digit at a time. If built in a environment where \f[B]BC_LONG_BIT\f[R] (see the \f[B]LIMITS\f[R] section) is \f[B]64\f[R], then each integer has \f[B]9\f[R] decimal digits. If built in an environment where \f[B]BC_LONG_BIT\f[R] is \f[B]32\f[R] then each integer has \f[B]4\f[R] decimal digits. This value (the number of decimal digits per large integer) is called \f[B]BC_BASE_DIGS\f[R]. .PP The actual values of \f[B]BC_LONG_BIT\f[R] and \f[B]BC_BASE_DIGS\f[R] can be queried with the \f[B]limits\f[R] statement. .PP In addition, this bc(1) uses an even larger integer for overflow checking. This integer type depends on the value of \f[B]BC_LONG_BIT\f[R], but is always at least twice as large as the integer type used to store digits. .SH LIMITS -.PP The following are the limits on bc(1): .TP \f[B]BC_LONG_BIT\f[R] The number of bits in the \f[B]long\f[R] type in the environment where bc(1) was built. This determines how many decimal digits can be stored in a single large integer (see the \f[B]PERFORMANCE\f[R] section). .TP \f[B]BC_BASE_DIGS\f[R] The number of decimal digits per large integer (see the \f[B]PERFORMANCE\f[R] section). Depends on \f[B]BC_LONG_BIT\f[R]. .TP \f[B]BC_BASE_POW\f[R] The max decimal number that each large integer can store (see \f[B]BC_BASE_DIGS\f[R]) plus \f[B]1\f[R]. Depends on \f[B]BC_BASE_DIGS\f[R]. .TP \f[B]BC_OVERFLOW_MAX\f[R] The max number that the overflow type (see the \f[B]PERFORMANCE\f[R] section) can hold. Depends on \f[B]BC_LONG_BIT\f[R]. .TP \f[B]BC_BASE_MAX\f[R] The maximum output base. Set at \f[B]BC_BASE_POW\f[R]. .TP \f[B]BC_DIM_MAX\f[R] The maximum size of arrays. Set at \f[B]SIZE_MAX-1\f[R]. .TP \f[B]BC_SCALE_MAX\f[R] The maximum \f[B]scale\f[R]. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_STRING_MAX\f[R] The maximum length of strings. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_NAME_MAX\f[R] The maximum length of identifiers. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_NUM_MAX\f[R] The maximum length of a number (in decimal digits), which includes digits after the decimal point. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP Exponent The maximum allowable exponent (positive or negative). Set at \f[B]BC_OVERFLOW_MAX\f[R]. .TP Number of vars The maximum number of vars/arrays. Set at \f[B]SIZE_MAX-1\f[R]. .PP The actual values can be queried with the \f[B]limits\f[R] statement. .PP These limits are meant to be effectively non-existent; the limits are so large (at least on 64-bit machines) that there should not be any point at which they become a problem. In fact, memory should be exhausted before these limits should be hit. .SH ENVIRONMENT VARIABLES -.PP As \f[B]non-portable extensions\f[R], bc(1) recognizes the following environment variables: .TP \f[B]POSIXLY_CORRECT\f[R] If this variable exists (no matter the contents), bc(1) behaves as if the \f[B]-s\f[R] option was given. .TP \f[B]BC_ENV_ARGS\f[R] This is another way to give command-line arguments to bc(1). They should be in the same format as all other command-line arguments. These are always processed first, so any files given in \f[B]BC_ENV_ARGS\f[R] will be processed before arguments and files given on the command-line. This gives the user the ability to set up \[lq]standard\[rq] options and files to be used at every invocation. The most useful thing for such files to contain would be useful functions that the user might want every time bc(1) runs. .RS .PP The code that parses \f[B]BC_ENV_ARGS\f[R] will correctly handle quoted arguments, but it does not understand escape sequences. For example, the string \f[B]\[lq]/home/gavin/some bc file.bc\[rq]\f[R] will be correctly parsed, but the string \f[B]\[lq]/home/gavin/some \[dq]bc\[dq] file.bc\[rq]\f[R] will include the backslashes. .PP The quote parsing will handle either kind of quotes, \f[B]\[cq]\f[R] or \f[B]\[lq]\f[R]. Thus, if you have a file with any number of single quotes in the name, you can use double quotes as the outside quotes, as in \f[B]\[lq]some `bc' file.bc\[rq]\f[R], and vice versa if you have a file with double quotes. However, handling a file with both kinds of quotes in \f[B]BC_ENV_ARGS\f[R] is not supported due to the complexity of the parsing, though such files are still supported on the command-line where the parsing is done by the shell. .RE .TP \f[B]BC_LINE_LENGTH\f[R] If this environment variable exists and contains an integer that is greater than \f[B]1\f[R] and is less than \f[B]UINT16_MAX\f[R] (\f[B]2\[ha]16-1\f[R]), bc(1) will output lines to that length, including the backslash (\f[B]\[rs]\f[R]). The default line length is \f[B]70\f[R]. .RS .PP The special value of \f[B]0\f[R] will disable line length checking and print numbers without regard to line length and without backslashes and newlines. .RE .TP \f[B]BC_BANNER\f[R] If this environment variable exists and contains an integer, then a non-zero value activates the copyright banner when bc(1) is in interactive mode, while zero deactivates it. .RS .PP If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), then this environment variable has no effect because bc(1) does not print the banner when not in interactive mode. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_SIGINT_RESET\f[R] If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), then this environment variable has no effect because bc(1) exits on \f[B]SIGINT\f[R] when not in interactive mode. .RS .PP However, when bc(1) is in interactive mode, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) reset on \f[B]SIGINT\f[R], rather than exit, and zero makes bc(1) exit. If this environment variable exists and is \f[I]not\f[R] an integer, then bc(1) will exit on \f[B]SIGINT\f[R]. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_TTY_MODE\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, then a non-zero value makes bc(1) use TTY mode, and zero makes bc(1) not use TTY mode. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_PROMPT\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) use a prompt, and zero or a non-integer makes bc(1) not use a prompt. If this environment variable does not exist and \f[B]BC_TTY_MODE\f[R] does, then the value of the \f[B]BC_TTY_MODE\f[R] environment variable is used. .PP This environment variable and the \f[B]BC_TTY_MODE\f[R] environment variable override the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_EXPR_EXIT\f[R] If any expressions or expression files are given on the command-line with \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R], then if this environment variable exists and contains an integer, a non-zero value makes bc(1) exit after executing the expressions and expression files, and a zero value makes bc(1) not exit. .RS .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_DIGIT_CLAMP\f[R] When parsing numbers and if this environment variable exists and contains an integer, a non-zero value makes bc(1) clamp digits that are greater than or equal to the current \f[B]ibase\f[R] so that all such digits are considered equal to the \f[B]ibase\f[R] minus 1, and a zero value disables such clamping so that those digits are always equal to their value, which is multiplied by the power of the \f[B]ibase\f[R]. .RS .PP This never applies to single-digit numbers, as per the standard (see the \f[B]STANDARDS\f[R] section). .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .SH EXIT STATUS -.PP bc(1) returns the following exit statuses: .TP \f[B]0\f[R] No error. .TP \f[B]1\f[R] A math error occurred. This follows standard practice of using \f[B]1\f[R] for expected errors, since math errors will happen in the process of normal execution. .RS .PP Math errors include divide by \f[B]0\f[R], taking the square root of a negative number, attempting to convert a negative number to a hardware integer, overflow when converting a number to a hardware integer, overflow when calculating the size of a number, and attempting to use a non-integer where an integer is required. .PP Converting to a hardware integer happens for the second operand of the power (\f[B]\[ha]\f[R]) operator and the corresponding assignment operator. .RE .TP \f[B]2\f[R] A parse error occurred. .RS .PP Parse errors include unexpected \f[B]EOF\f[R], using an invalid character, failing to find the end of a string or comment, using a token where it is invalid, giving an invalid expression, giving an invalid print statement, giving an invalid function definition, attempting to assign to an expression that is not a named expression (see the \f[I]Named Expressions\f[R] subsection of the \f[B]SYNTAX\f[R] section), giving an invalid \f[B]auto\f[R] list, having a duplicate \f[B]auto\f[R]/function parameter, failing to find the end of a code block, attempting to return a value from a \f[B]void\f[R] function, attempting to use a variable as a reference, and using any extensions when the option \f[B]-s\f[R] or any equivalents were given. .RE .TP \f[B]3\f[R] A runtime error occurred. .RS .PP Runtime errors include assigning an invalid number to any global (\f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R]), giving a bad expression to a \f[B]read()\f[R] call, calling \f[B]read()\f[R] inside of a \f[B]read()\f[R] call, type errors, passing the wrong number of arguments to functions, attempting to call an undefined function, and attempting to use a \f[B]void\f[R] function call as a value in an expression. .RE .TP \f[B]4\f[R] A fatal error occurred. .RS .PP Fatal errors include memory allocation errors, I/O errors, failing to open files, attempting to use files that do not have only ASCII characters (bc(1) only accepts ASCII characters), attempting to open a directory as a file, and giving invalid command-line options. .RE .PP The exit status \f[B]4\f[R] is special; when a fatal error occurs, bc(1) always exits and returns \f[B]4\f[R], no matter what mode bc(1) is in. .PP The other statuses will only be returned when bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), since bc(1) resets its state (see the \f[B]RESET\f[R] section) and accepts more input when one of those errors occurs in interactive mode. This is also the case when interactive mode is forced by the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .PP These exit statuses allow bc(1) to be used in shell scripting with error checking, and its normal behavior can be forced by using the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .SH INTERACTIVE MODE -.PP Per the standard (see the \f[B]STANDARDS\f[R] section), bc(1) has an interactive mode and a non-interactive mode. Interactive mode is turned on automatically when both \f[B]stdin\f[R] and \f[B]stdout\f[R] are hooked to a terminal, but the \f[B]-i\f[R] flag and \f[B]--interactive\f[R] option can turn it on in other situations. .PP In interactive mode, bc(1) attempts to recover from errors (see the \f[B]RESET\f[R] section), and in normal execution, flushes \f[B]stdout\f[R] as soon as execution is done for the current input. bc(1) may also reset on \f[B]SIGINT\f[R] instead of exit, depending on the contents of, or default for, the \f[B]BC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .SH TTY MODE -.PP If \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY, then \[lq]TTY mode\[rq] is considered to be available, and thus, bc(1) can turn on TTY mode, subject to some settings. .PP If there is the environment variable \f[B]BC_TTY_MODE\f[R] in the environment (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then if that environment variable contains a non-zero integer, bc(1) will turn on TTY mode when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY. If the \f[B]BC_TTY_MODE\f[R] environment variable exists but is \f[I]not\f[R] a non-zero integer, then bc(1) will not turn TTY mode on. .PP If the environment variable \f[B]BC_TTY_MODE\f[R] does \f[I]not\f[R] exist, the default setting is used. The default setting can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP TTY mode is different from interactive mode because interactive mode is required in the bc(1) standard (see the \f[B]STANDARDS\f[R] section), and interactive mode requires only \f[B]stdin\f[R] and \f[B]stdout\f[R] to be connected to a terminal. .SS Prompt -.PP If TTY mode is available, then a prompt can be enabled. Like TTY mode itself, it can be turned on or off with an environment variable: \f[B]BC_PROMPT\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP If the environment variable \f[B]BC_PROMPT\f[R] exists and is a non-zero integer, then the prompt is turned on when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options were not used. The read prompt will be turned on under the same conditions, except that the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options must also not be used. .PP However, if \f[B]BC_PROMPT\f[R] does not exist, the prompt can be enabled or disabled with the \f[B]BC_TTY_MODE\f[R] environment variable, the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options, and the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options. See the \f[B]ENVIRONMENT VARIABLES\f[R] and \f[B]OPTIONS\f[R] sections for more details. .SH SIGNAL HANDLING -.PP Sending a \f[B]SIGINT\f[R] will cause bc(1) to do one of two things. .PP If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), or the \f[B]BC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or its default, is either not an integer or it is zero, bc(1) will exit. .PP However, if bc(1) is in interactive mode, and the \f[B]BC_SIGINT_RESET\f[R] or its default is an integer and non-zero, then bc(1) will stop executing the current input and reset (see the \f[B]RESET\f[R] section) upon receiving a \f[B]SIGINT\f[R]. .PP Note that \[lq]current input\[rq] can mean one of two things. If bc(1) is processing input from \f[B]stdin\f[R] in interactive mode, it will ask for more input. If bc(1) is processing input from a file in interactive mode, it will stop processing the file and start processing the next file, if one exists, or ask for input from \f[B]stdin\f[R] if no other file exists. .PP This means that if a \f[B]SIGINT\f[R] is sent to bc(1) as it is executing a file, it can seem as though bc(1) did not respond to the signal since it will immediately start executing the next file. This is by design; most files that users execute when interacting with bc(1) have function definitions, which are quick to parse. If a file takes a long time to execute, there may be a bug in that file. The rest of the files could still be executed without problem, allowing the user to continue. .PP \f[B]SIGTERM\f[R] and \f[B]SIGQUIT\f[R] cause bc(1) to clean up and exit, and it uses the default handler for all other signals. .SH SEE ALSO -.PP dc(1) .SH STANDARDS -.PP bc(1) is compliant with the IEEE Std 1003.1-2017 (\[lq]POSIX.1-2017\[rq]) specification at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html . The flags \f[B]-efghiqsvVw\f[R], all long options, and the extensions noted above are extensions to that specification. .PP In addition, the behavior of the \f[B]quit\f[R] implements an interpretation of that specification that is different from all known implementations. For more information see the \f[B]Statements\f[R] subsection of the \f[B]SYNTAX\f[R] section. .PP Note that the specification explicitly says that bc(1) only accepts numbers that use a period (\f[B].\f[R]) as a radix point, regardless of the value of \f[B]LC_NUMERIC\f[R]. .SH BUGS -.PP Before version \f[B]6.1.0\f[R], this bc(1) had incorrect behavior for the \f[B]quit\f[R] statement. .PP No other bugs are known. Report bugs at https://git.gavinhoward.com/gavin/bc . .SH AUTHORS -.PP -Gavin D. -Howard and contributors. +Gavin D. Howard \c +.MT gavin@gavinhoward.com +.ME \c +\ and contributors. diff --git a/contrib/bc/manuals/bc/EN.1 b/contrib/bc/manuals/bc/EN.1 index a972005515a6..99ccb822ea78 100644 --- a/contrib/bc/manuals/bc/EN.1 +++ b/contrib/bc/manuals/bc/EN.1 @@ -1,1809 +1,1754 @@ .\" .\" SPDX-License-Identifier: BSD-2-Clause .\" .\" Copyright (c) 2018-2023 Gavin D. Howard and contributors. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions are met: .\" .\" * Redistributions of source code must retain the above copyright notice, .\" this list of conditions and the following disclaimer. .\" .\" * Redistributions in binary form must reproduce the above copyright notice, .\" this list of conditions and the following disclaimer in the documentation .\" and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.TH "BC" "1" "February 2023" "Gavin D. Howard" "General Commands Manual" +.TH "BC" "1" "November 2023" "Gavin D. Howard" "General Commands Manual" .nh .ad l .SH NAME -.PP bc - arbitrary-precision decimal arithmetic language and calculator .SH SYNOPSIS -.PP \f[B]bc\f[R] [\f[B]-cCghilPqRsvVw\f[R]] [\f[B]--digit-clamp\f[R]] [\f[B]--no-digit-clamp\f[R]] [\f[B]--global-stacks\f[R]] [\f[B]--help\f[R]] [\f[B]--interactive\f[R]] [\f[B]--mathlib\f[R]] [\f[B]--no-prompt\f[R]] [\f[B]--no-read-prompt\f[R]] [\f[B]--quiet\f[R]] [\f[B]--standard\f[R]] [\f[B]--warn\f[R]] [\f[B]--version\f[R]] [\f[B]-e\f[R] \f[I]expr\f[R]] [\f[B]--expression\f[R]=\f[I]expr\f[R]\&...] [\f[B]-f\f[R] \f[I]file\f[R]\&...] [\f[B]--file\f[R]=\f[I]file\f[R]\&...] [\f[I]file\f[R]\&...] .SH DESCRIPTION -.PP bc(1) is an interactive processor for a language first standardized in 1991 by POSIX. (See the \f[B]STANDARDS\f[R] section.) The language provides unlimited precision decimal arithmetic and is somewhat C-like, but there are differences. Such differences will be noted in this document. .PP After parsing and handling options, this bc(1) reads any files given on the command line and executes them before reading from \f[B]stdin\f[R]. .PP This bc(1) is a drop-in replacement for \f[I]any\f[R] bc(1), including (and especially) the GNU bc(1). It also has many extensions and extra features beyond other implementations. .PP \f[B]Note\f[R]: If running this bc(1) on \f[I]any\f[R] script meant for another bc(1) gives a parse error, it is probably because a word this bc(1) reserves as a keyword is used as the name of a function, variable, or array. To fix that, use the command-line option \f[B]-r\f[R] \f[I]keyword\f[R], where \f[I]keyword\f[R] is the keyword that is used as a name in the script. For more information, see the \f[B]OPTIONS\f[R] section. .PP If parsing scripts meant for other bc(1) implementations still does not work, that is a bug and should be reported. See the \f[B]BUGS\f[R] section. .SH OPTIONS -.PP The following are the options that bc(1) accepts. .TP \f[B]-C\f[R], \f[B]--no-digit-clamp\f[R] Disables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that the value added to a number from a digit is always that digit\[cq]s value multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-c\f[R] or \f[B]--digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-c\f[R], \f[B]--digit-clamp\f[R] Enables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that digits that the value added to a number from a digit that is greater than or equal to the ibase is the value of ibase minus 1 all multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-C\f[R] or \f[B]--no-digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-e\f[R] \f[I]expr\f[R], \f[B]--expression\f[R]=\f[I]expr\f[R] Evaluates \f[I]expr\f[R]. If multiple expressions are given, they are evaluated in order. If files are given as well (see the \f[B]-f\f[R] and \f[B]--file\f[R] options), the expressions and files are evaluated in the order given. This means that if a file is given before an expression, the file is read in and evaluated first. .RS .PP If this option is given on the command-line (i.e., not in \f[B]BC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, bc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R], whether on the command-line or in \f[B]BC_ENV_ARGS\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, bc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-f\f[R] \f[I]file\f[R], \f[B]--file\f[R]=\f[I]file\f[R] Reads in \f[I]file\f[R] and evaluates it, line by line, as though it were read through \f[B]stdin\f[R]. If expressions are also given (see the \f[B]-e\f[R] and \f[B]--expression\f[R] options), the expressions are evaluated in the order given. .RS .PP If this option is given on the command-line (i.e., not in \f[B]BC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, bc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, bc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-g\f[R], \f[B]--global-stacks\f[R] Turns the globals \f[B]ibase\f[R], \f[B]obase\f[R], and \f[B]scale\f[R] into stacks. .RS .PP This has the effect that a copy of the current value of all three are pushed onto a stack for every function call, as well as popped when every function returns. This means that functions can assign to any and all of those globals without worrying that the change will affect other functions. Thus, a hypothetical function named \f[B]output(x,b)\f[R] that simply printed \f[B]x\f[R] in base \f[B]b\f[R] could be written like this: .IP -.nf -\f[C] +.EX define void output(x, b) { obase=b x } -\f[R] -.fi +.EE .PP instead of like this: .IP -.nf -\f[C] +.EX define void output(x, b) { auto c c=obase obase=b x obase=c } -\f[R] -.fi +.EE .PP This makes writing functions much easier. .PP However, since using this flag means that functions cannot set \f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R] globally, functions that are made to do so cannot work anymore. There are two possible use cases for that, and each has a solution. .PP First, if a function is called on startup to turn bc(1) into a number converter, it is possible to replace that capability with various shell aliases. Examples: .IP -.nf -\f[C] +.EX alias d2o=\[dq]bc -e ibase=A -e obase=8\[dq] alias h2b=\[dq]bc -e ibase=G -e obase=2\[dq] -\f[R] -.fi +.EE .PP Second, if the purpose of a function is to set \f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R] globally for any other purpose, it could be split into one to three functions (based on how many globals it sets) and each of those functions could return the desired value for a global. .PP If the behavior of this option is desired for every run of bc(1), then users could make sure to define \f[B]BC_ENV_ARGS\f[R] and include this option (see the \f[B]ENVIRONMENT VARIABLES\f[R] section for more details). .PP If \f[B]-s\f[R], \f[B]-w\f[R], or any equivalents are used, this option is ignored. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-h\f[R], \f[B]--help\f[R] Prints a usage message and exits. .TP \f[B]-I\f[R] \f[I]ibase\f[R], \f[B]--ibase\f[R]=\f[I]ibase\f[R] Sets the builtin variable \f[B]ibase\f[R] to the value \f[I]ibase\f[R] assuming that \f[I]ibase\f[R] is in base 10. It is a fatal error if \f[I]ibase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-i\f[R], \f[B]--interactive\f[R] Forces interactive mode. (See the \f[B]INTERACTIVE MODE\f[R] section.) .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-L\f[R], \f[B]--no-line-length\f[R] Disables line length checking and prints numbers without backslashes and newlines. In other words, this option sets \f[B]BC_LINE_LENGTH\f[R] to \f[B]0\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-l\f[R], \f[B]--mathlib\f[R] Sets \f[B]scale\f[R] (see the \f[B]SYNTAX\f[R] section) to \f[B]20\f[R] and loads the included math library before running any code, including any expressions or files specified on the command line. .RS .PP To learn what is in the library, see the \f[B]LIBRARY\f[R] section. .RE .TP \f[B]-O\f[R] \f[I]obase\f[R], \f[B]--obase\f[R]=\f[I]obase\f[R] Sets the builtin variable \f[B]obase\f[R] to the value \f[I]obase\f[R] assuming that \f[I]obase\f[R] is in base 10. It is a fatal error if \f[I]obase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-P\f[R], \f[B]--no-prompt\f[R] Disables the prompt in TTY mode. (The prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a prompt or are not used to having them in bc(1). Most of those users would want to put this option in \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .RS .PP These options override the \f[B]BC_PROMPT\f[R] and \f[B]BC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-q\f[R], \f[B]--quiet\f[R] This option is for compatibility with the GNU bc(1) (https://www.gnu.org/software/bc/); it is a no-op. Without this option, GNU bc(1) prints a copyright header. This bc(1) only prints the copyright header if one or more of the \f[B]-v\f[R], \f[B]-V\f[R], or \f[B]--version\f[R] options are given unless the \f[B]BC_BANNER\f[R] environment variable is set and contains a non-zero integer or if this bc(1) was built with the header displayed by default. If \f[I]any\f[R] of that is the case, then this option \f[I]does\f[R] prevent bc(1) from printing the header. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-R\f[R], \f[B]--no-read-prompt\f[R] Disables the read prompt in TTY mode. (The read prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a read prompt or are not used to having them in bc(1). Most of those users would want to put this option in \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). This option is also useful in hash bang lines of bc(1) scripts that prompt for user input. .RS .PP This option does not disable the regular prompt because the read prompt is only used when the \f[B]read()\f[R] built-in function is called. .PP These options \f[I]do\f[R] override the \f[B]BC_PROMPT\f[R] and \f[B]BC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), but only for the read prompt. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-r\f[R] \f[I]keyword\f[R], \f[B]--redefine\f[R]=\f[I]keyword\f[R] Redefines \f[I]keyword\f[R] in order to allow it to be used as a function, variable, or array name. This is useful when this bc(1) gives parse errors when parsing scripts meant for other bc(1) implementations. .RS .PP The keywords this bc(1) allows to be redefined are: .IP \[bu] 2 \f[B]abs\f[R] .IP \[bu] 2 \f[B]asciify\f[R] .IP \[bu] 2 \f[B]continue\f[R] .IP \[bu] 2 \f[B]divmod\f[R] .IP \[bu] 2 \f[B]else\f[R] .IP \[bu] 2 \f[B]halt\f[R] .IP \[bu] 2 \f[B]last\f[R] .IP \[bu] 2 \f[B]limits\f[R] .IP \[bu] 2 \f[B]maxibase\f[R] .IP \[bu] 2 \f[B]maxobase\f[R] .IP \[bu] 2 \f[B]maxscale\f[R] .IP \[bu] 2 \f[B]modexp\f[R] .IP \[bu] 2 \f[B]print\f[R] .IP \[bu] 2 \f[B]read\f[R] .IP \[bu] 2 \f[B]stream\f[R] .PP If any of those keywords are used as a function, variable, or array name in a script, use this option with the keyword as the argument. If multiple are used, use this option for all of them; it can be used multiple times. .PP Keywords are \f[I]not\f[R] redefined when parsing the builtin math library (see the \f[B]LIBRARY\f[R] section). .PP It is a fatal error to redefine keywords mandated by the POSIX standard (see the \f[B]STANDARDS\f[R] section). It is a fatal error to attempt to redefine words that this bc(1) does not reserve as keywords. .RE .TP \f[B]-S\f[R] \f[I]scale\f[R], \f[B]--scale\f[R]=\f[I]scale\f[R] Sets the builtin variable \f[B]scale\f[R] to the value \f[I]scale\f[R] assuming that \f[I]scale\f[R] is in base 10. It is a fatal error if \f[I]scale\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-s\f[R], \f[B]--standard\f[R] Process exactly the language defined by the standard (see the \f[B]STANDARDS\f[R] section) and error if any extensions are used. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-v\f[R], \f[B]-V\f[R], \f[B]--version\f[R] Print the version information (copyright header) and exits. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-w\f[R], \f[B]--warn\f[R] Like \f[B]-s\f[R] and \f[B]--standard\f[R], except that warnings (and not errors) are printed for non-standard extensions and execution continues normally. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-z\f[R], \f[B]--leading-zeroes\f[R] Makes bc(1) print all numbers greater than \f[B]-1\f[R] and less than \f[B]1\f[R], and not equal to \f[B]0\f[R], with a leading zero. .RS .PP This can be set for individual numbers with the \f[B]plz(x)\f[R], \f[B]plznl(x)\f[R], \f[B]pnlz(x)\f[R], and \f[B]pnlznl(x)\f[R] functions in the extended math library (see the \f[B]LIBRARY\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .PP All long options are \f[B]non-portable extensions\f[R]. .SH STDIN -.PP If no files or expressions are given by the \f[B]-f\f[R], \f[B]--file\f[R], \f[B]-e\f[R], or \f[B]--expression\f[R] options, then bc(1) reads from \f[B]stdin\f[R]. .PP However, there are a few caveats to this. .PP First, \f[B]stdin\f[R] is evaluated a line at a time. The only exception to this is if the parse cannot complete. That means that starting a string without ending it or starting a function, \f[B]if\f[R] statement, or loop without ending it will also cause bc(1) to not execute. .PP Second, after an \f[B]if\f[R] statement, bc(1) doesn\[cq]t know if an \f[B]else\f[R] statement will follow, so it will not execute until it knows there will not be an \f[B]else\f[R] statement. .SH STDOUT -.PP Any non-error output is written to \f[B]stdout\f[R]. In addition, if history (see the \f[B]HISTORY\f[R] section) and the prompt (see the \f[B]TTY MODE\f[R] section) are enabled, both are output to \f[B]stdout\f[R]. .PP \f[B]Note\f[R]: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stdout\f[R], so if \f[B]stdout\f[R] is closed, as in -\f[B]bc >&-\f[R], it will quit with an error. +\f[B]bc >&-\f[R], it will quit with an error. This is done so that bc(1) can report problems when \f[B]stdout\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stdout\f[R] to \f[B]/dev/null\f[R]. .SH STDERR -.PP Any error output is written to \f[B]stderr\f[R]. .PP \f[B]Note\f[R]: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stderr\f[R], so if \f[B]stderr\f[R] is closed, as in -\f[B]bc 2>&-\f[R], it will quit with an error. +\f[B]bc 2>&-\f[R], it will quit with an error. This is done so that bc(1) can exit with an error code when \f[B]stderr\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stderr\f[R] to \f[B]/dev/null\f[R]. .SH SYNTAX -.PP The syntax for bc(1) programs is mostly C-like, with some differences. This bc(1) follows the POSIX standard (see the \f[B]STANDARDS\f[R] section), which is a much more thorough resource for the language this bc(1) accepts. This section is meant to be a summary and a listing of all the extensions to the standard. .PP In the sections below, \f[B]E\f[R] means expression, \f[B]S\f[R] means statement, and \f[B]I\f[R] means identifier. .PP Identifiers (\f[B]I\f[R]) start with a lowercase letter and can be followed by any number (up to \f[B]BC_NAME_MAX-1\f[R]) of lowercase letters (\f[B]a-z\f[R]), digits (\f[B]0-9\f[R]), and underscores (\f[B]_\f[R]). The regex is \f[B][a-z][a-z0-9_]*\f[R]. Identifiers with more than one character (letter) are a \f[B]non-portable extension\f[R]. .PP \f[B]ibase\f[R] is a global variable determining how to interpret constant numbers. It is the \[lq]input\[rq] base, or the number base used for interpreting input numbers. \f[B]ibase\f[R] is initially \f[B]10\f[R]. If the \f[B]-s\f[R] (\f[B]--standard\f[R]) and \f[B]-w\f[R] (\f[B]--warn\f[R]) flags were not given on the command line, the max allowable value for \f[B]ibase\f[R] is \f[B]36\f[R]. Otherwise, it is \f[B]16\f[R]. The min allowable value for \f[B]ibase\f[R] is \f[B]2\f[R]. The max allowable value for \f[B]ibase\f[R] can be queried in bc(1) programs with the \f[B]maxibase()\f[R] built-in function. .PP \f[B]obase\f[R] is a global variable determining how to output results. It is the \[lq]output\[rq] base, or the number base used for outputting numbers. \f[B]obase\f[R] is initially \f[B]10\f[R]. The max allowable value for \f[B]obase\f[R] is \f[B]BC_BASE_MAX\f[R] and can be queried in bc(1) programs with the \f[B]maxobase()\f[R] built-in function. The min allowable value for \f[B]obase\f[R] is \f[B]2\f[R]. Values are output in the specified base. .PP The \f[I]scale\f[R] of an expression is the number of digits in the result of the expression right of the decimal point, and \f[B]scale\f[R] is a global variable that sets the precision of any operations, with exceptions. \f[B]scale\f[R] is initially \f[B]0\f[R]. \f[B]scale\f[R] cannot be negative. The max allowable value for \f[B]scale\f[R] is \f[B]BC_SCALE_MAX\f[R] and can be queried in bc(1) programs with the \f[B]maxscale()\f[R] built-in function. .PP bc(1) has both \f[I]global\f[R] variables and \f[I]local\f[R] variables. All \f[I]local\f[R] variables are local to the function; they are parameters or are introduced in the \f[B]auto\f[R] list of a function (see the \f[B]FUNCTIONS\f[R] section). If a variable is accessed which is not a parameter or in the \f[B]auto\f[R] list, it is assumed to be \f[I]global\f[R]. If a parent function has a \f[I]local\f[R] variable version of a variable that a child function considers \f[I]global\f[R], the value of that \f[I]global\f[R] variable in the child function is the value of the variable in the parent function, not the value of the actual \f[I]global\f[R] variable. .PP All of the above applies to arrays as well. .PP The value of a statement that is an expression (i.e., any of the named expressions or operands) is printed unless the lowest precedence operator is an assignment operator \f[I]and\f[R] the expression is notsurrounded by parentheses. .PP The value that is printed is also assigned to the special variable \f[B]last\f[R]. A single dot (\f[B].\f[R]) may also be used as a synonym for \f[B]last\f[R]. These are \f[B]non-portable extensions\f[R]. .PP Either semicolons or newlines may separate statements. .SS Comments -.PP There are two kinds of comments: .IP "1." 3 Block comments are enclosed in \f[B]/*\f[R] and \f[B]*/\f[R]. .IP "2." 3 Line comments go from \f[B]#\f[R] until, and not including, the next newline. This is a \f[B]non-portable extension\f[R]. .SS Named Expressions -.PP The following are named expressions in bc(1): .IP "1." 3 Variables: \f[B]I\f[R] .IP "2." 3 Array Elements: \f[B]I[E]\f[R] .IP "3." 3 \f[B]ibase\f[R] .IP "4." 3 \f[B]obase\f[R] .IP "5." 3 \f[B]scale\f[R] .IP "6." 3 \f[B]last\f[R] or a single dot (\f[B].\f[R]) .PP Number 6 is a \f[B]non-portable extension\f[R]. .PP Variables and arrays do not interfere; users can have arrays named the same as variables. This also applies to functions (see the \f[B]FUNCTIONS\f[R] section), so a user can have a variable, array, and function that all have the same name, and they will not shadow each other, whether inside of functions or not. .PP Named expressions are required as the operand of \f[B]increment\f[R]/\f[B]decrement\f[R] operators and as the left side of \f[B]assignment\f[R] operators (see the \f[I]Operators\f[R] subsection). .SS Operands -.PP The following are valid operands in bc(1): .IP " 1." 4 Numbers (see the \f[I]Numbers\f[R] subsection below). .IP " 2." 4 Array indices (\f[B]I[E]\f[R]). .IP " 3." 4 \f[B](E)\f[R]: The value of \f[B]E\f[R] (used to change precedence). .IP " 4." 4 \f[B]sqrt(E)\f[R]: The square root of \f[B]E\f[R]. \f[B]E\f[R] must be non-negative. .IP " 5." 4 \f[B]length(E)\f[R]: The number of significant decimal digits in \f[B]E\f[R]. Returns \f[B]1\f[R] for \f[B]0\f[R] with no decimal places. If given a string, the length of the string is returned. Passing a string to \f[B]length(E)\f[R] is a \f[B]non-portable extension\f[R]. .IP " 6." 4 \f[B]length(I[])\f[R]: The number of elements in the array \f[B]I\f[R]. This is a \f[B]non-portable extension\f[R]. .IP " 7." 4 \f[B]scale(E)\f[R]: The \f[I]scale\f[R] of \f[B]E\f[R]. .IP " 8." 4 \f[B]abs(E)\f[R]: The absolute value of \f[B]E\f[R]. This is a \f[B]non-portable extension\f[R]. .IP " 9." 4 \f[B]is_number(E)\f[R]: \f[B]1\f[R] if the given argument is a number, \f[B]0\f[R] if it is a string. This is a \f[B]non-portable extension\f[R]. .IP "10." 4 \f[B]is_string(E)\f[R]: \f[B]1\f[R] if the given argument is a string, \f[B]0\f[R] if it is a number. This is a \f[B]non-portable extension\f[R]. .IP "11." 4 \f[B]modexp(E, E, E)\f[R]: Modular exponentiation, where the first expression is the base, the second is the exponent, and the third is the modulus. All three values must be integers. The second argument must be non-negative. The third argument must be non-zero. This is a \f[B]non-portable extension\f[R]. .IP "12." 4 \f[B]divmod(E, E, I[])\f[R]: Division and modulus in one operation. This is for optimization. The first expression is the dividend, and the second is the divisor, which must be non-zero. The return value is the quotient, and the modulus is stored in index \f[B]0\f[R] of the provided array (the last argument). This is a \f[B]non-portable extension\f[R]. .IP "13." 4 \f[B]asciify(E)\f[R]: If \f[B]E\f[R] is a string, returns a string that is the first letter of its argument. If it is a number, calculates the number mod \f[B]256\f[R] and returns that number as a one-character string. This is a \f[B]non-portable extension\f[R]. .IP "14." 4 \f[B]asciify(I[])\f[R]: A string that is made up of the characters that would result from running \f[B]asciify(E)\f[R] on each element of the array identified by the argument. This allows creating multi-character strings and storing them. This is a \f[B]non-portable extension\f[R]. .IP "15." 4 \f[B]I()\f[R], \f[B]I(E)\f[R], \f[B]I(E, E)\f[R], and so on, where \f[B]I\f[R] is an identifier for a non-\f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). The \f[B]E\f[R] argument(s) may also be arrays of the form \f[B]I[]\f[R], which will automatically be turned into array references (see the \f[I]Array References\f[R] subsection of the \f[B]FUNCTIONS\f[R] section) if the corresponding parameter in the function definition is an array reference. .IP "16." 4 \f[B]read()\f[R]: Reads a line from \f[B]stdin\f[R] and uses that as an expression. The result of that expression is the result of the \f[B]read()\f[R] operand. This is a \f[B]non-portable extension\f[R]. .IP "17." 4 \f[B]maxibase()\f[R]: The max allowable \f[B]ibase\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "18." 4 \f[B]maxobase()\f[R]: The max allowable \f[B]obase\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "19." 4 \f[B]maxscale()\f[R]: The max allowable \f[B]scale\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "20." 4 \f[B]line_length()\f[R]: The line length set with \f[B]BC_LINE_LENGTH\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). This is a \f[B]non-portable extension\f[R]. .IP "21." 4 \f[B]global_stacks()\f[R]: \f[B]0\f[R] if global stacks are not enabled with the \f[B]-g\f[R] or \f[B]--global-stacks\f[R] options, non-zero otherwise. See the \f[B]OPTIONS\f[R] section. This is a \f[B]non-portable extension\f[R]. .IP "22." 4 \f[B]leading_zero()\f[R]: \f[B]0\f[R] if leading zeroes are not enabled with the \f[B]-z\f[R] or \f[B]\[en]leading-zeroes\f[R] options, non-zero otherwise. See the \f[B]OPTIONS\f[R] section. This is a \f[B]non-portable extension\f[R]. .SS Numbers -.PP Numbers are strings made up of digits, uppercase letters, and at most \f[B]1\f[R] period for a radix. Numbers can have up to \f[B]BC_NUM_MAX\f[R] digits. Uppercase letters are equal to \f[B]9\f[R] plus their position in the alphabet, starting from \f[B]1\f[R] (i.e., \f[B]A\f[R] equals \f[B]10\f[R], or \f[B]9+1\f[R]). .PP If a digit or letter makes no sense with the current value of \f[B]ibase\f[R] (i.e., they are greater than or equal to the current value of \f[B]ibase\f[R]), then the behavior depends on the existence of the \f[B]-c\f[R]/\f[B]--digit-clamp\f[R] or \f[B]-C\f[R]/\f[B]--no-digit-clamp\f[R] options (see the \f[B]OPTIONS\f[R] section), the existence and setting of the \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or the default, which can be queried with the \f[B]-h\f[R]/\f[B]--help\f[R] option. .PP If clamping is off, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are not changed. Instead, their given value is multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*A+3\[ha]0*B\f[R], which is \f[B]3\f[R] times \f[B]10\f[R] plus \f[B]11\f[R], or \f[B]41\f[R]. .PP If clamping is on, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are set to the value of the highest valid digit in \f[B]ibase\f[R] before being multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*2+3\[ha]0*2\f[R], which is \f[B]3\f[R] times \f[B]2\f[R] plus \f[B]2\f[R], or \f[B]8\f[R]. .PP There is one exception to clamping: single-character numbers (i.e., \f[B]A\f[R] alone). Such numbers are never clamped and always take the value they would have in the highest possible \f[B]ibase\f[R]. This means that \f[B]A\f[R] alone always equals decimal \f[B]10\f[R] and \f[B]Z\f[R] alone always equals decimal \f[B]35\f[R]. This behavior is mandated by the standard (see the STANDARDS section) and is meant to provide an easy way to set the current \f[B]ibase\f[R] (with the \f[B]i\f[R] command) regardless of the current value of \f[B]ibase\f[R]. .PP If clamping is on, and the clamped value of a character is needed, use a leading zero, i.e., for \f[B]A\f[R], use \f[B]0A\f[R]. .SS Operators -.PP The following arithmetic and logical operators can be used. They are listed in order of decreasing precedence. Operators in the same group have the same precedence. .TP \f[B]++\f[R] \f[B]--\f[R] Type: Prefix and Postfix .RS .PP Associativity: None .PP Description: \f[B]increment\f[R], \f[B]decrement\f[R] .RE .TP \f[B]-\f[R] \f[B]!\f[R] Type: Prefix .RS .PP Associativity: None .PP Description: \f[B]negation\f[R], \f[B]boolean not\f[R] .RE .TP \f[B]\[ha]\f[R] Type: Binary .RS .PP Associativity: Right .PP Description: \f[B]power\f[R] .RE .TP \f[B]*\f[R] \f[B]/\f[R] \f[B]%\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]multiply\f[R], \f[B]divide\f[R], \f[B]modulus\f[R] .RE .TP \f[B]+\f[R] \f[B]-\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]add\f[R], \f[B]subtract\f[R] .RE .TP \f[B]=\f[R] \f[B]+=\f[R] \f[B]-=\f[R] \f[B]*=\f[R] \f[B]/=\f[R] \f[B]%=\f[R] \f[B]\[ha]=\f[R] Type: Binary .RS .PP Associativity: Right .PP Description: \f[B]assignment\f[R] .RE .TP \f[B]==\f[R] \f[B]<=\f[R] \f[B]>=\f[R] \f[B]!=\f[R] \f[B]<\f[R] \f[B]>\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]relational\f[R] .RE .TP \f[B]&&\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]boolean and\f[R] .RE .TP \f[B]||\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]boolean or\f[R] .RE .PP The operators will be described in more detail below. .TP \f[B]++\f[R] \f[B]--\f[R] The prefix and postfix \f[B]increment\f[R] and \f[B]decrement\f[R] -operators behave exactly like they would in C. -They require a named expression (see the \f[I]Named Expressions\f[R] -subsection) as an operand. +operators behave exactly like they would in C. They require a named +expression (see the \f[I]Named Expressions\f[R] subsection) as an +operand. .RS .PP The prefix versions of these operators are more efficient; use them where possible. .RE .TP \f[B]-\f[R] The \f[B]negation\f[R] operator returns \f[B]0\f[R] if a user attempts to negate any expression with the value \f[B]0\f[R]. Otherwise, a copy of the expression with its sign flipped is returned. .TP \f[B]!\f[R] The \f[B]boolean not\f[R] operator returns \f[B]1\f[R] if the expression is \f[B]0\f[R], or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[ha]\f[R] The \f[B]power\f[R] operator (not the \f[B]exclusive or\f[R] operator, as it would be in C) takes two expressions and raises the first to the power of the value of the second. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The second expression must be an integer (no \f[I]scale\f[R]), and if it is negative, the first value must be non-zero. .RE .TP \f[B]*\f[R] The \f[B]multiply\f[R] operator takes two expressions, multiplies them, and returns the product. If \f[B]a\f[R] is the \f[I]scale\f[R] of the first expression and \f[B]b\f[R] is the \f[I]scale\f[R] of the second expression, the \f[I]scale\f[R] of the result is equal to \f[B]min(a+b,max(scale,a,b))\f[R] where \f[B]min()\f[R] and \f[B]max()\f[R] return the obvious values. .TP \f[B]/\f[R] The \f[B]divide\f[R] operator takes two expressions, divides them, and returns the quotient. The \f[I]scale\f[R] of the result shall be the value of \f[B]scale\f[R]. .RS .PP The second expression must be non-zero. .RE .TP \f[B]%\f[R] The \f[B]modulus\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and evaluates them by 1) Computing \f[B]a/b\f[R] to current \f[B]scale\f[R] and 2) Using the result of step 1 to calculate \f[B]a-(a/b)*b\f[R] to \f[I]scale\f[R] \f[B]max(scale+scale(b),scale(a))\f[R]. .RS .PP The second expression must be non-zero. .RE .TP \f[B]+\f[R] The \f[B]add\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and returns the sum, with a \f[I]scale\f[R] equal to the max of the \f[I]scale\f[R]s of \f[B]a\f[R] and \f[B]b\f[R]. .TP \f[B]-\f[R] The \f[B]subtract\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and returns the difference, with a \f[I]scale\f[R] equal to the max of the \f[I]scale\f[R]s of \f[B]a\f[R] and \f[B]b\f[R]. .TP \f[B]=\f[R] \f[B]+=\f[R] \f[B]-=\f[R] \f[B]*=\f[R] \f[B]/=\f[R] \f[B]%=\f[R] \f[B]\[ha]=\f[R] The \f[B]assignment\f[R] operators take two expressions, \f[B]a\f[R] and \f[B]b\f[R] where \f[B]a\f[R] is a named expression (see the \f[I]Named Expressions\f[R] subsection). .RS .PP For \f[B]=\f[R], \f[B]b\f[R] is copied and the result is assigned to \f[B]a\f[R]. For all others, \f[B]a\f[R] and \f[B]b\f[R] are applied as operands to the corresponding arithmetic operator and the result is assigned to \f[B]a\f[R]. .RE .TP \f[B]==\f[R] \f[B]<=\f[R] \f[B]>=\f[R] \f[B]!=\f[R] \f[B]<\f[R] \f[B]>\f[R] The \f[B]relational\f[R] operators compare two expressions, \f[B]a\f[R] and \f[B]b\f[R], and if the relation holds, according to C language semantics, the result is \f[B]1\f[R]. Otherwise, it is \f[B]0\f[R]. .RS .PP Note that unlike in C, these operators have a lower precedence than the \f[B]assignment\f[R] operators, which means that \f[B]a=b>c\f[R] is interpreted as \f[B](a=b)>c\f[R]. .PP Also, unlike the standard (see the \f[B]STANDARDS\f[R] section) requires, these operators can appear anywhere any other expressions can be used. This allowance is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]&&\f[R] The \f[B]boolean and\f[R] operator takes two expressions and returns \f[B]1\f[R] if both expressions are non-zero, \f[B]0\f[R] otherwise. .RS .PP This is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]||\f[R] The \f[B]boolean or\f[R] operator takes two expressions and returns \f[B]1\f[R] if one of the expressions is non-zero, \f[B]0\f[R] otherwise. .RS .PP This is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Statements -.PP The following items are statements: .IP " 1." 4 \f[B]E\f[R] .IP " 2." 4 \f[B]{\f[R] \f[B]S\f[R] \f[B];\f[R] \&... \f[B];\f[R] \f[B]S\f[R] \f[B]}\f[R] .IP " 3." 4 \f[B]if\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] .IP " 4." 4 \f[B]if\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] \f[B]else\f[R] \f[B]S\f[R] .IP " 5." 4 \f[B]while\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] .IP " 6." 4 \f[B]for\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B];\f[R] \f[B]E\f[R] \f[B];\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] .IP " 7." 4 An empty statement .IP " 8." 4 \f[B]break\f[R] .IP " 9." 4 \f[B]continue\f[R] .IP "10." 4 \f[B]quit\f[R] .IP "11." 4 \f[B]halt\f[R] .IP "12." 4 \f[B]limits\f[R] .IP "13." 4 A string of characters, enclosed in double quotes .IP "14." 4 \f[B]print\f[R] \f[B]E\f[R] \f[B],\f[R] \&... \f[B],\f[R] \f[B]E\f[R] .IP "15." 4 \f[B]stream\f[R] \f[B]E\f[R] \f[B],\f[R] \&... \f[B],\f[R] \f[B]E\f[R] .IP "16." 4 \f[B]I()\f[R], \f[B]I(E)\f[R], \f[B]I(E, E)\f[R], and so on, where \f[B]I\f[R] is an identifier for a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). The \f[B]E\f[R] argument(s) may also be arrays of the form \f[B]I[]\f[R], which will automatically be turned into array references (see the \f[I]Array References\f[R] subsection of the \f[B]FUNCTIONS\f[R] section) if the corresponding parameter in the function definition is an array reference. .PP Numbers 4, 9, 11, 12, 14, 15, and 16 are \f[B]non-portable extensions\f[R]. .PP Also, as a \f[B]non-portable extension\f[R], any or all of the expressions in the header of a for loop may be omitted. If the condition (second expression) is omitted, it is assumed to be a constant \f[B]1\f[R]. .PP The \f[B]break\f[R] statement causes a loop to stop iterating and resume execution immediately following a loop. This is only allowed in loops. .PP The \f[B]continue\f[R] statement causes a loop iteration to stop early and returns to the start of the loop, including testing the loop condition. This is only allowed in loops. .PP The \f[B]if\f[R] \f[B]else\f[R] statement does the same thing as in C. .PP The \f[B]quit\f[R] statement causes bc(1) to quit, even if it is on a branch that will not be executed (it is a compile-time command). .PP \f[B]Warning\f[R]: The behavior of this bc(1) on \f[B]quit\f[R] is slightly different from other bc(1) implementations. Other bc(1) implementations will exit as soon as they finish parsing the line that a \f[B]quit\f[R] command is on. This bc(1) will execute any completed and executable statements that occur before the \f[B]quit\f[R] statement before exiting. .PP In other words, for the bc(1) code below: .IP -.nf -\f[C] +.EX for (i = 0; i < 3; ++i) i; quit -\f[R] -.fi +.EE .PP Other bc(1) implementations will print nothing, and this bc(1) will print \f[B]0\f[R], \f[B]1\f[R], and \f[B]2\f[R] on successive lines before exiting. .PP The \f[B]halt\f[R] statement causes bc(1) to quit, if it is executed. (Unlike \f[B]quit\f[R] if it is on a branch of an \f[B]if\f[R] statement that is not executed, bc(1) does not quit.) .PP The \f[B]limits\f[R] statement prints the limits that this bc(1) is subject to. This is like the \f[B]quit\f[R] statement in that it is a compile-time command. .PP An expression by itself is evaluated and printed, followed by a newline. .SS Strings -.PP If strings appear as a statement by themselves, they are printed without a trailing newline. .PP In addition to appearing as a lone statement by themselves, strings can be assigned to variables and array elements. They can also be passed to functions in variable parameters. .PP If any statement that expects a string is given a variable that had a string assigned to it, the statement acts as though it had received a string. .PP If any math operation is attempted on a string or a variable or array element that has been assigned a string, an error is raised, and bc(1) resets (see the \f[B]RESET\f[R] section). .PP Assigning strings to variables and array elements and passing them to functions are \f[B]non-portable extensions\f[R]. .SS Print Statement -.PP The \[lq]expressions\[rq] in a \f[B]print\f[R] statement may also be strings. If they are, there are backslash escape sequences that are interpreted specially. What those sequences are, and what they cause to be printed, are shown below: .PP \f[B]\[rs]a\f[R]: \f[B]\[rs]a\f[R] .PP \f[B]\[rs]b\f[R]: \f[B]\[rs]b\f[R] .PP \f[B]\[rs]\[rs]\f[R]: \f[B]\[rs]\f[R] .PP \f[B]\[rs]e\f[R]: \f[B]\[rs]\f[R] .PP \f[B]\[rs]f\f[R]: \f[B]\[rs]f\f[R] .PP \f[B]\[rs]n\f[R]: \f[B]\[rs]n\f[R] .PP \f[B]\[rs]q\f[R]: \f[B]\[lq]\f[R] .PP \f[B]\[rs]r\f[R]: \f[B]\[rs]r\f[R] .PP \f[B]\[rs]t\f[R]: \f[B]\[rs]t\f[R] .PP Any other character following a backslash causes the backslash and character to be printed as-is. .PP Any non-string expression in a print statement shall be assigned to \f[B]last\f[R], like any other expression that is printed. .SS Stream Statement -.PP The expressions in a \f[B]stream\f[R] statement may also be strings. .PP If a \f[B]stream\f[R] statement is given a string, it prints the string as though the string had appeared as its own statement. In other words, the \f[B]stream\f[R] statement prints strings normally, without a newline. .PP If a \f[B]stream\f[R] statement is given a number, a copy of it is truncated and its absolute value is calculated. The result is then printed as though \f[B]obase\f[R] is \f[B]256\f[R] and each digit is interpreted as an 8-bit ASCII character, making it a byte stream. .SS Order of Evaluation -.PP All expressions in a statment are evaluated left to right, except as necessary to maintain order of operations. This means, for example, assuming that \f[B]i\f[R] is equal to \f[B]0\f[R], in the expression .IP -.nf -\f[C] +.EX a[i++] = i++ -\f[R] -.fi +.EE .PP the first (or 0th) element of \f[B]a\f[R] is set to \f[B]1\f[R], and \f[B]i\f[R] is equal to \f[B]2\f[R] at the end of the expression. .PP This includes function arguments. Thus, assuming \f[B]i\f[R] is equal to \f[B]0\f[R], this means that in the expression .IP -.nf -\f[C] +.EX x(i++, i++) -\f[R] -.fi +.EE .PP the first argument passed to \f[B]x()\f[R] is \f[B]0\f[R], and the second argument is \f[B]1\f[R], while \f[B]i\f[R] is equal to \f[B]2\f[R] before the function starts executing. .SH FUNCTIONS -.PP Function definitions are as follows: .IP -.nf -\f[C] +.EX define I(I,...,I){ auto I,...,I S;...;S return(E) } -\f[R] -.fi +.EE .PP Any \f[B]I\f[R] in the parameter list or \f[B]auto\f[R] list may be replaced with \f[B]I[]\f[R] to make a parameter or \f[B]auto\f[R] var an array, and any \f[B]I\f[R] in the parameter list may be replaced with \f[B]*I[]\f[R] to make a parameter an array reference. Callers of functions that take array references should not put an asterisk in the call; they must be called with just \f[B]I[]\f[R] like normal array parameters and will be automatically converted into references. .PP As a \f[B]non-portable extension\f[R], the opening brace of a \f[B]define\f[R] statement may appear on the next line. .PP As a \f[B]non-portable extension\f[R], the return statement may also be in one of the following forms: .IP "1." 3 \f[B]return\f[R] .IP "2." 3 \f[B]return\f[R] \f[B](\f[R] \f[B])\f[R] .IP "3." 3 \f[B]return\f[R] \f[B]E\f[R] .PP The first two, or not specifying a \f[B]return\f[R] statement, is equivalent to \f[B]return (0)\f[R], unless the function is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection below). .SS Void Functions -.PP Functions can also be \f[B]void\f[R] functions, defined as follows: .IP -.nf -\f[C] +.EX define void I(I,...,I){ auto I,...,I S;...;S return } -\f[R] -.fi +.EE .PP They can only be used as standalone expressions, where such an expression would be printed alone, except in a print statement. .PP Void functions can only use the first two \f[B]return\f[R] statements listed above. They can also omit the return statement entirely. .PP The word \[lq]void\[rq] is not treated as a keyword; it is still possible to have variables, arrays, and functions named \f[B]void\f[R]. The word \[lq]void\[rq] is only treated specially right after the \f[B]define\f[R] keyword. .PP This is a \f[B]non-portable extension\f[R]. .SS Array References -.PP For any array in the parameter list, if the array is declared in the form .IP -.nf -\f[C] +.EX *I[] -\f[R] -.fi +.EE .PP it is a \f[B]reference\f[R]. Any changes to the array in the function are reflected, when the function returns, to the array that was passed in. .PP Other than this, all function arguments are passed by value. .PP This is a \f[B]non-portable extension\f[R]. .SH LIBRARY -.PP All of the functions below are available when the \f[B]-l\f[R] or \f[B]--mathlib\f[R] command-line flags are given. .SS Standard Library -.PP The standard (see the \f[B]STANDARDS\f[R] section) defines the following functions for the math library: .TP \f[B]s(x)\f[R] Returns the sine of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]c(x)\f[R] Returns the cosine of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]a(x)\f[R] Returns the arctangent of \f[B]x\f[R], in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]l(x)\f[R] Returns the natural logarithm of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]e(x)\f[R] Returns the mathematical constant \f[B]e\f[R] raised to the power of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]j(x, n)\f[R] Returns the bessel integer order \f[B]n\f[R] (truncated) of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .SS Transcendental Functions -.PP All transcendental functions can return slightly inaccurate results, up to 1 ULP (https://en.wikipedia.org/wiki/Unit_in_the_last_place). This is unavoidable, and the article at https://people.eecs.berkeley.edu/\[ti]wkahan/LOG10HAF.TXT explains why it is impossible and unnecessary to calculate exact results for the transcendental functions. .PP Because of the possible inaccuracy, I recommend that users call those functions with the precision (\f[B]scale\f[R]) set to at least 1 higher than is necessary. If exact results are \f[I]absolutely\f[R] required, users can double the precision (\f[B]scale\f[R]) and then truncate. .PP The transcendental functions in the standard math library are: .IP \[bu] 2 \f[B]s(x)\f[R] .IP \[bu] 2 \f[B]c(x)\f[R] .IP \[bu] 2 \f[B]a(x)\f[R] .IP \[bu] 2 \f[B]l(x)\f[R] .IP \[bu] 2 \f[B]e(x)\f[R] .IP \[bu] 2 \f[B]j(x, n)\f[R] .SH RESET -.PP When bc(1) encounters an error or a signal that it has a non-default handler for, it resets. This means that several things happen. .PP First, any functions that are executing are stopped and popped off the stack. The behavior is not unlike that of exceptions in programming languages. Then the execution point is set so that any code waiting to execute (after all functions returned) is skipped. .PP Thus, when bc(1) resets, it skips any remaining code waiting to be executed. Then, if it is interactive mode, and the error was not a fatal error (see the \f[B]EXIT STATUS\f[R] section), it asks for more input; otherwise, it exits with the appropriate return code. .PP Note that this reset behavior is different from the GNU bc(1), which attempts to start executing the statement right after the one that caused an error. .SH PERFORMANCE -.PP Most bc(1) implementations use \f[B]char\f[R] types to calculate the value of \f[B]1\f[R] decimal digit at a time, but that can be slow. This bc(1) does something different. .PP It uses large integers to calculate more than \f[B]1\f[R] decimal digit at a time. If built in a environment where \f[B]BC_LONG_BIT\f[R] (see the \f[B]LIMITS\f[R] section) is \f[B]64\f[R], then each integer has \f[B]9\f[R] decimal digits. If built in an environment where \f[B]BC_LONG_BIT\f[R] is \f[B]32\f[R] then each integer has \f[B]4\f[R] decimal digits. This value (the number of decimal digits per large integer) is called \f[B]BC_BASE_DIGS\f[R]. .PP The actual values of \f[B]BC_LONG_BIT\f[R] and \f[B]BC_BASE_DIGS\f[R] can be queried with the \f[B]limits\f[R] statement. .PP In addition, this bc(1) uses an even larger integer for overflow checking. This integer type depends on the value of \f[B]BC_LONG_BIT\f[R], but is always at least twice as large as the integer type used to store digits. .SH LIMITS -.PP The following are the limits on bc(1): .TP \f[B]BC_LONG_BIT\f[R] The number of bits in the \f[B]long\f[R] type in the environment where bc(1) was built. This determines how many decimal digits can be stored in a single large integer (see the \f[B]PERFORMANCE\f[R] section). .TP \f[B]BC_BASE_DIGS\f[R] The number of decimal digits per large integer (see the \f[B]PERFORMANCE\f[R] section). Depends on \f[B]BC_LONG_BIT\f[R]. .TP \f[B]BC_BASE_POW\f[R] The max decimal number that each large integer can store (see \f[B]BC_BASE_DIGS\f[R]) plus \f[B]1\f[R]. Depends on \f[B]BC_BASE_DIGS\f[R]. .TP \f[B]BC_OVERFLOW_MAX\f[R] The max number that the overflow type (see the \f[B]PERFORMANCE\f[R] section) can hold. Depends on \f[B]BC_LONG_BIT\f[R]. .TP \f[B]BC_BASE_MAX\f[R] The maximum output base. Set at \f[B]BC_BASE_POW\f[R]. .TP \f[B]BC_DIM_MAX\f[R] The maximum size of arrays. Set at \f[B]SIZE_MAX-1\f[R]. .TP \f[B]BC_SCALE_MAX\f[R] The maximum \f[B]scale\f[R]. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_STRING_MAX\f[R] The maximum length of strings. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_NAME_MAX\f[R] The maximum length of identifiers. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_NUM_MAX\f[R] The maximum length of a number (in decimal digits), which includes digits after the decimal point. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP Exponent The maximum allowable exponent (positive or negative). Set at \f[B]BC_OVERFLOW_MAX\f[R]. .TP Number of vars The maximum number of vars/arrays. Set at \f[B]SIZE_MAX-1\f[R]. .PP The actual values can be queried with the \f[B]limits\f[R] statement. .PP These limits are meant to be effectively non-existent; the limits are so large (at least on 64-bit machines) that there should not be any point at which they become a problem. In fact, memory should be exhausted before these limits should be hit. .SH ENVIRONMENT VARIABLES -.PP As \f[B]non-portable extensions\f[R], bc(1) recognizes the following environment variables: .TP \f[B]POSIXLY_CORRECT\f[R] If this variable exists (no matter the contents), bc(1) behaves as if the \f[B]-s\f[R] option was given. .TP \f[B]BC_ENV_ARGS\f[R] This is another way to give command-line arguments to bc(1). They should be in the same format as all other command-line arguments. These are always processed first, so any files given in \f[B]BC_ENV_ARGS\f[R] will be processed before arguments and files given on the command-line. This gives the user the ability to set up \[lq]standard\[rq] options and files to be used at every invocation. The most useful thing for such files to contain would be useful functions that the user might want every time bc(1) runs. .RS .PP The code that parses \f[B]BC_ENV_ARGS\f[R] will correctly handle quoted arguments, but it does not understand escape sequences. For example, the string \f[B]\[lq]/home/gavin/some bc file.bc\[rq]\f[R] will be correctly parsed, but the string \f[B]\[lq]/home/gavin/some \[dq]bc\[dq] file.bc\[rq]\f[R] will include the backslashes. .PP The quote parsing will handle either kind of quotes, \f[B]\[cq]\f[R] or \f[B]\[lq]\f[R]. Thus, if you have a file with any number of single quotes in the name, you can use double quotes as the outside quotes, as in \f[B]\[lq]some `bc' file.bc\[rq]\f[R], and vice versa if you have a file with double quotes. However, handling a file with both kinds of quotes in \f[B]BC_ENV_ARGS\f[R] is not supported due to the complexity of the parsing, though such files are still supported on the command-line where the parsing is done by the shell. .RE .TP \f[B]BC_LINE_LENGTH\f[R] If this environment variable exists and contains an integer that is greater than \f[B]1\f[R] and is less than \f[B]UINT16_MAX\f[R] (\f[B]2\[ha]16-1\f[R]), bc(1) will output lines to that length, including the backslash (\f[B]\[rs]\f[R]). The default line length is \f[B]70\f[R]. .RS .PP The special value of \f[B]0\f[R] will disable line length checking and print numbers without regard to line length and without backslashes and newlines. .RE .TP \f[B]BC_BANNER\f[R] If this environment variable exists and contains an integer, then a non-zero value activates the copyright banner when bc(1) is in interactive mode, while zero deactivates it. .RS .PP If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), then this environment variable has no effect because bc(1) does not print the banner when not in interactive mode. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_SIGINT_RESET\f[R] If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), then this environment variable has no effect because bc(1) exits on \f[B]SIGINT\f[R] when not in interactive mode. .RS .PP However, when bc(1) is in interactive mode, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) reset on \f[B]SIGINT\f[R], rather than exit, and zero makes bc(1) exit. If this environment variable exists and is \f[I]not\f[R] an integer, then bc(1) will exit on \f[B]SIGINT\f[R]. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_TTY_MODE\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, then a non-zero value makes bc(1) use TTY mode, and zero makes bc(1) not use TTY mode. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_PROMPT\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) use a prompt, and zero or a non-integer makes bc(1) not use a prompt. If this environment variable does not exist and \f[B]BC_TTY_MODE\f[R] does, then the value of the \f[B]BC_TTY_MODE\f[R] environment variable is used. .PP This environment variable and the \f[B]BC_TTY_MODE\f[R] environment variable override the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_EXPR_EXIT\f[R] If any expressions or expression files are given on the command-line with \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R], then if this environment variable exists and contains an integer, a non-zero value makes bc(1) exit after executing the expressions and expression files, and a zero value makes bc(1) not exit. .RS .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_DIGIT_CLAMP\f[R] When parsing numbers and if this environment variable exists and contains an integer, a non-zero value makes bc(1) clamp digits that are greater than or equal to the current \f[B]ibase\f[R] so that all such digits are considered equal to the \f[B]ibase\f[R] minus 1, and a zero value disables such clamping so that those digits are always equal to their value, which is multiplied by the power of the \f[B]ibase\f[R]. .RS .PP This never applies to single-digit numbers, as per the standard (see the \f[B]STANDARDS\f[R] section). .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .SH EXIT STATUS -.PP bc(1) returns the following exit statuses: .TP \f[B]0\f[R] No error. .TP \f[B]1\f[R] A math error occurred. This follows standard practice of using \f[B]1\f[R] for expected errors, since math errors will happen in the process of normal execution. .RS .PP Math errors include divide by \f[B]0\f[R], taking the square root of a negative number, attempting to convert a negative number to a hardware integer, overflow when converting a number to a hardware integer, overflow when calculating the size of a number, and attempting to use a non-integer where an integer is required. .PP Converting to a hardware integer happens for the second operand of the power (\f[B]\[ha]\f[R]) operator and the corresponding assignment operator. .RE .TP \f[B]2\f[R] A parse error occurred. .RS .PP Parse errors include unexpected \f[B]EOF\f[R], using an invalid character, failing to find the end of a string or comment, using a token where it is invalid, giving an invalid expression, giving an invalid print statement, giving an invalid function definition, attempting to assign to an expression that is not a named expression (see the \f[I]Named Expressions\f[R] subsection of the \f[B]SYNTAX\f[R] section), giving an invalid \f[B]auto\f[R] list, having a duplicate \f[B]auto\f[R]/function parameter, failing to find the end of a code block, attempting to return a value from a \f[B]void\f[R] function, attempting to use a variable as a reference, and using any extensions when the option \f[B]-s\f[R] or any equivalents were given. .RE .TP \f[B]3\f[R] A runtime error occurred. .RS .PP Runtime errors include assigning an invalid number to any global (\f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R]), giving a bad expression to a \f[B]read()\f[R] call, calling \f[B]read()\f[R] inside of a \f[B]read()\f[R] call, type errors, passing the wrong number of arguments to functions, attempting to call an undefined function, and attempting to use a \f[B]void\f[R] function call as a value in an expression. .RE .TP \f[B]4\f[R] A fatal error occurred. .RS .PP Fatal errors include memory allocation errors, I/O errors, failing to open files, attempting to use files that do not have only ASCII characters (bc(1) only accepts ASCII characters), attempting to open a directory as a file, and giving invalid command-line options. .RE .PP The exit status \f[B]4\f[R] is special; when a fatal error occurs, bc(1) always exits and returns \f[B]4\f[R], no matter what mode bc(1) is in. .PP The other statuses will only be returned when bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), since bc(1) resets its state (see the \f[B]RESET\f[R] section) and accepts more input when one of those errors occurs in interactive mode. This is also the case when interactive mode is forced by the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .PP These exit statuses allow bc(1) to be used in shell scripting with error checking, and its normal behavior can be forced by using the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .SH INTERACTIVE MODE -.PP Per the standard (see the \f[B]STANDARDS\f[R] section), bc(1) has an interactive mode and a non-interactive mode. Interactive mode is turned on automatically when both \f[B]stdin\f[R] and \f[B]stdout\f[R] are hooked to a terminal, but the \f[B]-i\f[R] flag and \f[B]--interactive\f[R] option can turn it on in other situations. .PP In interactive mode, bc(1) attempts to recover from errors (see the \f[B]RESET\f[R] section), and in normal execution, flushes \f[B]stdout\f[R] as soon as execution is done for the current input. bc(1) may also reset on \f[B]SIGINT\f[R] instead of exit, depending on the contents of, or default for, the \f[B]BC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .SH TTY MODE -.PP If \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY, then \[lq]TTY mode\[rq] is considered to be available, and thus, bc(1) can turn on TTY mode, subject to some settings. .PP If there is the environment variable \f[B]BC_TTY_MODE\f[R] in the environment (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then if that environment variable contains a non-zero integer, bc(1) will turn on TTY mode when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY. If the \f[B]BC_TTY_MODE\f[R] environment variable exists but is \f[I]not\f[R] a non-zero integer, then bc(1) will not turn TTY mode on. .PP If the environment variable \f[B]BC_TTY_MODE\f[R] does \f[I]not\f[R] exist, the default setting is used. The default setting can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP TTY mode is different from interactive mode because interactive mode is required in the bc(1) standard (see the \f[B]STANDARDS\f[R] section), and interactive mode requires only \f[B]stdin\f[R] and \f[B]stdout\f[R] to be connected to a terminal. .SS Command-Line History -.PP Command-line history is only enabled if TTY mode is, i.e., that \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the \f[B]BC_TTY_MODE\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and its default do not disable TTY mode. See the \f[B]COMMAND LINE HISTORY\f[R] section for more information. .SS Prompt -.PP If TTY mode is available, then a prompt can be enabled. Like TTY mode itself, it can be turned on or off with an environment variable: \f[B]BC_PROMPT\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP If the environment variable \f[B]BC_PROMPT\f[R] exists and is a non-zero integer, then the prompt is turned on when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options were not used. The read prompt will be turned on under the same conditions, except that the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options must also not be used. .PP However, if \f[B]BC_PROMPT\f[R] does not exist, the prompt can be enabled or disabled with the \f[B]BC_TTY_MODE\f[R] environment variable, the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options, and the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options. See the \f[B]ENVIRONMENT VARIABLES\f[R] and \f[B]OPTIONS\f[R] sections for more details. .SH SIGNAL HANDLING -.PP Sending a \f[B]SIGINT\f[R] will cause bc(1) to do one of two things. .PP If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), or the \f[B]BC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or its default, is either not an integer or it is zero, bc(1) will exit. .PP However, if bc(1) is in interactive mode, and the \f[B]BC_SIGINT_RESET\f[R] or its default is an integer and non-zero, then bc(1) will stop executing the current input and reset (see the \f[B]RESET\f[R] section) upon receiving a \f[B]SIGINT\f[R]. .PP Note that \[lq]current input\[rq] can mean one of two things. If bc(1) is processing input from \f[B]stdin\f[R] in interactive mode, it will ask for more input. If bc(1) is processing input from a file in interactive mode, it will stop processing the file and start processing the next file, if one exists, or ask for input from \f[B]stdin\f[R] if no other file exists. .PP This means that if a \f[B]SIGINT\f[R] is sent to bc(1) as it is executing a file, it can seem as though bc(1) did not respond to the signal since it will immediately start executing the next file. This is by design; most files that users execute when interacting with bc(1) have function definitions, which are quick to parse. If a file takes a long time to execute, there may be a bug in that file. The rest of the files could still be executed without problem, allowing the user to continue. .PP \f[B]SIGTERM\f[R] and \f[B]SIGQUIT\f[R] cause bc(1) to clean up and exit, and it uses the default handler for all other signals. The one exception is \f[B]SIGHUP\f[R]; in that case, and only when bc(1) is in TTY mode (see the \f[B]TTY MODE\f[R] section), a \f[B]SIGHUP\f[R] will cause bc(1) to clean up and exit. .SH COMMAND LINE HISTORY -.PP bc(1) supports interactive command-line editing. .PP If bc(1) can be in TTY mode (see the \f[B]TTY MODE\f[R] section), history can be enabled. This means that command-line history can only be enabled when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY. .PP Like TTY mode itself, it can be turned on or off with the environment variable \f[B]BC_TTY_MODE\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP If history is enabled, previous lines can be recalled and edited with the arrow keys. .PP \f[B]Note\f[R]: tabs are converted to 8 spaces. .SH SEE ALSO -.PP dc(1) .SH STANDARDS -.PP bc(1) is compliant with the IEEE Std 1003.1-2017 (\[lq]POSIX.1-2017\[rq]) specification at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html . The flags \f[B]-efghiqsvVw\f[R], all long options, and the extensions noted above are extensions to that specification. .PP In addition, the behavior of the \f[B]quit\f[R] implements an interpretation of that specification that is different from all known implementations. For more information see the \f[B]Statements\f[R] subsection of the \f[B]SYNTAX\f[R] section. .PP Note that the specification explicitly says that bc(1) only accepts numbers that use a period (\f[B].\f[R]) as a radix point, regardless of the value of \f[B]LC_NUMERIC\f[R]. .SH BUGS -.PP Before version \f[B]6.1.0\f[R], this bc(1) had incorrect behavior for the \f[B]quit\f[R] statement. .PP No other bugs are known. Report bugs at https://git.gavinhoward.com/gavin/bc . .SH AUTHORS -.PP -Gavin D. -Howard and contributors. +Gavin D. Howard \c +.MT gavin@gavinhoward.com +.ME \c +\ and contributors. diff --git a/contrib/bc/manuals/bc/H.1 b/contrib/bc/manuals/bc/H.1 index a26aee4f2d11..13b2caf84e16 100644 --- a/contrib/bc/manuals/bc/H.1 +++ b/contrib/bc/manuals/bc/H.1 @@ -1,2974 +1,2917 @@ .\" .\" SPDX-License-Identifier: BSD-2-Clause .\" .\" Copyright (c) 2018-2023 Gavin D. Howard and contributors. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions are met: .\" .\" * Redistributions of source code must retain the above copyright notice, .\" this list of conditions and the following disclaimer. .\" .\" * Redistributions in binary form must reproduce the above copyright notice, .\" this list of conditions and the following disclaimer in the documentation .\" and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.TH "BC" "1" "February 2023" "Gavin D. Howard" "General Commands Manual" +.TH "BC" "1" "November 2023" "Gavin D. Howard" "General Commands Manual" .nh .ad l .SH NAME -.PP bc - arbitrary-precision decimal arithmetic language and calculator .SH SYNOPSIS -.PP \f[B]bc\f[R] [\f[B]-cCghilPqRsvVw\f[R]] [\f[B]--digit-clamp\f[R]] [\f[B]--no-digit-clamp\f[R]] [\f[B]--global-stacks\f[R]] [\f[B]--help\f[R]] [\f[B]--interactive\f[R]] [\f[B]--mathlib\f[R]] [\f[B]--no-prompt\f[R]] [\f[B]--no-read-prompt\f[R]] [\f[B]--quiet\f[R]] [\f[B]--standard\f[R]] [\f[B]--warn\f[R]] [\f[B]--version\f[R]] [\f[B]-e\f[R] \f[I]expr\f[R]] [\f[B]--expression\f[R]=\f[I]expr\f[R]\&...] [\f[B]-f\f[R] \f[I]file\f[R]\&...] [\f[B]--file\f[R]=\f[I]file\f[R]\&...] [\f[I]file\f[R]\&...] [\f[B]-I\f[R] \f[I]ibase\f[R]] [\f[B]--ibase\f[R]=\f[I]ibase\f[R]] [\f[B]-O\f[R] \f[I]obase\f[R]] [\f[B]--obase\f[R]=\f[I]obase\f[R]] [\f[B]-S\f[R] \f[I]scale\f[R]] [\f[B]--scale\f[R]=\f[I]scale\f[R]] [\f[B]-E\f[R] \f[I]seed\f[R]] [\f[B]--seed\f[R]=\f[I]seed\f[R]] .SH DESCRIPTION -.PP bc(1) is an interactive processor for a language first standardized in 1991 by POSIX. (See the \f[B]STANDARDS\f[R] section.) The language provides unlimited precision decimal arithmetic and is somewhat C-like, but there are differences. Such differences will be noted in this document. .PP After parsing and handling options, this bc(1) reads any files given on the command line and executes them before reading from \f[B]stdin\f[R]. .PP This bc(1) is a drop-in replacement for \f[I]any\f[R] bc(1), including (and especially) the GNU bc(1). It also has many extensions and extra features beyond other implementations. .PP \f[B]Note\f[R]: If running this bc(1) on \f[I]any\f[R] script meant for another bc(1) gives a parse error, it is probably because a word this bc(1) reserves as a keyword is used as the name of a function, variable, or array. To fix that, use the command-line option \f[B]-r\f[R] \f[I]keyword\f[R], where \f[I]keyword\f[R] is the keyword that is used as a name in the script. For more information, see the \f[B]OPTIONS\f[R] section. .PP If parsing scripts meant for other bc(1) implementations still does not work, that is a bug and should be reported. See the \f[B]BUGS\f[R] section. .SH OPTIONS -.PP The following are the options that bc(1) accepts. .TP \f[B]-C\f[R], \f[B]--no-digit-clamp\f[R] Disables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that the value added to a number from a digit is always that digit\[cq]s value multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-c\f[R] or \f[B]--digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-c\f[R], \f[B]--digit-clamp\f[R] Enables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that digits that the value added to a number from a digit that is greater than or equal to the ibase is the value of ibase minus 1 all multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-C\f[R] or \f[B]--no-digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-E\f[R] \f[I]seed\f[R], \f[B]--seed\f[R]=\f[I]seed\f[R] Sets the builtin variable \f[B]seed\f[R] to the value \f[I]seed\f[R] assuming that \f[I]seed\f[R] is in base 10. It is a fatal error if \f[I]seed\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-e\f[R] \f[I]expr\f[R], \f[B]--expression\f[R]=\f[I]expr\f[R] Evaluates \f[I]expr\f[R]. If multiple expressions are given, they are evaluated in order. If files are given as well (see the \f[B]-f\f[R] and \f[B]--file\f[R] options), the expressions and files are evaluated in the order given. This means that if a file is given before an expression, the file is read in and evaluated first. .RS .PP If this option is given on the command-line (i.e., not in \f[B]BC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, bc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R], whether on the command-line or in \f[B]BC_ENV_ARGS\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, bc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-f\f[R] \f[I]file\f[R], \f[B]--file\f[R]=\f[I]file\f[R] Reads in \f[I]file\f[R] and evaluates it, line by line, as though it were read through \f[B]stdin\f[R]. If expressions are also given (see the \f[B]-e\f[R] and \f[B]--expression\f[R] options), the expressions are evaluated in the order given. .RS .PP If this option is given on the command-line (i.e., not in \f[B]BC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, bc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, bc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-g\f[R], \f[B]--global-stacks\f[R] Turns the globals \f[B]ibase\f[R], \f[B]obase\f[R], \f[B]scale\f[R], and \f[B]seed\f[R] into stacks. .RS .PP This has the effect that a copy of the current value of all four are pushed onto a stack for every function call, as well as popped when every function returns. This means that functions can assign to any and all of those globals without worrying that the change will affect other functions. Thus, a hypothetical function named \f[B]output(x,b)\f[R] that simply printed \f[B]x\f[R] in base \f[B]b\f[R] could be written like this: .IP -.nf -\f[C] +.EX define void output(x, b) { obase=b x } -\f[R] -.fi +.EE .PP instead of like this: .IP -.nf -\f[C] +.EX define void output(x, b) { auto c c=obase obase=b x obase=c } -\f[R] -.fi +.EE .PP This makes writing functions much easier. .PP (\f[B]Note\f[R]: the function \f[B]output(x,b)\f[R] exists in the extended math library. See the \f[B]LIBRARY\f[R] section.) .PP However, since using this flag means that functions cannot set \f[B]ibase\f[R], \f[B]obase\f[R], \f[B]scale\f[R], or \f[B]seed\f[R] globally, functions that are made to do so cannot work anymore. There are two possible use cases for that, and each has a solution. .PP First, if a function is called on startup to turn bc(1) into a number converter, it is possible to replace that capability with various shell aliases. Examples: .IP -.nf -\f[C] +.EX alias d2o=\[dq]bc -e ibase=A -e obase=8\[dq] alias h2b=\[dq]bc -e ibase=G -e obase=2\[dq] -\f[R] -.fi +.EE .PP Second, if the purpose of a function is to set \f[B]ibase\f[R], \f[B]obase\f[R], \f[B]scale\f[R], or \f[B]seed\f[R] globally for any other purpose, it could be split into one to four functions (based on how many globals it sets) and each of those functions could return the desired value for a global. .PP For functions that set \f[B]seed\f[R], the value assigned to \f[B]seed\f[R] is not propagated to parent functions. This means that the sequence of pseudo-random numbers that they see will not be the same sequence of pseudo-random numbers that any parent sees. This is only the case once \f[B]seed\f[R] has been set. .PP If a function desires to not affect the sequence of pseudo-random numbers of its parents, but wants to use the same \f[B]seed\f[R], it can use the following line: .IP -.nf -\f[C] +.EX seed = seed -\f[R] -.fi +.EE .PP If the behavior of this option is desired for every run of bc(1), then users could make sure to define \f[B]BC_ENV_ARGS\f[R] and include this option (see the \f[B]ENVIRONMENT VARIABLES\f[R] section for more details). .PP If \f[B]-s\f[R], \f[B]-w\f[R], or any equivalents are used, this option is ignored. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-h\f[R], \f[B]--help\f[R] Prints a usage message and exits. .TP \f[B]-I\f[R] \f[I]ibase\f[R], \f[B]--ibase\f[R]=\f[I]ibase\f[R] Sets the builtin variable \f[B]ibase\f[R] to the value \f[I]ibase\f[R] assuming that \f[I]ibase\f[R] is in base 10. It is a fatal error if \f[I]ibase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-i\f[R], \f[B]--interactive\f[R] Forces interactive mode. (See the \f[B]INTERACTIVE MODE\f[R] section.) .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-L\f[R], \f[B]--no-line-length\f[R] Disables line length checking and prints numbers without backslashes and newlines. In other words, this option sets \f[B]BC_LINE_LENGTH\f[R] to \f[B]0\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-l\f[R], \f[B]--mathlib\f[R] Sets \f[B]scale\f[R] (see the \f[B]SYNTAX\f[R] section) to \f[B]20\f[R] and loads the included math library and the extended math library before running any code, including any expressions or files specified on the command line. .RS .PP To learn what is in the libraries, see the \f[B]LIBRARY\f[R] section. .RE .TP \f[B]-O\f[R] \f[I]obase\f[R], \f[B]--obase\f[R]=\f[I]obase\f[R] Sets the builtin variable \f[B]obase\f[R] to the value \f[I]obase\f[R] assuming that \f[I]obase\f[R] is in base 10. It is a fatal error if \f[I]obase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-P\f[R], \f[B]--no-prompt\f[R] Disables the prompt in TTY mode. (The prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a prompt or are not used to having them in bc(1). Most of those users would want to put this option in \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .RS .PP These options override the \f[B]BC_PROMPT\f[R] and \f[B]BC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-q\f[R], \f[B]--quiet\f[R] This option is for compatibility with the GNU bc(1) (https://www.gnu.org/software/bc/); it is a no-op. Without this option, GNU bc(1) prints a copyright header. This bc(1) only prints the copyright header if one or more of the \f[B]-v\f[R], \f[B]-V\f[R], or \f[B]--version\f[R] options are given unless the \f[B]BC_BANNER\f[R] environment variable is set and contains a non-zero integer or if this bc(1) was built with the header displayed by default. If \f[I]any\f[R] of that is the case, then this option \f[I]does\f[R] prevent bc(1) from printing the header. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-R\f[R], \f[B]--no-read-prompt\f[R] Disables the read prompt in TTY mode. (The read prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a read prompt or are not used to having them in bc(1). Most of those users would want to put this option in \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). This option is also useful in hash bang lines of bc(1) scripts that prompt for user input. .RS .PP This option does not disable the regular prompt because the read prompt is only used when the \f[B]read()\f[R] built-in function is called. .PP These options \f[I]do\f[R] override the \f[B]BC_PROMPT\f[R] and \f[B]BC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), but only for the read prompt. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-r\f[R] \f[I]keyword\f[R], \f[B]--redefine\f[R]=\f[I]keyword\f[R] Redefines \f[I]keyword\f[R] in order to allow it to be used as a function, variable, or array name. This is useful when this bc(1) gives parse errors when parsing scripts meant for other bc(1) implementations. .RS .PP The keywords this bc(1) allows to be redefined are: .IP \[bu] 2 \f[B]abs\f[R] .IP \[bu] 2 \f[B]asciify\f[R] .IP \[bu] 2 \f[B]continue\f[R] .IP \[bu] 2 \f[B]divmod\f[R] .IP \[bu] 2 \f[B]else\f[R] .IP \[bu] 2 \f[B]halt\f[R] .IP \[bu] 2 \f[B]irand\f[R] .IP \[bu] 2 \f[B]last\f[R] .IP \[bu] 2 \f[B]limits\f[R] .IP \[bu] 2 \f[B]maxibase\f[R] .IP \[bu] 2 \f[B]maxobase\f[R] .IP \[bu] 2 \f[B]maxrand\f[R] .IP \[bu] 2 \f[B]maxscale\f[R] .IP \[bu] 2 \f[B]modexp\f[R] .IP \[bu] 2 \f[B]print\f[R] .IP \[bu] 2 \f[B]rand\f[R] .IP \[bu] 2 \f[B]read\f[R] .IP \[bu] 2 \f[B]seed\f[R] .IP \[bu] 2 \f[B]stream\f[R] .PP If any of those keywords are used as a function, variable, or array name in a script, use this option with the keyword as the argument. If multiple are used, use this option for all of them; it can be used multiple times. .PP Keywords are \f[I]not\f[R] redefined when parsing the builtin math library (see the \f[B]LIBRARY\f[R] section). .PP It is a fatal error to redefine keywords mandated by the POSIX standard (see the \f[B]STANDARDS\f[R] section). It is a fatal error to attempt to redefine words that this bc(1) does not reserve as keywords. .RE .TP \f[B]-S\f[R] \f[I]scale\f[R], \f[B]--scale\f[R]=\f[I]scale\f[R] Sets the builtin variable \f[B]scale\f[R] to the value \f[I]scale\f[R] assuming that \f[I]scale\f[R] is in base 10. It is a fatal error if \f[I]scale\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-s\f[R], \f[B]--standard\f[R] Process exactly the language defined by the standard (see the \f[B]STANDARDS\f[R] section) and error if any extensions are used. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-v\f[R], \f[B]-V\f[R], \f[B]--version\f[R] Print the version information (copyright header) and exits. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-w\f[R], \f[B]--warn\f[R] Like \f[B]-s\f[R] and \f[B]--standard\f[R], except that warnings (and not errors) are printed for non-standard extensions and execution continues normally. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-z\f[R], \f[B]--leading-zeroes\f[R] Makes bc(1) print all numbers greater than \f[B]-1\f[R] and less than \f[B]1\f[R], and not equal to \f[B]0\f[R], with a leading zero. .RS .PP This can be set for individual numbers with the \f[B]plz(x)\f[R], \f[B]plznl(x)\f[R], \f[B]pnlz(x)\f[R], and \f[B]pnlznl(x)\f[R] functions in the extended math library (see the \f[B]LIBRARY\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .PP All long options are \f[B]non-portable extensions\f[R]. .SH STDIN -.PP If no files or expressions are given by the \f[B]-f\f[R], \f[B]--file\f[R], \f[B]-e\f[R], or \f[B]--expression\f[R] options, then bc(1) reads from \f[B]stdin\f[R]. .PP However, there are a few caveats to this. .PP First, \f[B]stdin\f[R] is evaluated a line at a time. The only exception to this is if the parse cannot complete. That means that starting a string without ending it or starting a function, \f[B]if\f[R] statement, or loop without ending it will also cause bc(1) to not execute. .PP Second, after an \f[B]if\f[R] statement, bc(1) doesn\[cq]t know if an \f[B]else\f[R] statement will follow, so it will not execute until it knows there will not be an \f[B]else\f[R] statement. .SH STDOUT -.PP Any non-error output is written to \f[B]stdout\f[R]. In addition, if history (see the \f[B]HISTORY\f[R] section) and the prompt (see the \f[B]TTY MODE\f[R] section) are enabled, both are output to \f[B]stdout\f[R]. .PP \f[B]Note\f[R]: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stdout\f[R], so if \f[B]stdout\f[R] is closed, as in -\f[B]bc >&-\f[R], it will quit with an error. +\f[B]bc >&-\f[R], it will quit with an error. This is done so that bc(1) can report problems when \f[B]stdout\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stdout\f[R] to \f[B]/dev/null\f[R]. .SH STDERR -.PP Any error output is written to \f[B]stderr\f[R]. .PP \f[B]Note\f[R]: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stderr\f[R], so if \f[B]stderr\f[R] is closed, as in -\f[B]bc 2>&-\f[R], it will quit with an error. +\f[B]bc 2>&-\f[R], it will quit with an error. This is done so that bc(1) can exit with an error code when \f[B]stderr\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stderr\f[R] to \f[B]/dev/null\f[R]. .SH SYNTAX -.PP The syntax for bc(1) programs is mostly C-like, with some differences. This bc(1) follows the POSIX standard (see the \f[B]STANDARDS\f[R] section), which is a much more thorough resource for the language this bc(1) accepts. This section is meant to be a summary and a listing of all the extensions to the standard. .PP In the sections below, \f[B]E\f[R] means expression, \f[B]S\f[R] means statement, and \f[B]I\f[R] means identifier. .PP Identifiers (\f[B]I\f[R]) start with a lowercase letter and can be followed by any number (up to \f[B]BC_NAME_MAX-1\f[R]) of lowercase letters (\f[B]a-z\f[R]), digits (\f[B]0-9\f[R]), and underscores (\f[B]_\f[R]). The regex is \f[B][a-z][a-z0-9_]*\f[R]. Identifiers with more than one character (letter) are a \f[B]non-portable extension\f[R]. .PP \f[B]ibase\f[R] is a global variable determining how to interpret constant numbers. It is the \[lq]input\[rq] base, or the number base used for interpreting input numbers. \f[B]ibase\f[R] is initially \f[B]10\f[R]. If the \f[B]-s\f[R] (\f[B]--standard\f[R]) and \f[B]-w\f[R] (\f[B]--warn\f[R]) flags were not given on the command line, the max allowable value for \f[B]ibase\f[R] is \f[B]36\f[R]. Otherwise, it is \f[B]16\f[R]. The min allowable value for \f[B]ibase\f[R] is \f[B]2\f[R]. The max allowable value for \f[B]ibase\f[R] can be queried in bc(1) programs with the \f[B]maxibase()\f[R] built-in function. .PP \f[B]obase\f[R] is a global variable determining how to output results. It is the \[lq]output\[rq] base, or the number base used for outputting numbers. \f[B]obase\f[R] is initially \f[B]10\f[R]. The max allowable value for \f[B]obase\f[R] is \f[B]BC_BASE_MAX\f[R] and can be queried in bc(1) programs with the \f[B]maxobase()\f[R] built-in function. The min allowable value for \f[B]obase\f[R] is \f[B]0\f[R]. If \f[B]obase\f[R] is \f[B]0\f[R], values are output in scientific notation, and if \f[B]obase\f[R] is \f[B]1\f[R], values are output in engineering notation. Otherwise, values are output in the specified base. .PP Outputting in scientific and engineering notations are \f[B]non-portable extensions\f[R]. .PP The \f[I]scale\f[R] of an expression is the number of digits in the result of the expression right of the decimal point, and \f[B]scale\f[R] is a global variable that sets the precision of any operations, with exceptions. \f[B]scale\f[R] is initially \f[B]0\f[R]. \f[B]scale\f[R] cannot be negative. The max allowable value for \f[B]scale\f[R] is \f[B]BC_SCALE_MAX\f[R] and can be queried in bc(1) programs with the \f[B]maxscale()\f[R] built-in function. .PP bc(1) has both \f[I]global\f[R] variables and \f[I]local\f[R] variables. All \f[I]local\f[R] variables are local to the function; they are parameters or are introduced in the \f[B]auto\f[R] list of a function (see the \f[B]FUNCTIONS\f[R] section). If a variable is accessed which is not a parameter or in the \f[B]auto\f[R] list, it is assumed to be \f[I]global\f[R]. If a parent function has a \f[I]local\f[R] variable version of a variable that a child function considers \f[I]global\f[R], the value of that \f[I]global\f[R] variable in the child function is the value of the variable in the parent function, not the value of the actual \f[I]global\f[R] variable. .PP All of the above applies to arrays as well. .PP The value of a statement that is an expression (i.e., any of the named expressions or operands) is printed unless the lowest precedence operator is an assignment operator \f[I]and\f[R] the expression is notsurrounded by parentheses. .PP The value that is printed is also assigned to the special variable \f[B]last\f[R]. A single dot (\f[B].\f[R]) may also be used as a synonym for \f[B]last\f[R]. These are \f[B]non-portable extensions\f[R]. .PP Either semicolons or newlines may separate statements. .SS Comments -.PP There are two kinds of comments: .IP "1." 3 Block comments are enclosed in \f[B]/*\f[R] and \f[B]*/\f[R]. .IP "2." 3 Line comments go from \f[B]#\f[R] until, and not including, the next newline. This is a \f[B]non-portable extension\f[R]. .SS Named Expressions -.PP The following are named expressions in bc(1): .IP "1." 3 Variables: \f[B]I\f[R] .IP "2." 3 Array Elements: \f[B]I[E]\f[R] .IP "3." 3 \f[B]ibase\f[R] .IP "4." 3 \f[B]obase\f[R] .IP "5." 3 \f[B]scale\f[R] .IP "6." 3 \f[B]seed\f[R] .IP "7." 3 \f[B]last\f[R] or a single dot (\f[B].\f[R]) .PP Numbers 6 and 7 are \f[B]non-portable extensions\f[R]. .PP The meaning of \f[B]seed\f[R] is dependent on the current pseudo-random number generator but is guaranteed to not change except for new major versions. .PP The \f[I]scale\f[R] and sign of the value may be significant. .PP If a previously used \f[B]seed\f[R] value is assigned to \f[B]seed\f[R] and used again, the pseudo-random number generator is guaranteed to produce the same sequence of pseudo-random numbers as it did when the \f[B]seed\f[R] value was previously used. .PP The exact value assigned to \f[B]seed\f[R] is not guaranteed to be returned if \f[B]seed\f[R] is queried again immediately. However, if \f[B]seed\f[R] \f[I]does\f[R] return a different value, both values, when assigned to \f[B]seed\f[R], are guaranteed to produce the same sequence of pseudo-random numbers. This means that certain values assigned to \f[B]seed\f[R] will \f[I]not\f[R] produce unique sequences of pseudo-random numbers. The value of \f[B]seed\f[R] will change after any use of the \f[B]rand()\f[R] and \f[B]irand(E)\f[R] operands (see the \f[I]Operands\f[R] subsection below), except if the parameter passed to \f[B]irand(E)\f[R] is \f[B]0\f[R], \f[B]1\f[R], or negative. .PP There is no limit to the length (number of significant decimal digits) or \f[I]scale\f[R] of the value that can be assigned to \f[B]seed\f[R]. .PP Variables and arrays do not interfere; users can have arrays named the same as variables. This also applies to functions (see the \f[B]FUNCTIONS\f[R] section), so a user can have a variable, array, and function that all have the same name, and they will not shadow each other, whether inside of functions or not. .PP Named expressions are required as the operand of \f[B]increment\f[R]/\f[B]decrement\f[R] operators and as the left side of \f[B]assignment\f[R] operators (see the \f[I]Operators\f[R] subsection). .SS Operands -.PP The following are valid operands in bc(1): .IP " 1." 4 Numbers (see the \f[I]Numbers\f[R] subsection below). .IP " 2." 4 Array indices (\f[B]I[E]\f[R]). .IP " 3." 4 \f[B](E)\f[R]: The value of \f[B]E\f[R] (used to change precedence). .IP " 4." 4 \f[B]sqrt(E)\f[R]: The square root of \f[B]E\f[R]. \f[B]E\f[R] must be non-negative. .IP " 5." 4 \f[B]length(E)\f[R]: The number of significant decimal digits in \f[B]E\f[R]. Returns \f[B]1\f[R] for \f[B]0\f[R] with no decimal places. If given a string, the length of the string is returned. Passing a string to \f[B]length(E)\f[R] is a \f[B]non-portable extension\f[R]. .IP " 6." 4 \f[B]length(I[])\f[R]: The number of elements in the array \f[B]I\f[R]. This is a \f[B]non-portable extension\f[R]. .IP " 7." 4 \f[B]scale(E)\f[R]: The \f[I]scale\f[R] of \f[B]E\f[R]. .IP " 8." 4 \f[B]abs(E)\f[R]: The absolute value of \f[B]E\f[R]. This is a \f[B]non-portable extension\f[R]. .IP " 9." 4 \f[B]is_number(E)\f[R]: \f[B]1\f[R] if the given argument is a number, \f[B]0\f[R] if it is a string. This is a \f[B]non-portable extension\f[R]. .IP "10." 4 \f[B]is_string(E)\f[R]: \f[B]1\f[R] if the given argument is a string, \f[B]0\f[R] if it is a number. This is a \f[B]non-portable extension\f[R]. .IP "11." 4 \f[B]modexp(E, E, E)\f[R]: Modular exponentiation, where the first expression is the base, the second is the exponent, and the third is the modulus. All three values must be integers. The second argument must be non-negative. The third argument must be non-zero. This is a \f[B]non-portable extension\f[R]. .IP "12." 4 \f[B]divmod(E, E, I[])\f[R]: Division and modulus in one operation. This is for optimization. The first expression is the dividend, and the second is the divisor, which must be non-zero. The return value is the quotient, and the modulus is stored in index \f[B]0\f[R] of the provided array (the last argument). This is a \f[B]non-portable extension\f[R]. .IP "13." 4 \f[B]asciify(E)\f[R]: If \f[B]E\f[R] is a string, returns a string that is the first letter of its argument. If it is a number, calculates the number mod \f[B]256\f[R] and returns that number as a one-character string. This is a \f[B]non-portable extension\f[R]. .IP "14." 4 \f[B]asciify(I[])\f[R]: A string that is made up of the characters that would result from running \f[B]asciify(E)\f[R] on each element of the array identified by the argument. This allows creating multi-character strings and storing them. This is a \f[B]non-portable extension\f[R]. .IP "15." 4 \f[B]I()\f[R], \f[B]I(E)\f[R], \f[B]I(E, E)\f[R], and so on, where \f[B]I\f[R] is an identifier for a non-\f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). The \f[B]E\f[R] argument(s) may also be arrays of the form \f[B]I[]\f[R], which will automatically be turned into array references (see the \f[I]Array References\f[R] subsection of the \f[B]FUNCTIONS\f[R] section) if the corresponding parameter in the function definition is an array reference. .IP "16." 4 \f[B]read()\f[R]: Reads a line from \f[B]stdin\f[R] and uses that as an expression. The result of that expression is the result of the \f[B]read()\f[R] operand. This is a \f[B]non-portable extension\f[R]. .IP "17." 4 \f[B]maxibase()\f[R]: The max allowable \f[B]ibase\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "18." 4 \f[B]maxobase()\f[R]: The max allowable \f[B]obase\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "19." 4 \f[B]maxscale()\f[R]: The max allowable \f[B]scale\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "20." 4 \f[B]line_length()\f[R]: The line length set with \f[B]BC_LINE_LENGTH\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). This is a \f[B]non-portable extension\f[R]. .IP "21." 4 \f[B]global_stacks()\f[R]: \f[B]0\f[R] if global stacks are not enabled with the \f[B]-g\f[R] or \f[B]--global-stacks\f[R] options, non-zero otherwise. See the \f[B]OPTIONS\f[R] section. This is a \f[B]non-portable extension\f[R]. .IP "22." 4 \f[B]leading_zero()\f[R]: \f[B]0\f[R] if leading zeroes are not enabled with the \f[B]-z\f[R] or \f[B]\[en]leading-zeroes\f[R] options, non-zero otherwise. See the \f[B]OPTIONS\f[R] section. This is a \f[B]non-portable extension\f[R]. .IP "23." 4 \f[B]rand()\f[R]: A pseudo-random integer between \f[B]0\f[R] (inclusive) and \f[B]BC_RAND_MAX\f[R] (inclusive). Using this operand will change the value of \f[B]seed\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "24." 4 \f[B]irand(E)\f[R]: A pseudo-random integer between \f[B]0\f[R] (inclusive) and the value of \f[B]E\f[R] (exclusive). If \f[B]E\f[R] is negative or is a non-integer (\f[B]E\f[R]\[cq]s \f[I]scale\f[R] is not \f[B]0\f[R]), an error is raised, and bc(1) resets (see the \f[B]RESET\f[R] section) while \f[B]seed\f[R] remains unchanged. If \f[B]E\f[R] is larger than \f[B]BC_RAND_MAX\f[R], the higher bound is honored by generating several pseudo-random integers, multiplying them by appropriate powers of \f[B]BC_RAND_MAX+1\f[R], and adding them together. Thus, the size of integer that can be generated with this operand is unbounded. Using this operand will change the value of \f[B]seed\f[R], unless the value of \f[B]E\f[R] is \f[B]0\f[R] or \f[B]1\f[R]. In that case, \f[B]0\f[R] is returned, and \f[B]seed\f[R] is \f[I]not\f[R] changed. This is a \f[B]non-portable extension\f[R]. .IP "25." 4 \f[B]maxrand()\f[R]: The max integer returned by \f[B]rand()\f[R]. This is a \f[B]non-portable extension\f[R]. .PP The integers generated by \f[B]rand()\f[R] and \f[B]irand(E)\f[R] are guaranteed to be as unbiased as possible, subject to the limitations of the pseudo-random number generator. .PP \f[B]Note\f[R]: The values returned by the pseudo-random number generator with \f[B]rand()\f[R] and \f[B]irand(E)\f[R] are guaranteed to \f[I]NOT\f[R] be cryptographically secure. This is a consequence of using a seeded pseudo-random number generator. However, they \f[I]are\f[R] guaranteed to be reproducible with identical \f[B]seed\f[R] values. This means that the pseudo-random values from bc(1) should only be used where a reproducible stream of pseudo-random numbers is \f[I]ESSENTIAL\f[R]. In any other case, use a non-seeded pseudo-random number generator. .SS Numbers -.PP Numbers are strings made up of digits, uppercase letters, and at most \f[B]1\f[R] period for a radix. Numbers can have up to \f[B]BC_NUM_MAX\f[R] digits. Uppercase letters are equal to \f[B]9\f[R] plus their position in the alphabet, starting from \f[B]1\f[R] (i.e., \f[B]A\f[R] equals \f[B]10\f[R], or \f[B]9+1\f[R]). .PP If a digit or letter makes no sense with the current value of \f[B]ibase\f[R] (i.e., they are greater than or equal to the current value of \f[B]ibase\f[R]), then the behavior depends on the existence of the \f[B]-c\f[R]/\f[B]--digit-clamp\f[R] or \f[B]-C\f[R]/\f[B]--no-digit-clamp\f[R] options (see the \f[B]OPTIONS\f[R] section), the existence and setting of the \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or the default, which can be queried with the \f[B]-h\f[R]/\f[B]--help\f[R] option. .PP If clamping is off, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are not changed. Instead, their given value is multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*A+3\[ha]0*B\f[R], which is \f[B]3\f[R] times \f[B]10\f[R] plus \f[B]11\f[R], or \f[B]41\f[R]. .PP If clamping is on, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are set to the value of the highest valid digit in \f[B]ibase\f[R] before being multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*2+3\[ha]0*2\f[R], which is \f[B]3\f[R] times \f[B]2\f[R] plus \f[B]2\f[R], or \f[B]8\f[R]. .PP There is one exception to clamping: single-character numbers (i.e., \f[B]A\f[R] alone). Such numbers are never clamped and always take the value they would have in the highest possible \f[B]ibase\f[R]. This means that \f[B]A\f[R] alone always equals decimal \f[B]10\f[R] and \f[B]Z\f[R] alone always equals decimal \f[B]35\f[R]. This behavior is mandated by the standard (see the STANDARDS section) and is meant to provide an easy way to set the current \f[B]ibase\f[R] (with the \f[B]i\f[R] command) regardless of the current value of \f[B]ibase\f[R]. .PP If clamping is on, and the clamped value of a character is needed, use a leading zero, i.e., for \f[B]A\f[R], use \f[B]0A\f[R]. .PP In addition, bc(1) accepts numbers in scientific notation. These have the form \f[B]e\f[R]. The exponent (the portion after the \f[B]e\f[R]) must be an integer. An example is \f[B]1.89237e9\f[R], which is equal to \f[B]1892370000\f[R]. Negative exponents are also allowed, so \f[B]4.2890e-3\f[R] is equal to \f[B]0.0042890\f[R]. .PP Using scientific notation is an error or warning if the \f[B]-s\f[R] or \f[B]-w\f[R], respectively, command-line options (or equivalents) are given. .PP \f[B]WARNING\f[R]: Both the number and the exponent in scientific notation are interpreted according to the current \f[B]ibase\f[R], but the number is still multiplied by \f[B]10\[ha]exponent\f[R] regardless of the current \f[B]ibase\f[R]. For example, if \f[B]ibase\f[R] is \f[B]16\f[R] and bc(1) is given the number string \f[B]FFeA\f[R], the resulting decimal number will be \f[B]2550000000000\f[R], and if bc(1) is given the number string \f[B]10e-4\f[R], the resulting decimal number will be \f[B]0.0016\f[R]. .PP Accepting input as scientific notation is a \f[B]non-portable extension\f[R]. .SS Operators -.PP The following arithmetic and logical operators can be used. They are listed in order of decreasing precedence. Operators in the same group have the same precedence. .TP \f[B]++\f[R] \f[B]--\f[R] Type: Prefix and Postfix .RS .PP Associativity: None .PP Description: \f[B]increment\f[R], \f[B]decrement\f[R] .RE .TP \f[B]-\f[R] \f[B]!\f[R] Type: Prefix .RS .PP Associativity: None .PP Description: \f[B]negation\f[R], \f[B]boolean not\f[R] .RE .TP \f[B]$\f[R] Type: Postfix .RS .PP Associativity: None .PP Description: \f[B]truncation\f[R] .RE .TP \f[B]\[at]\f[R] Type: Binary .RS .PP Associativity: Right .PP Description: \f[B]set precision\f[R] .RE .TP \f[B]\[ha]\f[R] Type: Binary .RS .PP Associativity: Right .PP Description: \f[B]power\f[R] .RE .TP \f[B]*\f[R] \f[B]/\f[R] \f[B]%\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]multiply\f[R], \f[B]divide\f[R], \f[B]modulus\f[R] .RE .TP \f[B]+\f[R] \f[B]-\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]add\f[R], \f[B]subtract\f[R] .RE .TP \f[B]<<\f[R] \f[B]>>\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]shift left\f[R], \f[B]shift right\f[R] .RE .TP \f[B]=\f[R] \f[B]<<=\f[R] \f[B]>>=\f[R] \f[B]+=\f[R] \f[B]-=\f[R] \f[B]*=\f[R] \f[B]/=\f[R] \f[B]%=\f[R] \f[B]\[ha]=\f[R] \f[B]\[at]=\f[R] Type: Binary .RS .PP Associativity: Right .PP Description: \f[B]assignment\f[R] .RE .TP \f[B]==\f[R] \f[B]<=\f[R] \f[B]>=\f[R] \f[B]!=\f[R] \f[B]<\f[R] \f[B]>\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]relational\f[R] .RE .TP \f[B]&&\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]boolean and\f[R] .RE .TP \f[B]||\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]boolean or\f[R] .RE .PP The operators will be described in more detail below. .TP \f[B]++\f[R] \f[B]--\f[R] The prefix and postfix \f[B]increment\f[R] and \f[B]decrement\f[R] -operators behave exactly like they would in C. -They require a named expression (see the \f[I]Named Expressions\f[R] -subsection) as an operand. +operators behave exactly like they would in C. They require a named +expression (see the \f[I]Named Expressions\f[R] subsection) as an +operand. .RS .PP The prefix versions of these operators are more efficient; use them where possible. .RE .TP \f[B]-\f[R] The \f[B]negation\f[R] operator returns \f[B]0\f[R] if a user attempts to negate any expression with the value \f[B]0\f[R]. Otherwise, a copy of the expression with its sign flipped is returned. .TP \f[B]!\f[R] The \f[B]boolean not\f[R] operator returns \f[B]1\f[R] if the expression is \f[B]0\f[R], or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]$\f[R] The \f[B]truncation\f[R] operator returns a copy of the given expression with all of its \f[I]scale\f[R] removed. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[at]\f[R] The \f[B]set precision\f[R] operator takes two expressions and returns a copy of the first with its \f[I]scale\f[R] equal to the value of the second expression. That could either mean that the number is returned without change (if the \f[I]scale\f[R] of the first expression matches the value of the second expression), extended (if it is less), or truncated (if it is more). .RS .PP The second expression must be an integer (no \f[I]scale\f[R]) and non-negative. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[ha]\f[R] The \f[B]power\f[R] operator (not the \f[B]exclusive or\f[R] operator, as it would be in C) takes two expressions and raises the first to the power of the value of the second. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The second expression must be an integer (no \f[I]scale\f[R]), and if it is negative, the first value must be non-zero. .RE .TP \f[B]*\f[R] The \f[B]multiply\f[R] operator takes two expressions, multiplies them, and returns the product. If \f[B]a\f[R] is the \f[I]scale\f[R] of the first expression and \f[B]b\f[R] is the \f[I]scale\f[R] of the second expression, the \f[I]scale\f[R] of the result is equal to \f[B]min(a+b,max(scale,a,b))\f[R] where \f[B]min()\f[R] and \f[B]max()\f[R] return the obvious values. .TP \f[B]/\f[R] The \f[B]divide\f[R] operator takes two expressions, divides them, and returns the quotient. The \f[I]scale\f[R] of the result shall be the value of \f[B]scale\f[R]. .RS .PP The second expression must be non-zero. .RE .TP \f[B]%\f[R] The \f[B]modulus\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and evaluates them by 1) Computing \f[B]a/b\f[R] to current \f[B]scale\f[R] and 2) Using the result of step 1 to calculate \f[B]a-(a/b)*b\f[R] to \f[I]scale\f[R] \f[B]max(scale+scale(b),scale(a))\f[R]. .RS .PP The second expression must be non-zero. .RE .TP \f[B]+\f[R] The \f[B]add\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and returns the sum, with a \f[I]scale\f[R] equal to the max of the \f[I]scale\f[R]s of \f[B]a\f[R] and \f[B]b\f[R]. .TP \f[B]-\f[R] The \f[B]subtract\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and returns the difference, with a \f[I]scale\f[R] equal to the max of the \f[I]scale\f[R]s of \f[B]a\f[R] and \f[B]b\f[R]. .TP \f[B]<<\f[R] The \f[B]left shift\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and returns a copy of the value of \f[B]a\f[R] with its decimal point moved \f[B]b\f[R] places to the right. .RS .PP The second expression must be an integer (no \f[I]scale\f[R]) and non-negative. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]>>\f[R] The \f[B]right shift\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and returns a copy of the value of \f[B]a\f[R] with its decimal point moved \f[B]b\f[R] places to the left. .RS .PP The second expression must be an integer (no \f[I]scale\f[R]) and non-negative. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]=\f[R] \f[B]<<=\f[R] \f[B]>>=\f[R] \f[B]+=\f[R] \f[B]-=\f[R] \f[B]*=\f[R] \f[B]/=\f[R] \f[B]%=\f[R] \f[B]\[ha]=\f[R] \f[B]\[at]=\f[R] The \f[B]assignment\f[R] operators take two expressions, \f[B]a\f[R] and \f[B]b\f[R] where \f[B]a\f[R] is a named expression (see the \f[I]Named Expressions\f[R] subsection). .RS .PP For \f[B]=\f[R], \f[B]b\f[R] is copied and the result is assigned to \f[B]a\f[R]. For all others, \f[B]a\f[R] and \f[B]b\f[R] are applied as operands to the corresponding arithmetic operator and the result is assigned to \f[B]a\f[R]. .PP The \f[B]assignment\f[R] operators that correspond to operators that are extensions are themselves \f[B]non-portable extensions\f[R]. .RE .TP \f[B]==\f[R] \f[B]<=\f[R] \f[B]>=\f[R] \f[B]!=\f[R] \f[B]<\f[R] \f[B]>\f[R] The \f[B]relational\f[R] operators compare two expressions, \f[B]a\f[R] and \f[B]b\f[R], and if the relation holds, according to C language semantics, the result is \f[B]1\f[R]. Otherwise, it is \f[B]0\f[R]. .RS .PP Note that unlike in C, these operators have a lower precedence than the \f[B]assignment\f[R] operators, which means that \f[B]a=b>c\f[R] is interpreted as \f[B](a=b)>c\f[R]. .PP Also, unlike the standard (see the \f[B]STANDARDS\f[R] section) requires, these operators can appear anywhere any other expressions can be used. This allowance is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]&&\f[R] The \f[B]boolean and\f[R] operator takes two expressions and returns \f[B]1\f[R] if both expressions are non-zero, \f[B]0\f[R] otherwise. .RS .PP This is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]||\f[R] The \f[B]boolean or\f[R] operator takes two expressions and returns \f[B]1\f[R] if one of the expressions is non-zero, \f[B]0\f[R] otherwise. .RS .PP This is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Statements -.PP The following items are statements: .IP " 1." 4 \f[B]E\f[R] .IP " 2." 4 \f[B]{\f[R] \f[B]S\f[R] \f[B];\f[R] \&... \f[B];\f[R] \f[B]S\f[R] \f[B]}\f[R] .IP " 3." 4 \f[B]if\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] .IP " 4." 4 \f[B]if\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] \f[B]else\f[R] \f[B]S\f[R] .IP " 5." 4 \f[B]while\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] .IP " 6." 4 \f[B]for\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B];\f[R] \f[B]E\f[R] \f[B];\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] .IP " 7." 4 An empty statement .IP " 8." 4 \f[B]break\f[R] .IP " 9." 4 \f[B]continue\f[R] .IP "10." 4 \f[B]quit\f[R] .IP "11." 4 \f[B]halt\f[R] .IP "12." 4 \f[B]limits\f[R] .IP "13." 4 A string of characters, enclosed in double quotes .IP "14." 4 \f[B]print\f[R] \f[B]E\f[R] \f[B],\f[R] \&... \f[B],\f[R] \f[B]E\f[R] .IP "15." 4 \f[B]stream\f[R] \f[B]E\f[R] \f[B],\f[R] \&... \f[B],\f[R] \f[B]E\f[R] .IP "16." 4 \f[B]I()\f[R], \f[B]I(E)\f[R], \f[B]I(E, E)\f[R], and so on, where \f[B]I\f[R] is an identifier for a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). The \f[B]E\f[R] argument(s) may also be arrays of the form \f[B]I[]\f[R], which will automatically be turned into array references (see the \f[I]Array References\f[R] subsection of the \f[B]FUNCTIONS\f[R] section) if the corresponding parameter in the function definition is an array reference. .PP Numbers 4, 9, 11, 12, 14, 15, and 16 are \f[B]non-portable extensions\f[R]. .PP Also, as a \f[B]non-portable extension\f[R], any or all of the expressions in the header of a for loop may be omitted. If the condition (second expression) is omitted, it is assumed to be a constant \f[B]1\f[R]. .PP The \f[B]break\f[R] statement causes a loop to stop iterating and resume execution immediately following a loop. This is only allowed in loops. .PP The \f[B]continue\f[R] statement causes a loop iteration to stop early and returns to the start of the loop, including testing the loop condition. This is only allowed in loops. .PP The \f[B]if\f[R] \f[B]else\f[R] statement does the same thing as in C. .PP The \f[B]quit\f[R] statement causes bc(1) to quit, even if it is on a branch that will not be executed (it is a compile-time command). .PP \f[B]Warning\f[R]: The behavior of this bc(1) on \f[B]quit\f[R] is slightly different from other bc(1) implementations. Other bc(1) implementations will exit as soon as they finish parsing the line that a \f[B]quit\f[R] command is on. This bc(1) will execute any completed and executable statements that occur before the \f[B]quit\f[R] statement before exiting. .PP In other words, for the bc(1) code below: .IP -.nf -\f[C] +.EX for (i = 0; i < 3; ++i) i; quit -\f[R] -.fi +.EE .PP Other bc(1) implementations will print nothing, and this bc(1) will print \f[B]0\f[R], \f[B]1\f[R], and \f[B]2\f[R] on successive lines before exiting. .PP The \f[B]halt\f[R] statement causes bc(1) to quit, if it is executed. (Unlike \f[B]quit\f[R] if it is on a branch of an \f[B]if\f[R] statement that is not executed, bc(1) does not quit.) .PP The \f[B]limits\f[R] statement prints the limits that this bc(1) is subject to. This is like the \f[B]quit\f[R] statement in that it is a compile-time command. .PP An expression by itself is evaluated and printed, followed by a newline. .PP Both scientific notation and engineering notation are available for printing the results of expressions. Scientific notation is activated by assigning \f[B]0\f[R] to \f[B]obase\f[R], and engineering notation is activated by assigning \f[B]1\f[R] to \f[B]obase\f[R]. To deactivate them, just assign a different value to \f[B]obase\f[R]. .PP Scientific notation and engineering notation are disabled if bc(1) is run with either the \f[B]-s\f[R] or \f[B]-w\f[R] command-line options (or equivalents). .PP Printing numbers in scientific notation and/or engineering notation is a \f[B]non-portable extension\f[R]. .SS Strings -.PP If strings appear as a statement by themselves, they are printed without a trailing newline. .PP In addition to appearing as a lone statement by themselves, strings can be assigned to variables and array elements. They can also be passed to functions in variable parameters. .PP If any statement that expects a string is given a variable that had a string assigned to it, the statement acts as though it had received a string. .PP If any math operation is attempted on a string or a variable or array element that has been assigned a string, an error is raised, and bc(1) resets (see the \f[B]RESET\f[R] section). .PP Assigning strings to variables and array elements and passing them to functions are \f[B]non-portable extensions\f[R]. .SS Print Statement -.PP The \[lq]expressions\[rq] in a \f[B]print\f[R] statement may also be strings. If they are, there are backslash escape sequences that are interpreted specially. What those sequences are, and what they cause to be printed, are shown below: .PP \f[B]\[rs]a\f[R]: \f[B]\[rs]a\f[R] .PP \f[B]\[rs]b\f[R]: \f[B]\[rs]b\f[R] .PP \f[B]\[rs]\[rs]\f[R]: \f[B]\[rs]\f[R] .PP \f[B]\[rs]e\f[R]: \f[B]\[rs]\f[R] .PP \f[B]\[rs]f\f[R]: \f[B]\[rs]f\f[R] .PP \f[B]\[rs]n\f[R]: \f[B]\[rs]n\f[R] .PP \f[B]\[rs]q\f[R]: \f[B]\[lq]\f[R] .PP \f[B]\[rs]r\f[R]: \f[B]\[rs]r\f[R] .PP \f[B]\[rs]t\f[R]: \f[B]\[rs]t\f[R] .PP Any other character following a backslash causes the backslash and character to be printed as-is. .PP Any non-string expression in a print statement shall be assigned to \f[B]last\f[R], like any other expression that is printed. .SS Stream Statement -.PP The expressions in a \f[B]stream\f[R] statement may also be strings. .PP If a \f[B]stream\f[R] statement is given a string, it prints the string as though the string had appeared as its own statement. In other words, the \f[B]stream\f[R] statement prints strings normally, without a newline. .PP If a \f[B]stream\f[R] statement is given a number, a copy of it is truncated and its absolute value is calculated. The result is then printed as though \f[B]obase\f[R] is \f[B]256\f[R] and each digit is interpreted as an 8-bit ASCII character, making it a byte stream. .SS Order of Evaluation -.PP All expressions in a statment are evaluated left to right, except as necessary to maintain order of operations. This means, for example, assuming that \f[B]i\f[R] is equal to \f[B]0\f[R], in the expression .IP -.nf -\f[C] +.EX a[i++] = i++ -\f[R] -.fi +.EE .PP the first (or 0th) element of \f[B]a\f[R] is set to \f[B]1\f[R], and \f[B]i\f[R] is equal to \f[B]2\f[R] at the end of the expression. .PP This includes function arguments. Thus, assuming \f[B]i\f[R] is equal to \f[B]0\f[R], this means that in the expression .IP -.nf -\f[C] +.EX x(i++, i++) -\f[R] -.fi +.EE .PP the first argument passed to \f[B]x()\f[R] is \f[B]0\f[R], and the second argument is \f[B]1\f[R], while \f[B]i\f[R] is equal to \f[B]2\f[R] before the function starts executing. .SH FUNCTIONS -.PP Function definitions are as follows: .IP -.nf -\f[C] +.EX define I(I,...,I){ auto I,...,I S;...;S return(E) } -\f[R] -.fi +.EE .PP Any \f[B]I\f[R] in the parameter list or \f[B]auto\f[R] list may be replaced with \f[B]I[]\f[R] to make a parameter or \f[B]auto\f[R] var an array, and any \f[B]I\f[R] in the parameter list may be replaced with \f[B]*I[]\f[R] to make a parameter an array reference. Callers of functions that take array references should not put an asterisk in the call; they must be called with just \f[B]I[]\f[R] like normal array parameters and will be automatically converted into references. .PP As a \f[B]non-portable extension\f[R], the opening brace of a \f[B]define\f[R] statement may appear on the next line. .PP As a \f[B]non-portable extension\f[R], the return statement may also be in one of the following forms: .IP "1." 3 \f[B]return\f[R] .IP "2." 3 \f[B]return\f[R] \f[B](\f[R] \f[B])\f[R] .IP "3." 3 \f[B]return\f[R] \f[B]E\f[R] .PP The first two, or not specifying a \f[B]return\f[R] statement, is equivalent to \f[B]return (0)\f[R], unless the function is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection below). .SS Void Functions -.PP Functions can also be \f[B]void\f[R] functions, defined as follows: .IP -.nf -\f[C] +.EX define void I(I,...,I){ auto I,...,I S;...;S return } -\f[R] -.fi +.EE .PP They can only be used as standalone expressions, where such an expression would be printed alone, except in a print statement. .PP Void functions can only use the first two \f[B]return\f[R] statements listed above. They can also omit the return statement entirely. .PP The word \[lq]void\[rq] is not treated as a keyword; it is still possible to have variables, arrays, and functions named \f[B]void\f[R]. The word \[lq]void\[rq] is only treated specially right after the \f[B]define\f[R] keyword. .PP This is a \f[B]non-portable extension\f[R]. .SS Array References -.PP For any array in the parameter list, if the array is declared in the form .IP -.nf -\f[C] +.EX *I[] -\f[R] -.fi +.EE .PP it is a \f[B]reference\f[R]. Any changes to the array in the function are reflected, when the function returns, to the array that was passed in. .PP Other than this, all function arguments are passed by value. .PP This is a \f[B]non-portable extension\f[R]. .SH LIBRARY -.PP All of the functions below, including the functions in the extended math library (see the \f[I]Extended Library\f[R] subsection below), are available when the \f[B]-l\f[R] or \f[B]--mathlib\f[R] command-line flags are given, except that the extended math library is not available when the \f[B]-s\f[R] option, the \f[B]-w\f[R] option, or equivalents are given. .SS Standard Library -.PP The standard (see the \f[B]STANDARDS\f[R] section) defines the following functions for the math library: .TP \f[B]s(x)\f[R] Returns the sine of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]c(x)\f[R] Returns the cosine of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]a(x)\f[R] Returns the arctangent of \f[B]x\f[R], in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]l(x)\f[R] Returns the natural logarithm of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]e(x)\f[R] Returns the mathematical constant \f[B]e\f[R] raised to the power of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]j(x, n)\f[R] Returns the bessel integer order \f[B]n\f[R] (truncated) of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .SS Extended Library -.PP The extended library is \f[I]not\f[R] loaded when the \f[B]-s\f[R]/\f[B]--standard\f[R] or \f[B]-w\f[R]/\f[B]--warn\f[R] options are given since they are not part of the library defined by the standard (see the \f[B]STANDARDS\f[R] section). .PP The extended library is a \f[B]non-portable extension\f[R]. .TP \f[B]p(x, y)\f[R] Calculates \f[B]x\f[R] to the power of \f[B]y\f[R], even if \f[B]y\f[R] is not an integer, and returns the result to the current \f[B]scale\f[R]. .RS .PP It is an error if \f[B]y\f[R] is negative and \f[B]x\f[R] is \f[B]0\f[R]. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]r(x, p)\f[R] Returns \f[B]x\f[R] rounded to \f[B]p\f[R] decimal places according to the rounding mode round half away from \f[B]0\f[R] (https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero). .TP \f[B]ceil(x, p)\f[R] Returns \f[B]x\f[R] rounded to \f[B]p\f[R] decimal places according to the rounding mode round away from \f[B]0\f[R] (https://en.wikipedia.org/wiki/Rounding#Rounding_away_from_zero). .TP \f[B]f(x)\f[R] Returns the factorial of the truncated absolute value of \f[B]x\f[R]. .TP \f[B]max(a, b)\f[R] Returns \f[B]a\f[R] if \f[B]a\f[R] is greater than \f[B]b\f[R]; otherwise, returns \f[B]b\f[R]. .TP \f[B]min(a, b)\f[R] Returns \f[B]a\f[R] if \f[B]a\f[R] is less than \f[B]b\f[R]; otherwise, returns \f[B]b\f[R]. .TP \f[B]perm(n, k)\f[R] Returns the permutation of the truncated absolute value of \f[B]n\f[R] of the truncated absolute value of \f[B]k\f[R], if \f[B]k <= n\f[R]. If not, it returns \f[B]0\f[R]. .TP \f[B]comb(n, k)\f[R] Returns the combination of the truncated absolute value of \f[B]n\f[R] of the truncated absolute value of \f[B]k\f[R], if \f[B]k <= n\f[R]. If not, it returns \f[B]0\f[R]. .TP \f[B]fib(n)\f[R] Returns the Fibonacci number of the truncated absolute value of \f[B]n\f[R]. .TP \f[B]l2(x)\f[R] Returns the logarithm base \f[B]2\f[R] of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]l10(x)\f[R] Returns the logarithm base \f[B]10\f[R] of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]log(x, b)\f[R] Returns the logarithm base \f[B]b\f[R] of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]cbrt(x)\f[R] Returns the cube root of \f[B]x\f[R]. .TP \f[B]root(x, n)\f[R] Calculates the truncated value of \f[B]n\f[R], \f[B]r\f[R], and returns the \f[B]r\f[R]th root of \f[B]x\f[R] to the current \f[B]scale\f[R]. .RS .PP If \f[B]r\f[R] is \f[B]0\f[R] or negative, this raises an error and causes bc(1) to reset (see the \f[B]RESET\f[R] section). It also raises an error and causes bc(1) to reset if \f[B]r\f[R] is even and \f[B]x\f[R] is negative. .RE .TP \f[B]gcd(a, b)\f[R] Returns the greatest common divisor (factor) of the truncated absolute value of \f[B]a\f[R] and the truncated absolute value of \f[B]b\f[R]. .TP \f[B]lcm(a, b)\f[R] Returns the least common multiple of the truncated absolute value of \f[B]a\f[R] and the truncated absolute value of \f[B]b\f[R]. .TP \f[B]pi(p)\f[R] Returns \f[B]pi\f[R] to \f[B]p\f[R] decimal places. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]t(x)\f[R] Returns the tangent of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]a2(y, x)\f[R] Returns the arctangent of \f[B]y/x\f[R], in radians. If both \f[B]y\f[R] and \f[B]x\f[R] are equal to \f[B]0\f[R], it raises an error and causes bc(1) to reset (see the \f[B]RESET\f[R] section). Otherwise, if \f[B]x\f[R] is greater than \f[B]0\f[R], it returns \f[B]a(y/x)\f[R]. If \f[B]x\f[R] is less than \f[B]0\f[R], and \f[B]y\f[R] is greater than or equal to \f[B]0\f[R], it returns \f[B]a(y/x)+pi\f[R]. If \f[B]x\f[R] is less than \f[B]0\f[R], and \f[B]y\f[R] is less than \f[B]0\f[R], it returns \f[B]a(y/x)-pi\f[R]. If \f[B]x\f[R] is equal to \f[B]0\f[R], and \f[B]y\f[R] is greater than \f[B]0\f[R], it returns \f[B]pi/2\f[R]. If \f[B]x\f[R] is equal to \f[B]0\f[R], and \f[B]y\f[R] is less than \f[B]0\f[R], it returns \f[B]-pi/2\f[R]. .RS .PP This function is the same as the \f[B]atan2()\f[R] function in many programming languages. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]sin(x)\f[R] Returns the sine of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is an alias of \f[B]s(x)\f[R]. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]cos(x)\f[R] Returns the cosine of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is an alias of \f[B]c(x)\f[R]. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]tan(x)\f[R] Returns the tangent of \f[B]x\f[R], which is assumed to be in radians. .RS .PP If \f[B]x\f[R] is equal to \f[B]1\f[R] or \f[B]-1\f[R], this raises an error and causes bc(1) to reset (see the \f[B]RESET\f[R] section). .PP This is an alias of \f[B]t(x)\f[R]. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]atan(x)\f[R] Returns the arctangent of \f[B]x\f[R], in radians. .RS .PP This is an alias of \f[B]a(x)\f[R]. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]atan2(y, x)\f[R] Returns the arctangent of \f[B]y/x\f[R], in radians. If both \f[B]y\f[R] and \f[B]x\f[R] are equal to \f[B]0\f[R], it raises an error and causes bc(1) to reset (see the \f[B]RESET\f[R] section). Otherwise, if \f[B]x\f[R] is greater than \f[B]0\f[R], it returns \f[B]a(y/x)\f[R]. If \f[B]x\f[R] is less than \f[B]0\f[R], and \f[B]y\f[R] is greater than or equal to \f[B]0\f[R], it returns \f[B]a(y/x)+pi\f[R]. If \f[B]x\f[R] is less than \f[B]0\f[R], and \f[B]y\f[R] is less than \f[B]0\f[R], it returns \f[B]a(y/x)-pi\f[R]. If \f[B]x\f[R] is equal to \f[B]0\f[R], and \f[B]y\f[R] is greater than \f[B]0\f[R], it returns \f[B]pi/2\f[R]. If \f[B]x\f[R] is equal to \f[B]0\f[R], and \f[B]y\f[R] is less than \f[B]0\f[R], it returns \f[B]-pi/2\f[R]. .RS .PP This function is the same as the \f[B]atan2()\f[R] function in many programming languages. .PP This is an alias of \f[B]a2(y, x)\f[R]. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]r2d(x)\f[R] Converts \f[B]x\f[R] from radians to degrees and returns the result. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]d2r(x)\f[R] Converts \f[B]x\f[R] from degrees to radians and returns the result. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]frand(p)\f[R] Generates a pseudo-random integer between \f[B]0\f[R] (inclusive) and \f[B]1\f[R] (exclusive) with the number of decimal digits after the decimal point equal to the truncated absolute value of \f[B]p\f[R]. If \f[B]p\f[R] is not \f[B]0\f[R], then calling this function will change the value of \f[B]seed\f[R]. If \f[B]p\f[R] is \f[B]0\f[R], then \f[B]0\f[R] is returned, and \f[B]seed\f[R] is \f[I]not\f[R] changed. .TP \f[B]ifrand(i, p)\f[R] Generates a pseudo-random integer that is between \f[B]0\f[R] (inclusive) and the truncated absolute value of \f[B]i\f[R] (exclusive) with the number of decimal digits after the decimal point equal to the truncated absolute value of \f[B]p\f[R]. If the absolute value of \f[B]i\f[R] is greater than or equal to \f[B]2\f[R], and \f[B]p\f[R] is not \f[B]0\f[R], then calling this function will change the value of \f[B]seed\f[R]; otherwise, \f[B]0\f[R] is returned, and \f[B]seed\f[R] is not changed. .TP \f[B]i2rand(a, b)\f[R] Takes the truncated value of \f[B]a\f[R] and \f[B]b\f[R] and uses them as inclusive bounds to enerate a pseudo-random integer. If the difference of the truncated values of \f[B]a\f[R] and \f[B]b\f[R] is \f[B]0\f[R], then the truncated value is returned, and \f[B]seed\f[R] is \f[I]not\f[R] changed. Otherwise, this function will change the value of \f[B]seed\f[R]. .TP \f[B]srand(x)\f[R] Returns \f[B]x\f[R] with its sign flipped with probability \f[B]0.5\f[R]. In other words, it randomizes the sign of \f[B]x\f[R]. .TP \f[B]brand()\f[R] Returns a random boolean value (either \f[B]0\f[R] or \f[B]1\f[R]). .TP \f[B]band(a, b)\f[R] Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R] and calculates and returns the result of the bitwise \f[B]and\f[R] operation between them. .RS .PP If you want to use signed two\[cq]s complement arguments, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bor(a, b)\f[R] Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R] and calculates and returns the result of the bitwise \f[B]or\f[R] operation between them. .RS .PP If you want to use signed two\[cq]s complement arguments, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bxor(a, b)\f[R] Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R] and calculates and returns the result of the bitwise \f[B]xor\f[R] operation between them. .RS .PP If you want to use signed two\[cq]s complement arguments, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bshl(a, b)\f[R] Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R] and calculates and returns the result of \f[B]a\f[R] bit-shifted left by \f[B]b\f[R] places. .RS .PP If you want to use signed two\[cq]s complement arguments, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bshr(a, b)\f[R] Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R] and calculates and returns the truncated result of \f[B]a\f[R] bit-shifted right by \f[B]b\f[R] places. .RS .PP If you want to use signed two\[cq]s complement arguments, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bnotn(x, n)\f[R] Takes the truncated absolute value of \f[B]x\f[R] and does a bitwise not as though it has the same number of bytes as the truncated absolute value of \f[B]n\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bnot8(x)\f[R] Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as -though it has \f[B]8\f[R] binary digits (1 unsigned byte). +though it has \f[B]8\f[R] binary digits (\f[B]1\f[R] unsigned byte). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bnot16(x)\f[R] Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as -though it has \f[B]16\f[R] binary digits (2 unsigned bytes). +though it has \f[B]16\f[R] binary digits (\f[B]2\f[R] unsigned bytes). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bnot32(x)\f[R] Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as -though it has \f[B]32\f[R] binary digits (4 unsigned bytes). +though it has \f[B]32\f[R] binary digits (\f[B]4\f[R] unsigned bytes). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bnot64(x)\f[R] Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as -though it has \f[B]64\f[R] binary digits (8 unsigned bytes). +though it has \f[B]64\f[R] binary digits (\f[B]8\f[R] unsigned bytes). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bnot(x)\f[R] Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as though it has the minimum number of power of two unsigned bytes. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brevn(x, n)\f[R] Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as though it has the same number of 8-bit bytes as the truncated absolute value of \f[B]n\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brev8(x)\f[R] Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as -though it has 8 binary digits (1 unsigned byte). +though it has 8 binary digits (\f[B]1\f[R] unsigned byte). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brev16(x)\f[R] Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as -though it has 16 binary digits (2 unsigned bytes). +though it has 16 binary digits (\f[B]2\f[R] unsigned bytes). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brev32(x)\f[R] Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as -though it has 32 binary digits (4 unsigned bytes). +though it has 32 binary digits (\f[B]4\f[R] unsigned bytes). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brev64(x)\f[R] Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as -though it has 64 binary digits (8 unsigned bytes). +though it has 64 binary digits (\f[B]8\f[R] unsigned bytes). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brev(x)\f[R] Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as though it has the minimum number of power of two unsigned bytes. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]broln(x, p, n)\f[R] Does a left bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has the same number of unsigned 8-bit bytes as the truncated absolute value of \f[B]n\f[R], by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by the \f[B]2\f[R] to the power of the number of binary digits in \f[B]n\f[R] 8-bit bytes. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brol8(x, p)\f[R] Does a left bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has \f[B]8\f[R] binary digits (\f[B]1\f[R] unsigned byte), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]8\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brol16(x, p)\f[R] Does a left bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has \f[B]16\f[R] binary digits (\f[B]2\f[R] unsigned bytes), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]16\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brol32(x, p)\f[R] Does a left bitwise rotatation of the truncated absolute value of -\f[B]x\f[R], as though it has \f[B]32\f[R] binary digits (\f[B]2\f[R] +\f[B]x\f[R], as though it has \f[B]32\f[R] binary digits (\f[B]4\f[R] unsigned bytes), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]32\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brol64(x, p)\f[R] Does a left bitwise rotatation of the truncated absolute value of -\f[B]x\f[R], as though it has \f[B]64\f[R] binary digits (\f[B]2\f[R] +\f[B]x\f[R], as though it has \f[B]64\f[R] binary digits (\f[B]8\f[R] unsigned bytes), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]64\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brol(x, p)\f[R] Does a left bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has the minimum number of power of two unsigned 8-bit bytes, by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by 2 to the power of the number of binary digits in the minimum number of 8-bit bytes. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brorn(x, p, n)\f[R] Does a right bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has the same number of unsigned 8-bit bytes as the truncated absolute value of \f[B]n\f[R], by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by the \f[B]2\f[R] to the power of the number of binary digits in \f[B]n\f[R] 8-bit bytes. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bror8(x, p)\f[R] Does a right bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has \f[B]8\f[R] binary digits (\f[B]1\f[R] unsigned byte), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]8\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bror16(x, p)\f[R] Does a right bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has \f[B]16\f[R] binary digits (\f[B]2\f[R] unsigned bytes), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]16\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bror32(x, p)\f[R] Does a right bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has \f[B]32\f[R] binary digits (\f[B]2\f[R] unsigned bytes), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]32\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bror64(x, p)\f[R] Does a right bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has \f[B]64\f[R] binary digits (\f[B]2\f[R] unsigned bytes), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]64\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bror(x, p)\f[R] Does a right bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has the minimum number of power of two unsigned 8-bit bytes, by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by 2 to the power of the number of binary digits in the minimum number of 8-bit bytes. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bmodn(x, n)\f[R] Returns the modulus of the truncated absolute value of \f[B]x\f[R] by \f[B]2\f[R] to the power of the multiplication of the truncated absolute value of \f[B]n\f[R] and \f[B]8\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bmod8(x, n)\f[R] Returns the modulus of the truncated absolute value of \f[B]x\f[R] by \f[B]2\f[R] to the power of \f[B]8\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bmod16(x, n)\f[R] Returns the modulus of the truncated absolute value of \f[B]x\f[R] by \f[B]2\f[R] to the power of \f[B]16\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bmod32(x, n)\f[R] Returns the modulus of the truncated absolute value of \f[B]x\f[R] by \f[B]2\f[R] to the power of \f[B]32\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bmod64(x, n)\f[R] Returns the modulus of the truncated absolute value of \f[B]x\f[R] by \f[B]2\f[R] to the power of \f[B]64\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bunrev(t)\f[R] Assumes \f[B]t\f[R] is a bitwise-reversed number with an extra set bit one place more significant than the real most significant bit (which was the least significant bit in the original number). This number is reversed and returned without the extra set bit. .RS .PP This function is used to implement other bitwise functions; it is not meant to be used by users, but it can be. .RE .TP \f[B]plz(x)\f[R] If \f[B]x\f[R] is not equal to \f[B]0\f[R] and greater that \f[B]-1\f[R] and less than \f[B]1\f[R], it is printed with a leading zero, regardless of the use of the \f[B]-z\f[R] option (see the \f[B]OPTIONS\f[R] section) and without a trailing newline. .RS .PP Otherwise, \f[B]x\f[R] is printed normally, without a trailing newline. .RE .TP \f[B]plznl(x)\f[R] If \f[B]x\f[R] is not equal to \f[B]0\f[R] and greater that \f[B]-1\f[R] and less than \f[B]1\f[R], it is printed with a leading zero, regardless of the use of the \f[B]-z\f[R] option (see the \f[B]OPTIONS\f[R] section) and with a trailing newline. .RS .PP Otherwise, \f[B]x\f[R] is printed normally, with a trailing newline. .RE .TP \f[B]pnlz(x)\f[R] If \f[B]x\f[R] is not equal to \f[B]0\f[R] and greater that \f[B]-1\f[R] and less than \f[B]1\f[R], it is printed without a leading zero, regardless of the use of the \f[B]-z\f[R] option (see the \f[B]OPTIONS\f[R] section) and without a trailing newline. .RS .PP Otherwise, \f[B]x\f[R] is printed normally, without a trailing newline. .RE .TP \f[B]pnlznl(x)\f[R] If \f[B]x\f[R] is not equal to \f[B]0\f[R] and greater that \f[B]-1\f[R] and less than \f[B]1\f[R], it is printed without a leading zero, regardless of the use of the \f[B]-z\f[R] option (see the \f[B]OPTIONS\f[R] section) and with a trailing newline. .RS .PP Otherwise, \f[B]x\f[R] is printed normally, with a trailing newline. .RE .TP \f[B]ubytes(x)\f[R] Returns the numbers of unsigned integer bytes required to hold the truncated absolute value of \f[B]x\f[R]. .TP \f[B]sbytes(x)\f[R] Returns the numbers of signed, two\[cq]s-complement integer bytes required to hold the truncated value of \f[B]x\f[R]. .TP \f[B]s2u(x)\f[R] Returns \f[B]x\f[R] if it is non-negative. If it \f[I]is\f[R] negative, then it calculates what \f[B]x\f[R] would be as a 2\[cq]s-complement signed integer and returns the non-negative integer that would have the same representation in binary. .TP \f[B]s2un(x,n)\f[R] Returns \f[B]x\f[R] if it is non-negative. If it \f[I]is\f[R] negative, then it calculates what \f[B]x\f[R] would be as a 2\[cq]s-complement signed integer with \f[B]n\f[R] bytes and returns the non-negative integer that would have the same representation in binary. If \f[B]x\f[R] cannot fit into \f[B]n\f[R] 2\[cq]s-complement signed bytes, it is truncated to fit. .TP \f[B]hex(x)\f[R] Outputs the hexadecimal (base \f[B]16\f[R]) representation of \f[B]x\f[R]. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]binary(x)\f[R] Outputs the binary (base \f[B]2\f[R]) representation of \f[B]x\f[R]. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]output(x, b)\f[R] Outputs the base \f[B]b\f[R] representation of \f[B]x\f[R]. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]uint(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as an unsigned integer in as few power of two bytes as possible. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer or is negative, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]int(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as a signed, two\[cq]s-complement integer in as few power of two bytes as possible. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]uintn(x, n)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as an unsigned integer in \f[B]n\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer, is negative, or cannot fit into \f[B]n\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]intn(x, n)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as a signed, two\[cq]s-complement integer in \f[B]n\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer or cannot fit into \f[B]n\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]uint8(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as an unsigned integer in \f[B]1\f[R] byte. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer, is negative, or cannot fit into \f[B]1\f[R] byte, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]int8(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as a signed, two\[cq]s-complement integer in \f[B]1\f[R] byte. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer or cannot fit into \f[B]1\f[R] byte, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]uint16(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as an unsigned integer in \f[B]2\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer, is negative, or cannot fit into \f[B]2\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]int16(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as a signed, two\[cq]s-complement integer in \f[B]2\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer or cannot fit into \f[B]2\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]uint32(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as an unsigned integer in \f[B]4\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer, is negative, or cannot fit into \f[B]4\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]int32(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as a signed, two\[cq]s-complement integer in \f[B]4\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer or cannot fit into \f[B]4\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]uint64(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as an unsigned integer in \f[B]8\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer, is negative, or cannot fit into \f[B]8\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]int64(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as a signed, two\[cq]s-complement integer in \f[B]8\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer or cannot fit into \f[B]8\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]hex_uint(x, n)\f[R] Outputs the representation of the truncated absolute value of \f[B]x\f[R] as an unsigned integer in hexadecimal using \f[B]n\f[R] bytes. Not all of the value will be output if \f[B]n\f[R] is too small. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]binary_uint(x, n)\f[R] Outputs the representation of the truncated absolute value of \f[B]x\f[R] as an unsigned integer in binary using \f[B]n\f[R] bytes. Not all of the value will be output if \f[B]n\f[R] is too small. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]output_uint(x, n)\f[R] Outputs the representation of the truncated absolute value of \f[B]x\f[R] as an unsigned integer in the current \f[B]obase\f[R] (see the \f[B]SYNTAX\f[R] section) using \f[B]n\f[R] bytes. Not all of the value will be output if \f[B]n\f[R] is too small. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]output_byte(x, i)\f[R] Outputs byte \f[B]i\f[R] of the truncated absolute value of \f[B]x\f[R], where \f[B]0\f[R] is the least significant byte and \f[B]number_of_bytes - 1\f[R] is the most significant byte. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .SS Transcendental Functions -.PP All transcendental functions can return slightly inaccurate results, up to 1 ULP (https://en.wikipedia.org/wiki/Unit_in_the_last_place). This is unavoidable, and the article at https://people.eecs.berkeley.edu/\[ti]wkahan/LOG10HAF.TXT explains why it is impossible and unnecessary to calculate exact results for the transcendental functions. .PP Because of the possible inaccuracy, I recommend that users call those functions with the precision (\f[B]scale\f[R]) set to at least 1 higher than is necessary. If exact results are \f[I]absolutely\f[R] required, users can double the precision (\f[B]scale\f[R]) and then truncate. .PP The transcendental functions in the standard math library are: .IP \[bu] 2 \f[B]s(x)\f[R] .IP \[bu] 2 \f[B]c(x)\f[R] .IP \[bu] 2 \f[B]a(x)\f[R] .IP \[bu] 2 \f[B]l(x)\f[R] .IP \[bu] 2 \f[B]e(x)\f[R] .IP \[bu] 2 \f[B]j(x, n)\f[R] .PP The transcendental functions in the extended math library are: .IP \[bu] 2 \f[B]l2(x)\f[R] .IP \[bu] 2 \f[B]l10(x)\f[R] .IP \[bu] 2 \f[B]log(x, b)\f[R] .IP \[bu] 2 \f[B]pi(p)\f[R] .IP \[bu] 2 \f[B]t(x)\f[R] .IP \[bu] 2 \f[B]a2(y, x)\f[R] .IP \[bu] 2 \f[B]sin(x)\f[R] .IP \[bu] 2 \f[B]cos(x)\f[R] .IP \[bu] 2 \f[B]tan(x)\f[R] .IP \[bu] 2 \f[B]atan(x)\f[R] .IP \[bu] 2 \f[B]atan2(y, x)\f[R] .IP \[bu] 2 \f[B]r2d(x)\f[R] .IP \[bu] 2 \f[B]d2r(x)\f[R] .SH RESET -.PP When bc(1) encounters an error or a signal that it has a non-default handler for, it resets. This means that several things happen. .PP First, any functions that are executing are stopped and popped off the stack. The behavior is not unlike that of exceptions in programming languages. Then the execution point is set so that any code waiting to execute (after all functions returned) is skipped. .PP Thus, when bc(1) resets, it skips any remaining code waiting to be executed. Then, if it is interactive mode, and the error was not a fatal error (see the \f[B]EXIT STATUS\f[R] section), it asks for more input; otherwise, it exits with the appropriate return code. .PP Note that this reset behavior is different from the GNU bc(1), which attempts to start executing the statement right after the one that caused an error. .SH PERFORMANCE -.PP Most bc(1) implementations use \f[B]char\f[R] types to calculate the value of \f[B]1\f[R] decimal digit at a time, but that can be slow. This bc(1) does something different. .PP It uses large integers to calculate more than \f[B]1\f[R] decimal digit at a time. If built in a environment where \f[B]BC_LONG_BIT\f[R] (see the \f[B]LIMITS\f[R] section) is \f[B]64\f[R], then each integer has \f[B]9\f[R] decimal digits. If built in an environment where \f[B]BC_LONG_BIT\f[R] is \f[B]32\f[R] then each integer has \f[B]4\f[R] decimal digits. This value (the number of decimal digits per large integer) is called \f[B]BC_BASE_DIGS\f[R]. .PP The actual values of \f[B]BC_LONG_BIT\f[R] and \f[B]BC_BASE_DIGS\f[R] can be queried with the \f[B]limits\f[R] statement. .PP In addition, this bc(1) uses an even larger integer for overflow checking. This integer type depends on the value of \f[B]BC_LONG_BIT\f[R], but is always at least twice as large as the integer type used to store digits. .SH LIMITS -.PP The following are the limits on bc(1): .TP \f[B]BC_LONG_BIT\f[R] The number of bits in the \f[B]long\f[R] type in the environment where bc(1) was built. This determines how many decimal digits can be stored in a single large integer (see the \f[B]PERFORMANCE\f[R] section). .TP \f[B]BC_BASE_DIGS\f[R] The number of decimal digits per large integer (see the \f[B]PERFORMANCE\f[R] section). Depends on \f[B]BC_LONG_BIT\f[R]. .TP \f[B]BC_BASE_POW\f[R] The max decimal number that each large integer can store (see \f[B]BC_BASE_DIGS\f[R]) plus \f[B]1\f[R]. Depends on \f[B]BC_BASE_DIGS\f[R]. .TP \f[B]BC_OVERFLOW_MAX\f[R] The max number that the overflow type (see the \f[B]PERFORMANCE\f[R] section) can hold. Depends on \f[B]BC_LONG_BIT\f[R]. .TP \f[B]BC_BASE_MAX\f[R] The maximum output base. Set at \f[B]BC_BASE_POW\f[R]. .TP \f[B]BC_DIM_MAX\f[R] The maximum size of arrays. Set at \f[B]SIZE_MAX-1\f[R]. .TP \f[B]BC_SCALE_MAX\f[R] The maximum \f[B]scale\f[R]. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_STRING_MAX\f[R] The maximum length of strings. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_NAME_MAX\f[R] The maximum length of identifiers. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_NUM_MAX\f[R] The maximum length of a number (in decimal digits), which includes digits after the decimal point. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_RAND_MAX\f[R] The maximum integer (inclusive) returned by the \f[B]rand()\f[R] operand. Set at \f[B]2\[ha]BC_LONG_BIT-1\f[R]. .TP Exponent The maximum allowable exponent (positive or negative). Set at \f[B]BC_OVERFLOW_MAX\f[R]. .TP Number of vars The maximum number of vars/arrays. Set at \f[B]SIZE_MAX-1\f[R]. .PP The actual values can be queried with the \f[B]limits\f[R] statement. .PP These limits are meant to be effectively non-existent; the limits are so large (at least on 64-bit machines) that there should not be any point at which they become a problem. In fact, memory should be exhausted before these limits should be hit. .SH ENVIRONMENT VARIABLES -.PP As \f[B]non-portable extensions\f[R], bc(1) recognizes the following environment variables: .TP \f[B]POSIXLY_CORRECT\f[R] If this variable exists (no matter the contents), bc(1) behaves as if the \f[B]-s\f[R] option was given. .TP \f[B]BC_ENV_ARGS\f[R] This is another way to give command-line arguments to bc(1). They should be in the same format as all other command-line arguments. These are always processed first, so any files given in \f[B]BC_ENV_ARGS\f[R] will be processed before arguments and files given on the command-line. This gives the user the ability to set up \[lq]standard\[rq] options and files to be used at every invocation. The most useful thing for such files to contain would be useful functions that the user might want every time bc(1) runs. .RS .PP The code that parses \f[B]BC_ENV_ARGS\f[R] will correctly handle quoted arguments, but it does not understand escape sequences. For example, the string \f[B]\[lq]/home/gavin/some bc file.bc\[rq]\f[R] will be correctly parsed, but the string \f[B]\[lq]/home/gavin/some \[dq]bc\[dq] file.bc\[rq]\f[R] will include the backslashes. .PP The quote parsing will handle either kind of quotes, \f[B]\[cq]\f[R] or \f[B]\[lq]\f[R]. Thus, if you have a file with any number of single quotes in the name, you can use double quotes as the outside quotes, as in \f[B]\[lq]some `bc' file.bc\[rq]\f[R], and vice versa if you have a file with double quotes. However, handling a file with both kinds of quotes in \f[B]BC_ENV_ARGS\f[R] is not supported due to the complexity of the parsing, though such files are still supported on the command-line where the parsing is done by the shell. .RE .TP \f[B]BC_LINE_LENGTH\f[R] If this environment variable exists and contains an integer that is greater than \f[B]1\f[R] and is less than \f[B]UINT16_MAX\f[R] (\f[B]2\[ha]16-1\f[R]), bc(1) will output lines to that length, including the backslash (\f[B]\[rs]\f[R]). The default line length is \f[B]70\f[R]. .RS .PP The special value of \f[B]0\f[R] will disable line length checking and print numbers without regard to line length and without backslashes and newlines. .RE .TP \f[B]BC_BANNER\f[R] If this environment variable exists and contains an integer, then a non-zero value activates the copyright banner when bc(1) is in interactive mode, while zero deactivates it. .RS .PP If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), then this environment variable has no effect because bc(1) does not print the banner when not in interactive mode. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_SIGINT_RESET\f[R] If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), then this environment variable has no effect because bc(1) exits on \f[B]SIGINT\f[R] when not in interactive mode. .RS .PP However, when bc(1) is in interactive mode, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) reset on \f[B]SIGINT\f[R], rather than exit, and zero makes bc(1) exit. If this environment variable exists and is \f[I]not\f[R] an integer, then bc(1) will exit on \f[B]SIGINT\f[R]. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_TTY_MODE\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, then a non-zero value makes bc(1) use TTY mode, and zero makes bc(1) not use TTY mode. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_PROMPT\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) use a prompt, and zero or a non-integer makes bc(1) not use a prompt. If this environment variable does not exist and \f[B]BC_TTY_MODE\f[R] does, then the value of the \f[B]BC_TTY_MODE\f[R] environment variable is used. .PP This environment variable and the \f[B]BC_TTY_MODE\f[R] environment variable override the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_EXPR_EXIT\f[R] If any expressions or expression files are given on the command-line with \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R], then if this environment variable exists and contains an integer, a non-zero value makes bc(1) exit after executing the expressions and expression files, and a zero value makes bc(1) not exit. .RS .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_DIGIT_CLAMP\f[R] When parsing numbers and if this environment variable exists and contains an integer, a non-zero value makes bc(1) clamp digits that are greater than or equal to the current \f[B]ibase\f[R] so that all such digits are considered equal to the \f[B]ibase\f[R] minus 1, and a zero value disables such clamping so that those digits are always equal to their value, which is multiplied by the power of the \f[B]ibase\f[R]. .RS .PP This never applies to single-digit numbers, as per the standard (see the \f[B]STANDARDS\f[R] section). .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .SH EXIT STATUS -.PP bc(1) returns the following exit statuses: .TP \f[B]0\f[R] No error. .TP \f[B]1\f[R] A math error occurred. This follows standard practice of using \f[B]1\f[R] for expected errors, since math errors will happen in the process of normal execution. .RS .PP Math errors include divide by \f[B]0\f[R], taking the square root of a negative number, using a negative number as a bound for the pseudo-random number generator, attempting to convert a negative number to a hardware integer, overflow when converting a number to a hardware integer, overflow when calculating the size of a number, and attempting to use a non-integer where an integer is required. .PP Converting to a hardware integer happens for the second operand of the power (\f[B]\[ha]\f[R]), places (\f[B]\[at]\f[R]), left shift (\f[B]<<\f[R]), and right shift (\f[B]>>\f[R]) operators and their corresponding assignment operators. .RE .TP \f[B]2\f[R] A parse error occurred. .RS .PP Parse errors include unexpected \f[B]EOF\f[R], using an invalid character, failing to find the end of a string or comment, using a token where it is invalid, giving an invalid expression, giving an invalid print statement, giving an invalid function definition, attempting to assign to an expression that is not a named expression (see the \f[I]Named Expressions\f[R] subsection of the \f[B]SYNTAX\f[R] section), giving an invalid \f[B]auto\f[R] list, having a duplicate \f[B]auto\f[R]/function parameter, failing to find the end of a code block, attempting to return a value from a \f[B]void\f[R] function, attempting to use a variable as a reference, and using any extensions when the option \f[B]-s\f[R] or any equivalents were given. .RE .TP \f[B]3\f[R] A runtime error occurred. .RS .PP Runtime errors include assigning an invalid number to any global (\f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R]), giving a bad expression to a \f[B]read()\f[R] call, calling \f[B]read()\f[R] inside of a \f[B]read()\f[R] call, type errors, passing the wrong number of arguments to functions, attempting to call an undefined function, and attempting to use a \f[B]void\f[R] function call as a value in an expression. .RE .TP \f[B]4\f[R] A fatal error occurred. .RS .PP Fatal errors include memory allocation errors, I/O errors, failing to open files, attempting to use files that do not have only ASCII characters (bc(1) only accepts ASCII characters), attempting to open a directory as a file, and giving invalid command-line options. .RE .PP The exit status \f[B]4\f[R] is special; when a fatal error occurs, bc(1) always exits and returns \f[B]4\f[R], no matter what mode bc(1) is in. .PP The other statuses will only be returned when bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), since bc(1) resets its state (see the \f[B]RESET\f[R] section) and accepts more input when one of those errors occurs in interactive mode. This is also the case when interactive mode is forced by the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .PP These exit statuses allow bc(1) to be used in shell scripting with error checking, and its normal behavior can be forced by using the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .SH INTERACTIVE MODE -.PP Per the standard (see the \f[B]STANDARDS\f[R] section), bc(1) has an interactive mode and a non-interactive mode. Interactive mode is turned on automatically when both \f[B]stdin\f[R] and \f[B]stdout\f[R] are hooked to a terminal, but the \f[B]-i\f[R] flag and \f[B]--interactive\f[R] option can turn it on in other situations. .PP In interactive mode, bc(1) attempts to recover from errors (see the \f[B]RESET\f[R] section), and in normal execution, flushes \f[B]stdout\f[R] as soon as execution is done for the current input. bc(1) may also reset on \f[B]SIGINT\f[R] instead of exit, depending on the contents of, or default for, the \f[B]BC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .SH TTY MODE -.PP If \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY, then \[lq]TTY mode\[rq] is considered to be available, and thus, bc(1) can turn on TTY mode, subject to some settings. .PP If there is the environment variable \f[B]BC_TTY_MODE\f[R] in the environment (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then if that environment variable contains a non-zero integer, bc(1) will turn on TTY mode when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY. If the \f[B]BC_TTY_MODE\f[R] environment variable exists but is \f[I]not\f[R] a non-zero integer, then bc(1) will not turn TTY mode on. .PP If the environment variable \f[B]BC_TTY_MODE\f[R] does \f[I]not\f[R] exist, the default setting is used. The default setting can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP TTY mode is different from interactive mode because interactive mode is required in the bc(1) standard (see the \f[B]STANDARDS\f[R] section), and interactive mode requires only \f[B]stdin\f[R] and \f[B]stdout\f[R] to be connected to a terminal. .SS Prompt -.PP If TTY mode is available, then a prompt can be enabled. Like TTY mode itself, it can be turned on or off with an environment variable: \f[B]BC_PROMPT\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP If the environment variable \f[B]BC_PROMPT\f[R] exists and is a non-zero integer, then the prompt is turned on when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options were not used. The read prompt will be turned on under the same conditions, except that the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options must also not be used. .PP However, if \f[B]BC_PROMPT\f[R] does not exist, the prompt can be enabled or disabled with the \f[B]BC_TTY_MODE\f[R] environment variable, the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options, and the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options. See the \f[B]ENVIRONMENT VARIABLES\f[R] and \f[B]OPTIONS\f[R] sections for more details. .SH SIGNAL HANDLING -.PP Sending a \f[B]SIGINT\f[R] will cause bc(1) to do one of two things. .PP If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), or the \f[B]BC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or its default, is either not an integer or it is zero, bc(1) will exit. .PP However, if bc(1) is in interactive mode, and the \f[B]BC_SIGINT_RESET\f[R] or its default is an integer and non-zero, then bc(1) will stop executing the current input and reset (see the \f[B]RESET\f[R] section) upon receiving a \f[B]SIGINT\f[R]. .PP Note that \[lq]current input\[rq] can mean one of two things. If bc(1) is processing input from \f[B]stdin\f[R] in interactive mode, it will ask for more input. If bc(1) is processing input from a file in interactive mode, it will stop processing the file and start processing the next file, if one exists, or ask for input from \f[B]stdin\f[R] if no other file exists. .PP This means that if a \f[B]SIGINT\f[R] is sent to bc(1) as it is executing a file, it can seem as though bc(1) did not respond to the signal since it will immediately start executing the next file. This is by design; most files that users execute when interacting with bc(1) have function definitions, which are quick to parse. If a file takes a long time to execute, there may be a bug in that file. The rest of the files could still be executed without problem, allowing the user to continue. .PP \f[B]SIGTERM\f[R] and \f[B]SIGQUIT\f[R] cause bc(1) to clean up and exit, and it uses the default handler for all other signals. .SH LOCALES -.PP This bc(1) ships with support for adding error messages for different locales and thus, supports \f[B]LC_MESSAGES\f[R]. .SH SEE ALSO -.PP dc(1) .SH STANDARDS -.PP bc(1) is compliant with the IEEE Std 1003.1-2017 (\[lq]POSIX.1-2017\[rq]) specification at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html . The flags \f[B]-efghiqsvVw\f[R], all long options, and the extensions noted above are extensions to that specification. .PP In addition, the behavior of the \f[B]quit\f[R] implements an interpretation of that specification that is different from all known implementations. For more information see the \f[B]Statements\f[R] subsection of the \f[B]SYNTAX\f[R] section. .PP Note that the specification explicitly says that bc(1) only accepts numbers that use a period (\f[B].\f[R]) as a radix point, regardless of the value of \f[B]LC_NUMERIC\f[R]. .PP This bc(1) supports error messages for different locales, and thus, it supports \f[B]LC_MESSAGES\f[R]. .SH BUGS -.PP Before version \f[B]6.1.0\f[R], this bc(1) had incorrect behavior for the \f[B]quit\f[R] statement. .PP No other bugs are known. Report bugs at https://git.gavinhoward.com/gavin/bc . .SH AUTHORS -.PP -Gavin D. -Howard and contributors. +Gavin D. Howard \c +.MT gavin@gavinhoward.com +.ME \c +\ and contributors. diff --git a/contrib/bc/manuals/bc/H.1.md b/contrib/bc/manuals/bc/H.1.md index 24cae156d4c2..970fc5e584a4 100644 --- a/contrib/bc/manuals/bc/H.1.md +++ b/contrib/bc/manuals/bc/H.1.md @@ -1,2499 +1,2499 @@ # NAME bc - arbitrary-precision decimal arithmetic language and calculator # SYNOPSIS **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*] # DESCRIPTION bc(1) is an interactive processor for a language first standardized in 1991 by POSIX. (See the **STANDARDS** section.) The language provides unlimited precision decimal arithmetic and is somewhat C-like, but there are differences. Such differences will be noted in this document. After parsing and handling options, this bc(1) reads any files given on the command line and executes them before reading from **stdin**. This bc(1) is a drop-in replacement for *any* bc(1), including (and especially) the GNU bc(1). It also has many extensions and extra features beyond other implementations. **Note**: If running this bc(1) on *any* script meant for another bc(1) gives a parse error, it is probably because a word this bc(1) reserves as a keyword is used as the name of a function, variable, or array. To fix that, use the command-line option **-r** *keyword*, where *keyword* is the keyword that is used as a name in the script. For more information, see the **OPTIONS** section. If parsing scripts meant for other bc(1) implementations still does not work, that is a bug and should be reported. See the **BUGS** section. # OPTIONS The following are the options that bc(1) accepts. **-C**, **-\-no-digit-clamp** : Disables clamping of digits greater than or equal to the current **ibase** when parsing numbers. This means that the value added to a number from a digit is always that digit's value multiplied by the value of ibase raised to the power of the digit's position, which starts from 0 at the least significant digit. If this and/or the **-c** or **-\-digit-clamp** options are given multiple times, the last one given is used. This option overrides the **BC_DIGIT_CLAMP** environment variable (see the **ENVIRONMENT VARIABLES** section) and the default, which can be queried with the **-h** or **-\-help** options. This is a **non-portable extension**. **-c**, **-\-digit-clamp** : Enables clamping of digits greater than or equal to the current **ibase** when parsing numbers. This means that digits that the value added to a number from a digit that is greater than or equal to the ibase is the value of ibase minus 1 all multiplied by the value of ibase raised to the power of the digit's position, which starts from 0 at the least significant digit. If this and/or the **-C** or **-\-no-digit-clamp** options are given multiple times, the last one given is used. This option overrides the **BC_DIGIT_CLAMP** environment variable (see the **ENVIRONMENT VARIABLES** section) and the default, which can be queried with the **-h** or **-\-help** options. This is a **non-portable extension**. **-E** *seed*, **-\-seed**=*seed* : Sets the builtin variable **seed** to the value *seed* assuming that *seed* is in base 10. It is a fatal error if *seed* is not a valid number. If multiple instances of this option are given, the last is used. This is a **non-portable extension**. **-e** *expr*, **-\-expression**=*expr* : Evaluates *expr*. If multiple expressions are given, they are evaluated in order. If files are given as well (see the **-f** and **-\-file** options), the expressions and files are evaluated in the order given. This means that if a file is given before an expression, the file is read in and evaluated first. If this option is given on the command-line (i.e., not in **BC_ENV_ARGS**, see the **ENVIRONMENT VARIABLES** section), then after processing all expressions and files, bc(1) will exit, unless **-** (**stdin**) was given as an argument at least once to **-f** or **-\-file**, whether on the command-line or in **BC_ENV_ARGS**. However, if any other **-e**, **-\-expression**, **-f**, or **-\-file** arguments are given after **-f-** or equivalent is given, bc(1) will give a fatal error and exit. This is a **non-portable extension**. **-f** *file*, **-\-file**=*file* : Reads in *file* and evaluates it, line by line, as though it were read through **stdin**. If expressions are also given (see the **-e** and **-\-expression** options), the expressions are evaluated in the order given. If this option is given on the command-line (i.e., not in **BC_ENV_ARGS**, see the **ENVIRONMENT VARIABLES** section), then after processing all expressions and files, bc(1) will exit, unless **-** (**stdin**) was given as an argument at least once to **-f** or **-\-file**. However, if any other **-e**, **-\-expression**, **-f**, or **-\-file** arguments are given after **-f-** or equivalent is given, bc(1) will give a fatal error and exit. This is a **non-portable extension**. **-g**, **-\-global-stacks** : Turns the globals **ibase**, **obase**, **scale**, and **seed** into stacks. This has the effect that a copy of the current value of all four are pushed onto a stack for every function call, as well as popped when every function returns. This means that functions can assign to any and all of those globals without worrying that the change will affect other functions. Thus, a hypothetical function named **output(x,b)** that simply printed **x** in base **b** could be written like this: define void output(x, b) { obase=b x } instead of like this: define void output(x, b) { auto c c=obase obase=b x obase=c } This makes writing functions much easier. (**Note**: the function **output(x,b)** exists in the extended math library. See the **LIBRARY** section.) However, since using this flag means that functions cannot set **ibase**, **obase**, **scale**, or **seed** globally, functions that are made to do so cannot work anymore. There are two possible use cases for that, and each has a solution. First, if a function is called on startup to turn bc(1) into a number converter, it is possible to replace that capability with various shell aliases. Examples: alias d2o="bc -e ibase=A -e obase=8" alias h2b="bc -e ibase=G -e obase=2" Second, if the purpose of a function is to set **ibase**, **obase**, **scale**, or **seed** globally for any other purpose, it could be split into one to four functions (based on how many globals it sets) and each of those functions could return the desired value for a global. For functions that set **seed**, the value assigned to **seed** is not propagated to parent functions. This means that the sequence of pseudo-random numbers that they see will not be the same sequence of pseudo-random numbers that any parent sees. This is only the case once **seed** has been set. If a function desires to not affect the sequence of pseudo-random numbers of its parents, but wants to use the same **seed**, it can use the following line: seed = seed If the behavior of this option is desired for every run of bc(1), then users could make sure to define **BC_ENV_ARGS** and include this option (see the **ENVIRONMENT VARIABLES** section for more details). If **-s**, **-w**, or any equivalents are used, this option is ignored. This is a **non-portable extension**. **-h**, **-\-help** : Prints a usage message and exits. **-I** *ibase*, **-\-ibase**=*ibase* : Sets the builtin variable **ibase** to the value *ibase* assuming that *ibase* is in base 10. It is a fatal error if *ibase* is not a valid number. If multiple instances of this option are given, the last is used. This is a **non-portable extension**. **-i**, **-\-interactive** : Forces interactive mode. (See the **INTERACTIVE MODE** section.) This is a **non-portable extension**. **-L**, **-\-no-line-length** : Disables line length checking and prints numbers without backslashes and newlines. In other words, this option sets **BC_LINE_LENGTH** to **0** (see the **ENVIRONMENT VARIABLES** section). This is a **non-portable extension**. **-l**, **-\-mathlib** : Sets **scale** (see the **SYNTAX** section) to **20** and loads the included math library and the extended math library before running any code, including any expressions or files specified on the command line. To learn what is in the libraries, see the **LIBRARY** section. **-O** *obase*, **-\-obase**=*obase* : Sets the builtin variable **obase** to the value *obase* assuming that *obase* is in base 10. It is a fatal error if *obase* is not a valid number. If multiple instances of this option are given, the last is used. This is a **non-portable extension**. **-P**, **-\-no-prompt** : Disables the prompt in TTY mode. (The prompt is only enabled in TTY mode. See the **TTY MODE** section.) This is mostly for those users that do not want a prompt or are not used to having them in bc(1). Most of those users would want to put this option in **BC_ENV_ARGS** (see the **ENVIRONMENT VARIABLES** section). These options override the **BC_PROMPT** and **BC_TTY_MODE** environment variables (see the **ENVIRONMENT VARIABLES** section). This is a **non-portable extension**. **-q**, **-\-quiet** : This option is for compatibility with the GNU bc(1) (https://www.gnu.org/software/bc/); it is a no-op. Without this option, GNU bc(1) prints a copyright header. This bc(1) only prints the copyright header if one or more of the **-v**, **-V**, or **-\-version** options are given unless the **BC_BANNER** environment variable is set and contains a non-zero integer or if this bc(1) was built with the header displayed by default. If *any* of that is the case, then this option *does* prevent bc(1) from printing the header. This is a **non-portable extension**. **-R**, **-\-no-read-prompt** : Disables the read prompt in TTY mode. (The read prompt is only enabled in TTY mode. See the **TTY MODE** section.) This is mostly for those users that do not want a read prompt or are not used to having them in bc(1). Most of those users would want to put this option in **BC_ENV_ARGS** (see the **ENVIRONMENT VARIABLES** section). This option is also useful in hash bang lines of bc(1) scripts that prompt for user input. This option does not disable the regular prompt because the read prompt is only used when the **read()** built-in function is called. These options *do* override the **BC_PROMPT** and **BC_TTY_MODE** environment variables (see the **ENVIRONMENT VARIABLES** section), but only for the read prompt. This is a **non-portable extension**. **-r** *keyword*, **-\-redefine**=*keyword* : Redefines *keyword* in order to allow it to be used as a function, variable, or array name. This is useful when this bc(1) gives parse errors when parsing scripts meant for other bc(1) implementations. The keywords this bc(1) allows to be redefined are: * **abs** * **asciify** * **continue** * **divmod** * **else** * **halt** * **irand** * **last** * **limits** * **maxibase** * **maxobase** * **maxrand** * **maxscale** * **modexp** * **print** * **rand** * **read** * **seed** * **stream** If any of those keywords are used as a function, variable, or array name in a script, use this option with the keyword as the argument. If multiple are used, use this option for all of them; it can be used multiple times. Keywords are *not* redefined when parsing the builtin math library (see the **LIBRARY** section). It is a fatal error to redefine keywords mandated by the POSIX standard (see the **STANDARDS** section). It is a fatal error to attempt to redefine words that this bc(1) does not reserve as keywords. **-S** *scale*, **-\-scale**=*scale* : Sets the builtin variable **scale** to the value *scale* assuming that *scale* is in base 10. It is a fatal error if *scale* is not a valid number. If multiple instances of this option are given, the last is used. This is a **non-portable extension**. **-s**, **-\-standard** : Process exactly the language defined by the standard (see the **STANDARDS** section) and error if any extensions are used. This is a **non-portable extension**. **-v**, **-V**, **-\-version** : Print the version information (copyright header) and exits. This is a **non-portable extension**. **-w**, **-\-warn** : Like **-s** and **-\-standard**, except that warnings (and not errors) are printed for non-standard extensions and execution continues normally. This is a **non-portable extension**. **-z**, **-\-leading-zeroes** : Makes bc(1) print all numbers greater than **-1** and less than **1**, and not equal to **0**, with a leading zero. This can be set for individual numbers with the **plz(x)**, **plznl(x)**, **pnlz(x)**, and **pnlznl(x)** functions in the extended math library (see the **LIBRARY** section). This is a **non-portable extension**. All long options are **non-portable extensions**. # STDIN If no files or expressions are given by the **-f**, **-\-file**, **-e**, or **-\-expression** options, then bc(1) reads from **stdin**. However, there are a few caveats to this. First, **stdin** is evaluated a line at a time. The only exception to this is if the parse cannot complete. That means that starting a string without ending it or starting a function, **if** statement, or loop without ending it will also cause bc(1) to not execute. Second, after an **if** statement, bc(1) doesn't know if an **else** statement will follow, so it will not execute until it knows there will not be an **else** statement. # STDOUT Any non-error output is written to **stdout**. In addition, if history (see the **HISTORY** section) and the prompt (see the **TTY MODE** section) are enabled, both are output to **stdout**. **Note**: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the **EXIT STATUS** section) if it cannot write to **stdout**, so if **stdout** is closed, as in **bc >&-**, it will quit with an error. This is done so that bc(1) can report problems when **stdout** is redirected to a file. If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect **stdout** to **/dev/null**. # STDERR Any error output is written to **stderr**. **Note**: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the **EXIT STATUS** section) if it cannot write to **stderr**, so if **stderr** is closed, as in **bc 2>&-**, it will quit with an error. This is done so that bc(1) can exit with an error code when **stderr** is redirected to a file. If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect **stderr** to **/dev/null**. # SYNTAX The syntax for bc(1) programs is mostly C-like, with some differences. This bc(1) follows the POSIX standard (see the **STANDARDS** section), which is a much more thorough resource for the language this bc(1) accepts. This section is meant to be a summary and a listing of all the extensions to the standard. In the sections below, **E** means expression, **S** means statement, and **I** means identifier. Identifiers (**I**) start with a lowercase letter and can be followed by any number (up to **BC_NAME_MAX-1**) of lowercase letters (**a-z**), digits (**0-9**), and underscores (**\_**). The regex is **\[a-z\]\[a-z0-9\_\]\***. Identifiers with more than one character (letter) are a **non-portable extension**. **ibase** is a global variable determining how to interpret constant numbers. It is the "input" base, or the number base used for interpreting input numbers. **ibase** is initially **10**. If the **-s** (**-\-standard**) and **-w** (**-\-warn**) flags were not given on the command line, the max allowable value for **ibase** is **36**. Otherwise, it is **16**. The min allowable value for **ibase** is **2**. The max allowable value for **ibase** can be queried in bc(1) programs with the **maxibase()** built-in function. **obase** is a global variable determining how to output results. It is the "output" base, or the number base used for outputting numbers. **obase** is initially **10**. The max allowable value for **obase** is **BC_BASE_MAX** and can be queried in bc(1) programs with the **maxobase()** built-in function. The min allowable value for **obase** is **0**. If **obase** is **0**, values are output in scientific notation, and if **obase** is **1**, values are output in engineering notation. Otherwise, values are output in the specified base. Outputting in scientific and engineering notations are **non-portable extensions**. The *scale* of an expression is the number of digits in the result of the expression right of the decimal point, and **scale** is a global variable that sets the precision of any operations, with exceptions. **scale** is initially **0**. **scale** cannot be negative. The max allowable value for **scale** is **BC_SCALE_MAX** and can be queried in bc(1) programs with the **maxscale()** built-in function. bc(1) has both *global* variables and *local* variables. All *local* variables are local to the function; they are parameters or are introduced in the **auto** list of a function (see the **FUNCTIONS** section). If a variable is accessed which is not a parameter or in the **auto** list, it is assumed to be *global*. If a parent function has a *local* variable version of a variable that a child function considers *global*, the value of that *global* variable in the child function is the value of the variable in the parent function, not the value of the actual *global* variable. All of the above applies to arrays as well. The value of a statement that is an expression (i.e., any of the named expressions or operands) is printed unless the lowest precedence operator is an assignment operator *and* the expression is notsurrounded by parentheses. The value that is printed is also assigned to the special variable **last**. A single dot (**.**) may also be used as a synonym for **last**. These are **non-portable extensions**. Either semicolons or newlines may separate statements. ## Comments There are two kinds of comments: 1. Block comments are enclosed in **/\*** and **\*/**. 2. Line comments go from **#** until, and not including, the next newline. This is a **non-portable extension**. ## Named Expressions The following are named expressions in bc(1): 1. Variables: **I** 2. Array Elements: **I[E]** 3. **ibase** 4. **obase** 5. **scale** 6. **seed** 7. **last** or a single dot (**.**) Numbers 6 and 7 are **non-portable extensions**. The meaning of **seed** is dependent on the current pseudo-random number generator but is guaranteed to not change except for new major versions. The *scale* and sign of the value may be significant. If a previously used **seed** value is assigned to **seed** and used again, the pseudo-random number generator is guaranteed to produce the same sequence of pseudo-random numbers as it did when the **seed** value was previously used. The exact value assigned to **seed** is not guaranteed to be returned if **seed** is queried again immediately. However, if **seed** *does* return a different value, both values, when assigned to **seed**, are guaranteed to produce the same sequence of pseudo-random numbers. This means that certain values assigned to **seed** will *not* produce unique sequences of pseudo-random numbers. The value of **seed** will change after any use of the **rand()** and **irand(E)** operands (see the *Operands* subsection below), except if the parameter passed to **irand(E)** is **0**, **1**, or negative. There is no limit to the length (number of significant decimal digits) or *scale* of the value that can be assigned to **seed**. Variables and arrays do not interfere; users can have arrays named the same as variables. This also applies to functions (see the **FUNCTIONS** section), so a user can have a variable, array, and function that all have the same name, and they will not shadow each other, whether inside of functions or not. Named expressions are required as the operand of **increment**/**decrement** operators and as the left side of **assignment** operators (see the *Operators* subsection). ## Operands The following are valid operands in bc(1): 1. Numbers (see the *Numbers* subsection below). 2. Array indices (**I[E]**). 3. **(E)**: The value of **E** (used to change precedence). 4. **sqrt(E)**: The square root of **E**. **E** must be non-negative. 5. **length(E)**: The number of significant decimal digits in **E**. Returns **1** for **0** with no decimal places. If given a string, the length of the string is returned. Passing a string to **length(E)** is a **non-portable extension**. 6. **length(I[])**: The number of elements in the array **I**. This is a **non-portable extension**. 7. **scale(E)**: The *scale* of **E**. 8. **abs(E)**: The absolute value of **E**. This is a **non-portable extension**. 9. **is_number(E)**: **1** if the given argument is a number, **0** if it is a string. This is a **non-portable extension**. 10. **is_string(E)**: **1** if the given argument is a string, **0** if it is a number. This is a **non-portable extension**. 11. **modexp(E, E, E)**: Modular exponentiation, where the first expression is the base, the second is the exponent, and the third is the modulus. All three values must be integers. The second argument must be non-negative. The third argument must be non-zero. This is a **non-portable extension**. 11. **divmod(E, E, I[])**: Division and modulus in one operation. This is for optimization. The first expression is the dividend, and the second is the divisor, which must be non-zero. The return value is the quotient, and the modulus is stored in index **0** of the provided array (the last argument). This is a **non-portable extension**. 12. **asciify(E)**: If **E** is a string, returns a string that is the first letter of its argument. If it is a number, calculates the number mod **256** and returns that number as a one-character string. This is a **non-portable extension**. 13. **asciify(I[])**: A string that is made up of the characters that would result from running **asciify(E)** on each element of the array identified by the argument. This allows creating multi-character strings and storing them. This is a **non-portable extension**. 14. **I()**, **I(E)**, **I(E, E)**, and so on, where **I** is an identifier for a non-**void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). The **E** argument(s) may also be arrays of the form **I[]**, which will automatically be turned into array references (see the *Array References* subsection of the **FUNCTIONS** section) if the corresponding parameter in the function definition is an array reference. 15. **read()**: Reads a line from **stdin** and uses that as an expression. The result of that expression is the result of the **read()** operand. This is a **non-portable extension**. 16. **maxibase()**: The max allowable **ibase**. This is a **non-portable extension**. 17. **maxobase()**: The max allowable **obase**. This is a **non-portable extension**. 18. **maxscale()**: The max allowable **scale**. This is a **non-portable extension**. 19. **line_length()**: The line length set with **BC_LINE_LENGTH** (see the **ENVIRONMENT VARIABLES** section). This is a **non-portable extension**. 20. **global_stacks()**: **0** if global stacks are not enabled with the **-g** or **-\-global-stacks** options, non-zero otherwise. See the **OPTIONS** section. This is a **non-portable extension**. 21. **leading_zero()**: **0** if leading zeroes are not enabled with the **-z** or **--leading-zeroes** options, non-zero otherwise. See the **OPTIONS** section. This is a **non-portable extension**. 22. **rand()**: A pseudo-random integer between **0** (inclusive) and **BC_RAND_MAX** (inclusive). Using this operand will change the value of **seed**. This is a **non-portable extension**. 23. **irand(E)**: A pseudo-random integer between **0** (inclusive) and the value of **E** (exclusive). If **E** is negative or is a non-integer (**E**'s *scale* is not **0**), an error is raised, and bc(1) resets (see the **RESET** section) while **seed** remains unchanged. If **E** is larger than **BC_RAND_MAX**, the higher bound is honored by generating several pseudo-random integers, multiplying them by appropriate powers of **BC_RAND_MAX+1**, and adding them together. Thus, the size of integer that can be generated with this operand is unbounded. Using this operand will change the value of **seed**, unless the value of **E** is **0** or **1**. In that case, **0** is returned, and **seed** is *not* changed. This is a **non-portable extension**. 24. **maxrand()**: The max integer returned by **rand()**. This is a **non-portable extension**. The integers generated by **rand()** and **irand(E)** are guaranteed to be as unbiased as possible, subject to the limitations of the pseudo-random number generator. **Note**: The values returned by the pseudo-random number generator with **rand()** and **irand(E)** are guaranteed to *NOT* be cryptographically secure. This is a consequence of using a seeded pseudo-random number generator. However, they *are* guaranteed to be reproducible with identical **seed** values. This means that the pseudo-random values from bc(1) should only be used where a reproducible stream of pseudo-random numbers is *ESSENTIAL*. In any other case, use a non-seeded pseudo-random number generator. ## Numbers Numbers are strings made up of digits, uppercase letters, and at most **1** period for a radix. Numbers can have up to **BC_NUM_MAX** digits. Uppercase letters are equal to **9** plus their position in the alphabet, starting from **1** (i.e., **A** equals **10**, or **9+1**). If a digit or letter makes no sense with the current value of **ibase** (i.e., they are greater than or equal to the current value of **ibase**), then the behavior depends on the existence of the **-c**/**-\-digit-clamp** or **-C**/**-\-no-digit-clamp** options (see the **OPTIONS** section), the existence and setting of the **BC_DIGIT_CLAMP** environment variable (see the **ENVIRONMENT VARIABLES** section), or the default, which can be queried with the **-h**/**-\-help** option. If clamping is off, then digits or letters that are greater than or equal to the current value of **ibase** are not changed. Instead, their given value is multiplied by the appropriate power of **ibase** and added into the number. This means that, with an **ibase** of **3**, the number **AB** is equal to **3\^1\*A+3\^0\*B**, which is **3** times **10** plus **11**, or **41**. If clamping is on, then digits or letters that are greater than or equal to the current value of **ibase** are set to the value of the highest valid digit in **ibase** before being multiplied by the appropriate power of **ibase** and added into the number. This means that, with an **ibase** of **3**, the number **AB** is equal to **3\^1\*2+3\^0\*2**, which is **3** times **2** plus **2**, or **8**. There is one exception to clamping: single-character numbers (i.e., **A** alone). Such numbers are never clamped and always take the value they would have in the highest possible **ibase**. This means that **A** alone always equals decimal **10** and **Z** alone always equals decimal **35**. This behavior is mandated by the standard (see the STANDARDS section) and is meant to provide an easy way to set the current **ibase** (with the **i** command) regardless of the current value of **ibase**. If clamping is on, and the clamped value of a character is needed, use a leading zero, i.e., for **A**, use **0A**. In addition, bc(1) accepts numbers in scientific notation. These have the form **\e\**. The exponent (the portion after the **e**) must be an integer. An example is **1.89237e9**, which is equal to **1892370000**. Negative exponents are also allowed, so **4.2890e-3** is equal to **0.0042890**. Using scientific notation is an error or warning if the **-s** or **-w**, respectively, command-line options (or equivalents) are given. **WARNING**: Both the number and the exponent in scientific notation are interpreted according to the current **ibase**, but the number is still multiplied by **10\^exponent** regardless of the current **ibase**. For example, if **ibase** is **16** and bc(1) is given the number string **FFeA**, the resulting decimal number will be **2550000000000**, and if bc(1) is given the number string **10e-4**, the resulting decimal number will be **0.0016**. Accepting input as scientific notation is a **non-portable extension**. ## Operators The following arithmetic and logical operators can be used. They are listed in order of decreasing precedence. Operators in the same group have the same precedence. **++** **-\-** : Type: Prefix and Postfix Associativity: None Description: **increment**, **decrement** **-** **!** : Type: Prefix Associativity: None Description: **negation**, **boolean not** **\$** : Type: Postfix Associativity: None Description: **truncation** **\@** : Type: Binary Associativity: Right Description: **set precision** **\^** : Type: Binary Associativity: Right Description: **power** **\*** **/** **%** : Type: Binary Associativity: Left Description: **multiply**, **divide**, **modulus** **+** **-** : Type: Binary Associativity: Left Description: **add**, **subtract** **\<\<** **\>\>** : Type: Binary Associativity: Left Description: **shift left**, **shift right** **=** **\<\<=** **\>\>=** **+=** **-=** **\*=** **/=** **%=** **\^=** **\@=** : Type: Binary Associativity: Right Description: **assignment** **==** **\<=** **\>=** **!=** **\<** **\>** : Type: Binary Associativity: Left Description: **relational** **&&** : Type: Binary Associativity: Left Description: **boolean and** **||** : Type: Binary Associativity: Left Description: **boolean or** The operators will be described in more detail below. **++** **-\-** : The prefix and postfix **increment** and **decrement** operators behave exactly like they would in C. They require a named expression (see the *Named Expressions* subsection) as an operand. The prefix versions of these operators are more efficient; use them where possible. **-** : The **negation** operator returns **0** if a user attempts to negate any expression with the value **0**. Otherwise, a copy of the expression with its sign flipped is returned. **!** : The **boolean not** operator returns **1** if the expression is **0**, or **0** otherwise. This is a **non-portable extension**. **\$** : The **truncation** operator returns a copy of the given expression with all of its *scale* removed. This is a **non-portable extension**. **\@** : The **set precision** operator takes two expressions and returns a copy of the first with its *scale* equal to the value of the second expression. That could either mean that the number is returned without change (if the *scale* of the first expression matches the value of the second expression), extended (if it is less), or truncated (if it is more). The second expression must be an integer (no *scale*) and non-negative. This is a **non-portable extension**. **\^** : The **power** operator (not the **exclusive or** operator, as it would be in C) takes two expressions and raises the first to the power of the value of the second. The *scale* of the result is equal to **scale**. The second expression must be an integer (no *scale*), and if it is negative, the first value must be non-zero. **\*** : The **multiply** operator takes two expressions, multiplies them, and returns the product. If **a** is the *scale* of the first expression and **b** is the *scale* of the second expression, the *scale* of the result is equal to **min(a+b,max(scale,a,b))** where **min()** and **max()** return the obvious values. **/** : The **divide** operator takes two expressions, divides them, and returns the quotient. The *scale* of the result shall be the value of **scale**. The second expression must be non-zero. **%** : The **modulus** operator takes two expressions, **a** and **b**, and evaluates them by 1) Computing **a/b** to current **scale** and 2) Using the result of step 1 to calculate **a-(a/b)\*b** to *scale* **max(scale+scale(b),scale(a))**. The second expression must be non-zero. **+** : The **add** operator takes two expressions, **a** and **b**, and returns the sum, with a *scale* equal to the max of the *scale*s of **a** and **b**. **-** : The **subtract** operator takes two expressions, **a** and **b**, and returns the difference, with a *scale* equal to the max of the *scale*s of **a** and **b**. **\<\<** : The **left shift** operator takes two expressions, **a** and **b**, and returns a copy of the value of **a** with its decimal point moved **b** places to the right. The second expression must be an integer (no *scale*) and non-negative. This is a **non-portable extension**. **\>\>** : The **right shift** operator takes two expressions, **a** and **b**, and returns a copy of the value of **a** with its decimal point moved **b** places to the left. The second expression must be an integer (no *scale*) and non-negative. This is a **non-portable extension**. **=** **\<\<=** **\>\>=** **+=** **-=** **\*=** **/=** **%=** **\^=** **\@=** : The **assignment** operators take two expressions, **a** and **b** where **a** is a named expression (see the *Named Expressions* subsection). For **=**, **b** is copied and the result is assigned to **a**. For all others, **a** and **b** are applied as operands to the corresponding arithmetic operator and the result is assigned to **a**. The **assignment** operators that correspond to operators that are extensions are themselves **non-portable extensions**. **==** **\<=** **\>=** **!=** **\<** **\>** : The **relational** operators compare two expressions, **a** and **b**, and if the relation holds, according to C language semantics, the result is **1**. Otherwise, it is **0**. Note that unlike in C, these operators have a lower precedence than the **assignment** operators, which means that **a=b\>c** is interpreted as **(a=b)\>c**. Also, unlike the standard (see the **STANDARDS** section) requires, these operators can appear anywhere any other expressions can be used. This allowance is a **non-portable extension**. **&&** : The **boolean and** operator takes two expressions and returns **1** if both expressions are non-zero, **0** otherwise. This is *not* a short-circuit operator. This is a **non-portable extension**. **||** : The **boolean or** operator takes two expressions and returns **1** if one of the expressions is non-zero, **0** otherwise. This is *not* a short-circuit operator. This is a **non-portable extension**. ## Statements The following items are statements: 1. **E** 2. **{** **S** **;** ... **;** **S** **}** 3. **if** **(** **E** **)** **S** 4. **if** **(** **E** **)** **S** **else** **S** 5. **while** **(** **E** **)** **S** 6. **for** **(** **E** **;** **E** **;** **E** **)** **S** 7. An empty statement 8. **break** 9. **continue** 10. **quit** 11. **halt** 12. **limits** 13. A string of characters, enclosed in double quotes 14. **print** **E** **,** ... **,** **E** 15. **stream** **E** **,** ... **,** **E** 16. **I()**, **I(E)**, **I(E, E)**, and so on, where **I** is an identifier for a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). The **E** argument(s) may also be arrays of the form **I[]**, which will automatically be turned into array references (see the *Array References* subsection of the **FUNCTIONS** section) if the corresponding parameter in the function definition is an array reference. Numbers 4, 9, 11, 12, 14, 15, and 16 are **non-portable extensions**. Also, as a **non-portable extension**, any or all of the expressions in the header of a for loop may be omitted. If the condition (second expression) is omitted, it is assumed to be a constant **1**. The **break** statement causes a loop to stop iterating and resume execution immediately following a loop. This is only allowed in loops. The **continue** statement causes a loop iteration to stop early and returns to the start of the loop, including testing the loop condition. This is only allowed in loops. The **if** **else** statement does the same thing as in C. The **quit** statement causes bc(1) to quit, even if it is on a branch that will not be executed (it is a compile-time command). **Warning**: The behavior of this bc(1) on **quit** is slightly different from other bc(1) implementations. Other bc(1) implementations will exit as soon as they finish parsing the line that a **quit** command is on. This bc(1) will execute any completed and executable statements that occur before the **quit** statement before exiting. In other words, for the bc(1) code below: for (i = 0; i < 3; ++i) i; quit Other bc(1) implementations will print nothing, and this bc(1) will print **0**, **1**, and **2** on successive lines before exiting. The **halt** statement causes bc(1) to quit, if it is executed. (Unlike **quit** if it is on a branch of an **if** statement that is not executed, bc(1) does not quit.) The **limits** statement prints the limits that this bc(1) is subject to. This is like the **quit** statement in that it is a compile-time command. An expression by itself is evaluated and printed, followed by a newline. Both scientific notation and engineering notation are available for printing the results of expressions. Scientific notation is activated by assigning **0** to **obase**, and engineering notation is activated by assigning **1** to **obase**. To deactivate them, just assign a different value to **obase**. Scientific notation and engineering notation are disabled if bc(1) is run with either the **-s** or **-w** command-line options (or equivalents). Printing numbers in scientific notation and/or engineering notation is a **non-portable extension**. ## Strings If strings appear as a statement by themselves, they are printed without a trailing newline. In addition to appearing as a lone statement by themselves, strings can be assigned to variables and array elements. They can also be passed to functions in variable parameters. If any statement that expects a string is given a variable that had a string assigned to it, the statement acts as though it had received a string. If any math operation is attempted on a string or a variable or array element that has been assigned a string, an error is raised, and bc(1) resets (see the **RESET** section). Assigning strings to variables and array elements and passing them to functions are **non-portable extensions**. ## Print Statement The "expressions" in a **print** statement may also be strings. If they are, there are backslash escape sequences that are interpreted specially. What those sequences are, and what they cause to be printed, are shown below: **\\a**: **\\a** **\\b**: **\\b** **\\\\**: **\\** **\\e**: **\\** **\\f**: **\\f** **\\n**: **\\n** **\\q**: **"** **\\r**: **\\r** **\\t**: **\\t** Any other character following a backslash causes the backslash and character to be printed as-is. Any non-string expression in a print statement shall be assigned to **last**, like any other expression that is printed. ## Stream Statement The expressions in a **stream** statement may also be strings. If a **stream** statement is given a string, it prints the string as though the string had appeared as its own statement. In other words, the **stream** statement prints strings normally, without a newline. If a **stream** statement is given a number, a copy of it is truncated and its absolute value is calculated. The result is then printed as though **obase** is **256** and each digit is interpreted as an 8-bit ASCII character, making it a byte stream. ## Order of Evaluation All expressions in a statment are evaluated left to right, except as necessary to maintain order of operations. This means, for example, assuming that **i** is equal to **0**, in the expression a[i++] = i++ the first (or 0th) element of **a** is set to **1**, and **i** is equal to **2** at the end of the expression. This includes function arguments. Thus, assuming **i** is equal to **0**, this means that in the expression x(i++, i++) the first argument passed to **x()** is **0**, and the second argument is **1**, while **i** is equal to **2** before the function starts executing. # FUNCTIONS Function definitions are as follows: ``` define I(I,...,I){ auto I,...,I S;...;S return(E) } ``` Any **I** in the parameter list or **auto** list may be replaced with **I[]** to make a parameter or **auto** var an array, and any **I** in the parameter list may be replaced with **\*I[]** to make a parameter an array reference. Callers of functions that take array references should not put an asterisk in the call; they must be called with just **I[]** like normal array parameters and will be automatically converted into references. As a **non-portable extension**, the opening brace of a **define** statement may appear on the next line. As a **non-portable extension**, the return statement may also be in one of the following forms: 1. **return** 2. **return** **(** **)** 3. **return** **E** The first two, or not specifying a **return** statement, is equivalent to **return (0)**, unless the function is a **void** function (see the *Void Functions* subsection below). ## Void Functions Functions can also be **void** functions, defined as follows: ``` define void I(I,...,I){ auto I,...,I S;...;S return } ``` They can only be used as standalone expressions, where such an expression would be printed alone, except in a print statement. Void functions can only use the first two **return** statements listed above. They can also omit the return statement entirely. The word "void" is not treated as a keyword; it is still possible to have variables, arrays, and functions named **void**. The word "void" is only treated specially right after the **define** keyword. This is a **non-portable extension**. ## Array References For any array in the parameter list, if the array is declared in the form ``` *I[] ``` it is a **reference**. Any changes to the array in the function are reflected, when the function returns, to the array that was passed in. Other than this, all function arguments are passed by value. This is a **non-portable extension**. # LIBRARY All of the functions below, including the functions in the extended math library (see the *Extended Library* subsection below), are available when the **-l** or **-\-mathlib** command-line flags are given, except that the extended math library is not available when the **-s** option, the **-w** option, or equivalents are given. ## Standard Library The standard (see the **STANDARDS** section) defines the following functions for the math library: **s(x)** : Returns the sine of **x**, which is assumed to be in radians. This is a transcendental function (see the *Transcendental Functions* subsection below). **c(x)** : Returns the cosine of **x**, which is assumed to be in radians. This is a transcendental function (see the *Transcendental Functions* subsection below). **a(x)** : Returns the arctangent of **x**, in radians. This is a transcendental function (see the *Transcendental Functions* subsection below). **l(x)** : Returns the natural logarithm of **x**. This is a transcendental function (see the *Transcendental Functions* subsection below). **e(x)** : Returns the mathematical constant **e** raised to the power of **x**. This is a transcendental function (see the *Transcendental Functions* subsection below). **j(x, n)** : Returns the bessel integer order **n** (truncated) of **x**. This is a transcendental function (see the *Transcendental Functions* subsection below). ## Extended Library The extended library is *not* loaded when the **-s**/**-\-standard** or **-w**/**-\-warn** options are given since they are not part of the library defined by the standard (see the **STANDARDS** section). The extended library is a **non-portable extension**. **p(x, y)** : Calculates **x** to the power of **y**, even if **y** is not an integer, and returns the result to the current **scale**. It is an error if **y** is negative and **x** is **0**. This is a transcendental function (see the *Transcendental Functions* subsection below). **r(x, p)** : Returns **x** rounded to **p** decimal places according to the rounding mode round half away from **0** (https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero). **ceil(x, p)** : Returns **x** rounded to **p** decimal places according to the rounding mode round away from **0** (https://en.wikipedia.org/wiki/Rounding#Rounding_away_from_zero). **f(x)** : Returns the factorial of the truncated absolute value of **x**. **max(a, b)** : Returns **a** if **a** is greater than **b**; otherwise, returns **b**. **min(a, b)** : Returns **a** if **a** is less than **b**; otherwise, returns **b**. **perm(n, k)** : Returns the permutation of the truncated absolute value of **n** of the truncated absolute value of **k**, if **k \<= n**. If not, it returns **0**. **comb(n, k)** : Returns the combination of the truncated absolute value of **n** of the truncated absolute value of **k**, if **k \<= n**. If not, it returns **0**. **fib(n)** : Returns the Fibonacci number of the truncated absolute value of **n**. **l2(x)** : Returns the logarithm base **2** of **x**. This is a transcendental function (see the *Transcendental Functions* subsection below). **l10(x)** : Returns the logarithm base **10** of **x**. This is a transcendental function (see the *Transcendental Functions* subsection below). **log(x, b)** : Returns the logarithm base **b** of **x**. This is a transcendental function (see the *Transcendental Functions* subsection below). **cbrt(x)** : Returns the cube root of **x**. **root(x, n)** : Calculates the truncated value of **n**, **r**, and returns the **r**th root of **x** to the current **scale**. If **r** is **0** or negative, this raises an error and causes bc(1) to reset (see the **RESET** section). It also raises an error and causes bc(1) to reset if **r** is even and **x** is negative. **gcd(a, b)** : Returns the greatest common divisor (factor) of the truncated absolute value of **a** and the truncated absolute value of **b**. **lcm(a, b)** : Returns the least common multiple of the truncated absolute value of **a** and the truncated absolute value of **b**. **pi(p)** : Returns **pi** to **p** decimal places. This is a transcendental function (see the *Transcendental Functions* subsection below). **t(x)** : Returns the tangent of **x**, which is assumed to be in radians. This is a transcendental function (see the *Transcendental Functions* subsection below). **a2(y, x)** : Returns the arctangent of **y/x**, in radians. If both **y** and **x** are equal to **0**, it raises an error and causes bc(1) to reset (see the **RESET** section). Otherwise, if **x** is greater than **0**, it returns **a(y/x)**. If **x** is less than **0**, and **y** is greater than or equal to **0**, it returns **a(y/x)+pi**. If **x** is less than **0**, and **y** is less than **0**, it returns **a(y/x)-pi**. If **x** is equal to **0**, and **y** is greater than **0**, it returns **pi/2**. If **x** is equal to **0**, and **y** is less than **0**, it returns **-pi/2**. This function is the same as the **atan2()** function in many programming languages. This is a transcendental function (see the *Transcendental Functions* subsection below). **sin(x)** : Returns the sine of **x**, which is assumed to be in radians. This is an alias of **s(x)**. This is a transcendental function (see the *Transcendental Functions* subsection below). **cos(x)** : Returns the cosine of **x**, which is assumed to be in radians. This is an alias of **c(x)**. This is a transcendental function (see the *Transcendental Functions* subsection below). **tan(x)** : Returns the tangent of **x**, which is assumed to be in radians. If **x** is equal to **1** or **-1**, this raises an error and causes bc(1) to reset (see the **RESET** section). This is an alias of **t(x)**. This is a transcendental function (see the *Transcendental Functions* subsection below). **atan(x)** : Returns the arctangent of **x**, in radians. This is an alias of **a(x)**. This is a transcendental function (see the *Transcendental Functions* subsection below). **atan2(y, x)** : Returns the arctangent of **y/x**, in radians. If both **y** and **x** are equal to **0**, it raises an error and causes bc(1) to reset (see the **RESET** section). Otherwise, if **x** is greater than **0**, it returns **a(y/x)**. If **x** is less than **0**, and **y** is greater than or equal to **0**, it returns **a(y/x)+pi**. If **x** is less than **0**, and **y** is less than **0**, it returns **a(y/x)-pi**. If **x** is equal to **0**, and **y** is greater than **0**, it returns **pi/2**. If **x** is equal to **0**, and **y** is less than **0**, it returns **-pi/2**. This function is the same as the **atan2()** function in many programming languages. This is an alias of **a2(y, x)**. This is a transcendental function (see the *Transcendental Functions* subsection below). **r2d(x)** : Converts **x** from radians to degrees and returns the result. This is a transcendental function (see the *Transcendental Functions* subsection below). **d2r(x)** : Converts **x** from degrees to radians and returns the result. This is a transcendental function (see the *Transcendental Functions* subsection below). **frand(p)** : Generates a pseudo-random integer between **0** (inclusive) and **1** (exclusive) with the number of decimal digits after the decimal point equal to the truncated absolute value of **p**. If **p** is not **0**, then calling this function will change the value of **seed**. If **p** is **0**, then **0** is returned, and **seed** is *not* changed. **ifrand(i, p)** : Generates a pseudo-random integer that is between **0** (inclusive) and the truncated absolute value of **i** (exclusive) with the number of decimal digits after the decimal point equal to the truncated absolute value of **p**. If the absolute value of **i** is greater than or equal to **2**, and **p** is not **0**, then calling this function will change the value of **seed**; otherwise, **0** is returned, and **seed** is not changed. **i2rand(a, b)** : Takes the truncated value of **a** and **b** and uses them as inclusive bounds to enerate a pseudo-random integer. If the difference of the truncated values of **a** and **b** is **0**, then the truncated value is returned, and **seed** is *not* changed. Otherwise, this function will change the value of **seed**. **srand(x)** : Returns **x** with its sign flipped with probability **0.5**. In other words, it randomizes the sign of **x**. **brand()** : Returns a random boolean value (either **0** or **1**). **band(a, b)** : Takes the truncated absolute value of both **a** and **b** and calculates and returns the result of the bitwise **and** operation between them. If you want to use signed two's complement arguments, use **s2u(x)** to convert. **bor(a, b)** : Takes the truncated absolute value of both **a** and **b** and calculates and returns the result of the bitwise **or** operation between them. If you want to use signed two's complement arguments, use **s2u(x)** to convert. **bxor(a, b)** : Takes the truncated absolute value of both **a** and **b** and calculates and returns the result of the bitwise **xor** operation between them. If you want to use signed two's complement arguments, use **s2u(x)** to convert. **bshl(a, b)** : Takes the truncated absolute value of both **a** and **b** and calculates and returns the result of **a** bit-shifted left by **b** places. If you want to use signed two's complement arguments, use **s2u(x)** to convert. **bshr(a, b)** : Takes the truncated absolute value of both **a** and **b** and calculates and returns the truncated result of **a** bit-shifted right by **b** places. If you want to use signed two's complement arguments, use **s2u(x)** to convert. **bnotn(x, n)** : Takes the truncated absolute value of **x** and does a bitwise not as though it has the same number of bytes as the truncated absolute value of **n**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bnot8(x)** : Does a bitwise not of the truncated absolute value of **x** as though it has - **8** binary digits (1 unsigned byte). + **8** binary digits (**1** unsigned byte). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bnot16(x)** : Does a bitwise not of the truncated absolute value of **x** as though it has - **16** binary digits (2 unsigned bytes). + **16** binary digits (**2** unsigned bytes). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bnot32(x)** : Does a bitwise not of the truncated absolute value of **x** as though it has - **32** binary digits (4 unsigned bytes). + **32** binary digits (**4** unsigned bytes). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bnot64(x)** : Does a bitwise not of the truncated absolute value of **x** as though it has - **64** binary digits (8 unsigned bytes). + **64** binary digits (**8** unsigned bytes). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bnot(x)** : Does a bitwise not of the truncated absolute value of **x** as though it has the minimum number of power of two unsigned bytes. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brevn(x, n)** : Runs a bit reversal on the truncated absolute value of **x** as though it has the same number of 8-bit bytes as the truncated absolute value of **n**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brev8(x)** : Runs a bit reversal on the truncated absolute value of **x** as though it - has 8 binary digits (1 unsigned byte). + has 8 binary digits (**1** unsigned byte). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brev16(x)** : Runs a bit reversal on the truncated absolute value of **x** as though it - has 16 binary digits (2 unsigned bytes). + has 16 binary digits (**2** unsigned bytes). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brev32(x)** : Runs a bit reversal on the truncated absolute value of **x** as though it - has 32 binary digits (4 unsigned bytes). + has 32 binary digits (**4** unsigned bytes). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brev64(x)** : Runs a bit reversal on the truncated absolute value of **x** as though it - has 64 binary digits (8 unsigned bytes). + has 64 binary digits (**8** unsigned bytes). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brev(x)** : Runs a bit reversal on the truncated absolute value of **x** as though it has the minimum number of power of two unsigned bytes. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **broln(x, p, n)** : Does a left bitwise rotatation of the truncated absolute value of **x**, as though it has the same number of unsigned 8-bit bytes as the truncated absolute value of **n**, by the number of places equal to the truncated absolute value of **p** modded by the **2** to the power of the number of binary digits in **n** 8-bit bytes. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brol8(x, p)** : Does a left bitwise rotatation of the truncated absolute value of **x**, as though it has **8** binary digits (**1** unsigned byte), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **8**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brol16(x, p)** : Does a left bitwise rotatation of the truncated absolute value of **x**, as though it has **16** binary digits (**2** unsigned bytes), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **16**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brol32(x, p)** : Does a left bitwise rotatation of the truncated absolute value of **x**, as - though it has **32** binary digits (**2** unsigned bytes), by the number of + though it has **32** binary digits (**4** unsigned bytes), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **32**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brol64(x, p)** : Does a left bitwise rotatation of the truncated absolute value of **x**, as - though it has **64** binary digits (**2** unsigned bytes), by the number of + though it has **64** binary digits (**8** unsigned bytes), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **64**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brol(x, p)** : Does a left bitwise rotatation of the truncated absolute value of **x**, as though it has the minimum number of power of two unsigned 8-bit bytes, by the number of places equal to the truncated absolute value of **p** modded by 2 to the power of the number of binary digits in the minimum number of 8-bit bytes. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brorn(x, p, n)** : Does a right bitwise rotatation of the truncated absolute value of **x**, as though it has the same number of unsigned 8-bit bytes as the truncated absolute value of **n**, by the number of places equal to the truncated absolute value of **p** modded by the **2** to the power of the number of binary digits in **n** 8-bit bytes. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bror8(x, p)** : Does a right bitwise rotatation of the truncated absolute value of **x**, as though it has **8** binary digits (**1** unsigned byte), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **8**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bror16(x, p)** : Does a right bitwise rotatation of the truncated absolute value of **x**, as though it has **16** binary digits (**2** unsigned bytes), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **16**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bror32(x, p)** : Does a right bitwise rotatation of the truncated absolute value of **x**, as though it has **32** binary digits (**2** unsigned bytes), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **32**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bror64(x, p)** : Does a right bitwise rotatation of the truncated absolute value of **x**, as though it has **64** binary digits (**2** unsigned bytes), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **64**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bror(x, p)** : Does a right bitwise rotatation of the truncated absolute value of **x**, as though it has the minimum number of power of two unsigned 8-bit bytes, by the number of places equal to the truncated absolute value of **p** modded by 2 to the power of the number of binary digits in the minimum number of 8-bit bytes. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bmodn(x, n)** : Returns the modulus of the truncated absolute value of **x** by **2** to the power of the multiplication of the truncated absolute value of **n** and **8**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bmod8(x, n)** : Returns the modulus of the truncated absolute value of **x** by **2** to the power of **8**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bmod16(x, n)** : Returns the modulus of the truncated absolute value of **x** by **2** to the power of **16**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bmod32(x, n)** : Returns the modulus of the truncated absolute value of **x** by **2** to the power of **32**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bmod64(x, n)** : Returns the modulus of the truncated absolute value of **x** by **2** to the power of **64**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bunrev(t)** : Assumes **t** is a bitwise-reversed number with an extra set bit one place more significant than the real most significant bit (which was the least significant bit in the original number). This number is reversed and returned without the extra set bit. This function is used to implement other bitwise functions; it is not meant to be used by users, but it can be. **plz(x)** : If **x** is not equal to **0** and greater that **-1** and less than **1**, it is printed with a leading zero, regardless of the use of the **-z** option (see the **OPTIONS** section) and without a trailing newline. Otherwise, **x** is printed normally, without a trailing newline. **plznl(x)** : If **x** is not equal to **0** and greater that **-1** and less than **1**, it is printed with a leading zero, regardless of the use of the **-z** option (see the **OPTIONS** section) and with a trailing newline. Otherwise, **x** is printed normally, with a trailing newline. **pnlz(x)** : If **x** is not equal to **0** and greater that **-1** and less than **1**, it is printed without a leading zero, regardless of the use of the **-z** option (see the **OPTIONS** section) and without a trailing newline. Otherwise, **x** is printed normally, without a trailing newline. **pnlznl(x)** : If **x** is not equal to **0** and greater that **-1** and less than **1**, it is printed without a leading zero, regardless of the use of the **-z** option (see the **OPTIONS** section) and with a trailing newline. Otherwise, **x** is printed normally, with a trailing newline. **ubytes(x)** : Returns the numbers of unsigned integer bytes required to hold the truncated absolute value of **x**. **sbytes(x)** : Returns the numbers of signed, two's-complement integer bytes required to hold the truncated value of **x**. **s2u(x)** : Returns **x** if it is non-negative. If it *is* negative, then it calculates what **x** would be as a 2's-complement signed integer and returns the non-negative integer that would have the same representation in binary. **s2un(x,n)** : Returns **x** if it is non-negative. If it *is* negative, then it calculates what **x** would be as a 2's-complement signed integer with **n** bytes and returns the non-negative integer that would have the same representation in binary. If **x** cannot fit into **n** 2's-complement signed bytes, it is truncated to fit. **hex(x)** : Outputs the hexadecimal (base **16**) representation of **x**. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **binary(x)** : Outputs the binary (base **2**) representation of **x**. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **output(x, b)** : Outputs the base **b** representation of **x**. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **uint(x)** : Outputs the representation, in binary and hexadecimal, of **x** as an unsigned integer in as few power of two bytes as possible. Both outputs are split into bytes separated by spaces. If **x** is not an integer or is negative, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **int(x)** : Outputs the representation, in binary and hexadecimal, of **x** as a signed, two's-complement integer in as few power of two bytes as possible. Both outputs are split into bytes separated by spaces. If **x** is not an integer, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **uintn(x, n)** : Outputs the representation, in binary and hexadecimal, of **x** as an unsigned integer in **n** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer, is negative, or cannot fit into **n** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **intn(x, n)** : Outputs the representation, in binary and hexadecimal, of **x** as a signed, two's-complement integer in **n** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer or cannot fit into **n** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **uint8(x)** : Outputs the representation, in binary and hexadecimal, of **x** as an unsigned integer in **1** byte. Both outputs are split into bytes separated by spaces. If **x** is not an integer, is negative, or cannot fit into **1** byte, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **int8(x)** : Outputs the representation, in binary and hexadecimal, of **x** as a signed, two's-complement integer in **1** byte. Both outputs are split into bytes separated by spaces. If **x** is not an integer or cannot fit into **1** byte, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **uint16(x)** : Outputs the representation, in binary and hexadecimal, of **x** as an unsigned integer in **2** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer, is negative, or cannot fit into **2** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **int16(x)** : Outputs the representation, in binary and hexadecimal, of **x** as a signed, two's-complement integer in **2** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer or cannot fit into **2** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **uint32(x)** : Outputs the representation, in binary and hexadecimal, of **x** as an unsigned integer in **4** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer, is negative, or cannot fit into **4** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **int32(x)** : Outputs the representation, in binary and hexadecimal, of **x** as a signed, two's-complement integer in **4** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer or cannot fit into **4** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **uint64(x)** : Outputs the representation, in binary and hexadecimal, of **x** as an unsigned integer in **8** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer, is negative, or cannot fit into **8** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **int64(x)** : Outputs the representation, in binary and hexadecimal, of **x** as a signed, two's-complement integer in **8** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer or cannot fit into **8** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **hex_uint(x, n)** : Outputs the representation of the truncated absolute value of **x** as an unsigned integer in hexadecimal using **n** bytes. Not all of the value will be output if **n** is too small. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **binary_uint(x, n)** : Outputs the representation of the truncated absolute value of **x** as an unsigned integer in binary using **n** bytes. Not all of the value will be output if **n** is too small. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **output_uint(x, n)** : Outputs the representation of the truncated absolute value of **x** as an unsigned integer in the current **obase** (see the **SYNTAX** section) using **n** bytes. Not all of the value will be output if **n** is too small. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **output_byte(x, i)** : Outputs byte **i** of the truncated absolute value of **x**, where **0** is the least significant byte and **number_of_bytes - 1** is the most significant byte. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). ## Transcendental Functions All transcendental functions can return slightly inaccurate results, up to 1 ULP (https://en.wikipedia.org/wiki/Unit_in_the_last_place). This is unavoidable, and the article at https://people.eecs.berkeley.edu/~wkahan/LOG10HAF.TXT explains why it is impossible and unnecessary to calculate exact results for the transcendental functions. Because of the possible inaccuracy, I recommend that users call those functions with the precision (**scale**) set to at least 1 higher than is necessary. If exact results are *absolutely* required, users can double the precision (**scale**) and then truncate. The transcendental functions in the standard math library are: * **s(x)** * **c(x)** * **a(x)** * **l(x)** * **e(x)** * **j(x, n)** The transcendental functions in the extended math library are: * **l2(x)** * **l10(x)** * **log(x, b)** * **pi(p)** * **t(x)** * **a2(y, x)** * **sin(x)** * **cos(x)** * **tan(x)** * **atan(x)** * **atan2(y, x)** * **r2d(x)** * **d2r(x)** # RESET When bc(1) encounters an error or a signal that it has a non-default handler for, it resets. This means that several things happen. First, any functions that are executing are stopped and popped off the stack. The behavior is not unlike that of exceptions in programming languages. Then the execution point is set so that any code waiting to execute (after all functions returned) is skipped. Thus, when bc(1) resets, it skips any remaining code waiting to be executed. Then, if it is interactive mode, and the error was not a fatal error (see the **EXIT STATUS** section), it asks for more input; otherwise, it exits with the appropriate return code. Note that this reset behavior is different from the GNU bc(1), which attempts to start executing the statement right after the one that caused an error. # PERFORMANCE Most bc(1) implementations use **char** types to calculate the value of **1** decimal digit at a time, but that can be slow. This bc(1) does something different. It uses large integers to calculate more than **1** decimal digit at a time. If built in a environment where **BC_LONG_BIT** (see the **LIMITS** section) is **64**, then each integer has **9** decimal digits. If built in an environment where **BC_LONG_BIT** is **32** then each integer has **4** decimal digits. This value (the number of decimal digits per large integer) is called **BC_BASE_DIGS**. The actual values of **BC_LONG_BIT** and **BC_BASE_DIGS** can be queried with the **limits** statement. In addition, this bc(1) uses an even larger integer for overflow checking. This integer type depends on the value of **BC_LONG_BIT**, but is always at least twice as large as the integer type used to store digits. # LIMITS The following are the limits on bc(1): **BC_LONG_BIT** : The number of bits in the **long** type in the environment where bc(1) was built. This determines how many decimal digits can be stored in a single large integer (see the **PERFORMANCE** section). **BC_BASE_DIGS** : The number of decimal digits per large integer (see the **PERFORMANCE** section). Depends on **BC_LONG_BIT**. **BC_BASE_POW** : The max decimal number that each large integer can store (see **BC_BASE_DIGS**) plus **1**. Depends on **BC_BASE_DIGS**. **BC_OVERFLOW_MAX** : The max number that the overflow type (see the **PERFORMANCE** section) can hold. Depends on **BC_LONG_BIT**. **BC_BASE_MAX** : The maximum output base. Set at **BC_BASE_POW**. **BC_DIM_MAX** : The maximum size of arrays. Set at **SIZE_MAX-1**. **BC_SCALE_MAX** : The maximum **scale**. Set at **BC_OVERFLOW_MAX-1**. **BC_STRING_MAX** : The maximum length of strings. Set at **BC_OVERFLOW_MAX-1**. **BC_NAME_MAX** : The maximum length of identifiers. Set at **BC_OVERFLOW_MAX-1**. **BC_NUM_MAX** : The maximum length of a number (in decimal digits), which includes digits after the decimal point. Set at **BC_OVERFLOW_MAX-1**. **BC_RAND_MAX** : The maximum integer (inclusive) returned by the **rand()** operand. Set at **2\^BC_LONG_BIT-1**. Exponent : The maximum allowable exponent (positive or negative). Set at **BC_OVERFLOW_MAX**. Number of vars : The maximum number of vars/arrays. Set at **SIZE_MAX-1**. The actual values can be queried with the **limits** statement. These limits are meant to be effectively non-existent; the limits are so large (at least on 64-bit machines) that there should not be any point at which they become a problem. In fact, memory should be exhausted before these limits should be hit. # ENVIRONMENT VARIABLES As **non-portable extensions**, bc(1) recognizes the following environment variables: **POSIXLY_CORRECT** : If this variable exists (no matter the contents), bc(1) behaves as if the **-s** option was given. **BC_ENV_ARGS** : This is another way to give command-line arguments to bc(1). They should be in the same format as all other command-line arguments. These are always processed first, so any files given in **BC_ENV_ARGS** will be processed before arguments and files given on the command-line. This gives the user the ability to set up "standard" options and files to be used at every invocation. The most useful thing for such files to contain would be useful functions that the user might want every time bc(1) runs. The code that parses **BC_ENV_ARGS** will correctly handle quoted arguments, but it does not understand escape sequences. For example, the string **"/home/gavin/some bc file.bc"** will be correctly parsed, but the string **"/home/gavin/some \"bc\" file.bc"** will include the backslashes. The quote parsing will handle either kind of quotes, **'** or **"**. Thus, if you have a file with any number of single quotes in the name, you can use double quotes as the outside quotes, as in **"some 'bc' file.bc"**, and vice versa if you have a file with double quotes. However, handling a file with both kinds of quotes in **BC_ENV_ARGS** is not supported due to the complexity of the parsing, though such files are still supported on the command-line where the parsing is done by the shell. **BC_LINE_LENGTH** : If this environment variable exists and contains an integer that is greater than **1** and is less than **UINT16_MAX** (**2\^16-1**), bc(1) will output lines to that length, including the backslash (**\\**). The default line length is **70**. The special value of **0** will disable line length checking and print numbers without regard to line length and without backslashes and newlines. **BC_BANNER** : If this environment variable exists and contains an integer, then a non-zero value activates the copyright banner when bc(1) is in interactive mode, while zero deactivates it. If bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section), then this environment variable has no effect because bc(1) does not print the banner when not in interactive mode. This environment variable overrides the default, which can be queried with the **-h** or **-\-help** options. **BC_SIGINT_RESET** : If bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section), then this environment variable has no effect because bc(1) exits on **SIGINT** when not in interactive mode. However, when bc(1) is in interactive mode, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) reset on **SIGINT**, rather than exit, and zero makes bc(1) exit. If this environment variable exists and is *not* an integer, then bc(1) will exit on **SIGINT**. This environment variable overrides the default, which can be queried with the **-h** or **-\-help** options. **BC_TTY_MODE** : If TTY mode is *not* available (see the **TTY MODE** section), then this environment variable has no effect. However, when TTY mode is available, then if this environment variable exists and contains an integer, then a non-zero value makes bc(1) use TTY mode, and zero makes bc(1) not use TTY mode. This environment variable overrides the default, which can be queried with the **-h** or **-\-help** options. **BC_PROMPT** : If TTY mode is *not* available (see the **TTY MODE** section), then this environment variable has no effect. However, when TTY mode is available, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) use a prompt, and zero or a non-integer makes bc(1) not use a prompt. If this environment variable does not exist and **BC_TTY_MODE** does, then the value of the **BC_TTY_MODE** environment variable is used. This environment variable and the **BC_TTY_MODE** environment variable override the default, which can be queried with the **-h** or **-\-help** options. **BC_EXPR_EXIT** : If any expressions or expression files are given on the command-line with **-e**, **-\-expression**, **-f**, or **-\-file**, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) exit after executing the expressions and expression files, and a zero value makes bc(1) not exit. This environment variable overrides the default, which can be queried with the **-h** or **-\-help** options. **BC_DIGIT_CLAMP** : When parsing numbers and if this environment variable exists and contains an integer, a non-zero value makes bc(1) clamp digits that are greater than or equal to the current **ibase** so that all such digits are considered equal to the **ibase** minus 1, and a zero value disables such clamping so that those digits are always equal to their value, which is multiplied by the power of the **ibase**. This never applies to single-digit numbers, as per the standard (see the **STANDARDS** section). This environment variable overrides the default, which can be queried with the **-h** or **-\-help** options. # EXIT STATUS bc(1) returns the following exit statuses: **0** : No error. **1** : A math error occurred. This follows standard practice of using **1** for expected errors, since math errors will happen in the process of normal execution. Math errors include divide by **0**, taking the square root of a negative number, using a negative number as a bound for the pseudo-random number generator, attempting to convert a negative number to a hardware integer, overflow when converting a number to a hardware integer, overflow when calculating the size of a number, and attempting to use a non-integer where an integer is required. Converting to a hardware integer happens for the second operand of the power (**\^**), places (**\@**), left shift (**\<\<**), and right shift (**\>\>**) operators and their corresponding assignment operators. **2** : A parse error occurred. Parse errors include unexpected **EOF**, using an invalid character, failing to find the end of a string or comment, using a token where it is invalid, giving an invalid expression, giving an invalid print statement, giving an invalid function definition, attempting to assign to an expression that is not a named expression (see the *Named Expressions* subsection of the **SYNTAX** section), giving an invalid **auto** list, having a duplicate **auto**/function parameter, failing to find the end of a code block, attempting to return a value from a **void** function, attempting to use a variable as a reference, and using any extensions when the option **-s** or any equivalents were given. **3** : A runtime error occurred. Runtime errors include assigning an invalid number to any global (**ibase**, **obase**, or **scale**), giving a bad expression to a **read()** call, calling **read()** inside of a **read()** call, type errors, passing the wrong number of arguments to functions, attempting to call an undefined function, and attempting to use a **void** function call as a value in an expression. **4** : A fatal error occurred. Fatal errors include memory allocation errors, I/O errors, failing to open files, attempting to use files that do not have only ASCII characters (bc(1) only accepts ASCII characters), attempting to open a directory as a file, and giving invalid command-line options. The exit status **4** is special; when a fatal error occurs, bc(1) always exits and returns **4**, no matter what mode bc(1) is in. The other statuses will only be returned when bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section), since bc(1) resets its state (see the **RESET** section) and accepts more input when one of those errors occurs in interactive mode. This is also the case when interactive mode is forced by the **-i** flag or **-\-interactive** option. These exit statuses allow bc(1) to be used in shell scripting with error checking, and its normal behavior can be forced by using the **-i** flag or **-\-interactive** option. # INTERACTIVE MODE Per the standard (see the **STANDARDS** section), bc(1) has an interactive mode and a non-interactive mode. Interactive mode is turned on automatically when both **stdin** and **stdout** are hooked to a terminal, but the **-i** flag and **-\-interactive** option can turn it on in other situations. In interactive mode, bc(1) attempts to recover from errors (see the **RESET** section), and in normal execution, flushes **stdout** as soon as execution is done for the current input. bc(1) may also reset on **SIGINT** instead of exit, depending on the contents of, or default for, the **BC_SIGINT_RESET** environment variable (see the **ENVIRONMENT VARIABLES** section). # TTY MODE If **stdin**, **stdout**, and **stderr** are all connected to a TTY, then "TTY mode" is considered to be available, and thus, bc(1) can turn on TTY mode, subject to some settings. If there is the environment variable **BC_TTY_MODE** in the environment (see the **ENVIRONMENT VARIABLES** section), then if that environment variable contains a non-zero integer, bc(1) will turn on TTY mode when **stdin**, **stdout**, and **stderr** are all connected to a TTY. If the **BC_TTY_MODE** environment variable exists but is *not* a non-zero integer, then bc(1) will not turn TTY mode on. If the environment variable **BC_TTY_MODE** does *not* exist, the default setting is used. The default setting can be queried with the **-h** or **-\-help** options. TTY mode is different from interactive mode because interactive mode is required in the bc(1) standard (see the **STANDARDS** section), and interactive mode requires only **stdin** and **stdout** to be connected to a terminal. ## Prompt If TTY mode is available, then a prompt can be enabled. Like TTY mode itself, it can be turned on or off with an environment variable: **BC_PROMPT** (see the **ENVIRONMENT VARIABLES** section). If the environment variable **BC_PROMPT** exists and is a non-zero integer, then the prompt is turned on when **stdin**, **stdout**, and **stderr** are connected to a TTY and the **-P** and **-\-no-prompt** options were not used. The read prompt will be turned on under the same conditions, except that the **-R** and **-\-no-read-prompt** options must also not be used. However, if **BC_PROMPT** does not exist, the prompt can be enabled or disabled with the **BC_TTY_MODE** environment variable, the **-P** and **-\-no-prompt** options, and the **-R** and **-\-no-read-prompt** options. See the **ENVIRONMENT VARIABLES** and **OPTIONS** sections for more details. # SIGNAL HANDLING Sending a **SIGINT** will cause bc(1) to do one of two things. If bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section), or the **BC_SIGINT_RESET** environment variable (see the **ENVIRONMENT VARIABLES** section), or its default, is either not an integer or it is zero, bc(1) will exit. However, if bc(1) is in interactive mode, and the **BC_SIGINT_RESET** or its default is an integer and non-zero, then bc(1) will stop executing the current input and reset (see the **RESET** section) upon receiving a **SIGINT**. Note that "current input" can mean one of two things. If bc(1) is processing input from **stdin** in interactive mode, it will ask for more input. If bc(1) is processing input from a file in interactive mode, it will stop processing the file and start processing the next file, if one exists, or ask for input from **stdin** if no other file exists. This means that if a **SIGINT** is sent to bc(1) as it is executing a file, it can seem as though bc(1) did not respond to the signal since it will immediately start executing the next file. This is by design; most files that users execute when interacting with bc(1) have function definitions, which are quick to parse. If a file takes a long time to execute, there may be a bug in that file. The rest of the files could still be executed without problem, allowing the user to continue. **SIGTERM** and **SIGQUIT** cause bc(1) to clean up and exit, and it uses the default handler for all other signals. # LOCALES This bc(1) ships with support for adding error messages for different locales and thus, supports **LC_MESSAGES**. # SEE ALSO dc(1) # STANDARDS bc(1) is compliant with the IEEE Std 1003.1-2017 (“POSIX.1-2017”) specification at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html . The flags **-efghiqsvVw**, all long options, and the extensions noted above are extensions to that specification. In addition, the behavior of the **quit** implements an interpretation of that specification that is different from all known implementations. For more information see the **Statements** subsection of the **SYNTAX** section. Note that the specification explicitly says that bc(1) only accepts numbers that use a period (**.**) as a radix point, regardless of the value of **LC_NUMERIC**. This bc(1) supports error messages for different locales, and thus, it supports **LC_MESSAGES**. # BUGS Before version **6.1.0**, this bc(1) had incorrect behavior for the **quit** statement. No other bugs are known. Report bugs at https://git.gavinhoward.com/gavin/bc . # AUTHORS Gavin D. Howard and contributors. diff --git a/contrib/bc/manuals/bc/HN.1 b/contrib/bc/manuals/bc/HN.1 index 3f567180a168..3373fd9ac8c6 100644 --- a/contrib/bc/manuals/bc/HN.1 +++ b/contrib/bc/manuals/bc/HN.1 @@ -1,2967 +1,2911 @@ .\" .\" SPDX-License-Identifier: BSD-2-Clause .\" .\" Copyright (c) 2018-2023 Gavin D. Howard and contributors. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions are met: .\" .\" * Redistributions of source code must retain the above copyright notice, .\" this list of conditions and the following disclaimer. .\" .\" * Redistributions in binary form must reproduce the above copyright notice, .\" this list of conditions and the following disclaimer in the documentation .\" and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.TH "BC" "1" "February 2023" "Gavin D. Howard" "General Commands Manual" +.TH "BC" "1" "November 2023" "Gavin D. Howard" "General Commands Manual" .nh .ad l .SH NAME -.PP bc - arbitrary-precision decimal arithmetic language and calculator .SH SYNOPSIS -.PP \f[B]bc\f[R] [\f[B]-cCghilPqRsvVw\f[R]] [\f[B]--digit-clamp\f[R]] [\f[B]--no-digit-clamp\f[R]] [\f[B]--global-stacks\f[R]] [\f[B]--help\f[R]] [\f[B]--interactive\f[R]] [\f[B]--mathlib\f[R]] [\f[B]--no-prompt\f[R]] [\f[B]--no-read-prompt\f[R]] [\f[B]--quiet\f[R]] [\f[B]--standard\f[R]] [\f[B]--warn\f[R]] [\f[B]--version\f[R]] [\f[B]-e\f[R] \f[I]expr\f[R]] [\f[B]--expression\f[R]=\f[I]expr\f[R]\&...] [\f[B]-f\f[R] \f[I]file\f[R]\&...] [\f[B]--file\f[R]=\f[I]file\f[R]\&...] [\f[I]file\f[R]\&...] [\f[B]-I\f[R] \f[I]ibase\f[R]] [\f[B]--ibase\f[R]=\f[I]ibase\f[R]] [\f[B]-O\f[R] \f[I]obase\f[R]] [\f[B]--obase\f[R]=\f[I]obase\f[R]] [\f[B]-S\f[R] \f[I]scale\f[R]] [\f[B]--scale\f[R]=\f[I]scale\f[R]] [\f[B]-E\f[R] \f[I]seed\f[R]] [\f[B]--seed\f[R]=\f[I]seed\f[R]] .SH DESCRIPTION -.PP bc(1) is an interactive processor for a language first standardized in 1991 by POSIX. (See the \f[B]STANDARDS\f[R] section.) The language provides unlimited precision decimal arithmetic and is somewhat C-like, but there are differences. Such differences will be noted in this document. .PP After parsing and handling options, this bc(1) reads any files given on the command line and executes them before reading from \f[B]stdin\f[R]. .PP This bc(1) is a drop-in replacement for \f[I]any\f[R] bc(1), including (and especially) the GNU bc(1). It also has many extensions and extra features beyond other implementations. .PP \f[B]Note\f[R]: If running this bc(1) on \f[I]any\f[R] script meant for another bc(1) gives a parse error, it is probably because a word this bc(1) reserves as a keyword is used as the name of a function, variable, or array. To fix that, use the command-line option \f[B]-r\f[R] \f[I]keyword\f[R], where \f[I]keyword\f[R] is the keyword that is used as a name in the script. For more information, see the \f[B]OPTIONS\f[R] section. .PP If parsing scripts meant for other bc(1) implementations still does not work, that is a bug and should be reported. See the \f[B]BUGS\f[R] section. .SH OPTIONS -.PP The following are the options that bc(1) accepts. .TP \f[B]-C\f[R], \f[B]--no-digit-clamp\f[R] Disables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that the value added to a number from a digit is always that digit\[cq]s value multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-c\f[R] or \f[B]--digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-c\f[R], \f[B]--digit-clamp\f[R] Enables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that digits that the value added to a number from a digit that is greater than or equal to the ibase is the value of ibase minus 1 all multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-C\f[R] or \f[B]--no-digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-E\f[R] \f[I]seed\f[R], \f[B]--seed\f[R]=\f[I]seed\f[R] Sets the builtin variable \f[B]seed\f[R] to the value \f[I]seed\f[R] assuming that \f[I]seed\f[R] is in base 10. It is a fatal error if \f[I]seed\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-e\f[R] \f[I]expr\f[R], \f[B]--expression\f[R]=\f[I]expr\f[R] Evaluates \f[I]expr\f[R]. If multiple expressions are given, they are evaluated in order. If files are given as well (see the \f[B]-f\f[R] and \f[B]--file\f[R] options), the expressions and files are evaluated in the order given. This means that if a file is given before an expression, the file is read in and evaluated first. .RS .PP If this option is given on the command-line (i.e., not in \f[B]BC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, bc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R], whether on the command-line or in \f[B]BC_ENV_ARGS\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, bc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-f\f[R] \f[I]file\f[R], \f[B]--file\f[R]=\f[I]file\f[R] Reads in \f[I]file\f[R] and evaluates it, line by line, as though it were read through \f[B]stdin\f[R]. If expressions are also given (see the \f[B]-e\f[R] and \f[B]--expression\f[R] options), the expressions are evaluated in the order given. .RS .PP If this option is given on the command-line (i.e., not in \f[B]BC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, bc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, bc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-g\f[R], \f[B]--global-stacks\f[R] Turns the globals \f[B]ibase\f[R], \f[B]obase\f[R], \f[B]scale\f[R], and \f[B]seed\f[R] into stacks. .RS .PP This has the effect that a copy of the current value of all four are pushed onto a stack for every function call, as well as popped when every function returns. This means that functions can assign to any and all of those globals without worrying that the change will affect other functions. Thus, a hypothetical function named \f[B]output(x,b)\f[R] that simply printed \f[B]x\f[R] in base \f[B]b\f[R] could be written like this: .IP -.nf -\f[C] +.EX define void output(x, b) { obase=b x } -\f[R] -.fi +.EE .PP instead of like this: .IP -.nf -\f[C] +.EX define void output(x, b) { auto c c=obase obase=b x obase=c } -\f[R] -.fi +.EE .PP This makes writing functions much easier. .PP (\f[B]Note\f[R]: the function \f[B]output(x,b)\f[R] exists in the extended math library. See the \f[B]LIBRARY\f[R] section.) .PP However, since using this flag means that functions cannot set \f[B]ibase\f[R], \f[B]obase\f[R], \f[B]scale\f[R], or \f[B]seed\f[R] globally, functions that are made to do so cannot work anymore. There are two possible use cases for that, and each has a solution. .PP First, if a function is called on startup to turn bc(1) into a number converter, it is possible to replace that capability with various shell aliases. Examples: .IP -.nf -\f[C] +.EX alias d2o=\[dq]bc -e ibase=A -e obase=8\[dq] alias h2b=\[dq]bc -e ibase=G -e obase=2\[dq] -\f[R] -.fi +.EE .PP Second, if the purpose of a function is to set \f[B]ibase\f[R], \f[B]obase\f[R], \f[B]scale\f[R], or \f[B]seed\f[R] globally for any other purpose, it could be split into one to four functions (based on how many globals it sets) and each of those functions could return the desired value for a global. .PP For functions that set \f[B]seed\f[R], the value assigned to \f[B]seed\f[R] is not propagated to parent functions. This means that the sequence of pseudo-random numbers that they see will not be the same sequence of pseudo-random numbers that any parent sees. This is only the case once \f[B]seed\f[R] has been set. .PP If a function desires to not affect the sequence of pseudo-random numbers of its parents, but wants to use the same \f[B]seed\f[R], it can use the following line: .IP -.nf -\f[C] +.EX seed = seed -\f[R] -.fi +.EE .PP If the behavior of this option is desired for every run of bc(1), then users could make sure to define \f[B]BC_ENV_ARGS\f[R] and include this option (see the \f[B]ENVIRONMENT VARIABLES\f[R] section for more details). .PP If \f[B]-s\f[R], \f[B]-w\f[R], or any equivalents are used, this option is ignored. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-h\f[R], \f[B]--help\f[R] Prints a usage message and exits. .TP \f[B]-I\f[R] \f[I]ibase\f[R], \f[B]--ibase\f[R]=\f[I]ibase\f[R] Sets the builtin variable \f[B]ibase\f[R] to the value \f[I]ibase\f[R] assuming that \f[I]ibase\f[R] is in base 10. It is a fatal error if \f[I]ibase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-i\f[R], \f[B]--interactive\f[R] Forces interactive mode. (See the \f[B]INTERACTIVE MODE\f[R] section.) .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-L\f[R], \f[B]--no-line-length\f[R] Disables line length checking and prints numbers without backslashes and newlines. In other words, this option sets \f[B]BC_LINE_LENGTH\f[R] to \f[B]0\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-l\f[R], \f[B]--mathlib\f[R] Sets \f[B]scale\f[R] (see the \f[B]SYNTAX\f[R] section) to \f[B]20\f[R] and loads the included math library and the extended math library before running any code, including any expressions or files specified on the command line. .RS .PP To learn what is in the libraries, see the \f[B]LIBRARY\f[R] section. .RE .TP \f[B]-O\f[R] \f[I]obase\f[R], \f[B]--obase\f[R]=\f[I]obase\f[R] Sets the builtin variable \f[B]obase\f[R] to the value \f[I]obase\f[R] assuming that \f[I]obase\f[R] is in base 10. It is a fatal error if \f[I]obase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-P\f[R], \f[B]--no-prompt\f[R] Disables the prompt in TTY mode. (The prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a prompt or are not used to having them in bc(1). Most of those users would want to put this option in \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .RS .PP These options override the \f[B]BC_PROMPT\f[R] and \f[B]BC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-q\f[R], \f[B]--quiet\f[R] This option is for compatibility with the GNU bc(1) (https://www.gnu.org/software/bc/); it is a no-op. Without this option, GNU bc(1) prints a copyright header. This bc(1) only prints the copyright header if one or more of the \f[B]-v\f[R], \f[B]-V\f[R], or \f[B]--version\f[R] options are given unless the \f[B]BC_BANNER\f[R] environment variable is set and contains a non-zero integer or if this bc(1) was built with the header displayed by default. If \f[I]any\f[R] of that is the case, then this option \f[I]does\f[R] prevent bc(1) from printing the header. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-R\f[R], \f[B]--no-read-prompt\f[R] Disables the read prompt in TTY mode. (The read prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a read prompt or are not used to having them in bc(1). Most of those users would want to put this option in \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). This option is also useful in hash bang lines of bc(1) scripts that prompt for user input. .RS .PP This option does not disable the regular prompt because the read prompt is only used when the \f[B]read()\f[R] built-in function is called. .PP These options \f[I]do\f[R] override the \f[B]BC_PROMPT\f[R] and \f[B]BC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), but only for the read prompt. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-r\f[R] \f[I]keyword\f[R], \f[B]--redefine\f[R]=\f[I]keyword\f[R] Redefines \f[I]keyword\f[R] in order to allow it to be used as a function, variable, or array name. This is useful when this bc(1) gives parse errors when parsing scripts meant for other bc(1) implementations. .RS .PP The keywords this bc(1) allows to be redefined are: .IP \[bu] 2 \f[B]abs\f[R] .IP \[bu] 2 \f[B]asciify\f[R] .IP \[bu] 2 \f[B]continue\f[R] .IP \[bu] 2 \f[B]divmod\f[R] .IP \[bu] 2 \f[B]else\f[R] .IP \[bu] 2 \f[B]halt\f[R] .IP \[bu] 2 \f[B]irand\f[R] .IP \[bu] 2 \f[B]last\f[R] .IP \[bu] 2 \f[B]limits\f[R] .IP \[bu] 2 \f[B]maxibase\f[R] .IP \[bu] 2 \f[B]maxobase\f[R] .IP \[bu] 2 \f[B]maxrand\f[R] .IP \[bu] 2 \f[B]maxscale\f[R] .IP \[bu] 2 \f[B]modexp\f[R] .IP \[bu] 2 \f[B]print\f[R] .IP \[bu] 2 \f[B]rand\f[R] .IP \[bu] 2 \f[B]read\f[R] .IP \[bu] 2 \f[B]seed\f[R] .IP \[bu] 2 \f[B]stream\f[R] .PP If any of those keywords are used as a function, variable, or array name in a script, use this option with the keyword as the argument. If multiple are used, use this option for all of them; it can be used multiple times. .PP Keywords are \f[I]not\f[R] redefined when parsing the builtin math library (see the \f[B]LIBRARY\f[R] section). .PP It is a fatal error to redefine keywords mandated by the POSIX standard (see the \f[B]STANDARDS\f[R] section). It is a fatal error to attempt to redefine words that this bc(1) does not reserve as keywords. .RE .TP \f[B]-S\f[R] \f[I]scale\f[R], \f[B]--scale\f[R]=\f[I]scale\f[R] Sets the builtin variable \f[B]scale\f[R] to the value \f[I]scale\f[R] assuming that \f[I]scale\f[R] is in base 10. It is a fatal error if \f[I]scale\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-s\f[R], \f[B]--standard\f[R] Process exactly the language defined by the standard (see the \f[B]STANDARDS\f[R] section) and error if any extensions are used. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-v\f[R], \f[B]-V\f[R], \f[B]--version\f[R] Print the version information (copyright header) and exits. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-w\f[R], \f[B]--warn\f[R] Like \f[B]-s\f[R] and \f[B]--standard\f[R], except that warnings (and not errors) are printed for non-standard extensions and execution continues normally. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-z\f[R], \f[B]--leading-zeroes\f[R] Makes bc(1) print all numbers greater than \f[B]-1\f[R] and less than \f[B]1\f[R], and not equal to \f[B]0\f[R], with a leading zero. .RS .PP This can be set for individual numbers with the \f[B]plz(x)\f[R], \f[B]plznl(x)\f[R], \f[B]pnlz(x)\f[R], and \f[B]pnlznl(x)\f[R] functions in the extended math library (see the \f[B]LIBRARY\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .PP All long options are \f[B]non-portable extensions\f[R]. .SH STDIN -.PP If no files or expressions are given by the \f[B]-f\f[R], \f[B]--file\f[R], \f[B]-e\f[R], or \f[B]--expression\f[R] options, then bc(1) reads from \f[B]stdin\f[R]. .PP However, there are a few caveats to this. .PP First, \f[B]stdin\f[R] is evaluated a line at a time. The only exception to this is if the parse cannot complete. That means that starting a string without ending it or starting a function, \f[B]if\f[R] statement, or loop without ending it will also cause bc(1) to not execute. .PP Second, after an \f[B]if\f[R] statement, bc(1) doesn\[cq]t know if an \f[B]else\f[R] statement will follow, so it will not execute until it knows there will not be an \f[B]else\f[R] statement. .SH STDOUT -.PP Any non-error output is written to \f[B]stdout\f[R]. In addition, if history (see the \f[B]HISTORY\f[R] section) and the prompt (see the \f[B]TTY MODE\f[R] section) are enabled, both are output to \f[B]stdout\f[R]. .PP \f[B]Note\f[R]: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stdout\f[R], so if \f[B]stdout\f[R] is closed, as in -\f[B]bc >&-\f[R], it will quit with an error. +\f[B]bc >&-\f[R], it will quit with an error. This is done so that bc(1) can report problems when \f[B]stdout\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stdout\f[R] to \f[B]/dev/null\f[R]. .SH STDERR -.PP Any error output is written to \f[B]stderr\f[R]. .PP \f[B]Note\f[R]: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stderr\f[R], so if \f[B]stderr\f[R] is closed, as in -\f[B]bc 2>&-\f[R], it will quit with an error. +\f[B]bc 2>&-\f[R], it will quit with an error. This is done so that bc(1) can exit with an error code when \f[B]stderr\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stderr\f[R] to \f[B]/dev/null\f[R]. .SH SYNTAX -.PP The syntax for bc(1) programs is mostly C-like, with some differences. This bc(1) follows the POSIX standard (see the \f[B]STANDARDS\f[R] section), which is a much more thorough resource for the language this bc(1) accepts. This section is meant to be a summary and a listing of all the extensions to the standard. .PP In the sections below, \f[B]E\f[R] means expression, \f[B]S\f[R] means statement, and \f[B]I\f[R] means identifier. .PP Identifiers (\f[B]I\f[R]) start with a lowercase letter and can be followed by any number (up to \f[B]BC_NAME_MAX-1\f[R]) of lowercase letters (\f[B]a-z\f[R]), digits (\f[B]0-9\f[R]), and underscores (\f[B]_\f[R]). The regex is \f[B][a-z][a-z0-9_]*\f[R]. Identifiers with more than one character (letter) are a \f[B]non-portable extension\f[R]. .PP \f[B]ibase\f[R] is a global variable determining how to interpret constant numbers. It is the \[lq]input\[rq] base, or the number base used for interpreting input numbers. \f[B]ibase\f[R] is initially \f[B]10\f[R]. If the \f[B]-s\f[R] (\f[B]--standard\f[R]) and \f[B]-w\f[R] (\f[B]--warn\f[R]) flags were not given on the command line, the max allowable value for \f[B]ibase\f[R] is \f[B]36\f[R]. Otherwise, it is \f[B]16\f[R]. The min allowable value for \f[B]ibase\f[R] is \f[B]2\f[R]. The max allowable value for \f[B]ibase\f[R] can be queried in bc(1) programs with the \f[B]maxibase()\f[R] built-in function. .PP \f[B]obase\f[R] is a global variable determining how to output results. It is the \[lq]output\[rq] base, or the number base used for outputting numbers. \f[B]obase\f[R] is initially \f[B]10\f[R]. The max allowable value for \f[B]obase\f[R] is \f[B]BC_BASE_MAX\f[R] and can be queried in bc(1) programs with the \f[B]maxobase()\f[R] built-in function. The min allowable value for \f[B]obase\f[R] is \f[B]0\f[R]. If \f[B]obase\f[R] is \f[B]0\f[R], values are output in scientific notation, and if \f[B]obase\f[R] is \f[B]1\f[R], values are output in engineering notation. Otherwise, values are output in the specified base. .PP Outputting in scientific and engineering notations are \f[B]non-portable extensions\f[R]. .PP The \f[I]scale\f[R] of an expression is the number of digits in the result of the expression right of the decimal point, and \f[B]scale\f[R] is a global variable that sets the precision of any operations, with exceptions. \f[B]scale\f[R] is initially \f[B]0\f[R]. \f[B]scale\f[R] cannot be negative. The max allowable value for \f[B]scale\f[R] is \f[B]BC_SCALE_MAX\f[R] and can be queried in bc(1) programs with the \f[B]maxscale()\f[R] built-in function. .PP bc(1) has both \f[I]global\f[R] variables and \f[I]local\f[R] variables. All \f[I]local\f[R] variables are local to the function; they are parameters or are introduced in the \f[B]auto\f[R] list of a function (see the \f[B]FUNCTIONS\f[R] section). If a variable is accessed which is not a parameter or in the \f[B]auto\f[R] list, it is assumed to be \f[I]global\f[R]. If a parent function has a \f[I]local\f[R] variable version of a variable that a child function considers \f[I]global\f[R], the value of that \f[I]global\f[R] variable in the child function is the value of the variable in the parent function, not the value of the actual \f[I]global\f[R] variable. .PP All of the above applies to arrays as well. .PP The value of a statement that is an expression (i.e., any of the named expressions or operands) is printed unless the lowest precedence operator is an assignment operator \f[I]and\f[R] the expression is notsurrounded by parentheses. .PP The value that is printed is also assigned to the special variable \f[B]last\f[R]. A single dot (\f[B].\f[R]) may also be used as a synonym for \f[B]last\f[R]. These are \f[B]non-portable extensions\f[R]. .PP Either semicolons or newlines may separate statements. .SS Comments -.PP There are two kinds of comments: .IP "1." 3 Block comments are enclosed in \f[B]/*\f[R] and \f[B]*/\f[R]. .IP "2." 3 Line comments go from \f[B]#\f[R] until, and not including, the next newline. This is a \f[B]non-portable extension\f[R]. .SS Named Expressions -.PP The following are named expressions in bc(1): .IP "1." 3 Variables: \f[B]I\f[R] .IP "2." 3 Array Elements: \f[B]I[E]\f[R] .IP "3." 3 \f[B]ibase\f[R] .IP "4." 3 \f[B]obase\f[R] .IP "5." 3 \f[B]scale\f[R] .IP "6." 3 \f[B]seed\f[R] .IP "7." 3 \f[B]last\f[R] or a single dot (\f[B].\f[R]) .PP Numbers 6 and 7 are \f[B]non-portable extensions\f[R]. .PP The meaning of \f[B]seed\f[R] is dependent on the current pseudo-random number generator but is guaranteed to not change except for new major versions. .PP The \f[I]scale\f[R] and sign of the value may be significant. .PP If a previously used \f[B]seed\f[R] value is assigned to \f[B]seed\f[R] and used again, the pseudo-random number generator is guaranteed to produce the same sequence of pseudo-random numbers as it did when the \f[B]seed\f[R] value was previously used. .PP The exact value assigned to \f[B]seed\f[R] is not guaranteed to be returned if \f[B]seed\f[R] is queried again immediately. However, if \f[B]seed\f[R] \f[I]does\f[R] return a different value, both values, when assigned to \f[B]seed\f[R], are guaranteed to produce the same sequence of pseudo-random numbers. This means that certain values assigned to \f[B]seed\f[R] will \f[I]not\f[R] produce unique sequences of pseudo-random numbers. The value of \f[B]seed\f[R] will change after any use of the \f[B]rand()\f[R] and \f[B]irand(E)\f[R] operands (see the \f[I]Operands\f[R] subsection below), except if the parameter passed to \f[B]irand(E)\f[R] is \f[B]0\f[R], \f[B]1\f[R], or negative. .PP There is no limit to the length (number of significant decimal digits) or \f[I]scale\f[R] of the value that can be assigned to \f[B]seed\f[R]. .PP Variables and arrays do not interfere; users can have arrays named the same as variables. This also applies to functions (see the \f[B]FUNCTIONS\f[R] section), so a user can have a variable, array, and function that all have the same name, and they will not shadow each other, whether inside of functions or not. .PP Named expressions are required as the operand of \f[B]increment\f[R]/\f[B]decrement\f[R] operators and as the left side of \f[B]assignment\f[R] operators (see the \f[I]Operators\f[R] subsection). .SS Operands -.PP The following are valid operands in bc(1): .IP " 1." 4 Numbers (see the \f[I]Numbers\f[R] subsection below). .IP " 2." 4 Array indices (\f[B]I[E]\f[R]). .IP " 3." 4 \f[B](E)\f[R]: The value of \f[B]E\f[R] (used to change precedence). .IP " 4." 4 \f[B]sqrt(E)\f[R]: The square root of \f[B]E\f[R]. \f[B]E\f[R] must be non-negative. .IP " 5." 4 \f[B]length(E)\f[R]: The number of significant decimal digits in \f[B]E\f[R]. Returns \f[B]1\f[R] for \f[B]0\f[R] with no decimal places. If given a string, the length of the string is returned. Passing a string to \f[B]length(E)\f[R] is a \f[B]non-portable extension\f[R]. .IP " 6." 4 \f[B]length(I[])\f[R]: The number of elements in the array \f[B]I\f[R]. This is a \f[B]non-portable extension\f[R]. .IP " 7." 4 \f[B]scale(E)\f[R]: The \f[I]scale\f[R] of \f[B]E\f[R]. .IP " 8." 4 \f[B]abs(E)\f[R]: The absolute value of \f[B]E\f[R]. This is a \f[B]non-portable extension\f[R]. .IP " 9." 4 \f[B]is_number(E)\f[R]: \f[B]1\f[R] if the given argument is a number, \f[B]0\f[R] if it is a string. This is a \f[B]non-portable extension\f[R]. .IP "10." 4 \f[B]is_string(E)\f[R]: \f[B]1\f[R] if the given argument is a string, \f[B]0\f[R] if it is a number. This is a \f[B]non-portable extension\f[R]. .IP "11." 4 \f[B]modexp(E, E, E)\f[R]: Modular exponentiation, where the first expression is the base, the second is the exponent, and the third is the modulus. All three values must be integers. The second argument must be non-negative. The third argument must be non-zero. This is a \f[B]non-portable extension\f[R]. .IP "12." 4 \f[B]divmod(E, E, I[])\f[R]: Division and modulus in one operation. This is for optimization. The first expression is the dividend, and the second is the divisor, which must be non-zero. The return value is the quotient, and the modulus is stored in index \f[B]0\f[R] of the provided array (the last argument). This is a \f[B]non-portable extension\f[R]. .IP "13." 4 \f[B]asciify(E)\f[R]: If \f[B]E\f[R] is a string, returns a string that is the first letter of its argument. If it is a number, calculates the number mod \f[B]256\f[R] and returns that number as a one-character string. This is a \f[B]non-portable extension\f[R]. .IP "14." 4 \f[B]asciify(I[])\f[R]: A string that is made up of the characters that would result from running \f[B]asciify(E)\f[R] on each element of the array identified by the argument. This allows creating multi-character strings and storing them. This is a \f[B]non-portable extension\f[R]. .IP "15." 4 \f[B]I()\f[R], \f[B]I(E)\f[R], \f[B]I(E, E)\f[R], and so on, where \f[B]I\f[R] is an identifier for a non-\f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). The \f[B]E\f[R] argument(s) may also be arrays of the form \f[B]I[]\f[R], which will automatically be turned into array references (see the \f[I]Array References\f[R] subsection of the \f[B]FUNCTIONS\f[R] section) if the corresponding parameter in the function definition is an array reference. .IP "16." 4 \f[B]read()\f[R]: Reads a line from \f[B]stdin\f[R] and uses that as an expression. The result of that expression is the result of the \f[B]read()\f[R] operand. This is a \f[B]non-portable extension\f[R]. .IP "17." 4 \f[B]maxibase()\f[R]: The max allowable \f[B]ibase\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "18." 4 \f[B]maxobase()\f[R]: The max allowable \f[B]obase\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "19." 4 \f[B]maxscale()\f[R]: The max allowable \f[B]scale\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "20." 4 \f[B]line_length()\f[R]: The line length set with \f[B]BC_LINE_LENGTH\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). This is a \f[B]non-portable extension\f[R]. .IP "21." 4 \f[B]global_stacks()\f[R]: \f[B]0\f[R] if global stacks are not enabled with the \f[B]-g\f[R] or \f[B]--global-stacks\f[R] options, non-zero otherwise. See the \f[B]OPTIONS\f[R] section. This is a \f[B]non-portable extension\f[R]. .IP "22." 4 \f[B]leading_zero()\f[R]: \f[B]0\f[R] if leading zeroes are not enabled with the \f[B]-z\f[R] or \f[B]\[en]leading-zeroes\f[R] options, non-zero otherwise. See the \f[B]OPTIONS\f[R] section. This is a \f[B]non-portable extension\f[R]. .IP "23." 4 \f[B]rand()\f[R]: A pseudo-random integer between \f[B]0\f[R] (inclusive) and \f[B]BC_RAND_MAX\f[R] (inclusive). Using this operand will change the value of \f[B]seed\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "24." 4 \f[B]irand(E)\f[R]: A pseudo-random integer between \f[B]0\f[R] (inclusive) and the value of \f[B]E\f[R] (exclusive). If \f[B]E\f[R] is negative or is a non-integer (\f[B]E\f[R]\[cq]s \f[I]scale\f[R] is not \f[B]0\f[R]), an error is raised, and bc(1) resets (see the \f[B]RESET\f[R] section) while \f[B]seed\f[R] remains unchanged. If \f[B]E\f[R] is larger than \f[B]BC_RAND_MAX\f[R], the higher bound is honored by generating several pseudo-random integers, multiplying them by appropriate powers of \f[B]BC_RAND_MAX+1\f[R], and adding them together. Thus, the size of integer that can be generated with this operand is unbounded. Using this operand will change the value of \f[B]seed\f[R], unless the value of \f[B]E\f[R] is \f[B]0\f[R] or \f[B]1\f[R]. In that case, \f[B]0\f[R] is returned, and \f[B]seed\f[R] is \f[I]not\f[R] changed. This is a \f[B]non-portable extension\f[R]. .IP "25." 4 \f[B]maxrand()\f[R]: The max integer returned by \f[B]rand()\f[R]. This is a \f[B]non-portable extension\f[R]. .PP The integers generated by \f[B]rand()\f[R] and \f[B]irand(E)\f[R] are guaranteed to be as unbiased as possible, subject to the limitations of the pseudo-random number generator. .PP \f[B]Note\f[R]: The values returned by the pseudo-random number generator with \f[B]rand()\f[R] and \f[B]irand(E)\f[R] are guaranteed to \f[I]NOT\f[R] be cryptographically secure. This is a consequence of using a seeded pseudo-random number generator. However, they \f[I]are\f[R] guaranteed to be reproducible with identical \f[B]seed\f[R] values. This means that the pseudo-random values from bc(1) should only be used where a reproducible stream of pseudo-random numbers is \f[I]ESSENTIAL\f[R]. In any other case, use a non-seeded pseudo-random number generator. .SS Numbers -.PP Numbers are strings made up of digits, uppercase letters, and at most \f[B]1\f[R] period for a radix. Numbers can have up to \f[B]BC_NUM_MAX\f[R] digits. Uppercase letters are equal to \f[B]9\f[R] plus their position in the alphabet, starting from \f[B]1\f[R] (i.e., \f[B]A\f[R] equals \f[B]10\f[R], or \f[B]9+1\f[R]). .PP If a digit or letter makes no sense with the current value of \f[B]ibase\f[R] (i.e., they are greater than or equal to the current value of \f[B]ibase\f[R]), then the behavior depends on the existence of the \f[B]-c\f[R]/\f[B]--digit-clamp\f[R] or \f[B]-C\f[R]/\f[B]--no-digit-clamp\f[R] options (see the \f[B]OPTIONS\f[R] section), the existence and setting of the \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or the default, which can be queried with the \f[B]-h\f[R]/\f[B]--help\f[R] option. .PP If clamping is off, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are not changed. Instead, their given value is multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*A+3\[ha]0*B\f[R], which is \f[B]3\f[R] times \f[B]10\f[R] plus \f[B]11\f[R], or \f[B]41\f[R]. .PP If clamping is on, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are set to the value of the highest valid digit in \f[B]ibase\f[R] before being multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*2+3\[ha]0*2\f[R], which is \f[B]3\f[R] times \f[B]2\f[R] plus \f[B]2\f[R], or \f[B]8\f[R]. .PP There is one exception to clamping: single-character numbers (i.e., \f[B]A\f[R] alone). Such numbers are never clamped and always take the value they would have in the highest possible \f[B]ibase\f[R]. This means that \f[B]A\f[R] alone always equals decimal \f[B]10\f[R] and \f[B]Z\f[R] alone always equals decimal \f[B]35\f[R]. This behavior is mandated by the standard (see the STANDARDS section) and is meant to provide an easy way to set the current \f[B]ibase\f[R] (with the \f[B]i\f[R] command) regardless of the current value of \f[B]ibase\f[R]. .PP If clamping is on, and the clamped value of a character is needed, use a leading zero, i.e., for \f[B]A\f[R], use \f[B]0A\f[R]. .PP In addition, bc(1) accepts numbers in scientific notation. These have the form \f[B]e\f[R]. The exponent (the portion after the \f[B]e\f[R]) must be an integer. An example is \f[B]1.89237e9\f[R], which is equal to \f[B]1892370000\f[R]. Negative exponents are also allowed, so \f[B]4.2890e-3\f[R] is equal to \f[B]0.0042890\f[R]. .PP Using scientific notation is an error or warning if the \f[B]-s\f[R] or \f[B]-w\f[R], respectively, command-line options (or equivalents) are given. .PP \f[B]WARNING\f[R]: Both the number and the exponent in scientific notation are interpreted according to the current \f[B]ibase\f[R], but the number is still multiplied by \f[B]10\[ha]exponent\f[R] regardless of the current \f[B]ibase\f[R]. For example, if \f[B]ibase\f[R] is \f[B]16\f[R] and bc(1) is given the number string \f[B]FFeA\f[R], the resulting decimal number will be \f[B]2550000000000\f[R], and if bc(1) is given the number string \f[B]10e-4\f[R], the resulting decimal number will be \f[B]0.0016\f[R]. .PP Accepting input as scientific notation is a \f[B]non-portable extension\f[R]. .SS Operators -.PP The following arithmetic and logical operators can be used. They are listed in order of decreasing precedence. Operators in the same group have the same precedence. .TP \f[B]++\f[R] \f[B]--\f[R] Type: Prefix and Postfix .RS .PP Associativity: None .PP Description: \f[B]increment\f[R], \f[B]decrement\f[R] .RE .TP \f[B]-\f[R] \f[B]!\f[R] Type: Prefix .RS .PP Associativity: None .PP Description: \f[B]negation\f[R], \f[B]boolean not\f[R] .RE .TP \f[B]$\f[R] Type: Postfix .RS .PP Associativity: None .PP Description: \f[B]truncation\f[R] .RE .TP \f[B]\[at]\f[R] Type: Binary .RS .PP Associativity: Right .PP Description: \f[B]set precision\f[R] .RE .TP \f[B]\[ha]\f[R] Type: Binary .RS .PP Associativity: Right .PP Description: \f[B]power\f[R] .RE .TP \f[B]*\f[R] \f[B]/\f[R] \f[B]%\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]multiply\f[R], \f[B]divide\f[R], \f[B]modulus\f[R] .RE .TP \f[B]+\f[R] \f[B]-\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]add\f[R], \f[B]subtract\f[R] .RE .TP \f[B]<<\f[R] \f[B]>>\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]shift left\f[R], \f[B]shift right\f[R] .RE .TP \f[B]=\f[R] \f[B]<<=\f[R] \f[B]>>=\f[R] \f[B]+=\f[R] \f[B]-=\f[R] \f[B]*=\f[R] \f[B]/=\f[R] \f[B]%=\f[R] \f[B]\[ha]=\f[R] \f[B]\[at]=\f[R] Type: Binary .RS .PP Associativity: Right .PP Description: \f[B]assignment\f[R] .RE .TP \f[B]==\f[R] \f[B]<=\f[R] \f[B]>=\f[R] \f[B]!=\f[R] \f[B]<\f[R] \f[B]>\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]relational\f[R] .RE .TP \f[B]&&\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]boolean and\f[R] .RE .TP \f[B]||\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]boolean or\f[R] .RE .PP The operators will be described in more detail below. .TP \f[B]++\f[R] \f[B]--\f[R] The prefix and postfix \f[B]increment\f[R] and \f[B]decrement\f[R] -operators behave exactly like they would in C. -They require a named expression (see the \f[I]Named Expressions\f[R] -subsection) as an operand. +operators behave exactly like they would in C. They require a named +expression (see the \f[I]Named Expressions\f[R] subsection) as an +operand. .RS .PP The prefix versions of these operators are more efficient; use them where possible. .RE .TP \f[B]-\f[R] The \f[B]negation\f[R] operator returns \f[B]0\f[R] if a user attempts to negate any expression with the value \f[B]0\f[R]. Otherwise, a copy of the expression with its sign flipped is returned. .TP \f[B]!\f[R] The \f[B]boolean not\f[R] operator returns \f[B]1\f[R] if the expression is \f[B]0\f[R], or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]$\f[R] The \f[B]truncation\f[R] operator returns a copy of the given expression with all of its \f[I]scale\f[R] removed. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[at]\f[R] The \f[B]set precision\f[R] operator takes two expressions and returns a copy of the first with its \f[I]scale\f[R] equal to the value of the second expression. That could either mean that the number is returned without change (if the \f[I]scale\f[R] of the first expression matches the value of the second expression), extended (if it is less), or truncated (if it is more). .RS .PP The second expression must be an integer (no \f[I]scale\f[R]) and non-negative. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[ha]\f[R] The \f[B]power\f[R] operator (not the \f[B]exclusive or\f[R] operator, as it would be in C) takes two expressions and raises the first to the power of the value of the second. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The second expression must be an integer (no \f[I]scale\f[R]), and if it is negative, the first value must be non-zero. .RE .TP \f[B]*\f[R] The \f[B]multiply\f[R] operator takes two expressions, multiplies them, and returns the product. If \f[B]a\f[R] is the \f[I]scale\f[R] of the first expression and \f[B]b\f[R] is the \f[I]scale\f[R] of the second expression, the \f[I]scale\f[R] of the result is equal to \f[B]min(a+b,max(scale,a,b))\f[R] where \f[B]min()\f[R] and \f[B]max()\f[R] return the obvious values. .TP \f[B]/\f[R] The \f[B]divide\f[R] operator takes two expressions, divides them, and returns the quotient. The \f[I]scale\f[R] of the result shall be the value of \f[B]scale\f[R]. .RS .PP The second expression must be non-zero. .RE .TP \f[B]%\f[R] The \f[B]modulus\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and evaluates them by 1) Computing \f[B]a/b\f[R] to current \f[B]scale\f[R] and 2) Using the result of step 1 to calculate \f[B]a-(a/b)*b\f[R] to \f[I]scale\f[R] \f[B]max(scale+scale(b),scale(a))\f[R]. .RS .PP The second expression must be non-zero. .RE .TP \f[B]+\f[R] The \f[B]add\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and returns the sum, with a \f[I]scale\f[R] equal to the max of the \f[I]scale\f[R]s of \f[B]a\f[R] and \f[B]b\f[R]. .TP \f[B]-\f[R] The \f[B]subtract\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and returns the difference, with a \f[I]scale\f[R] equal to the max of the \f[I]scale\f[R]s of \f[B]a\f[R] and \f[B]b\f[R]. .TP \f[B]<<\f[R] The \f[B]left shift\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and returns a copy of the value of \f[B]a\f[R] with its decimal point moved \f[B]b\f[R] places to the right. .RS .PP The second expression must be an integer (no \f[I]scale\f[R]) and non-negative. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]>>\f[R] The \f[B]right shift\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and returns a copy of the value of \f[B]a\f[R] with its decimal point moved \f[B]b\f[R] places to the left. .RS .PP The second expression must be an integer (no \f[I]scale\f[R]) and non-negative. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]=\f[R] \f[B]<<=\f[R] \f[B]>>=\f[R] \f[B]+=\f[R] \f[B]-=\f[R] \f[B]*=\f[R] \f[B]/=\f[R] \f[B]%=\f[R] \f[B]\[ha]=\f[R] \f[B]\[at]=\f[R] The \f[B]assignment\f[R] operators take two expressions, \f[B]a\f[R] and \f[B]b\f[R] where \f[B]a\f[R] is a named expression (see the \f[I]Named Expressions\f[R] subsection). .RS .PP For \f[B]=\f[R], \f[B]b\f[R] is copied and the result is assigned to \f[B]a\f[R]. For all others, \f[B]a\f[R] and \f[B]b\f[R] are applied as operands to the corresponding arithmetic operator and the result is assigned to \f[B]a\f[R]. .PP The \f[B]assignment\f[R] operators that correspond to operators that are extensions are themselves \f[B]non-portable extensions\f[R]. .RE .TP \f[B]==\f[R] \f[B]<=\f[R] \f[B]>=\f[R] \f[B]!=\f[R] \f[B]<\f[R] \f[B]>\f[R] The \f[B]relational\f[R] operators compare two expressions, \f[B]a\f[R] and \f[B]b\f[R], and if the relation holds, according to C language semantics, the result is \f[B]1\f[R]. Otherwise, it is \f[B]0\f[R]. .RS .PP Note that unlike in C, these operators have a lower precedence than the \f[B]assignment\f[R] operators, which means that \f[B]a=b>c\f[R] is interpreted as \f[B](a=b)>c\f[R]. .PP Also, unlike the standard (see the \f[B]STANDARDS\f[R] section) requires, these operators can appear anywhere any other expressions can be used. This allowance is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]&&\f[R] The \f[B]boolean and\f[R] operator takes two expressions and returns \f[B]1\f[R] if both expressions are non-zero, \f[B]0\f[R] otherwise. .RS .PP This is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]||\f[R] The \f[B]boolean or\f[R] operator takes two expressions and returns \f[B]1\f[R] if one of the expressions is non-zero, \f[B]0\f[R] otherwise. .RS .PP This is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Statements -.PP The following items are statements: .IP " 1." 4 \f[B]E\f[R] .IP " 2." 4 \f[B]{\f[R] \f[B]S\f[R] \f[B];\f[R] \&... \f[B];\f[R] \f[B]S\f[R] \f[B]}\f[R] .IP " 3." 4 \f[B]if\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] .IP " 4." 4 \f[B]if\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] \f[B]else\f[R] \f[B]S\f[R] .IP " 5." 4 \f[B]while\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] .IP " 6." 4 \f[B]for\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B];\f[R] \f[B]E\f[R] \f[B];\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] .IP " 7." 4 An empty statement .IP " 8." 4 \f[B]break\f[R] .IP " 9." 4 \f[B]continue\f[R] .IP "10." 4 \f[B]quit\f[R] .IP "11." 4 \f[B]halt\f[R] .IP "12." 4 \f[B]limits\f[R] .IP "13." 4 A string of characters, enclosed in double quotes .IP "14." 4 \f[B]print\f[R] \f[B]E\f[R] \f[B],\f[R] \&... \f[B],\f[R] \f[B]E\f[R] .IP "15." 4 \f[B]stream\f[R] \f[B]E\f[R] \f[B],\f[R] \&... \f[B],\f[R] \f[B]E\f[R] .IP "16." 4 \f[B]I()\f[R], \f[B]I(E)\f[R], \f[B]I(E, E)\f[R], and so on, where \f[B]I\f[R] is an identifier for a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). The \f[B]E\f[R] argument(s) may also be arrays of the form \f[B]I[]\f[R], which will automatically be turned into array references (see the \f[I]Array References\f[R] subsection of the \f[B]FUNCTIONS\f[R] section) if the corresponding parameter in the function definition is an array reference. .PP Numbers 4, 9, 11, 12, 14, 15, and 16 are \f[B]non-portable extensions\f[R]. .PP Also, as a \f[B]non-portable extension\f[R], any or all of the expressions in the header of a for loop may be omitted. If the condition (second expression) is omitted, it is assumed to be a constant \f[B]1\f[R]. .PP The \f[B]break\f[R] statement causes a loop to stop iterating and resume execution immediately following a loop. This is only allowed in loops. .PP The \f[B]continue\f[R] statement causes a loop iteration to stop early and returns to the start of the loop, including testing the loop condition. This is only allowed in loops. .PP The \f[B]if\f[R] \f[B]else\f[R] statement does the same thing as in C. .PP The \f[B]quit\f[R] statement causes bc(1) to quit, even if it is on a branch that will not be executed (it is a compile-time command). .PP \f[B]Warning\f[R]: The behavior of this bc(1) on \f[B]quit\f[R] is slightly different from other bc(1) implementations. Other bc(1) implementations will exit as soon as they finish parsing the line that a \f[B]quit\f[R] command is on. This bc(1) will execute any completed and executable statements that occur before the \f[B]quit\f[R] statement before exiting. .PP In other words, for the bc(1) code below: .IP -.nf -\f[C] +.EX for (i = 0; i < 3; ++i) i; quit -\f[R] -.fi +.EE .PP Other bc(1) implementations will print nothing, and this bc(1) will print \f[B]0\f[R], \f[B]1\f[R], and \f[B]2\f[R] on successive lines before exiting. .PP The \f[B]halt\f[R] statement causes bc(1) to quit, if it is executed. (Unlike \f[B]quit\f[R] if it is on a branch of an \f[B]if\f[R] statement that is not executed, bc(1) does not quit.) .PP The \f[B]limits\f[R] statement prints the limits that this bc(1) is subject to. This is like the \f[B]quit\f[R] statement in that it is a compile-time command. .PP An expression by itself is evaluated and printed, followed by a newline. .PP Both scientific notation and engineering notation are available for printing the results of expressions. Scientific notation is activated by assigning \f[B]0\f[R] to \f[B]obase\f[R], and engineering notation is activated by assigning \f[B]1\f[R] to \f[B]obase\f[R]. To deactivate them, just assign a different value to \f[B]obase\f[R]. .PP Scientific notation and engineering notation are disabled if bc(1) is run with either the \f[B]-s\f[R] or \f[B]-w\f[R] command-line options (or equivalents). .PP Printing numbers in scientific notation and/or engineering notation is a \f[B]non-portable extension\f[R]. .SS Strings -.PP If strings appear as a statement by themselves, they are printed without a trailing newline. .PP In addition to appearing as a lone statement by themselves, strings can be assigned to variables and array elements. They can also be passed to functions in variable parameters. .PP If any statement that expects a string is given a variable that had a string assigned to it, the statement acts as though it had received a string. .PP If any math operation is attempted on a string or a variable or array element that has been assigned a string, an error is raised, and bc(1) resets (see the \f[B]RESET\f[R] section). .PP Assigning strings to variables and array elements and passing them to functions are \f[B]non-portable extensions\f[R]. .SS Print Statement -.PP The \[lq]expressions\[rq] in a \f[B]print\f[R] statement may also be strings. If they are, there are backslash escape sequences that are interpreted specially. What those sequences are, and what they cause to be printed, are shown below: .PP \f[B]\[rs]a\f[R]: \f[B]\[rs]a\f[R] .PP \f[B]\[rs]b\f[R]: \f[B]\[rs]b\f[R] .PP \f[B]\[rs]\[rs]\f[R]: \f[B]\[rs]\f[R] .PP \f[B]\[rs]e\f[R]: \f[B]\[rs]\f[R] .PP \f[B]\[rs]f\f[R]: \f[B]\[rs]f\f[R] .PP \f[B]\[rs]n\f[R]: \f[B]\[rs]n\f[R] .PP \f[B]\[rs]q\f[R]: \f[B]\[lq]\f[R] .PP \f[B]\[rs]r\f[R]: \f[B]\[rs]r\f[R] .PP \f[B]\[rs]t\f[R]: \f[B]\[rs]t\f[R] .PP Any other character following a backslash causes the backslash and character to be printed as-is. .PP Any non-string expression in a print statement shall be assigned to \f[B]last\f[R], like any other expression that is printed. .SS Stream Statement -.PP The expressions in a \f[B]stream\f[R] statement may also be strings. .PP If a \f[B]stream\f[R] statement is given a string, it prints the string as though the string had appeared as its own statement. In other words, the \f[B]stream\f[R] statement prints strings normally, without a newline. .PP If a \f[B]stream\f[R] statement is given a number, a copy of it is truncated and its absolute value is calculated. The result is then printed as though \f[B]obase\f[R] is \f[B]256\f[R] and each digit is interpreted as an 8-bit ASCII character, making it a byte stream. .SS Order of Evaluation -.PP All expressions in a statment are evaluated left to right, except as necessary to maintain order of operations. This means, for example, assuming that \f[B]i\f[R] is equal to \f[B]0\f[R], in the expression .IP -.nf -\f[C] +.EX a[i++] = i++ -\f[R] -.fi +.EE .PP the first (or 0th) element of \f[B]a\f[R] is set to \f[B]1\f[R], and \f[B]i\f[R] is equal to \f[B]2\f[R] at the end of the expression. .PP This includes function arguments. Thus, assuming \f[B]i\f[R] is equal to \f[B]0\f[R], this means that in the expression .IP -.nf -\f[C] +.EX x(i++, i++) -\f[R] -.fi +.EE .PP the first argument passed to \f[B]x()\f[R] is \f[B]0\f[R], and the second argument is \f[B]1\f[R], while \f[B]i\f[R] is equal to \f[B]2\f[R] before the function starts executing. .SH FUNCTIONS -.PP Function definitions are as follows: .IP -.nf -\f[C] +.EX define I(I,...,I){ auto I,...,I S;...;S return(E) } -\f[R] -.fi +.EE .PP Any \f[B]I\f[R] in the parameter list or \f[B]auto\f[R] list may be replaced with \f[B]I[]\f[R] to make a parameter or \f[B]auto\f[R] var an array, and any \f[B]I\f[R] in the parameter list may be replaced with \f[B]*I[]\f[R] to make a parameter an array reference. Callers of functions that take array references should not put an asterisk in the call; they must be called with just \f[B]I[]\f[R] like normal array parameters and will be automatically converted into references. .PP As a \f[B]non-portable extension\f[R], the opening brace of a \f[B]define\f[R] statement may appear on the next line. .PP As a \f[B]non-portable extension\f[R], the return statement may also be in one of the following forms: .IP "1." 3 \f[B]return\f[R] .IP "2." 3 \f[B]return\f[R] \f[B](\f[R] \f[B])\f[R] .IP "3." 3 \f[B]return\f[R] \f[B]E\f[R] .PP The first two, or not specifying a \f[B]return\f[R] statement, is equivalent to \f[B]return (0)\f[R], unless the function is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection below). .SS Void Functions -.PP Functions can also be \f[B]void\f[R] functions, defined as follows: .IP -.nf -\f[C] +.EX define void I(I,...,I){ auto I,...,I S;...;S return } -\f[R] -.fi +.EE .PP They can only be used as standalone expressions, where such an expression would be printed alone, except in a print statement. .PP Void functions can only use the first two \f[B]return\f[R] statements listed above. They can also omit the return statement entirely. .PP The word \[lq]void\[rq] is not treated as a keyword; it is still possible to have variables, arrays, and functions named \f[B]void\f[R]. The word \[lq]void\[rq] is only treated specially right after the \f[B]define\f[R] keyword. .PP This is a \f[B]non-portable extension\f[R]. .SS Array References -.PP For any array in the parameter list, if the array is declared in the form .IP -.nf -\f[C] +.EX *I[] -\f[R] -.fi +.EE .PP it is a \f[B]reference\f[R]. Any changes to the array in the function are reflected, when the function returns, to the array that was passed in. .PP Other than this, all function arguments are passed by value. .PP This is a \f[B]non-portable extension\f[R]. .SH LIBRARY -.PP All of the functions below, including the functions in the extended math library (see the \f[I]Extended Library\f[R] subsection below), are available when the \f[B]-l\f[R] or \f[B]--mathlib\f[R] command-line flags are given, except that the extended math library is not available when the \f[B]-s\f[R] option, the \f[B]-w\f[R] option, or equivalents are given. .SS Standard Library -.PP The standard (see the \f[B]STANDARDS\f[R] section) defines the following functions for the math library: .TP \f[B]s(x)\f[R] Returns the sine of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]c(x)\f[R] Returns the cosine of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]a(x)\f[R] Returns the arctangent of \f[B]x\f[R], in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]l(x)\f[R] Returns the natural logarithm of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]e(x)\f[R] Returns the mathematical constant \f[B]e\f[R] raised to the power of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]j(x, n)\f[R] Returns the bessel integer order \f[B]n\f[R] (truncated) of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .SS Extended Library -.PP The extended library is \f[I]not\f[R] loaded when the \f[B]-s\f[R]/\f[B]--standard\f[R] or \f[B]-w\f[R]/\f[B]--warn\f[R] options are given since they are not part of the library defined by the standard (see the \f[B]STANDARDS\f[R] section). .PP The extended library is a \f[B]non-portable extension\f[R]. .TP \f[B]p(x, y)\f[R] Calculates \f[B]x\f[R] to the power of \f[B]y\f[R], even if \f[B]y\f[R] is not an integer, and returns the result to the current \f[B]scale\f[R]. .RS .PP It is an error if \f[B]y\f[R] is negative and \f[B]x\f[R] is \f[B]0\f[R]. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]r(x, p)\f[R] Returns \f[B]x\f[R] rounded to \f[B]p\f[R] decimal places according to the rounding mode round half away from \f[B]0\f[R] (https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero). .TP \f[B]ceil(x, p)\f[R] Returns \f[B]x\f[R] rounded to \f[B]p\f[R] decimal places according to the rounding mode round away from \f[B]0\f[R] (https://en.wikipedia.org/wiki/Rounding#Rounding_away_from_zero). .TP \f[B]f(x)\f[R] Returns the factorial of the truncated absolute value of \f[B]x\f[R]. .TP \f[B]max(a, b)\f[R] Returns \f[B]a\f[R] if \f[B]a\f[R] is greater than \f[B]b\f[R]; otherwise, returns \f[B]b\f[R]. .TP \f[B]min(a, b)\f[R] Returns \f[B]a\f[R] if \f[B]a\f[R] is less than \f[B]b\f[R]; otherwise, returns \f[B]b\f[R]. .TP \f[B]perm(n, k)\f[R] Returns the permutation of the truncated absolute value of \f[B]n\f[R] of the truncated absolute value of \f[B]k\f[R], if \f[B]k <= n\f[R]. If not, it returns \f[B]0\f[R]. .TP \f[B]comb(n, k)\f[R] Returns the combination of the truncated absolute value of \f[B]n\f[R] of the truncated absolute value of \f[B]k\f[R], if \f[B]k <= n\f[R]. If not, it returns \f[B]0\f[R]. .TP \f[B]fib(n)\f[R] Returns the Fibonacci number of the truncated absolute value of \f[B]n\f[R]. .TP \f[B]l2(x)\f[R] Returns the logarithm base \f[B]2\f[R] of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]l10(x)\f[R] Returns the logarithm base \f[B]10\f[R] of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]log(x, b)\f[R] Returns the logarithm base \f[B]b\f[R] of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]cbrt(x)\f[R] Returns the cube root of \f[B]x\f[R]. .TP \f[B]root(x, n)\f[R] Calculates the truncated value of \f[B]n\f[R], \f[B]r\f[R], and returns the \f[B]r\f[R]th root of \f[B]x\f[R] to the current \f[B]scale\f[R]. .RS .PP If \f[B]r\f[R] is \f[B]0\f[R] or negative, this raises an error and causes bc(1) to reset (see the \f[B]RESET\f[R] section). It also raises an error and causes bc(1) to reset if \f[B]r\f[R] is even and \f[B]x\f[R] is negative. .RE .TP \f[B]gcd(a, b)\f[R] Returns the greatest common divisor (factor) of the truncated absolute value of \f[B]a\f[R] and the truncated absolute value of \f[B]b\f[R]. .TP \f[B]lcm(a, b)\f[R] Returns the least common multiple of the truncated absolute value of \f[B]a\f[R] and the truncated absolute value of \f[B]b\f[R]. .TP \f[B]pi(p)\f[R] Returns \f[B]pi\f[R] to \f[B]p\f[R] decimal places. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]t(x)\f[R] Returns the tangent of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]a2(y, x)\f[R] Returns the arctangent of \f[B]y/x\f[R], in radians. If both \f[B]y\f[R] and \f[B]x\f[R] are equal to \f[B]0\f[R], it raises an error and causes bc(1) to reset (see the \f[B]RESET\f[R] section). Otherwise, if \f[B]x\f[R] is greater than \f[B]0\f[R], it returns \f[B]a(y/x)\f[R]. If \f[B]x\f[R] is less than \f[B]0\f[R], and \f[B]y\f[R] is greater than or equal to \f[B]0\f[R], it returns \f[B]a(y/x)+pi\f[R]. If \f[B]x\f[R] is less than \f[B]0\f[R], and \f[B]y\f[R] is less than \f[B]0\f[R], it returns \f[B]a(y/x)-pi\f[R]. If \f[B]x\f[R] is equal to \f[B]0\f[R], and \f[B]y\f[R] is greater than \f[B]0\f[R], it returns \f[B]pi/2\f[R]. If \f[B]x\f[R] is equal to \f[B]0\f[R], and \f[B]y\f[R] is less than \f[B]0\f[R], it returns \f[B]-pi/2\f[R]. .RS .PP This function is the same as the \f[B]atan2()\f[R] function in many programming languages. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]sin(x)\f[R] Returns the sine of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is an alias of \f[B]s(x)\f[R]. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]cos(x)\f[R] Returns the cosine of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is an alias of \f[B]c(x)\f[R]. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]tan(x)\f[R] Returns the tangent of \f[B]x\f[R], which is assumed to be in radians. .RS .PP If \f[B]x\f[R] is equal to \f[B]1\f[R] or \f[B]-1\f[R], this raises an error and causes bc(1) to reset (see the \f[B]RESET\f[R] section). .PP This is an alias of \f[B]t(x)\f[R]. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]atan(x)\f[R] Returns the arctangent of \f[B]x\f[R], in radians. .RS .PP This is an alias of \f[B]a(x)\f[R]. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]atan2(y, x)\f[R] Returns the arctangent of \f[B]y/x\f[R], in radians. If both \f[B]y\f[R] and \f[B]x\f[R] are equal to \f[B]0\f[R], it raises an error and causes bc(1) to reset (see the \f[B]RESET\f[R] section). Otherwise, if \f[B]x\f[R] is greater than \f[B]0\f[R], it returns \f[B]a(y/x)\f[R]. If \f[B]x\f[R] is less than \f[B]0\f[R], and \f[B]y\f[R] is greater than or equal to \f[B]0\f[R], it returns \f[B]a(y/x)+pi\f[R]. If \f[B]x\f[R] is less than \f[B]0\f[R], and \f[B]y\f[R] is less than \f[B]0\f[R], it returns \f[B]a(y/x)-pi\f[R]. If \f[B]x\f[R] is equal to \f[B]0\f[R], and \f[B]y\f[R] is greater than \f[B]0\f[R], it returns \f[B]pi/2\f[R]. If \f[B]x\f[R] is equal to \f[B]0\f[R], and \f[B]y\f[R] is less than \f[B]0\f[R], it returns \f[B]-pi/2\f[R]. .RS .PP This function is the same as the \f[B]atan2()\f[R] function in many programming languages. .PP This is an alias of \f[B]a2(y, x)\f[R]. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]r2d(x)\f[R] Converts \f[B]x\f[R] from radians to degrees and returns the result. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]d2r(x)\f[R] Converts \f[B]x\f[R] from degrees to radians and returns the result. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]frand(p)\f[R] Generates a pseudo-random integer between \f[B]0\f[R] (inclusive) and \f[B]1\f[R] (exclusive) with the number of decimal digits after the decimal point equal to the truncated absolute value of \f[B]p\f[R]. If \f[B]p\f[R] is not \f[B]0\f[R], then calling this function will change the value of \f[B]seed\f[R]. If \f[B]p\f[R] is \f[B]0\f[R], then \f[B]0\f[R] is returned, and \f[B]seed\f[R] is \f[I]not\f[R] changed. .TP \f[B]ifrand(i, p)\f[R] Generates a pseudo-random integer that is between \f[B]0\f[R] (inclusive) and the truncated absolute value of \f[B]i\f[R] (exclusive) with the number of decimal digits after the decimal point equal to the truncated absolute value of \f[B]p\f[R]. If the absolute value of \f[B]i\f[R] is greater than or equal to \f[B]2\f[R], and \f[B]p\f[R] is not \f[B]0\f[R], then calling this function will change the value of \f[B]seed\f[R]; otherwise, \f[B]0\f[R] is returned, and \f[B]seed\f[R] is not changed. .TP \f[B]i2rand(a, b)\f[R] Takes the truncated value of \f[B]a\f[R] and \f[B]b\f[R] and uses them as inclusive bounds to enerate a pseudo-random integer. If the difference of the truncated values of \f[B]a\f[R] and \f[B]b\f[R] is \f[B]0\f[R], then the truncated value is returned, and \f[B]seed\f[R] is \f[I]not\f[R] changed. Otherwise, this function will change the value of \f[B]seed\f[R]. .TP \f[B]srand(x)\f[R] Returns \f[B]x\f[R] with its sign flipped with probability \f[B]0.5\f[R]. In other words, it randomizes the sign of \f[B]x\f[R]. .TP \f[B]brand()\f[R] Returns a random boolean value (either \f[B]0\f[R] or \f[B]1\f[R]). .TP \f[B]band(a, b)\f[R] Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R] and calculates and returns the result of the bitwise \f[B]and\f[R] operation between them. .RS .PP If you want to use signed two\[cq]s complement arguments, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bor(a, b)\f[R] Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R] and calculates and returns the result of the bitwise \f[B]or\f[R] operation between them. .RS .PP If you want to use signed two\[cq]s complement arguments, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bxor(a, b)\f[R] Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R] and calculates and returns the result of the bitwise \f[B]xor\f[R] operation between them. .RS .PP If you want to use signed two\[cq]s complement arguments, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bshl(a, b)\f[R] Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R] and calculates and returns the result of \f[B]a\f[R] bit-shifted left by \f[B]b\f[R] places. .RS .PP If you want to use signed two\[cq]s complement arguments, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bshr(a, b)\f[R] Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R] and calculates and returns the truncated result of \f[B]a\f[R] bit-shifted right by \f[B]b\f[R] places. .RS .PP If you want to use signed two\[cq]s complement arguments, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bnotn(x, n)\f[R] Takes the truncated absolute value of \f[B]x\f[R] and does a bitwise not as though it has the same number of bytes as the truncated absolute value of \f[B]n\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bnot8(x)\f[R] Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as -though it has \f[B]8\f[R] binary digits (1 unsigned byte). +though it has \f[B]8\f[R] binary digits (\f[B]1\f[R] unsigned byte). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bnot16(x)\f[R] Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as -though it has \f[B]16\f[R] binary digits (2 unsigned bytes). +though it has \f[B]16\f[R] binary digits (\f[B]2\f[R] unsigned bytes). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bnot32(x)\f[R] Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as -though it has \f[B]32\f[R] binary digits (4 unsigned bytes). +though it has \f[B]32\f[R] binary digits (\f[B]4\f[R] unsigned bytes). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bnot64(x)\f[R] Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as -though it has \f[B]64\f[R] binary digits (8 unsigned bytes). +though it has \f[B]64\f[R] binary digits (\f[B]8\f[R] unsigned bytes). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bnot(x)\f[R] Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as though it has the minimum number of power of two unsigned bytes. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brevn(x, n)\f[R] Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as though it has the same number of 8-bit bytes as the truncated absolute value of \f[B]n\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brev8(x)\f[R] Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as -though it has 8 binary digits (1 unsigned byte). +though it has 8 binary digits (\f[B]1\f[R] unsigned byte). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brev16(x)\f[R] Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as -though it has 16 binary digits (2 unsigned bytes). +though it has 16 binary digits (\f[B]2\f[R] unsigned bytes). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brev32(x)\f[R] Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as -though it has 32 binary digits (4 unsigned bytes). +though it has 32 binary digits (\f[B]4\f[R] unsigned bytes). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brev64(x)\f[R] Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as -though it has 64 binary digits (8 unsigned bytes). +though it has 64 binary digits (\f[B]8\f[R] unsigned bytes). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brev(x)\f[R] Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as though it has the minimum number of power of two unsigned bytes. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]broln(x, p, n)\f[R] Does a left bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has the same number of unsigned 8-bit bytes as the truncated absolute value of \f[B]n\f[R], by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by the \f[B]2\f[R] to the power of the number of binary digits in \f[B]n\f[R] 8-bit bytes. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brol8(x, p)\f[R] Does a left bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has \f[B]8\f[R] binary digits (\f[B]1\f[R] unsigned byte), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]8\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brol16(x, p)\f[R] Does a left bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has \f[B]16\f[R] binary digits (\f[B]2\f[R] unsigned bytes), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]16\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brol32(x, p)\f[R] Does a left bitwise rotatation of the truncated absolute value of -\f[B]x\f[R], as though it has \f[B]32\f[R] binary digits (\f[B]2\f[R] +\f[B]x\f[R], as though it has \f[B]32\f[R] binary digits (\f[B]4\f[R] unsigned bytes), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]32\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brol64(x, p)\f[R] Does a left bitwise rotatation of the truncated absolute value of -\f[B]x\f[R], as though it has \f[B]64\f[R] binary digits (\f[B]2\f[R] +\f[B]x\f[R], as though it has \f[B]64\f[R] binary digits (\f[B]8\f[R] unsigned bytes), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]64\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brol(x, p)\f[R] Does a left bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has the minimum number of power of two unsigned 8-bit bytes, by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by 2 to the power of the number of binary digits in the minimum number of 8-bit bytes. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brorn(x, p, n)\f[R] Does a right bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has the same number of unsigned 8-bit bytes as the truncated absolute value of \f[B]n\f[R], by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by the \f[B]2\f[R] to the power of the number of binary digits in \f[B]n\f[R] 8-bit bytes. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bror8(x, p)\f[R] Does a right bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has \f[B]8\f[R] binary digits (\f[B]1\f[R] unsigned byte), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]8\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bror16(x, p)\f[R] Does a right bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has \f[B]16\f[R] binary digits (\f[B]2\f[R] unsigned bytes), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]16\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bror32(x, p)\f[R] Does a right bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has \f[B]32\f[R] binary digits (\f[B]2\f[R] unsigned bytes), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]32\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bror64(x, p)\f[R] Does a right bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has \f[B]64\f[R] binary digits (\f[B]2\f[R] unsigned bytes), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]64\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bror(x, p)\f[R] Does a right bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has the minimum number of power of two unsigned 8-bit bytes, by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by 2 to the power of the number of binary digits in the minimum number of 8-bit bytes. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bmodn(x, n)\f[R] Returns the modulus of the truncated absolute value of \f[B]x\f[R] by \f[B]2\f[R] to the power of the multiplication of the truncated absolute value of \f[B]n\f[R] and \f[B]8\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bmod8(x, n)\f[R] Returns the modulus of the truncated absolute value of \f[B]x\f[R] by \f[B]2\f[R] to the power of \f[B]8\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bmod16(x, n)\f[R] Returns the modulus of the truncated absolute value of \f[B]x\f[R] by \f[B]2\f[R] to the power of \f[B]16\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bmod32(x, n)\f[R] Returns the modulus of the truncated absolute value of \f[B]x\f[R] by \f[B]2\f[R] to the power of \f[B]32\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bmod64(x, n)\f[R] Returns the modulus of the truncated absolute value of \f[B]x\f[R] by \f[B]2\f[R] to the power of \f[B]64\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bunrev(t)\f[R] Assumes \f[B]t\f[R] is a bitwise-reversed number with an extra set bit one place more significant than the real most significant bit (which was the least significant bit in the original number). This number is reversed and returned without the extra set bit. .RS .PP This function is used to implement other bitwise functions; it is not meant to be used by users, but it can be. .RE .TP \f[B]plz(x)\f[R] If \f[B]x\f[R] is not equal to \f[B]0\f[R] and greater that \f[B]-1\f[R] and less than \f[B]1\f[R], it is printed with a leading zero, regardless of the use of the \f[B]-z\f[R] option (see the \f[B]OPTIONS\f[R] section) and without a trailing newline. .RS .PP Otherwise, \f[B]x\f[R] is printed normally, without a trailing newline. .RE .TP \f[B]plznl(x)\f[R] If \f[B]x\f[R] is not equal to \f[B]0\f[R] and greater that \f[B]-1\f[R] and less than \f[B]1\f[R], it is printed with a leading zero, regardless of the use of the \f[B]-z\f[R] option (see the \f[B]OPTIONS\f[R] section) and with a trailing newline. .RS .PP Otherwise, \f[B]x\f[R] is printed normally, with a trailing newline. .RE .TP \f[B]pnlz(x)\f[R] If \f[B]x\f[R] is not equal to \f[B]0\f[R] and greater that \f[B]-1\f[R] and less than \f[B]1\f[R], it is printed without a leading zero, regardless of the use of the \f[B]-z\f[R] option (see the \f[B]OPTIONS\f[R] section) and without a trailing newline. .RS .PP Otherwise, \f[B]x\f[R] is printed normally, without a trailing newline. .RE .TP \f[B]pnlznl(x)\f[R] If \f[B]x\f[R] is not equal to \f[B]0\f[R] and greater that \f[B]-1\f[R] and less than \f[B]1\f[R], it is printed without a leading zero, regardless of the use of the \f[B]-z\f[R] option (see the \f[B]OPTIONS\f[R] section) and with a trailing newline. .RS .PP Otherwise, \f[B]x\f[R] is printed normally, with a trailing newline. .RE .TP \f[B]ubytes(x)\f[R] Returns the numbers of unsigned integer bytes required to hold the truncated absolute value of \f[B]x\f[R]. .TP \f[B]sbytes(x)\f[R] Returns the numbers of signed, two\[cq]s-complement integer bytes required to hold the truncated value of \f[B]x\f[R]. .TP \f[B]s2u(x)\f[R] Returns \f[B]x\f[R] if it is non-negative. If it \f[I]is\f[R] negative, then it calculates what \f[B]x\f[R] would be as a 2\[cq]s-complement signed integer and returns the non-negative integer that would have the same representation in binary. .TP \f[B]s2un(x,n)\f[R] Returns \f[B]x\f[R] if it is non-negative. If it \f[I]is\f[R] negative, then it calculates what \f[B]x\f[R] would be as a 2\[cq]s-complement signed integer with \f[B]n\f[R] bytes and returns the non-negative integer that would have the same representation in binary. If \f[B]x\f[R] cannot fit into \f[B]n\f[R] 2\[cq]s-complement signed bytes, it is truncated to fit. .TP \f[B]hex(x)\f[R] Outputs the hexadecimal (base \f[B]16\f[R]) representation of \f[B]x\f[R]. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]binary(x)\f[R] Outputs the binary (base \f[B]2\f[R]) representation of \f[B]x\f[R]. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]output(x, b)\f[R] Outputs the base \f[B]b\f[R] representation of \f[B]x\f[R]. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]uint(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as an unsigned integer in as few power of two bytes as possible. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer or is negative, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]int(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as a signed, two\[cq]s-complement integer in as few power of two bytes as possible. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]uintn(x, n)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as an unsigned integer in \f[B]n\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer, is negative, or cannot fit into \f[B]n\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]intn(x, n)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as a signed, two\[cq]s-complement integer in \f[B]n\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer or cannot fit into \f[B]n\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]uint8(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as an unsigned integer in \f[B]1\f[R] byte. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer, is negative, or cannot fit into \f[B]1\f[R] byte, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]int8(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as a signed, two\[cq]s-complement integer in \f[B]1\f[R] byte. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer or cannot fit into \f[B]1\f[R] byte, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]uint16(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as an unsigned integer in \f[B]2\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer, is negative, or cannot fit into \f[B]2\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]int16(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as a signed, two\[cq]s-complement integer in \f[B]2\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer or cannot fit into \f[B]2\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]uint32(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as an unsigned integer in \f[B]4\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer, is negative, or cannot fit into \f[B]4\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]int32(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as a signed, two\[cq]s-complement integer in \f[B]4\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer or cannot fit into \f[B]4\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]uint64(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as an unsigned integer in \f[B]8\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer, is negative, or cannot fit into \f[B]8\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]int64(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as a signed, two\[cq]s-complement integer in \f[B]8\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer or cannot fit into \f[B]8\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]hex_uint(x, n)\f[R] Outputs the representation of the truncated absolute value of \f[B]x\f[R] as an unsigned integer in hexadecimal using \f[B]n\f[R] bytes. Not all of the value will be output if \f[B]n\f[R] is too small. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]binary_uint(x, n)\f[R] Outputs the representation of the truncated absolute value of \f[B]x\f[R] as an unsigned integer in binary using \f[B]n\f[R] bytes. Not all of the value will be output if \f[B]n\f[R] is too small. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]output_uint(x, n)\f[R] Outputs the representation of the truncated absolute value of \f[B]x\f[R] as an unsigned integer in the current \f[B]obase\f[R] (see the \f[B]SYNTAX\f[R] section) using \f[B]n\f[R] bytes. Not all of the value will be output if \f[B]n\f[R] is too small. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]output_byte(x, i)\f[R] Outputs byte \f[B]i\f[R] of the truncated absolute value of \f[B]x\f[R], where \f[B]0\f[R] is the least significant byte and \f[B]number_of_bytes - 1\f[R] is the most significant byte. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .SS Transcendental Functions -.PP All transcendental functions can return slightly inaccurate results, up to 1 ULP (https://en.wikipedia.org/wiki/Unit_in_the_last_place). This is unavoidable, and the article at https://people.eecs.berkeley.edu/\[ti]wkahan/LOG10HAF.TXT explains why it is impossible and unnecessary to calculate exact results for the transcendental functions. .PP Because of the possible inaccuracy, I recommend that users call those functions with the precision (\f[B]scale\f[R]) set to at least 1 higher than is necessary. If exact results are \f[I]absolutely\f[R] required, users can double the precision (\f[B]scale\f[R]) and then truncate. .PP The transcendental functions in the standard math library are: .IP \[bu] 2 \f[B]s(x)\f[R] .IP \[bu] 2 \f[B]c(x)\f[R] .IP \[bu] 2 \f[B]a(x)\f[R] .IP \[bu] 2 \f[B]l(x)\f[R] .IP \[bu] 2 \f[B]e(x)\f[R] .IP \[bu] 2 \f[B]j(x, n)\f[R] .PP The transcendental functions in the extended math library are: .IP \[bu] 2 \f[B]l2(x)\f[R] .IP \[bu] 2 \f[B]l10(x)\f[R] .IP \[bu] 2 \f[B]log(x, b)\f[R] .IP \[bu] 2 \f[B]pi(p)\f[R] .IP \[bu] 2 \f[B]t(x)\f[R] .IP \[bu] 2 \f[B]a2(y, x)\f[R] .IP \[bu] 2 \f[B]sin(x)\f[R] .IP \[bu] 2 \f[B]cos(x)\f[R] .IP \[bu] 2 \f[B]tan(x)\f[R] .IP \[bu] 2 \f[B]atan(x)\f[R] .IP \[bu] 2 \f[B]atan2(y, x)\f[R] .IP \[bu] 2 \f[B]r2d(x)\f[R] .IP \[bu] 2 \f[B]d2r(x)\f[R] .SH RESET -.PP When bc(1) encounters an error or a signal that it has a non-default handler for, it resets. This means that several things happen. .PP First, any functions that are executing are stopped and popped off the stack. The behavior is not unlike that of exceptions in programming languages. Then the execution point is set so that any code waiting to execute (after all functions returned) is skipped. .PP Thus, when bc(1) resets, it skips any remaining code waiting to be executed. Then, if it is interactive mode, and the error was not a fatal error (see the \f[B]EXIT STATUS\f[R] section), it asks for more input; otherwise, it exits with the appropriate return code. .PP Note that this reset behavior is different from the GNU bc(1), which attempts to start executing the statement right after the one that caused an error. .SH PERFORMANCE -.PP Most bc(1) implementations use \f[B]char\f[R] types to calculate the value of \f[B]1\f[R] decimal digit at a time, but that can be slow. This bc(1) does something different. .PP It uses large integers to calculate more than \f[B]1\f[R] decimal digit at a time. If built in a environment where \f[B]BC_LONG_BIT\f[R] (see the \f[B]LIMITS\f[R] section) is \f[B]64\f[R], then each integer has \f[B]9\f[R] decimal digits. If built in an environment where \f[B]BC_LONG_BIT\f[R] is \f[B]32\f[R] then each integer has \f[B]4\f[R] decimal digits. This value (the number of decimal digits per large integer) is called \f[B]BC_BASE_DIGS\f[R]. .PP The actual values of \f[B]BC_LONG_BIT\f[R] and \f[B]BC_BASE_DIGS\f[R] can be queried with the \f[B]limits\f[R] statement. .PP In addition, this bc(1) uses an even larger integer for overflow checking. This integer type depends on the value of \f[B]BC_LONG_BIT\f[R], but is always at least twice as large as the integer type used to store digits. .SH LIMITS -.PP The following are the limits on bc(1): .TP \f[B]BC_LONG_BIT\f[R] The number of bits in the \f[B]long\f[R] type in the environment where bc(1) was built. This determines how many decimal digits can be stored in a single large integer (see the \f[B]PERFORMANCE\f[R] section). .TP \f[B]BC_BASE_DIGS\f[R] The number of decimal digits per large integer (see the \f[B]PERFORMANCE\f[R] section). Depends on \f[B]BC_LONG_BIT\f[R]. .TP \f[B]BC_BASE_POW\f[R] The max decimal number that each large integer can store (see \f[B]BC_BASE_DIGS\f[R]) plus \f[B]1\f[R]. Depends on \f[B]BC_BASE_DIGS\f[R]. .TP \f[B]BC_OVERFLOW_MAX\f[R] The max number that the overflow type (see the \f[B]PERFORMANCE\f[R] section) can hold. Depends on \f[B]BC_LONG_BIT\f[R]. .TP \f[B]BC_BASE_MAX\f[R] The maximum output base. Set at \f[B]BC_BASE_POW\f[R]. .TP \f[B]BC_DIM_MAX\f[R] The maximum size of arrays. Set at \f[B]SIZE_MAX-1\f[R]. .TP \f[B]BC_SCALE_MAX\f[R] The maximum \f[B]scale\f[R]. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_STRING_MAX\f[R] The maximum length of strings. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_NAME_MAX\f[R] The maximum length of identifiers. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_NUM_MAX\f[R] The maximum length of a number (in decimal digits), which includes digits after the decimal point. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_RAND_MAX\f[R] The maximum integer (inclusive) returned by the \f[B]rand()\f[R] operand. Set at \f[B]2\[ha]BC_LONG_BIT-1\f[R]. .TP Exponent The maximum allowable exponent (positive or negative). Set at \f[B]BC_OVERFLOW_MAX\f[R]. .TP Number of vars The maximum number of vars/arrays. Set at \f[B]SIZE_MAX-1\f[R]. .PP The actual values can be queried with the \f[B]limits\f[R] statement. .PP These limits are meant to be effectively non-existent; the limits are so large (at least on 64-bit machines) that there should not be any point at which they become a problem. In fact, memory should be exhausted before these limits should be hit. .SH ENVIRONMENT VARIABLES -.PP As \f[B]non-portable extensions\f[R], bc(1) recognizes the following environment variables: .TP \f[B]POSIXLY_CORRECT\f[R] If this variable exists (no matter the contents), bc(1) behaves as if the \f[B]-s\f[R] option was given. .TP \f[B]BC_ENV_ARGS\f[R] This is another way to give command-line arguments to bc(1). They should be in the same format as all other command-line arguments. These are always processed first, so any files given in \f[B]BC_ENV_ARGS\f[R] will be processed before arguments and files given on the command-line. This gives the user the ability to set up \[lq]standard\[rq] options and files to be used at every invocation. The most useful thing for such files to contain would be useful functions that the user might want every time bc(1) runs. .RS .PP The code that parses \f[B]BC_ENV_ARGS\f[R] will correctly handle quoted arguments, but it does not understand escape sequences. For example, the string \f[B]\[lq]/home/gavin/some bc file.bc\[rq]\f[R] will be correctly parsed, but the string \f[B]\[lq]/home/gavin/some \[dq]bc\[dq] file.bc\[rq]\f[R] will include the backslashes. .PP The quote parsing will handle either kind of quotes, \f[B]\[cq]\f[R] or \f[B]\[lq]\f[R]. Thus, if you have a file with any number of single quotes in the name, you can use double quotes as the outside quotes, as in \f[B]\[lq]some `bc' file.bc\[rq]\f[R], and vice versa if you have a file with double quotes. However, handling a file with both kinds of quotes in \f[B]BC_ENV_ARGS\f[R] is not supported due to the complexity of the parsing, though such files are still supported on the command-line where the parsing is done by the shell. .RE .TP \f[B]BC_LINE_LENGTH\f[R] If this environment variable exists and contains an integer that is greater than \f[B]1\f[R] and is less than \f[B]UINT16_MAX\f[R] (\f[B]2\[ha]16-1\f[R]), bc(1) will output lines to that length, including the backslash (\f[B]\[rs]\f[R]). The default line length is \f[B]70\f[R]. .RS .PP The special value of \f[B]0\f[R] will disable line length checking and print numbers without regard to line length and without backslashes and newlines. .RE .TP \f[B]BC_BANNER\f[R] If this environment variable exists and contains an integer, then a non-zero value activates the copyright banner when bc(1) is in interactive mode, while zero deactivates it. .RS .PP If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), then this environment variable has no effect because bc(1) does not print the banner when not in interactive mode. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_SIGINT_RESET\f[R] If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), then this environment variable has no effect because bc(1) exits on \f[B]SIGINT\f[R] when not in interactive mode. .RS .PP However, when bc(1) is in interactive mode, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) reset on \f[B]SIGINT\f[R], rather than exit, and zero makes bc(1) exit. If this environment variable exists and is \f[I]not\f[R] an integer, then bc(1) will exit on \f[B]SIGINT\f[R]. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_TTY_MODE\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, then a non-zero value makes bc(1) use TTY mode, and zero makes bc(1) not use TTY mode. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_PROMPT\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) use a prompt, and zero or a non-integer makes bc(1) not use a prompt. If this environment variable does not exist and \f[B]BC_TTY_MODE\f[R] does, then the value of the \f[B]BC_TTY_MODE\f[R] environment variable is used. .PP This environment variable and the \f[B]BC_TTY_MODE\f[R] environment variable override the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_EXPR_EXIT\f[R] If any expressions or expression files are given on the command-line with \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R], then if this environment variable exists and contains an integer, a non-zero value makes bc(1) exit after executing the expressions and expression files, and a zero value makes bc(1) not exit. .RS .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_DIGIT_CLAMP\f[R] When parsing numbers and if this environment variable exists and contains an integer, a non-zero value makes bc(1) clamp digits that are greater than or equal to the current \f[B]ibase\f[R] so that all such digits are considered equal to the \f[B]ibase\f[R] minus 1, and a zero value disables such clamping so that those digits are always equal to their value, which is multiplied by the power of the \f[B]ibase\f[R]. .RS .PP This never applies to single-digit numbers, as per the standard (see the \f[B]STANDARDS\f[R] section). .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .SH EXIT STATUS -.PP bc(1) returns the following exit statuses: .TP \f[B]0\f[R] No error. .TP \f[B]1\f[R] A math error occurred. This follows standard practice of using \f[B]1\f[R] for expected errors, since math errors will happen in the process of normal execution. .RS .PP Math errors include divide by \f[B]0\f[R], taking the square root of a negative number, using a negative number as a bound for the pseudo-random number generator, attempting to convert a negative number to a hardware integer, overflow when converting a number to a hardware integer, overflow when calculating the size of a number, and attempting to use a non-integer where an integer is required. .PP Converting to a hardware integer happens for the second operand of the power (\f[B]\[ha]\f[R]), places (\f[B]\[at]\f[R]), left shift (\f[B]<<\f[R]), and right shift (\f[B]>>\f[R]) operators and their corresponding assignment operators. .RE .TP \f[B]2\f[R] A parse error occurred. .RS .PP Parse errors include unexpected \f[B]EOF\f[R], using an invalid character, failing to find the end of a string or comment, using a token where it is invalid, giving an invalid expression, giving an invalid print statement, giving an invalid function definition, attempting to assign to an expression that is not a named expression (see the \f[I]Named Expressions\f[R] subsection of the \f[B]SYNTAX\f[R] section), giving an invalid \f[B]auto\f[R] list, having a duplicate \f[B]auto\f[R]/function parameter, failing to find the end of a code block, attempting to return a value from a \f[B]void\f[R] function, attempting to use a variable as a reference, and using any extensions when the option \f[B]-s\f[R] or any equivalents were given. .RE .TP \f[B]3\f[R] A runtime error occurred. .RS .PP Runtime errors include assigning an invalid number to any global (\f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R]), giving a bad expression to a \f[B]read()\f[R] call, calling \f[B]read()\f[R] inside of a \f[B]read()\f[R] call, type errors, passing the wrong number of arguments to functions, attempting to call an undefined function, and attempting to use a \f[B]void\f[R] function call as a value in an expression. .RE .TP \f[B]4\f[R] A fatal error occurred. .RS .PP Fatal errors include memory allocation errors, I/O errors, failing to open files, attempting to use files that do not have only ASCII characters (bc(1) only accepts ASCII characters), attempting to open a directory as a file, and giving invalid command-line options. .RE .PP The exit status \f[B]4\f[R] is special; when a fatal error occurs, bc(1) always exits and returns \f[B]4\f[R], no matter what mode bc(1) is in. .PP The other statuses will only be returned when bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), since bc(1) resets its state (see the \f[B]RESET\f[R] section) and accepts more input when one of those errors occurs in interactive mode. This is also the case when interactive mode is forced by the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .PP These exit statuses allow bc(1) to be used in shell scripting with error checking, and its normal behavior can be forced by using the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .SH INTERACTIVE MODE -.PP Per the standard (see the \f[B]STANDARDS\f[R] section), bc(1) has an interactive mode and a non-interactive mode. Interactive mode is turned on automatically when both \f[B]stdin\f[R] and \f[B]stdout\f[R] are hooked to a terminal, but the \f[B]-i\f[R] flag and \f[B]--interactive\f[R] option can turn it on in other situations. .PP In interactive mode, bc(1) attempts to recover from errors (see the \f[B]RESET\f[R] section), and in normal execution, flushes \f[B]stdout\f[R] as soon as execution is done for the current input. bc(1) may also reset on \f[B]SIGINT\f[R] instead of exit, depending on the contents of, or default for, the \f[B]BC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .SH TTY MODE -.PP If \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY, then \[lq]TTY mode\[rq] is considered to be available, and thus, bc(1) can turn on TTY mode, subject to some settings. .PP If there is the environment variable \f[B]BC_TTY_MODE\f[R] in the environment (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then if that environment variable contains a non-zero integer, bc(1) will turn on TTY mode when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY. If the \f[B]BC_TTY_MODE\f[R] environment variable exists but is \f[I]not\f[R] a non-zero integer, then bc(1) will not turn TTY mode on. .PP If the environment variable \f[B]BC_TTY_MODE\f[R] does \f[I]not\f[R] exist, the default setting is used. The default setting can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP TTY mode is different from interactive mode because interactive mode is required in the bc(1) standard (see the \f[B]STANDARDS\f[R] section), and interactive mode requires only \f[B]stdin\f[R] and \f[B]stdout\f[R] to be connected to a terminal. .SS Prompt -.PP If TTY mode is available, then a prompt can be enabled. Like TTY mode itself, it can be turned on or off with an environment variable: \f[B]BC_PROMPT\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP If the environment variable \f[B]BC_PROMPT\f[R] exists and is a non-zero integer, then the prompt is turned on when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options were not used. The read prompt will be turned on under the same conditions, except that the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options must also not be used. .PP However, if \f[B]BC_PROMPT\f[R] does not exist, the prompt can be enabled or disabled with the \f[B]BC_TTY_MODE\f[R] environment variable, the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options, and the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options. See the \f[B]ENVIRONMENT VARIABLES\f[R] and \f[B]OPTIONS\f[R] sections for more details. .SH SIGNAL HANDLING -.PP Sending a \f[B]SIGINT\f[R] will cause bc(1) to do one of two things. .PP If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), or the \f[B]BC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or its default, is either not an integer or it is zero, bc(1) will exit. .PP However, if bc(1) is in interactive mode, and the \f[B]BC_SIGINT_RESET\f[R] or its default is an integer and non-zero, then bc(1) will stop executing the current input and reset (see the \f[B]RESET\f[R] section) upon receiving a \f[B]SIGINT\f[R]. .PP Note that \[lq]current input\[rq] can mean one of two things. If bc(1) is processing input from \f[B]stdin\f[R] in interactive mode, it will ask for more input. If bc(1) is processing input from a file in interactive mode, it will stop processing the file and start processing the next file, if one exists, or ask for input from \f[B]stdin\f[R] if no other file exists. .PP This means that if a \f[B]SIGINT\f[R] is sent to bc(1) as it is executing a file, it can seem as though bc(1) did not respond to the signal since it will immediately start executing the next file. This is by design; most files that users execute when interacting with bc(1) have function definitions, which are quick to parse. If a file takes a long time to execute, there may be a bug in that file. The rest of the files could still be executed without problem, allowing the user to continue. .PP \f[B]SIGTERM\f[R] and \f[B]SIGQUIT\f[R] cause bc(1) to clean up and exit, and it uses the default handler for all other signals. .SH SEE ALSO -.PP dc(1) .SH STANDARDS -.PP bc(1) is compliant with the IEEE Std 1003.1-2017 (\[lq]POSIX.1-2017\[rq]) specification at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html . The flags \f[B]-efghiqsvVw\f[R], all long options, and the extensions noted above are extensions to that specification. .PP In addition, the behavior of the \f[B]quit\f[R] implements an interpretation of that specification that is different from all known implementations. For more information see the \f[B]Statements\f[R] subsection of the \f[B]SYNTAX\f[R] section. .PP Note that the specification explicitly says that bc(1) only accepts numbers that use a period (\f[B].\f[R]) as a radix point, regardless of the value of \f[B]LC_NUMERIC\f[R]. .SH BUGS -.PP Before version \f[B]6.1.0\f[R], this bc(1) had incorrect behavior for the \f[B]quit\f[R] statement. .PP No other bugs are known. Report bugs at https://git.gavinhoward.com/gavin/bc . .SH AUTHORS -.PP -Gavin D. -Howard and contributors. +Gavin D. Howard \c +.MT gavin@gavinhoward.com +.ME \c +\ and contributors. diff --git a/contrib/bc/manuals/bc/HN.1.md b/contrib/bc/manuals/bc/HN.1.md index 09ea524d95c2..2b3b0d6aa3eb 100644 --- a/contrib/bc/manuals/bc/HN.1.md +++ b/contrib/bc/manuals/bc/HN.1.md @@ -1,2491 +1,2491 @@ # NAME bc - arbitrary-precision decimal arithmetic language and calculator # SYNOPSIS **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*] # DESCRIPTION bc(1) is an interactive processor for a language first standardized in 1991 by POSIX. (See the **STANDARDS** section.) The language provides unlimited precision decimal arithmetic and is somewhat C-like, but there are differences. Such differences will be noted in this document. After parsing and handling options, this bc(1) reads any files given on the command line and executes them before reading from **stdin**. This bc(1) is a drop-in replacement for *any* bc(1), including (and especially) the GNU bc(1). It also has many extensions and extra features beyond other implementations. **Note**: If running this bc(1) on *any* script meant for another bc(1) gives a parse error, it is probably because a word this bc(1) reserves as a keyword is used as the name of a function, variable, or array. To fix that, use the command-line option **-r** *keyword*, where *keyword* is the keyword that is used as a name in the script. For more information, see the **OPTIONS** section. If parsing scripts meant for other bc(1) implementations still does not work, that is a bug and should be reported. See the **BUGS** section. # OPTIONS The following are the options that bc(1) accepts. **-C**, **-\-no-digit-clamp** : Disables clamping of digits greater than or equal to the current **ibase** when parsing numbers. This means that the value added to a number from a digit is always that digit's value multiplied by the value of ibase raised to the power of the digit's position, which starts from 0 at the least significant digit. If this and/or the **-c** or **-\-digit-clamp** options are given multiple times, the last one given is used. This option overrides the **BC_DIGIT_CLAMP** environment variable (see the **ENVIRONMENT VARIABLES** section) and the default, which can be queried with the **-h** or **-\-help** options. This is a **non-portable extension**. **-c**, **-\-digit-clamp** : Enables clamping of digits greater than or equal to the current **ibase** when parsing numbers. This means that digits that the value added to a number from a digit that is greater than or equal to the ibase is the value of ibase minus 1 all multiplied by the value of ibase raised to the power of the digit's position, which starts from 0 at the least significant digit. If this and/or the **-C** or **-\-no-digit-clamp** options are given multiple times, the last one given is used. This option overrides the **BC_DIGIT_CLAMP** environment variable (see the **ENVIRONMENT VARIABLES** section) and the default, which can be queried with the **-h** or **-\-help** options. This is a **non-portable extension**. **-E** *seed*, **-\-seed**=*seed* : Sets the builtin variable **seed** to the value *seed* assuming that *seed* is in base 10. It is a fatal error if *seed* is not a valid number. If multiple instances of this option are given, the last is used. This is a **non-portable extension**. **-e** *expr*, **-\-expression**=*expr* : Evaluates *expr*. If multiple expressions are given, they are evaluated in order. If files are given as well (see the **-f** and **-\-file** options), the expressions and files are evaluated in the order given. This means that if a file is given before an expression, the file is read in and evaluated first. If this option is given on the command-line (i.e., not in **BC_ENV_ARGS**, see the **ENVIRONMENT VARIABLES** section), then after processing all expressions and files, bc(1) will exit, unless **-** (**stdin**) was given as an argument at least once to **-f** or **-\-file**, whether on the command-line or in **BC_ENV_ARGS**. However, if any other **-e**, **-\-expression**, **-f**, or **-\-file** arguments are given after **-f-** or equivalent is given, bc(1) will give a fatal error and exit. This is a **non-portable extension**. **-f** *file*, **-\-file**=*file* : Reads in *file* and evaluates it, line by line, as though it were read through **stdin**. If expressions are also given (see the **-e** and **-\-expression** options), the expressions are evaluated in the order given. If this option is given on the command-line (i.e., not in **BC_ENV_ARGS**, see the **ENVIRONMENT VARIABLES** section), then after processing all expressions and files, bc(1) will exit, unless **-** (**stdin**) was given as an argument at least once to **-f** or **-\-file**. However, if any other **-e**, **-\-expression**, **-f**, or **-\-file** arguments are given after **-f-** or equivalent is given, bc(1) will give a fatal error and exit. This is a **non-portable extension**. **-g**, **-\-global-stacks** : Turns the globals **ibase**, **obase**, **scale**, and **seed** into stacks. This has the effect that a copy of the current value of all four are pushed onto a stack for every function call, as well as popped when every function returns. This means that functions can assign to any and all of those globals without worrying that the change will affect other functions. Thus, a hypothetical function named **output(x,b)** that simply printed **x** in base **b** could be written like this: define void output(x, b) { obase=b x } instead of like this: define void output(x, b) { auto c c=obase obase=b x obase=c } This makes writing functions much easier. (**Note**: the function **output(x,b)** exists in the extended math library. See the **LIBRARY** section.) However, since using this flag means that functions cannot set **ibase**, **obase**, **scale**, or **seed** globally, functions that are made to do so cannot work anymore. There are two possible use cases for that, and each has a solution. First, if a function is called on startup to turn bc(1) into a number converter, it is possible to replace that capability with various shell aliases. Examples: alias d2o="bc -e ibase=A -e obase=8" alias h2b="bc -e ibase=G -e obase=2" Second, if the purpose of a function is to set **ibase**, **obase**, **scale**, or **seed** globally for any other purpose, it could be split into one to four functions (based on how many globals it sets) and each of those functions could return the desired value for a global. For functions that set **seed**, the value assigned to **seed** is not propagated to parent functions. This means that the sequence of pseudo-random numbers that they see will not be the same sequence of pseudo-random numbers that any parent sees. This is only the case once **seed** has been set. If a function desires to not affect the sequence of pseudo-random numbers of its parents, but wants to use the same **seed**, it can use the following line: seed = seed If the behavior of this option is desired for every run of bc(1), then users could make sure to define **BC_ENV_ARGS** and include this option (see the **ENVIRONMENT VARIABLES** section for more details). If **-s**, **-w**, or any equivalents are used, this option is ignored. This is a **non-portable extension**. **-h**, **-\-help** : Prints a usage message and exits. **-I** *ibase*, **-\-ibase**=*ibase* : Sets the builtin variable **ibase** to the value *ibase* assuming that *ibase* is in base 10. It is a fatal error if *ibase* is not a valid number. If multiple instances of this option are given, the last is used. This is a **non-portable extension**. **-i**, **-\-interactive** : Forces interactive mode. (See the **INTERACTIVE MODE** section.) This is a **non-portable extension**. **-L**, **-\-no-line-length** : Disables line length checking and prints numbers without backslashes and newlines. In other words, this option sets **BC_LINE_LENGTH** to **0** (see the **ENVIRONMENT VARIABLES** section). This is a **non-portable extension**. **-l**, **-\-mathlib** : Sets **scale** (see the **SYNTAX** section) to **20** and loads the included math library and the extended math library before running any code, including any expressions or files specified on the command line. To learn what is in the libraries, see the **LIBRARY** section. **-O** *obase*, **-\-obase**=*obase* : Sets the builtin variable **obase** to the value *obase* assuming that *obase* is in base 10. It is a fatal error if *obase* is not a valid number. If multiple instances of this option are given, the last is used. This is a **non-portable extension**. **-P**, **-\-no-prompt** : Disables the prompt in TTY mode. (The prompt is only enabled in TTY mode. See the **TTY MODE** section.) This is mostly for those users that do not want a prompt or are not used to having them in bc(1). Most of those users would want to put this option in **BC_ENV_ARGS** (see the **ENVIRONMENT VARIABLES** section). These options override the **BC_PROMPT** and **BC_TTY_MODE** environment variables (see the **ENVIRONMENT VARIABLES** section). This is a **non-portable extension**. **-q**, **-\-quiet** : This option is for compatibility with the GNU bc(1) (https://www.gnu.org/software/bc/); it is a no-op. Without this option, GNU bc(1) prints a copyright header. This bc(1) only prints the copyright header if one or more of the **-v**, **-V**, or **-\-version** options are given unless the **BC_BANNER** environment variable is set and contains a non-zero integer or if this bc(1) was built with the header displayed by default. If *any* of that is the case, then this option *does* prevent bc(1) from printing the header. This is a **non-portable extension**. **-R**, **-\-no-read-prompt** : Disables the read prompt in TTY mode. (The read prompt is only enabled in TTY mode. See the **TTY MODE** section.) This is mostly for those users that do not want a read prompt or are not used to having them in bc(1). Most of those users would want to put this option in **BC_ENV_ARGS** (see the **ENVIRONMENT VARIABLES** section). This option is also useful in hash bang lines of bc(1) scripts that prompt for user input. This option does not disable the regular prompt because the read prompt is only used when the **read()** built-in function is called. These options *do* override the **BC_PROMPT** and **BC_TTY_MODE** environment variables (see the **ENVIRONMENT VARIABLES** section), but only for the read prompt. This is a **non-portable extension**. **-r** *keyword*, **-\-redefine**=*keyword* : Redefines *keyword* in order to allow it to be used as a function, variable, or array name. This is useful when this bc(1) gives parse errors when parsing scripts meant for other bc(1) implementations. The keywords this bc(1) allows to be redefined are: * **abs** * **asciify** * **continue** * **divmod** * **else** * **halt** * **irand** * **last** * **limits** * **maxibase** * **maxobase** * **maxrand** * **maxscale** * **modexp** * **print** * **rand** * **read** * **seed** * **stream** If any of those keywords are used as a function, variable, or array name in a script, use this option with the keyword as the argument. If multiple are used, use this option for all of them; it can be used multiple times. Keywords are *not* redefined when parsing the builtin math library (see the **LIBRARY** section). It is a fatal error to redefine keywords mandated by the POSIX standard (see the **STANDARDS** section). It is a fatal error to attempt to redefine words that this bc(1) does not reserve as keywords. **-S** *scale*, **-\-scale**=*scale* : Sets the builtin variable **scale** to the value *scale* assuming that *scale* is in base 10. It is a fatal error if *scale* is not a valid number. If multiple instances of this option are given, the last is used. This is a **non-portable extension**. **-s**, **-\-standard** : Process exactly the language defined by the standard (see the **STANDARDS** section) and error if any extensions are used. This is a **non-portable extension**. **-v**, **-V**, **-\-version** : Print the version information (copyright header) and exits. This is a **non-portable extension**. **-w**, **-\-warn** : Like **-s** and **-\-standard**, except that warnings (and not errors) are printed for non-standard extensions and execution continues normally. This is a **non-portable extension**. **-z**, **-\-leading-zeroes** : Makes bc(1) print all numbers greater than **-1** and less than **1**, and not equal to **0**, with a leading zero. This can be set for individual numbers with the **plz(x)**, **plznl(x)**, **pnlz(x)**, and **pnlznl(x)** functions in the extended math library (see the **LIBRARY** section). This is a **non-portable extension**. All long options are **non-portable extensions**. # STDIN If no files or expressions are given by the **-f**, **-\-file**, **-e**, or **-\-expression** options, then bc(1) reads from **stdin**. However, there are a few caveats to this. First, **stdin** is evaluated a line at a time. The only exception to this is if the parse cannot complete. That means that starting a string without ending it or starting a function, **if** statement, or loop without ending it will also cause bc(1) to not execute. Second, after an **if** statement, bc(1) doesn't know if an **else** statement will follow, so it will not execute until it knows there will not be an **else** statement. # STDOUT Any non-error output is written to **stdout**. In addition, if history (see the **HISTORY** section) and the prompt (see the **TTY MODE** section) are enabled, both are output to **stdout**. **Note**: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the **EXIT STATUS** section) if it cannot write to **stdout**, so if **stdout** is closed, as in **bc >&-**, it will quit with an error. This is done so that bc(1) can report problems when **stdout** is redirected to a file. If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect **stdout** to **/dev/null**. # STDERR Any error output is written to **stderr**. **Note**: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the **EXIT STATUS** section) if it cannot write to **stderr**, so if **stderr** is closed, as in **bc 2>&-**, it will quit with an error. This is done so that bc(1) can exit with an error code when **stderr** is redirected to a file. If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect **stderr** to **/dev/null**. # SYNTAX The syntax for bc(1) programs is mostly C-like, with some differences. This bc(1) follows the POSIX standard (see the **STANDARDS** section), which is a much more thorough resource for the language this bc(1) accepts. This section is meant to be a summary and a listing of all the extensions to the standard. In the sections below, **E** means expression, **S** means statement, and **I** means identifier. Identifiers (**I**) start with a lowercase letter and can be followed by any number (up to **BC_NAME_MAX-1**) of lowercase letters (**a-z**), digits (**0-9**), and underscores (**\_**). The regex is **\[a-z\]\[a-z0-9\_\]\***. Identifiers with more than one character (letter) are a **non-portable extension**. **ibase** is a global variable determining how to interpret constant numbers. It is the "input" base, or the number base used for interpreting input numbers. **ibase** is initially **10**. If the **-s** (**-\-standard**) and **-w** (**-\-warn**) flags were not given on the command line, the max allowable value for **ibase** is **36**. Otherwise, it is **16**. The min allowable value for **ibase** is **2**. The max allowable value for **ibase** can be queried in bc(1) programs with the **maxibase()** built-in function. **obase** is a global variable determining how to output results. It is the "output" base, or the number base used for outputting numbers. **obase** is initially **10**. The max allowable value for **obase** is **BC_BASE_MAX** and can be queried in bc(1) programs with the **maxobase()** built-in function. The min allowable value for **obase** is **0**. If **obase** is **0**, values are output in scientific notation, and if **obase** is **1**, values are output in engineering notation. Otherwise, values are output in the specified base. Outputting in scientific and engineering notations are **non-portable extensions**. The *scale* of an expression is the number of digits in the result of the expression right of the decimal point, and **scale** is a global variable that sets the precision of any operations, with exceptions. **scale** is initially **0**. **scale** cannot be negative. The max allowable value for **scale** is **BC_SCALE_MAX** and can be queried in bc(1) programs with the **maxscale()** built-in function. bc(1) has both *global* variables and *local* variables. All *local* variables are local to the function; they are parameters or are introduced in the **auto** list of a function (see the **FUNCTIONS** section). If a variable is accessed which is not a parameter or in the **auto** list, it is assumed to be *global*. If a parent function has a *local* variable version of a variable that a child function considers *global*, the value of that *global* variable in the child function is the value of the variable in the parent function, not the value of the actual *global* variable. All of the above applies to arrays as well. The value of a statement that is an expression (i.e., any of the named expressions or operands) is printed unless the lowest precedence operator is an assignment operator *and* the expression is notsurrounded by parentheses. The value that is printed is also assigned to the special variable **last**. A single dot (**.**) may also be used as a synonym for **last**. These are **non-portable extensions**. Either semicolons or newlines may separate statements. ## Comments There are two kinds of comments: 1. Block comments are enclosed in **/\*** and **\*/**. 2. Line comments go from **#** until, and not including, the next newline. This is a **non-portable extension**. ## Named Expressions The following are named expressions in bc(1): 1. Variables: **I** 2. Array Elements: **I[E]** 3. **ibase** 4. **obase** 5. **scale** 6. **seed** 7. **last** or a single dot (**.**) Numbers 6 and 7 are **non-portable extensions**. The meaning of **seed** is dependent on the current pseudo-random number generator but is guaranteed to not change except for new major versions. The *scale* and sign of the value may be significant. If a previously used **seed** value is assigned to **seed** and used again, the pseudo-random number generator is guaranteed to produce the same sequence of pseudo-random numbers as it did when the **seed** value was previously used. The exact value assigned to **seed** is not guaranteed to be returned if **seed** is queried again immediately. However, if **seed** *does* return a different value, both values, when assigned to **seed**, are guaranteed to produce the same sequence of pseudo-random numbers. This means that certain values assigned to **seed** will *not* produce unique sequences of pseudo-random numbers. The value of **seed** will change after any use of the **rand()** and **irand(E)** operands (see the *Operands* subsection below), except if the parameter passed to **irand(E)** is **0**, **1**, or negative. There is no limit to the length (number of significant decimal digits) or *scale* of the value that can be assigned to **seed**. Variables and arrays do not interfere; users can have arrays named the same as variables. This also applies to functions (see the **FUNCTIONS** section), so a user can have a variable, array, and function that all have the same name, and they will not shadow each other, whether inside of functions or not. Named expressions are required as the operand of **increment**/**decrement** operators and as the left side of **assignment** operators (see the *Operators* subsection). ## Operands The following are valid operands in bc(1): 1. Numbers (see the *Numbers* subsection below). 2. Array indices (**I[E]**). 3. **(E)**: The value of **E** (used to change precedence). 4. **sqrt(E)**: The square root of **E**. **E** must be non-negative. 5. **length(E)**: The number of significant decimal digits in **E**. Returns **1** for **0** with no decimal places. If given a string, the length of the string is returned. Passing a string to **length(E)** is a **non-portable extension**. 6. **length(I[])**: The number of elements in the array **I**. This is a **non-portable extension**. 7. **scale(E)**: The *scale* of **E**. 8. **abs(E)**: The absolute value of **E**. This is a **non-portable extension**. 9. **is_number(E)**: **1** if the given argument is a number, **0** if it is a string. This is a **non-portable extension**. 10. **is_string(E)**: **1** if the given argument is a string, **0** if it is a number. This is a **non-portable extension**. 11. **modexp(E, E, E)**: Modular exponentiation, where the first expression is the base, the second is the exponent, and the third is the modulus. All three values must be integers. The second argument must be non-negative. The third argument must be non-zero. This is a **non-portable extension**. 11. **divmod(E, E, I[])**: Division and modulus in one operation. This is for optimization. The first expression is the dividend, and the second is the divisor, which must be non-zero. The return value is the quotient, and the modulus is stored in index **0** of the provided array (the last argument). This is a **non-portable extension**. 12. **asciify(E)**: If **E** is a string, returns a string that is the first letter of its argument. If it is a number, calculates the number mod **256** and returns that number as a one-character string. This is a **non-portable extension**. 13. **asciify(I[])**: A string that is made up of the characters that would result from running **asciify(E)** on each element of the array identified by the argument. This allows creating multi-character strings and storing them. This is a **non-portable extension**. 14. **I()**, **I(E)**, **I(E, E)**, and so on, where **I** is an identifier for a non-**void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). The **E** argument(s) may also be arrays of the form **I[]**, which will automatically be turned into array references (see the *Array References* subsection of the **FUNCTIONS** section) if the corresponding parameter in the function definition is an array reference. 15. **read()**: Reads a line from **stdin** and uses that as an expression. The result of that expression is the result of the **read()** operand. This is a **non-portable extension**. 16. **maxibase()**: The max allowable **ibase**. This is a **non-portable extension**. 17. **maxobase()**: The max allowable **obase**. This is a **non-portable extension**. 18. **maxscale()**: The max allowable **scale**. This is a **non-portable extension**. 19. **line_length()**: The line length set with **BC_LINE_LENGTH** (see the **ENVIRONMENT VARIABLES** section). This is a **non-portable extension**. 20. **global_stacks()**: **0** if global stacks are not enabled with the **-g** or **-\-global-stacks** options, non-zero otherwise. See the **OPTIONS** section. This is a **non-portable extension**. 21. **leading_zero()**: **0** if leading zeroes are not enabled with the **-z** or **--leading-zeroes** options, non-zero otherwise. See the **OPTIONS** section. This is a **non-portable extension**. 22. **rand()**: A pseudo-random integer between **0** (inclusive) and **BC_RAND_MAX** (inclusive). Using this operand will change the value of **seed**. This is a **non-portable extension**. 23. **irand(E)**: A pseudo-random integer between **0** (inclusive) and the value of **E** (exclusive). If **E** is negative or is a non-integer (**E**'s *scale* is not **0**), an error is raised, and bc(1) resets (see the **RESET** section) while **seed** remains unchanged. If **E** is larger than **BC_RAND_MAX**, the higher bound is honored by generating several pseudo-random integers, multiplying them by appropriate powers of **BC_RAND_MAX+1**, and adding them together. Thus, the size of integer that can be generated with this operand is unbounded. Using this operand will change the value of **seed**, unless the value of **E** is **0** or **1**. In that case, **0** is returned, and **seed** is *not* changed. This is a **non-portable extension**. 24. **maxrand()**: The max integer returned by **rand()**. This is a **non-portable extension**. The integers generated by **rand()** and **irand(E)** are guaranteed to be as unbiased as possible, subject to the limitations of the pseudo-random number generator. **Note**: The values returned by the pseudo-random number generator with **rand()** and **irand(E)** are guaranteed to *NOT* be cryptographically secure. This is a consequence of using a seeded pseudo-random number generator. However, they *are* guaranteed to be reproducible with identical **seed** values. This means that the pseudo-random values from bc(1) should only be used where a reproducible stream of pseudo-random numbers is *ESSENTIAL*. In any other case, use a non-seeded pseudo-random number generator. ## Numbers Numbers are strings made up of digits, uppercase letters, and at most **1** period for a radix. Numbers can have up to **BC_NUM_MAX** digits. Uppercase letters are equal to **9** plus their position in the alphabet, starting from **1** (i.e., **A** equals **10**, or **9+1**). If a digit or letter makes no sense with the current value of **ibase** (i.e., they are greater than or equal to the current value of **ibase**), then the behavior depends on the existence of the **-c**/**-\-digit-clamp** or **-C**/**-\-no-digit-clamp** options (see the **OPTIONS** section), the existence and setting of the **BC_DIGIT_CLAMP** environment variable (see the **ENVIRONMENT VARIABLES** section), or the default, which can be queried with the **-h**/**-\-help** option. If clamping is off, then digits or letters that are greater than or equal to the current value of **ibase** are not changed. Instead, their given value is multiplied by the appropriate power of **ibase** and added into the number. This means that, with an **ibase** of **3**, the number **AB** is equal to **3\^1\*A+3\^0\*B**, which is **3** times **10** plus **11**, or **41**. If clamping is on, then digits or letters that are greater than or equal to the current value of **ibase** are set to the value of the highest valid digit in **ibase** before being multiplied by the appropriate power of **ibase** and added into the number. This means that, with an **ibase** of **3**, the number **AB** is equal to **3\^1\*2+3\^0\*2**, which is **3** times **2** plus **2**, or **8**. There is one exception to clamping: single-character numbers (i.e., **A** alone). Such numbers are never clamped and always take the value they would have in the highest possible **ibase**. This means that **A** alone always equals decimal **10** and **Z** alone always equals decimal **35**. This behavior is mandated by the standard (see the STANDARDS section) and is meant to provide an easy way to set the current **ibase** (with the **i** command) regardless of the current value of **ibase**. If clamping is on, and the clamped value of a character is needed, use a leading zero, i.e., for **A**, use **0A**. In addition, bc(1) accepts numbers in scientific notation. These have the form **\e\**. The exponent (the portion after the **e**) must be an integer. An example is **1.89237e9**, which is equal to **1892370000**. Negative exponents are also allowed, so **4.2890e-3** is equal to **0.0042890**. Using scientific notation is an error or warning if the **-s** or **-w**, respectively, command-line options (or equivalents) are given. **WARNING**: Both the number and the exponent in scientific notation are interpreted according to the current **ibase**, but the number is still multiplied by **10\^exponent** regardless of the current **ibase**. For example, if **ibase** is **16** and bc(1) is given the number string **FFeA**, the resulting decimal number will be **2550000000000**, and if bc(1) is given the number string **10e-4**, the resulting decimal number will be **0.0016**. Accepting input as scientific notation is a **non-portable extension**. ## Operators The following arithmetic and logical operators can be used. They are listed in order of decreasing precedence. Operators in the same group have the same precedence. **++** **-\-** : Type: Prefix and Postfix Associativity: None Description: **increment**, **decrement** **-** **!** : Type: Prefix Associativity: None Description: **negation**, **boolean not** **\$** : Type: Postfix Associativity: None Description: **truncation** **\@** : Type: Binary Associativity: Right Description: **set precision** **\^** : Type: Binary Associativity: Right Description: **power** **\*** **/** **%** : Type: Binary Associativity: Left Description: **multiply**, **divide**, **modulus** **+** **-** : Type: Binary Associativity: Left Description: **add**, **subtract** **\<\<** **\>\>** : Type: Binary Associativity: Left Description: **shift left**, **shift right** **=** **\<\<=** **\>\>=** **+=** **-=** **\*=** **/=** **%=** **\^=** **\@=** : Type: Binary Associativity: Right Description: **assignment** **==** **\<=** **\>=** **!=** **\<** **\>** : Type: Binary Associativity: Left Description: **relational** **&&** : Type: Binary Associativity: Left Description: **boolean and** **||** : Type: Binary Associativity: Left Description: **boolean or** The operators will be described in more detail below. **++** **-\-** : The prefix and postfix **increment** and **decrement** operators behave exactly like they would in C. They require a named expression (see the *Named Expressions* subsection) as an operand. The prefix versions of these operators are more efficient; use them where possible. **-** : The **negation** operator returns **0** if a user attempts to negate any expression with the value **0**. Otherwise, a copy of the expression with its sign flipped is returned. **!** : The **boolean not** operator returns **1** if the expression is **0**, or **0** otherwise. This is a **non-portable extension**. **\$** : The **truncation** operator returns a copy of the given expression with all of its *scale* removed. This is a **non-portable extension**. **\@** : The **set precision** operator takes two expressions and returns a copy of the first with its *scale* equal to the value of the second expression. That could either mean that the number is returned without change (if the *scale* of the first expression matches the value of the second expression), extended (if it is less), or truncated (if it is more). The second expression must be an integer (no *scale*) and non-negative. This is a **non-portable extension**. **\^** : The **power** operator (not the **exclusive or** operator, as it would be in C) takes two expressions and raises the first to the power of the value of the second. The *scale* of the result is equal to **scale**. The second expression must be an integer (no *scale*), and if it is negative, the first value must be non-zero. **\*** : The **multiply** operator takes two expressions, multiplies them, and returns the product. If **a** is the *scale* of the first expression and **b** is the *scale* of the second expression, the *scale* of the result is equal to **min(a+b,max(scale,a,b))** where **min()** and **max()** return the obvious values. **/** : The **divide** operator takes two expressions, divides them, and returns the quotient. The *scale* of the result shall be the value of **scale**. The second expression must be non-zero. **%** : The **modulus** operator takes two expressions, **a** and **b**, and evaluates them by 1) Computing **a/b** to current **scale** and 2) Using the result of step 1 to calculate **a-(a/b)\*b** to *scale* **max(scale+scale(b),scale(a))**. The second expression must be non-zero. **+** : The **add** operator takes two expressions, **a** and **b**, and returns the sum, with a *scale* equal to the max of the *scale*s of **a** and **b**. **-** : The **subtract** operator takes two expressions, **a** and **b**, and returns the difference, with a *scale* equal to the max of the *scale*s of **a** and **b**. **\<\<** : The **left shift** operator takes two expressions, **a** and **b**, and returns a copy of the value of **a** with its decimal point moved **b** places to the right. The second expression must be an integer (no *scale*) and non-negative. This is a **non-portable extension**. **\>\>** : The **right shift** operator takes two expressions, **a** and **b**, and returns a copy of the value of **a** with its decimal point moved **b** places to the left. The second expression must be an integer (no *scale*) and non-negative. This is a **non-portable extension**. **=** **\<\<=** **\>\>=** **+=** **-=** **\*=** **/=** **%=** **\^=** **\@=** : The **assignment** operators take two expressions, **a** and **b** where **a** is a named expression (see the *Named Expressions* subsection). For **=**, **b** is copied and the result is assigned to **a**. For all others, **a** and **b** are applied as operands to the corresponding arithmetic operator and the result is assigned to **a**. The **assignment** operators that correspond to operators that are extensions are themselves **non-portable extensions**. **==** **\<=** **\>=** **!=** **\<** **\>** : The **relational** operators compare two expressions, **a** and **b**, and if the relation holds, according to C language semantics, the result is **1**. Otherwise, it is **0**. Note that unlike in C, these operators have a lower precedence than the **assignment** operators, which means that **a=b\>c** is interpreted as **(a=b)\>c**. Also, unlike the standard (see the **STANDARDS** section) requires, these operators can appear anywhere any other expressions can be used. This allowance is a **non-portable extension**. **&&** : The **boolean and** operator takes two expressions and returns **1** if both expressions are non-zero, **0** otherwise. This is *not* a short-circuit operator. This is a **non-portable extension**. **||** : The **boolean or** operator takes two expressions and returns **1** if one of the expressions is non-zero, **0** otherwise. This is *not* a short-circuit operator. This is a **non-portable extension**. ## Statements The following items are statements: 1. **E** 2. **{** **S** **;** ... **;** **S** **}** 3. **if** **(** **E** **)** **S** 4. **if** **(** **E** **)** **S** **else** **S** 5. **while** **(** **E** **)** **S** 6. **for** **(** **E** **;** **E** **;** **E** **)** **S** 7. An empty statement 8. **break** 9. **continue** 10. **quit** 11. **halt** 12. **limits** 13. A string of characters, enclosed in double quotes 14. **print** **E** **,** ... **,** **E** 15. **stream** **E** **,** ... **,** **E** 16. **I()**, **I(E)**, **I(E, E)**, and so on, where **I** is an identifier for a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). The **E** argument(s) may also be arrays of the form **I[]**, which will automatically be turned into array references (see the *Array References* subsection of the **FUNCTIONS** section) if the corresponding parameter in the function definition is an array reference. Numbers 4, 9, 11, 12, 14, 15, and 16 are **non-portable extensions**. Also, as a **non-portable extension**, any or all of the expressions in the header of a for loop may be omitted. If the condition (second expression) is omitted, it is assumed to be a constant **1**. The **break** statement causes a loop to stop iterating and resume execution immediately following a loop. This is only allowed in loops. The **continue** statement causes a loop iteration to stop early and returns to the start of the loop, including testing the loop condition. This is only allowed in loops. The **if** **else** statement does the same thing as in C. The **quit** statement causes bc(1) to quit, even if it is on a branch that will not be executed (it is a compile-time command). **Warning**: The behavior of this bc(1) on **quit** is slightly different from other bc(1) implementations. Other bc(1) implementations will exit as soon as they finish parsing the line that a **quit** command is on. This bc(1) will execute any completed and executable statements that occur before the **quit** statement before exiting. In other words, for the bc(1) code below: for (i = 0; i < 3; ++i) i; quit Other bc(1) implementations will print nothing, and this bc(1) will print **0**, **1**, and **2** on successive lines before exiting. The **halt** statement causes bc(1) to quit, if it is executed. (Unlike **quit** if it is on a branch of an **if** statement that is not executed, bc(1) does not quit.) The **limits** statement prints the limits that this bc(1) is subject to. This is like the **quit** statement in that it is a compile-time command. An expression by itself is evaluated and printed, followed by a newline. Both scientific notation and engineering notation are available for printing the results of expressions. Scientific notation is activated by assigning **0** to **obase**, and engineering notation is activated by assigning **1** to **obase**. To deactivate them, just assign a different value to **obase**. Scientific notation and engineering notation are disabled if bc(1) is run with either the **-s** or **-w** command-line options (or equivalents). Printing numbers in scientific notation and/or engineering notation is a **non-portable extension**. ## Strings If strings appear as a statement by themselves, they are printed without a trailing newline. In addition to appearing as a lone statement by themselves, strings can be assigned to variables and array elements. They can also be passed to functions in variable parameters. If any statement that expects a string is given a variable that had a string assigned to it, the statement acts as though it had received a string. If any math operation is attempted on a string or a variable or array element that has been assigned a string, an error is raised, and bc(1) resets (see the **RESET** section). Assigning strings to variables and array elements and passing them to functions are **non-portable extensions**. ## Print Statement The "expressions" in a **print** statement may also be strings. If they are, there are backslash escape sequences that are interpreted specially. What those sequences are, and what they cause to be printed, are shown below: **\\a**: **\\a** **\\b**: **\\b** **\\\\**: **\\** **\\e**: **\\** **\\f**: **\\f** **\\n**: **\\n** **\\q**: **"** **\\r**: **\\r** **\\t**: **\\t** Any other character following a backslash causes the backslash and character to be printed as-is. Any non-string expression in a print statement shall be assigned to **last**, like any other expression that is printed. ## Stream Statement The expressions in a **stream** statement may also be strings. If a **stream** statement is given a string, it prints the string as though the string had appeared as its own statement. In other words, the **stream** statement prints strings normally, without a newline. If a **stream** statement is given a number, a copy of it is truncated and its absolute value is calculated. The result is then printed as though **obase** is **256** and each digit is interpreted as an 8-bit ASCII character, making it a byte stream. ## Order of Evaluation All expressions in a statment are evaluated left to right, except as necessary to maintain order of operations. This means, for example, assuming that **i** is equal to **0**, in the expression a[i++] = i++ the first (or 0th) element of **a** is set to **1**, and **i** is equal to **2** at the end of the expression. This includes function arguments. Thus, assuming **i** is equal to **0**, this means that in the expression x(i++, i++) the first argument passed to **x()** is **0**, and the second argument is **1**, while **i** is equal to **2** before the function starts executing. # FUNCTIONS Function definitions are as follows: ``` define I(I,...,I){ auto I,...,I S;...;S return(E) } ``` Any **I** in the parameter list or **auto** list may be replaced with **I[]** to make a parameter or **auto** var an array, and any **I** in the parameter list may be replaced with **\*I[]** to make a parameter an array reference. Callers of functions that take array references should not put an asterisk in the call; they must be called with just **I[]** like normal array parameters and will be automatically converted into references. As a **non-portable extension**, the opening brace of a **define** statement may appear on the next line. As a **non-portable extension**, the return statement may also be in one of the following forms: 1. **return** 2. **return** **(** **)** 3. **return** **E** The first two, or not specifying a **return** statement, is equivalent to **return (0)**, unless the function is a **void** function (see the *Void Functions* subsection below). ## Void Functions Functions can also be **void** functions, defined as follows: ``` define void I(I,...,I){ auto I,...,I S;...;S return } ``` They can only be used as standalone expressions, where such an expression would be printed alone, except in a print statement. Void functions can only use the first two **return** statements listed above. They can also omit the return statement entirely. The word "void" is not treated as a keyword; it is still possible to have variables, arrays, and functions named **void**. The word "void" is only treated specially right after the **define** keyword. This is a **non-portable extension**. ## Array References For any array in the parameter list, if the array is declared in the form ``` *I[] ``` it is a **reference**. Any changes to the array in the function are reflected, when the function returns, to the array that was passed in. Other than this, all function arguments are passed by value. This is a **non-portable extension**. # LIBRARY All of the functions below, including the functions in the extended math library (see the *Extended Library* subsection below), are available when the **-l** or **-\-mathlib** command-line flags are given, except that the extended math library is not available when the **-s** option, the **-w** option, or equivalents are given. ## Standard Library The standard (see the **STANDARDS** section) defines the following functions for the math library: **s(x)** : Returns the sine of **x**, which is assumed to be in radians. This is a transcendental function (see the *Transcendental Functions* subsection below). **c(x)** : Returns the cosine of **x**, which is assumed to be in radians. This is a transcendental function (see the *Transcendental Functions* subsection below). **a(x)** : Returns the arctangent of **x**, in radians. This is a transcendental function (see the *Transcendental Functions* subsection below). **l(x)** : Returns the natural logarithm of **x**. This is a transcendental function (see the *Transcendental Functions* subsection below). **e(x)** : Returns the mathematical constant **e** raised to the power of **x**. This is a transcendental function (see the *Transcendental Functions* subsection below). **j(x, n)** : Returns the bessel integer order **n** (truncated) of **x**. This is a transcendental function (see the *Transcendental Functions* subsection below). ## Extended Library The extended library is *not* loaded when the **-s**/**-\-standard** or **-w**/**-\-warn** options are given since they are not part of the library defined by the standard (see the **STANDARDS** section). The extended library is a **non-portable extension**. **p(x, y)** : Calculates **x** to the power of **y**, even if **y** is not an integer, and returns the result to the current **scale**. It is an error if **y** is negative and **x** is **0**. This is a transcendental function (see the *Transcendental Functions* subsection below). **r(x, p)** : Returns **x** rounded to **p** decimal places according to the rounding mode round half away from **0** (https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero). **ceil(x, p)** : Returns **x** rounded to **p** decimal places according to the rounding mode round away from **0** (https://en.wikipedia.org/wiki/Rounding#Rounding_away_from_zero). **f(x)** : Returns the factorial of the truncated absolute value of **x**. **max(a, b)** : Returns **a** if **a** is greater than **b**; otherwise, returns **b**. **min(a, b)** : Returns **a** if **a** is less than **b**; otherwise, returns **b**. **perm(n, k)** : Returns the permutation of the truncated absolute value of **n** of the truncated absolute value of **k**, if **k \<= n**. If not, it returns **0**. **comb(n, k)** : Returns the combination of the truncated absolute value of **n** of the truncated absolute value of **k**, if **k \<= n**. If not, it returns **0**. **fib(n)** : Returns the Fibonacci number of the truncated absolute value of **n**. **l2(x)** : Returns the logarithm base **2** of **x**. This is a transcendental function (see the *Transcendental Functions* subsection below). **l10(x)** : Returns the logarithm base **10** of **x**. This is a transcendental function (see the *Transcendental Functions* subsection below). **log(x, b)** : Returns the logarithm base **b** of **x**. This is a transcendental function (see the *Transcendental Functions* subsection below). **cbrt(x)** : Returns the cube root of **x**. **root(x, n)** : Calculates the truncated value of **n**, **r**, and returns the **r**th root of **x** to the current **scale**. If **r** is **0** or negative, this raises an error and causes bc(1) to reset (see the **RESET** section). It also raises an error and causes bc(1) to reset if **r** is even and **x** is negative. **gcd(a, b)** : Returns the greatest common divisor (factor) of the truncated absolute value of **a** and the truncated absolute value of **b**. **lcm(a, b)** : Returns the least common multiple of the truncated absolute value of **a** and the truncated absolute value of **b**. **pi(p)** : Returns **pi** to **p** decimal places. This is a transcendental function (see the *Transcendental Functions* subsection below). **t(x)** : Returns the tangent of **x**, which is assumed to be in radians. This is a transcendental function (see the *Transcendental Functions* subsection below). **a2(y, x)** : Returns the arctangent of **y/x**, in radians. If both **y** and **x** are equal to **0**, it raises an error and causes bc(1) to reset (see the **RESET** section). Otherwise, if **x** is greater than **0**, it returns **a(y/x)**. If **x** is less than **0**, and **y** is greater than or equal to **0**, it returns **a(y/x)+pi**. If **x** is less than **0**, and **y** is less than **0**, it returns **a(y/x)-pi**. If **x** is equal to **0**, and **y** is greater than **0**, it returns **pi/2**. If **x** is equal to **0**, and **y** is less than **0**, it returns **-pi/2**. This function is the same as the **atan2()** function in many programming languages. This is a transcendental function (see the *Transcendental Functions* subsection below). **sin(x)** : Returns the sine of **x**, which is assumed to be in radians. This is an alias of **s(x)**. This is a transcendental function (see the *Transcendental Functions* subsection below). **cos(x)** : Returns the cosine of **x**, which is assumed to be in radians. This is an alias of **c(x)**. This is a transcendental function (see the *Transcendental Functions* subsection below). **tan(x)** : Returns the tangent of **x**, which is assumed to be in radians. If **x** is equal to **1** or **-1**, this raises an error and causes bc(1) to reset (see the **RESET** section). This is an alias of **t(x)**. This is a transcendental function (see the *Transcendental Functions* subsection below). **atan(x)** : Returns the arctangent of **x**, in radians. This is an alias of **a(x)**. This is a transcendental function (see the *Transcendental Functions* subsection below). **atan2(y, x)** : Returns the arctangent of **y/x**, in radians. If both **y** and **x** are equal to **0**, it raises an error and causes bc(1) to reset (see the **RESET** section). Otherwise, if **x** is greater than **0**, it returns **a(y/x)**. If **x** is less than **0**, and **y** is greater than or equal to **0**, it returns **a(y/x)+pi**. If **x** is less than **0**, and **y** is less than **0**, it returns **a(y/x)-pi**. If **x** is equal to **0**, and **y** is greater than **0**, it returns **pi/2**. If **x** is equal to **0**, and **y** is less than **0**, it returns **-pi/2**. This function is the same as the **atan2()** function in many programming languages. This is an alias of **a2(y, x)**. This is a transcendental function (see the *Transcendental Functions* subsection below). **r2d(x)** : Converts **x** from radians to degrees and returns the result. This is a transcendental function (see the *Transcendental Functions* subsection below). **d2r(x)** : Converts **x** from degrees to radians and returns the result. This is a transcendental function (see the *Transcendental Functions* subsection below). **frand(p)** : Generates a pseudo-random integer between **0** (inclusive) and **1** (exclusive) with the number of decimal digits after the decimal point equal to the truncated absolute value of **p**. If **p** is not **0**, then calling this function will change the value of **seed**. If **p** is **0**, then **0** is returned, and **seed** is *not* changed. **ifrand(i, p)** : Generates a pseudo-random integer that is between **0** (inclusive) and the truncated absolute value of **i** (exclusive) with the number of decimal digits after the decimal point equal to the truncated absolute value of **p**. If the absolute value of **i** is greater than or equal to **2**, and **p** is not **0**, then calling this function will change the value of **seed**; otherwise, **0** is returned, and **seed** is not changed. **i2rand(a, b)** : Takes the truncated value of **a** and **b** and uses them as inclusive bounds to enerate a pseudo-random integer. If the difference of the truncated values of **a** and **b** is **0**, then the truncated value is returned, and **seed** is *not* changed. Otherwise, this function will change the value of **seed**. **srand(x)** : Returns **x** with its sign flipped with probability **0.5**. In other words, it randomizes the sign of **x**. **brand()** : Returns a random boolean value (either **0** or **1**). **band(a, b)** : Takes the truncated absolute value of both **a** and **b** and calculates and returns the result of the bitwise **and** operation between them. If you want to use signed two's complement arguments, use **s2u(x)** to convert. **bor(a, b)** : Takes the truncated absolute value of both **a** and **b** and calculates and returns the result of the bitwise **or** operation between them. If you want to use signed two's complement arguments, use **s2u(x)** to convert. **bxor(a, b)** : Takes the truncated absolute value of both **a** and **b** and calculates and returns the result of the bitwise **xor** operation between them. If you want to use signed two's complement arguments, use **s2u(x)** to convert. **bshl(a, b)** : Takes the truncated absolute value of both **a** and **b** and calculates and returns the result of **a** bit-shifted left by **b** places. If you want to use signed two's complement arguments, use **s2u(x)** to convert. **bshr(a, b)** : Takes the truncated absolute value of both **a** and **b** and calculates and returns the truncated result of **a** bit-shifted right by **b** places. If you want to use signed two's complement arguments, use **s2u(x)** to convert. **bnotn(x, n)** : Takes the truncated absolute value of **x** and does a bitwise not as though it has the same number of bytes as the truncated absolute value of **n**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bnot8(x)** : Does a bitwise not of the truncated absolute value of **x** as though it has - **8** binary digits (1 unsigned byte). + **8** binary digits (**1** unsigned byte). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bnot16(x)** : Does a bitwise not of the truncated absolute value of **x** as though it has - **16** binary digits (2 unsigned bytes). + **16** binary digits (**2** unsigned bytes). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bnot32(x)** : Does a bitwise not of the truncated absolute value of **x** as though it has - **32** binary digits (4 unsigned bytes). + **32** binary digits (**4** unsigned bytes). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bnot64(x)** : Does a bitwise not of the truncated absolute value of **x** as though it has - **64** binary digits (8 unsigned bytes). + **64** binary digits (**8** unsigned bytes). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bnot(x)** : Does a bitwise not of the truncated absolute value of **x** as though it has the minimum number of power of two unsigned bytes. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brevn(x, n)** : Runs a bit reversal on the truncated absolute value of **x** as though it has the same number of 8-bit bytes as the truncated absolute value of **n**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brev8(x)** : Runs a bit reversal on the truncated absolute value of **x** as though it - has 8 binary digits (1 unsigned byte). + has 8 binary digits (**1** unsigned byte). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brev16(x)** : Runs a bit reversal on the truncated absolute value of **x** as though it - has 16 binary digits (2 unsigned bytes). + has 16 binary digits (**2** unsigned bytes). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brev32(x)** : Runs a bit reversal on the truncated absolute value of **x** as though it - has 32 binary digits (4 unsigned bytes). + has 32 binary digits (**4** unsigned bytes). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brev64(x)** : Runs a bit reversal on the truncated absolute value of **x** as though it - has 64 binary digits (8 unsigned bytes). + has 64 binary digits (**8** unsigned bytes). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brev(x)** : Runs a bit reversal on the truncated absolute value of **x** as though it has the minimum number of power of two unsigned bytes. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **broln(x, p, n)** : Does a left bitwise rotatation of the truncated absolute value of **x**, as though it has the same number of unsigned 8-bit bytes as the truncated absolute value of **n**, by the number of places equal to the truncated absolute value of **p** modded by the **2** to the power of the number of binary digits in **n** 8-bit bytes. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brol8(x, p)** : Does a left bitwise rotatation of the truncated absolute value of **x**, as though it has **8** binary digits (**1** unsigned byte), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **8**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brol16(x, p)** : Does a left bitwise rotatation of the truncated absolute value of **x**, as though it has **16** binary digits (**2** unsigned bytes), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **16**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brol32(x, p)** : Does a left bitwise rotatation of the truncated absolute value of **x**, as - though it has **32** binary digits (**2** unsigned bytes), by the number of + though it has **32** binary digits (**4** unsigned bytes), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **32**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brol64(x, p)** : Does a left bitwise rotatation of the truncated absolute value of **x**, as - though it has **64** binary digits (**2** unsigned bytes), by the number of + though it has **64** binary digits (**8** unsigned bytes), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **64**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brol(x, p)** : Does a left bitwise rotatation of the truncated absolute value of **x**, as though it has the minimum number of power of two unsigned 8-bit bytes, by the number of places equal to the truncated absolute value of **p** modded by 2 to the power of the number of binary digits in the minimum number of 8-bit bytes. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brorn(x, p, n)** : Does a right bitwise rotatation of the truncated absolute value of **x**, as though it has the same number of unsigned 8-bit bytes as the truncated absolute value of **n**, by the number of places equal to the truncated absolute value of **p** modded by the **2** to the power of the number of binary digits in **n** 8-bit bytes. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bror8(x, p)** : Does a right bitwise rotatation of the truncated absolute value of **x**, as though it has **8** binary digits (**1** unsigned byte), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **8**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bror16(x, p)** : Does a right bitwise rotatation of the truncated absolute value of **x**, as though it has **16** binary digits (**2** unsigned bytes), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **16**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bror32(x, p)** : Does a right bitwise rotatation of the truncated absolute value of **x**, as though it has **32** binary digits (**2** unsigned bytes), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **32**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bror64(x, p)** : Does a right bitwise rotatation of the truncated absolute value of **x**, as though it has **64** binary digits (**2** unsigned bytes), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **64**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bror(x, p)** : Does a right bitwise rotatation of the truncated absolute value of **x**, as though it has the minimum number of power of two unsigned 8-bit bytes, by the number of places equal to the truncated absolute value of **p** modded by 2 to the power of the number of binary digits in the minimum number of 8-bit bytes. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bmodn(x, n)** : Returns the modulus of the truncated absolute value of **x** by **2** to the power of the multiplication of the truncated absolute value of **n** and **8**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bmod8(x, n)** : Returns the modulus of the truncated absolute value of **x** by **2** to the power of **8**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bmod16(x, n)** : Returns the modulus of the truncated absolute value of **x** by **2** to the power of **16**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bmod32(x, n)** : Returns the modulus of the truncated absolute value of **x** by **2** to the power of **32**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bmod64(x, n)** : Returns the modulus of the truncated absolute value of **x** by **2** to the power of **64**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bunrev(t)** : Assumes **t** is a bitwise-reversed number with an extra set bit one place more significant than the real most significant bit (which was the least significant bit in the original number). This number is reversed and returned without the extra set bit. This function is used to implement other bitwise functions; it is not meant to be used by users, but it can be. **plz(x)** : If **x** is not equal to **0** and greater that **-1** and less than **1**, it is printed with a leading zero, regardless of the use of the **-z** option (see the **OPTIONS** section) and without a trailing newline. Otherwise, **x** is printed normally, without a trailing newline. **plznl(x)** : If **x** is not equal to **0** and greater that **-1** and less than **1**, it is printed with a leading zero, regardless of the use of the **-z** option (see the **OPTIONS** section) and with a trailing newline. Otherwise, **x** is printed normally, with a trailing newline. **pnlz(x)** : If **x** is not equal to **0** and greater that **-1** and less than **1**, it is printed without a leading zero, regardless of the use of the **-z** option (see the **OPTIONS** section) and without a trailing newline. Otherwise, **x** is printed normally, without a trailing newline. **pnlznl(x)** : If **x** is not equal to **0** and greater that **-1** and less than **1**, it is printed without a leading zero, regardless of the use of the **-z** option (see the **OPTIONS** section) and with a trailing newline. Otherwise, **x** is printed normally, with a trailing newline. **ubytes(x)** : Returns the numbers of unsigned integer bytes required to hold the truncated absolute value of **x**. **sbytes(x)** : Returns the numbers of signed, two's-complement integer bytes required to hold the truncated value of **x**. **s2u(x)** : Returns **x** if it is non-negative. If it *is* negative, then it calculates what **x** would be as a 2's-complement signed integer and returns the non-negative integer that would have the same representation in binary. **s2un(x,n)** : Returns **x** if it is non-negative. If it *is* negative, then it calculates what **x** would be as a 2's-complement signed integer with **n** bytes and returns the non-negative integer that would have the same representation in binary. If **x** cannot fit into **n** 2's-complement signed bytes, it is truncated to fit. **hex(x)** : Outputs the hexadecimal (base **16**) representation of **x**. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **binary(x)** : Outputs the binary (base **2**) representation of **x**. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **output(x, b)** : Outputs the base **b** representation of **x**. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **uint(x)** : Outputs the representation, in binary and hexadecimal, of **x** as an unsigned integer in as few power of two bytes as possible. Both outputs are split into bytes separated by spaces. If **x** is not an integer or is negative, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **int(x)** : Outputs the representation, in binary and hexadecimal, of **x** as a signed, two's-complement integer in as few power of two bytes as possible. Both outputs are split into bytes separated by spaces. If **x** is not an integer, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **uintn(x, n)** : Outputs the representation, in binary and hexadecimal, of **x** as an unsigned integer in **n** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer, is negative, or cannot fit into **n** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **intn(x, n)** : Outputs the representation, in binary and hexadecimal, of **x** as a signed, two's-complement integer in **n** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer or cannot fit into **n** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **uint8(x)** : Outputs the representation, in binary and hexadecimal, of **x** as an unsigned integer in **1** byte. Both outputs are split into bytes separated by spaces. If **x** is not an integer, is negative, or cannot fit into **1** byte, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **int8(x)** : Outputs the representation, in binary and hexadecimal, of **x** as a signed, two's-complement integer in **1** byte. Both outputs are split into bytes separated by spaces. If **x** is not an integer or cannot fit into **1** byte, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **uint16(x)** : Outputs the representation, in binary and hexadecimal, of **x** as an unsigned integer in **2** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer, is negative, or cannot fit into **2** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **int16(x)** : Outputs the representation, in binary and hexadecimal, of **x** as a signed, two's-complement integer in **2** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer or cannot fit into **2** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **uint32(x)** : Outputs the representation, in binary and hexadecimal, of **x** as an unsigned integer in **4** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer, is negative, or cannot fit into **4** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **int32(x)** : Outputs the representation, in binary and hexadecimal, of **x** as a signed, two's-complement integer in **4** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer or cannot fit into **4** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **uint64(x)** : Outputs the representation, in binary and hexadecimal, of **x** as an unsigned integer in **8** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer, is negative, or cannot fit into **8** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **int64(x)** : Outputs the representation, in binary and hexadecimal, of **x** as a signed, two's-complement integer in **8** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer or cannot fit into **8** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **hex_uint(x, n)** : Outputs the representation of the truncated absolute value of **x** as an unsigned integer in hexadecimal using **n** bytes. Not all of the value will be output if **n** is too small. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **binary_uint(x, n)** : Outputs the representation of the truncated absolute value of **x** as an unsigned integer in binary using **n** bytes. Not all of the value will be output if **n** is too small. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **output_uint(x, n)** : Outputs the representation of the truncated absolute value of **x** as an unsigned integer in the current **obase** (see the **SYNTAX** section) using **n** bytes. Not all of the value will be output if **n** is too small. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **output_byte(x, i)** : Outputs byte **i** of the truncated absolute value of **x**, where **0** is the least significant byte and **number_of_bytes - 1** is the most significant byte. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). ## Transcendental Functions All transcendental functions can return slightly inaccurate results, up to 1 ULP (https://en.wikipedia.org/wiki/Unit_in_the_last_place). This is unavoidable, and the article at https://people.eecs.berkeley.edu/~wkahan/LOG10HAF.TXT explains why it is impossible and unnecessary to calculate exact results for the transcendental functions. Because of the possible inaccuracy, I recommend that users call those functions with the precision (**scale**) set to at least 1 higher than is necessary. If exact results are *absolutely* required, users can double the precision (**scale**) and then truncate. The transcendental functions in the standard math library are: * **s(x)** * **c(x)** * **a(x)** * **l(x)** * **e(x)** * **j(x, n)** The transcendental functions in the extended math library are: * **l2(x)** * **l10(x)** * **log(x, b)** * **pi(p)** * **t(x)** * **a2(y, x)** * **sin(x)** * **cos(x)** * **tan(x)** * **atan(x)** * **atan2(y, x)** * **r2d(x)** * **d2r(x)** # RESET When bc(1) encounters an error or a signal that it has a non-default handler for, it resets. This means that several things happen. First, any functions that are executing are stopped and popped off the stack. The behavior is not unlike that of exceptions in programming languages. Then the execution point is set so that any code waiting to execute (after all functions returned) is skipped. Thus, when bc(1) resets, it skips any remaining code waiting to be executed. Then, if it is interactive mode, and the error was not a fatal error (see the **EXIT STATUS** section), it asks for more input; otherwise, it exits with the appropriate return code. Note that this reset behavior is different from the GNU bc(1), which attempts to start executing the statement right after the one that caused an error. # PERFORMANCE Most bc(1) implementations use **char** types to calculate the value of **1** decimal digit at a time, but that can be slow. This bc(1) does something different. It uses large integers to calculate more than **1** decimal digit at a time. If built in a environment where **BC_LONG_BIT** (see the **LIMITS** section) is **64**, then each integer has **9** decimal digits. If built in an environment where **BC_LONG_BIT** is **32** then each integer has **4** decimal digits. This value (the number of decimal digits per large integer) is called **BC_BASE_DIGS**. The actual values of **BC_LONG_BIT** and **BC_BASE_DIGS** can be queried with the **limits** statement. In addition, this bc(1) uses an even larger integer for overflow checking. This integer type depends on the value of **BC_LONG_BIT**, but is always at least twice as large as the integer type used to store digits. # LIMITS The following are the limits on bc(1): **BC_LONG_BIT** : The number of bits in the **long** type in the environment where bc(1) was built. This determines how many decimal digits can be stored in a single large integer (see the **PERFORMANCE** section). **BC_BASE_DIGS** : The number of decimal digits per large integer (see the **PERFORMANCE** section). Depends on **BC_LONG_BIT**. **BC_BASE_POW** : The max decimal number that each large integer can store (see **BC_BASE_DIGS**) plus **1**. Depends on **BC_BASE_DIGS**. **BC_OVERFLOW_MAX** : The max number that the overflow type (see the **PERFORMANCE** section) can hold. Depends on **BC_LONG_BIT**. **BC_BASE_MAX** : The maximum output base. Set at **BC_BASE_POW**. **BC_DIM_MAX** : The maximum size of arrays. Set at **SIZE_MAX-1**. **BC_SCALE_MAX** : The maximum **scale**. Set at **BC_OVERFLOW_MAX-1**. **BC_STRING_MAX** : The maximum length of strings. Set at **BC_OVERFLOW_MAX-1**. **BC_NAME_MAX** : The maximum length of identifiers. Set at **BC_OVERFLOW_MAX-1**. **BC_NUM_MAX** : The maximum length of a number (in decimal digits), which includes digits after the decimal point. Set at **BC_OVERFLOW_MAX-1**. **BC_RAND_MAX** : The maximum integer (inclusive) returned by the **rand()** operand. Set at **2\^BC_LONG_BIT-1**. Exponent : The maximum allowable exponent (positive or negative). Set at **BC_OVERFLOW_MAX**. Number of vars : The maximum number of vars/arrays. Set at **SIZE_MAX-1**. The actual values can be queried with the **limits** statement. These limits are meant to be effectively non-existent; the limits are so large (at least on 64-bit machines) that there should not be any point at which they become a problem. In fact, memory should be exhausted before these limits should be hit. # ENVIRONMENT VARIABLES As **non-portable extensions**, bc(1) recognizes the following environment variables: **POSIXLY_CORRECT** : If this variable exists (no matter the contents), bc(1) behaves as if the **-s** option was given. **BC_ENV_ARGS** : This is another way to give command-line arguments to bc(1). They should be in the same format as all other command-line arguments. These are always processed first, so any files given in **BC_ENV_ARGS** will be processed before arguments and files given on the command-line. This gives the user the ability to set up "standard" options and files to be used at every invocation. The most useful thing for such files to contain would be useful functions that the user might want every time bc(1) runs. The code that parses **BC_ENV_ARGS** will correctly handle quoted arguments, but it does not understand escape sequences. For example, the string **"/home/gavin/some bc file.bc"** will be correctly parsed, but the string **"/home/gavin/some \"bc\" file.bc"** will include the backslashes. The quote parsing will handle either kind of quotes, **'** or **"**. Thus, if you have a file with any number of single quotes in the name, you can use double quotes as the outside quotes, as in **"some 'bc' file.bc"**, and vice versa if you have a file with double quotes. However, handling a file with both kinds of quotes in **BC_ENV_ARGS** is not supported due to the complexity of the parsing, though such files are still supported on the command-line where the parsing is done by the shell. **BC_LINE_LENGTH** : If this environment variable exists and contains an integer that is greater than **1** and is less than **UINT16_MAX** (**2\^16-1**), bc(1) will output lines to that length, including the backslash (**\\**). The default line length is **70**. The special value of **0** will disable line length checking and print numbers without regard to line length and without backslashes and newlines. **BC_BANNER** : If this environment variable exists and contains an integer, then a non-zero value activates the copyright banner when bc(1) is in interactive mode, while zero deactivates it. If bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section), then this environment variable has no effect because bc(1) does not print the banner when not in interactive mode. This environment variable overrides the default, which can be queried with the **-h** or **-\-help** options. **BC_SIGINT_RESET** : If bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section), then this environment variable has no effect because bc(1) exits on **SIGINT** when not in interactive mode. However, when bc(1) is in interactive mode, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) reset on **SIGINT**, rather than exit, and zero makes bc(1) exit. If this environment variable exists and is *not* an integer, then bc(1) will exit on **SIGINT**. This environment variable overrides the default, which can be queried with the **-h** or **-\-help** options. **BC_TTY_MODE** : If TTY mode is *not* available (see the **TTY MODE** section), then this environment variable has no effect. However, when TTY mode is available, then if this environment variable exists and contains an integer, then a non-zero value makes bc(1) use TTY mode, and zero makes bc(1) not use TTY mode. This environment variable overrides the default, which can be queried with the **-h** or **-\-help** options. **BC_PROMPT** : If TTY mode is *not* available (see the **TTY MODE** section), then this environment variable has no effect. However, when TTY mode is available, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) use a prompt, and zero or a non-integer makes bc(1) not use a prompt. If this environment variable does not exist and **BC_TTY_MODE** does, then the value of the **BC_TTY_MODE** environment variable is used. This environment variable and the **BC_TTY_MODE** environment variable override the default, which can be queried with the **-h** or **-\-help** options. **BC_EXPR_EXIT** : If any expressions or expression files are given on the command-line with **-e**, **-\-expression**, **-f**, or **-\-file**, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) exit after executing the expressions and expression files, and a zero value makes bc(1) not exit. This environment variable overrides the default, which can be queried with the **-h** or **-\-help** options. **BC_DIGIT_CLAMP** : When parsing numbers and if this environment variable exists and contains an integer, a non-zero value makes bc(1) clamp digits that are greater than or equal to the current **ibase** so that all such digits are considered equal to the **ibase** minus 1, and a zero value disables such clamping so that those digits are always equal to their value, which is multiplied by the power of the **ibase**. This never applies to single-digit numbers, as per the standard (see the **STANDARDS** section). This environment variable overrides the default, which can be queried with the **-h** or **-\-help** options. # EXIT STATUS bc(1) returns the following exit statuses: **0** : No error. **1** : A math error occurred. This follows standard practice of using **1** for expected errors, since math errors will happen in the process of normal execution. Math errors include divide by **0**, taking the square root of a negative number, using a negative number as a bound for the pseudo-random number generator, attempting to convert a negative number to a hardware integer, overflow when converting a number to a hardware integer, overflow when calculating the size of a number, and attempting to use a non-integer where an integer is required. Converting to a hardware integer happens for the second operand of the power (**\^**), places (**\@**), left shift (**\<\<**), and right shift (**\>\>**) operators and their corresponding assignment operators. **2** : A parse error occurred. Parse errors include unexpected **EOF**, using an invalid character, failing to find the end of a string or comment, using a token where it is invalid, giving an invalid expression, giving an invalid print statement, giving an invalid function definition, attempting to assign to an expression that is not a named expression (see the *Named Expressions* subsection of the **SYNTAX** section), giving an invalid **auto** list, having a duplicate **auto**/function parameter, failing to find the end of a code block, attempting to return a value from a **void** function, attempting to use a variable as a reference, and using any extensions when the option **-s** or any equivalents were given. **3** : A runtime error occurred. Runtime errors include assigning an invalid number to any global (**ibase**, **obase**, or **scale**), giving a bad expression to a **read()** call, calling **read()** inside of a **read()** call, type errors, passing the wrong number of arguments to functions, attempting to call an undefined function, and attempting to use a **void** function call as a value in an expression. **4** : A fatal error occurred. Fatal errors include memory allocation errors, I/O errors, failing to open files, attempting to use files that do not have only ASCII characters (bc(1) only accepts ASCII characters), attempting to open a directory as a file, and giving invalid command-line options. The exit status **4** is special; when a fatal error occurs, bc(1) always exits and returns **4**, no matter what mode bc(1) is in. The other statuses will only be returned when bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section), since bc(1) resets its state (see the **RESET** section) and accepts more input when one of those errors occurs in interactive mode. This is also the case when interactive mode is forced by the **-i** flag or **-\-interactive** option. These exit statuses allow bc(1) to be used in shell scripting with error checking, and its normal behavior can be forced by using the **-i** flag or **-\-interactive** option. # INTERACTIVE MODE Per the standard (see the **STANDARDS** section), bc(1) has an interactive mode and a non-interactive mode. Interactive mode is turned on automatically when both **stdin** and **stdout** are hooked to a terminal, but the **-i** flag and **-\-interactive** option can turn it on in other situations. In interactive mode, bc(1) attempts to recover from errors (see the **RESET** section), and in normal execution, flushes **stdout** as soon as execution is done for the current input. bc(1) may also reset on **SIGINT** instead of exit, depending on the contents of, or default for, the **BC_SIGINT_RESET** environment variable (see the **ENVIRONMENT VARIABLES** section). # TTY MODE If **stdin**, **stdout**, and **stderr** are all connected to a TTY, then "TTY mode" is considered to be available, and thus, bc(1) can turn on TTY mode, subject to some settings. If there is the environment variable **BC_TTY_MODE** in the environment (see the **ENVIRONMENT VARIABLES** section), then if that environment variable contains a non-zero integer, bc(1) will turn on TTY mode when **stdin**, **stdout**, and **stderr** are all connected to a TTY. If the **BC_TTY_MODE** environment variable exists but is *not* a non-zero integer, then bc(1) will not turn TTY mode on. If the environment variable **BC_TTY_MODE** does *not* exist, the default setting is used. The default setting can be queried with the **-h** or **-\-help** options. TTY mode is different from interactive mode because interactive mode is required in the bc(1) standard (see the **STANDARDS** section), and interactive mode requires only **stdin** and **stdout** to be connected to a terminal. ## Prompt If TTY mode is available, then a prompt can be enabled. Like TTY mode itself, it can be turned on or off with an environment variable: **BC_PROMPT** (see the **ENVIRONMENT VARIABLES** section). If the environment variable **BC_PROMPT** exists and is a non-zero integer, then the prompt is turned on when **stdin**, **stdout**, and **stderr** are connected to a TTY and the **-P** and **-\-no-prompt** options were not used. The read prompt will be turned on under the same conditions, except that the **-R** and **-\-no-read-prompt** options must also not be used. However, if **BC_PROMPT** does not exist, the prompt can be enabled or disabled with the **BC_TTY_MODE** environment variable, the **-P** and **-\-no-prompt** options, and the **-R** and **-\-no-read-prompt** options. See the **ENVIRONMENT VARIABLES** and **OPTIONS** sections for more details. # SIGNAL HANDLING Sending a **SIGINT** will cause bc(1) to do one of two things. If bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section), or the **BC_SIGINT_RESET** environment variable (see the **ENVIRONMENT VARIABLES** section), or its default, is either not an integer or it is zero, bc(1) will exit. However, if bc(1) is in interactive mode, and the **BC_SIGINT_RESET** or its default is an integer and non-zero, then bc(1) will stop executing the current input and reset (see the **RESET** section) upon receiving a **SIGINT**. Note that "current input" can mean one of two things. If bc(1) is processing input from **stdin** in interactive mode, it will ask for more input. If bc(1) is processing input from a file in interactive mode, it will stop processing the file and start processing the next file, if one exists, or ask for input from **stdin** if no other file exists. This means that if a **SIGINT** is sent to bc(1) as it is executing a file, it can seem as though bc(1) did not respond to the signal since it will immediately start executing the next file. This is by design; most files that users execute when interacting with bc(1) have function definitions, which are quick to parse. If a file takes a long time to execute, there may be a bug in that file. The rest of the files could still be executed without problem, allowing the user to continue. **SIGTERM** and **SIGQUIT** cause bc(1) to clean up and exit, and it uses the default handler for all other signals. # SEE ALSO dc(1) # STANDARDS bc(1) is compliant with the IEEE Std 1003.1-2017 (“POSIX.1-2017”) specification at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html . The flags **-efghiqsvVw**, all long options, and the extensions noted above are extensions to that specification. In addition, the behavior of the **quit** implements an interpretation of that specification that is different from all known implementations. For more information see the **Statements** subsection of the **SYNTAX** section. Note that the specification explicitly says that bc(1) only accepts numbers that use a period (**.**) as a radix point, regardless of the value of **LC_NUMERIC**. # BUGS Before version **6.1.0**, this bc(1) had incorrect behavior for the **quit** statement. No other bugs are known. Report bugs at https://git.gavinhoward.com/gavin/bc . # AUTHORS Gavin D. Howard and contributors. diff --git a/contrib/bc/manuals/bc/N.1 b/contrib/bc/manuals/bc/N.1 index 79e053ec41fa..7628c50cb4fb 100644 --- a/contrib/bc/manuals/bc/N.1 +++ b/contrib/bc/manuals/bc/N.1 @@ -1,2996 +1,2938 @@ .\" .\" SPDX-License-Identifier: BSD-2-Clause .\" .\" Copyright (c) 2018-2023 Gavin D. Howard and contributors. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions are met: .\" .\" * Redistributions of source code must retain the above copyright notice, .\" this list of conditions and the following disclaimer. .\" .\" * Redistributions in binary form must reproduce the above copyright notice, .\" this list of conditions and the following disclaimer in the documentation .\" and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.TH "BC" "1" "February 2023" "Gavin D. Howard" "General Commands Manual" +.TH "BC" "1" "November 2023" "Gavin D. Howard" "General Commands Manual" .nh .ad l .SH NAME -.PP bc - arbitrary-precision decimal arithmetic language and calculator .SH SYNOPSIS -.PP \f[B]bc\f[R] [\f[B]-cCghilPqRsvVw\f[R]] [\f[B]--digit-clamp\f[R]] [\f[B]--no-digit-clamp\f[R]] [\f[B]--global-stacks\f[R]] [\f[B]--help\f[R]] [\f[B]--interactive\f[R]] [\f[B]--mathlib\f[R]] [\f[B]--no-prompt\f[R]] [\f[B]--no-read-prompt\f[R]] [\f[B]--quiet\f[R]] [\f[B]--standard\f[R]] [\f[B]--warn\f[R]] [\f[B]--version\f[R]] [\f[B]-e\f[R] \f[I]expr\f[R]] [\f[B]--expression\f[R]=\f[I]expr\f[R]\&...] [\f[B]-f\f[R] \f[I]file\f[R]\&...] [\f[B]--file\f[R]=\f[I]file\f[R]\&...] [\f[I]file\f[R]\&...] [\f[B]-I\f[R] \f[I]ibase\f[R]] [\f[B]--ibase\f[R]=\f[I]ibase\f[R]] [\f[B]-O\f[R] \f[I]obase\f[R]] [\f[B]--obase\f[R]=\f[I]obase\f[R]] [\f[B]-S\f[R] \f[I]scale\f[R]] [\f[B]--scale\f[R]=\f[I]scale\f[R]] [\f[B]-E\f[R] \f[I]seed\f[R]] [\f[B]--seed\f[R]=\f[I]seed\f[R]] .SH DESCRIPTION -.PP bc(1) is an interactive processor for a language first standardized in 1991 by POSIX. (See the \f[B]STANDARDS\f[R] section.) The language provides unlimited precision decimal arithmetic and is somewhat C-like, but there are differences. Such differences will be noted in this document. .PP After parsing and handling options, this bc(1) reads any files given on the command line and executes them before reading from \f[B]stdin\f[R]. .PP This bc(1) is a drop-in replacement for \f[I]any\f[R] bc(1), including (and especially) the GNU bc(1). It also has many extensions and extra features beyond other implementations. .PP \f[B]Note\f[R]: If running this bc(1) on \f[I]any\f[R] script meant for another bc(1) gives a parse error, it is probably because a word this bc(1) reserves as a keyword is used as the name of a function, variable, or array. To fix that, use the command-line option \f[B]-r\f[R] \f[I]keyword\f[R], where \f[I]keyword\f[R] is the keyword that is used as a name in the script. For more information, see the \f[B]OPTIONS\f[R] section. .PP If parsing scripts meant for other bc(1) implementations still does not work, that is a bug and should be reported. See the \f[B]BUGS\f[R] section. .SH OPTIONS -.PP The following are the options that bc(1) accepts. .TP \f[B]-C\f[R], \f[B]--no-digit-clamp\f[R] Disables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that the value added to a number from a digit is always that digit\[cq]s value multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-c\f[R] or \f[B]--digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-c\f[R], \f[B]--digit-clamp\f[R] Enables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that digits that the value added to a number from a digit that is greater than or equal to the ibase is the value of ibase minus 1 all multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-C\f[R] or \f[B]--no-digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-E\f[R] \f[I]seed\f[R], \f[B]--seed\f[R]=\f[I]seed\f[R] Sets the builtin variable \f[B]seed\f[R] to the value \f[I]seed\f[R] assuming that \f[I]seed\f[R] is in base 10. It is a fatal error if \f[I]seed\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-e\f[R] \f[I]expr\f[R], \f[B]--expression\f[R]=\f[I]expr\f[R] Evaluates \f[I]expr\f[R]. If multiple expressions are given, they are evaluated in order. If files are given as well (see the \f[B]-f\f[R] and \f[B]--file\f[R] options), the expressions and files are evaluated in the order given. This means that if a file is given before an expression, the file is read in and evaluated first. .RS .PP If this option is given on the command-line (i.e., not in \f[B]BC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, bc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R], whether on the command-line or in \f[B]BC_ENV_ARGS\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, bc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-f\f[R] \f[I]file\f[R], \f[B]--file\f[R]=\f[I]file\f[R] Reads in \f[I]file\f[R] and evaluates it, line by line, as though it were read through \f[B]stdin\f[R]. If expressions are also given (see the \f[B]-e\f[R] and \f[B]--expression\f[R] options), the expressions are evaluated in the order given. .RS .PP If this option is given on the command-line (i.e., not in \f[B]BC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, bc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, bc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-g\f[R], \f[B]--global-stacks\f[R] Turns the globals \f[B]ibase\f[R], \f[B]obase\f[R], \f[B]scale\f[R], and \f[B]seed\f[R] into stacks. .RS .PP This has the effect that a copy of the current value of all four are pushed onto a stack for every function call, as well as popped when every function returns. This means that functions can assign to any and all of those globals without worrying that the change will affect other functions. Thus, a hypothetical function named \f[B]output(x,b)\f[R] that simply printed \f[B]x\f[R] in base \f[B]b\f[R] could be written like this: .IP -.nf -\f[C] +.EX define void output(x, b) { obase=b x } -\f[R] -.fi +.EE .PP instead of like this: .IP -.nf -\f[C] +.EX define void output(x, b) { auto c c=obase obase=b x obase=c } -\f[R] -.fi +.EE .PP This makes writing functions much easier. .PP (\f[B]Note\f[R]: the function \f[B]output(x,b)\f[R] exists in the extended math library. See the \f[B]LIBRARY\f[R] section.) .PP However, since using this flag means that functions cannot set \f[B]ibase\f[R], \f[B]obase\f[R], \f[B]scale\f[R], or \f[B]seed\f[R] globally, functions that are made to do so cannot work anymore. There are two possible use cases for that, and each has a solution. .PP First, if a function is called on startup to turn bc(1) into a number converter, it is possible to replace that capability with various shell aliases. Examples: .IP -.nf -\f[C] +.EX alias d2o=\[dq]bc -e ibase=A -e obase=8\[dq] alias h2b=\[dq]bc -e ibase=G -e obase=2\[dq] -\f[R] -.fi +.EE .PP Second, if the purpose of a function is to set \f[B]ibase\f[R], \f[B]obase\f[R], \f[B]scale\f[R], or \f[B]seed\f[R] globally for any other purpose, it could be split into one to four functions (based on how many globals it sets) and each of those functions could return the desired value for a global. .PP For functions that set \f[B]seed\f[R], the value assigned to \f[B]seed\f[R] is not propagated to parent functions. This means that the sequence of pseudo-random numbers that they see will not be the same sequence of pseudo-random numbers that any parent sees. This is only the case once \f[B]seed\f[R] has been set. .PP If a function desires to not affect the sequence of pseudo-random numbers of its parents, but wants to use the same \f[B]seed\f[R], it can use the following line: .IP -.nf -\f[C] +.EX seed = seed -\f[R] -.fi +.EE .PP If the behavior of this option is desired for every run of bc(1), then users could make sure to define \f[B]BC_ENV_ARGS\f[R] and include this option (see the \f[B]ENVIRONMENT VARIABLES\f[R] section for more details). .PP If \f[B]-s\f[R], \f[B]-w\f[R], or any equivalents are used, this option is ignored. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-h\f[R], \f[B]--help\f[R] Prints a usage message and exits. .TP \f[B]-I\f[R] \f[I]ibase\f[R], \f[B]--ibase\f[R]=\f[I]ibase\f[R] Sets the builtin variable \f[B]ibase\f[R] to the value \f[I]ibase\f[R] assuming that \f[I]ibase\f[R] is in base 10. It is a fatal error if \f[I]ibase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-i\f[R], \f[B]--interactive\f[R] Forces interactive mode. (See the \f[B]INTERACTIVE MODE\f[R] section.) .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-L\f[R], \f[B]--no-line-length\f[R] Disables line length checking and prints numbers without backslashes and newlines. In other words, this option sets \f[B]BC_LINE_LENGTH\f[R] to \f[B]0\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-l\f[R], \f[B]--mathlib\f[R] Sets \f[B]scale\f[R] (see the \f[B]SYNTAX\f[R] section) to \f[B]20\f[R] and loads the included math library and the extended math library before running any code, including any expressions or files specified on the command line. .RS .PP To learn what is in the libraries, see the \f[B]LIBRARY\f[R] section. .RE .TP \f[B]-O\f[R] \f[I]obase\f[R], \f[B]--obase\f[R]=\f[I]obase\f[R] Sets the builtin variable \f[B]obase\f[R] to the value \f[I]obase\f[R] assuming that \f[I]obase\f[R] is in base 10. It is a fatal error if \f[I]obase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-P\f[R], \f[B]--no-prompt\f[R] Disables the prompt in TTY mode. (The prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a prompt or are not used to having them in bc(1). Most of those users would want to put this option in \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .RS .PP These options override the \f[B]BC_PROMPT\f[R] and \f[B]BC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-q\f[R], \f[B]--quiet\f[R] This option is for compatibility with the GNU bc(1) (https://www.gnu.org/software/bc/); it is a no-op. Without this option, GNU bc(1) prints a copyright header. This bc(1) only prints the copyright header if one or more of the \f[B]-v\f[R], \f[B]-V\f[R], or \f[B]--version\f[R] options are given unless the \f[B]BC_BANNER\f[R] environment variable is set and contains a non-zero integer or if this bc(1) was built with the header displayed by default. If \f[I]any\f[R] of that is the case, then this option \f[I]does\f[R] prevent bc(1) from printing the header. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-R\f[R], \f[B]--no-read-prompt\f[R] Disables the read prompt in TTY mode. (The read prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a read prompt or are not used to having them in bc(1). Most of those users would want to put this option in \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). This option is also useful in hash bang lines of bc(1) scripts that prompt for user input. .RS .PP This option does not disable the regular prompt because the read prompt is only used when the \f[B]read()\f[R] built-in function is called. .PP These options \f[I]do\f[R] override the \f[B]BC_PROMPT\f[R] and \f[B]BC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), but only for the read prompt. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-r\f[R] \f[I]keyword\f[R], \f[B]--redefine\f[R]=\f[I]keyword\f[R] Redefines \f[I]keyword\f[R] in order to allow it to be used as a function, variable, or array name. This is useful when this bc(1) gives parse errors when parsing scripts meant for other bc(1) implementations. .RS .PP The keywords this bc(1) allows to be redefined are: .IP \[bu] 2 \f[B]abs\f[R] .IP \[bu] 2 \f[B]asciify\f[R] .IP \[bu] 2 \f[B]continue\f[R] .IP \[bu] 2 \f[B]divmod\f[R] .IP \[bu] 2 \f[B]else\f[R] .IP \[bu] 2 \f[B]halt\f[R] .IP \[bu] 2 \f[B]irand\f[R] .IP \[bu] 2 \f[B]last\f[R] .IP \[bu] 2 \f[B]limits\f[R] .IP \[bu] 2 \f[B]maxibase\f[R] .IP \[bu] 2 \f[B]maxobase\f[R] .IP \[bu] 2 \f[B]maxrand\f[R] .IP \[bu] 2 \f[B]maxscale\f[R] .IP \[bu] 2 \f[B]modexp\f[R] .IP \[bu] 2 \f[B]print\f[R] .IP \[bu] 2 \f[B]rand\f[R] .IP \[bu] 2 \f[B]read\f[R] .IP \[bu] 2 \f[B]seed\f[R] .IP \[bu] 2 \f[B]stream\f[R] .PP If any of those keywords are used as a function, variable, or array name in a script, use this option with the keyword as the argument. If multiple are used, use this option for all of them; it can be used multiple times. .PP Keywords are \f[I]not\f[R] redefined when parsing the builtin math library (see the \f[B]LIBRARY\f[R] section). .PP It is a fatal error to redefine keywords mandated by the POSIX standard (see the \f[B]STANDARDS\f[R] section). It is a fatal error to attempt to redefine words that this bc(1) does not reserve as keywords. .RE .TP \f[B]-S\f[R] \f[I]scale\f[R], \f[B]--scale\f[R]=\f[I]scale\f[R] Sets the builtin variable \f[B]scale\f[R] to the value \f[I]scale\f[R] assuming that \f[I]scale\f[R] is in base 10. It is a fatal error if \f[I]scale\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-s\f[R], \f[B]--standard\f[R] Process exactly the language defined by the standard (see the \f[B]STANDARDS\f[R] section) and error if any extensions are used. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-v\f[R], \f[B]-V\f[R], \f[B]--version\f[R] Print the version information (copyright header) and exits. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-w\f[R], \f[B]--warn\f[R] Like \f[B]-s\f[R] and \f[B]--standard\f[R], except that warnings (and not errors) are printed for non-standard extensions and execution continues normally. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-z\f[R], \f[B]--leading-zeroes\f[R] Makes bc(1) print all numbers greater than \f[B]-1\f[R] and less than \f[B]1\f[R], and not equal to \f[B]0\f[R], with a leading zero. .RS .PP This can be set for individual numbers with the \f[B]plz(x)\f[R], \f[B]plznl(x)\f[R], \f[B]pnlz(x)\f[R], and \f[B]pnlznl(x)\f[R] functions in the extended math library (see the \f[B]LIBRARY\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .PP All long options are \f[B]non-portable extensions\f[R]. .SH STDIN -.PP If no files or expressions are given by the \f[B]-f\f[R], \f[B]--file\f[R], \f[B]-e\f[R], or \f[B]--expression\f[R] options, then bc(1) reads from \f[B]stdin\f[R]. .PP However, there are a few caveats to this. .PP First, \f[B]stdin\f[R] is evaluated a line at a time. The only exception to this is if the parse cannot complete. That means that starting a string without ending it or starting a function, \f[B]if\f[R] statement, or loop without ending it will also cause bc(1) to not execute. .PP Second, after an \f[B]if\f[R] statement, bc(1) doesn\[cq]t know if an \f[B]else\f[R] statement will follow, so it will not execute until it knows there will not be an \f[B]else\f[R] statement. .SH STDOUT -.PP Any non-error output is written to \f[B]stdout\f[R]. In addition, if history (see the \f[B]HISTORY\f[R] section) and the prompt (see the \f[B]TTY MODE\f[R] section) are enabled, both are output to \f[B]stdout\f[R]. .PP \f[B]Note\f[R]: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stdout\f[R], so if \f[B]stdout\f[R] is closed, as in -\f[B]bc >&-\f[R], it will quit with an error. +\f[B]bc >&-\f[R], it will quit with an error. This is done so that bc(1) can report problems when \f[B]stdout\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stdout\f[R] to \f[B]/dev/null\f[R]. .SH STDERR -.PP Any error output is written to \f[B]stderr\f[R]. .PP \f[B]Note\f[R]: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stderr\f[R], so if \f[B]stderr\f[R] is closed, as in -\f[B]bc 2>&-\f[R], it will quit with an error. +\f[B]bc 2>&-\f[R], it will quit with an error. This is done so that bc(1) can exit with an error code when \f[B]stderr\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stderr\f[R] to \f[B]/dev/null\f[R]. .SH SYNTAX -.PP The syntax for bc(1) programs is mostly C-like, with some differences. This bc(1) follows the POSIX standard (see the \f[B]STANDARDS\f[R] section), which is a much more thorough resource for the language this bc(1) accepts. This section is meant to be a summary and a listing of all the extensions to the standard. .PP In the sections below, \f[B]E\f[R] means expression, \f[B]S\f[R] means statement, and \f[B]I\f[R] means identifier. .PP Identifiers (\f[B]I\f[R]) start with a lowercase letter and can be followed by any number (up to \f[B]BC_NAME_MAX-1\f[R]) of lowercase letters (\f[B]a-z\f[R]), digits (\f[B]0-9\f[R]), and underscores (\f[B]_\f[R]). The regex is \f[B][a-z][a-z0-9_]*\f[R]. Identifiers with more than one character (letter) are a \f[B]non-portable extension\f[R]. .PP \f[B]ibase\f[R] is a global variable determining how to interpret constant numbers. It is the \[lq]input\[rq] base, or the number base used for interpreting input numbers. \f[B]ibase\f[R] is initially \f[B]10\f[R]. If the \f[B]-s\f[R] (\f[B]--standard\f[R]) and \f[B]-w\f[R] (\f[B]--warn\f[R]) flags were not given on the command line, the max allowable value for \f[B]ibase\f[R] is \f[B]36\f[R]. Otherwise, it is \f[B]16\f[R]. The min allowable value for \f[B]ibase\f[R] is \f[B]2\f[R]. The max allowable value for \f[B]ibase\f[R] can be queried in bc(1) programs with the \f[B]maxibase()\f[R] built-in function. .PP \f[B]obase\f[R] is a global variable determining how to output results. It is the \[lq]output\[rq] base, or the number base used for outputting numbers. \f[B]obase\f[R] is initially \f[B]10\f[R]. The max allowable value for \f[B]obase\f[R] is \f[B]BC_BASE_MAX\f[R] and can be queried in bc(1) programs with the \f[B]maxobase()\f[R] built-in function. The min allowable value for \f[B]obase\f[R] is \f[B]0\f[R]. If \f[B]obase\f[R] is \f[B]0\f[R], values are output in scientific notation, and if \f[B]obase\f[R] is \f[B]1\f[R], values are output in engineering notation. Otherwise, values are output in the specified base. .PP Outputting in scientific and engineering notations are \f[B]non-portable extensions\f[R]. .PP The \f[I]scale\f[R] of an expression is the number of digits in the result of the expression right of the decimal point, and \f[B]scale\f[R] is a global variable that sets the precision of any operations, with exceptions. \f[B]scale\f[R] is initially \f[B]0\f[R]. \f[B]scale\f[R] cannot be negative. The max allowable value for \f[B]scale\f[R] is \f[B]BC_SCALE_MAX\f[R] and can be queried in bc(1) programs with the \f[B]maxscale()\f[R] built-in function. .PP bc(1) has both \f[I]global\f[R] variables and \f[I]local\f[R] variables. All \f[I]local\f[R] variables are local to the function; they are parameters or are introduced in the \f[B]auto\f[R] list of a function (see the \f[B]FUNCTIONS\f[R] section). If a variable is accessed which is not a parameter or in the \f[B]auto\f[R] list, it is assumed to be \f[I]global\f[R]. If a parent function has a \f[I]local\f[R] variable version of a variable that a child function considers \f[I]global\f[R], the value of that \f[I]global\f[R] variable in the child function is the value of the variable in the parent function, not the value of the actual \f[I]global\f[R] variable. .PP All of the above applies to arrays as well. .PP The value of a statement that is an expression (i.e., any of the named expressions or operands) is printed unless the lowest precedence operator is an assignment operator \f[I]and\f[R] the expression is notsurrounded by parentheses. .PP The value that is printed is also assigned to the special variable \f[B]last\f[R]. A single dot (\f[B].\f[R]) may also be used as a synonym for \f[B]last\f[R]. These are \f[B]non-portable extensions\f[R]. .PP Either semicolons or newlines may separate statements. .SS Comments -.PP There are two kinds of comments: .IP "1." 3 Block comments are enclosed in \f[B]/*\f[R] and \f[B]*/\f[R]. .IP "2." 3 Line comments go from \f[B]#\f[R] until, and not including, the next newline. This is a \f[B]non-portable extension\f[R]. .SS Named Expressions -.PP The following are named expressions in bc(1): .IP "1." 3 Variables: \f[B]I\f[R] .IP "2." 3 Array Elements: \f[B]I[E]\f[R] .IP "3." 3 \f[B]ibase\f[R] .IP "4." 3 \f[B]obase\f[R] .IP "5." 3 \f[B]scale\f[R] .IP "6." 3 \f[B]seed\f[R] .IP "7." 3 \f[B]last\f[R] or a single dot (\f[B].\f[R]) .PP Numbers 6 and 7 are \f[B]non-portable extensions\f[R]. .PP The meaning of \f[B]seed\f[R] is dependent on the current pseudo-random number generator but is guaranteed to not change except for new major versions. .PP The \f[I]scale\f[R] and sign of the value may be significant. .PP If a previously used \f[B]seed\f[R] value is assigned to \f[B]seed\f[R] and used again, the pseudo-random number generator is guaranteed to produce the same sequence of pseudo-random numbers as it did when the \f[B]seed\f[R] value was previously used. .PP The exact value assigned to \f[B]seed\f[R] is not guaranteed to be returned if \f[B]seed\f[R] is queried again immediately. However, if \f[B]seed\f[R] \f[I]does\f[R] return a different value, both values, when assigned to \f[B]seed\f[R], are guaranteed to produce the same sequence of pseudo-random numbers. This means that certain values assigned to \f[B]seed\f[R] will \f[I]not\f[R] produce unique sequences of pseudo-random numbers. The value of \f[B]seed\f[R] will change after any use of the \f[B]rand()\f[R] and \f[B]irand(E)\f[R] operands (see the \f[I]Operands\f[R] subsection below), except if the parameter passed to \f[B]irand(E)\f[R] is \f[B]0\f[R], \f[B]1\f[R], or negative. .PP There is no limit to the length (number of significant decimal digits) or \f[I]scale\f[R] of the value that can be assigned to \f[B]seed\f[R]. .PP Variables and arrays do not interfere; users can have arrays named the same as variables. This also applies to functions (see the \f[B]FUNCTIONS\f[R] section), so a user can have a variable, array, and function that all have the same name, and they will not shadow each other, whether inside of functions or not. .PP Named expressions are required as the operand of \f[B]increment\f[R]/\f[B]decrement\f[R] operators and as the left side of \f[B]assignment\f[R] operators (see the \f[I]Operators\f[R] subsection). .SS Operands -.PP The following are valid operands in bc(1): .IP " 1." 4 Numbers (see the \f[I]Numbers\f[R] subsection below). .IP " 2." 4 Array indices (\f[B]I[E]\f[R]). .IP " 3." 4 \f[B](E)\f[R]: The value of \f[B]E\f[R] (used to change precedence). .IP " 4." 4 \f[B]sqrt(E)\f[R]: The square root of \f[B]E\f[R]. \f[B]E\f[R] must be non-negative. .IP " 5." 4 \f[B]length(E)\f[R]: The number of significant decimal digits in \f[B]E\f[R]. Returns \f[B]1\f[R] for \f[B]0\f[R] with no decimal places. If given a string, the length of the string is returned. Passing a string to \f[B]length(E)\f[R] is a \f[B]non-portable extension\f[R]. .IP " 6." 4 \f[B]length(I[])\f[R]: The number of elements in the array \f[B]I\f[R]. This is a \f[B]non-portable extension\f[R]. .IP " 7." 4 \f[B]scale(E)\f[R]: The \f[I]scale\f[R] of \f[B]E\f[R]. .IP " 8." 4 \f[B]abs(E)\f[R]: The absolute value of \f[B]E\f[R]. This is a \f[B]non-portable extension\f[R]. .IP " 9." 4 \f[B]is_number(E)\f[R]: \f[B]1\f[R] if the given argument is a number, \f[B]0\f[R] if it is a string. This is a \f[B]non-portable extension\f[R]. .IP "10." 4 \f[B]is_string(E)\f[R]: \f[B]1\f[R] if the given argument is a string, \f[B]0\f[R] if it is a number. This is a \f[B]non-portable extension\f[R]. .IP "11." 4 \f[B]modexp(E, E, E)\f[R]: Modular exponentiation, where the first expression is the base, the second is the exponent, and the third is the modulus. All three values must be integers. The second argument must be non-negative. The third argument must be non-zero. This is a \f[B]non-portable extension\f[R]. .IP "12." 4 \f[B]divmod(E, E, I[])\f[R]: Division and modulus in one operation. This is for optimization. The first expression is the dividend, and the second is the divisor, which must be non-zero. The return value is the quotient, and the modulus is stored in index \f[B]0\f[R] of the provided array (the last argument). This is a \f[B]non-portable extension\f[R]. .IP "13." 4 \f[B]asciify(E)\f[R]: If \f[B]E\f[R] is a string, returns a string that is the first letter of its argument. If it is a number, calculates the number mod \f[B]256\f[R] and returns that number as a one-character string. This is a \f[B]non-portable extension\f[R]. .IP "14." 4 \f[B]asciify(I[])\f[R]: A string that is made up of the characters that would result from running \f[B]asciify(E)\f[R] on each element of the array identified by the argument. This allows creating multi-character strings and storing them. This is a \f[B]non-portable extension\f[R]. .IP "15." 4 \f[B]I()\f[R], \f[B]I(E)\f[R], \f[B]I(E, E)\f[R], and so on, where \f[B]I\f[R] is an identifier for a non-\f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). The \f[B]E\f[R] argument(s) may also be arrays of the form \f[B]I[]\f[R], which will automatically be turned into array references (see the \f[I]Array References\f[R] subsection of the \f[B]FUNCTIONS\f[R] section) if the corresponding parameter in the function definition is an array reference. .IP "16." 4 \f[B]read()\f[R]: Reads a line from \f[B]stdin\f[R] and uses that as an expression. The result of that expression is the result of the \f[B]read()\f[R] operand. This is a \f[B]non-portable extension\f[R]. .IP "17." 4 \f[B]maxibase()\f[R]: The max allowable \f[B]ibase\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "18." 4 \f[B]maxobase()\f[R]: The max allowable \f[B]obase\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "19." 4 \f[B]maxscale()\f[R]: The max allowable \f[B]scale\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "20." 4 \f[B]line_length()\f[R]: The line length set with \f[B]BC_LINE_LENGTH\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). This is a \f[B]non-portable extension\f[R]. .IP "21." 4 \f[B]global_stacks()\f[R]: \f[B]0\f[R] if global stacks are not enabled with the \f[B]-g\f[R] or \f[B]--global-stacks\f[R] options, non-zero otherwise. See the \f[B]OPTIONS\f[R] section. This is a \f[B]non-portable extension\f[R]. .IP "22." 4 \f[B]leading_zero()\f[R]: \f[B]0\f[R] if leading zeroes are not enabled with the \f[B]-z\f[R] or \f[B]\[en]leading-zeroes\f[R] options, non-zero otherwise. See the \f[B]OPTIONS\f[R] section. This is a \f[B]non-portable extension\f[R]. .IP "23." 4 \f[B]rand()\f[R]: A pseudo-random integer between \f[B]0\f[R] (inclusive) and \f[B]BC_RAND_MAX\f[R] (inclusive). Using this operand will change the value of \f[B]seed\f[R]. This is a \f[B]non-portable extension\f[R]. .IP "24." 4 \f[B]irand(E)\f[R]: A pseudo-random integer between \f[B]0\f[R] (inclusive) and the value of \f[B]E\f[R] (exclusive). If \f[B]E\f[R] is negative or is a non-integer (\f[B]E\f[R]\[cq]s \f[I]scale\f[R] is not \f[B]0\f[R]), an error is raised, and bc(1) resets (see the \f[B]RESET\f[R] section) while \f[B]seed\f[R] remains unchanged. If \f[B]E\f[R] is larger than \f[B]BC_RAND_MAX\f[R], the higher bound is honored by generating several pseudo-random integers, multiplying them by appropriate powers of \f[B]BC_RAND_MAX+1\f[R], and adding them together. Thus, the size of integer that can be generated with this operand is unbounded. Using this operand will change the value of \f[B]seed\f[R], unless the value of \f[B]E\f[R] is \f[B]0\f[R] or \f[B]1\f[R]. In that case, \f[B]0\f[R] is returned, and \f[B]seed\f[R] is \f[I]not\f[R] changed. This is a \f[B]non-portable extension\f[R]. .IP "25." 4 \f[B]maxrand()\f[R]: The max integer returned by \f[B]rand()\f[R]. This is a \f[B]non-portable extension\f[R]. .PP The integers generated by \f[B]rand()\f[R] and \f[B]irand(E)\f[R] are guaranteed to be as unbiased as possible, subject to the limitations of the pseudo-random number generator. .PP \f[B]Note\f[R]: The values returned by the pseudo-random number generator with \f[B]rand()\f[R] and \f[B]irand(E)\f[R] are guaranteed to \f[I]NOT\f[R] be cryptographically secure. This is a consequence of using a seeded pseudo-random number generator. However, they \f[I]are\f[R] guaranteed to be reproducible with identical \f[B]seed\f[R] values. This means that the pseudo-random values from bc(1) should only be used where a reproducible stream of pseudo-random numbers is \f[I]ESSENTIAL\f[R]. In any other case, use a non-seeded pseudo-random number generator. .SS Numbers -.PP Numbers are strings made up of digits, uppercase letters, and at most \f[B]1\f[R] period for a radix. Numbers can have up to \f[B]BC_NUM_MAX\f[R] digits. Uppercase letters are equal to \f[B]9\f[R] plus their position in the alphabet, starting from \f[B]1\f[R] (i.e., \f[B]A\f[R] equals \f[B]10\f[R], or \f[B]9+1\f[R]). .PP If a digit or letter makes no sense with the current value of \f[B]ibase\f[R] (i.e., they are greater than or equal to the current value of \f[B]ibase\f[R]), then the behavior depends on the existence of the \f[B]-c\f[R]/\f[B]--digit-clamp\f[R] or \f[B]-C\f[R]/\f[B]--no-digit-clamp\f[R] options (see the \f[B]OPTIONS\f[R] section), the existence and setting of the \f[B]BC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or the default, which can be queried with the \f[B]-h\f[R]/\f[B]--help\f[R] option. .PP If clamping is off, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are not changed. Instead, their given value is multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*A+3\[ha]0*B\f[R], which is \f[B]3\f[R] times \f[B]10\f[R] plus \f[B]11\f[R], or \f[B]41\f[R]. .PP If clamping is on, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are set to the value of the highest valid digit in \f[B]ibase\f[R] before being multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*2+3\[ha]0*2\f[R], which is \f[B]3\f[R] times \f[B]2\f[R] plus \f[B]2\f[R], or \f[B]8\f[R]. .PP There is one exception to clamping: single-character numbers (i.e., \f[B]A\f[R] alone). Such numbers are never clamped and always take the value they would have in the highest possible \f[B]ibase\f[R]. This means that \f[B]A\f[R] alone always equals decimal \f[B]10\f[R] and \f[B]Z\f[R] alone always equals decimal \f[B]35\f[R]. This behavior is mandated by the standard (see the STANDARDS section) and is meant to provide an easy way to set the current \f[B]ibase\f[R] (with the \f[B]i\f[R] command) regardless of the current value of \f[B]ibase\f[R]. .PP If clamping is on, and the clamped value of a character is needed, use a leading zero, i.e., for \f[B]A\f[R], use \f[B]0A\f[R]. .PP In addition, bc(1) accepts numbers in scientific notation. These have the form \f[B]e\f[R]. The exponent (the portion after the \f[B]e\f[R]) must be an integer. An example is \f[B]1.89237e9\f[R], which is equal to \f[B]1892370000\f[R]. Negative exponents are also allowed, so \f[B]4.2890e-3\f[R] is equal to \f[B]0.0042890\f[R]. .PP Using scientific notation is an error or warning if the \f[B]-s\f[R] or \f[B]-w\f[R], respectively, command-line options (or equivalents) are given. .PP \f[B]WARNING\f[R]: Both the number and the exponent in scientific notation are interpreted according to the current \f[B]ibase\f[R], but the number is still multiplied by \f[B]10\[ha]exponent\f[R] regardless of the current \f[B]ibase\f[R]. For example, if \f[B]ibase\f[R] is \f[B]16\f[R] and bc(1) is given the number string \f[B]FFeA\f[R], the resulting decimal number will be \f[B]2550000000000\f[R], and if bc(1) is given the number string \f[B]10e-4\f[R], the resulting decimal number will be \f[B]0.0016\f[R]. .PP Accepting input as scientific notation is a \f[B]non-portable extension\f[R]. .SS Operators -.PP The following arithmetic and logical operators can be used. They are listed in order of decreasing precedence. Operators in the same group have the same precedence. .TP \f[B]++\f[R] \f[B]--\f[R] Type: Prefix and Postfix .RS .PP Associativity: None .PP Description: \f[B]increment\f[R], \f[B]decrement\f[R] .RE .TP \f[B]-\f[R] \f[B]!\f[R] Type: Prefix .RS .PP Associativity: None .PP Description: \f[B]negation\f[R], \f[B]boolean not\f[R] .RE .TP \f[B]$\f[R] Type: Postfix .RS .PP Associativity: None .PP Description: \f[B]truncation\f[R] .RE .TP \f[B]\[at]\f[R] Type: Binary .RS .PP Associativity: Right .PP Description: \f[B]set precision\f[R] .RE .TP \f[B]\[ha]\f[R] Type: Binary .RS .PP Associativity: Right .PP Description: \f[B]power\f[R] .RE .TP \f[B]*\f[R] \f[B]/\f[R] \f[B]%\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]multiply\f[R], \f[B]divide\f[R], \f[B]modulus\f[R] .RE .TP \f[B]+\f[R] \f[B]-\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]add\f[R], \f[B]subtract\f[R] .RE .TP \f[B]<<\f[R] \f[B]>>\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]shift left\f[R], \f[B]shift right\f[R] .RE .TP \f[B]=\f[R] \f[B]<<=\f[R] \f[B]>>=\f[R] \f[B]+=\f[R] \f[B]-=\f[R] \f[B]*=\f[R] \f[B]/=\f[R] \f[B]%=\f[R] \f[B]\[ha]=\f[R] \f[B]\[at]=\f[R] Type: Binary .RS .PP Associativity: Right .PP Description: \f[B]assignment\f[R] .RE .TP \f[B]==\f[R] \f[B]<=\f[R] \f[B]>=\f[R] \f[B]!=\f[R] \f[B]<\f[R] \f[B]>\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]relational\f[R] .RE .TP \f[B]&&\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]boolean and\f[R] .RE .TP \f[B]||\f[R] Type: Binary .RS .PP Associativity: Left .PP Description: \f[B]boolean or\f[R] .RE .PP The operators will be described in more detail below. .TP \f[B]++\f[R] \f[B]--\f[R] The prefix and postfix \f[B]increment\f[R] and \f[B]decrement\f[R] -operators behave exactly like they would in C. -They require a named expression (see the \f[I]Named Expressions\f[R] -subsection) as an operand. +operators behave exactly like they would in C. They require a named +expression (see the \f[I]Named Expressions\f[R] subsection) as an +operand. .RS .PP The prefix versions of these operators are more efficient; use them where possible. .RE .TP \f[B]-\f[R] The \f[B]negation\f[R] operator returns \f[B]0\f[R] if a user attempts to negate any expression with the value \f[B]0\f[R]. Otherwise, a copy of the expression with its sign flipped is returned. .TP \f[B]!\f[R] The \f[B]boolean not\f[R] operator returns \f[B]1\f[R] if the expression is \f[B]0\f[R], or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]$\f[R] The \f[B]truncation\f[R] operator returns a copy of the given expression with all of its \f[I]scale\f[R] removed. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[at]\f[R] The \f[B]set precision\f[R] operator takes two expressions and returns a copy of the first with its \f[I]scale\f[R] equal to the value of the second expression. That could either mean that the number is returned without change (if the \f[I]scale\f[R] of the first expression matches the value of the second expression), extended (if it is less), or truncated (if it is more). .RS .PP The second expression must be an integer (no \f[I]scale\f[R]) and non-negative. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[ha]\f[R] The \f[B]power\f[R] operator (not the \f[B]exclusive or\f[R] operator, as it would be in C) takes two expressions and raises the first to the power of the value of the second. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The second expression must be an integer (no \f[I]scale\f[R]), and if it is negative, the first value must be non-zero. .RE .TP \f[B]*\f[R] The \f[B]multiply\f[R] operator takes two expressions, multiplies them, and returns the product. If \f[B]a\f[R] is the \f[I]scale\f[R] of the first expression and \f[B]b\f[R] is the \f[I]scale\f[R] of the second expression, the \f[I]scale\f[R] of the result is equal to \f[B]min(a+b,max(scale,a,b))\f[R] where \f[B]min()\f[R] and \f[B]max()\f[R] return the obvious values. .TP \f[B]/\f[R] The \f[B]divide\f[R] operator takes two expressions, divides them, and returns the quotient. The \f[I]scale\f[R] of the result shall be the value of \f[B]scale\f[R]. .RS .PP The second expression must be non-zero. .RE .TP \f[B]%\f[R] The \f[B]modulus\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and evaluates them by 1) Computing \f[B]a/b\f[R] to current \f[B]scale\f[R] and 2) Using the result of step 1 to calculate \f[B]a-(a/b)*b\f[R] to \f[I]scale\f[R] \f[B]max(scale+scale(b),scale(a))\f[R]. .RS .PP The second expression must be non-zero. .RE .TP \f[B]+\f[R] The \f[B]add\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and returns the sum, with a \f[I]scale\f[R] equal to the max of the \f[I]scale\f[R]s of \f[B]a\f[R] and \f[B]b\f[R]. .TP \f[B]-\f[R] The \f[B]subtract\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and returns the difference, with a \f[I]scale\f[R] equal to the max of the \f[I]scale\f[R]s of \f[B]a\f[R] and \f[B]b\f[R]. .TP \f[B]<<\f[R] The \f[B]left shift\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and returns a copy of the value of \f[B]a\f[R] with its decimal point moved \f[B]b\f[R] places to the right. .RS .PP The second expression must be an integer (no \f[I]scale\f[R]) and non-negative. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]>>\f[R] The \f[B]right shift\f[R] operator takes two expressions, \f[B]a\f[R] and \f[B]b\f[R], and returns a copy of the value of \f[B]a\f[R] with its decimal point moved \f[B]b\f[R] places to the left. .RS .PP The second expression must be an integer (no \f[I]scale\f[R]) and non-negative. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]=\f[R] \f[B]<<=\f[R] \f[B]>>=\f[R] \f[B]+=\f[R] \f[B]-=\f[R] \f[B]*=\f[R] \f[B]/=\f[R] \f[B]%=\f[R] \f[B]\[ha]=\f[R] \f[B]\[at]=\f[R] The \f[B]assignment\f[R] operators take two expressions, \f[B]a\f[R] and \f[B]b\f[R] where \f[B]a\f[R] is a named expression (see the \f[I]Named Expressions\f[R] subsection). .RS .PP For \f[B]=\f[R], \f[B]b\f[R] is copied and the result is assigned to \f[B]a\f[R]. For all others, \f[B]a\f[R] and \f[B]b\f[R] are applied as operands to the corresponding arithmetic operator and the result is assigned to \f[B]a\f[R]. .PP The \f[B]assignment\f[R] operators that correspond to operators that are extensions are themselves \f[B]non-portable extensions\f[R]. .RE .TP \f[B]==\f[R] \f[B]<=\f[R] \f[B]>=\f[R] \f[B]!=\f[R] \f[B]<\f[R] \f[B]>\f[R] The \f[B]relational\f[R] operators compare two expressions, \f[B]a\f[R] and \f[B]b\f[R], and if the relation holds, according to C language semantics, the result is \f[B]1\f[R]. Otherwise, it is \f[B]0\f[R]. .RS .PP Note that unlike in C, these operators have a lower precedence than the \f[B]assignment\f[R] operators, which means that \f[B]a=b>c\f[R] is interpreted as \f[B](a=b)>c\f[R]. .PP Also, unlike the standard (see the \f[B]STANDARDS\f[R] section) requires, these operators can appear anywhere any other expressions can be used. This allowance is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]&&\f[R] The \f[B]boolean and\f[R] operator takes two expressions and returns \f[B]1\f[R] if both expressions are non-zero, \f[B]0\f[R] otherwise. .RS .PP This is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]||\f[R] The \f[B]boolean or\f[R] operator takes two expressions and returns \f[B]1\f[R] if one of the expressions is non-zero, \f[B]0\f[R] otherwise. .RS .PP This is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Statements -.PP The following items are statements: .IP " 1." 4 \f[B]E\f[R] .IP " 2." 4 \f[B]{\f[R] \f[B]S\f[R] \f[B];\f[R] \&... \f[B];\f[R] \f[B]S\f[R] \f[B]}\f[R] .IP " 3." 4 \f[B]if\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] .IP " 4." 4 \f[B]if\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] \f[B]else\f[R] \f[B]S\f[R] .IP " 5." 4 \f[B]while\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] .IP " 6." 4 \f[B]for\f[R] \f[B](\f[R] \f[B]E\f[R] \f[B];\f[R] \f[B]E\f[R] \f[B];\f[R] \f[B]E\f[R] \f[B])\f[R] \f[B]S\f[R] .IP " 7." 4 An empty statement .IP " 8." 4 \f[B]break\f[R] .IP " 9." 4 \f[B]continue\f[R] .IP "10." 4 \f[B]quit\f[R] .IP "11." 4 \f[B]halt\f[R] .IP "12." 4 \f[B]limits\f[R] .IP "13." 4 A string of characters, enclosed in double quotes .IP "14." 4 \f[B]print\f[R] \f[B]E\f[R] \f[B],\f[R] \&... \f[B],\f[R] \f[B]E\f[R] .IP "15." 4 \f[B]stream\f[R] \f[B]E\f[R] \f[B],\f[R] \&... \f[B],\f[R] \f[B]E\f[R] .IP "16." 4 \f[B]I()\f[R], \f[B]I(E)\f[R], \f[B]I(E, E)\f[R], and so on, where \f[B]I\f[R] is an identifier for a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). The \f[B]E\f[R] argument(s) may also be arrays of the form \f[B]I[]\f[R], which will automatically be turned into array references (see the \f[I]Array References\f[R] subsection of the \f[B]FUNCTIONS\f[R] section) if the corresponding parameter in the function definition is an array reference. .PP Numbers 4, 9, 11, 12, 14, 15, and 16 are \f[B]non-portable extensions\f[R]. .PP Also, as a \f[B]non-portable extension\f[R], any or all of the expressions in the header of a for loop may be omitted. If the condition (second expression) is omitted, it is assumed to be a constant \f[B]1\f[R]. .PP The \f[B]break\f[R] statement causes a loop to stop iterating and resume execution immediately following a loop. This is only allowed in loops. .PP The \f[B]continue\f[R] statement causes a loop iteration to stop early and returns to the start of the loop, including testing the loop condition. This is only allowed in loops. .PP The \f[B]if\f[R] \f[B]else\f[R] statement does the same thing as in C. .PP The \f[B]quit\f[R] statement causes bc(1) to quit, even if it is on a branch that will not be executed (it is a compile-time command). .PP \f[B]Warning\f[R]: The behavior of this bc(1) on \f[B]quit\f[R] is slightly different from other bc(1) implementations. Other bc(1) implementations will exit as soon as they finish parsing the line that a \f[B]quit\f[R] command is on. This bc(1) will execute any completed and executable statements that occur before the \f[B]quit\f[R] statement before exiting. .PP In other words, for the bc(1) code below: .IP -.nf -\f[C] +.EX for (i = 0; i < 3; ++i) i; quit -\f[R] -.fi +.EE .PP Other bc(1) implementations will print nothing, and this bc(1) will print \f[B]0\f[R], \f[B]1\f[R], and \f[B]2\f[R] on successive lines before exiting. .PP The \f[B]halt\f[R] statement causes bc(1) to quit, if it is executed. (Unlike \f[B]quit\f[R] if it is on a branch of an \f[B]if\f[R] statement that is not executed, bc(1) does not quit.) .PP The \f[B]limits\f[R] statement prints the limits that this bc(1) is subject to. This is like the \f[B]quit\f[R] statement in that it is a compile-time command. .PP An expression by itself is evaluated and printed, followed by a newline. .PP Both scientific notation and engineering notation are available for printing the results of expressions. Scientific notation is activated by assigning \f[B]0\f[R] to \f[B]obase\f[R], and engineering notation is activated by assigning \f[B]1\f[R] to \f[B]obase\f[R]. To deactivate them, just assign a different value to \f[B]obase\f[R]. .PP Scientific notation and engineering notation are disabled if bc(1) is run with either the \f[B]-s\f[R] or \f[B]-w\f[R] command-line options (or equivalents). .PP Printing numbers in scientific notation and/or engineering notation is a \f[B]non-portable extension\f[R]. .SS Strings -.PP If strings appear as a statement by themselves, they are printed without a trailing newline. .PP In addition to appearing as a lone statement by themselves, strings can be assigned to variables and array elements. They can also be passed to functions in variable parameters. .PP If any statement that expects a string is given a variable that had a string assigned to it, the statement acts as though it had received a string. .PP If any math operation is attempted on a string or a variable or array element that has been assigned a string, an error is raised, and bc(1) resets (see the \f[B]RESET\f[R] section). .PP Assigning strings to variables and array elements and passing them to functions are \f[B]non-portable extensions\f[R]. .SS Print Statement -.PP The \[lq]expressions\[rq] in a \f[B]print\f[R] statement may also be strings. If they are, there are backslash escape sequences that are interpreted specially. What those sequences are, and what they cause to be printed, are shown below: .PP \f[B]\[rs]a\f[R]: \f[B]\[rs]a\f[R] .PP \f[B]\[rs]b\f[R]: \f[B]\[rs]b\f[R] .PP \f[B]\[rs]\[rs]\f[R]: \f[B]\[rs]\f[R] .PP \f[B]\[rs]e\f[R]: \f[B]\[rs]\f[R] .PP \f[B]\[rs]f\f[R]: \f[B]\[rs]f\f[R] .PP \f[B]\[rs]n\f[R]: \f[B]\[rs]n\f[R] .PP \f[B]\[rs]q\f[R]: \f[B]\[lq]\f[R] .PP \f[B]\[rs]r\f[R]: \f[B]\[rs]r\f[R] .PP \f[B]\[rs]t\f[R]: \f[B]\[rs]t\f[R] .PP Any other character following a backslash causes the backslash and character to be printed as-is. .PP Any non-string expression in a print statement shall be assigned to \f[B]last\f[R], like any other expression that is printed. .SS Stream Statement -.PP The expressions in a \f[B]stream\f[R] statement may also be strings. .PP If a \f[B]stream\f[R] statement is given a string, it prints the string as though the string had appeared as its own statement. In other words, the \f[B]stream\f[R] statement prints strings normally, without a newline. .PP If a \f[B]stream\f[R] statement is given a number, a copy of it is truncated and its absolute value is calculated. The result is then printed as though \f[B]obase\f[R] is \f[B]256\f[R] and each digit is interpreted as an 8-bit ASCII character, making it a byte stream. .SS Order of Evaluation -.PP All expressions in a statment are evaluated left to right, except as necessary to maintain order of operations. This means, for example, assuming that \f[B]i\f[R] is equal to \f[B]0\f[R], in the expression .IP -.nf -\f[C] +.EX a[i++] = i++ -\f[R] -.fi +.EE .PP the first (or 0th) element of \f[B]a\f[R] is set to \f[B]1\f[R], and \f[B]i\f[R] is equal to \f[B]2\f[R] at the end of the expression. .PP This includes function arguments. Thus, assuming \f[B]i\f[R] is equal to \f[B]0\f[R], this means that in the expression .IP -.nf -\f[C] +.EX x(i++, i++) -\f[R] -.fi +.EE .PP the first argument passed to \f[B]x()\f[R] is \f[B]0\f[R], and the second argument is \f[B]1\f[R], while \f[B]i\f[R] is equal to \f[B]2\f[R] before the function starts executing. .SH FUNCTIONS -.PP Function definitions are as follows: .IP -.nf -\f[C] +.EX define I(I,...,I){ auto I,...,I S;...;S return(E) } -\f[R] -.fi +.EE .PP Any \f[B]I\f[R] in the parameter list or \f[B]auto\f[R] list may be replaced with \f[B]I[]\f[R] to make a parameter or \f[B]auto\f[R] var an array, and any \f[B]I\f[R] in the parameter list may be replaced with \f[B]*I[]\f[R] to make a parameter an array reference. Callers of functions that take array references should not put an asterisk in the call; they must be called with just \f[B]I[]\f[R] like normal array parameters and will be automatically converted into references. .PP As a \f[B]non-portable extension\f[R], the opening brace of a \f[B]define\f[R] statement may appear on the next line. .PP As a \f[B]non-portable extension\f[R], the return statement may also be in one of the following forms: .IP "1." 3 \f[B]return\f[R] .IP "2." 3 \f[B]return\f[R] \f[B](\f[R] \f[B])\f[R] .IP "3." 3 \f[B]return\f[R] \f[B]E\f[R] .PP The first two, or not specifying a \f[B]return\f[R] statement, is equivalent to \f[B]return (0)\f[R], unless the function is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection below). .SS Void Functions -.PP Functions can also be \f[B]void\f[R] functions, defined as follows: .IP -.nf -\f[C] +.EX define void I(I,...,I){ auto I,...,I S;...;S return } -\f[R] -.fi +.EE .PP They can only be used as standalone expressions, where such an expression would be printed alone, except in a print statement. .PP Void functions can only use the first two \f[B]return\f[R] statements listed above. They can also omit the return statement entirely. .PP The word \[lq]void\[rq] is not treated as a keyword; it is still possible to have variables, arrays, and functions named \f[B]void\f[R]. The word \[lq]void\[rq] is only treated specially right after the \f[B]define\f[R] keyword. .PP This is a \f[B]non-portable extension\f[R]. .SS Array References -.PP For any array in the parameter list, if the array is declared in the form .IP -.nf -\f[C] +.EX *I[] -\f[R] -.fi +.EE .PP it is a \f[B]reference\f[R]. Any changes to the array in the function are reflected, when the function returns, to the array that was passed in. .PP Other than this, all function arguments are passed by value. .PP This is a \f[B]non-portable extension\f[R]. .SH LIBRARY -.PP All of the functions below, including the functions in the extended math library (see the \f[I]Extended Library\f[R] subsection below), are available when the \f[B]-l\f[R] or \f[B]--mathlib\f[R] command-line flags are given, except that the extended math library is not available when the \f[B]-s\f[R] option, the \f[B]-w\f[R] option, or equivalents are given. .SS Standard Library -.PP The standard (see the \f[B]STANDARDS\f[R] section) defines the following functions for the math library: .TP \f[B]s(x)\f[R] Returns the sine of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]c(x)\f[R] Returns the cosine of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]a(x)\f[R] Returns the arctangent of \f[B]x\f[R], in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]l(x)\f[R] Returns the natural logarithm of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]e(x)\f[R] Returns the mathematical constant \f[B]e\f[R] raised to the power of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]j(x, n)\f[R] Returns the bessel integer order \f[B]n\f[R] (truncated) of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .SS Extended Library -.PP The extended library is \f[I]not\f[R] loaded when the \f[B]-s\f[R]/\f[B]--standard\f[R] or \f[B]-w\f[R]/\f[B]--warn\f[R] options are given since they are not part of the library defined by the standard (see the \f[B]STANDARDS\f[R] section). .PP The extended library is a \f[B]non-portable extension\f[R]. .TP \f[B]p(x, y)\f[R] Calculates \f[B]x\f[R] to the power of \f[B]y\f[R], even if \f[B]y\f[R] is not an integer, and returns the result to the current \f[B]scale\f[R]. .RS .PP It is an error if \f[B]y\f[R] is negative and \f[B]x\f[R] is \f[B]0\f[R]. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]r(x, p)\f[R] Returns \f[B]x\f[R] rounded to \f[B]p\f[R] decimal places according to the rounding mode round half away from \f[B]0\f[R] (https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero). .TP \f[B]ceil(x, p)\f[R] Returns \f[B]x\f[R] rounded to \f[B]p\f[R] decimal places according to the rounding mode round away from \f[B]0\f[R] (https://en.wikipedia.org/wiki/Rounding#Rounding_away_from_zero). .TP \f[B]f(x)\f[R] Returns the factorial of the truncated absolute value of \f[B]x\f[R]. .TP \f[B]max(a, b)\f[R] Returns \f[B]a\f[R] if \f[B]a\f[R] is greater than \f[B]b\f[R]; otherwise, returns \f[B]b\f[R]. .TP \f[B]min(a, b)\f[R] Returns \f[B]a\f[R] if \f[B]a\f[R] is less than \f[B]b\f[R]; otherwise, returns \f[B]b\f[R]. .TP \f[B]perm(n, k)\f[R] Returns the permutation of the truncated absolute value of \f[B]n\f[R] of the truncated absolute value of \f[B]k\f[R], if \f[B]k <= n\f[R]. If not, it returns \f[B]0\f[R]. .TP \f[B]comb(n, k)\f[R] Returns the combination of the truncated absolute value of \f[B]n\f[R] of the truncated absolute value of \f[B]k\f[R], if \f[B]k <= n\f[R]. If not, it returns \f[B]0\f[R]. .TP \f[B]fib(n)\f[R] Returns the Fibonacci number of the truncated absolute value of \f[B]n\f[R]. .TP \f[B]l2(x)\f[R] Returns the logarithm base \f[B]2\f[R] of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]l10(x)\f[R] Returns the logarithm base \f[B]10\f[R] of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]log(x, b)\f[R] Returns the logarithm base \f[B]b\f[R] of \f[B]x\f[R]. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]cbrt(x)\f[R] Returns the cube root of \f[B]x\f[R]. .TP \f[B]root(x, n)\f[R] Calculates the truncated value of \f[B]n\f[R], \f[B]r\f[R], and returns the \f[B]r\f[R]th root of \f[B]x\f[R] to the current \f[B]scale\f[R]. .RS .PP If \f[B]r\f[R] is \f[B]0\f[R] or negative, this raises an error and causes bc(1) to reset (see the \f[B]RESET\f[R] section). It also raises an error and causes bc(1) to reset if \f[B]r\f[R] is even and \f[B]x\f[R] is negative. .RE .TP \f[B]gcd(a, b)\f[R] Returns the greatest common divisor (factor) of the truncated absolute value of \f[B]a\f[R] and the truncated absolute value of \f[B]b\f[R]. .TP \f[B]lcm(a, b)\f[R] Returns the least common multiple of the truncated absolute value of \f[B]a\f[R] and the truncated absolute value of \f[B]b\f[R]. .TP \f[B]pi(p)\f[R] Returns \f[B]pi\f[R] to \f[B]p\f[R] decimal places. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]t(x)\f[R] Returns the tangent of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]a2(y, x)\f[R] Returns the arctangent of \f[B]y/x\f[R], in radians. If both \f[B]y\f[R] and \f[B]x\f[R] are equal to \f[B]0\f[R], it raises an error and causes bc(1) to reset (see the \f[B]RESET\f[R] section). Otherwise, if \f[B]x\f[R] is greater than \f[B]0\f[R], it returns \f[B]a(y/x)\f[R]. If \f[B]x\f[R] is less than \f[B]0\f[R], and \f[B]y\f[R] is greater than or equal to \f[B]0\f[R], it returns \f[B]a(y/x)+pi\f[R]. If \f[B]x\f[R] is less than \f[B]0\f[R], and \f[B]y\f[R] is less than \f[B]0\f[R], it returns \f[B]a(y/x)-pi\f[R]. If \f[B]x\f[R] is equal to \f[B]0\f[R], and \f[B]y\f[R] is greater than \f[B]0\f[R], it returns \f[B]pi/2\f[R]. If \f[B]x\f[R] is equal to \f[B]0\f[R], and \f[B]y\f[R] is less than \f[B]0\f[R], it returns \f[B]-pi/2\f[R]. .RS .PP This function is the same as the \f[B]atan2()\f[R] function in many programming languages. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]sin(x)\f[R] Returns the sine of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is an alias of \f[B]s(x)\f[R]. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]cos(x)\f[R] Returns the cosine of \f[B]x\f[R], which is assumed to be in radians. .RS .PP This is an alias of \f[B]c(x)\f[R]. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]tan(x)\f[R] Returns the tangent of \f[B]x\f[R], which is assumed to be in radians. .RS .PP If \f[B]x\f[R] is equal to \f[B]1\f[R] or \f[B]-1\f[R], this raises an error and causes bc(1) to reset (see the \f[B]RESET\f[R] section). .PP This is an alias of \f[B]t(x)\f[R]. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]atan(x)\f[R] Returns the arctangent of \f[B]x\f[R], in radians. .RS .PP This is an alias of \f[B]a(x)\f[R]. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]atan2(y, x)\f[R] Returns the arctangent of \f[B]y/x\f[R], in radians. If both \f[B]y\f[R] and \f[B]x\f[R] are equal to \f[B]0\f[R], it raises an error and causes bc(1) to reset (see the \f[B]RESET\f[R] section). Otherwise, if \f[B]x\f[R] is greater than \f[B]0\f[R], it returns \f[B]a(y/x)\f[R]. If \f[B]x\f[R] is less than \f[B]0\f[R], and \f[B]y\f[R] is greater than or equal to \f[B]0\f[R], it returns \f[B]a(y/x)+pi\f[R]. If \f[B]x\f[R] is less than \f[B]0\f[R], and \f[B]y\f[R] is less than \f[B]0\f[R], it returns \f[B]a(y/x)-pi\f[R]. If \f[B]x\f[R] is equal to \f[B]0\f[R], and \f[B]y\f[R] is greater than \f[B]0\f[R], it returns \f[B]pi/2\f[R]. If \f[B]x\f[R] is equal to \f[B]0\f[R], and \f[B]y\f[R] is less than \f[B]0\f[R], it returns \f[B]-pi/2\f[R]. .RS .PP This function is the same as the \f[B]atan2()\f[R] function in many programming languages. .PP This is an alias of \f[B]a2(y, x)\f[R]. .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]r2d(x)\f[R] Converts \f[B]x\f[R] from radians to degrees and returns the result. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]d2r(x)\f[R] Converts \f[B]x\f[R] from degrees to radians and returns the result. .RS .PP This is a transcendental function (see the \f[I]Transcendental Functions\f[R] subsection below). .RE .TP \f[B]frand(p)\f[R] Generates a pseudo-random integer between \f[B]0\f[R] (inclusive) and \f[B]1\f[R] (exclusive) with the number of decimal digits after the decimal point equal to the truncated absolute value of \f[B]p\f[R]. If \f[B]p\f[R] is not \f[B]0\f[R], then calling this function will change the value of \f[B]seed\f[R]. If \f[B]p\f[R] is \f[B]0\f[R], then \f[B]0\f[R] is returned, and \f[B]seed\f[R] is \f[I]not\f[R] changed. .TP \f[B]ifrand(i, p)\f[R] Generates a pseudo-random integer that is between \f[B]0\f[R] (inclusive) and the truncated absolute value of \f[B]i\f[R] (exclusive) with the number of decimal digits after the decimal point equal to the truncated absolute value of \f[B]p\f[R]. If the absolute value of \f[B]i\f[R] is greater than or equal to \f[B]2\f[R], and \f[B]p\f[R] is not \f[B]0\f[R], then calling this function will change the value of \f[B]seed\f[R]; otherwise, \f[B]0\f[R] is returned, and \f[B]seed\f[R] is not changed. .TP \f[B]i2rand(a, b)\f[R] Takes the truncated value of \f[B]a\f[R] and \f[B]b\f[R] and uses them as inclusive bounds to enerate a pseudo-random integer. If the difference of the truncated values of \f[B]a\f[R] and \f[B]b\f[R] is \f[B]0\f[R], then the truncated value is returned, and \f[B]seed\f[R] is \f[I]not\f[R] changed. Otherwise, this function will change the value of \f[B]seed\f[R]. .TP \f[B]srand(x)\f[R] Returns \f[B]x\f[R] with its sign flipped with probability \f[B]0.5\f[R]. In other words, it randomizes the sign of \f[B]x\f[R]. .TP \f[B]brand()\f[R] Returns a random boolean value (either \f[B]0\f[R] or \f[B]1\f[R]). .TP \f[B]band(a, b)\f[R] Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R] and calculates and returns the result of the bitwise \f[B]and\f[R] operation between them. .RS .PP If you want to use signed two\[cq]s complement arguments, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bor(a, b)\f[R] Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R] and calculates and returns the result of the bitwise \f[B]or\f[R] operation between them. .RS .PP If you want to use signed two\[cq]s complement arguments, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bxor(a, b)\f[R] Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R] and calculates and returns the result of the bitwise \f[B]xor\f[R] operation between them. .RS .PP If you want to use signed two\[cq]s complement arguments, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bshl(a, b)\f[R] Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R] and calculates and returns the result of \f[B]a\f[R] bit-shifted left by \f[B]b\f[R] places. .RS .PP If you want to use signed two\[cq]s complement arguments, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bshr(a, b)\f[R] Takes the truncated absolute value of both \f[B]a\f[R] and \f[B]b\f[R] and calculates and returns the truncated result of \f[B]a\f[R] bit-shifted right by \f[B]b\f[R] places. .RS .PP If you want to use signed two\[cq]s complement arguments, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bnotn(x, n)\f[R] Takes the truncated absolute value of \f[B]x\f[R] and does a bitwise not as though it has the same number of bytes as the truncated absolute value of \f[B]n\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bnot8(x)\f[R] Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as -though it has \f[B]8\f[R] binary digits (1 unsigned byte). +though it has \f[B]8\f[R] binary digits (\f[B]1\f[R] unsigned byte). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bnot16(x)\f[R] Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as -though it has \f[B]16\f[R] binary digits (2 unsigned bytes). +though it has \f[B]16\f[R] binary digits (\f[B]2\f[R] unsigned bytes). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bnot32(x)\f[R] Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as -though it has \f[B]32\f[R] binary digits (4 unsigned bytes). +though it has \f[B]32\f[R] binary digits (\f[B]4\f[R] unsigned bytes). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bnot64(x)\f[R] Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as -though it has \f[B]64\f[R] binary digits (8 unsigned bytes). +though it has \f[B]64\f[R] binary digits (\f[B]8\f[R] unsigned bytes). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bnot(x)\f[R] Does a bitwise not of the truncated absolute value of \f[B]x\f[R] as though it has the minimum number of power of two unsigned bytes. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brevn(x, n)\f[R] Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as though it has the same number of 8-bit bytes as the truncated absolute value of \f[B]n\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brev8(x)\f[R] Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as -though it has 8 binary digits (1 unsigned byte). +though it has 8 binary digits (\f[B]1\f[R] unsigned byte). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brev16(x)\f[R] Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as -though it has 16 binary digits (2 unsigned bytes). +though it has 16 binary digits (\f[B]2\f[R] unsigned bytes). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brev32(x)\f[R] Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as -though it has 32 binary digits (4 unsigned bytes). +though it has 32 binary digits (\f[B]4\f[R] unsigned bytes). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brev64(x)\f[R] Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as -though it has 64 binary digits (8 unsigned bytes). +though it has 64 binary digits (\f[B]8\f[R] unsigned bytes). .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brev(x)\f[R] Runs a bit reversal on the truncated absolute value of \f[B]x\f[R] as though it has the minimum number of power of two unsigned bytes. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]broln(x, p, n)\f[R] Does a left bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has the same number of unsigned 8-bit bytes as the truncated absolute value of \f[B]n\f[R], by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by the \f[B]2\f[R] to the power of the number of binary digits in \f[B]n\f[R] 8-bit bytes. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brol8(x, p)\f[R] Does a left bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has \f[B]8\f[R] binary digits (\f[B]1\f[R] unsigned byte), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]8\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brol16(x, p)\f[R] Does a left bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has \f[B]16\f[R] binary digits (\f[B]2\f[R] unsigned bytes), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]16\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brol32(x, p)\f[R] Does a left bitwise rotatation of the truncated absolute value of -\f[B]x\f[R], as though it has \f[B]32\f[R] binary digits (\f[B]2\f[R] +\f[B]x\f[R], as though it has \f[B]32\f[R] binary digits (\f[B]4\f[R] unsigned bytes), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]32\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brol64(x, p)\f[R] Does a left bitwise rotatation of the truncated absolute value of -\f[B]x\f[R], as though it has \f[B]64\f[R] binary digits (\f[B]2\f[R] +\f[B]x\f[R], as though it has \f[B]64\f[R] binary digits (\f[B]8\f[R] unsigned bytes), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]64\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brol(x, p)\f[R] Does a left bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has the minimum number of power of two unsigned 8-bit bytes, by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by 2 to the power of the number of binary digits in the minimum number of 8-bit bytes. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]brorn(x, p, n)\f[R] Does a right bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has the same number of unsigned 8-bit bytes as the truncated absolute value of \f[B]n\f[R], by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by the \f[B]2\f[R] to the power of the number of binary digits in \f[B]n\f[R] 8-bit bytes. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bror8(x, p)\f[R] Does a right bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has \f[B]8\f[R] binary digits (\f[B]1\f[R] unsigned byte), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]8\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bror16(x, p)\f[R] Does a right bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has \f[B]16\f[R] binary digits (\f[B]2\f[R] unsigned bytes), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]16\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bror32(x, p)\f[R] Does a right bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has \f[B]32\f[R] binary digits (\f[B]2\f[R] unsigned bytes), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]32\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bror64(x, p)\f[R] Does a right bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has \f[B]64\f[R] binary digits (\f[B]2\f[R] unsigned bytes), by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by \f[B]2\f[R] to the power of \f[B]64\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bror(x, p)\f[R] Does a right bitwise rotatation of the truncated absolute value of \f[B]x\f[R], as though it has the minimum number of power of two unsigned 8-bit bytes, by the number of places equal to the truncated absolute value of \f[B]p\f[R] modded by 2 to the power of the number of binary digits in the minimum number of 8-bit bytes. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bmodn(x, n)\f[R] Returns the modulus of the truncated absolute value of \f[B]x\f[R] by \f[B]2\f[R] to the power of the multiplication of the truncated absolute value of \f[B]n\f[R] and \f[B]8\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bmod8(x, n)\f[R] Returns the modulus of the truncated absolute value of \f[B]x\f[R] by \f[B]2\f[R] to the power of \f[B]8\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bmod16(x, n)\f[R] Returns the modulus of the truncated absolute value of \f[B]x\f[R] by \f[B]2\f[R] to the power of \f[B]16\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bmod32(x, n)\f[R] Returns the modulus of the truncated absolute value of \f[B]x\f[R] by \f[B]2\f[R] to the power of \f[B]32\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bmod64(x, n)\f[R] Returns the modulus of the truncated absolute value of \f[B]x\f[R] by \f[B]2\f[R] to the power of \f[B]64\f[R]. .RS .PP If you want to a use signed two\[cq]s complement argument, use \f[B]s2u(x)\f[R] to convert. .RE .TP \f[B]bunrev(t)\f[R] Assumes \f[B]t\f[R] is a bitwise-reversed number with an extra set bit one place more significant than the real most significant bit (which was the least significant bit in the original number). This number is reversed and returned without the extra set bit. .RS .PP This function is used to implement other bitwise functions; it is not meant to be used by users, but it can be. .RE .TP \f[B]plz(x)\f[R] If \f[B]x\f[R] is not equal to \f[B]0\f[R] and greater that \f[B]-1\f[R] and less than \f[B]1\f[R], it is printed with a leading zero, regardless of the use of the \f[B]-z\f[R] option (see the \f[B]OPTIONS\f[R] section) and without a trailing newline. .RS .PP Otherwise, \f[B]x\f[R] is printed normally, without a trailing newline. .RE .TP \f[B]plznl(x)\f[R] If \f[B]x\f[R] is not equal to \f[B]0\f[R] and greater that \f[B]-1\f[R] and less than \f[B]1\f[R], it is printed with a leading zero, regardless of the use of the \f[B]-z\f[R] option (see the \f[B]OPTIONS\f[R] section) and with a trailing newline. .RS .PP Otherwise, \f[B]x\f[R] is printed normally, with a trailing newline. .RE .TP \f[B]pnlz(x)\f[R] If \f[B]x\f[R] is not equal to \f[B]0\f[R] and greater that \f[B]-1\f[R] and less than \f[B]1\f[R], it is printed without a leading zero, regardless of the use of the \f[B]-z\f[R] option (see the \f[B]OPTIONS\f[R] section) and without a trailing newline. .RS .PP Otherwise, \f[B]x\f[R] is printed normally, without a trailing newline. .RE .TP \f[B]pnlznl(x)\f[R] If \f[B]x\f[R] is not equal to \f[B]0\f[R] and greater that \f[B]-1\f[R] and less than \f[B]1\f[R], it is printed without a leading zero, regardless of the use of the \f[B]-z\f[R] option (see the \f[B]OPTIONS\f[R] section) and with a trailing newline. .RS .PP Otherwise, \f[B]x\f[R] is printed normally, with a trailing newline. .RE .TP \f[B]ubytes(x)\f[R] Returns the numbers of unsigned integer bytes required to hold the truncated absolute value of \f[B]x\f[R]. .TP \f[B]sbytes(x)\f[R] Returns the numbers of signed, two\[cq]s-complement integer bytes required to hold the truncated value of \f[B]x\f[R]. .TP \f[B]s2u(x)\f[R] Returns \f[B]x\f[R] if it is non-negative. If it \f[I]is\f[R] negative, then it calculates what \f[B]x\f[R] would be as a 2\[cq]s-complement signed integer and returns the non-negative integer that would have the same representation in binary. .TP \f[B]s2un(x,n)\f[R] Returns \f[B]x\f[R] if it is non-negative. If it \f[I]is\f[R] negative, then it calculates what \f[B]x\f[R] would be as a 2\[cq]s-complement signed integer with \f[B]n\f[R] bytes and returns the non-negative integer that would have the same representation in binary. If \f[B]x\f[R] cannot fit into \f[B]n\f[R] 2\[cq]s-complement signed bytes, it is truncated to fit. .TP \f[B]hex(x)\f[R] Outputs the hexadecimal (base \f[B]16\f[R]) representation of \f[B]x\f[R]. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]binary(x)\f[R] Outputs the binary (base \f[B]2\f[R]) representation of \f[B]x\f[R]. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]output(x, b)\f[R] Outputs the base \f[B]b\f[R] representation of \f[B]x\f[R]. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]uint(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as an unsigned integer in as few power of two bytes as possible. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer or is negative, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]int(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as a signed, two\[cq]s-complement integer in as few power of two bytes as possible. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]uintn(x, n)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as an unsigned integer in \f[B]n\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer, is negative, or cannot fit into \f[B]n\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]intn(x, n)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as a signed, two\[cq]s-complement integer in \f[B]n\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer or cannot fit into \f[B]n\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]uint8(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as an unsigned integer in \f[B]1\f[R] byte. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer, is negative, or cannot fit into \f[B]1\f[R] byte, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]int8(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as a signed, two\[cq]s-complement integer in \f[B]1\f[R] byte. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer or cannot fit into \f[B]1\f[R] byte, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]uint16(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as an unsigned integer in \f[B]2\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer, is negative, or cannot fit into \f[B]2\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]int16(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as a signed, two\[cq]s-complement integer in \f[B]2\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer or cannot fit into \f[B]2\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]uint32(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as an unsigned integer in \f[B]4\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer, is negative, or cannot fit into \f[B]4\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]int32(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as a signed, two\[cq]s-complement integer in \f[B]4\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer or cannot fit into \f[B]4\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]uint64(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as an unsigned integer in \f[B]8\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer, is negative, or cannot fit into \f[B]8\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]int64(x)\f[R] Outputs the representation, in binary and hexadecimal, of \f[B]x\f[R] as a signed, two\[cq]s-complement integer in \f[B]8\f[R] bytes. Both outputs are split into bytes separated by spaces. .RS .PP If \f[B]x\f[R] is not an integer or cannot fit into \f[B]8\f[R] bytes, an error message is printed instead, but bc(1) is not reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]hex_uint(x, n)\f[R] Outputs the representation of the truncated absolute value of \f[B]x\f[R] as an unsigned integer in hexadecimal using \f[B]n\f[R] bytes. Not all of the value will be output if \f[B]n\f[R] is too small. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]binary_uint(x, n)\f[R] Outputs the representation of the truncated absolute value of \f[B]x\f[R] as an unsigned integer in binary using \f[B]n\f[R] bytes. Not all of the value will be output if \f[B]n\f[R] is too small. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]output_uint(x, n)\f[R] Outputs the representation of the truncated absolute value of \f[B]x\f[R] as an unsigned integer in the current \f[B]obase\f[R] (see the \f[B]SYNTAX\f[R] section) using \f[B]n\f[R] bytes. Not all of the value will be output if \f[B]n\f[R] is too small. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .TP \f[B]output_byte(x, i)\f[R] Outputs byte \f[B]i\f[R] of the truncated absolute value of \f[B]x\f[R], where \f[B]0\f[R] is the least significant byte and \f[B]number_of_bytes - 1\f[R] is the most significant byte. .RS .PP This is a \f[B]void\f[R] function (see the \f[I]Void Functions\f[R] subsection of the \f[B]FUNCTIONS\f[R] section). .RE .SS Transcendental Functions -.PP All transcendental functions can return slightly inaccurate results, up to 1 ULP (https://en.wikipedia.org/wiki/Unit_in_the_last_place). This is unavoidable, and the article at https://people.eecs.berkeley.edu/\[ti]wkahan/LOG10HAF.TXT explains why it is impossible and unnecessary to calculate exact results for the transcendental functions. .PP Because of the possible inaccuracy, I recommend that users call those functions with the precision (\f[B]scale\f[R]) set to at least 1 higher than is necessary. If exact results are \f[I]absolutely\f[R] required, users can double the precision (\f[B]scale\f[R]) and then truncate. .PP The transcendental functions in the standard math library are: .IP \[bu] 2 \f[B]s(x)\f[R] .IP \[bu] 2 \f[B]c(x)\f[R] .IP \[bu] 2 \f[B]a(x)\f[R] .IP \[bu] 2 \f[B]l(x)\f[R] .IP \[bu] 2 \f[B]e(x)\f[R] .IP \[bu] 2 \f[B]j(x, n)\f[R] .PP The transcendental functions in the extended math library are: .IP \[bu] 2 \f[B]l2(x)\f[R] .IP \[bu] 2 \f[B]l10(x)\f[R] .IP \[bu] 2 \f[B]log(x, b)\f[R] .IP \[bu] 2 \f[B]pi(p)\f[R] .IP \[bu] 2 \f[B]t(x)\f[R] .IP \[bu] 2 \f[B]a2(y, x)\f[R] .IP \[bu] 2 \f[B]sin(x)\f[R] .IP \[bu] 2 \f[B]cos(x)\f[R] .IP \[bu] 2 \f[B]tan(x)\f[R] .IP \[bu] 2 \f[B]atan(x)\f[R] .IP \[bu] 2 \f[B]atan2(y, x)\f[R] .IP \[bu] 2 \f[B]r2d(x)\f[R] .IP \[bu] 2 \f[B]d2r(x)\f[R] .SH RESET -.PP When bc(1) encounters an error or a signal that it has a non-default handler for, it resets. This means that several things happen. .PP First, any functions that are executing are stopped and popped off the stack. The behavior is not unlike that of exceptions in programming languages. Then the execution point is set so that any code waiting to execute (after all functions returned) is skipped. .PP Thus, when bc(1) resets, it skips any remaining code waiting to be executed. Then, if it is interactive mode, and the error was not a fatal error (see the \f[B]EXIT STATUS\f[R] section), it asks for more input; otherwise, it exits with the appropriate return code. .PP Note that this reset behavior is different from the GNU bc(1), which attempts to start executing the statement right after the one that caused an error. .SH PERFORMANCE -.PP Most bc(1) implementations use \f[B]char\f[R] types to calculate the value of \f[B]1\f[R] decimal digit at a time, but that can be slow. This bc(1) does something different. .PP It uses large integers to calculate more than \f[B]1\f[R] decimal digit at a time. If built in a environment where \f[B]BC_LONG_BIT\f[R] (see the \f[B]LIMITS\f[R] section) is \f[B]64\f[R], then each integer has \f[B]9\f[R] decimal digits. If built in an environment where \f[B]BC_LONG_BIT\f[R] is \f[B]32\f[R] then each integer has \f[B]4\f[R] decimal digits. This value (the number of decimal digits per large integer) is called \f[B]BC_BASE_DIGS\f[R]. .PP The actual values of \f[B]BC_LONG_BIT\f[R] and \f[B]BC_BASE_DIGS\f[R] can be queried with the \f[B]limits\f[R] statement. .PP In addition, this bc(1) uses an even larger integer for overflow checking. This integer type depends on the value of \f[B]BC_LONG_BIT\f[R], but is always at least twice as large as the integer type used to store digits. .SH LIMITS -.PP The following are the limits on bc(1): .TP \f[B]BC_LONG_BIT\f[R] The number of bits in the \f[B]long\f[R] type in the environment where bc(1) was built. This determines how many decimal digits can be stored in a single large integer (see the \f[B]PERFORMANCE\f[R] section). .TP \f[B]BC_BASE_DIGS\f[R] The number of decimal digits per large integer (see the \f[B]PERFORMANCE\f[R] section). Depends on \f[B]BC_LONG_BIT\f[R]. .TP \f[B]BC_BASE_POW\f[R] The max decimal number that each large integer can store (see \f[B]BC_BASE_DIGS\f[R]) plus \f[B]1\f[R]. Depends on \f[B]BC_BASE_DIGS\f[R]. .TP \f[B]BC_OVERFLOW_MAX\f[R] The max number that the overflow type (see the \f[B]PERFORMANCE\f[R] section) can hold. Depends on \f[B]BC_LONG_BIT\f[R]. .TP \f[B]BC_BASE_MAX\f[R] The maximum output base. Set at \f[B]BC_BASE_POW\f[R]. .TP \f[B]BC_DIM_MAX\f[R] The maximum size of arrays. Set at \f[B]SIZE_MAX-1\f[R]. .TP \f[B]BC_SCALE_MAX\f[R] The maximum \f[B]scale\f[R]. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_STRING_MAX\f[R] The maximum length of strings. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_NAME_MAX\f[R] The maximum length of identifiers. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_NUM_MAX\f[R] The maximum length of a number (in decimal digits), which includes digits after the decimal point. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_RAND_MAX\f[R] The maximum integer (inclusive) returned by the \f[B]rand()\f[R] operand. Set at \f[B]2\[ha]BC_LONG_BIT-1\f[R]. .TP Exponent The maximum allowable exponent (positive or negative). Set at \f[B]BC_OVERFLOW_MAX\f[R]. .TP Number of vars The maximum number of vars/arrays. Set at \f[B]SIZE_MAX-1\f[R]. .PP The actual values can be queried with the \f[B]limits\f[R] statement. .PP These limits are meant to be effectively non-existent; the limits are so large (at least on 64-bit machines) that there should not be any point at which they become a problem. In fact, memory should be exhausted before these limits should be hit. .SH ENVIRONMENT VARIABLES -.PP As \f[B]non-portable extensions\f[R], bc(1) recognizes the following environment variables: .TP \f[B]POSIXLY_CORRECT\f[R] If this variable exists (no matter the contents), bc(1) behaves as if the \f[B]-s\f[R] option was given. .TP \f[B]BC_ENV_ARGS\f[R] This is another way to give command-line arguments to bc(1). They should be in the same format as all other command-line arguments. These are always processed first, so any files given in \f[B]BC_ENV_ARGS\f[R] will be processed before arguments and files given on the command-line. This gives the user the ability to set up \[lq]standard\[rq] options and files to be used at every invocation. The most useful thing for such files to contain would be useful functions that the user might want every time bc(1) runs. .RS .PP The code that parses \f[B]BC_ENV_ARGS\f[R] will correctly handle quoted arguments, but it does not understand escape sequences. For example, the string \f[B]\[lq]/home/gavin/some bc file.bc\[rq]\f[R] will be correctly parsed, but the string \f[B]\[lq]/home/gavin/some \[dq]bc\[dq] file.bc\[rq]\f[R] will include the backslashes. .PP The quote parsing will handle either kind of quotes, \f[B]\[cq]\f[R] or \f[B]\[lq]\f[R]. Thus, if you have a file with any number of single quotes in the name, you can use double quotes as the outside quotes, as in \f[B]\[lq]some `bc' file.bc\[rq]\f[R], and vice versa if you have a file with double quotes. However, handling a file with both kinds of quotes in \f[B]BC_ENV_ARGS\f[R] is not supported due to the complexity of the parsing, though such files are still supported on the command-line where the parsing is done by the shell. .RE .TP \f[B]BC_LINE_LENGTH\f[R] If this environment variable exists and contains an integer that is greater than \f[B]1\f[R] and is less than \f[B]UINT16_MAX\f[R] (\f[B]2\[ha]16-1\f[R]), bc(1) will output lines to that length, including the backslash (\f[B]\[rs]\f[R]). The default line length is \f[B]70\f[R]. .RS .PP The special value of \f[B]0\f[R] will disable line length checking and print numbers without regard to line length and without backslashes and newlines. .RE .TP \f[B]BC_BANNER\f[R] If this environment variable exists and contains an integer, then a non-zero value activates the copyright banner when bc(1) is in interactive mode, while zero deactivates it. .RS .PP If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), then this environment variable has no effect because bc(1) does not print the banner when not in interactive mode. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_SIGINT_RESET\f[R] If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), then this environment variable has no effect because bc(1) exits on \f[B]SIGINT\f[R] when not in interactive mode. .RS .PP However, when bc(1) is in interactive mode, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) reset on \f[B]SIGINT\f[R], rather than exit, and zero makes bc(1) exit. If this environment variable exists and is \f[I]not\f[R] an integer, then bc(1) will exit on \f[B]SIGINT\f[R]. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_TTY_MODE\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, then a non-zero value makes bc(1) use TTY mode, and zero makes bc(1) not use TTY mode. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_PROMPT\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) use a prompt, and zero or a non-integer makes bc(1) not use a prompt. If this environment variable does not exist and \f[B]BC_TTY_MODE\f[R] does, then the value of the \f[B]BC_TTY_MODE\f[R] environment variable is used. .PP This environment variable and the \f[B]BC_TTY_MODE\f[R] environment variable override the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_EXPR_EXIT\f[R] If any expressions or expression files are given on the command-line with \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R], then if this environment variable exists and contains an integer, a non-zero value makes bc(1) exit after executing the expressions and expression files, and a zero value makes bc(1) not exit. .RS .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]BC_DIGIT_CLAMP\f[R] When parsing numbers and if this environment variable exists and contains an integer, a non-zero value makes bc(1) clamp digits that are greater than or equal to the current \f[B]ibase\f[R] so that all such digits are considered equal to the \f[B]ibase\f[R] minus 1, and a zero value disables such clamping so that those digits are always equal to their value, which is multiplied by the power of the \f[B]ibase\f[R]. .RS .PP This never applies to single-digit numbers, as per the standard (see the \f[B]STANDARDS\f[R] section). .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .SH EXIT STATUS -.PP bc(1) returns the following exit statuses: .TP \f[B]0\f[R] No error. .TP \f[B]1\f[R] A math error occurred. This follows standard practice of using \f[B]1\f[R] for expected errors, since math errors will happen in the process of normal execution. .RS .PP Math errors include divide by \f[B]0\f[R], taking the square root of a negative number, using a negative number as a bound for the pseudo-random number generator, attempting to convert a negative number to a hardware integer, overflow when converting a number to a hardware integer, overflow when calculating the size of a number, and attempting to use a non-integer where an integer is required. .PP Converting to a hardware integer happens for the second operand of the power (\f[B]\[ha]\f[R]), places (\f[B]\[at]\f[R]), left shift (\f[B]<<\f[R]), and right shift (\f[B]>>\f[R]) operators and their corresponding assignment operators. .RE .TP \f[B]2\f[R] A parse error occurred. .RS .PP Parse errors include unexpected \f[B]EOF\f[R], using an invalid character, failing to find the end of a string or comment, using a token where it is invalid, giving an invalid expression, giving an invalid print statement, giving an invalid function definition, attempting to assign to an expression that is not a named expression (see the \f[I]Named Expressions\f[R] subsection of the \f[B]SYNTAX\f[R] section), giving an invalid \f[B]auto\f[R] list, having a duplicate \f[B]auto\f[R]/function parameter, failing to find the end of a code block, attempting to return a value from a \f[B]void\f[R] function, attempting to use a variable as a reference, and using any extensions when the option \f[B]-s\f[R] or any equivalents were given. .RE .TP \f[B]3\f[R] A runtime error occurred. .RS .PP Runtime errors include assigning an invalid number to any global (\f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R]), giving a bad expression to a \f[B]read()\f[R] call, calling \f[B]read()\f[R] inside of a \f[B]read()\f[R] call, type errors, passing the wrong number of arguments to functions, attempting to call an undefined function, and attempting to use a \f[B]void\f[R] function call as a value in an expression. .RE .TP \f[B]4\f[R] A fatal error occurred. .RS .PP Fatal errors include memory allocation errors, I/O errors, failing to open files, attempting to use files that do not have only ASCII characters (bc(1) only accepts ASCII characters), attempting to open a directory as a file, and giving invalid command-line options. .RE .PP The exit status \f[B]4\f[R] is special; when a fatal error occurs, bc(1) always exits and returns \f[B]4\f[R], no matter what mode bc(1) is in. .PP The other statuses will only be returned when bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), since bc(1) resets its state (see the \f[B]RESET\f[R] section) and accepts more input when one of those errors occurs in interactive mode. This is also the case when interactive mode is forced by the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .PP These exit statuses allow bc(1) to be used in shell scripting with error checking, and its normal behavior can be forced by using the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .SH INTERACTIVE MODE -.PP Per the standard (see the \f[B]STANDARDS\f[R] section), bc(1) has an interactive mode and a non-interactive mode. Interactive mode is turned on automatically when both \f[B]stdin\f[R] and \f[B]stdout\f[R] are hooked to a terminal, but the \f[B]-i\f[R] flag and \f[B]--interactive\f[R] option can turn it on in other situations. .PP In interactive mode, bc(1) attempts to recover from errors (see the \f[B]RESET\f[R] section), and in normal execution, flushes \f[B]stdout\f[R] as soon as execution is done for the current input. bc(1) may also reset on \f[B]SIGINT\f[R] instead of exit, depending on the contents of, or default for, the \f[B]BC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .SH TTY MODE -.PP If \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY, then \[lq]TTY mode\[rq] is considered to be available, and thus, bc(1) can turn on TTY mode, subject to some settings. .PP If there is the environment variable \f[B]BC_TTY_MODE\f[R] in the environment (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then if that environment variable contains a non-zero integer, bc(1) will turn on TTY mode when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY. If the \f[B]BC_TTY_MODE\f[R] environment variable exists but is \f[I]not\f[R] a non-zero integer, then bc(1) will not turn TTY mode on. .PP If the environment variable \f[B]BC_TTY_MODE\f[R] does \f[I]not\f[R] exist, the default setting is used. The default setting can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP TTY mode is different from interactive mode because interactive mode is required in the bc(1) standard (see the \f[B]STANDARDS\f[R] section), and interactive mode requires only \f[B]stdin\f[R] and \f[B]stdout\f[R] to be connected to a terminal. .SS Command-Line History -.PP Command-line history is only enabled if TTY mode is, i.e., that \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the \f[B]BC_TTY_MODE\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and its default do not disable TTY mode. See the \f[B]COMMAND LINE HISTORY\f[R] section for more information. .SS Prompt -.PP If TTY mode is available, then a prompt can be enabled. Like TTY mode itself, it can be turned on or off with an environment variable: \f[B]BC_PROMPT\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP If the environment variable \f[B]BC_PROMPT\f[R] exists and is a non-zero integer, then the prompt is turned on when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options were not used. The read prompt will be turned on under the same conditions, except that the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options must also not be used. .PP However, if \f[B]BC_PROMPT\f[R] does not exist, the prompt can be enabled or disabled with the \f[B]BC_TTY_MODE\f[R] environment variable, the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options, and the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options. See the \f[B]ENVIRONMENT VARIABLES\f[R] and \f[B]OPTIONS\f[R] sections for more details. .SH SIGNAL HANDLING -.PP Sending a \f[B]SIGINT\f[R] will cause bc(1) to do one of two things. .PP If bc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), or the \f[B]BC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or its default, is either not an integer or it is zero, bc(1) will exit. .PP However, if bc(1) is in interactive mode, and the \f[B]BC_SIGINT_RESET\f[R] or its default is an integer and non-zero, then bc(1) will stop executing the current input and reset (see the \f[B]RESET\f[R] section) upon receiving a \f[B]SIGINT\f[R]. .PP Note that \[lq]current input\[rq] can mean one of two things. If bc(1) is processing input from \f[B]stdin\f[R] in interactive mode, it will ask for more input. If bc(1) is processing input from a file in interactive mode, it will stop processing the file and start processing the next file, if one exists, or ask for input from \f[B]stdin\f[R] if no other file exists. .PP This means that if a \f[B]SIGINT\f[R] is sent to bc(1) as it is executing a file, it can seem as though bc(1) did not respond to the signal since it will immediately start executing the next file. This is by design; most files that users execute when interacting with bc(1) have function definitions, which are quick to parse. If a file takes a long time to execute, there may be a bug in that file. The rest of the files could still be executed without problem, allowing the user to continue. .PP \f[B]SIGTERM\f[R] and \f[B]SIGQUIT\f[R] cause bc(1) to clean up and exit, and it uses the default handler for all other signals. The one exception is \f[B]SIGHUP\f[R]; in that case, and only when bc(1) is in TTY mode (see the \f[B]TTY MODE\f[R] section), a \f[B]SIGHUP\f[R] will cause bc(1) to clean up and exit. .SH COMMAND LINE HISTORY -.PP bc(1) supports interactive command-line editing. .PP If bc(1) can be in TTY mode (see the \f[B]TTY MODE\f[R] section), history can be enabled. This means that command-line history can only be enabled when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY. .PP Like TTY mode itself, it can be turned on or off with the environment variable \f[B]BC_TTY_MODE\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP If history is enabled, previous lines can be recalled and edited with the arrow keys. .PP \f[B]Note\f[R]: tabs are converted to 8 spaces. .SH SEE ALSO -.PP dc(1) .SH STANDARDS -.PP bc(1) is compliant with the IEEE Std 1003.1-2017 (\[lq]POSIX.1-2017\[rq]) specification at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html . The flags \f[B]-efghiqsvVw\f[R], all long options, and the extensions noted above are extensions to that specification. .PP In addition, the behavior of the \f[B]quit\f[R] implements an interpretation of that specification that is different from all known implementations. For more information see the \f[B]Statements\f[R] subsection of the \f[B]SYNTAX\f[R] section. .PP Note that the specification explicitly says that bc(1) only accepts numbers that use a period (\f[B].\f[R]) as a radix point, regardless of the value of \f[B]LC_NUMERIC\f[R]. .SH BUGS -.PP Before version \f[B]6.1.0\f[R], this bc(1) had incorrect behavior for the \f[B]quit\f[R] statement. .PP No other bugs are known. Report bugs at https://git.gavinhoward.com/gavin/bc . .SH AUTHORS -.PP -Gavin D. -Howard and contributors. +Gavin D. Howard \c +.MT gavin@gavinhoward.com +.ME \c +\ and contributors. diff --git a/contrib/bc/manuals/bc/N.1.md b/contrib/bc/manuals/bc/N.1.md index d4a08817c952..5d5334c70d72 100644 --- a/contrib/bc/manuals/bc/N.1.md +++ b/contrib/bc/manuals/bc/N.1.md @@ -1,2517 +1,2517 @@ # NAME bc - arbitrary-precision decimal arithmetic language and calculator # SYNOPSIS **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*] # DESCRIPTION bc(1) is an interactive processor for a language first standardized in 1991 by POSIX. (See the **STANDARDS** section.) The language provides unlimited precision decimal arithmetic and is somewhat C-like, but there are differences. Such differences will be noted in this document. After parsing and handling options, this bc(1) reads any files given on the command line and executes them before reading from **stdin**. This bc(1) is a drop-in replacement for *any* bc(1), including (and especially) the GNU bc(1). It also has many extensions and extra features beyond other implementations. **Note**: If running this bc(1) on *any* script meant for another bc(1) gives a parse error, it is probably because a word this bc(1) reserves as a keyword is used as the name of a function, variable, or array. To fix that, use the command-line option **-r** *keyword*, where *keyword* is the keyword that is used as a name in the script. For more information, see the **OPTIONS** section. If parsing scripts meant for other bc(1) implementations still does not work, that is a bug and should be reported. See the **BUGS** section. # OPTIONS The following are the options that bc(1) accepts. **-C**, **-\-no-digit-clamp** : Disables clamping of digits greater than or equal to the current **ibase** when parsing numbers. This means that the value added to a number from a digit is always that digit's value multiplied by the value of ibase raised to the power of the digit's position, which starts from 0 at the least significant digit. If this and/or the **-c** or **-\-digit-clamp** options are given multiple times, the last one given is used. This option overrides the **BC_DIGIT_CLAMP** environment variable (see the **ENVIRONMENT VARIABLES** section) and the default, which can be queried with the **-h** or **-\-help** options. This is a **non-portable extension**. **-c**, **-\-digit-clamp** : Enables clamping of digits greater than or equal to the current **ibase** when parsing numbers. This means that digits that the value added to a number from a digit that is greater than or equal to the ibase is the value of ibase minus 1 all multiplied by the value of ibase raised to the power of the digit's position, which starts from 0 at the least significant digit. If this and/or the **-C** or **-\-no-digit-clamp** options are given multiple times, the last one given is used. This option overrides the **BC_DIGIT_CLAMP** environment variable (see the **ENVIRONMENT VARIABLES** section) and the default, which can be queried with the **-h** or **-\-help** options. This is a **non-portable extension**. **-E** *seed*, **-\-seed**=*seed* : Sets the builtin variable **seed** to the value *seed* assuming that *seed* is in base 10. It is a fatal error if *seed* is not a valid number. If multiple instances of this option are given, the last is used. This is a **non-portable extension**. **-e** *expr*, **-\-expression**=*expr* : Evaluates *expr*. If multiple expressions are given, they are evaluated in order. If files are given as well (see the **-f** and **-\-file** options), the expressions and files are evaluated in the order given. This means that if a file is given before an expression, the file is read in and evaluated first. If this option is given on the command-line (i.e., not in **BC_ENV_ARGS**, see the **ENVIRONMENT VARIABLES** section), then after processing all expressions and files, bc(1) will exit, unless **-** (**stdin**) was given as an argument at least once to **-f** or **-\-file**, whether on the command-line or in **BC_ENV_ARGS**. However, if any other **-e**, **-\-expression**, **-f**, or **-\-file** arguments are given after **-f-** or equivalent is given, bc(1) will give a fatal error and exit. This is a **non-portable extension**. **-f** *file*, **-\-file**=*file* : Reads in *file* and evaluates it, line by line, as though it were read through **stdin**. If expressions are also given (see the **-e** and **-\-expression** options), the expressions are evaluated in the order given. If this option is given on the command-line (i.e., not in **BC_ENV_ARGS**, see the **ENVIRONMENT VARIABLES** section), then after processing all expressions and files, bc(1) will exit, unless **-** (**stdin**) was given as an argument at least once to **-f** or **-\-file**. However, if any other **-e**, **-\-expression**, **-f**, or **-\-file** arguments are given after **-f-** or equivalent is given, bc(1) will give a fatal error and exit. This is a **non-portable extension**. **-g**, **-\-global-stacks** : Turns the globals **ibase**, **obase**, **scale**, and **seed** into stacks. This has the effect that a copy of the current value of all four are pushed onto a stack for every function call, as well as popped when every function returns. This means that functions can assign to any and all of those globals without worrying that the change will affect other functions. Thus, a hypothetical function named **output(x,b)** that simply printed **x** in base **b** could be written like this: define void output(x, b) { obase=b x } instead of like this: define void output(x, b) { auto c c=obase obase=b x obase=c } This makes writing functions much easier. (**Note**: the function **output(x,b)** exists in the extended math library. See the **LIBRARY** section.) However, since using this flag means that functions cannot set **ibase**, **obase**, **scale**, or **seed** globally, functions that are made to do so cannot work anymore. There are two possible use cases for that, and each has a solution. First, if a function is called on startup to turn bc(1) into a number converter, it is possible to replace that capability with various shell aliases. Examples: alias d2o="bc -e ibase=A -e obase=8" alias h2b="bc -e ibase=G -e obase=2" Second, if the purpose of a function is to set **ibase**, **obase**, **scale**, or **seed** globally for any other purpose, it could be split into one to four functions (based on how many globals it sets) and each of those functions could return the desired value for a global. For functions that set **seed**, the value assigned to **seed** is not propagated to parent functions. This means that the sequence of pseudo-random numbers that they see will not be the same sequence of pseudo-random numbers that any parent sees. This is only the case once **seed** has been set. If a function desires to not affect the sequence of pseudo-random numbers of its parents, but wants to use the same **seed**, it can use the following line: seed = seed If the behavior of this option is desired for every run of bc(1), then users could make sure to define **BC_ENV_ARGS** and include this option (see the **ENVIRONMENT VARIABLES** section for more details). If **-s**, **-w**, or any equivalents are used, this option is ignored. This is a **non-portable extension**. **-h**, **-\-help** : Prints a usage message and exits. **-I** *ibase*, **-\-ibase**=*ibase* : Sets the builtin variable **ibase** to the value *ibase* assuming that *ibase* is in base 10. It is a fatal error if *ibase* is not a valid number. If multiple instances of this option are given, the last is used. This is a **non-portable extension**. **-i**, **-\-interactive** : Forces interactive mode. (See the **INTERACTIVE MODE** section.) This is a **non-portable extension**. **-L**, **-\-no-line-length** : Disables line length checking and prints numbers without backslashes and newlines. In other words, this option sets **BC_LINE_LENGTH** to **0** (see the **ENVIRONMENT VARIABLES** section). This is a **non-portable extension**. **-l**, **-\-mathlib** : Sets **scale** (see the **SYNTAX** section) to **20** and loads the included math library and the extended math library before running any code, including any expressions or files specified on the command line. To learn what is in the libraries, see the **LIBRARY** section. **-O** *obase*, **-\-obase**=*obase* : Sets the builtin variable **obase** to the value *obase* assuming that *obase* is in base 10. It is a fatal error if *obase* is not a valid number. If multiple instances of this option are given, the last is used. This is a **non-portable extension**. **-P**, **-\-no-prompt** : Disables the prompt in TTY mode. (The prompt is only enabled in TTY mode. See the **TTY MODE** section.) This is mostly for those users that do not want a prompt or are not used to having them in bc(1). Most of those users would want to put this option in **BC_ENV_ARGS** (see the **ENVIRONMENT VARIABLES** section). These options override the **BC_PROMPT** and **BC_TTY_MODE** environment variables (see the **ENVIRONMENT VARIABLES** section). This is a **non-portable extension**. **-q**, **-\-quiet** : This option is for compatibility with the GNU bc(1) (https://www.gnu.org/software/bc/); it is a no-op. Without this option, GNU bc(1) prints a copyright header. This bc(1) only prints the copyright header if one or more of the **-v**, **-V**, or **-\-version** options are given unless the **BC_BANNER** environment variable is set and contains a non-zero integer or if this bc(1) was built with the header displayed by default. If *any* of that is the case, then this option *does* prevent bc(1) from printing the header. This is a **non-portable extension**. **-R**, **-\-no-read-prompt** : Disables the read prompt in TTY mode. (The read prompt is only enabled in TTY mode. See the **TTY MODE** section.) This is mostly for those users that do not want a read prompt or are not used to having them in bc(1). Most of those users would want to put this option in **BC_ENV_ARGS** (see the **ENVIRONMENT VARIABLES** section). This option is also useful in hash bang lines of bc(1) scripts that prompt for user input. This option does not disable the regular prompt because the read prompt is only used when the **read()** built-in function is called. These options *do* override the **BC_PROMPT** and **BC_TTY_MODE** environment variables (see the **ENVIRONMENT VARIABLES** section), but only for the read prompt. This is a **non-portable extension**. **-r** *keyword*, **-\-redefine**=*keyword* : Redefines *keyword* in order to allow it to be used as a function, variable, or array name. This is useful when this bc(1) gives parse errors when parsing scripts meant for other bc(1) implementations. The keywords this bc(1) allows to be redefined are: * **abs** * **asciify** * **continue** * **divmod** * **else** * **halt** * **irand** * **last** * **limits** * **maxibase** * **maxobase** * **maxrand** * **maxscale** * **modexp** * **print** * **rand** * **read** * **seed** * **stream** If any of those keywords are used as a function, variable, or array name in a script, use this option with the keyword as the argument. If multiple are used, use this option for all of them; it can be used multiple times. Keywords are *not* redefined when parsing the builtin math library (see the **LIBRARY** section). It is a fatal error to redefine keywords mandated by the POSIX standard (see the **STANDARDS** section). It is a fatal error to attempt to redefine words that this bc(1) does not reserve as keywords. **-S** *scale*, **-\-scale**=*scale* : Sets the builtin variable **scale** to the value *scale* assuming that *scale* is in base 10. It is a fatal error if *scale* is not a valid number. If multiple instances of this option are given, the last is used. This is a **non-portable extension**. **-s**, **-\-standard** : Process exactly the language defined by the standard (see the **STANDARDS** section) and error if any extensions are used. This is a **non-portable extension**. **-v**, **-V**, **-\-version** : Print the version information (copyright header) and exits. This is a **non-portable extension**. **-w**, **-\-warn** : Like **-s** and **-\-standard**, except that warnings (and not errors) are printed for non-standard extensions and execution continues normally. This is a **non-portable extension**. **-z**, **-\-leading-zeroes** : Makes bc(1) print all numbers greater than **-1** and less than **1**, and not equal to **0**, with a leading zero. This can be set for individual numbers with the **plz(x)**, **plznl(x)**, **pnlz(x)**, and **pnlznl(x)** functions in the extended math library (see the **LIBRARY** section). This is a **non-portable extension**. All long options are **non-portable extensions**. # STDIN If no files or expressions are given by the **-f**, **-\-file**, **-e**, or **-\-expression** options, then bc(1) reads from **stdin**. However, there are a few caveats to this. First, **stdin** is evaluated a line at a time. The only exception to this is if the parse cannot complete. That means that starting a string without ending it or starting a function, **if** statement, or loop without ending it will also cause bc(1) to not execute. Second, after an **if** statement, bc(1) doesn't know if an **else** statement will follow, so it will not execute until it knows there will not be an **else** statement. # STDOUT Any non-error output is written to **stdout**. In addition, if history (see the **HISTORY** section) and the prompt (see the **TTY MODE** section) are enabled, both are output to **stdout**. **Note**: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the **EXIT STATUS** section) if it cannot write to **stdout**, so if **stdout** is closed, as in **bc >&-**, it will quit with an error. This is done so that bc(1) can report problems when **stdout** is redirected to a file. If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect **stdout** to **/dev/null**. # STDERR Any error output is written to **stderr**. **Note**: Unlike other bc(1) implementations, this bc(1) will issue a fatal error (see the **EXIT STATUS** section) if it cannot write to **stderr**, so if **stderr** is closed, as in **bc 2>&-**, it will quit with an error. This is done so that bc(1) can exit with an error code when **stderr** is redirected to a file. If there are scripts that depend on the behavior of other bc(1) implementations, it is recommended that those scripts be changed to redirect **stderr** to **/dev/null**. # SYNTAX The syntax for bc(1) programs is mostly C-like, with some differences. This bc(1) follows the POSIX standard (see the **STANDARDS** section), which is a much more thorough resource for the language this bc(1) accepts. This section is meant to be a summary and a listing of all the extensions to the standard. In the sections below, **E** means expression, **S** means statement, and **I** means identifier. Identifiers (**I**) start with a lowercase letter and can be followed by any number (up to **BC_NAME_MAX-1**) of lowercase letters (**a-z**), digits (**0-9**), and underscores (**\_**). The regex is **\[a-z\]\[a-z0-9\_\]\***. Identifiers with more than one character (letter) are a **non-portable extension**. **ibase** is a global variable determining how to interpret constant numbers. It is the "input" base, or the number base used for interpreting input numbers. **ibase** is initially **10**. If the **-s** (**-\-standard**) and **-w** (**-\-warn**) flags were not given on the command line, the max allowable value for **ibase** is **36**. Otherwise, it is **16**. The min allowable value for **ibase** is **2**. The max allowable value for **ibase** can be queried in bc(1) programs with the **maxibase()** built-in function. **obase** is a global variable determining how to output results. It is the "output" base, or the number base used for outputting numbers. **obase** is initially **10**. The max allowable value for **obase** is **BC_BASE_MAX** and can be queried in bc(1) programs with the **maxobase()** built-in function. The min allowable value for **obase** is **0**. If **obase** is **0**, values are output in scientific notation, and if **obase** is **1**, values are output in engineering notation. Otherwise, values are output in the specified base. Outputting in scientific and engineering notations are **non-portable extensions**. The *scale* of an expression is the number of digits in the result of the expression right of the decimal point, and **scale** is a global variable that sets the precision of any operations, with exceptions. **scale** is initially **0**. **scale** cannot be negative. The max allowable value for **scale** is **BC_SCALE_MAX** and can be queried in bc(1) programs with the **maxscale()** built-in function. bc(1) has both *global* variables and *local* variables. All *local* variables are local to the function; they are parameters or are introduced in the **auto** list of a function (see the **FUNCTIONS** section). If a variable is accessed which is not a parameter or in the **auto** list, it is assumed to be *global*. If a parent function has a *local* variable version of a variable that a child function considers *global*, the value of that *global* variable in the child function is the value of the variable in the parent function, not the value of the actual *global* variable. All of the above applies to arrays as well. The value of a statement that is an expression (i.e., any of the named expressions or operands) is printed unless the lowest precedence operator is an assignment operator *and* the expression is notsurrounded by parentheses. The value that is printed is also assigned to the special variable **last**. A single dot (**.**) may also be used as a synonym for **last**. These are **non-portable extensions**. Either semicolons or newlines may separate statements. ## Comments There are two kinds of comments: 1. Block comments are enclosed in **/\*** and **\*/**. 2. Line comments go from **#** until, and not including, the next newline. This is a **non-portable extension**. ## Named Expressions The following are named expressions in bc(1): 1. Variables: **I** 2. Array Elements: **I[E]** 3. **ibase** 4. **obase** 5. **scale** 6. **seed** 7. **last** or a single dot (**.**) Numbers 6 and 7 are **non-portable extensions**. The meaning of **seed** is dependent on the current pseudo-random number generator but is guaranteed to not change except for new major versions. The *scale* and sign of the value may be significant. If a previously used **seed** value is assigned to **seed** and used again, the pseudo-random number generator is guaranteed to produce the same sequence of pseudo-random numbers as it did when the **seed** value was previously used. The exact value assigned to **seed** is not guaranteed to be returned if **seed** is queried again immediately. However, if **seed** *does* return a different value, both values, when assigned to **seed**, are guaranteed to produce the same sequence of pseudo-random numbers. This means that certain values assigned to **seed** will *not* produce unique sequences of pseudo-random numbers. The value of **seed** will change after any use of the **rand()** and **irand(E)** operands (see the *Operands* subsection below), except if the parameter passed to **irand(E)** is **0**, **1**, or negative. There is no limit to the length (number of significant decimal digits) or *scale* of the value that can be assigned to **seed**. Variables and arrays do not interfere; users can have arrays named the same as variables. This also applies to functions (see the **FUNCTIONS** section), so a user can have a variable, array, and function that all have the same name, and they will not shadow each other, whether inside of functions or not. Named expressions are required as the operand of **increment**/**decrement** operators and as the left side of **assignment** operators (see the *Operators* subsection). ## Operands The following are valid operands in bc(1): 1. Numbers (see the *Numbers* subsection below). 2. Array indices (**I[E]**). 3. **(E)**: The value of **E** (used to change precedence). 4. **sqrt(E)**: The square root of **E**. **E** must be non-negative. 5. **length(E)**: The number of significant decimal digits in **E**. Returns **1** for **0** with no decimal places. If given a string, the length of the string is returned. Passing a string to **length(E)** is a **non-portable extension**. 6. **length(I[])**: The number of elements in the array **I**. This is a **non-portable extension**. 7. **scale(E)**: The *scale* of **E**. 8. **abs(E)**: The absolute value of **E**. This is a **non-portable extension**. 9. **is_number(E)**: **1** if the given argument is a number, **0** if it is a string. This is a **non-portable extension**. 10. **is_string(E)**: **1** if the given argument is a string, **0** if it is a number. This is a **non-portable extension**. 11. **modexp(E, E, E)**: Modular exponentiation, where the first expression is the base, the second is the exponent, and the third is the modulus. All three values must be integers. The second argument must be non-negative. The third argument must be non-zero. This is a **non-portable extension**. 11. **divmod(E, E, I[])**: Division and modulus in one operation. This is for optimization. The first expression is the dividend, and the second is the divisor, which must be non-zero. The return value is the quotient, and the modulus is stored in index **0** of the provided array (the last argument). This is a **non-portable extension**. 12. **asciify(E)**: If **E** is a string, returns a string that is the first letter of its argument. If it is a number, calculates the number mod **256** and returns that number as a one-character string. This is a **non-portable extension**. 13. **asciify(I[])**: A string that is made up of the characters that would result from running **asciify(E)** on each element of the array identified by the argument. This allows creating multi-character strings and storing them. This is a **non-portable extension**. 14. **I()**, **I(E)**, **I(E, E)**, and so on, where **I** is an identifier for a non-**void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). The **E** argument(s) may also be arrays of the form **I[]**, which will automatically be turned into array references (see the *Array References* subsection of the **FUNCTIONS** section) if the corresponding parameter in the function definition is an array reference. 15. **read()**: Reads a line from **stdin** and uses that as an expression. The result of that expression is the result of the **read()** operand. This is a **non-portable extension**. 16. **maxibase()**: The max allowable **ibase**. This is a **non-portable extension**. 17. **maxobase()**: The max allowable **obase**. This is a **non-portable extension**. 18. **maxscale()**: The max allowable **scale**. This is a **non-portable extension**. 19. **line_length()**: The line length set with **BC_LINE_LENGTH** (see the **ENVIRONMENT VARIABLES** section). This is a **non-portable extension**. 20. **global_stacks()**: **0** if global stacks are not enabled with the **-g** or **-\-global-stacks** options, non-zero otherwise. See the **OPTIONS** section. This is a **non-portable extension**. 21. **leading_zero()**: **0** if leading zeroes are not enabled with the **-z** or **--leading-zeroes** options, non-zero otherwise. See the **OPTIONS** section. This is a **non-portable extension**. 22. **rand()**: A pseudo-random integer between **0** (inclusive) and **BC_RAND_MAX** (inclusive). Using this operand will change the value of **seed**. This is a **non-portable extension**. 23. **irand(E)**: A pseudo-random integer between **0** (inclusive) and the value of **E** (exclusive). If **E** is negative or is a non-integer (**E**'s *scale* is not **0**), an error is raised, and bc(1) resets (see the **RESET** section) while **seed** remains unchanged. If **E** is larger than **BC_RAND_MAX**, the higher bound is honored by generating several pseudo-random integers, multiplying them by appropriate powers of **BC_RAND_MAX+1**, and adding them together. Thus, the size of integer that can be generated with this operand is unbounded. Using this operand will change the value of **seed**, unless the value of **E** is **0** or **1**. In that case, **0** is returned, and **seed** is *not* changed. This is a **non-portable extension**. 24. **maxrand()**: The max integer returned by **rand()**. This is a **non-portable extension**. The integers generated by **rand()** and **irand(E)** are guaranteed to be as unbiased as possible, subject to the limitations of the pseudo-random number generator. **Note**: The values returned by the pseudo-random number generator with **rand()** and **irand(E)** are guaranteed to *NOT* be cryptographically secure. This is a consequence of using a seeded pseudo-random number generator. However, they *are* guaranteed to be reproducible with identical **seed** values. This means that the pseudo-random values from bc(1) should only be used where a reproducible stream of pseudo-random numbers is *ESSENTIAL*. In any other case, use a non-seeded pseudo-random number generator. ## Numbers Numbers are strings made up of digits, uppercase letters, and at most **1** period for a radix. Numbers can have up to **BC_NUM_MAX** digits. Uppercase letters are equal to **9** plus their position in the alphabet, starting from **1** (i.e., **A** equals **10**, or **9+1**). If a digit or letter makes no sense with the current value of **ibase** (i.e., they are greater than or equal to the current value of **ibase**), then the behavior depends on the existence of the **-c**/**-\-digit-clamp** or **-C**/**-\-no-digit-clamp** options (see the **OPTIONS** section), the existence and setting of the **BC_DIGIT_CLAMP** environment variable (see the **ENVIRONMENT VARIABLES** section), or the default, which can be queried with the **-h**/**-\-help** option. If clamping is off, then digits or letters that are greater than or equal to the current value of **ibase** are not changed. Instead, their given value is multiplied by the appropriate power of **ibase** and added into the number. This means that, with an **ibase** of **3**, the number **AB** is equal to **3\^1\*A+3\^0\*B**, which is **3** times **10** plus **11**, or **41**. If clamping is on, then digits or letters that are greater than or equal to the current value of **ibase** are set to the value of the highest valid digit in **ibase** before being multiplied by the appropriate power of **ibase** and added into the number. This means that, with an **ibase** of **3**, the number **AB** is equal to **3\^1\*2+3\^0\*2**, which is **3** times **2** plus **2**, or **8**. There is one exception to clamping: single-character numbers (i.e., **A** alone). Such numbers are never clamped and always take the value they would have in the highest possible **ibase**. This means that **A** alone always equals decimal **10** and **Z** alone always equals decimal **35**. This behavior is mandated by the standard (see the STANDARDS section) and is meant to provide an easy way to set the current **ibase** (with the **i** command) regardless of the current value of **ibase**. If clamping is on, and the clamped value of a character is needed, use a leading zero, i.e., for **A**, use **0A**. In addition, bc(1) accepts numbers in scientific notation. These have the form **\e\**. The exponent (the portion after the **e**) must be an integer. An example is **1.89237e9**, which is equal to **1892370000**. Negative exponents are also allowed, so **4.2890e-3** is equal to **0.0042890**. Using scientific notation is an error or warning if the **-s** or **-w**, respectively, command-line options (or equivalents) are given. **WARNING**: Both the number and the exponent in scientific notation are interpreted according to the current **ibase**, but the number is still multiplied by **10\^exponent** regardless of the current **ibase**. For example, if **ibase** is **16** and bc(1) is given the number string **FFeA**, the resulting decimal number will be **2550000000000**, and if bc(1) is given the number string **10e-4**, the resulting decimal number will be **0.0016**. Accepting input as scientific notation is a **non-portable extension**. ## Operators The following arithmetic and logical operators can be used. They are listed in order of decreasing precedence. Operators in the same group have the same precedence. **++** **-\-** : Type: Prefix and Postfix Associativity: None Description: **increment**, **decrement** **-** **!** : Type: Prefix Associativity: None Description: **negation**, **boolean not** **\$** : Type: Postfix Associativity: None Description: **truncation** **\@** : Type: Binary Associativity: Right Description: **set precision** **\^** : Type: Binary Associativity: Right Description: **power** **\*** **/** **%** : Type: Binary Associativity: Left Description: **multiply**, **divide**, **modulus** **+** **-** : Type: Binary Associativity: Left Description: **add**, **subtract** **\<\<** **\>\>** : Type: Binary Associativity: Left Description: **shift left**, **shift right** **=** **\<\<=** **\>\>=** **+=** **-=** **\*=** **/=** **%=** **\^=** **\@=** : Type: Binary Associativity: Right Description: **assignment** **==** **\<=** **\>=** **!=** **\<** **\>** : Type: Binary Associativity: Left Description: **relational** **&&** : Type: Binary Associativity: Left Description: **boolean and** **||** : Type: Binary Associativity: Left Description: **boolean or** The operators will be described in more detail below. **++** **-\-** : The prefix and postfix **increment** and **decrement** operators behave exactly like they would in C. They require a named expression (see the *Named Expressions* subsection) as an operand. The prefix versions of these operators are more efficient; use them where possible. **-** : The **negation** operator returns **0** if a user attempts to negate any expression with the value **0**. Otherwise, a copy of the expression with its sign flipped is returned. **!** : The **boolean not** operator returns **1** if the expression is **0**, or **0** otherwise. This is a **non-portable extension**. **\$** : The **truncation** operator returns a copy of the given expression with all of its *scale* removed. This is a **non-portable extension**. **\@** : The **set precision** operator takes two expressions and returns a copy of the first with its *scale* equal to the value of the second expression. That could either mean that the number is returned without change (if the *scale* of the first expression matches the value of the second expression), extended (if it is less), or truncated (if it is more). The second expression must be an integer (no *scale*) and non-negative. This is a **non-portable extension**. **\^** : The **power** operator (not the **exclusive or** operator, as it would be in C) takes two expressions and raises the first to the power of the value of the second. The *scale* of the result is equal to **scale**. The second expression must be an integer (no *scale*), and if it is negative, the first value must be non-zero. **\*** : The **multiply** operator takes two expressions, multiplies them, and returns the product. If **a** is the *scale* of the first expression and **b** is the *scale* of the second expression, the *scale* of the result is equal to **min(a+b,max(scale,a,b))** where **min()** and **max()** return the obvious values. **/** : The **divide** operator takes two expressions, divides them, and returns the quotient. The *scale* of the result shall be the value of **scale**. The second expression must be non-zero. **%** : The **modulus** operator takes two expressions, **a** and **b**, and evaluates them by 1) Computing **a/b** to current **scale** and 2) Using the result of step 1 to calculate **a-(a/b)\*b** to *scale* **max(scale+scale(b),scale(a))**. The second expression must be non-zero. **+** : The **add** operator takes two expressions, **a** and **b**, and returns the sum, with a *scale* equal to the max of the *scale*s of **a** and **b**. **-** : The **subtract** operator takes two expressions, **a** and **b**, and returns the difference, with a *scale* equal to the max of the *scale*s of **a** and **b**. **\<\<** : The **left shift** operator takes two expressions, **a** and **b**, and returns a copy of the value of **a** with its decimal point moved **b** places to the right. The second expression must be an integer (no *scale*) and non-negative. This is a **non-portable extension**. **\>\>** : The **right shift** operator takes two expressions, **a** and **b**, and returns a copy of the value of **a** with its decimal point moved **b** places to the left. The second expression must be an integer (no *scale*) and non-negative. This is a **non-portable extension**. **=** **\<\<=** **\>\>=** **+=** **-=** **\*=** **/=** **%=** **\^=** **\@=** : The **assignment** operators take two expressions, **a** and **b** where **a** is a named expression (see the *Named Expressions* subsection). For **=**, **b** is copied and the result is assigned to **a**. For all others, **a** and **b** are applied as operands to the corresponding arithmetic operator and the result is assigned to **a**. The **assignment** operators that correspond to operators that are extensions are themselves **non-portable extensions**. **==** **\<=** **\>=** **!=** **\<** **\>** : The **relational** operators compare two expressions, **a** and **b**, and if the relation holds, according to C language semantics, the result is **1**. Otherwise, it is **0**. Note that unlike in C, these operators have a lower precedence than the **assignment** operators, which means that **a=b\>c** is interpreted as **(a=b)\>c**. Also, unlike the standard (see the **STANDARDS** section) requires, these operators can appear anywhere any other expressions can be used. This allowance is a **non-portable extension**. **&&** : The **boolean and** operator takes two expressions and returns **1** if both expressions are non-zero, **0** otherwise. This is *not* a short-circuit operator. This is a **non-portable extension**. **||** : The **boolean or** operator takes two expressions and returns **1** if one of the expressions is non-zero, **0** otherwise. This is *not* a short-circuit operator. This is a **non-portable extension**. ## Statements The following items are statements: 1. **E** 2. **{** **S** **;** ... **;** **S** **}** 3. **if** **(** **E** **)** **S** 4. **if** **(** **E** **)** **S** **else** **S** 5. **while** **(** **E** **)** **S** 6. **for** **(** **E** **;** **E** **;** **E** **)** **S** 7. An empty statement 8. **break** 9. **continue** 10. **quit** 11. **halt** 12. **limits** 13. A string of characters, enclosed in double quotes 14. **print** **E** **,** ... **,** **E** 15. **stream** **E** **,** ... **,** **E** 16. **I()**, **I(E)**, **I(E, E)**, and so on, where **I** is an identifier for a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). The **E** argument(s) may also be arrays of the form **I[]**, which will automatically be turned into array references (see the *Array References* subsection of the **FUNCTIONS** section) if the corresponding parameter in the function definition is an array reference. Numbers 4, 9, 11, 12, 14, 15, and 16 are **non-portable extensions**. Also, as a **non-portable extension**, any or all of the expressions in the header of a for loop may be omitted. If the condition (second expression) is omitted, it is assumed to be a constant **1**. The **break** statement causes a loop to stop iterating and resume execution immediately following a loop. This is only allowed in loops. The **continue** statement causes a loop iteration to stop early and returns to the start of the loop, including testing the loop condition. This is only allowed in loops. The **if** **else** statement does the same thing as in C. The **quit** statement causes bc(1) to quit, even if it is on a branch that will not be executed (it is a compile-time command). **Warning**: The behavior of this bc(1) on **quit** is slightly different from other bc(1) implementations. Other bc(1) implementations will exit as soon as they finish parsing the line that a **quit** command is on. This bc(1) will execute any completed and executable statements that occur before the **quit** statement before exiting. In other words, for the bc(1) code below: for (i = 0; i < 3; ++i) i; quit Other bc(1) implementations will print nothing, and this bc(1) will print **0**, **1**, and **2** on successive lines before exiting. The **halt** statement causes bc(1) to quit, if it is executed. (Unlike **quit** if it is on a branch of an **if** statement that is not executed, bc(1) does not quit.) The **limits** statement prints the limits that this bc(1) is subject to. This is like the **quit** statement in that it is a compile-time command. An expression by itself is evaluated and printed, followed by a newline. Both scientific notation and engineering notation are available for printing the results of expressions. Scientific notation is activated by assigning **0** to **obase**, and engineering notation is activated by assigning **1** to **obase**. To deactivate them, just assign a different value to **obase**. Scientific notation and engineering notation are disabled if bc(1) is run with either the **-s** or **-w** command-line options (or equivalents). Printing numbers in scientific notation and/or engineering notation is a **non-portable extension**. ## Strings If strings appear as a statement by themselves, they are printed without a trailing newline. In addition to appearing as a lone statement by themselves, strings can be assigned to variables and array elements. They can also be passed to functions in variable parameters. If any statement that expects a string is given a variable that had a string assigned to it, the statement acts as though it had received a string. If any math operation is attempted on a string or a variable or array element that has been assigned a string, an error is raised, and bc(1) resets (see the **RESET** section). Assigning strings to variables and array elements and passing them to functions are **non-portable extensions**. ## Print Statement The "expressions" in a **print** statement may also be strings. If they are, there are backslash escape sequences that are interpreted specially. What those sequences are, and what they cause to be printed, are shown below: **\\a**: **\\a** **\\b**: **\\b** **\\\\**: **\\** **\\e**: **\\** **\\f**: **\\f** **\\n**: **\\n** **\\q**: **"** **\\r**: **\\r** **\\t**: **\\t** Any other character following a backslash causes the backslash and character to be printed as-is. Any non-string expression in a print statement shall be assigned to **last**, like any other expression that is printed. ## Stream Statement The expressions in a **stream** statement may also be strings. If a **stream** statement is given a string, it prints the string as though the string had appeared as its own statement. In other words, the **stream** statement prints strings normally, without a newline. If a **stream** statement is given a number, a copy of it is truncated and its absolute value is calculated. The result is then printed as though **obase** is **256** and each digit is interpreted as an 8-bit ASCII character, making it a byte stream. ## Order of Evaluation All expressions in a statment are evaluated left to right, except as necessary to maintain order of operations. This means, for example, assuming that **i** is equal to **0**, in the expression a[i++] = i++ the first (or 0th) element of **a** is set to **1**, and **i** is equal to **2** at the end of the expression. This includes function arguments. Thus, assuming **i** is equal to **0**, this means that in the expression x(i++, i++) the first argument passed to **x()** is **0**, and the second argument is **1**, while **i** is equal to **2** before the function starts executing. # FUNCTIONS Function definitions are as follows: ``` define I(I,...,I){ auto I,...,I S;...;S return(E) } ``` Any **I** in the parameter list or **auto** list may be replaced with **I[]** to make a parameter or **auto** var an array, and any **I** in the parameter list may be replaced with **\*I[]** to make a parameter an array reference. Callers of functions that take array references should not put an asterisk in the call; they must be called with just **I[]** like normal array parameters and will be automatically converted into references. As a **non-portable extension**, the opening brace of a **define** statement may appear on the next line. As a **non-portable extension**, the return statement may also be in one of the following forms: 1. **return** 2. **return** **(** **)** 3. **return** **E** The first two, or not specifying a **return** statement, is equivalent to **return (0)**, unless the function is a **void** function (see the *Void Functions* subsection below). ## Void Functions Functions can also be **void** functions, defined as follows: ``` define void I(I,...,I){ auto I,...,I S;...;S return } ``` They can only be used as standalone expressions, where such an expression would be printed alone, except in a print statement. Void functions can only use the first two **return** statements listed above. They can also omit the return statement entirely. The word "void" is not treated as a keyword; it is still possible to have variables, arrays, and functions named **void**. The word "void" is only treated specially right after the **define** keyword. This is a **non-portable extension**. ## Array References For any array in the parameter list, if the array is declared in the form ``` *I[] ``` it is a **reference**. Any changes to the array in the function are reflected, when the function returns, to the array that was passed in. Other than this, all function arguments are passed by value. This is a **non-portable extension**. # LIBRARY All of the functions below, including the functions in the extended math library (see the *Extended Library* subsection below), are available when the **-l** or **-\-mathlib** command-line flags are given, except that the extended math library is not available when the **-s** option, the **-w** option, or equivalents are given. ## Standard Library The standard (see the **STANDARDS** section) defines the following functions for the math library: **s(x)** : Returns the sine of **x**, which is assumed to be in radians. This is a transcendental function (see the *Transcendental Functions* subsection below). **c(x)** : Returns the cosine of **x**, which is assumed to be in radians. This is a transcendental function (see the *Transcendental Functions* subsection below). **a(x)** : Returns the arctangent of **x**, in radians. This is a transcendental function (see the *Transcendental Functions* subsection below). **l(x)** : Returns the natural logarithm of **x**. This is a transcendental function (see the *Transcendental Functions* subsection below). **e(x)** : Returns the mathematical constant **e** raised to the power of **x**. This is a transcendental function (see the *Transcendental Functions* subsection below). **j(x, n)** : Returns the bessel integer order **n** (truncated) of **x**. This is a transcendental function (see the *Transcendental Functions* subsection below). ## Extended Library The extended library is *not* loaded when the **-s**/**-\-standard** or **-w**/**-\-warn** options are given since they are not part of the library defined by the standard (see the **STANDARDS** section). The extended library is a **non-portable extension**. **p(x, y)** : Calculates **x** to the power of **y**, even if **y** is not an integer, and returns the result to the current **scale**. It is an error if **y** is negative and **x** is **0**. This is a transcendental function (see the *Transcendental Functions* subsection below). **r(x, p)** : Returns **x** rounded to **p** decimal places according to the rounding mode round half away from **0** (https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero). **ceil(x, p)** : Returns **x** rounded to **p** decimal places according to the rounding mode round away from **0** (https://en.wikipedia.org/wiki/Rounding#Rounding_away_from_zero). **f(x)** : Returns the factorial of the truncated absolute value of **x**. **max(a, b)** : Returns **a** if **a** is greater than **b**; otherwise, returns **b**. **min(a, b)** : Returns **a** if **a** is less than **b**; otherwise, returns **b**. **perm(n, k)** : Returns the permutation of the truncated absolute value of **n** of the truncated absolute value of **k**, if **k \<= n**. If not, it returns **0**. **comb(n, k)** : Returns the combination of the truncated absolute value of **n** of the truncated absolute value of **k**, if **k \<= n**. If not, it returns **0**. **fib(n)** : Returns the Fibonacci number of the truncated absolute value of **n**. **l2(x)** : Returns the logarithm base **2** of **x**. This is a transcendental function (see the *Transcendental Functions* subsection below). **l10(x)** : Returns the logarithm base **10** of **x**. This is a transcendental function (see the *Transcendental Functions* subsection below). **log(x, b)** : Returns the logarithm base **b** of **x**. This is a transcendental function (see the *Transcendental Functions* subsection below). **cbrt(x)** : Returns the cube root of **x**. **root(x, n)** : Calculates the truncated value of **n**, **r**, and returns the **r**th root of **x** to the current **scale**. If **r** is **0** or negative, this raises an error and causes bc(1) to reset (see the **RESET** section). It also raises an error and causes bc(1) to reset if **r** is even and **x** is negative. **gcd(a, b)** : Returns the greatest common divisor (factor) of the truncated absolute value of **a** and the truncated absolute value of **b**. **lcm(a, b)** : Returns the least common multiple of the truncated absolute value of **a** and the truncated absolute value of **b**. **pi(p)** : Returns **pi** to **p** decimal places. This is a transcendental function (see the *Transcendental Functions* subsection below). **t(x)** : Returns the tangent of **x**, which is assumed to be in radians. This is a transcendental function (see the *Transcendental Functions* subsection below). **a2(y, x)** : Returns the arctangent of **y/x**, in radians. If both **y** and **x** are equal to **0**, it raises an error and causes bc(1) to reset (see the **RESET** section). Otherwise, if **x** is greater than **0**, it returns **a(y/x)**. If **x** is less than **0**, and **y** is greater than or equal to **0**, it returns **a(y/x)+pi**. If **x** is less than **0**, and **y** is less than **0**, it returns **a(y/x)-pi**. If **x** is equal to **0**, and **y** is greater than **0**, it returns **pi/2**. If **x** is equal to **0**, and **y** is less than **0**, it returns **-pi/2**. This function is the same as the **atan2()** function in many programming languages. This is a transcendental function (see the *Transcendental Functions* subsection below). **sin(x)** : Returns the sine of **x**, which is assumed to be in radians. This is an alias of **s(x)**. This is a transcendental function (see the *Transcendental Functions* subsection below). **cos(x)** : Returns the cosine of **x**, which is assumed to be in radians. This is an alias of **c(x)**. This is a transcendental function (see the *Transcendental Functions* subsection below). **tan(x)** : Returns the tangent of **x**, which is assumed to be in radians. If **x** is equal to **1** or **-1**, this raises an error and causes bc(1) to reset (see the **RESET** section). This is an alias of **t(x)**. This is a transcendental function (see the *Transcendental Functions* subsection below). **atan(x)** : Returns the arctangent of **x**, in radians. This is an alias of **a(x)**. This is a transcendental function (see the *Transcendental Functions* subsection below). **atan2(y, x)** : Returns the arctangent of **y/x**, in radians. If both **y** and **x** are equal to **0**, it raises an error and causes bc(1) to reset (see the **RESET** section). Otherwise, if **x** is greater than **0**, it returns **a(y/x)**. If **x** is less than **0**, and **y** is greater than or equal to **0**, it returns **a(y/x)+pi**. If **x** is less than **0**, and **y** is less than **0**, it returns **a(y/x)-pi**. If **x** is equal to **0**, and **y** is greater than **0**, it returns **pi/2**. If **x** is equal to **0**, and **y** is less than **0**, it returns **-pi/2**. This function is the same as the **atan2()** function in many programming languages. This is an alias of **a2(y, x)**. This is a transcendental function (see the *Transcendental Functions* subsection below). **r2d(x)** : Converts **x** from radians to degrees and returns the result. This is a transcendental function (see the *Transcendental Functions* subsection below). **d2r(x)** : Converts **x** from degrees to radians and returns the result. This is a transcendental function (see the *Transcendental Functions* subsection below). **frand(p)** : Generates a pseudo-random integer between **0** (inclusive) and **1** (exclusive) with the number of decimal digits after the decimal point equal to the truncated absolute value of **p**. If **p** is not **0**, then calling this function will change the value of **seed**. If **p** is **0**, then **0** is returned, and **seed** is *not* changed. **ifrand(i, p)** : Generates a pseudo-random integer that is between **0** (inclusive) and the truncated absolute value of **i** (exclusive) with the number of decimal digits after the decimal point equal to the truncated absolute value of **p**. If the absolute value of **i** is greater than or equal to **2**, and **p** is not **0**, then calling this function will change the value of **seed**; otherwise, **0** is returned, and **seed** is not changed. **i2rand(a, b)** : Takes the truncated value of **a** and **b** and uses them as inclusive bounds to enerate a pseudo-random integer. If the difference of the truncated values of **a** and **b** is **0**, then the truncated value is returned, and **seed** is *not* changed. Otherwise, this function will change the value of **seed**. **srand(x)** : Returns **x** with its sign flipped with probability **0.5**. In other words, it randomizes the sign of **x**. **brand()** : Returns a random boolean value (either **0** or **1**). **band(a, b)** : Takes the truncated absolute value of both **a** and **b** and calculates and returns the result of the bitwise **and** operation between them. If you want to use signed two's complement arguments, use **s2u(x)** to convert. **bor(a, b)** : Takes the truncated absolute value of both **a** and **b** and calculates and returns the result of the bitwise **or** operation between them. If you want to use signed two's complement arguments, use **s2u(x)** to convert. **bxor(a, b)** : Takes the truncated absolute value of both **a** and **b** and calculates and returns the result of the bitwise **xor** operation between them. If you want to use signed two's complement arguments, use **s2u(x)** to convert. **bshl(a, b)** : Takes the truncated absolute value of both **a** and **b** and calculates and returns the result of **a** bit-shifted left by **b** places. If you want to use signed two's complement arguments, use **s2u(x)** to convert. **bshr(a, b)** : Takes the truncated absolute value of both **a** and **b** and calculates and returns the truncated result of **a** bit-shifted right by **b** places. If you want to use signed two's complement arguments, use **s2u(x)** to convert. **bnotn(x, n)** : Takes the truncated absolute value of **x** and does a bitwise not as though it has the same number of bytes as the truncated absolute value of **n**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bnot8(x)** : Does a bitwise not of the truncated absolute value of **x** as though it has - **8** binary digits (1 unsigned byte). + **8** binary digits (**1** unsigned byte). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bnot16(x)** : Does a bitwise not of the truncated absolute value of **x** as though it has - **16** binary digits (2 unsigned bytes). + **16** binary digits (**2** unsigned bytes). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bnot32(x)** : Does a bitwise not of the truncated absolute value of **x** as though it has - **32** binary digits (4 unsigned bytes). + **32** binary digits (**4** unsigned bytes). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bnot64(x)** : Does a bitwise not of the truncated absolute value of **x** as though it has - **64** binary digits (8 unsigned bytes). + **64** binary digits (**8** unsigned bytes). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bnot(x)** : Does a bitwise not of the truncated absolute value of **x** as though it has the minimum number of power of two unsigned bytes. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brevn(x, n)** : Runs a bit reversal on the truncated absolute value of **x** as though it has the same number of 8-bit bytes as the truncated absolute value of **n**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brev8(x)** : Runs a bit reversal on the truncated absolute value of **x** as though it - has 8 binary digits (1 unsigned byte). + has 8 binary digits (**1** unsigned byte). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brev16(x)** : Runs a bit reversal on the truncated absolute value of **x** as though it - has 16 binary digits (2 unsigned bytes). + has 16 binary digits (**2** unsigned bytes). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brev32(x)** : Runs a bit reversal on the truncated absolute value of **x** as though it - has 32 binary digits (4 unsigned bytes). + has 32 binary digits (**4** unsigned bytes). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brev64(x)** : Runs a bit reversal on the truncated absolute value of **x** as though it - has 64 binary digits (8 unsigned bytes). + has 64 binary digits (**8** unsigned bytes). If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brev(x)** : Runs a bit reversal on the truncated absolute value of **x** as though it has the minimum number of power of two unsigned bytes. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **broln(x, p, n)** : Does a left bitwise rotatation of the truncated absolute value of **x**, as though it has the same number of unsigned 8-bit bytes as the truncated absolute value of **n**, by the number of places equal to the truncated absolute value of **p** modded by the **2** to the power of the number of binary digits in **n** 8-bit bytes. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brol8(x, p)** : Does a left bitwise rotatation of the truncated absolute value of **x**, as though it has **8** binary digits (**1** unsigned byte), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **8**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brol16(x, p)** : Does a left bitwise rotatation of the truncated absolute value of **x**, as though it has **16** binary digits (**2** unsigned bytes), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **16**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brol32(x, p)** : Does a left bitwise rotatation of the truncated absolute value of **x**, as - though it has **32** binary digits (**2** unsigned bytes), by the number of + though it has **32** binary digits (**4** unsigned bytes), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **32**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brol64(x, p)** : Does a left bitwise rotatation of the truncated absolute value of **x**, as - though it has **64** binary digits (**2** unsigned bytes), by the number of + though it has **64** binary digits (**8** unsigned bytes), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **64**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brol(x, p)** : Does a left bitwise rotatation of the truncated absolute value of **x**, as though it has the minimum number of power of two unsigned 8-bit bytes, by the number of places equal to the truncated absolute value of **p** modded by 2 to the power of the number of binary digits in the minimum number of 8-bit bytes. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **brorn(x, p, n)** : Does a right bitwise rotatation of the truncated absolute value of **x**, as though it has the same number of unsigned 8-bit bytes as the truncated absolute value of **n**, by the number of places equal to the truncated absolute value of **p** modded by the **2** to the power of the number of binary digits in **n** 8-bit bytes. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bror8(x, p)** : Does a right bitwise rotatation of the truncated absolute value of **x**, as though it has **8** binary digits (**1** unsigned byte), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **8**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bror16(x, p)** : Does a right bitwise rotatation of the truncated absolute value of **x**, as though it has **16** binary digits (**2** unsigned bytes), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **16**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bror32(x, p)** : Does a right bitwise rotatation of the truncated absolute value of **x**, as though it has **32** binary digits (**2** unsigned bytes), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **32**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bror64(x, p)** : Does a right bitwise rotatation of the truncated absolute value of **x**, as though it has **64** binary digits (**2** unsigned bytes), by the number of places equal to the truncated absolute value of **p** modded by **2** to the power of **64**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bror(x, p)** : Does a right bitwise rotatation of the truncated absolute value of **x**, as though it has the minimum number of power of two unsigned 8-bit bytes, by the number of places equal to the truncated absolute value of **p** modded by 2 to the power of the number of binary digits in the minimum number of 8-bit bytes. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bmodn(x, n)** : Returns the modulus of the truncated absolute value of **x** by **2** to the power of the multiplication of the truncated absolute value of **n** and **8**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bmod8(x, n)** : Returns the modulus of the truncated absolute value of **x** by **2** to the power of **8**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bmod16(x, n)** : Returns the modulus of the truncated absolute value of **x** by **2** to the power of **16**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bmod32(x, n)** : Returns the modulus of the truncated absolute value of **x** by **2** to the power of **32**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bmod64(x, n)** : Returns the modulus of the truncated absolute value of **x** by **2** to the power of **64**. If you want to a use signed two's complement argument, use **s2u(x)** to convert. **bunrev(t)** : Assumes **t** is a bitwise-reversed number with an extra set bit one place more significant than the real most significant bit (which was the least significant bit in the original number). This number is reversed and returned without the extra set bit. This function is used to implement other bitwise functions; it is not meant to be used by users, but it can be. **plz(x)** : If **x** is not equal to **0** and greater that **-1** and less than **1**, it is printed with a leading zero, regardless of the use of the **-z** option (see the **OPTIONS** section) and without a trailing newline. Otherwise, **x** is printed normally, without a trailing newline. **plznl(x)** : If **x** is not equal to **0** and greater that **-1** and less than **1**, it is printed with a leading zero, regardless of the use of the **-z** option (see the **OPTIONS** section) and with a trailing newline. Otherwise, **x** is printed normally, with a trailing newline. **pnlz(x)** : If **x** is not equal to **0** and greater that **-1** and less than **1**, it is printed without a leading zero, regardless of the use of the **-z** option (see the **OPTIONS** section) and without a trailing newline. Otherwise, **x** is printed normally, without a trailing newline. **pnlznl(x)** : If **x** is not equal to **0** and greater that **-1** and less than **1**, it is printed without a leading zero, regardless of the use of the **-z** option (see the **OPTIONS** section) and with a trailing newline. Otherwise, **x** is printed normally, with a trailing newline. **ubytes(x)** : Returns the numbers of unsigned integer bytes required to hold the truncated absolute value of **x**. **sbytes(x)** : Returns the numbers of signed, two's-complement integer bytes required to hold the truncated value of **x**. **s2u(x)** : Returns **x** if it is non-negative. If it *is* negative, then it calculates what **x** would be as a 2's-complement signed integer and returns the non-negative integer that would have the same representation in binary. **s2un(x,n)** : Returns **x** if it is non-negative. If it *is* negative, then it calculates what **x** would be as a 2's-complement signed integer with **n** bytes and returns the non-negative integer that would have the same representation in binary. If **x** cannot fit into **n** 2's-complement signed bytes, it is truncated to fit. **hex(x)** : Outputs the hexadecimal (base **16**) representation of **x**. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **binary(x)** : Outputs the binary (base **2**) representation of **x**. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **output(x, b)** : Outputs the base **b** representation of **x**. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **uint(x)** : Outputs the representation, in binary and hexadecimal, of **x** as an unsigned integer in as few power of two bytes as possible. Both outputs are split into bytes separated by spaces. If **x** is not an integer or is negative, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **int(x)** : Outputs the representation, in binary and hexadecimal, of **x** as a signed, two's-complement integer in as few power of two bytes as possible. Both outputs are split into bytes separated by spaces. If **x** is not an integer, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **uintn(x, n)** : Outputs the representation, in binary and hexadecimal, of **x** as an unsigned integer in **n** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer, is negative, or cannot fit into **n** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **intn(x, n)** : Outputs the representation, in binary and hexadecimal, of **x** as a signed, two's-complement integer in **n** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer or cannot fit into **n** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **uint8(x)** : Outputs the representation, in binary and hexadecimal, of **x** as an unsigned integer in **1** byte. Both outputs are split into bytes separated by spaces. If **x** is not an integer, is negative, or cannot fit into **1** byte, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **int8(x)** : Outputs the representation, in binary and hexadecimal, of **x** as a signed, two's-complement integer in **1** byte. Both outputs are split into bytes separated by spaces. If **x** is not an integer or cannot fit into **1** byte, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **uint16(x)** : Outputs the representation, in binary and hexadecimal, of **x** as an unsigned integer in **2** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer, is negative, or cannot fit into **2** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **int16(x)** : Outputs the representation, in binary and hexadecimal, of **x** as a signed, two's-complement integer in **2** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer or cannot fit into **2** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **uint32(x)** : Outputs the representation, in binary and hexadecimal, of **x** as an unsigned integer in **4** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer, is negative, or cannot fit into **4** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **int32(x)** : Outputs the representation, in binary and hexadecimal, of **x** as a signed, two's-complement integer in **4** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer or cannot fit into **4** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **uint64(x)** : Outputs the representation, in binary and hexadecimal, of **x** as an unsigned integer in **8** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer, is negative, or cannot fit into **8** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **int64(x)** : Outputs the representation, in binary and hexadecimal, of **x** as a signed, two's-complement integer in **8** bytes. Both outputs are split into bytes separated by spaces. If **x** is not an integer or cannot fit into **8** bytes, an error message is printed instead, but bc(1) is not reset (see the **RESET** section). This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **hex_uint(x, n)** : Outputs the representation of the truncated absolute value of **x** as an unsigned integer in hexadecimal using **n** bytes. Not all of the value will be output if **n** is too small. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **binary_uint(x, n)** : Outputs the representation of the truncated absolute value of **x** as an unsigned integer in binary using **n** bytes. Not all of the value will be output if **n** is too small. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **output_uint(x, n)** : Outputs the representation of the truncated absolute value of **x** as an unsigned integer in the current **obase** (see the **SYNTAX** section) using **n** bytes. Not all of the value will be output if **n** is too small. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). **output_byte(x, i)** : Outputs byte **i** of the truncated absolute value of **x**, where **0** is the least significant byte and **number_of_bytes - 1** is the most significant byte. This is a **void** function (see the *Void Functions* subsection of the **FUNCTIONS** section). ## Transcendental Functions All transcendental functions can return slightly inaccurate results, up to 1 ULP (https://en.wikipedia.org/wiki/Unit_in_the_last_place). This is unavoidable, and the article at https://people.eecs.berkeley.edu/~wkahan/LOG10HAF.TXT explains why it is impossible and unnecessary to calculate exact results for the transcendental functions. Because of the possible inaccuracy, I recommend that users call those functions with the precision (**scale**) set to at least 1 higher than is necessary. If exact results are *absolutely* required, users can double the precision (**scale**) and then truncate. The transcendental functions in the standard math library are: * **s(x)** * **c(x)** * **a(x)** * **l(x)** * **e(x)** * **j(x, n)** The transcendental functions in the extended math library are: * **l2(x)** * **l10(x)** * **log(x, b)** * **pi(p)** * **t(x)** * **a2(y, x)** * **sin(x)** * **cos(x)** * **tan(x)** * **atan(x)** * **atan2(y, x)** * **r2d(x)** * **d2r(x)** # RESET When bc(1) encounters an error or a signal that it has a non-default handler for, it resets. This means that several things happen. First, any functions that are executing are stopped and popped off the stack. The behavior is not unlike that of exceptions in programming languages. Then the execution point is set so that any code waiting to execute (after all functions returned) is skipped. Thus, when bc(1) resets, it skips any remaining code waiting to be executed. Then, if it is interactive mode, and the error was not a fatal error (see the **EXIT STATUS** section), it asks for more input; otherwise, it exits with the appropriate return code. Note that this reset behavior is different from the GNU bc(1), which attempts to start executing the statement right after the one that caused an error. # PERFORMANCE Most bc(1) implementations use **char** types to calculate the value of **1** decimal digit at a time, but that can be slow. This bc(1) does something different. It uses large integers to calculate more than **1** decimal digit at a time. If built in a environment where **BC_LONG_BIT** (see the **LIMITS** section) is **64**, then each integer has **9** decimal digits. If built in an environment where **BC_LONG_BIT** is **32** then each integer has **4** decimal digits. This value (the number of decimal digits per large integer) is called **BC_BASE_DIGS**. The actual values of **BC_LONG_BIT** and **BC_BASE_DIGS** can be queried with the **limits** statement. In addition, this bc(1) uses an even larger integer for overflow checking. This integer type depends on the value of **BC_LONG_BIT**, but is always at least twice as large as the integer type used to store digits. # LIMITS The following are the limits on bc(1): **BC_LONG_BIT** : The number of bits in the **long** type in the environment where bc(1) was built. This determines how many decimal digits can be stored in a single large integer (see the **PERFORMANCE** section). **BC_BASE_DIGS** : The number of decimal digits per large integer (see the **PERFORMANCE** section). Depends on **BC_LONG_BIT**. **BC_BASE_POW** : The max decimal number that each large integer can store (see **BC_BASE_DIGS**) plus **1**. Depends on **BC_BASE_DIGS**. **BC_OVERFLOW_MAX** : The max number that the overflow type (see the **PERFORMANCE** section) can hold. Depends on **BC_LONG_BIT**. **BC_BASE_MAX** : The maximum output base. Set at **BC_BASE_POW**. **BC_DIM_MAX** : The maximum size of arrays. Set at **SIZE_MAX-1**. **BC_SCALE_MAX** : The maximum **scale**. Set at **BC_OVERFLOW_MAX-1**. **BC_STRING_MAX** : The maximum length of strings. Set at **BC_OVERFLOW_MAX-1**. **BC_NAME_MAX** : The maximum length of identifiers. Set at **BC_OVERFLOW_MAX-1**. **BC_NUM_MAX** : The maximum length of a number (in decimal digits), which includes digits after the decimal point. Set at **BC_OVERFLOW_MAX-1**. **BC_RAND_MAX** : The maximum integer (inclusive) returned by the **rand()** operand. Set at **2\^BC_LONG_BIT-1**. Exponent : The maximum allowable exponent (positive or negative). Set at **BC_OVERFLOW_MAX**. Number of vars : The maximum number of vars/arrays. Set at **SIZE_MAX-1**. The actual values can be queried with the **limits** statement. These limits are meant to be effectively non-existent; the limits are so large (at least on 64-bit machines) that there should not be any point at which they become a problem. In fact, memory should be exhausted before these limits should be hit. # ENVIRONMENT VARIABLES As **non-portable extensions**, bc(1) recognizes the following environment variables: **POSIXLY_CORRECT** : If this variable exists (no matter the contents), bc(1) behaves as if the **-s** option was given. **BC_ENV_ARGS** : This is another way to give command-line arguments to bc(1). They should be in the same format as all other command-line arguments. These are always processed first, so any files given in **BC_ENV_ARGS** will be processed before arguments and files given on the command-line. This gives the user the ability to set up "standard" options and files to be used at every invocation. The most useful thing for such files to contain would be useful functions that the user might want every time bc(1) runs. The code that parses **BC_ENV_ARGS** will correctly handle quoted arguments, but it does not understand escape sequences. For example, the string **"/home/gavin/some bc file.bc"** will be correctly parsed, but the string **"/home/gavin/some \"bc\" file.bc"** will include the backslashes. The quote parsing will handle either kind of quotes, **'** or **"**. Thus, if you have a file with any number of single quotes in the name, you can use double quotes as the outside quotes, as in **"some 'bc' file.bc"**, and vice versa if you have a file with double quotes. However, handling a file with both kinds of quotes in **BC_ENV_ARGS** is not supported due to the complexity of the parsing, though such files are still supported on the command-line where the parsing is done by the shell. **BC_LINE_LENGTH** : If this environment variable exists and contains an integer that is greater than **1** and is less than **UINT16_MAX** (**2\^16-1**), bc(1) will output lines to that length, including the backslash (**\\**). The default line length is **70**. The special value of **0** will disable line length checking and print numbers without regard to line length and without backslashes and newlines. **BC_BANNER** : If this environment variable exists and contains an integer, then a non-zero value activates the copyright banner when bc(1) is in interactive mode, while zero deactivates it. If bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section), then this environment variable has no effect because bc(1) does not print the banner when not in interactive mode. This environment variable overrides the default, which can be queried with the **-h** or **-\-help** options. **BC_SIGINT_RESET** : If bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section), then this environment variable has no effect because bc(1) exits on **SIGINT** when not in interactive mode. However, when bc(1) is in interactive mode, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) reset on **SIGINT**, rather than exit, and zero makes bc(1) exit. If this environment variable exists and is *not* an integer, then bc(1) will exit on **SIGINT**. This environment variable overrides the default, which can be queried with the **-h** or **-\-help** options. **BC_TTY_MODE** : If TTY mode is *not* available (see the **TTY MODE** section), then this environment variable has no effect. However, when TTY mode is available, then if this environment variable exists and contains an integer, then a non-zero value makes bc(1) use TTY mode, and zero makes bc(1) not use TTY mode. This environment variable overrides the default, which can be queried with the **-h** or **-\-help** options. **BC_PROMPT** : If TTY mode is *not* available (see the **TTY MODE** section), then this environment variable has no effect. However, when TTY mode is available, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) use a prompt, and zero or a non-integer makes bc(1) not use a prompt. If this environment variable does not exist and **BC_TTY_MODE** does, then the value of the **BC_TTY_MODE** environment variable is used. This environment variable and the **BC_TTY_MODE** environment variable override the default, which can be queried with the **-h** or **-\-help** options. **BC_EXPR_EXIT** : If any expressions or expression files are given on the command-line with **-e**, **-\-expression**, **-f**, or **-\-file**, then if this environment variable exists and contains an integer, a non-zero value makes bc(1) exit after executing the expressions and expression files, and a zero value makes bc(1) not exit. This environment variable overrides the default, which can be queried with the **-h** or **-\-help** options. **BC_DIGIT_CLAMP** : When parsing numbers and if this environment variable exists and contains an integer, a non-zero value makes bc(1) clamp digits that are greater than or equal to the current **ibase** so that all such digits are considered equal to the **ibase** minus 1, and a zero value disables such clamping so that those digits are always equal to their value, which is multiplied by the power of the **ibase**. This never applies to single-digit numbers, as per the standard (see the **STANDARDS** section). This environment variable overrides the default, which can be queried with the **-h** or **-\-help** options. # EXIT STATUS bc(1) returns the following exit statuses: **0** : No error. **1** : A math error occurred. This follows standard practice of using **1** for expected errors, since math errors will happen in the process of normal execution. Math errors include divide by **0**, taking the square root of a negative number, using a negative number as a bound for the pseudo-random number generator, attempting to convert a negative number to a hardware integer, overflow when converting a number to a hardware integer, overflow when calculating the size of a number, and attempting to use a non-integer where an integer is required. Converting to a hardware integer happens for the second operand of the power (**\^**), places (**\@**), left shift (**\<\<**), and right shift (**\>\>**) operators and their corresponding assignment operators. **2** : A parse error occurred. Parse errors include unexpected **EOF**, using an invalid character, failing to find the end of a string or comment, using a token where it is invalid, giving an invalid expression, giving an invalid print statement, giving an invalid function definition, attempting to assign to an expression that is not a named expression (see the *Named Expressions* subsection of the **SYNTAX** section), giving an invalid **auto** list, having a duplicate **auto**/function parameter, failing to find the end of a code block, attempting to return a value from a **void** function, attempting to use a variable as a reference, and using any extensions when the option **-s** or any equivalents were given. **3** : A runtime error occurred. Runtime errors include assigning an invalid number to any global (**ibase**, **obase**, or **scale**), giving a bad expression to a **read()** call, calling **read()** inside of a **read()** call, type errors, passing the wrong number of arguments to functions, attempting to call an undefined function, and attempting to use a **void** function call as a value in an expression. **4** : A fatal error occurred. Fatal errors include memory allocation errors, I/O errors, failing to open files, attempting to use files that do not have only ASCII characters (bc(1) only accepts ASCII characters), attempting to open a directory as a file, and giving invalid command-line options. The exit status **4** is special; when a fatal error occurs, bc(1) always exits and returns **4**, no matter what mode bc(1) is in. The other statuses will only be returned when bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section), since bc(1) resets its state (see the **RESET** section) and accepts more input when one of those errors occurs in interactive mode. This is also the case when interactive mode is forced by the **-i** flag or **-\-interactive** option. These exit statuses allow bc(1) to be used in shell scripting with error checking, and its normal behavior can be forced by using the **-i** flag or **-\-interactive** option. # INTERACTIVE MODE Per the standard (see the **STANDARDS** section), bc(1) has an interactive mode and a non-interactive mode. Interactive mode is turned on automatically when both **stdin** and **stdout** are hooked to a terminal, but the **-i** flag and **-\-interactive** option can turn it on in other situations. In interactive mode, bc(1) attempts to recover from errors (see the **RESET** section), and in normal execution, flushes **stdout** as soon as execution is done for the current input. bc(1) may also reset on **SIGINT** instead of exit, depending on the contents of, or default for, the **BC_SIGINT_RESET** environment variable (see the **ENVIRONMENT VARIABLES** section). # TTY MODE If **stdin**, **stdout**, and **stderr** are all connected to a TTY, then "TTY mode" is considered to be available, and thus, bc(1) can turn on TTY mode, subject to some settings. If there is the environment variable **BC_TTY_MODE** in the environment (see the **ENVIRONMENT VARIABLES** section), then if that environment variable contains a non-zero integer, bc(1) will turn on TTY mode when **stdin**, **stdout**, and **stderr** are all connected to a TTY. If the **BC_TTY_MODE** environment variable exists but is *not* a non-zero integer, then bc(1) will not turn TTY mode on. If the environment variable **BC_TTY_MODE** does *not* exist, the default setting is used. The default setting can be queried with the **-h** or **-\-help** options. TTY mode is different from interactive mode because interactive mode is required in the bc(1) standard (see the **STANDARDS** section), and interactive mode requires only **stdin** and **stdout** to be connected to a terminal. ## Command-Line History Command-line history is only enabled if TTY mode is, i.e., that **stdin**, **stdout**, and **stderr** are connected to a TTY and the **BC_TTY_MODE** environment variable (see the **ENVIRONMENT VARIABLES** section) and its default do not disable TTY mode. See the **COMMAND LINE HISTORY** section for more information. ## Prompt If TTY mode is available, then a prompt can be enabled. Like TTY mode itself, it can be turned on or off with an environment variable: **BC_PROMPT** (see the **ENVIRONMENT VARIABLES** section). If the environment variable **BC_PROMPT** exists and is a non-zero integer, then the prompt is turned on when **stdin**, **stdout**, and **stderr** are connected to a TTY and the **-P** and **-\-no-prompt** options were not used. The read prompt will be turned on under the same conditions, except that the **-R** and **-\-no-read-prompt** options must also not be used. However, if **BC_PROMPT** does not exist, the prompt can be enabled or disabled with the **BC_TTY_MODE** environment variable, the **-P** and **-\-no-prompt** options, and the **-R** and **-\-no-read-prompt** options. See the **ENVIRONMENT VARIABLES** and **OPTIONS** sections for more details. # SIGNAL HANDLING Sending a **SIGINT** will cause bc(1) to do one of two things. If bc(1) is not in interactive mode (see the **INTERACTIVE MODE** section), or the **BC_SIGINT_RESET** environment variable (see the **ENVIRONMENT VARIABLES** section), or its default, is either not an integer or it is zero, bc(1) will exit. However, if bc(1) is in interactive mode, and the **BC_SIGINT_RESET** or its default is an integer and non-zero, then bc(1) will stop executing the current input and reset (see the **RESET** section) upon receiving a **SIGINT**. Note that "current input" can mean one of two things. If bc(1) is processing input from **stdin** in interactive mode, it will ask for more input. If bc(1) is processing input from a file in interactive mode, it will stop processing the file and start processing the next file, if one exists, or ask for input from **stdin** if no other file exists. This means that if a **SIGINT** is sent to bc(1) as it is executing a file, it can seem as though bc(1) did not respond to the signal since it will immediately start executing the next file. This is by design; most files that users execute when interacting with bc(1) have function definitions, which are quick to parse. If a file takes a long time to execute, there may be a bug in that file. The rest of the files could still be executed without problem, allowing the user to continue. **SIGTERM** and **SIGQUIT** cause bc(1) to clean up and exit, and it uses the default handler for all other signals. The one exception is **SIGHUP**; in that case, and only when bc(1) is in TTY mode (see the **TTY MODE** section), a **SIGHUP** will cause bc(1) to clean up and exit. # COMMAND LINE HISTORY bc(1) supports interactive command-line editing. If bc(1) can be in TTY mode (see the **TTY MODE** section), history can be enabled. This means that command-line history can only be enabled when **stdin**, **stdout**, and **stderr** are all connected to a TTY. Like TTY mode itself, it can be turned on or off with the environment variable **BC_TTY_MODE** (see the **ENVIRONMENT VARIABLES** section). If history is enabled, previous lines can be recalled and edited with the arrow keys. **Note**: tabs are converted to 8 spaces. # SEE ALSO dc(1) # STANDARDS bc(1) is compliant with the IEEE Std 1003.1-2017 (“POSIX.1-2017”) specification at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html . The flags **-efghiqsvVw**, all long options, and the extensions noted above are extensions to that specification. In addition, the behavior of the **quit** implements an interpretation of that specification that is different from all known implementations. For more information see the **Statements** subsection of the **SYNTAX** section. Note that the specification explicitly says that bc(1) only accepts numbers that use a period (**.**) as a radix point, regardless of the value of **LC_NUMERIC**. # BUGS Before version **6.1.0**, this bc(1) had incorrect behavior for the **quit** statement. No other bugs are known. Report bugs at https://git.gavinhoward.com/gavin/bc . # AUTHORS Gavin D. Howard and contributors. diff --git a/contrib/bc/manuals/bcl.3 b/contrib/bc/manuals/bcl.3 index cb65a2b8b991..09e5de74597e 100644 --- a/contrib/bc/manuals/bcl.3 +++ b/contrib/bc/manuals/bcl.3 @@ -1,1867 +1,1840 @@ .\" .\" SPDX-License-Identifier: BSD-2-Clause .\" .\" Copyright (c) 2018-2023 Gavin D. Howard and contributors. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions are met: .\" .\" * Redistributions of source code must retain the above copyright notice, .\" this list of conditions and the following disclaimer. .\" .\" * Redistributions in binary form must reproduce the above copyright notice, .\" this list of conditions and the following disclaimer in the documentation .\" and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" .TH "BCL" "3" "February 2023" "Gavin D. Howard" "Libraries Manual" .nh .ad l .SH NAME -.PP bcl - library of arbitrary precision decimal arithmetic .SH SYNOPSIS .SS Use -.PP \f[I]#include \f[R] .PP Link with \f[I]-lbcl\f[R], and on POSIX systems, \f[I]-lpthread\f[R] is also required. .SS Setup -.PP These items allow clients to set up bcl(3). .PP \f[B]BclError bcl_start(\f[R]\f[I]void\f[R]\f[B]);\f[R] .PP \f[B]void bcl_end(\f[R]\f[I]void\f[R]\f[B]);\f[R] .PP \f[B]BclError bcl_init(\f[R]\f[I]void\f[R]\f[B]);\f[R] .PP \f[B]void bcl_free(\f[R]\f[I]void\f[R]\f[B]);\f[R] .PP \f[B]bool bcl_abortOnFatalError(\f[R]\f[I]void\f[R]\f[B]);\f[R] .PP \f[B]void bcl_setAbortOnFatalError(bool\f[R] \f[I]abrt\f[R]\f[B]);\f[R] .PP \f[B]bool bcl_leadingZeroes(\f[R]\f[I]void\f[R]\f[B]);\f[R] .PP \f[B]void bcl_setLeadingZeroes(bool\f[R] \f[I]leadingZeroes\f[R]\f[B]);\f[R] .PP \f[B]void bcl_gc(\f[R]\f[I]void\f[R]\f[B]);\f[R] .PP \f[B]bool bcl_digitClamp(\f[R]\f[I]void\f[R]\f[B]);\f[R] .PP \f[B]void bcl_setDigitClamp(bool\f[R] \f[I]digitClamp\f[R]\f[B]);\f[R] .SS Contexts -.PP These items will allow clients to handle contexts, which are isolated from each other. This allows more than one client to use bcl(3) in the same program. .PP \f[B]struct BclCtxt;\f[R] .PP \f[B]typedef struct BclCtxt* BclContext;\f[R] .PP \f[B]BclContext bcl_ctxt_create(\f[R]\f[I]void\f[R]\f[B]);\f[R] .PP \f[B]void bcl_ctxt_free(BclContext\f[R] \f[I]ctxt\f[R]\f[B]);\f[R] .PP \f[B]BclError bcl_pushContext(BclContext\f[R] \f[I]ctxt\f[R]\f[B]);\f[R] .PP \f[B]void bcl_popContext(\f[R]\f[I]void\f[R]\f[B]);\f[R] .PP \f[B]BclContext bcl_context(\f[R]\f[I]void\f[R]\f[B]);\f[R] .PP \f[B]void bcl_ctxt_freeNums(BclContext\f[R] \f[I]ctxt\f[R]\f[B]);\f[R] .PP \f[B]size_t bcl_ctxt_scale(BclContext\f[R] \f[I]ctxt\f[R]\f[B]);\f[R] .PP \f[B]void bcl_ctxt_setScale(BclContext\f[R] \f[I]ctxt\f[R]\f[B], size_t\f[R] \f[I]scale\f[R]\f[B]);\f[R] .PP \f[B]size_t bcl_ctxt_ibase(BclContext\f[R] \f[I]ctxt\f[R]\f[B]);\f[R] .PP \f[B]void bcl_ctxt_setIbase(BclContext\f[R] \f[I]ctxt\f[R]\f[B], size_t\f[R] \f[I]ibase\f[R]\f[B]);\f[R] .PP \f[B]size_t bcl_ctxt_obase(BclContext\f[R] \f[I]ctxt\f[R]\f[B]);\f[R] .PP \f[B]void bcl_ctxt_setObase(BclContext\f[R] \f[I]ctxt\f[R]\f[B], size_t\f[R] \f[I]obase\f[R]\f[B]);\f[R] .SS Errors -.PP These items allow clients to handle errors. .PP \f[B]typedef enum BclError BclError;\f[R] .PP \f[B]BclError bcl_err(BclNumber\f[R] \f[I]n\f[R]\f[B]);\f[R] .SS Numbers -.PP These items allow clients to manipulate and query the arbitrary-precision numbers managed by bcl(3). .PP \f[B]typedef struct { size_t i; } BclNumber;\f[R] .PP \f[B]BclNumber bcl_num_create(\f[R]\f[I]void\f[R]\f[B]);\f[R] .PP \f[B]void bcl_num_free(BclNumber\f[R] \f[I]n\f[R]\f[B]);\f[R] .PP \f[B]bool bcl_num_neg(BclNumber\f[R] \f[I]n\f[R]\f[B]);\f[R] .PP \f[B]void bcl_num_setNeg(BclNumber\f[R] \f[I]n\f[R]\f[B], bool\f[R] \f[I]neg\f[R]\f[B]);\f[R] .PP \f[B]size_t bcl_num_scale(BclNumber\f[R] \f[I]n\f[R]\f[B]);\f[R] .PP \f[B]BclError bcl_num_setScale(BclNumber\f[R] \f[I]n\f[R]\f[B], size_t\f[R] \f[I]scale\f[R]\f[B]);\f[R] .PP \f[B]size_t bcl_num_len(BclNumber\f[R] \f[I]n\f[R]\f[B]);\f[R] .SS Conversion -.PP These items allow clients to convert numbers into and from strings and integers. .PP \f[B]BclNumber bcl_parse(const char *restrict\f[R] \f[I]val\f[R]\f[B]);\f[R] .PP \f[B]char* bcl_string(BclNumber\f[R] \f[I]n\f[R]\f[B]);\f[R] .PP \f[B]char* bcl_string_keep(BclNumber\f[R] \f[I]n\f[R]\f[B]);\f[R] .PP \f[B]BclError bcl_bigdig(BclNumber\f[R] \f[I]n\f[R]\f[B], BclBigDig *\f[R]\f[I]result\f[R]\f[B]);\f[R] .PP \f[B]BclError bcl_bigdig_keep(BclNumber\f[R] \f[I]n\f[R]\f[B], BclBigDig *\f[R]\f[I]result\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_bigdig2num(BclBigDig\f[R] \f[I]val\f[R]\f[B]);\f[R] .SS Math -.PP These items allow clients to run math on numbers. .PP \f[B]BclNumber bcl_add(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_add_keep(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_sub(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_sub_keep(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_mul(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_mul_keep(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_div(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_div_keep(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_mod(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_mod_keep(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_pow(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_pow_keep(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_lshift(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_lshift_keep(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_rshift(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_rshift_keep(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_sqrt(BclNumber\f[R] \f[I]a\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_sqrt_keep(BclNumber\f[R] \f[I]a\f[R]\f[B]);\f[R] .PP \f[B]BclError bcl_divmod(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B], BclNumber *\f[R]\f[I]c\f[R]\f[B], BclNumber *\f[R]\f[I]d\f[R]\f[B]);\f[R] .PP \f[B]BclError bcl_divmod_keep(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B], BclNumber *\f[R]\f[I]c\f[R]\f[B], BclNumber *\f[R]\f[I]d\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_modexp(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B], BclNumber\f[R] \f[I]c\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_modexp_keep(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B], BclNumber\f[R] \f[I]c\f[R]\f[B]);\f[R] .SS Miscellaneous -.PP These items are miscellaneous. .PP \f[B]void bcl_zero(BclNumber\f[R] \f[I]n\f[R]\f[B]);\f[R] .PP \f[B]void bcl_one(BclNumber\f[R] \f[I]n\f[R]\f[B]);\f[R] .PP \f[B]ssize_t bcl_cmp(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B]);\f[R] .PP \f[B]BclError bcl_copy(BclNumber\f[R] \f[I]d\f[R]\f[B], BclNumber\f[R] \f[I]s\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_dup(BclNumber\f[R] \f[I]s\f[R]\f[B]);\f[R] .SS Pseudo-Random Number Generator -.PP These items allow clients to manipulate the seeded pseudo-random number generator in bcl(3). .PP \f[B]#define BCL_SEED_ULONGS\f[R] .PP \f[B]#define BCL_SEED_SIZE\f[R] .PP \f[B]typedef unsigned long BclBigDig;\f[R] .PP \f[B]typedef unsigned long BclRandInt;\f[R] .PP \f[B]BclNumber bcl_irand(BclNumber\f[R] \f[I]a\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_irand_keep(BclNumber\f[R] \f[I]a\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_frand(size_t\f[R] \f[I]places\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_ifrand(BclNumber\f[R] \f[I]a\f[R]\f[B], size_t\f[R] \f[I]places\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_ifrand_keep(BclNumber\f[R] \f[I]a\f[R]\f[B], size_t\f[R] \f[I]places\f[R]\f[B]);\f[R] .PP \f[B]BclError bcl_rand_seedWithNum(BclNumber\f[R] \f[I]n\f[R]\f[B]);\f[R] .PP \f[B]BclError bcl_rand_seedWithNum_keep(BclNumber\f[R] \f[I]n\f[R]\f[B]);\f[R] .PP \f[B]BclError bcl_rand_seed(unsigned char\f[R] \f[I]seed\f[R]\f[B][\f[R]\f[I]BCL_SEED_SIZE\f[R]\f[B]]);\f[R] .PP \f[B]void bcl_rand_reseed(\f[R]\f[I]void\f[R]\f[B]);\f[R] .PP \f[B]BclNumber bcl_rand_seed2num(\f[R]\f[I]void\f[R]\f[B]);\f[R] .PP \f[B]BclRandInt bcl_rand_int(\f[R]\f[I]void\f[R]\f[B]);\f[R] .PP \f[B]BclRandInt bcl_rand_bounded(BclRandInt\f[R] \f[I]bound\f[R]\f[B]);\f[R] .SH DESCRIPTION -.PP bcl(3) is a library that implements arbitrary-precision decimal math, as standardized by POSIX (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html) in bc(1). .PP bcl(3) assumes that it is allowed to use the \f[B]bcl\f[R], \f[B]Bcl\f[R], \f[B]bc\f[R], and \f[B]Bc\f[R] prefixes for symbol names without collision. .PP All of the items in its interface are described below. See the documentation for each function for what each function can return. .SS Setup .TP \f[B]BclError bcl_start(\f[R]\f[I]void\f[R]\f[B])\f[R] Initializes this library. This function can be called multiple times, but \f[B]bcl_end()\f[R] must only be called \f[I]once\f[R]. This is to make it possible for multiple libraries and applications to initialize bcl(3) without problem. .RS .PP It is suggested that client libraries call this function, but do not call \f[B]bcl_end()\f[R], and client applications should call both. .PP If there was no error, \f[B]BCL_ERROR_NONE\f[R] is returned. Otherwise, this function can return: .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .PP This function must be the first one clients call. Calling any other function without calling this one first is undefined behavior. .RE .TP \f[B]void bcl_end(\f[R]\f[I]void\f[R]\f[B])\f[R] Deinitializes this library. This function must only be called \f[I]once\f[R]. .RS .PP All data must have been freed before calling this function. .PP This function must be the last one clients call. Calling this function before calling any other function is undefined behavior. .RE .TP \f[B]BclError bcl_init(\f[R]\f[I]void\f[R]\f[B])\f[R] Initializes the library for the current thread. This function can be called multiple times, but each call must be matched by a call to \f[B]bcl_free(\f[R]\f[I]void\f[R]\f[B])\f[R]. This is to make it possible for multiple libraries and applications to initialize threads for bcl(3) without problem. .RS .PP This function \f[I]must\f[R] be called from the thread that it is supposed to initialize. .PP If there was no error, \f[B]BCL_ERROR_NONE\f[R] is returned. Otherwise, this function can return: .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .PP This function must be the second one clients call. Calling any other function without calling \f[B]bcl_start()\f[R] and then this one first is undefined behavior, except in the case of new threads. New threads can safely call this function without calling \f[B]bcl_start()\f[R] if another thread has previously called \f[B]bcl_start()\f[R]. But this function must still be the first function in bcl(3) called by that new thread. .RE .TP \f[B]void bcl_free(\f[R]\f[I]void\f[R]\f[B])\f[R] Decrements bcl(3)\[cq]s reference count and frees the data associated with it if the reference count is \f[B]0\f[R]. .RS .PP This function \f[I]must\f[R] be called from the thread that it is supposed to deinitialize. .PP This function must be the second to last one clients call. Calling this function before calling any other function besides \f[B]bcl_end()\f[R] is undefined behavior. .RE .TP \f[B]bool bcl_abortOnFatalError(\f[R]\f[I]void\f[R]\f[B])\f[R] Queries and returns the current state of calling \f[B]abort()\f[R] on fatal errors. If \f[B]true\f[R] is returned, bcl(3) will cause a \f[B]SIGABRT\f[R] if a fatal error occurs. .RS .PP If activated, clients do not need to check for fatal errors. .PP This value is \f[I]thread-local\f[R]; it applies to just the thread it is read on. .PP The default is \f[B]false\f[R]. .RE .TP \f[B]void bcl_setAbortOnFatalError(bool\f[R] \f[I]abrt\f[R]\f[B])\f[R] Sets the state of calling \f[B]abort()\f[R] on fatal errors. If \f[I]abrt\f[R] is \f[B]false\f[R], bcl(3) will not cause a \f[B]SIGABRT\f[R] on fatal errors after the call. If \f[I]abrt\f[R] is \f[B]true\f[R], bcl(3) will cause a \f[B]SIGABRT\f[R] on fatal errors after the call. .RS .PP This value is \f[I]thread-local\f[R]; it applies to just the thread it is set on. .PP If activated, clients do not need to check for fatal errors. .RE .TP \f[B]bool bcl_leadingZeroes(\f[R]\f[I]void\f[R]\f[B])\f[R] Queries and returns the state of whether leading zeroes are added to strings returned by \f[B]bcl_string()\f[R] when numbers are greater than \f[B]-1\f[R], less than \f[B]1\f[R], and not equal to \f[B]0\f[R]. If \f[B]true\f[R] is returned, then leading zeroes will be added. .RS .PP This value is \f[I]thread-local\f[R]; it applies to just the thread it is read on. .PP The default is \f[B]false\f[R]. .RE .TP \f[B]void bcl_setLeadingZeroes(bool\f[R] \f[I]leadingZeroes\f[R]\f[B])\f[R] Sets the state of whether leading zeroes are added to strings returned by \f[B]bcl_string()\f[R] when numbers are greater than \f[B]-1\f[R], less than \f[B]1\f[R], and not equal to \f[B]0\f[R]. If \f[I]leadingZeroes\f[R] is \f[B]true\f[R], leading zeroes will be added to strings returned by \f[B]bcl_string()\f[R]. .RS .PP This value is \f[I]thread-local\f[R]; it applies to just the thread it is set on. .RE .TP \f[B]bool bcl_digitClamp(\f[R]\f[I]void\f[R]\f[B])\f[R] Queries and returns the state of whether digits in number strings that are greater than or equal to the current \f[B]ibase\f[R] are clamped or not. .RS .PP If \f[B]true\f[R] is returned, then digits are treated as though they are equal to the value of \f[B]ibase\f[R] minus \f[B]1\f[R]. If this is \f[I]not\f[R] true, then digits are treated as though they are equal to the value they would have if \f[B]ibase\f[R] was large enough. They are then multiplied by the appropriate power of \f[B]ibase\f[R]. .PP For example, with clamping off and an \f[B]ibase\f[R] of \f[B]3\f[R], the string \[lq]AB\[rq] would equal \f[B]3\[ha]1*A+3\[ha]0*B\f[R], which is \f[B]3\f[R] times \f[B]10\f[R] plus \f[B]11\f[R], or \f[B]41\f[R], while with clamping on and an \f[B]ibase\f[R] of \f[B]3\f[R], the string \[lq]AB\[rq] would be equal to \f[B]3\[ha]1*2+3\[ha]0*2\f[R], which is \f[B]3\f[R] times \f[B]2\f[R] plus \f[B]2\f[R], or \f[B]8\f[R]. .PP This value is \f[I]thread-local\f[R]; it applies to just the thread it is read on. .PP The default is \f[B]true\f[R]. .RE .TP \f[B]void bcl_setDigitClamp(bool\f[R] \f[I]digitClamp\f[R]\f[B])\f[R] Sets the state of whether digits in number strings that are greater than or equal to the current \f[B]ibase\f[R] are clamped or not. For more information, see the \f[B]bcl_digitClamp(\f[R]\f[I]void\f[R]\f[B])\f[R] function. .RS .PP This value is \f[I]thread-local\f[R]; it applies to just the thread it is set on. .RE .TP \f[B]void bcl_gc(\f[R]\f[I]void\f[R]\f[B])\f[R] Garbage collects cached instances of arbitrary-precision numbers. This only frees the memory of numbers that are \f[I]not\f[R] in use, so it is safe to call at any time. .SS Contexts -.PP All procedures that take a \f[B]BclContext\f[R] parameter a require a valid context as an argument. .TP \f[B]struct BclCtxt\f[R] A forward declaration for a hidden \f[B]struct\f[R] type. Clients cannot access the internals of the \f[B]struct\f[R] type directly. All interactions with the type are done through pointers. See \f[B]BclContext\f[R] below. .TP \f[B]BclContext\f[R] A typedef to a pointer of \f[B]struct BclCtxt\f[R]. This is the only handle clients can get to \f[B]struct BclCtxt\f[R]. .RS .PP A \f[B]BclContext\f[R] contains the values \f[B]scale\f[R], \f[B]ibase\f[R], and \f[B]obase\f[R], as well as a list of numbers. .PP \f[B]scale\f[R] is a value used to control how many decimal places calculations should use. A value of \f[B]0\f[R] means that calculations are done on integers only, where applicable, and a value of 20, for example, means that all applicable calculations return results with 20 decimal places. The default is \f[B]0\f[R]. .PP \f[B]ibase\f[R] is a value used to control the input base. The minimum \f[B]ibase\f[R] is \f[B]2\f[R], and the maximum is \f[B]36\f[R]. If \f[B]ibase\f[R] is \f[B]2\f[R], numbers are parsed as though they are in binary, and any digits larger than \f[B]1\f[R] are clamped. Likewise, a value of \f[B]10\f[R] means that numbers are parsed as though they are decimal, and any larger digits are clamped. The default is \f[B]10\f[R]. .PP \f[B]obase\f[R] is a value used to control the output base. The minimum \f[B]obase\f[R] is \f[B]0\f[R] and the maximum is \f[B]BC_BASE_MAX\f[R] (see the \f[B]LIMITS\f[R] section). .PP Numbers created in one context are not valid in another context. It is undefined behavior to use a number created in a different context. Contexts are meant to isolate the numbers used by different clients in the same application. .PP Different threads also have different contexts, so any numbers created in one thread are not valid in another thread. To pass values between contexts and threads, use \f[B]bcl_string()\f[R] to produce a string to pass around, and use \f[B]bcl_parse()\f[R] to parse the string. It is suggested that the \f[B]obase\f[R] used to create the string be passed around with the string and used as the \f[B]ibase\f[R] for \f[B]bcl_parse()\f[R] to ensure that the number will be the same. .RE .TP \f[B]BclContext bcl_ctxt_create(\f[R]\f[I]void\f[R]\f[B])\f[R] Creates a context and returns it. Returns \f[B]NULL\f[R] if there was an error. .TP \f[B]void bcl_ctxt_free(BclContext\f[R] \f[I]ctxt\f[R]\f[B])\f[R] Frees \f[I]ctxt\f[R], after which it is no longer valid. It is undefined behavior to attempt to use an invalid context. .TP \f[B]BclError bcl_pushContext(BclContext\f[R] \f[I]ctxt\f[R]\f[B])\f[R] Pushes \f[I]ctxt\f[R] onto bcl(3)\[cq]s stack of contexts. \f[I]ctxt\f[R] must have been created with \f[B]bcl_ctxt_create(\f[R]\f[I]void\f[R]\f[B])\f[R]. .RS .PP If there was no error, \f[B]BCL_ERROR_NONE\f[R] is returned. Otherwise, this function can return: .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .PP There \f[I]must\f[R] be a valid context to do any arithmetic. .RE .TP \f[B]void bcl_popContext(\f[R]\f[I]void\f[R]\f[B])\f[R] Pops the current context off of the stack, if one exists. .TP \f[B]BclContext bcl_context(\f[R]\f[I]void\f[R]\f[B])\f[R] Returns the current context, or \f[B]NULL\f[R] if no context exists. .TP \f[B]void bcl_ctxt_freeNums(BclContext\f[R] \f[I]ctxt\f[R]\f[B])\f[R] Frees all numbers in use that are associated with \f[I]ctxt\f[R]. It is undefined behavior to attempt to use a number associated with \f[I]ctxt\f[R] after calling this procedure unless such numbers have been created with \f[B]bcl_num_create(\f[R]\f[I]void\f[R]\f[B])\f[R] after calling this procedure. .TP \f[B]size_t bcl_ctxt_scale(BclContext\f[R] \f[I]ctxt\f[R]\f[B])\f[R] Returns the \f[B]scale\f[R] for given context. .TP \f[B]void bcl_ctxt_setScale(BclContext\f[R] \f[I]ctxt\f[R]\f[B], size_t\f[R] \f[I]scale\f[R]\f[B])\f[R] Sets the \f[B]scale\f[R] for the given context to the argument \f[I]scale\f[R]. .TP \f[B]size_t bcl_ctxt_ibase(BclContext\f[R] \f[I]ctxt\f[R]\f[B])\f[R] Returns the \f[B]ibase\f[R] for the given context. .TP \f[B]void bcl_ctxt_setIbase(BclContext\f[R] \f[I]ctxt\f[R]\f[B], size_t\f[R] \f[I]ibase\f[R]\f[B])\f[R] Sets the \f[B]ibase\f[R] for the given context to the argument \f[I]ibase\f[R]. If the argument \f[I]ibase\f[R] is invalid, it clamped, so an \f[I]ibase\f[R] of \f[B]0\f[R] or \f[B]1\f[R] is clamped to \f[B]2\f[R], and any values above \f[B]36\f[R] are clamped to \f[B]36\f[R]. .TP \f[B]size_t bcl_ctxt_obase(BclContext\f[R] \f[I]ctxt\f[R]\f[B])\f[R] Returns the \f[B]obase\f[R] for the given context. .TP \f[B]void bcl_ctxt_setObase(BclContext\f[R] \f[I]ctxt\f[R]\f[B], size_t\f[R] \f[I]obase\f[R]\f[B])\f[R] Sets the \f[B]obase\f[R] for the given context to the argument \f[I]obase\f[R]. .SS Errors .TP \f[B]BclError\f[R] An \f[B]enum\f[R] of possible error codes. See the \f[B]ERRORS\f[R] section for a complete listing the codes. .TP \f[B]BclError bcl_err(BclNumber\f[R] \f[I]n\f[R]\f[B])\f[R] Checks for errors in a \f[B]BclNumber\f[R]. All functions that can return a \f[B]BclNumber\f[R] can encode an error in the number, and this function will return the error, if any. If there was no error, it will return \f[B]BCL_ERROR_NONE\f[R]. .RS .PP There must be a valid current context. .RE .SS Numbers -.PP All procedures in this section require a valid current context. .TP \f[B]BclNumber\f[R] A handle to an arbitrary-precision number. The actual number type is not exposed; the \f[B]BclNumber\f[R] handle is the only way clients can refer to instances of arbitrary-precision numbers. .TP \f[B]BclNumber bcl_num_create(\f[R]\f[I]void\f[R]\f[B])\f[R] Creates and returns a \f[B]BclNumber\f[R]. .RS .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]void bcl_num_free(BclNumber\f[R] \f[I]n\f[R]\f[B])\f[R] Frees \f[I]n\f[R]. It is undefined behavior to use \f[I]n\f[R] after calling this function. .TP \f[B]bool bcl_num_neg(BclNumber\f[R] \f[I]n\f[R]\f[B])\f[R] Returns \f[B]true\f[R] if \f[I]n\f[R] is negative, \f[B]false\f[R] otherwise. .TP \f[B]void bcl_num_setNeg(BclNumber\f[R] \f[I]n\f[R]\f[B], bool\f[R] \f[I]neg\f[R]\f[B])\f[R] Sets \f[I]n\f[R]\[cq]s sign to \f[I]neg\f[R], where \f[B]true\f[R] is negative, and \f[B]false\f[R] is positive. .TP \f[B]size_t bcl_num_scale(BclNumber\f[R] \f[I]n\f[R]\f[B])\f[R] Returns the \f[I]scale\f[R] of \f[I]n\f[R]. .RS .PP The \f[I]scale\f[R] of a number is the number of decimal places it has after the radix (decimal point). .RE .TP \f[B]BclError bcl_num_setScale(BclNumber\f[R] \f[I]n\f[R]\f[B], size_t\f[R] \f[I]scale\f[R]\f[B])\f[R] Sets the \f[I]scale\f[R] of \f[I]n\f[R] to the argument \f[I]scale\f[R]. If the argument \f[I]scale\f[R] is greater than the \f[I]scale\f[R] of \f[I]n\f[R], \f[I]n\f[R] is extended. If the argument \f[I]scale\f[R] is less than the \f[I]scale\f[R] of \f[I]n\f[R], \f[I]n\f[R] is truncated. .RS .PP If there was no error, \f[B]BCL_ERROR_NONE\f[R] is returned. Otherwise, this function can return: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]size_t bcl_num_len(BclNumber\f[R] \f[I]n\f[R]\f[B])\f[R] Returns the number of \f[I]significant decimal digits\f[R] in \f[I]n\f[R]. .SS Conversion -.PP All procedures in this section require a valid current context. .PP All procedures in this section without the \f[B]_keep\f[R] suffix in their name consume the given \f[B]BclNumber\f[R] arguments that are not given to pointer arguments. See the \f[B]Consumption and Propagation\f[R] subsection below. .TP \f[B]BclNumber bcl_parse(const char *restrict\f[R] \f[I]val\f[R]\f[B])\f[R] Parses a number string according to the current context\[cq]s \f[B]ibase\f[R] and returns the resulting number. .RS .PP \f[I]val\f[R] must be non-\f[B]NULL\f[R] and a valid string. See \f[B]BCL_ERROR_PARSE_INVALID_STR\f[R] in the \f[B]ERRORS\f[R] section for more information. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_PARSE_INVALID_STR\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]char* bcl_string(BclNumber\f[R] \f[I]n\f[R]\f[B])\f[R] Returns a string representation of \f[I]n\f[R] according the the current context\[cq]s \f[B]ibase\f[R]. The string is dynamically allocated and must be freed by the caller. .RS .PP \f[I]n\f[R] is consumed; it cannot be used after the call. See the \f[B]Consumption and Propagation\f[R] subsection below. .RE .TP \f[B]char* bcl_string_keep(BclNumber\f[R] \f[I]n\f[R]\f[B])\f[R] Returns a string representation of \f[I]n\f[R] according the the current context\[cq]s \f[B]ibase\f[R]. The string is dynamically allocated and must be freed by the caller. .TP \f[B]BclError bcl_bigdig(BclNumber\f[R] \f[I]n\f[R]\f[B], BclBigDig *\f[R]\f[I]result\f[R]\f[B])\f[R] Converts \f[I]n\f[R] into a \f[B]BclBigDig\f[R] and returns the result in the space pointed to by \f[I]result\f[R]. .RS .PP \f[I]a\f[R] must be smaller than \f[B]BC_OVERFLOW_MAX\f[R]. See the \f[B]LIMITS\f[R] section. .PP If there was no error, \f[B]BCL_ERROR_NONE\f[R] is returned. Otherwise, this function can return: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_OVERFLOW\f[R] .PP \f[I]n\f[R] is consumed; it cannot be used after the call. See the \f[B]Consumption and Propagation\f[R] subsection below. .RE .TP \f[B]BclError bcl_bigdig_keep(BclNumber\f[R] \f[I]n\f[R]\f[B], BclBigDig *\f[R]\f[I]result\f[R]\f[B])\f[R] Converts \f[I]n\f[R] into a \f[B]BclBigDig\f[R] and returns the result in the space pointed to by \f[I]result\f[R]. .RS .PP \f[I]a\f[R] must be smaller than \f[B]BC_OVERFLOW_MAX\f[R]. See the \f[B]LIMITS\f[R] section. .PP If there was no error, \f[B]BCL_ERROR_NONE\f[R] is returned. Otherwise, this function can return: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_OVERFLOW\f[R] .RE .TP \f[B]BclNumber bcl_bigdig2num(BclBigDig\f[R] \f[I]val\f[R]\f[B])\f[R] Creates a \f[B]BclNumber\f[R] from \f[I]val\f[R]. .RS .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .SS Math -.PP All procedures in this section require a valid current context. .PP All procedures in this section without the \f[B]_keep\f[R] suffix in their name consume the given \f[B]BclNumber\f[R] arguments that are not given to pointer arguments. See the \f[B]Consumption and Propagation\f[R] subsection below. .PP All procedures in this section can return the following errors: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .TP \f[B]BclNumber bcl_add(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B])\f[R] Adds \f[I]a\f[R] and \f[I]b\f[R] and returns the result. The \f[I]scale\f[R] of the result is the max of the \f[I]scale\f[R]s of \f[I]a\f[R] and \f[I]b\f[R]. .RS .PP \f[I]a\f[R] and \f[I]b\f[R] are consumed; they cannot be used after the call. See the \f[B]Consumption and Propagation\f[R] subsection below. .PP \f[I]a\f[R] and \f[I]b\f[R] can be the same number. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclNumber bcl_add_keep(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B])\f[R] Adds \f[I]a\f[R] and \f[I]b\f[R] and returns the result. The \f[I]scale\f[R] of the result is the max of the \f[I]scale\f[R]s of \f[I]a\f[R] and \f[I]b\f[R]. .RS .PP \f[I]a\f[R] and \f[I]b\f[R] can be the same number. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclNumber bcl_sub(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B])\f[R] Subtracts \f[I]b\f[R] from \f[I]a\f[R] and returns the result. The \f[I]scale\f[R] of the result is the max of the \f[I]scale\f[R]s of \f[I]a\f[R] and \f[I]b\f[R]. .RS .PP \f[I]a\f[R] and \f[I]b\f[R] are consumed; they cannot be used after the call. See the \f[B]Consumption and Propagation\f[R] subsection below. .PP \f[I]a\f[R] and \f[I]b\f[R] can be the same number. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclNumber bcl_sub_keep(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B])\f[R] Subtracts \f[I]b\f[R] from \f[I]a\f[R] and returns the result. The \f[I]scale\f[R] of the result is the max of the \f[I]scale\f[R]s of \f[I]a\f[R] and \f[I]b\f[R]. .RS .PP \f[I]a\f[R] and \f[I]b\f[R] can be the same number. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclNumber bcl_mul(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B])\f[R] Multiplies \f[I]a\f[R] and \f[I]b\f[R] and returns the result. If \f[I]ascale\f[R] is the \f[I]scale\f[R] of \f[I]a\f[R] and \f[I]bscale\f[R] is the \f[I]scale\f[R] of \f[I]b\f[R], the \f[I]scale\f[R] of the result is equal to \f[B]min(ascale+bscale,max(scale,ascale,bscale))\f[R], where \f[B]min()\f[R] and \f[B]max()\f[R] return the obvious values. .RS .PP \f[I]a\f[R] and \f[I]b\f[R] are consumed; they cannot be used after the call. See the \f[B]Consumption and Propagation\f[R] subsection below. .PP \f[I]a\f[R] and \f[I]b\f[R] can be the same number. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclNumber bcl_mul_keep(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B])\f[R] Multiplies \f[I]a\f[R] and \f[I]b\f[R] and returns the result. If \f[I]ascale\f[R] is the \f[I]scale\f[R] of \f[I]a\f[R] and \f[I]bscale\f[R] is the \f[I]scale\f[R] of \f[I]b\f[R], the \f[I]scale\f[R] of the result is equal to \f[B]min(ascale+bscale,max(scale,ascale,bscale))\f[R], where \f[B]min()\f[R] and \f[B]max()\f[R] return the obvious values. .RS .PP \f[I]a\f[R] and \f[I]b\f[R] can be the same number. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclNumber bcl_div(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B])\f[R] Divides \f[I]a\f[R] by \f[I]b\f[R] and returns the result. The \f[I]scale\f[R] of the result is the \f[I]scale\f[R] of the current context. .RS .PP \f[I]b\f[R] cannot be \f[B]0\f[R]. .PP \f[I]a\f[R] and \f[I]b\f[R] are consumed; they cannot be used after the call. See the \f[B]Consumption and Propagation\f[R] subsection below. .PP \f[I]a\f[R] and \f[I]b\f[R] can be the same number. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_DIVIDE_BY_ZERO\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclNumber bcl_div_keep(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B])\f[R] Divides \f[I]a\f[R] by \f[I]b\f[R] and returns the result. The \f[I]scale\f[R] of the result is the \f[I]scale\f[R] of the current context. .RS .PP \f[I]b\f[R] cannot be \f[B]0\f[R]. .PP \f[I]a\f[R] and \f[I]b\f[R] can be the same number. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_DIVIDE_BY_ZERO\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclNumber bcl_mod(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B])\f[R] Divides \f[I]a\f[R] by \f[I]b\f[R] to the \f[I]scale\f[R] of the current context, computes the modulus \f[B]a-(a/b)*b\f[R], and returns the modulus. .RS .PP \f[I]b\f[R] cannot be \f[B]0\f[R]. .PP \f[I]a\f[R] and \f[I]b\f[R] are consumed; they cannot be used after the call. See the \f[B]Consumption and Propagation\f[R] subsection below. .PP \f[I]a\f[R] and \f[I]b\f[R] can be the same number. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_DIVIDE_BY_ZERO\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclNumber bcl_mod_keep(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B])\f[R] Divides \f[I]a\f[R] by \f[I]b\f[R] to the \f[I]scale\f[R] of the current context, computes the modulus \f[B]a-(a/b)*b\f[R], and returns the modulus. .RS .PP \f[I]b\f[R] cannot be \f[B]0\f[R]. .PP \f[I]a\f[R] and \f[I]b\f[R] can be the same number. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_DIVIDE_BY_ZERO\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclNumber bcl_pow(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B])\f[R] Calculates \f[I]a\f[R] to the power of \f[I]b\f[R] to the \f[I]scale\f[R] of the current context. \f[I]b\f[R] must be an integer, but can be negative. If it is negative, \f[I]a\f[R] must be non-zero. .RS .PP \f[I]b\f[R] must be an integer. If \f[I]b\f[R] is negative, \f[I]a\f[R] must not be \f[B]0\f[R]. .PP \f[I]a\f[R] must be smaller than \f[B]BC_OVERFLOW_MAX\f[R]. See the \f[B]LIMITS\f[R] section. .PP \f[I]a\f[R] and \f[I]b\f[R] are consumed; they cannot be used after the call. See the \f[B]Consumption and Propagation\f[R] subsection below. .PP \f[I]a\f[R] and \f[I]b\f[R] can be the same number. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_NON_INTEGER\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_OVERFLOW\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_DIVIDE_BY_ZERO\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclNumber bcl_pow_keep(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B])\f[R] Calculates \f[I]a\f[R] to the power of \f[I]b\f[R] to the \f[I]scale\f[R] of the current context. \f[I]b\f[R] must be an integer, but can be negative. If it is negative, \f[I]a\f[R] must be non-zero. .RS .PP \f[I]b\f[R] must be an integer. If \f[I]b\f[R] is negative, \f[I]a\f[R] must not be \f[B]0\f[R]. .PP \f[I]a\f[R] must be smaller than \f[B]BC_OVERFLOW_MAX\f[R]. See the \f[B]LIMITS\f[R] section. .PP \f[I]a\f[R] and \f[I]b\f[R] can be the same number. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_NON_INTEGER\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_OVERFLOW\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_DIVIDE_BY_ZERO\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclNumber bcl_lshift(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B])\f[R] Shifts \f[I]a\f[R] left (moves the radix right) by \f[I]b\f[R] places and returns the result. This is done in decimal. \f[I]b\f[R] must be an integer. .RS .PP \f[I]b\f[R] must be an integer. .PP \f[I]a\f[R] and \f[I]b\f[R] are consumed; they cannot be used after the call. See the \f[B]Consumption and Propagation\f[R] subsection below. .PP \f[I]a\f[R] and \f[I]b\f[R] can be the same number. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_NON_INTEGER\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclNumber bcl_lshift_keep(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B])\f[R] Shifts \f[I]a\f[R] left (moves the radix right) by \f[I]b\f[R] places and returns the result. This is done in decimal. \f[I]b\f[R] must be an integer. .RS .PP \f[I]b\f[R] must be an integer. .PP \f[I]a\f[R] and \f[I]b\f[R] can be the same number. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_NON_INTEGER\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclNumber bcl_rshift(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B])\f[R] Shifts \f[I]a\f[R] right (moves the radix left) by \f[I]b\f[R] places and returns the result. This is done in decimal. \f[I]b\f[R] must be an integer. .RS .PP \f[I]b\f[R] must be an integer. .PP \f[I]a\f[R] and \f[I]b\f[R] are consumed; they cannot be used after the call. See the \f[B]Consumption and Propagation\f[R] subsection below. .PP \f[I]a\f[R] and \f[I]b\f[R] can be the same number. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_NON_INTEGER\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclNumber bcl_rshift_keep(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B])\f[R] Shifts \f[I]a\f[R] right (moves the radix left) by \f[I]b\f[R] places and returns the result. This is done in decimal. \f[I]b\f[R] must be an integer. .RS .PP \f[I]b\f[R] must be an integer. .PP \f[I]a\f[R] and \f[I]b\f[R] can be the same number. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_NON_INTEGER\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclNumber bcl_sqrt(BclNumber\f[R] \f[I]a\f[R]\f[B])\f[R] Calculates the square root of \f[I]a\f[R] and returns the result. The \f[I]scale\f[R] of the result is equal to the \f[B]scale\f[R] of the current context. .RS .PP \f[I]a\f[R] cannot be negative. .PP \f[I]a\f[R] is consumed; it cannot be used after the call. See the \f[B]Consumption and Propagation\f[R] subsection below. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_NEGATIVE\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclNumber bcl_sqrt_keep(BclNumber\f[R] \f[I]a\f[R]\f[B])\f[R] Calculates the square root of \f[I]a\f[R] and returns the result. The \f[I]scale\f[R] of the result is equal to the \f[B]scale\f[R] of the current context. .RS .PP \f[I]a\f[R] cannot be negative. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_NEGATIVE\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclError bcl_divmod(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B], BclNumber *\f[R]\f[I]c\f[R]\f[B], BclNumber *\f[R]\f[I]d\f[R]\f[B])\f[R] Divides \f[I]a\f[R] by \f[I]b\f[R] and returns the quotient in a new number which is put into the space pointed to by \f[I]c\f[R], and puts the modulus in a new number which is put into the space pointed to by \f[I]d\f[R]. .RS .PP \f[I]b\f[R] cannot be \f[B]0\f[R]. .PP \f[I]a\f[R] and \f[I]b\f[R] are consumed; they cannot be used after the call. See the \f[B]Consumption and Propagation\f[R] subsection below. .PP \f[I]c\f[R] and \f[I]d\f[R] cannot point to the same place, nor can they point to the space occupied by \f[I]a\f[R] or \f[I]b\f[R]. .PP If there was no error, \f[B]BCL_ERROR_NONE\f[R] is returned. Otherwise, this function can return: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_DIVIDE_BY_ZERO\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclError bcl_divmod_keep(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B], BclNumber *\f[R]\f[I]c\f[R]\f[B], BclNumber *\f[R]\f[I]d\f[R]\f[B])\f[R] Divides \f[I]a\f[R] by \f[I]b\f[R] and returns the quotient in a new number which is put into the space pointed to by \f[I]c\f[R], and puts the modulus in a new number which is put into the space pointed to by \f[I]d\f[R]. .RS .PP \f[I]b\f[R] cannot be \f[B]0\f[R]. .PP \f[I]c\f[R] and \f[I]d\f[R] cannot point to the same place, nor can they point to the space occupied by \f[I]a\f[R] or \f[I]b\f[R]. .PP If there was no error, \f[B]BCL_ERROR_NONE\f[R] is returned. Otherwise, this function can return: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_DIVIDE_BY_ZERO\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclNumber bcl_modexp(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B], BclNumber\f[R] \f[I]c\f[R]\f[B])\f[R] Computes a modular exponentiation where \f[I]a\f[R] is the base, \f[I]b\f[R] is the exponent, and \f[I]c\f[R] is the modulus, and returns the result. The \f[I]scale\f[R] of the result is equal to the \f[B]scale\f[R] of the current context. .RS .PP \f[I]a\f[R], \f[I]b\f[R], and \f[I]c\f[R] must be integers. \f[I]c\f[R] must not be \f[B]0\f[R]. \f[I]b\f[R] must not be negative. .PP \f[I]a\f[R], \f[I]b\f[R], and \f[I]c\f[R] are consumed; they cannot be used after the call. See the \f[B]Consumption and Propagation\f[R] subsection below. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_NEGATIVE\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_NON_INTEGER\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_DIVIDE_BY_ZERO\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclNumber bcl_modexp_keep(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B], BclNumber\f[R] \f[I]c\f[R]\f[B])\f[R] Computes a modular exponentiation where \f[I]a\f[R] is the base, \f[I]b\f[R] is the exponent, and \f[I]c\f[R] is the modulus, and returns the result. The \f[I]scale\f[R] of the result is equal to the \f[B]scale\f[R] of the current context. .RS .PP \f[I]a\f[R], \f[I]b\f[R], and \f[I]c\f[R] must be integers. \f[I]c\f[R] must not be \f[B]0\f[R]. \f[I]b\f[R] must not be negative. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_NEGATIVE\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_NON_INTEGER\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_DIVIDE_BY_ZERO\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .SS Miscellaneous .TP \f[B]void bcl_zero(BclNumber\f[R] \f[I]n\f[R]\f[B])\f[R] Sets \f[I]n\f[R] to \f[B]0\f[R]. .TP \f[B]void bcl_one(BclNumber\f[R] \f[I]n\f[R]\f[B])\f[R] Sets \f[I]n\f[R] to \f[B]1\f[R]. .TP \f[B]ssize_t bcl_cmp(BclNumber\f[R] \f[I]a\f[R]\f[B], BclNumber\f[R] \f[I]b\f[R]\f[B])\f[R] Compares \f[I]a\f[R] and \f[I]b\f[R] and returns \f[B]0\f[R] if \f[I]a\f[R] and \f[I]b\f[R] are equal, \f[B]<0\f[R] if \f[I]a\f[R] is less than \f[I]b\f[R], and \f[B]>0\f[R] if \f[I]a\f[R] is greater than \f[I]b\f[R]. .TP \f[B]BclError bcl_copy(BclNumber\f[R] \f[I]d\f[R]\f[B], BclNumber\f[R] \f[I]s\f[R]\f[B])\f[R] Copies \f[I]s\f[R] into \f[I]d\f[R]. .RS .PP If there was no error, \f[B]BCL_ERROR_NONE\f[R] is returned. Otherwise, this function can return: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclNumber bcl_dup(BclNumber\f[R] \f[I]s\f[R]\f[B])\f[R] Creates and returns a new \f[B]BclNumber\f[R] that is a copy of \f[I]s\f[R]. .RS .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .SS Pseudo-Random Number Generator -.PP The pseudo-random number generator in bcl(3) is a \f[I]seeded\f[R] PRNG. Given the same seed twice, it will produce the same sequence of pseudo-random numbers twice. .PP By default, bcl(3) attempts to seed the PRNG with data from \f[B]/dev/urandom\f[R]. If that fails, it seeds itself with by calling \f[B]libc\f[R]\[cq]s \f[B]srand(time(NULL))\f[R] and then calling \f[B]rand()\f[R] for each byte, since \f[B]rand()\f[R] is only guaranteed to return \f[B]15\f[R] bits. .PP This should provide fairly good seeding in the standard case while also remaining fairly portable. .PP If necessary, the PRNG can be reseeded with one of the following functions: .IP \[bu] 2 \f[B]bcl_rand_seedWithNum(BclNumber)\f[R] .IP \[bu] 2 \f[B]bcl_rand_seed(unsigned char[\f[R]\f[I]BCL_SEED_SIZE\f[R]\f[B]])\f[R] .IP \[bu] 2 \f[B]bcl_rand_reseed(\f[R]\f[I]void\f[R]\f[B])\f[R] .PP All procedures in this section without the \f[B]_keep\f[R] suffix in their name consume the given \f[B]BclNumber\f[R] arguments that are not given to pointer arguments. See the \f[B]Consumption and Propagation\f[R] subsection below. .PP The following items allow clients to use the pseudo-random number generator. All procedures require a valid current context. .TP \f[B]BCL_SEED_ULONGS\f[R] The number of \f[B]unsigned long\f[R]\[cq]s in a seed for bcl(3)\[cq]s random number generator. .TP \f[B]BCL_SEED_SIZE\f[R] The size, in \f[B]char\f[R]\[cq]s, of a seed for bcl(3)\[cq]s random number generator. .TP \f[B]BclBigDig\f[R] bcl(3)\[cq]s overflow type (see the \f[B]PERFORMANCE\f[R] section). .TP \f[B]BclRandInt\f[R] An unsigned integer type returned by bcl(3)\[cq]s random number generator. .TP \f[B]BclNumber bcl_irand(BclNumber\f[R] \f[I]a\f[R]\f[B])\f[R] Returns a random number that is not larger than \f[I]a\f[R] in a new number. If \f[I]a\f[R] is \f[B]0\f[R] or \f[B]1\f[R], the new number is equal to \f[B]0\f[R]. The bound is unlimited, so it is not bound to the size of \f[B]BclRandInt\f[R]. This is done by generating as many random numbers as necessary, multiplying them by certain exponents, and adding them all together. .RS .PP \f[I]a\f[R] must be an integer and non-negative. .PP \f[I]a\f[R] is consumed; it cannot be used after the call. See the \f[B]Consumption and Propagation\f[R] subsection below. .PP This procedure requires a valid current context. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_NEGATIVE\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_NON_INTEGER\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclNumber bcl_irand_keep(BclNumber\f[R] \f[I]a\f[R]\f[B])\f[R] Returns a random number that is not larger than \f[I]a\f[R] in a new number. If \f[I]a\f[R] is \f[B]0\f[R] or \f[B]1\f[R], the new number is equal to \f[B]0\f[R]. The bound is unlimited, so it is not bound to the size of \f[B]BclRandInt\f[R]. This is done by generating as many random numbers as necessary, multiplying them by certain exponents, and adding them all together. .RS .PP \f[I]a\f[R] must be an integer and non-negative. .PP This procedure requires a valid current context. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_NEGATIVE\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_NON_INTEGER\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclNumber bcl_frand(size_t\f[R] \f[I]places\f[R]\f[B])\f[R] Returns a random number between \f[B]0\f[R] (inclusive) and \f[B]1\f[R] (exclusive) that has \f[I]places\f[R] decimal digits after the radix (decimal point). There are no limits on \f[I]places\f[R]. .RS .PP This procedure requires a valid current context. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclNumber bcl_ifrand(BclNumber\f[R] \f[I]a\f[R]\f[B], size_t\f[R] \f[I]places\f[R]\f[B])\f[R] Returns a random number less than \f[I]a\f[R] with \f[I]places\f[R] decimal digits after the radix (decimal point). There are no limits on \f[I]a\f[R] or \f[I]places\f[R]. .RS .PP \f[I]a\f[R] must be an integer and non-negative. .PP \f[I]a\f[R] is consumed; it cannot be used after the call. See the \f[B]Consumption and Propagation\f[R] subsection below. .PP This procedure requires a valid current context. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_NEGATIVE\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_NON_INTEGER\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclNumber bcl_ifrand_keep(BclNumber\f[R] \f[I]a\f[R]\f[B], size_t\f[R] \f[I]places\f[R]\f[B])\f[R] Returns a random number less than \f[I]a\f[R] with \f[I]places\f[R] decimal digits after the radix (decimal point). There are no limits on \f[I]a\f[R] or \f[I]places\f[R]. .RS .PP \f[I]a\f[R] must be an integer and non-negative. .PP This procedure requires a valid current context. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_NEGATIVE\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_MATH_NON_INTEGER\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclError bcl_rand_seedWithNum(BclNumber\f[R] \f[I]n\f[R]\f[B])\f[R] Seeds the PRNG with \f[I]n\f[R]. .RS .PP \f[I]n\f[R] is consumed. .PP This procedure requires a valid current context. .PP If there was no error, \f[B]BCL_ERROR_NONE\f[R] is returned. Otherwise, this function can return: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .PP Note that if \f[B]bcl_rand_seed2num(\f[R]\f[I]void\f[R]\f[B])\f[R] or \f[B]bcl_rand_seed2num_err(BclNumber)\f[R] are called right after this function, they are not guaranteed to return a number equal to \f[I]n\f[R]. .RE .TP \f[B]BclError bcl_rand_seedWithNum_keep(BclNumber\f[R] \f[I]n\f[R]\f[B])\f[R] Seeds the PRNG with \f[I]n\f[R]. .RS .PP This procedure requires a valid current context. .PP If there was no error, \f[B]BCL_ERROR_NONE\f[R] is returned. Otherwise, this function can return: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_NUM\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .PP Note that if \f[B]bcl_rand_seed2num(\f[R]\f[I]void\f[R]\f[B])\f[R] or \f[B]bcl_rand_seed2num_err(BclNumber)\f[R] are called right after this function, they are not guaranteed to return a number equal to \f[I]n\f[R]. .RE .TP \f[B]BclError bcl_rand_seed(unsigned char\f[R] \f[I]seed\f[R]\f[B][\f[R]\f[I]BCL_SEED_SIZE\f[R]\f[B]])\f[R] Seeds the PRNG with the bytes in \f[I]seed\f[R]. .RS .PP If there was no error, \f[B]BCL_ERROR_NONE\f[R] is returned. Otherwise, this function can return: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .RE .TP \f[B]void bcl_rand_reseed(\f[R]\f[I]void\f[R]\f[B])\f[R] Reseeds the PRNG with the default reseeding behavior. First, it attempts to read data from \f[B]/dev/urandom\f[R] and falls back to \f[B]libc\f[R]\[cq]s \f[B]rand()\f[R]. .RS .PP This procedure cannot fail. .RE .TP \f[B]BclNumber bcl_rand_seed2num(\f[R]\f[I]void\f[R]\f[B])\f[R] Returns the current seed of the PRNG as a \f[B]BclNumber\f[R]. .RS .PP This procedure requires a valid current context. .PP bcl(3) will encode an error in the return value, if there was one. The error can be queried with \f[B]bcl_err(BclNumber)\f[R]. Possible errors include: .IP \[bu] 2 \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] .IP \[bu] 2 \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] .RE .TP \f[B]BclRandInt bcl_rand_int(\f[R]\f[I]void\f[R]\f[B])\f[R] Returns a random integer between \f[B]0\f[R] and \f[B]BC_RAND_MAX\f[R] (inclusive). .RS .PP This procedure cannot fail. .RE .TP \f[B]BclRandInt bcl_rand_bounded(BclRandInt\f[R] \f[I]bound\f[R]\f[B])\f[R] Returns a random integer between \f[B]0\f[R] and \f[I]bound\f[R] (exclusive). Bias is removed before returning the integer. .RS .PP This procedure cannot fail. .RE .SS Consumption and Propagation -.PP Some functions are listed as consuming some or all of their arguments. This means that the arguments are freed, regardless of if there were errors or not. .PP This is to enable compact code like the following: .IP -.nf -\f[C] +.EX BclNumber n = bcl_num_add(bcl_num_mul(a, b), bcl_num_div(c, d)); -\f[R] -.fi +.EE .PP If arguments to those functions were not consumed, memory would be leaked until reclaimed with \f[B]bcl_ctxt_freeNums(BclContext)\f[R]. .PP When errors occur, they are propagated through. The result should always be checked with \f[B]bcl_err(BclNumber)\f[R], so the example above should properly be: .IP -.nf -\f[C] +.EX BclNumber n = bcl_num_add(bcl_num_mul(a, b), bcl_num_div(c, d)); if (bcl_err(n) != BCL_ERROR_NONE) { // Handle the error. } -\f[R] -.fi +.EE .SH ERRORS -.PP Most functions in bcl(3) return, directly or indirectly, any one of the error codes defined in \f[B]BclError\f[R]. The complete list of codes is the following: .TP \f[B]BCL_ERROR_NONE\f[R] Success; no error occurred. .TP \f[B]BCL_ERROR_INVALID_NUM\f[R] An invalid \f[B]BclNumber\f[R] was given as a parameter. .TP \f[B]BCL_ERROR_INVALID_CONTEXT\f[R] An invalid \f[B]BclContext\f[R] is being used. .TP \f[B]BCL_ERROR_MATH_NEGATIVE\f[R] A negative number was given as an argument to a parameter that cannot accept negative numbers, such as for square roots. .TP \f[B]BCL_ERROR_MATH_NON_INTEGER\f[R] A non-integer was given as an argument to a parameter that cannot accept non-integer numbers, such as for the second parameter of \f[B]bcl_num_pow()\f[R]. .TP \f[B]BCL_ERROR_MATH_OVERFLOW\f[R] A number that would overflow its result was given as an argument, such as for converting a \f[B]BclNumber\f[R] to a \f[B]BclBigDig\f[R]. .TP \f[B]BCL_ERROR_MATH_DIVIDE_BY_ZERO\f[R] A divide by zero occurred. .TP \f[B]BCL_ERROR_PARSE_INVALID_STR\f[R] An invalid number string was passed to a parsing function. .RS .PP A valid number string can only be one radix (period). In addition, any lowercase ASCII letters, symbols, or non-ASCII characters are invalid. It is allowed for the first character to be a dash. In that case, the number is considered to be negative. .PP There is one exception to the above: one lowercase \f[B]e\f[R] is allowed in the number, after the radix, if it exists. If the letter \f[B]e\f[R] exists, the number is considered to be in scientific notation, where the part before the \f[B]e\f[R] is the number, and the part after, which must be an integer, is the exponent. There can be a dash right after the \f[B]e\f[R] to indicate a negative exponent. .PP \f[B]WARNING\f[R]: Both the number and the exponent in scientific notation are interpreted according to the current \f[B]ibase\f[R], but the number is still multiplied by \f[B]10\[ha]exponent\f[R] regardless of the current \f[B]ibase\f[R]. For example, if \f[B]ibase\f[R] is \f[B]16\f[R] and bcl(3) is given the number string \f[B]FFeA\f[R], the resulting decimal number will be \f[B]2550000000000\f[R], and if bcl(3) is given the number string \f[B]10e-4\f[R], the resulting decimal number will be \f[B]0.0016\f[R]. .RE .TP \f[B]BCL_ERROR_FATAL_ALLOC_ERR\f[R] bcl(3) failed to allocate memory. .RS .PP If clients call \f[B]bcl_setAbortOnFatalError()\f[R] with an \f[B]true\f[R] argument, this error will cause bcl(3) to throw a \f[B]SIGABRT\f[R]. This behavior can also be turned off later by calling that same function with a \f[B]false\f[R] argument. By default, this behavior is off. .PP It is highly recommended that client libraries do \f[I]not\f[R] activate this behavior. .RE .TP \f[B]BCL_ERROR_FATAL_UNKNOWN_ERR\f[R] An unknown error occurred. .RS .PP If clients call \f[B]bcl_setAbortOnFatalError()\f[R] with an \f[B]true\f[R] argument, this error will cause bcl(3) to throw a \f[B]SIGABRT\f[R]. This behavior can also be turned off later by calling that same function with a \f[B]false\f[R] argument. By default, this behavior is off. .PP It is highly recommended that client libraries do \f[I]not\f[R] activate this behavior. .RE .SH ATTRIBUTES -.PP bcl(3) is \f[I]MT-Safe\f[R]: it is safe to call any functions from more than one thread. However, is is \f[I]not\f[R] safe to pass any data between threads except for strings returned by \f[B]bcl_string()\f[R]. .PP bcl(3) is not \f[I]async-signal-safe\f[R]. It was not possible to make bcl(3) safe with signals and also make it safe with multiple threads. If it is necessary to be able to interrupt bcl(3), spawn a separate thread to run the calculation. .SH PERFORMANCE -.PP Most bc(1) implementations use \f[B]char\f[R] types to calculate the value of \f[B]1\f[R] decimal digit at a time, but that can be slow. bcl(3) does something different. .PP It uses large integers to calculate more than \f[B]1\f[R] decimal digit at a time. If built in a environment where \f[B]BC_LONG_BIT\f[R] (see the \f[B]LIMITS\f[R] section) is \f[B]64\f[R], then each integer has \f[B]9\f[R] decimal digits. If built in an environment where \f[B]BC_LONG_BIT\f[R] is \f[B]32\f[R] then each integer has \f[B]4\f[R] decimal digits. This value (the number of decimal digits per large integer) is called \f[B]BC_BASE_DIGS\f[R]. .PP In addition, this bcl(3) uses an even larger integer for overflow checking. This integer type depends on the value of \f[B]BC_LONG_BIT\f[R], but is always at least twice as large as the integer type used to store digits. .SH LIMITS -.PP The following are the limits on bcl(3): .TP \f[B]BC_LONG_BIT\f[R] The number of bits in the \f[B]long\f[R] type in the environment where bcl(3) was built. This determines how many decimal digits can be stored in a single large integer (see the \f[B]PERFORMANCE\f[R] section). .TP \f[B]BC_BASE_DIGS\f[R] The number of decimal digits per large integer (see the \f[B]PERFORMANCE\f[R] section). Depends on \f[B]BC_LONG_BIT\f[R]. .TP \f[B]BC_BASE_POW\f[R] The max decimal number that each large integer can store (see \f[B]BC_BASE_DIGS\f[R]) plus \f[B]1\f[R]. Depends on \f[B]BC_BASE_DIGS\f[R]. .TP \f[B]BC_OVERFLOW_MAX\f[R] The max number that the overflow type (see the \f[B]PERFORMANCE\f[R] section) can hold. Depends on \f[B]BC_LONG_BIT\f[R]. .TP \f[B]BC_BASE_MAX\f[R] The maximum output base. Set at \f[B]BC_BASE_POW\f[R]. .TP \f[B]BC_SCALE_MAX\f[R] The maximum \f[B]scale\f[R]. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_NUM_MAX\f[R] The maximum length of a number (in decimal digits), which includes digits after the decimal point. Set at \f[B]BC_OVERFLOW_MAX-1\f[R]. .TP \f[B]BC_RAND_MAX\f[R] The maximum integer (inclusive) returned by the \f[B]bcl_rand_int()\f[R] function. Set at \f[B]2\[ha]BC_LONG_BIT-1\f[R]. .TP Exponent The maximum allowable exponent (positive or negative). Set at \f[B]BC_OVERFLOW_MAX\f[R]. .PP These limits are meant to be effectively non-existent; the limits are so large (at least on 64-bit machines) that there should not be any point at which they become a problem. In fact, memory should be exhausted before these limits should be hit. .SH SEE ALSO -.PP bc(1) and dc(1) .SH STANDARDS -.PP bcl(3) is compliant with the arithmetic defined in the IEEE Std 1003.1-2017 (\[lq]POSIX.1-2017\[rq]) specification at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html for bc(1). .PP Note that the specification explicitly says that bc(1) only accepts numbers that use a period (\f[B].\f[R]) as a radix point, regardless of the value of \f[B]LC_NUMERIC\f[R]. This is also true of bcl(3). .SH BUGS -.PP None are known. Report bugs at https://git.gavinhoward.com/gavin/bc. .SH AUTHORS -.PP -Gavin D. -Howard and contributors. +Gavin D. Howard \c +.MT gavin@gavinhoward.com +.ME \c +\ and contributors. diff --git a/contrib/bc/manuals/dc/A.1 b/contrib/bc/manuals/dc/A.1 index bef54876d7f2..7de20035f43f 100644 --- a/contrib/bc/manuals/dc/A.1 +++ b/contrib/bc/manuals/dc/A.1 @@ -1,1727 +1,1690 @@ .\" .\" SPDX-License-Identifier: BSD-2-Clause .\" .\" Copyright (c) 2018-2023 Gavin D. Howard and contributors. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions are met: .\" .\" * Redistributions of source code must retain the above copyright notice, .\" this list of conditions and the following disclaimer. .\" .\" * Redistributions in binary form must reproduce the above copyright notice, .\" this list of conditions and the following disclaimer in the documentation .\" and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" .TH "DC" "1" "February 2023" "Gavin D. Howard" "General Commands Manual" .nh .ad l .SH Name -.PP dc - arbitrary-precision decimal reverse-Polish notation calculator .SH SYNOPSIS -.PP \f[B]dc\f[R] [\f[B]-cChiPRvVx\f[R]] [\f[B]--version\f[R]] [\f[B]--help\f[R]] [\f[B]--digit-clamp\f[R]] [\f[B]--no-digit-clamp\f[R]] [\f[B]--interactive\f[R]] [\f[B]--no-prompt\f[R]] [\f[B]--no-read-prompt\f[R]] [\f[B]--extended-register\f[R]] [\f[B]-e\f[R] \f[I]expr\f[R]] [\f[B]--expression\f[R]=\f[I]expr\f[R]\&...] [\f[B]-f\f[R] \f[I]file\f[R]\&...] [\f[B]--file\f[R]=\f[I]file\f[R]\&...] [\f[I]file\f[R]\&...] [\f[B]-I\f[R] \f[I]ibase\f[R]] [\f[B]--ibase\f[R]=\f[I]ibase\f[R]] [\f[B]-O\f[R] \f[I]obase\f[R]] [\f[B]--obase\f[R]=\f[I]obase\f[R]] [\f[B]-S\f[R] \f[I]scale\f[R]] [\f[B]--scale\f[R]=\f[I]scale\f[R]] [\f[B]-E\f[R] \f[I]seed\f[R]] [\f[B]--seed\f[R]=\f[I]seed\f[R]] .SH DESCRIPTION -.PP dc(1) is an arbitrary-precision calculator. It uses a stack (reverse Polish notation) to store numbers and results of computations. Arithmetic operations pop arguments off of the stack and push the results. .PP If no files are given on the command-line, then dc(1) reads from \f[B]stdin\f[R] (see the \f[B]STDIN\f[R] section). Otherwise, those files are processed, and dc(1) will then exit. .PP If a user wants to set up a standard environment, they can use \f[B]DC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). For example, if a user wants the \f[B]scale\f[R] always set to \f[B]10\f[R], they can set \f[B]DC_ENV_ARGS\f[R] to \f[B]-e 10k\f[R], and this dc(1) will always start with a \f[B]scale\f[R] of \f[B]10\f[R]. .SH OPTIONS -.PP The following are the options that dc(1) accepts. .TP \f[B]-C\f[R], \f[B]--no-digit-clamp\f[R] Disables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that the value added to a number from a digit is always that digit\[cq]s value multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-c\f[R] or \f[B]--digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]DC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-c\f[R], \f[B]--digit-clamp\f[R] Enables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that digits that the value added to a number from a digit that is greater than or equal to the ibase is the value of ibase minus 1 all multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-C\f[R] or \f[B]--no-digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]DC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-E\f[R] \f[I]seed\f[R], \f[B]--seed\f[R]=\f[I]seed\f[R] Sets the builtin variable \f[B]seed\f[R] to the value \f[I]seed\f[R] assuming that \f[I]seed\f[R] is in base 10. It is a fatal error if \f[I]seed\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-e\f[R] \f[I]expr\f[R], \f[B]--expression\f[R]=\f[I]expr\f[R] Evaluates \f[I]expr\f[R]. If multiple expressions are given, they are evaluated in order. If files are given as well (see below), the expressions and files are evaluated in the order given. This means that if a file is given before an expression, the file is read in and evaluated first. .RS .PP If this option is given on the command-line (i.e., not in \f[B]DC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, dc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R], whether on the command-line or in \f[B]DC_ENV_ARGS\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, dc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-f\f[R] \f[I]file\f[R], \f[B]--file\f[R]=\f[I]file\f[R] Reads in \f[I]file\f[R] and evaluates it, line by line, as though it were read through \f[B]stdin\f[R]. If expressions are also given (see above), the expressions are evaluated in the order given. .RS .PP If this option is given on the command-line (i.e., not in \f[B]DC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, dc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, dc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-h\f[R], \f[B]--help\f[R] Prints a usage message and exits. .TP \f[B]-I\f[R] \f[I]ibase\f[R], \f[B]--ibase\f[R]=\f[I]ibase\f[R] Sets the builtin variable \f[B]ibase\f[R] to the value \f[I]ibase\f[R] assuming that \f[I]ibase\f[R] is in base 10. It is a fatal error if \f[I]ibase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-i\f[R], \f[B]--interactive\f[R] Forces interactive mode. (See the \f[B]INTERACTIVE MODE\f[R] section.) .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-L\f[R], \f[B]--no-line-length\f[R] Disables line length checking and prints numbers without backslashes and newlines. In other words, this option sets \f[B]BC_LINE_LENGTH\f[R] to \f[B]0\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-O\f[R] \f[I]obase\f[R], \f[B]--obase\f[R]=\f[I]obase\f[R] Sets the builtin variable \f[B]obase\f[R] to the value \f[I]obase\f[R] assuming that \f[I]obase\f[R] is in base 10. It is a fatal error if \f[I]obase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-P\f[R], \f[B]--no-prompt\f[R] Disables the prompt in TTY mode. (The prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a prompt or are not used to having them in dc(1). Most of those users would want to put this option in \f[B]DC_ENV_ARGS\f[R]. .RS .PP These options override the \f[B]DC_PROMPT\f[R] and \f[B]DC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-R\f[R], \f[B]--no-read-prompt\f[R] Disables the read prompt in TTY mode. (The read prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a read prompt or are not used to having them in dc(1). Most of those users would want to put this option in \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). This option is also useful in hash bang lines of dc(1) scripts that prompt for user input. .RS .PP This option does not disable the regular prompt because the read prompt is only used when the \f[B]?\f[R] command is used. .PP These options \f[I]do\f[R] override the \f[B]DC_PROMPT\f[R] and \f[B]DC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), but only for the read prompt. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-S\f[R] \f[I]scale\f[R], \f[B]--scale\f[R]=\f[I]scale\f[R] Sets the builtin variable \f[B]scale\f[R] to the value \f[I]scale\f[R] assuming that \f[I]scale\f[R] is in base 10. It is a fatal error if \f[I]scale\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-v\f[R], \f[B]-V\f[R], \f[B]--version\f[R] Print the version information (copyright header) and exits. .TP \f[B]-x\f[R] \f[B]--extended-register\f[R] Enables extended register mode. See the \f[I]Extended Register Mode\f[R] subsection of the \f[B]REGISTERS\f[R] section for more information. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-z\f[R], \f[B]--leading-zeroes\f[R] Makes dc(1) print all numbers greater than \f[B]-1\f[R] and less than \f[B]1\f[R], and not equal to \f[B]0\f[R], with a leading zero. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .PP All long options are \f[B]non-portable extensions\f[R]. .SH STDIN -.PP If no files are given on the command-line and no files or expressions are given by the \f[B]-f\f[R], \f[B]--file\f[R], \f[B]-e\f[R], or \f[B]--expression\f[R] options, then dc(1) reads from \f[B]stdin\f[R]. .PP However, there is a caveat to this. .PP First, \f[B]stdin\f[R] is evaluated a line at a time. The only exception to this is if a string has been finished, but not ended. This means that, except for escaped brackets, all brackets must be balanced before dc(1) parses and executes. .SH STDOUT -.PP Any non-error output is written to \f[B]stdout\f[R]. In addition, if history (see the \f[B]HISTORY\f[R] section) and the prompt (see the \f[B]TTY MODE\f[R] section) are enabled, both are output to \f[B]stdout\f[R]. .PP \f[B]Note\f[R]: Unlike other dc(1) implementations, this dc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stdout\f[R], so if \f[B]stdout\f[R] is closed, as in -\f[B]dc >&-\f[R], it will quit with an error. +\f[B]dc >&-\f[R], it will quit with an error. This is done so that dc(1) can report problems when \f[B]stdout\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other dc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stdout\f[R] to \f[B]/dev/null\f[R]. .SH STDERR -.PP Any error output is written to \f[B]stderr\f[R]. .PP \f[B]Note\f[R]: Unlike other dc(1) implementations, this dc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stderr\f[R], so if \f[B]stderr\f[R] is closed, as in -\f[B]dc 2>&-\f[R], it will quit with an error. +\f[B]dc 2>&-\f[R], it will quit with an error. This is done so that dc(1) can exit with an error code when \f[B]stderr\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other dc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stderr\f[R] to \f[B]/dev/null\f[R]. .SH SYNTAX -.PP Each item in the input source code, either a number (see the \f[B]NUMBERS\f[R] section) or a command (see the \f[B]COMMANDS\f[R] section), is processed and executed, in order. Input is processed immediately when entered. .PP \f[B]ibase\f[R] is a register (see the \f[B]REGISTERS\f[R] section) that determines how to interpret constant numbers. It is the \[lq]input\[rq] base, or the number base used for interpreting input numbers. \f[B]ibase\f[R] is initially \f[B]10\f[R]. The max allowable value for \f[B]ibase\f[R] is \f[B]16\f[R]. The min allowable value for \f[B]ibase\f[R] is \f[B]2\f[R]. The max allowable value for \f[B]ibase\f[R] can be queried in dc(1) programs with the \f[B]T\f[R] command. .PP \f[B]obase\f[R] is a register (see the \f[B]REGISTERS\f[R] section) that determines how to output results. It is the \[lq]output\[rq] base, or the number base used for outputting numbers. \f[B]obase\f[R] is initially \f[B]10\f[R]. The max allowable value for \f[B]obase\f[R] is \f[B]DC_BASE_MAX\f[R] and can be queried with the \f[B]U\f[R] command. The min allowable value for \f[B]obase\f[R] is \f[B]0\f[R]. If \f[B]obase\f[R] is \f[B]0\f[R], values are output in scientific notation, and if \f[B]obase\f[R] is \f[B]1\f[R], values are output in engineering notation. Otherwise, values are output in the specified base. .PP Outputting in scientific and engineering notations are \f[B]non-portable extensions\f[R]. .PP The \f[I]scale\f[R] of an expression is the number of digits in the result of the expression right of the decimal point, and \f[B]scale\f[R] is a register (see the \f[B]REGISTERS\f[R] section) that sets the precision of any operations (with exceptions). \f[B]scale\f[R] is initially \f[B]0\f[R]. \f[B]scale\f[R] cannot be negative. The max allowable value for \f[B]scale\f[R] can be queried in dc(1) programs with the \f[B]V\f[R] command. .PP \f[B]seed\f[R] is a register containing the current seed for the pseudo-random number generator. If the current value of \f[B]seed\f[R] is queried and stored, then if it is assigned to \f[B]seed\f[R] later, the pseudo-random number generator is guaranteed to produce the same sequence of pseudo-random numbers that were generated after the value of \f[B]seed\f[R] was first queried. .PP Multiple values assigned to \f[B]seed\f[R] can produce the same sequence of pseudo-random numbers. Likewise, when a value is assigned to \f[B]seed\f[R], it is not guaranteed that querying \f[B]seed\f[R] immediately after will return the same value. In addition, the value of \f[B]seed\f[R] will change after any call to the \f[B]\[cq]\f[R] command or the \f[B]\[lq]\f[R] command that does not get receive a value of \f[B]0\f[R] or \f[B]1\f[R]. The maximum integer returned by the \f[B]\[cq]\f[R] command can be queried with the \f[B]W\f[R] command. .PP \f[B]Note\f[R]: The values returned by the pseudo-random number generator with the \f[B]\[cq]\f[R] and \f[B]\[lq]\f[R] commands are guaranteed to \f[B]NOT\f[R] be cryptographically secure. This is a consequence of using a seeded pseudo-random number generator. However, they \f[I]are\f[R] guaranteed to be reproducible with identical \f[B]seed\f[R] values. This means that the pseudo-random values from dc(1) should only be used where a reproducible stream of pseudo-random numbers is \f[I]ESSENTIAL\f[R]. In any other case, use a non-seeded pseudo-random number generator. .PP The pseudo-random number generator, \f[B]seed\f[R], and all associated operations are \f[B]non-portable extensions\f[R]. .SS Comments -.PP Comments go from \f[B]#\f[R] until, and not including, the next newline. This is a \f[B]non-portable extension\f[R]. .SH NUMBERS -.PP Numbers are strings made up of digits, uppercase letters up to \f[B]F\f[R], and at most \f[B]1\f[R] period for a radix. Numbers can have up to \f[B]DC_NUM_MAX\f[R] digits. Uppercase letters are equal to \f[B]9\f[R] plus their position in the alphabet (i.e., \f[B]A\f[R] equals \f[B]10\f[R], or \f[B]9+1\f[R]). .PP If a digit or letter makes no sense with the current value of \f[B]ibase\f[R] (i.e., they are greater than or equal to the current value of \f[B]ibase\f[R]), then the behavior depends on the existence of the \f[B]-c\f[R]/\f[B]--digit-clamp\f[R] or \f[B]-C\f[R]/\f[B]--no-digit-clamp\f[R] options (see the \f[B]OPTIONS\f[R] section), the existence and setting of the \f[B]DC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or the default, which can be queried with the \f[B]-h\f[R]/\f[B]--help\f[R] option. .PP If clamping is off, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are not changed. Instead, their given value is multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*A+3\[ha]0*B\f[R], which is \f[B]3\f[R] times \f[B]10\f[R] plus \f[B]11\f[R], or \f[B]41\f[R]. .PP If clamping is on, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are set to the value of the highest valid digit in \f[B]ibase\f[R] before being multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*2+3\[ha]0*2\f[R], which is \f[B]3\f[R] times \f[B]2\f[R] plus \f[B]2\f[R], or \f[B]8\f[R]. .PP There is one exception to clamping: single-character numbers (i.e., \f[B]A\f[R] alone). Such numbers are never clamped and always take the value they would have in the highest possible \f[B]ibase\f[R]. This means that \f[B]A\f[R] alone always equals decimal \f[B]10\f[R] and \f[B]Z\f[R] alone always equals decimal \f[B]35\f[R]. This behavior is mandated by the standard for bc(1) (see the STANDARDS section) and is meant to provide an easy way to set the current \f[B]ibase\f[R] (with the \f[B]i\f[R] command) regardless of the current value of \f[B]ibase\f[R]. .PP If clamping is on, and the clamped value of a character is needed, use a leading zero, i.e., for \f[B]A\f[R], use \f[B]0A\f[R]. .PP In addition, dc(1) accepts numbers in scientific notation. These have the form \f[B]e\f[R]. The exponent (the portion after the \f[B]e\f[R]) must be an integer. An example is \f[B]1.89237e9\f[R], which is equal to \f[B]1892370000\f[R]. Negative exponents are also allowed, so \f[B]4.2890e_3\f[R] is equal to \f[B]0.0042890\f[R]. .PP \f[B]WARNING\f[R]: Both the number and the exponent in scientific notation are interpreted according to the current \f[B]ibase\f[R], but the number is still multiplied by \f[B]10\[ha]exponent\f[R] regardless of the current \f[B]ibase\f[R]. For example, if \f[B]ibase\f[R] is \f[B]16\f[R] and dc(1) is given the number string \f[B]FFeA\f[R], the resulting decimal number will be \f[B]2550000000000\f[R], and if dc(1) is given the number string \f[B]10e_4\f[R], the resulting decimal number will be \f[B]0.0016\f[R]. .PP Accepting input as scientific notation is a \f[B]non-portable extension\f[R]. .SH COMMANDS -.PP The valid commands are listed below. .SS Printing -.PP These commands are used for printing. .PP Note that both scientific notation and engineering notation are available for printing numbers. Scientific notation is activated by assigning \f[B]0\f[R] to \f[B]obase\f[R] using \f[B]0o\f[R], and engineering notation is activated by assigning \f[B]1\f[R] to \f[B]obase\f[R] using \f[B]1o\f[R]. To deactivate them, just assign a different value to \f[B]obase\f[R]. .PP Printing numbers in scientific notation and/or engineering notation is a \f[B]non-portable extension\f[R]. .TP \f[B]p\f[R] Prints the value on top of the stack, whether number or string, and prints a newline after. .RS .PP This does not alter the stack. .RE .TP \f[B]n\f[R] Prints the value on top of the stack, whether number or string, and pops it off of the stack. .TP \f[B]P\f[R] Pops a value off the stack. .RS .PP If the value is a number, it is truncated and the absolute value of the result is printed as though \f[B]obase\f[R] is \f[B]256\f[R] and each digit is interpreted as an 8-bit ASCII character, making it a byte stream. .PP If the value is a string, it is printed without a trailing newline. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]f\f[R] Prints the entire contents of the stack, in order from newest to oldest, without altering anything. .RS .PP Users should use this command when they get lost. .RE .SS Arithmetic -.PP These are the commands used for arithmetic. .TP \f[B]+\f[R] The top two values are popped off the stack, added, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to the max \f[I]scale\f[R] of both operands. .TP \f[B]-\f[R] The top two values are popped off the stack, subtracted, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to the max \f[I]scale\f[R] of both operands. .TP \f[B]*\f[R] The top two values are popped off the stack, multiplied, and the result is pushed onto the stack. If \f[B]a\f[R] is the \f[I]scale\f[R] of the first expression and \f[B]b\f[R] is the \f[I]scale\f[R] of the second expression, the \f[I]scale\f[R] of the result is equal to \f[B]min(a+b,max(scale,a,b))\f[R] where \f[B]min()\f[R] and \f[B]max()\f[R] return the obvious values. .TP \f[B]/\f[R] The top two values are popped off the stack, divided, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The first value popped off of the stack must be non-zero. .RE .TP \f[B]%\f[R] The top two values are popped off the stack, remaindered, and the result is pushed onto the stack. .RS .PP Remaindering is equivalent to 1) Computing \f[B]a/b\f[R] to current \f[B]scale\f[R], and 2) Using the result of step 1 to calculate \f[B]a-(a/b)*b\f[R] to \f[I]scale\f[R] \f[B]max(scale+scale(b),scale(a))\f[R]. .PP The first value popped off of the stack must be non-zero. .RE .TP \f[B]\[ti]\f[R] The top two values are popped off the stack, divided and remaindered, and the results (divided first, remainder second) are pushed onto the stack. This is equivalent to \f[B]x y / x y %\f[R] except that \f[B]x\f[R] and \f[B]y\f[R] are only evaluated once. .RS .PP The first value popped off of the stack must be non-zero. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[ha]\f[R] The top two values are popped off the stack, the second is raised to the power of the first, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The first value popped off of the stack must be an integer, and if that value is negative, the second value popped off of the stack must be non-zero. .RE .TP \f[B]v\f[R] The top value is popped off the stack, its square root is computed, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The value popped off of the stack must be non-negative. .RE .TP \f[B]_\f[R] If this command \f[I]immediately\f[R] precedes a number (i.e., no spaces or other commands), then that number is input as a negative number. .RS .PP Otherwise, the top value on the stack is popped and copied, and the copy is negated and pushed onto the stack. This behavior without a number is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]b\f[R] The top value is popped off the stack, and if it is zero, it is pushed back onto the stack. Otherwise, its absolute value is pushed onto the stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]|\f[R] The top three values are popped off the stack, a modular exponentiation is computed, and the result is pushed onto the stack. .RS .PP The first value popped is used as the reduction modulus and must be an integer and non-zero. The second value popped is used as the exponent and must be an integer and non-negative. The third value popped is the base and must be an integer. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]$\f[R] The top value is popped off the stack and copied, and the copy is truncated and pushed onto the stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[at]\f[R] The top two values are popped off the stack, and the precision of the second is set to the value of the first, whether by truncation or extension. .RS .PP The first value popped off of the stack must be an integer and non-negative. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]H\f[R] The top two values are popped off the stack, and the second is shifted left (radix shifted right) to the value of the first. .RS .PP The first value popped off of the stack must be an integer and non-negative. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]h\f[R] The top two values are popped off the stack, and the second is shifted right (radix shifted left) to the value of the first. .RS .PP The first value popped off of the stack must be an integer and non-negative. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]G\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if they are equal, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]N\f[R] The top value is popped off of the stack, and if it a \f[B]0\f[R], a \f[B]1\f[R] is pushed; otherwise, a \f[B]0\f[R] is pushed. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B](\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is less than the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]{\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is less than or equal to the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B])\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is greater than the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]}\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is greater than or equal to the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]M\f[R] The top two values are popped off of the stack. If they are both non-zero, a \f[B]1\f[R] is pushed onto the stack. If either of them is zero, or both of them are, then a \f[B]0\f[R] is pushed onto the stack. .RS .PP This is like the \f[B]&&\f[R] operator in bc(1), and it is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]m\f[R] The top two values are popped off of the stack. If at least one of them is non-zero, a \f[B]1\f[R] is pushed onto the stack. If both of them are zero, then a \f[B]0\f[R] is pushed onto the stack. .RS .PP This is like the \f[B]||\f[R] operator in bc(1), and it is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Pseudo-Random Number Generator -.PP dc(1) has a built-in pseudo-random number generator. These commands query the pseudo-random number generator. (See Parameters for more information about the \f[B]seed\f[R] value that controls the pseudo-random number generator.) .PP The pseudo-random number generator is guaranteed to \f[B]NOT\f[R] be cryptographically secure. .TP \f[B]\[cq]\f[R] Generates an integer between 0 and \f[B]DC_RAND_MAX\f[R], inclusive (see the \f[B]LIMITS\f[R] section). .RS .PP The generated integer is made as unbiased as possible, subject to the limitations of the pseudo-random number generator. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[lq]\f[R] Pops a value off of the stack, which is used as an \f[B]exclusive\f[R] upper bound on the integer that will be generated. If the bound is negative or is a non-integer, an error is raised, and dc(1) resets (see the \f[B]RESET\f[R] section) while \f[B]seed\f[R] remains unchanged. If the bound is larger than \f[B]DC_RAND_MAX\f[R], the higher bound is honored by generating several pseudo-random integers, multiplying them by appropriate powers of \f[B]DC_RAND_MAX+1\f[R], and adding them together. Thus, the size of integer that can be generated with this command is unbounded. Using this command will change the value of \f[B]seed\f[R], unless the operand is \f[B]0\f[R] or \f[B]1\f[R]. In that case, \f[B]0\f[R] is pushed onto the stack, and \f[B]seed\f[R] is \f[I]not\f[R] changed. .RS .PP The generated integer is made as unbiased as possible, subject to the limitations of the pseudo-random number generator. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Stack Control -.PP These commands control the stack. .TP \f[B]c\f[R] Removes all items from (\[lq]clears\[rq]) the stack. .TP \f[B]d\f[R] Copies the item on top of the stack (\[lq]duplicates\[rq]) and pushes the copy onto the stack. .TP \f[B]r\f[R] Swaps (\[lq]reverses\[rq]) the two top items on the stack. .TP \f[B]R\f[R] Pops (\[lq]removes\[rq]) the top value from the stack. .SS Register Control -.PP These commands control registers (see the \f[B]REGISTERS\f[R] section). .TP \f[B]s\f[R]\f[I]r\f[R] Pops the value off the top of the stack and stores it into register \f[I]r\f[R]. .TP \f[B]l\f[R]\f[I]r\f[R] Copies the value in register \f[I]r\f[R] and pushes it onto the stack. This does not alter the contents of \f[I]r\f[R]. .TP \f[B]S\f[R]\f[I]r\f[R] Pops the value off the top of the (main) stack and pushes it onto the stack of register \f[I]r\f[R]. The previous value of the register becomes inaccessible. .TP \f[B]L\f[R]\f[I]r\f[R] Pops the value off the top of the stack for register \f[I]r\f[R] and push it onto the main stack. The previous value in the stack for register \f[I]r\f[R], if any, is now accessible via the \f[B]l\f[R]\f[I]r\f[R] command. .SS Parameters -.PP These commands control the values of \f[B]ibase\f[R], \f[B]obase\f[R], \f[B]scale\f[R], and \f[B]seed\f[R]. Also see the \f[B]SYNTAX\f[R] section. .TP \f[B]i\f[R] Pops the value off of the top of the stack and uses it to set \f[B]ibase\f[R], which must be between \f[B]2\f[R] and \f[B]16\f[R], inclusive. .RS .PP If the value on top of the stack has any \f[I]scale\f[R], the \f[I]scale\f[R] is ignored. .RE .TP \f[B]o\f[R] Pops the value off of the top of the stack and uses it to set \f[B]obase\f[R], which must be between \f[B]0\f[R] and \f[B]DC_BASE_MAX\f[R], inclusive (see the \f[B]LIMITS\f[R] section and the \f[B]NUMBERS\f[R] section). .RS .PP If the value on top of the stack has any \f[I]scale\f[R], the \f[I]scale\f[R] is ignored. .RE .TP \f[B]k\f[R] Pops the value off of the top of the stack and uses it to set \f[B]scale\f[R], which must be non-negative. .RS .PP If the value on top of the stack has any \f[I]scale\f[R], the \f[I]scale\f[R] is ignored. .RE .TP \f[B]j\f[R] Pops the value off of the top of the stack and uses it to set \f[B]seed\f[R]. The meaning of \f[B]seed\f[R] is dependent on the current pseudo-random number generator but is guaranteed to not change except for new major versions. .RS .PP The \f[I]scale\f[R] and sign of the value may be significant. .PP If a previously used \f[B]seed\f[R] value is used again, the pseudo-random number generator is guaranteed to produce the same sequence of pseudo-random numbers as it did when the \f[B]seed\f[R] value was previously used. .PP The exact value assigned to \f[B]seed\f[R] is not guaranteed to be returned if the \f[B]J\f[R] command is used. However, if \f[B]seed\f[R] \f[I]does\f[R] return a different value, both values, when assigned to \f[B]seed\f[R], are guaranteed to produce the same sequence of pseudo-random numbers. This means that certain values assigned to \f[B]seed\f[R] will not produce unique sequences of pseudo-random numbers. .PP There is no limit to the length (number of significant decimal digits) or \f[I]scale\f[R] of the value that can be assigned to \f[B]seed\f[R]. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]I\f[R] Pushes the current value of \f[B]ibase\f[R] onto the main stack. .TP \f[B]O\f[R] Pushes the current value of \f[B]obase\f[R] onto the main stack. .TP \f[B]K\f[R] Pushes the current value of \f[B]scale\f[R] onto the main stack. .TP \f[B]J\f[R] Pushes the current value of \f[B]seed\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]T\f[R] Pushes the maximum allowable value of \f[B]ibase\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]U\f[R] Pushes the maximum allowable value of \f[B]obase\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]V\f[R] Pushes the maximum allowable value of \f[B]scale\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]W\f[R] Pushes the maximum (inclusive) integer that can be generated with the \f[B]\[cq]\f[R] pseudo-random number generator command. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Strings -.PP The following commands control strings. .PP dc(1) can work with both numbers and strings, and registers (see the \f[B]REGISTERS\f[R] section) can hold both strings and numbers. dc(1) always knows whether the contents of a register are a string or a number. .PP While arithmetic operations have to have numbers, and will print an error if given a string, other commands accept strings. .PP Strings can also be executed as macros. For example, if the string \f[B][1pR]\f[R] is executed as a macro, then the code \f[B]1pR\f[R] is executed, meaning that the \f[B]1\f[R] will be printed with a newline after and then popped from the stack. .TP \f[B][\f[R]\f[I]characters\f[R]\f[B]]\f[R] Makes a string containing \f[I]characters\f[R] and pushes it onto the stack. .RS .PP If there are brackets (\f[B][\f[R] and \f[B]]\f[R]) in the string, then they must be balanced. Unbalanced brackets can be escaped using a backslash (\f[B]\[rs]\f[R]) character. .PP If there is a backslash character in the string, the character after it (even another backslash) is put into the string verbatim, but the (first) backslash is not. .RE .TP \f[B]a\f[R] The value on top of the stack is popped. .RS .PP If it is a number, it is truncated and its absolute value is taken. The result mod \f[B]256\f[R] is calculated. If that result is \f[B]0\f[R], push an empty string; otherwise, push a one-character string where the character is the result of the mod interpreted as an ASCII character. .PP If it is a string, then a new string is made. If the original string is empty, the new string is empty. If it is not, then the first character of the original string is used to create the new string as a one-character string. The new string is then pushed onto the stack. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]x\f[R] Pops a value off of the top of the stack. .RS .PP If it is a number, it is pushed back onto the stack. .PP If it is a string, it is executed as a macro. .PP This behavior is the norm whenever a macro is executed, whether by this command or by the conditional execution commands below. .RE .TP \f[B]>\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is greater than the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP For example, \f[B]0 1>a\f[R] will execute the contents of register \f[B]a\f[R], and \f[B]1 0>a\f[R] will not. .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]>\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]!>\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is not greater than the second (less than or equal to), then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]!>\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]<\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is less than the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]<\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]!<\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is not less than the second (greater than or equal to), then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]!<\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]=\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is equal to the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]=\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]!=\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is not equal to the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]!=\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]?\f[R] Reads a line from the \f[B]stdin\f[R] and executes it. This is to allow macros to request input from users. .TP \f[B]q\f[R] During execution of a macro, this exits the execution of that macro and the execution of the macro that executed it. If there are no macros, or only one macro executing, dc(1) exits. .TP \f[B]Q\f[R] Pops a value from the stack which must be non-negative and is used the number of macro executions to pop off of the execution stack. If the number of levels to pop is greater than the number of executing macros, dc(1) exits. .TP \f[B],\f[R] Pushes the depth of the execution stack onto the stack. The execution stack is the stack of string executions. The number that is pushed onto the stack is exactly as many as is needed to make dc(1) exit with the \f[B]Q\f[R] command, so the sequence \f[B],Q\f[R] will make dc(1) exit. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Status -.PP These commands query status of the stack or its top value. .TP \f[B]Z\f[R] Pops a value off of the stack. .RS .PP If it is a number, calculates the number of significant decimal digits it has and pushes the result. It will push \f[B]1\f[R] if the argument is \f[B]0\f[R] with no decimal places. .PP If it is a string, pushes the number of characters the string has. .RE .TP \f[B]X\f[R] Pops a value off of the stack. .RS .PP If it is a number, pushes the \f[I]scale\f[R] of the value onto the stack. .PP If it is a string, pushes \f[B]0\f[R]. .RE .TP \f[B]u\f[R] Pops one value off of the stack. If the value is a number, this pushes \f[B]1\f[R] onto the stack. Otherwise (if it is a string), it pushes \f[B]0\f[R]. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]t\f[R] Pops one value off of the stack. If the value is a string, this pushes \f[B]1\f[R] onto the stack. Otherwise (if it is a number), it pushes \f[B]0\f[R]. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]z\f[R] Pushes the current depth of the stack (before execution of this command) onto the stack. .TP \f[B]y\f[R]\f[I]r\f[R] Pushes the current stack depth of the register \f[I]r\f[R] onto the main stack. .RS .PP Because each register has a depth of \f[B]1\f[R] (with the value \f[B]0\f[R] in the top item) when dc(1) starts, dc(1) requires that each register\[cq]s stack must always have at least one item; dc(1) will give an error and reset otherwise (see the \f[B]RESET\f[R] section). This means that this command will never push \f[B]0\f[R]. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Arrays -.PP These commands manipulate arrays. .TP \f[B]:\f[R]\f[I]r\f[R] Pops the top two values off of the stack. The second value will be stored in the array \f[I]r\f[R] (see the \f[B]REGISTERS\f[R] section), indexed by the first value. .TP \f[B];\f[R]\f[I]r\f[R] Pops the value on top of the stack and uses it as an index into the array \f[I]r\f[R]. The selected value is then pushed onto the stack. .TP \f[B]Y\f[R]\f[I]r\f[R] Pushes the length of the array \f[I]r\f[R] onto the stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Global Settings -.PP These commands retrieve global settings. These are the only commands that require multiple specific characters, and all of them begin with the letter \f[B]g\f[R]. Only the characters below are allowed after the character \f[B]g\f[R]; any other character produces a parse error (see the \f[B]ERRORS\f[R] section). .TP \f[B]gl\f[R] Pushes the line length set by \f[B]DC_LINE_LENGTH\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) onto the stack. .TP \f[B]gx\f[R] Pushes \f[B]1\f[R] onto the stack if extended register mode is on, \f[B]0\f[R] otherwise. See the \f[I]Extended Register Mode\f[R] subsection of the \f[B]REGISTERS\f[R] section for more information. .TP \f[B]gz\f[R] Pushes \f[B]0\f[R] onto the stack if the leading zero setting has not been enabled with the \f[B]-z\f[R] or \f[B]--leading-zeroes\f[R] options (see the \f[B]OPTIONS\f[R] section), non-zero otherwise. .SH REGISTERS -.PP Registers are names that can store strings, numbers, and arrays. (Number/string registers do not interfere with array registers.) .PP Each register is also its own stack, so the current register value is the top of the stack for the register. All registers, when first referenced, have one value (\f[B]0\f[R]) in their stack, and it is a runtime error to attempt to pop that item off of the register stack. .PP In non-extended register mode, a register name is just the single character that follows any command that needs a register name. The only exceptions are: a newline (\f[B]`\[rs]n'\f[R]) and a left bracket (\f[B]`['\f[R]); it is a parse error for a newline or a left bracket to be used as a register name. .SS Extended Register Mode -.PP Unlike most other dc(1) implentations, this dc(1) provides nearly unlimited amounts of registers, if extended register mode is enabled. .PP If extended register mode is enabled (\f[B]-x\f[R] or \f[B]--extended-register\f[R] command-line arguments are given), then normal single character registers are used \f[I]unless\f[R] the character immediately following a command that needs a register name is a space (according to \f[B]isspace()\f[R]) and not a newline (\f[B]`\[rs]n'\f[R]). .PP In that case, the register name is found according to the regex \f[B][a-z][a-z0-9_]*\f[R] (like bc(1) identifiers), and it is a parse error if the next non-space characters do not match that regex. .SH RESET -.PP When dc(1) encounters an error or a signal that it has a non-default handler for, it resets. This means that several things happen. .PP First, any macros that are executing are stopped and popped off the stack. The behavior is not unlike that of exceptions in programming languages. Then the execution point is set so that any code waiting to execute (after all macros returned) is skipped. .PP Thus, when dc(1) resets, it skips any remaining code waiting to be executed. Then, if it is interactive mode, and the error was not a fatal error (see the \f[B]EXIT STATUS\f[R] section), it asks for more input; otherwise, it exits with the appropriate return code. .SH PERFORMANCE -.PP Most dc(1) implementations use \f[B]char\f[R] types to calculate the value of \f[B]1\f[R] decimal digit at a time, but that can be slow. This dc(1) does something different. .PP It uses large integers to calculate more than \f[B]1\f[R] decimal digit at a time. If built in a environment where \f[B]DC_LONG_BIT\f[R] (see the \f[B]LIMITS\f[R] section) is \f[B]64\f[R], then each integer has \f[B]9\f[R] decimal digits. If built in an environment where \f[B]DC_LONG_BIT\f[R] is \f[B]32\f[R] then each integer has \f[B]4\f[R] decimal digits. This value (the number of decimal digits per large integer) is called \f[B]DC_BASE_DIGS\f[R]. .PP In addition, this dc(1) uses an even larger integer for overflow checking. This integer type depends on the value of \f[B]DC_LONG_BIT\f[R], but is always at least twice as large as the integer type used to store digits. .SH LIMITS -.PP The following are the limits on dc(1): .TP \f[B]DC_LONG_BIT\f[R] The number of bits in the \f[B]long\f[R] type in the environment where dc(1) was built. This determines how many decimal digits can be stored in a single large integer (see the \f[B]PERFORMANCE\f[R] section). .TP \f[B]DC_BASE_DIGS\f[R] The number of decimal digits per large integer (see the \f[B]PERFORMANCE\f[R] section). Depends on \f[B]DC_LONG_BIT\f[R]. .TP \f[B]DC_BASE_POW\f[R] The max decimal number that each large integer can store (see \f[B]DC_BASE_DIGS\f[R]) plus \f[B]1\f[R]. Depends on \f[B]DC_BASE_DIGS\f[R]. .TP \f[B]DC_OVERFLOW_MAX\f[R] The max number that the overflow type (see the \f[B]PERFORMANCE\f[R] section) can hold. Depends on \f[B]DC_LONG_BIT\f[R]. .TP \f[B]DC_BASE_MAX\f[R] The maximum output base. Set at \f[B]DC_BASE_POW\f[R]. .TP \f[B]DC_DIM_MAX\f[R] The maximum size of arrays. Set at \f[B]SIZE_MAX-1\f[R]. .TP \f[B]DC_SCALE_MAX\f[R] The maximum \f[B]scale\f[R]. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_STRING_MAX\f[R] The maximum length of strings. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_NAME_MAX\f[R] The maximum length of identifiers. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_NUM_MAX\f[R] The maximum length of a number (in decimal digits), which includes digits after the decimal point. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_RAND_MAX\f[R] The maximum integer (inclusive) returned by the \f[B]\[cq]\f[R] command, if dc(1). Set at \f[B]2\[ha]DC_LONG_BIT-1\f[R]. .TP Exponent The maximum allowable exponent (positive or negative). Set at \f[B]DC_OVERFLOW_MAX\f[R]. .TP Number of vars The maximum number of vars/arrays. Set at \f[B]SIZE_MAX-1\f[R]. .PP These limits are meant to be effectively non-existent; the limits are so large (at least on 64-bit machines) that there should not be any point at which they become a problem. In fact, memory should be exhausted before these limits should be hit. .SH ENVIRONMENT VARIABLES -.PP As \f[B]non-portable extensions\f[R], dc(1) recognizes the following environment variables: .TP \f[B]DC_ENV_ARGS\f[R] This is another way to give command-line arguments to dc(1). They should be in the same format as all other command-line arguments. These are always processed first, so any files given in \f[B]DC_ENV_ARGS\f[R] will be processed before arguments and files given on the command-line. This gives the user the ability to set up \[lq]standard\[rq] options and files to be used at every invocation. The most useful thing for such files to contain would be useful functions that the user might want every time dc(1) runs. Another use would be to use the \f[B]-e\f[R] option to set \f[B]scale\f[R] to a value other than \f[B]0\f[R]. .RS .PP The code that parses \f[B]DC_ENV_ARGS\f[R] will correctly handle quoted arguments, but it does not understand escape sequences. For example, the string \f[B]\[lq]/home/gavin/some dc file.dc\[rq]\f[R] will be correctly parsed, but the string \f[B]\[lq]/home/gavin/some \[dq]dc\[dq] file.dc\[rq]\f[R] will include the backslashes. .PP The quote parsing will handle either kind of quotes, \f[B]\[cq]\f[R] or \f[B]\[lq]\f[R]. Thus, if you have a file with any number of single quotes in the name, you can use double quotes as the outside quotes, as in \f[B]\[lq]some `dc' file.dc\[rq]\f[R], and vice versa if you have a file with double quotes. However, handling a file with both kinds of quotes in \f[B]DC_ENV_ARGS\f[R] is not supported due to the complexity of the parsing, though such files are still supported on the command-line where the parsing is done by the shell. .RE .TP \f[B]DC_LINE_LENGTH\f[R] If this environment variable exists and contains an integer that is greater than \f[B]1\f[R] and is less than \f[B]UINT16_MAX\f[R] (\f[B]2\[ha]16-1\f[R]), dc(1) will output lines to that length, including the backslash newline combo. The default line length is \f[B]70\f[R]. .RS .PP The special value of \f[B]0\f[R] will disable line length checking and print numbers without regard to line length and without backslashes and newlines. .RE .TP \f[B]DC_SIGINT_RESET\f[R] If dc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), then this environment variable has no effect because dc(1) exits on \f[B]SIGINT\f[R] when not in interactive mode. .RS .PP However, when dc(1) is in interactive mode, then if this environment variable exists and contains an integer, a non-zero value makes dc(1) reset on \f[B]SIGINT\f[R], rather than exit, and zero makes dc(1) exit. If this environment variable exists and is \f[I]not\f[R] an integer, then dc(1) will exit on \f[B]SIGINT\f[R]. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_TTY_MODE\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, then a non-zero value makes dc(1) use TTY mode, and zero makes dc(1) not use TTY mode. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_PROMPT\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, a non-zero value makes dc(1) use a prompt, and zero or a non-integer makes dc(1) not use a prompt. If this environment variable does not exist and \f[B]DC_TTY_MODE\f[R] does, then the value of the \f[B]DC_TTY_MODE\f[R] environment variable is used. .PP This environment variable and the \f[B]DC_TTY_MODE\f[R] environment variable override the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_EXPR_EXIT\f[R] If any expressions or expression files are given on the command-line with \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R], then if this environment variable exists and contains an integer, a non-zero value makes dc(1) exit after executing the expressions and expression files, and a zero value makes dc(1) not exit. .RS .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_DIGIT_CLAMP\f[R] When parsing numbers and if this environment variable exists and contains an integer, a non-zero value makes dc(1) clamp digits that are greater than or equal to the current \f[B]ibase\f[R] so that all such digits are considered equal to the \f[B]ibase\f[R] minus 1, and a zero value disables such clamping so that those digits are always equal to their value, which is multiplied by the power of the \f[B]ibase\f[R]. .RS .PP This never applies to single-digit numbers, as per the bc(1) standard (see the \f[B]STANDARDS\f[R] section). .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .SH EXIT STATUS -.PP dc(1) returns the following exit statuses: .TP \f[B]0\f[R] No error. .TP \f[B]1\f[R] A math error occurred. This follows standard practice of using \f[B]1\f[R] for expected errors, since math errors will happen in the process of normal execution. .RS .PP Math errors include divide by \f[B]0\f[R], taking the square root of a negative number, using a negative number as a bound for the pseudo-random number generator, attempting to convert a negative number to a hardware integer, overflow when converting a number to a hardware integer, overflow when calculating the size of a number, and attempting to use a non-integer where an integer is required. .PP Converting to a hardware integer happens for the second operand of the power (\f[B]\[ha]\f[R]), places (\f[B]\[at]\f[R]), left shift (\f[B]H\f[R]), and right shift (\f[B]h\f[R]) operators. .RE .TP \f[B]2\f[R] A parse error occurred. .RS .PP Parse errors include unexpected \f[B]EOF\f[R], using an invalid character, failing to find the end of a string or comment, and using a token where it is invalid. .RE .TP \f[B]3\f[R] A runtime error occurred. .RS .PP Runtime errors include assigning an invalid number to any global (\f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R]), giving a bad expression to a \f[B]read()\f[R] call, calling \f[B]read()\f[R] inside of a \f[B]read()\f[R] call, type errors (including attempting to execute a number), and attempting an operation when the stack has too few elements. .RE .TP \f[B]4\f[R] A fatal error occurred. .RS .PP Fatal errors include memory allocation errors, I/O errors, failing to open files, attempting to use files that do not have only ASCII characters (dc(1) only accepts ASCII characters), attempting to open a directory as a file, and giving invalid command-line options. .RE .PP The exit status \f[B]4\f[R] is special; when a fatal error occurs, dc(1) always exits and returns \f[B]4\f[R], no matter what mode dc(1) is in. .PP The other statuses will only be returned when dc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), since dc(1) resets its state (see the \f[B]RESET\f[R] section) and accepts more input when one of those errors occurs in interactive mode. This is also the case when interactive mode is forced by the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .PP These exit statuses allow dc(1) to be used in shell scripting with error checking, and its normal behavior can be forced by using the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .SH INTERACTIVE MODE -.PP Like bc(1), dc(1) has an interactive mode and a non-interactive mode. Interactive mode is turned on automatically when both \f[B]stdin\f[R] and \f[B]stdout\f[R] are hooked to a terminal, but the \f[B]-i\f[R] flag and \f[B]--interactive\f[R] option can turn it on in other situations. .PP In interactive mode, dc(1) attempts to recover from errors (see the \f[B]RESET\f[R] section), and in normal execution, flushes \f[B]stdout\f[R] as soon as execution is done for the current input. dc(1) may also reset on \f[B]SIGINT\f[R] instead of exit, depending on the contents of, or default for, the \f[B]DC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .SH TTY MODE -.PP If \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY, then \[lq]TTY mode\[rq] is considered to be available, and thus, dc(1) can turn on TTY mode, subject to some settings. .PP If there is the environment variable \f[B]DC_TTY_MODE\f[R] in the environment (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then if that environment variable contains a non-zero integer, dc(1) will turn on TTY mode when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY. If the \f[B]DC_TTY_MODE\f[R] environment variable exists but is \f[I]not\f[R] a non-zero integer, then dc(1) will not turn TTY mode on. .PP If the environment variable \f[B]DC_TTY_MODE\f[R] does \f[I]not\f[R] exist, the default setting is used. The default setting can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP TTY mode is different from interactive mode because interactive mode is required in the bc(1) specification (see the \f[B]STANDARDS\f[R] section), and interactive mode requires only \f[B]stdin\f[R] and \f[B]stdout\f[R] to be connected to a terminal. .SS Command-Line History -.PP Command-line history is only enabled if TTY mode is, i.e., that \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the \f[B]DC_TTY_MODE\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and its default do not disable TTY mode. See the \f[B]COMMAND LINE HISTORY\f[R] section for more information. .SS Prompt -.PP If TTY mode is available, then a prompt can be enabled. Like TTY mode itself, it can be turned on or off with an environment variable: \f[B]DC_PROMPT\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP If the environment variable \f[B]DC_PROMPT\f[R] exists and is a non-zero integer, then the prompt is turned on when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options were not used. The read prompt will be turned on under the same conditions, except that the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options must also not be used. .PP However, if \f[B]DC_PROMPT\f[R] does not exist, the prompt can be enabled or disabled with the \f[B]DC_TTY_MODE\f[R] environment variable, the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options, and the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options. See the \f[B]ENVIRONMENT VARIABLES\f[R] and \f[B]OPTIONS\f[R] sections for more details. .SH SIGNAL HANDLING -.PP Sending a \f[B]SIGINT\f[R] will cause dc(1) to do one of two things. .PP If dc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), or the \f[B]DC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or its default, is either not an integer or it is zero, dc(1) will exit. .PP However, if dc(1) is in interactive mode, and the \f[B]DC_SIGINT_RESET\f[R] or its default is an integer and non-zero, then dc(1) will stop executing the current input and reset (see the \f[B]RESET\f[R] section) upon receiving a \f[B]SIGINT\f[R]. .PP Note that \[lq]current input\[rq] can mean one of two things. If dc(1) is processing input from \f[B]stdin\f[R] in interactive mode, it will ask for more input. If dc(1) is processing input from a file in interactive mode, it will stop processing the file and start processing the next file, if one exists, or ask for input from \f[B]stdin\f[R] if no other file exists. .PP This means that if a \f[B]SIGINT\f[R] is sent to dc(1) as it is executing a file, it can seem as though dc(1) did not respond to the signal since it will immediately start executing the next file. This is by design; most files that users execute when interacting with dc(1) have function definitions, which are quick to parse. If a file takes a long time to execute, there may be a bug in that file. The rest of the files could still be executed without problem, allowing the user to continue. .PP \f[B]SIGTERM\f[R] and \f[B]SIGQUIT\f[R] cause dc(1) to clean up and exit, and it uses the default handler for all other signals. The one exception is \f[B]SIGHUP\f[R]; in that case, and only when dc(1) is in TTY mode (see the \f[B]TTY MODE\f[R] section), a \f[B]SIGHUP\f[R] will cause dc(1) to clean up and exit. .SH COMMAND LINE HISTORY -.PP dc(1) supports interactive command-line editing. .PP If dc(1) can be in TTY mode (see the \f[B]TTY MODE\f[R] section), history can be enabled. This means that command-line history can only be enabled when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY. .PP Like TTY mode itself, it can be turned on or off with the environment variable \f[B]DC_TTY_MODE\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP \f[B]Note\f[R]: tabs are converted to 8 spaces. .SH LOCALES -.PP This dc(1) ships with support for adding error messages for different locales and thus, supports \f[B]LC_MESSAGES\f[R]. .SH SEE ALSO -.PP bc(1) .SH STANDARDS -.PP The dc(1) utility operators and some behavior are compliant with the operators in the IEEE Std 1003.1-2017 (\[lq]POSIX.1-2017\[rq]) bc(1) specification at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html . .SH BUGS -.PP None are known. Report bugs at https://git.gavinhoward.com/gavin/bc . .SH AUTHOR -.PP -Gavin D. -Howard and contributors. +Gavin D. Howard \c +.MT gavin@gavinhoward.com +.ME \c +\ and contributors. diff --git a/contrib/bc/manuals/dc/E.1 b/contrib/bc/manuals/dc/E.1 index 41de5e08f743..bc8d66eb3f97 100644 --- a/contrib/bc/manuals/dc/E.1 +++ b/contrib/bc/manuals/dc/E.1 @@ -1,1505 +1,1469 @@ .\" .\" SPDX-License-Identifier: BSD-2-Clause .\" .\" Copyright (c) 2018-2023 Gavin D. Howard and contributors. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions are met: .\" .\" * Redistributions of source code must retain the above copyright notice, .\" this list of conditions and the following disclaimer. .\" .\" * Redistributions in binary form must reproduce the above copyright notice, .\" this list of conditions and the following disclaimer in the documentation .\" and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" .TH "DC" "1" "February 2023" "Gavin D. Howard" "General Commands Manual" .nh .ad l .SH Name -.PP dc - arbitrary-precision decimal reverse-Polish notation calculator .SH SYNOPSIS -.PP \f[B]dc\f[R] [\f[B]-cChiPRvVx\f[R]] [\f[B]--version\f[R]] [\f[B]--help\f[R]] [\f[B]--digit-clamp\f[R]] [\f[B]--no-digit-clamp\f[R]] [\f[B]--interactive\f[R]] [\f[B]--no-prompt\f[R]] [\f[B]--no-read-prompt\f[R]] [\f[B]--extended-register\f[R]] [\f[B]-e\f[R] \f[I]expr\f[R]] [\f[B]--expression\f[R]=\f[I]expr\f[R]\&...] [\f[B]-f\f[R] \f[I]file\f[R]\&...] [\f[B]--file\f[R]=\f[I]file\f[R]\&...] [\f[I]file\f[R]\&...] .SH DESCRIPTION -.PP dc(1) is an arbitrary-precision calculator. It uses a stack (reverse Polish notation) to store numbers and results of computations. Arithmetic operations pop arguments off of the stack and push the results. .PP If no files are given on the command-line, then dc(1) reads from \f[B]stdin\f[R] (see the \f[B]STDIN\f[R] section). Otherwise, those files are processed, and dc(1) will then exit. .PP If a user wants to set up a standard environment, they can use \f[B]DC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). For example, if a user wants the \f[B]scale\f[R] always set to \f[B]10\f[R], they can set \f[B]DC_ENV_ARGS\f[R] to \f[B]-e 10k\f[R], and this dc(1) will always start with a \f[B]scale\f[R] of \f[B]10\f[R]. .SH OPTIONS -.PP The following are the options that dc(1) accepts. .TP \f[B]-C\f[R], \f[B]--no-digit-clamp\f[R] Disables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that the value added to a number from a digit is always that digit\[cq]s value multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-c\f[R] or \f[B]--digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]DC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-c\f[R], \f[B]--digit-clamp\f[R] Enables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that digits that the value added to a number from a digit that is greater than or equal to the ibase is the value of ibase minus 1 all multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-C\f[R] or \f[B]--no-digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]DC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-e\f[R] \f[I]expr\f[R], \f[B]--expression\f[R]=\f[I]expr\f[R] Evaluates \f[I]expr\f[R]. If multiple expressions are given, they are evaluated in order. If files are given as well (see below), the expressions and files are evaluated in the order given. This means that if a file is given before an expression, the file is read in and evaluated first. .RS .PP If this option is given on the command-line (i.e., not in \f[B]DC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, dc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R], whether on the command-line or in \f[B]DC_ENV_ARGS\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, dc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-f\f[R] \f[I]file\f[R], \f[B]--file\f[R]=\f[I]file\f[R] Reads in \f[I]file\f[R] and evaluates it, line by line, as though it were read through \f[B]stdin\f[R]. If expressions are also given (see above), the expressions are evaluated in the order given. .RS .PP If this option is given on the command-line (i.e., not in \f[B]DC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, dc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, dc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-h\f[R], \f[B]--help\f[R] Prints a usage message and exits. .TP \f[B]-I\f[R] \f[I]ibase\f[R], \f[B]--ibase\f[R]=\f[I]ibase\f[R] Sets the builtin variable \f[B]ibase\f[R] to the value \f[I]ibase\f[R] assuming that \f[I]ibase\f[R] is in base 10. It is a fatal error if \f[I]ibase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-i\f[R], \f[B]--interactive\f[R] Forces interactive mode. (See the \f[B]INTERACTIVE MODE\f[R] section.) .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-L\f[R], \f[B]--no-line-length\f[R] Disables line length checking and prints numbers without backslashes and newlines. In other words, this option sets \f[B]BC_LINE_LENGTH\f[R] to \f[B]0\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-O\f[R] \f[I]obase\f[R], \f[B]--obase\f[R]=\f[I]obase\f[R] Sets the builtin variable \f[B]obase\f[R] to the value \f[I]obase\f[R] assuming that \f[I]obase\f[R] is in base 10. It is a fatal error if \f[I]obase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-P\f[R], \f[B]--no-prompt\f[R] Disables the prompt in TTY mode. (The prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a prompt or are not used to having them in dc(1). Most of those users would want to put this option in \f[B]DC_ENV_ARGS\f[R]. .RS .PP These options override the \f[B]DC_PROMPT\f[R] and \f[B]DC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-R\f[R], \f[B]--no-read-prompt\f[R] Disables the read prompt in TTY mode. (The read prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a read prompt or are not used to having them in dc(1). Most of those users would want to put this option in \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). This option is also useful in hash bang lines of dc(1) scripts that prompt for user input. .RS .PP This option does not disable the regular prompt because the read prompt is only used when the \f[B]?\f[R] command is used. .PP These options \f[I]do\f[R] override the \f[B]DC_PROMPT\f[R] and \f[B]DC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), but only for the read prompt. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-S\f[R] \f[I]scale\f[R], \f[B]--scale\f[R]=\f[I]scale\f[R] Sets the builtin variable \f[B]scale\f[R] to the value \f[I]scale\f[R] assuming that \f[I]scale\f[R] is in base 10. It is a fatal error if \f[I]scale\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-v\f[R], \f[B]-V\f[R], \f[B]--version\f[R] Print the version information (copyright header) and exits. .TP \f[B]-x\f[R] \f[B]--extended-register\f[R] Enables extended register mode. See the \f[I]Extended Register Mode\f[R] subsection of the \f[B]REGISTERS\f[R] section for more information. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-z\f[R], \f[B]--leading-zeroes\f[R] Makes dc(1) print all numbers greater than \f[B]-1\f[R] and less than \f[B]1\f[R], and not equal to \f[B]0\f[R], with a leading zero. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .PP All long options are \f[B]non-portable extensions\f[R]. .SH STDIN -.PP If no files are given on the command-line and no files or expressions are given by the \f[B]-f\f[R], \f[B]--file\f[R], \f[B]-e\f[R], or \f[B]--expression\f[R] options, then dc(1) reads from \f[B]stdin\f[R]. .PP However, there is a caveat to this. .PP First, \f[B]stdin\f[R] is evaluated a line at a time. The only exception to this is if a string has been finished, but not ended. This means that, except for escaped brackets, all brackets must be balanced before dc(1) parses and executes. .SH STDOUT -.PP Any non-error output is written to \f[B]stdout\f[R]. In addition, if history (see the \f[B]HISTORY\f[R] section) and the prompt (see the \f[B]TTY MODE\f[R] section) are enabled, both are output to \f[B]stdout\f[R]. .PP \f[B]Note\f[R]: Unlike other dc(1) implementations, this dc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stdout\f[R], so if \f[B]stdout\f[R] is closed, as in -\f[B]dc >&-\f[R], it will quit with an error. +\f[B]dc >&-\f[R], it will quit with an error. This is done so that dc(1) can report problems when \f[B]stdout\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other dc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stdout\f[R] to \f[B]/dev/null\f[R]. .SH STDERR -.PP Any error output is written to \f[B]stderr\f[R]. .PP \f[B]Note\f[R]: Unlike other dc(1) implementations, this dc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stderr\f[R], so if \f[B]stderr\f[R] is closed, as in -\f[B]dc 2>&-\f[R], it will quit with an error. +\f[B]dc 2>&-\f[R], it will quit with an error. This is done so that dc(1) can exit with an error code when \f[B]stderr\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other dc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stderr\f[R] to \f[B]/dev/null\f[R]. .SH SYNTAX -.PP Each item in the input source code, either a number (see the \f[B]NUMBERS\f[R] section) or a command (see the \f[B]COMMANDS\f[R] section), is processed and executed, in order. Input is processed immediately when entered. .PP \f[B]ibase\f[R] is a register (see the \f[B]REGISTERS\f[R] section) that determines how to interpret constant numbers. It is the \[lq]input\[rq] base, or the number base used for interpreting input numbers. \f[B]ibase\f[R] is initially \f[B]10\f[R]. The max allowable value for \f[B]ibase\f[R] is \f[B]16\f[R]. The min allowable value for \f[B]ibase\f[R] is \f[B]2\f[R]. The max allowable value for \f[B]ibase\f[R] can be queried in dc(1) programs with the \f[B]T\f[R] command. .PP \f[B]obase\f[R] is a register (see the \f[B]REGISTERS\f[R] section) that determines how to output results. It is the \[lq]output\[rq] base, or the number base used for outputting numbers. \f[B]obase\f[R] is initially \f[B]10\f[R]. The max allowable value for \f[B]obase\f[R] is \f[B]DC_BASE_MAX\f[R] and can be queried with the \f[B]U\f[R] command. The min allowable value for \f[B]obase\f[R] is \f[B]2\f[R]. Values are output in the specified base. .PP The \f[I]scale\f[R] of an expression is the number of digits in the result of the expression right of the decimal point, and \f[B]scale\f[R] is a register (see the \f[B]REGISTERS\f[R] section) that sets the precision of any operations (with exceptions). \f[B]scale\f[R] is initially \f[B]0\f[R]. \f[B]scale\f[R] cannot be negative. The max allowable value for \f[B]scale\f[R] can be queried in dc(1) programs with the \f[B]V\f[R] command. .SS Comments -.PP Comments go from \f[B]#\f[R] until, and not including, the next newline. This is a \f[B]non-portable extension\f[R]. .SH NUMBERS -.PP Numbers are strings made up of digits, uppercase letters up to \f[B]F\f[R], and at most \f[B]1\f[R] period for a radix. Numbers can have up to \f[B]DC_NUM_MAX\f[R] digits. Uppercase letters are equal to \f[B]9\f[R] plus their position in the alphabet (i.e., \f[B]A\f[R] equals \f[B]10\f[R], or \f[B]9+1\f[R]). .PP If a digit or letter makes no sense with the current value of \f[B]ibase\f[R] (i.e., they are greater than or equal to the current value of \f[B]ibase\f[R]), then the behavior depends on the existence of the \f[B]-c\f[R]/\f[B]--digit-clamp\f[R] or \f[B]-C\f[R]/\f[B]--no-digit-clamp\f[R] options (see the \f[B]OPTIONS\f[R] section), the existence and setting of the \f[B]DC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or the default, which can be queried with the \f[B]-h\f[R]/\f[B]--help\f[R] option. .PP If clamping is off, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are not changed. Instead, their given value is multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*A+3\[ha]0*B\f[R], which is \f[B]3\f[R] times \f[B]10\f[R] plus \f[B]11\f[R], or \f[B]41\f[R]. .PP If clamping is on, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are set to the value of the highest valid digit in \f[B]ibase\f[R] before being multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*2+3\[ha]0*2\f[R], which is \f[B]3\f[R] times \f[B]2\f[R] plus \f[B]2\f[R], or \f[B]8\f[R]. .PP There is one exception to clamping: single-character numbers (i.e., \f[B]A\f[R] alone). Such numbers are never clamped and always take the value they would have in the highest possible \f[B]ibase\f[R]. This means that \f[B]A\f[R] alone always equals decimal \f[B]10\f[R] and \f[B]Z\f[R] alone always equals decimal \f[B]35\f[R]. This behavior is mandated by the standard for bc(1) (see the STANDARDS section) and is meant to provide an easy way to set the current \f[B]ibase\f[R] (with the \f[B]i\f[R] command) regardless of the current value of \f[B]ibase\f[R]. .PP If clamping is on, and the clamped value of a character is needed, use a leading zero, i.e., for \f[B]A\f[R], use \f[B]0A\f[R]. .SH COMMANDS -.PP The valid commands are listed below. .SS Printing -.PP These commands are used for printing. .TP \f[B]p\f[R] Prints the value on top of the stack, whether number or string, and prints a newline after. .RS .PP This does not alter the stack. .RE .TP \f[B]n\f[R] Prints the value on top of the stack, whether number or string, and pops it off of the stack. .TP \f[B]P\f[R] Pops a value off the stack. .RS .PP If the value is a number, it is truncated and the absolute value of the result is printed as though \f[B]obase\f[R] is \f[B]256\f[R] and each digit is interpreted as an 8-bit ASCII character, making it a byte stream. .PP If the value is a string, it is printed without a trailing newline. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]f\f[R] Prints the entire contents of the stack, in order from newest to oldest, without altering anything. .RS .PP Users should use this command when they get lost. .RE .SS Arithmetic -.PP These are the commands used for arithmetic. .TP \f[B]+\f[R] The top two values are popped off the stack, added, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to the max \f[I]scale\f[R] of both operands. .TP \f[B]-\f[R] The top two values are popped off the stack, subtracted, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to the max \f[I]scale\f[R] of both operands. .TP \f[B]*\f[R] The top two values are popped off the stack, multiplied, and the result is pushed onto the stack. If \f[B]a\f[R] is the \f[I]scale\f[R] of the first expression and \f[B]b\f[R] is the \f[I]scale\f[R] of the second expression, the \f[I]scale\f[R] of the result is equal to \f[B]min(a+b,max(scale,a,b))\f[R] where \f[B]min()\f[R] and \f[B]max()\f[R] return the obvious values. .TP \f[B]/\f[R] The top two values are popped off the stack, divided, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The first value popped off of the stack must be non-zero. .RE .TP \f[B]%\f[R] The top two values are popped off the stack, remaindered, and the result is pushed onto the stack. .RS .PP Remaindering is equivalent to 1) Computing \f[B]a/b\f[R] to current \f[B]scale\f[R], and 2) Using the result of step 1 to calculate \f[B]a-(a/b)*b\f[R] to \f[I]scale\f[R] \f[B]max(scale+scale(b),scale(a))\f[R]. .PP The first value popped off of the stack must be non-zero. .RE .TP \f[B]\[ti]\f[R] The top two values are popped off the stack, divided and remaindered, and the results (divided first, remainder second) are pushed onto the stack. This is equivalent to \f[B]x y / x y %\f[R] except that \f[B]x\f[R] and \f[B]y\f[R] are only evaluated once. .RS .PP The first value popped off of the stack must be non-zero. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[ha]\f[R] The top two values are popped off the stack, the second is raised to the power of the first, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The first value popped off of the stack must be an integer, and if that value is negative, the second value popped off of the stack must be non-zero. .RE .TP \f[B]v\f[R] The top value is popped off the stack, its square root is computed, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The value popped off of the stack must be non-negative. .RE .TP \f[B]_\f[R] If this command \f[I]immediately\f[R] precedes a number (i.e., no spaces or other commands), then that number is input as a negative number. .RS .PP Otherwise, the top value on the stack is popped and copied, and the copy is negated and pushed onto the stack. This behavior without a number is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]b\f[R] The top value is popped off the stack, and if it is zero, it is pushed back onto the stack. Otherwise, its absolute value is pushed onto the stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]|\f[R] The top three values are popped off the stack, a modular exponentiation is computed, and the result is pushed onto the stack. .RS .PP The first value popped is used as the reduction modulus and must be an integer and non-zero. The second value popped is used as the exponent and must be an integer and non-negative. The third value popped is the base and must be an integer. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]G\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if they are equal, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]N\f[R] The top value is popped off of the stack, and if it a \f[B]0\f[R], a \f[B]1\f[R] is pushed; otherwise, a \f[B]0\f[R] is pushed. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B](\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is less than the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]{\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is less than or equal to the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B])\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is greater than the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]}\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is greater than or equal to the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]M\f[R] The top two values are popped off of the stack. If they are both non-zero, a \f[B]1\f[R] is pushed onto the stack. If either of them is zero, or both of them are, then a \f[B]0\f[R] is pushed onto the stack. .RS .PP This is like the \f[B]&&\f[R] operator in bc(1), and it is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]m\f[R] The top two values are popped off of the stack. If at least one of them is non-zero, a \f[B]1\f[R] is pushed onto the stack. If both of them are zero, then a \f[B]0\f[R] is pushed onto the stack. .RS .PP This is like the \f[B]||\f[R] operator in bc(1), and it is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Stack Control -.PP These commands control the stack. .TP \f[B]c\f[R] Removes all items from (\[lq]clears\[rq]) the stack. .TP \f[B]d\f[R] Copies the item on top of the stack (\[lq]duplicates\[rq]) and pushes the copy onto the stack. .TP \f[B]r\f[R] Swaps (\[lq]reverses\[rq]) the two top items on the stack. .TP \f[B]R\f[R] Pops (\[lq]removes\[rq]) the top value from the stack. .SS Register Control -.PP These commands control registers (see the \f[B]REGISTERS\f[R] section). .TP \f[B]s\f[R]\f[I]r\f[R] Pops the value off the top of the stack and stores it into register \f[I]r\f[R]. .TP \f[B]l\f[R]\f[I]r\f[R] Copies the value in register \f[I]r\f[R] and pushes it onto the stack. This does not alter the contents of \f[I]r\f[R]. .TP \f[B]S\f[R]\f[I]r\f[R] Pops the value off the top of the (main) stack and pushes it onto the stack of register \f[I]r\f[R]. The previous value of the register becomes inaccessible. .TP \f[B]L\f[R]\f[I]r\f[R] Pops the value off the top of the stack for register \f[I]r\f[R] and push it onto the main stack. The previous value in the stack for register \f[I]r\f[R], if any, is now accessible via the \f[B]l\f[R]\f[I]r\f[R] command. .SS Parameters -.PP These commands control the values of \f[B]ibase\f[R], \f[B]obase\f[R], and \f[B]scale\f[R]. Also see the \f[B]SYNTAX\f[R] section. .TP \f[B]i\f[R] Pops the value off of the top of the stack and uses it to set \f[B]ibase\f[R], which must be between \f[B]2\f[R] and \f[B]16\f[R], inclusive. .RS .PP If the value on top of the stack has any \f[I]scale\f[R], the \f[I]scale\f[R] is ignored. .RE .TP \f[B]o\f[R] Pops the value off of the top of the stack and uses it to set \f[B]obase\f[R], which must be between \f[B]2\f[R] and \f[B]DC_BASE_MAX\f[R], inclusive (see the \f[B]LIMITS\f[R] section). .RS .PP If the value on top of the stack has any \f[I]scale\f[R], the \f[I]scale\f[R] is ignored. .RE .TP \f[B]k\f[R] Pops the value off of the top of the stack and uses it to set \f[B]scale\f[R], which must be non-negative. .RS .PP If the value on top of the stack has any \f[I]scale\f[R], the \f[I]scale\f[R] is ignored. .RE .TP \f[B]I\f[R] Pushes the current value of \f[B]ibase\f[R] onto the main stack. .TP \f[B]O\f[R] Pushes the current value of \f[B]obase\f[R] onto the main stack. .TP \f[B]K\f[R] Pushes the current value of \f[B]scale\f[R] onto the main stack. .TP \f[B]T\f[R] Pushes the maximum allowable value of \f[B]ibase\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]U\f[R] Pushes the maximum allowable value of \f[B]obase\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]V\f[R] Pushes the maximum allowable value of \f[B]scale\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Strings -.PP The following commands control strings. .PP dc(1) can work with both numbers and strings, and registers (see the \f[B]REGISTERS\f[R] section) can hold both strings and numbers. dc(1) always knows whether the contents of a register are a string or a number. .PP While arithmetic operations have to have numbers, and will print an error if given a string, other commands accept strings. .PP Strings can also be executed as macros. For example, if the string \f[B][1pR]\f[R] is executed as a macro, then the code \f[B]1pR\f[R] is executed, meaning that the \f[B]1\f[R] will be printed with a newline after and then popped from the stack. .TP \f[B][\f[R]\f[I]characters\f[R]\f[B]]\f[R] Makes a string containing \f[I]characters\f[R] and pushes it onto the stack. .RS .PP If there are brackets (\f[B][\f[R] and \f[B]]\f[R]) in the string, then they must be balanced. Unbalanced brackets can be escaped using a backslash (\f[B]\[rs]\f[R]) character. .PP If there is a backslash character in the string, the character after it (even another backslash) is put into the string verbatim, but the (first) backslash is not. .RE .TP \f[B]a\f[R] The value on top of the stack is popped. .RS .PP If it is a number, it is truncated and its absolute value is taken. The result mod \f[B]256\f[R] is calculated. If that result is \f[B]0\f[R], push an empty string; otherwise, push a one-character string where the character is the result of the mod interpreted as an ASCII character. .PP If it is a string, then a new string is made. If the original string is empty, the new string is empty. If it is not, then the first character of the original string is used to create the new string as a one-character string. The new string is then pushed onto the stack. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]x\f[R] Pops a value off of the top of the stack. .RS .PP If it is a number, it is pushed back onto the stack. .PP If it is a string, it is executed as a macro. .PP This behavior is the norm whenever a macro is executed, whether by this command or by the conditional execution commands below. .RE .TP \f[B]>\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is greater than the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP For example, \f[B]0 1>a\f[R] will execute the contents of register \f[B]a\f[R], and \f[B]1 0>a\f[R] will not. .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]>\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]!>\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is not greater than the second (less than or equal to), then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]!>\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]<\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is less than the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]<\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]!<\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is not less than the second (greater than or equal to), then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]!<\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]=\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is equal to the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]=\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]!=\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is not equal to the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]!=\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]?\f[R] Reads a line from the \f[B]stdin\f[R] and executes it. This is to allow macros to request input from users. .TP \f[B]q\f[R] During execution of a macro, this exits the execution of that macro and the execution of the macro that executed it. If there are no macros, or only one macro executing, dc(1) exits. .TP \f[B]Q\f[R] Pops a value from the stack which must be non-negative and is used the number of macro executions to pop off of the execution stack. If the number of levels to pop is greater than the number of executing macros, dc(1) exits. .TP \f[B],\f[R] Pushes the depth of the execution stack onto the stack. The execution stack is the stack of string executions. The number that is pushed onto the stack is exactly as many as is needed to make dc(1) exit with the \f[B]Q\f[R] command, so the sequence \f[B],Q\f[R] will make dc(1) exit. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Status -.PP These commands query status of the stack or its top value. .TP \f[B]Z\f[R] Pops a value off of the stack. .RS .PP If it is a number, calculates the number of significant decimal digits it has and pushes the result. It will push \f[B]1\f[R] if the argument is \f[B]0\f[R] with no decimal places. .PP If it is a string, pushes the number of characters the string has. .RE .TP \f[B]X\f[R] Pops a value off of the stack. .RS .PP If it is a number, pushes the \f[I]scale\f[R] of the value onto the stack. .PP If it is a string, pushes \f[B]0\f[R]. .RE .TP \f[B]u\f[R] Pops one value off of the stack. If the value is a number, this pushes \f[B]1\f[R] onto the stack. Otherwise (if it is a string), it pushes \f[B]0\f[R]. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]t\f[R] Pops one value off of the stack. If the value is a string, this pushes \f[B]1\f[R] onto the stack. Otherwise (if it is a number), it pushes \f[B]0\f[R]. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]z\f[R] Pushes the current depth of the stack (before execution of this command) onto the stack. .TP \f[B]y\f[R]\f[I]r\f[R] Pushes the current stack depth of the register \f[I]r\f[R] onto the main stack. .RS .PP Because each register has a depth of \f[B]1\f[R] (with the value \f[B]0\f[R] in the top item) when dc(1) starts, dc(1) requires that each register\[cq]s stack must always have at least one item; dc(1) will give an error and reset otherwise (see the \f[B]RESET\f[R] section). This means that this command will never push \f[B]0\f[R]. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Arrays -.PP These commands manipulate arrays. .TP \f[B]:\f[R]\f[I]r\f[R] Pops the top two values off of the stack. The second value will be stored in the array \f[I]r\f[R] (see the \f[B]REGISTERS\f[R] section), indexed by the first value. .TP \f[B];\f[R]\f[I]r\f[R] Pops the value on top of the stack and uses it as an index into the array \f[I]r\f[R]. The selected value is then pushed onto the stack. .TP \f[B]Y\f[R]\f[I]r\f[R] Pushes the length of the array \f[I]r\f[R] onto the stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Global Settings -.PP These commands retrieve global settings. These are the only commands that require multiple specific characters, and all of them begin with the letter \f[B]g\f[R]. Only the characters below are allowed after the character \f[B]g\f[R]; any other character produces a parse error (see the \f[B]ERRORS\f[R] section). .TP \f[B]gl\f[R] Pushes the line length set by \f[B]DC_LINE_LENGTH\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) onto the stack. .TP \f[B]gx\f[R] Pushes \f[B]1\f[R] onto the stack if extended register mode is on, \f[B]0\f[R] otherwise. See the \f[I]Extended Register Mode\f[R] subsection of the \f[B]REGISTERS\f[R] section for more information. .TP \f[B]gz\f[R] Pushes \f[B]0\f[R] onto the stack if the leading zero setting has not been enabled with the \f[B]-z\f[R] or \f[B]--leading-zeroes\f[R] options (see the \f[B]OPTIONS\f[R] section), non-zero otherwise. .SH REGISTERS -.PP Registers are names that can store strings, numbers, and arrays. (Number/string registers do not interfere with array registers.) .PP Each register is also its own stack, so the current register value is the top of the stack for the register. All registers, when first referenced, have one value (\f[B]0\f[R]) in their stack, and it is a runtime error to attempt to pop that item off of the register stack. .PP In non-extended register mode, a register name is just the single character that follows any command that needs a register name. The only exceptions are: a newline (\f[B]`\[rs]n'\f[R]) and a left bracket (\f[B]`['\f[R]); it is a parse error for a newline or a left bracket to be used as a register name. .SS Extended Register Mode -.PP Unlike most other dc(1) implentations, this dc(1) provides nearly unlimited amounts of registers, if extended register mode is enabled. .PP If extended register mode is enabled (\f[B]-x\f[R] or \f[B]--extended-register\f[R] command-line arguments are given), then normal single character registers are used \f[I]unless\f[R] the character immediately following a command that needs a register name is a space (according to \f[B]isspace()\f[R]) and not a newline (\f[B]`\[rs]n'\f[R]). .PP In that case, the register name is found according to the regex \f[B][a-z][a-z0-9_]*\f[R] (like bc(1) identifiers), and it is a parse error if the next non-space characters do not match that regex. .SH RESET -.PP When dc(1) encounters an error or a signal that it has a non-default handler for, it resets. This means that several things happen. .PP First, any macros that are executing are stopped and popped off the stack. The behavior is not unlike that of exceptions in programming languages. Then the execution point is set so that any code waiting to execute (after all macros returned) is skipped. .PP Thus, when dc(1) resets, it skips any remaining code waiting to be executed. Then, if it is interactive mode, and the error was not a fatal error (see the \f[B]EXIT STATUS\f[R] section), it asks for more input; otherwise, it exits with the appropriate return code. .SH PERFORMANCE -.PP Most dc(1) implementations use \f[B]char\f[R] types to calculate the value of \f[B]1\f[R] decimal digit at a time, but that can be slow. This dc(1) does something different. .PP It uses large integers to calculate more than \f[B]1\f[R] decimal digit at a time. If built in a environment where \f[B]DC_LONG_BIT\f[R] (see the \f[B]LIMITS\f[R] section) is \f[B]64\f[R], then each integer has \f[B]9\f[R] decimal digits. If built in an environment where \f[B]DC_LONG_BIT\f[R] is \f[B]32\f[R] then each integer has \f[B]4\f[R] decimal digits. This value (the number of decimal digits per large integer) is called \f[B]DC_BASE_DIGS\f[R]. .PP In addition, this dc(1) uses an even larger integer for overflow checking. This integer type depends on the value of \f[B]DC_LONG_BIT\f[R], but is always at least twice as large as the integer type used to store digits. .SH LIMITS -.PP The following are the limits on dc(1): .TP \f[B]DC_LONG_BIT\f[R] The number of bits in the \f[B]long\f[R] type in the environment where dc(1) was built. This determines how many decimal digits can be stored in a single large integer (see the \f[B]PERFORMANCE\f[R] section). .TP \f[B]DC_BASE_DIGS\f[R] The number of decimal digits per large integer (see the \f[B]PERFORMANCE\f[R] section). Depends on \f[B]DC_LONG_BIT\f[R]. .TP \f[B]DC_BASE_POW\f[R] The max decimal number that each large integer can store (see \f[B]DC_BASE_DIGS\f[R]) plus \f[B]1\f[R]. Depends on \f[B]DC_BASE_DIGS\f[R]. .TP \f[B]DC_OVERFLOW_MAX\f[R] The max number that the overflow type (see the \f[B]PERFORMANCE\f[R] section) can hold. Depends on \f[B]DC_LONG_BIT\f[R]. .TP \f[B]DC_BASE_MAX\f[R] The maximum output base. Set at \f[B]DC_BASE_POW\f[R]. .TP \f[B]DC_DIM_MAX\f[R] The maximum size of arrays. Set at \f[B]SIZE_MAX-1\f[R]. .TP \f[B]DC_SCALE_MAX\f[R] The maximum \f[B]scale\f[R]. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_STRING_MAX\f[R] The maximum length of strings. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_NAME_MAX\f[R] The maximum length of identifiers. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_NUM_MAX\f[R] The maximum length of a number (in decimal digits), which includes digits after the decimal point. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP Exponent The maximum allowable exponent (positive or negative). Set at \f[B]DC_OVERFLOW_MAX\f[R]. .TP Number of vars The maximum number of vars/arrays. Set at \f[B]SIZE_MAX-1\f[R]. .PP These limits are meant to be effectively non-existent; the limits are so large (at least on 64-bit machines) that there should not be any point at which they become a problem. In fact, memory should be exhausted before these limits should be hit. .SH ENVIRONMENT VARIABLES -.PP As \f[B]non-portable extensions\f[R], dc(1) recognizes the following environment variables: .TP \f[B]DC_ENV_ARGS\f[R] This is another way to give command-line arguments to dc(1). They should be in the same format as all other command-line arguments. These are always processed first, so any files given in \f[B]DC_ENV_ARGS\f[R] will be processed before arguments and files given on the command-line. This gives the user the ability to set up \[lq]standard\[rq] options and files to be used at every invocation. The most useful thing for such files to contain would be useful functions that the user might want every time dc(1) runs. Another use would be to use the \f[B]-e\f[R] option to set \f[B]scale\f[R] to a value other than \f[B]0\f[R]. .RS .PP The code that parses \f[B]DC_ENV_ARGS\f[R] will correctly handle quoted arguments, but it does not understand escape sequences. For example, the string \f[B]\[lq]/home/gavin/some dc file.dc\[rq]\f[R] will be correctly parsed, but the string \f[B]\[lq]/home/gavin/some \[dq]dc\[dq] file.dc\[rq]\f[R] will include the backslashes. .PP The quote parsing will handle either kind of quotes, \f[B]\[cq]\f[R] or \f[B]\[lq]\f[R]. Thus, if you have a file with any number of single quotes in the name, you can use double quotes as the outside quotes, as in \f[B]\[lq]some `dc' file.dc\[rq]\f[R], and vice versa if you have a file with double quotes. However, handling a file with both kinds of quotes in \f[B]DC_ENV_ARGS\f[R] is not supported due to the complexity of the parsing, though such files are still supported on the command-line where the parsing is done by the shell. .RE .TP \f[B]DC_LINE_LENGTH\f[R] If this environment variable exists and contains an integer that is greater than \f[B]1\f[R] and is less than \f[B]UINT16_MAX\f[R] (\f[B]2\[ha]16-1\f[R]), dc(1) will output lines to that length, including the backslash newline combo. The default line length is \f[B]70\f[R]. .RS .PP The special value of \f[B]0\f[R] will disable line length checking and print numbers without regard to line length and without backslashes and newlines. .RE .TP \f[B]DC_SIGINT_RESET\f[R] If dc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), then this environment variable has no effect because dc(1) exits on \f[B]SIGINT\f[R] when not in interactive mode. .RS .PP However, when dc(1) is in interactive mode, then if this environment variable exists and contains an integer, a non-zero value makes dc(1) reset on \f[B]SIGINT\f[R], rather than exit, and zero makes dc(1) exit. If this environment variable exists and is \f[I]not\f[R] an integer, then dc(1) will exit on \f[B]SIGINT\f[R]. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_TTY_MODE\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, then a non-zero value makes dc(1) use TTY mode, and zero makes dc(1) not use TTY mode. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_PROMPT\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, a non-zero value makes dc(1) use a prompt, and zero or a non-integer makes dc(1) not use a prompt. If this environment variable does not exist and \f[B]DC_TTY_MODE\f[R] does, then the value of the \f[B]DC_TTY_MODE\f[R] environment variable is used. .PP This environment variable and the \f[B]DC_TTY_MODE\f[R] environment variable override the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_EXPR_EXIT\f[R] If any expressions or expression files are given on the command-line with \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R], then if this environment variable exists and contains an integer, a non-zero value makes dc(1) exit after executing the expressions and expression files, and a zero value makes dc(1) not exit. .RS .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_DIGIT_CLAMP\f[R] When parsing numbers and if this environment variable exists and contains an integer, a non-zero value makes dc(1) clamp digits that are greater than or equal to the current \f[B]ibase\f[R] so that all such digits are considered equal to the \f[B]ibase\f[R] minus 1, and a zero value disables such clamping so that those digits are always equal to their value, which is multiplied by the power of the \f[B]ibase\f[R]. .RS .PP This never applies to single-digit numbers, as per the bc(1) standard (see the \f[B]STANDARDS\f[R] section). .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .SH EXIT STATUS -.PP dc(1) returns the following exit statuses: .TP \f[B]0\f[R] No error. .TP \f[B]1\f[R] A math error occurred. This follows standard practice of using \f[B]1\f[R] for expected errors, since math errors will happen in the process of normal execution. .RS .PP Math errors include divide by \f[B]0\f[R], taking the square root of a negative number, attempting to convert a negative number to a hardware integer, overflow when converting a number to a hardware integer, overflow when calculating the size of a number, and attempting to use a non-integer where an integer is required. .PP Converting to a hardware integer happens for the second operand of the power (\f[B]\[ha]\f[R]) operator. .RE .TP \f[B]2\f[R] A parse error occurred. .RS .PP Parse errors include unexpected \f[B]EOF\f[R], using an invalid character, failing to find the end of a string or comment, and using a token where it is invalid. .RE .TP \f[B]3\f[R] A runtime error occurred. .RS .PP Runtime errors include assigning an invalid number to any global (\f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R]), giving a bad expression to a \f[B]read()\f[R] call, calling \f[B]read()\f[R] inside of a \f[B]read()\f[R] call, type errors (including attempting to execute a number), and attempting an operation when the stack has too few elements. .RE .TP \f[B]4\f[R] A fatal error occurred. .RS .PP Fatal errors include memory allocation errors, I/O errors, failing to open files, attempting to use files that do not have only ASCII characters (dc(1) only accepts ASCII characters), attempting to open a directory as a file, and giving invalid command-line options. .RE .PP The exit status \f[B]4\f[R] is special; when a fatal error occurs, dc(1) always exits and returns \f[B]4\f[R], no matter what mode dc(1) is in. .PP The other statuses will only be returned when dc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), since dc(1) resets its state (see the \f[B]RESET\f[R] section) and accepts more input when one of those errors occurs in interactive mode. This is also the case when interactive mode is forced by the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .PP These exit statuses allow dc(1) to be used in shell scripting with error checking, and its normal behavior can be forced by using the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .SH INTERACTIVE MODE -.PP Like bc(1), dc(1) has an interactive mode and a non-interactive mode. Interactive mode is turned on automatically when both \f[B]stdin\f[R] and \f[B]stdout\f[R] are hooked to a terminal, but the \f[B]-i\f[R] flag and \f[B]--interactive\f[R] option can turn it on in other situations. .PP In interactive mode, dc(1) attempts to recover from errors (see the \f[B]RESET\f[R] section), and in normal execution, flushes \f[B]stdout\f[R] as soon as execution is done for the current input. dc(1) may also reset on \f[B]SIGINT\f[R] instead of exit, depending on the contents of, or default for, the \f[B]DC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .SH TTY MODE -.PP If \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY, then \[lq]TTY mode\[rq] is considered to be available, and thus, dc(1) can turn on TTY mode, subject to some settings. .PP If there is the environment variable \f[B]DC_TTY_MODE\f[R] in the environment (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then if that environment variable contains a non-zero integer, dc(1) will turn on TTY mode when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY. If the \f[B]DC_TTY_MODE\f[R] environment variable exists but is \f[I]not\f[R] a non-zero integer, then dc(1) will not turn TTY mode on. .PP If the environment variable \f[B]DC_TTY_MODE\f[R] does \f[I]not\f[R] exist, the default setting is used. The default setting can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP TTY mode is different from interactive mode because interactive mode is required in the bc(1) specification (see the \f[B]STANDARDS\f[R] section), and interactive mode requires only \f[B]stdin\f[R] and \f[B]stdout\f[R] to be connected to a terminal. .SS Command-Line History -.PP Command-line history is only enabled if TTY mode is, i.e., that \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the \f[B]DC_TTY_MODE\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and its default do not disable TTY mode. See the \f[B]COMMAND LINE HISTORY\f[R] section for more information. .SS Prompt -.PP If TTY mode is available, then a prompt can be enabled. Like TTY mode itself, it can be turned on or off with an environment variable: \f[B]DC_PROMPT\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP If the environment variable \f[B]DC_PROMPT\f[R] exists and is a non-zero integer, then the prompt is turned on when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options were not used. The read prompt will be turned on under the same conditions, except that the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options must also not be used. .PP However, if \f[B]DC_PROMPT\f[R] does not exist, the prompt can be enabled or disabled with the \f[B]DC_TTY_MODE\f[R] environment variable, the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options, and the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options. See the \f[B]ENVIRONMENT VARIABLES\f[R] and \f[B]OPTIONS\f[R] sections for more details. .SH SIGNAL HANDLING -.PP Sending a \f[B]SIGINT\f[R] will cause dc(1) to do one of two things. .PP If dc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), or the \f[B]DC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or its default, is either not an integer or it is zero, dc(1) will exit. .PP However, if dc(1) is in interactive mode, and the \f[B]DC_SIGINT_RESET\f[R] or its default is an integer and non-zero, then dc(1) will stop executing the current input and reset (see the \f[B]RESET\f[R] section) upon receiving a \f[B]SIGINT\f[R]. .PP Note that \[lq]current input\[rq] can mean one of two things. If dc(1) is processing input from \f[B]stdin\f[R] in interactive mode, it will ask for more input. If dc(1) is processing input from a file in interactive mode, it will stop processing the file and start processing the next file, if one exists, or ask for input from \f[B]stdin\f[R] if no other file exists. .PP This means that if a \f[B]SIGINT\f[R] is sent to dc(1) as it is executing a file, it can seem as though dc(1) did not respond to the signal since it will immediately start executing the next file. This is by design; most files that users execute when interacting with dc(1) have function definitions, which are quick to parse. If a file takes a long time to execute, there may be a bug in that file. The rest of the files could still be executed without problem, allowing the user to continue. .PP \f[B]SIGTERM\f[R] and \f[B]SIGQUIT\f[R] cause dc(1) to clean up and exit, and it uses the default handler for all other signals. The one exception is \f[B]SIGHUP\f[R]; in that case, and only when dc(1) is in TTY mode (see the \f[B]TTY MODE\f[R] section), a \f[B]SIGHUP\f[R] will cause dc(1) to clean up and exit. .SH COMMAND LINE HISTORY -.PP dc(1) supports interactive command-line editing. .PP If dc(1) can be in TTY mode (see the \f[B]TTY MODE\f[R] section), history can be enabled. This means that command-line history can only be enabled when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY. .PP Like TTY mode itself, it can be turned on or off with the environment variable \f[B]DC_TTY_MODE\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP \f[B]Note\f[R]: tabs are converted to 8 spaces. .SH LOCALES -.PP This dc(1) ships with support for adding error messages for different locales and thus, supports \f[B]LC_MESSAGES\f[R]. .SH SEE ALSO -.PP bc(1) .SH STANDARDS -.PP The dc(1) utility operators and some behavior are compliant with the operators in the IEEE Std 1003.1-2017 (\[lq]POSIX.1-2017\[rq]) bc(1) specification at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html . .SH BUGS -.PP None are known. Report bugs at https://git.gavinhoward.com/gavin/bc . .SH AUTHOR -.PP -Gavin D. -Howard and contributors. +Gavin D. Howard \c +.MT gavin@gavinhoward.com +.ME \c +\ and contributors. diff --git a/contrib/bc/manuals/dc/EH.1 b/contrib/bc/manuals/dc/EH.1 index 08cb483dc926..83b62e24b913 100644 --- a/contrib/bc/manuals/dc/EH.1 +++ b/contrib/bc/manuals/dc/EH.1 @@ -1,1479 +1,1445 @@ .\" .\" SPDX-License-Identifier: BSD-2-Clause .\" .\" Copyright (c) 2018-2023 Gavin D. Howard and contributors. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions are met: .\" .\" * Redistributions of source code must retain the above copyright notice, .\" this list of conditions and the following disclaimer. .\" .\" * Redistributions in binary form must reproduce the above copyright notice, .\" this list of conditions and the following disclaimer in the documentation .\" and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" .TH "DC" "1" "February 2023" "Gavin D. Howard" "General Commands Manual" .nh .ad l .SH Name -.PP dc - arbitrary-precision decimal reverse-Polish notation calculator .SH SYNOPSIS -.PP \f[B]dc\f[R] [\f[B]-cChiPRvVx\f[R]] [\f[B]--version\f[R]] [\f[B]--help\f[R]] [\f[B]--digit-clamp\f[R]] [\f[B]--no-digit-clamp\f[R]] [\f[B]--interactive\f[R]] [\f[B]--no-prompt\f[R]] [\f[B]--no-read-prompt\f[R]] [\f[B]--extended-register\f[R]] [\f[B]-e\f[R] \f[I]expr\f[R]] [\f[B]--expression\f[R]=\f[I]expr\f[R]\&...] [\f[B]-f\f[R] \f[I]file\f[R]\&...] [\f[B]--file\f[R]=\f[I]file\f[R]\&...] [\f[I]file\f[R]\&...] .SH DESCRIPTION -.PP dc(1) is an arbitrary-precision calculator. It uses a stack (reverse Polish notation) to store numbers and results of computations. Arithmetic operations pop arguments off of the stack and push the results. .PP If no files are given on the command-line, then dc(1) reads from \f[B]stdin\f[R] (see the \f[B]STDIN\f[R] section). Otherwise, those files are processed, and dc(1) will then exit. .PP If a user wants to set up a standard environment, they can use \f[B]DC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). For example, if a user wants the \f[B]scale\f[R] always set to \f[B]10\f[R], they can set \f[B]DC_ENV_ARGS\f[R] to \f[B]-e 10k\f[R], and this dc(1) will always start with a \f[B]scale\f[R] of \f[B]10\f[R]. .SH OPTIONS -.PP The following are the options that dc(1) accepts. .TP \f[B]-C\f[R], \f[B]--no-digit-clamp\f[R] Disables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that the value added to a number from a digit is always that digit\[cq]s value multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-c\f[R] or \f[B]--digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]DC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-c\f[R], \f[B]--digit-clamp\f[R] Enables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that digits that the value added to a number from a digit that is greater than or equal to the ibase is the value of ibase minus 1 all multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-C\f[R] or \f[B]--no-digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]DC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-e\f[R] \f[I]expr\f[R], \f[B]--expression\f[R]=\f[I]expr\f[R] Evaluates \f[I]expr\f[R]. If multiple expressions are given, they are evaluated in order. If files are given as well (see below), the expressions and files are evaluated in the order given. This means that if a file is given before an expression, the file is read in and evaluated first. .RS .PP If this option is given on the command-line (i.e., not in \f[B]DC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, dc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R], whether on the command-line or in \f[B]DC_ENV_ARGS\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, dc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-f\f[R] \f[I]file\f[R], \f[B]--file\f[R]=\f[I]file\f[R] Reads in \f[I]file\f[R] and evaluates it, line by line, as though it were read through \f[B]stdin\f[R]. If expressions are also given (see above), the expressions are evaluated in the order given. .RS .PP If this option is given on the command-line (i.e., not in \f[B]DC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, dc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, dc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-h\f[R], \f[B]--help\f[R] Prints a usage message and exits. .TP \f[B]-I\f[R] \f[I]ibase\f[R], \f[B]--ibase\f[R]=\f[I]ibase\f[R] Sets the builtin variable \f[B]ibase\f[R] to the value \f[I]ibase\f[R] assuming that \f[I]ibase\f[R] is in base 10. It is a fatal error if \f[I]ibase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-i\f[R], \f[B]--interactive\f[R] Forces interactive mode. (See the \f[B]INTERACTIVE MODE\f[R] section.) .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-L\f[R], \f[B]--no-line-length\f[R] Disables line length checking and prints numbers without backslashes and newlines. In other words, this option sets \f[B]BC_LINE_LENGTH\f[R] to \f[B]0\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-O\f[R] \f[I]obase\f[R], \f[B]--obase\f[R]=\f[I]obase\f[R] Sets the builtin variable \f[B]obase\f[R] to the value \f[I]obase\f[R] assuming that \f[I]obase\f[R] is in base 10. It is a fatal error if \f[I]obase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-P\f[R], \f[B]--no-prompt\f[R] Disables the prompt in TTY mode. (The prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a prompt or are not used to having them in dc(1). Most of those users would want to put this option in \f[B]DC_ENV_ARGS\f[R]. .RS .PP These options override the \f[B]DC_PROMPT\f[R] and \f[B]DC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-R\f[R], \f[B]--no-read-prompt\f[R] Disables the read prompt in TTY mode. (The read prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a read prompt or are not used to having them in dc(1). Most of those users would want to put this option in \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). This option is also useful in hash bang lines of dc(1) scripts that prompt for user input. .RS .PP This option does not disable the regular prompt because the read prompt is only used when the \f[B]?\f[R] command is used. .PP These options \f[I]do\f[R] override the \f[B]DC_PROMPT\f[R] and \f[B]DC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), but only for the read prompt. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-S\f[R] \f[I]scale\f[R], \f[B]--scale\f[R]=\f[I]scale\f[R] Sets the builtin variable \f[B]scale\f[R] to the value \f[I]scale\f[R] assuming that \f[I]scale\f[R] is in base 10. It is a fatal error if \f[I]scale\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-v\f[R], \f[B]-V\f[R], \f[B]--version\f[R] Print the version information (copyright header) and exits. .TP \f[B]-x\f[R] \f[B]--extended-register\f[R] Enables extended register mode. See the \f[I]Extended Register Mode\f[R] subsection of the \f[B]REGISTERS\f[R] section for more information. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-z\f[R], \f[B]--leading-zeroes\f[R] Makes dc(1) print all numbers greater than \f[B]-1\f[R] and less than \f[B]1\f[R], and not equal to \f[B]0\f[R], with a leading zero. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .PP All long options are \f[B]non-portable extensions\f[R]. .SH STDIN -.PP If no files are given on the command-line and no files or expressions are given by the \f[B]-f\f[R], \f[B]--file\f[R], \f[B]-e\f[R], or \f[B]--expression\f[R] options, then dc(1) reads from \f[B]stdin\f[R]. .PP However, there is a caveat to this. .PP First, \f[B]stdin\f[R] is evaluated a line at a time. The only exception to this is if a string has been finished, but not ended. This means that, except for escaped brackets, all brackets must be balanced before dc(1) parses and executes. .SH STDOUT -.PP Any non-error output is written to \f[B]stdout\f[R]. In addition, if history (see the \f[B]HISTORY\f[R] section) and the prompt (see the \f[B]TTY MODE\f[R] section) are enabled, both are output to \f[B]stdout\f[R]. .PP \f[B]Note\f[R]: Unlike other dc(1) implementations, this dc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stdout\f[R], so if \f[B]stdout\f[R] is closed, as in -\f[B]dc >&-\f[R], it will quit with an error. +\f[B]dc >&-\f[R], it will quit with an error. This is done so that dc(1) can report problems when \f[B]stdout\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other dc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stdout\f[R] to \f[B]/dev/null\f[R]. .SH STDERR -.PP Any error output is written to \f[B]stderr\f[R]. .PP \f[B]Note\f[R]: Unlike other dc(1) implementations, this dc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stderr\f[R], so if \f[B]stderr\f[R] is closed, as in -\f[B]dc 2>&-\f[R], it will quit with an error. +\f[B]dc 2>&-\f[R], it will quit with an error. This is done so that dc(1) can exit with an error code when \f[B]stderr\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other dc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stderr\f[R] to \f[B]/dev/null\f[R]. .SH SYNTAX -.PP Each item in the input source code, either a number (see the \f[B]NUMBERS\f[R] section) or a command (see the \f[B]COMMANDS\f[R] section), is processed and executed, in order. Input is processed immediately when entered. .PP \f[B]ibase\f[R] is a register (see the \f[B]REGISTERS\f[R] section) that determines how to interpret constant numbers. It is the \[lq]input\[rq] base, or the number base used for interpreting input numbers. \f[B]ibase\f[R] is initially \f[B]10\f[R]. The max allowable value for \f[B]ibase\f[R] is \f[B]16\f[R]. The min allowable value for \f[B]ibase\f[R] is \f[B]2\f[R]. The max allowable value for \f[B]ibase\f[R] can be queried in dc(1) programs with the \f[B]T\f[R] command. .PP \f[B]obase\f[R] is a register (see the \f[B]REGISTERS\f[R] section) that determines how to output results. It is the \[lq]output\[rq] base, or the number base used for outputting numbers. \f[B]obase\f[R] is initially \f[B]10\f[R]. The max allowable value for \f[B]obase\f[R] is \f[B]DC_BASE_MAX\f[R] and can be queried with the \f[B]U\f[R] command. The min allowable value for \f[B]obase\f[R] is \f[B]2\f[R]. Values are output in the specified base. .PP The \f[I]scale\f[R] of an expression is the number of digits in the result of the expression right of the decimal point, and \f[B]scale\f[R] is a register (see the \f[B]REGISTERS\f[R] section) that sets the precision of any operations (with exceptions). \f[B]scale\f[R] is initially \f[B]0\f[R]. \f[B]scale\f[R] cannot be negative. The max allowable value for \f[B]scale\f[R] can be queried in dc(1) programs with the \f[B]V\f[R] command. .SS Comments -.PP Comments go from \f[B]#\f[R] until, and not including, the next newline. This is a \f[B]non-portable extension\f[R]. .SH NUMBERS -.PP Numbers are strings made up of digits, uppercase letters up to \f[B]F\f[R], and at most \f[B]1\f[R] period for a radix. Numbers can have up to \f[B]DC_NUM_MAX\f[R] digits. Uppercase letters are equal to \f[B]9\f[R] plus their position in the alphabet (i.e., \f[B]A\f[R] equals \f[B]10\f[R], or \f[B]9+1\f[R]). .PP If a digit or letter makes no sense with the current value of \f[B]ibase\f[R] (i.e., they are greater than or equal to the current value of \f[B]ibase\f[R]), then the behavior depends on the existence of the \f[B]-c\f[R]/\f[B]--digit-clamp\f[R] or \f[B]-C\f[R]/\f[B]--no-digit-clamp\f[R] options (see the \f[B]OPTIONS\f[R] section), the existence and setting of the \f[B]DC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or the default, which can be queried with the \f[B]-h\f[R]/\f[B]--help\f[R] option. .PP If clamping is off, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are not changed. Instead, their given value is multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*A+3\[ha]0*B\f[R], which is \f[B]3\f[R] times \f[B]10\f[R] plus \f[B]11\f[R], or \f[B]41\f[R]. .PP If clamping is on, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are set to the value of the highest valid digit in \f[B]ibase\f[R] before being multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*2+3\[ha]0*2\f[R], which is \f[B]3\f[R] times \f[B]2\f[R] plus \f[B]2\f[R], or \f[B]8\f[R]. .PP There is one exception to clamping: single-character numbers (i.e., \f[B]A\f[R] alone). Such numbers are never clamped and always take the value they would have in the highest possible \f[B]ibase\f[R]. This means that \f[B]A\f[R] alone always equals decimal \f[B]10\f[R] and \f[B]Z\f[R] alone always equals decimal \f[B]35\f[R]. This behavior is mandated by the standard for bc(1) (see the STANDARDS section) and is meant to provide an easy way to set the current \f[B]ibase\f[R] (with the \f[B]i\f[R] command) regardless of the current value of \f[B]ibase\f[R]. .PP If clamping is on, and the clamped value of a character is needed, use a leading zero, i.e., for \f[B]A\f[R], use \f[B]0A\f[R]. .SH COMMANDS -.PP The valid commands are listed below. .SS Printing -.PP These commands are used for printing. .TP \f[B]p\f[R] Prints the value on top of the stack, whether number or string, and prints a newline after. .RS .PP This does not alter the stack. .RE .TP \f[B]n\f[R] Prints the value on top of the stack, whether number or string, and pops it off of the stack. .TP \f[B]P\f[R] Pops a value off the stack. .RS .PP If the value is a number, it is truncated and the absolute value of the result is printed as though \f[B]obase\f[R] is \f[B]256\f[R] and each digit is interpreted as an 8-bit ASCII character, making it a byte stream. .PP If the value is a string, it is printed without a trailing newline. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]f\f[R] Prints the entire contents of the stack, in order from newest to oldest, without altering anything. .RS .PP Users should use this command when they get lost. .RE .SS Arithmetic -.PP These are the commands used for arithmetic. .TP \f[B]+\f[R] The top two values are popped off the stack, added, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to the max \f[I]scale\f[R] of both operands. .TP \f[B]-\f[R] The top two values are popped off the stack, subtracted, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to the max \f[I]scale\f[R] of both operands. .TP \f[B]*\f[R] The top two values are popped off the stack, multiplied, and the result is pushed onto the stack. If \f[B]a\f[R] is the \f[I]scale\f[R] of the first expression and \f[B]b\f[R] is the \f[I]scale\f[R] of the second expression, the \f[I]scale\f[R] of the result is equal to \f[B]min(a+b,max(scale,a,b))\f[R] where \f[B]min()\f[R] and \f[B]max()\f[R] return the obvious values. .TP \f[B]/\f[R] The top two values are popped off the stack, divided, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The first value popped off of the stack must be non-zero. .RE .TP \f[B]%\f[R] The top two values are popped off the stack, remaindered, and the result is pushed onto the stack. .RS .PP Remaindering is equivalent to 1) Computing \f[B]a/b\f[R] to current \f[B]scale\f[R], and 2) Using the result of step 1 to calculate \f[B]a-(a/b)*b\f[R] to \f[I]scale\f[R] \f[B]max(scale+scale(b),scale(a))\f[R]. .PP The first value popped off of the stack must be non-zero. .RE .TP \f[B]\[ti]\f[R] The top two values are popped off the stack, divided and remaindered, and the results (divided first, remainder second) are pushed onto the stack. This is equivalent to \f[B]x y / x y %\f[R] except that \f[B]x\f[R] and \f[B]y\f[R] are only evaluated once. .RS .PP The first value popped off of the stack must be non-zero. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[ha]\f[R] The top two values are popped off the stack, the second is raised to the power of the first, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The first value popped off of the stack must be an integer, and if that value is negative, the second value popped off of the stack must be non-zero. .RE .TP \f[B]v\f[R] The top value is popped off the stack, its square root is computed, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The value popped off of the stack must be non-negative. .RE .TP \f[B]_\f[R] If this command \f[I]immediately\f[R] precedes a number (i.e., no spaces or other commands), then that number is input as a negative number. .RS .PP Otherwise, the top value on the stack is popped and copied, and the copy is negated and pushed onto the stack. This behavior without a number is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]b\f[R] The top value is popped off the stack, and if it is zero, it is pushed back onto the stack. Otherwise, its absolute value is pushed onto the stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]|\f[R] The top three values are popped off the stack, a modular exponentiation is computed, and the result is pushed onto the stack. .RS .PP The first value popped is used as the reduction modulus and must be an integer and non-zero. The second value popped is used as the exponent and must be an integer and non-negative. The third value popped is the base and must be an integer. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]G\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if they are equal, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]N\f[R] The top value is popped off of the stack, and if it a \f[B]0\f[R], a \f[B]1\f[R] is pushed; otherwise, a \f[B]0\f[R] is pushed. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B](\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is less than the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]{\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is less than or equal to the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B])\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is greater than the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]}\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is greater than or equal to the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]M\f[R] The top two values are popped off of the stack. If they are both non-zero, a \f[B]1\f[R] is pushed onto the stack. If either of them is zero, or both of them are, then a \f[B]0\f[R] is pushed onto the stack. .RS .PP This is like the \f[B]&&\f[R] operator in bc(1), and it is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]m\f[R] The top two values are popped off of the stack. If at least one of them is non-zero, a \f[B]1\f[R] is pushed onto the stack. If both of them are zero, then a \f[B]0\f[R] is pushed onto the stack. .RS .PP This is like the \f[B]||\f[R] operator in bc(1), and it is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Stack Control -.PP These commands control the stack. .TP \f[B]c\f[R] Removes all items from (\[lq]clears\[rq]) the stack. .TP \f[B]d\f[R] Copies the item on top of the stack (\[lq]duplicates\[rq]) and pushes the copy onto the stack. .TP \f[B]r\f[R] Swaps (\[lq]reverses\[rq]) the two top items on the stack. .TP \f[B]R\f[R] Pops (\[lq]removes\[rq]) the top value from the stack. .SS Register Control -.PP These commands control registers (see the \f[B]REGISTERS\f[R] section). .TP \f[B]s\f[R]\f[I]r\f[R] Pops the value off the top of the stack and stores it into register \f[I]r\f[R]. .TP \f[B]l\f[R]\f[I]r\f[R] Copies the value in register \f[I]r\f[R] and pushes it onto the stack. This does not alter the contents of \f[I]r\f[R]. .TP \f[B]S\f[R]\f[I]r\f[R] Pops the value off the top of the (main) stack and pushes it onto the stack of register \f[I]r\f[R]. The previous value of the register becomes inaccessible. .TP \f[B]L\f[R]\f[I]r\f[R] Pops the value off the top of the stack for register \f[I]r\f[R] and push it onto the main stack. The previous value in the stack for register \f[I]r\f[R], if any, is now accessible via the \f[B]l\f[R]\f[I]r\f[R] command. .SS Parameters -.PP These commands control the values of \f[B]ibase\f[R], \f[B]obase\f[R], and \f[B]scale\f[R]. Also see the \f[B]SYNTAX\f[R] section. .TP \f[B]i\f[R] Pops the value off of the top of the stack and uses it to set \f[B]ibase\f[R], which must be between \f[B]2\f[R] and \f[B]16\f[R], inclusive. .RS .PP If the value on top of the stack has any \f[I]scale\f[R], the \f[I]scale\f[R] is ignored. .RE .TP \f[B]o\f[R] Pops the value off of the top of the stack and uses it to set \f[B]obase\f[R], which must be between \f[B]2\f[R] and \f[B]DC_BASE_MAX\f[R], inclusive (see the \f[B]LIMITS\f[R] section). .RS .PP If the value on top of the stack has any \f[I]scale\f[R], the \f[I]scale\f[R] is ignored. .RE .TP \f[B]k\f[R] Pops the value off of the top of the stack and uses it to set \f[B]scale\f[R], which must be non-negative. .RS .PP If the value on top of the stack has any \f[I]scale\f[R], the \f[I]scale\f[R] is ignored. .RE .TP \f[B]I\f[R] Pushes the current value of \f[B]ibase\f[R] onto the main stack. .TP \f[B]O\f[R] Pushes the current value of \f[B]obase\f[R] onto the main stack. .TP \f[B]K\f[R] Pushes the current value of \f[B]scale\f[R] onto the main stack. .TP \f[B]T\f[R] Pushes the maximum allowable value of \f[B]ibase\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]U\f[R] Pushes the maximum allowable value of \f[B]obase\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]V\f[R] Pushes the maximum allowable value of \f[B]scale\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Strings -.PP The following commands control strings. .PP dc(1) can work with both numbers and strings, and registers (see the \f[B]REGISTERS\f[R] section) can hold both strings and numbers. dc(1) always knows whether the contents of a register are a string or a number. .PP While arithmetic operations have to have numbers, and will print an error if given a string, other commands accept strings. .PP Strings can also be executed as macros. For example, if the string \f[B][1pR]\f[R] is executed as a macro, then the code \f[B]1pR\f[R] is executed, meaning that the \f[B]1\f[R] will be printed with a newline after and then popped from the stack. .TP \f[B][\f[R]\f[I]characters\f[R]\f[B]]\f[R] Makes a string containing \f[I]characters\f[R] and pushes it onto the stack. .RS .PP If there are brackets (\f[B][\f[R] and \f[B]]\f[R]) in the string, then they must be balanced. Unbalanced brackets can be escaped using a backslash (\f[B]\[rs]\f[R]) character. .PP If there is a backslash character in the string, the character after it (even another backslash) is put into the string verbatim, but the (first) backslash is not. .RE .TP \f[B]a\f[R] The value on top of the stack is popped. .RS .PP If it is a number, it is truncated and its absolute value is taken. The result mod \f[B]256\f[R] is calculated. If that result is \f[B]0\f[R], push an empty string; otherwise, push a one-character string where the character is the result of the mod interpreted as an ASCII character. .PP If it is a string, then a new string is made. If the original string is empty, the new string is empty. If it is not, then the first character of the original string is used to create the new string as a one-character string. The new string is then pushed onto the stack. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]x\f[R] Pops a value off of the top of the stack. .RS .PP If it is a number, it is pushed back onto the stack. .PP If it is a string, it is executed as a macro. .PP This behavior is the norm whenever a macro is executed, whether by this command or by the conditional execution commands below. .RE .TP \f[B]>\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is greater than the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP For example, \f[B]0 1>a\f[R] will execute the contents of register \f[B]a\f[R], and \f[B]1 0>a\f[R] will not. .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]>\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]!>\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is not greater than the second (less than or equal to), then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]!>\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]<\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is less than the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]<\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]!<\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is not less than the second (greater than or equal to), then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]!<\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]=\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is equal to the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]=\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]!=\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is not equal to the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]!=\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]?\f[R] Reads a line from the \f[B]stdin\f[R] and executes it. This is to allow macros to request input from users. .TP \f[B]q\f[R] During execution of a macro, this exits the execution of that macro and the execution of the macro that executed it. If there are no macros, or only one macro executing, dc(1) exits. .TP \f[B]Q\f[R] Pops a value from the stack which must be non-negative and is used the number of macro executions to pop off of the execution stack. If the number of levels to pop is greater than the number of executing macros, dc(1) exits. .TP \f[B],\f[R] Pushes the depth of the execution stack onto the stack. The execution stack is the stack of string executions. The number that is pushed onto the stack is exactly as many as is needed to make dc(1) exit with the \f[B]Q\f[R] command, so the sequence \f[B],Q\f[R] will make dc(1) exit. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Status -.PP These commands query status of the stack or its top value. .TP \f[B]Z\f[R] Pops a value off of the stack. .RS .PP If it is a number, calculates the number of significant decimal digits it has and pushes the result. It will push \f[B]1\f[R] if the argument is \f[B]0\f[R] with no decimal places. .PP If it is a string, pushes the number of characters the string has. .RE .TP \f[B]X\f[R] Pops a value off of the stack. .RS .PP If it is a number, pushes the \f[I]scale\f[R] of the value onto the stack. .PP If it is a string, pushes \f[B]0\f[R]. .RE .TP \f[B]u\f[R] Pops one value off of the stack. If the value is a number, this pushes \f[B]1\f[R] onto the stack. Otherwise (if it is a string), it pushes \f[B]0\f[R]. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]t\f[R] Pops one value off of the stack. If the value is a string, this pushes \f[B]1\f[R] onto the stack. Otherwise (if it is a number), it pushes \f[B]0\f[R]. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]z\f[R] Pushes the current depth of the stack (before execution of this command) onto the stack. .TP \f[B]y\f[R]\f[I]r\f[R] Pushes the current stack depth of the register \f[I]r\f[R] onto the main stack. .RS .PP Because each register has a depth of \f[B]1\f[R] (with the value \f[B]0\f[R] in the top item) when dc(1) starts, dc(1) requires that each register\[cq]s stack must always have at least one item; dc(1) will give an error and reset otherwise (see the \f[B]RESET\f[R] section). This means that this command will never push \f[B]0\f[R]. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Arrays -.PP These commands manipulate arrays. .TP \f[B]:\f[R]\f[I]r\f[R] Pops the top two values off of the stack. The second value will be stored in the array \f[I]r\f[R] (see the \f[B]REGISTERS\f[R] section), indexed by the first value. .TP \f[B];\f[R]\f[I]r\f[R] Pops the value on top of the stack and uses it as an index into the array \f[I]r\f[R]. The selected value is then pushed onto the stack. .TP \f[B]Y\f[R]\f[I]r\f[R] Pushes the length of the array \f[I]r\f[R] onto the stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Global Settings -.PP These commands retrieve global settings. These are the only commands that require multiple specific characters, and all of them begin with the letter \f[B]g\f[R]. Only the characters below are allowed after the character \f[B]g\f[R]; any other character produces a parse error (see the \f[B]ERRORS\f[R] section). .TP \f[B]gl\f[R] Pushes the line length set by \f[B]DC_LINE_LENGTH\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) onto the stack. .TP \f[B]gx\f[R] Pushes \f[B]1\f[R] onto the stack if extended register mode is on, \f[B]0\f[R] otherwise. See the \f[I]Extended Register Mode\f[R] subsection of the \f[B]REGISTERS\f[R] section for more information. .TP \f[B]gz\f[R] Pushes \f[B]0\f[R] onto the stack if the leading zero setting has not been enabled with the \f[B]-z\f[R] or \f[B]--leading-zeroes\f[R] options (see the \f[B]OPTIONS\f[R] section), non-zero otherwise. .SH REGISTERS -.PP Registers are names that can store strings, numbers, and arrays. (Number/string registers do not interfere with array registers.) .PP Each register is also its own stack, so the current register value is the top of the stack for the register. All registers, when first referenced, have one value (\f[B]0\f[R]) in their stack, and it is a runtime error to attempt to pop that item off of the register stack. .PP In non-extended register mode, a register name is just the single character that follows any command that needs a register name. The only exceptions are: a newline (\f[B]`\[rs]n'\f[R]) and a left bracket (\f[B]`['\f[R]); it is a parse error for a newline or a left bracket to be used as a register name. .SS Extended Register Mode -.PP Unlike most other dc(1) implentations, this dc(1) provides nearly unlimited amounts of registers, if extended register mode is enabled. .PP If extended register mode is enabled (\f[B]-x\f[R] or \f[B]--extended-register\f[R] command-line arguments are given), then normal single character registers are used \f[I]unless\f[R] the character immediately following a command that needs a register name is a space (according to \f[B]isspace()\f[R]) and not a newline (\f[B]`\[rs]n'\f[R]). .PP In that case, the register name is found according to the regex \f[B][a-z][a-z0-9_]*\f[R] (like bc(1) identifiers), and it is a parse error if the next non-space characters do not match that regex. .SH RESET -.PP When dc(1) encounters an error or a signal that it has a non-default handler for, it resets. This means that several things happen. .PP First, any macros that are executing are stopped and popped off the stack. The behavior is not unlike that of exceptions in programming languages. Then the execution point is set so that any code waiting to execute (after all macros returned) is skipped. .PP Thus, when dc(1) resets, it skips any remaining code waiting to be executed. Then, if it is interactive mode, and the error was not a fatal error (see the \f[B]EXIT STATUS\f[R] section), it asks for more input; otherwise, it exits with the appropriate return code. .SH PERFORMANCE -.PP Most dc(1) implementations use \f[B]char\f[R] types to calculate the value of \f[B]1\f[R] decimal digit at a time, but that can be slow. This dc(1) does something different. .PP It uses large integers to calculate more than \f[B]1\f[R] decimal digit at a time. If built in a environment where \f[B]DC_LONG_BIT\f[R] (see the \f[B]LIMITS\f[R] section) is \f[B]64\f[R], then each integer has \f[B]9\f[R] decimal digits. If built in an environment where \f[B]DC_LONG_BIT\f[R] is \f[B]32\f[R] then each integer has \f[B]4\f[R] decimal digits. This value (the number of decimal digits per large integer) is called \f[B]DC_BASE_DIGS\f[R]. .PP In addition, this dc(1) uses an even larger integer for overflow checking. This integer type depends on the value of \f[B]DC_LONG_BIT\f[R], but is always at least twice as large as the integer type used to store digits. .SH LIMITS -.PP The following are the limits on dc(1): .TP \f[B]DC_LONG_BIT\f[R] The number of bits in the \f[B]long\f[R] type in the environment where dc(1) was built. This determines how many decimal digits can be stored in a single large integer (see the \f[B]PERFORMANCE\f[R] section). .TP \f[B]DC_BASE_DIGS\f[R] The number of decimal digits per large integer (see the \f[B]PERFORMANCE\f[R] section). Depends on \f[B]DC_LONG_BIT\f[R]. .TP \f[B]DC_BASE_POW\f[R] The max decimal number that each large integer can store (see \f[B]DC_BASE_DIGS\f[R]) plus \f[B]1\f[R]. Depends on \f[B]DC_BASE_DIGS\f[R]. .TP \f[B]DC_OVERFLOW_MAX\f[R] The max number that the overflow type (see the \f[B]PERFORMANCE\f[R] section) can hold. Depends on \f[B]DC_LONG_BIT\f[R]. .TP \f[B]DC_BASE_MAX\f[R] The maximum output base. Set at \f[B]DC_BASE_POW\f[R]. .TP \f[B]DC_DIM_MAX\f[R] The maximum size of arrays. Set at \f[B]SIZE_MAX-1\f[R]. .TP \f[B]DC_SCALE_MAX\f[R] The maximum \f[B]scale\f[R]. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_STRING_MAX\f[R] The maximum length of strings. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_NAME_MAX\f[R] The maximum length of identifiers. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_NUM_MAX\f[R] The maximum length of a number (in decimal digits), which includes digits after the decimal point. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP Exponent The maximum allowable exponent (positive or negative). Set at \f[B]DC_OVERFLOW_MAX\f[R]. .TP Number of vars The maximum number of vars/arrays. Set at \f[B]SIZE_MAX-1\f[R]. .PP These limits are meant to be effectively non-existent; the limits are so large (at least on 64-bit machines) that there should not be any point at which they become a problem. In fact, memory should be exhausted before these limits should be hit. .SH ENVIRONMENT VARIABLES -.PP As \f[B]non-portable extensions\f[R], dc(1) recognizes the following environment variables: .TP \f[B]DC_ENV_ARGS\f[R] This is another way to give command-line arguments to dc(1). They should be in the same format as all other command-line arguments. These are always processed first, so any files given in \f[B]DC_ENV_ARGS\f[R] will be processed before arguments and files given on the command-line. This gives the user the ability to set up \[lq]standard\[rq] options and files to be used at every invocation. The most useful thing for such files to contain would be useful functions that the user might want every time dc(1) runs. Another use would be to use the \f[B]-e\f[R] option to set \f[B]scale\f[R] to a value other than \f[B]0\f[R]. .RS .PP The code that parses \f[B]DC_ENV_ARGS\f[R] will correctly handle quoted arguments, but it does not understand escape sequences. For example, the string \f[B]\[lq]/home/gavin/some dc file.dc\[rq]\f[R] will be correctly parsed, but the string \f[B]\[lq]/home/gavin/some \[dq]dc\[dq] file.dc\[rq]\f[R] will include the backslashes. .PP The quote parsing will handle either kind of quotes, \f[B]\[cq]\f[R] or \f[B]\[lq]\f[R]. Thus, if you have a file with any number of single quotes in the name, you can use double quotes as the outside quotes, as in \f[B]\[lq]some `dc' file.dc\[rq]\f[R], and vice versa if you have a file with double quotes. However, handling a file with both kinds of quotes in \f[B]DC_ENV_ARGS\f[R] is not supported due to the complexity of the parsing, though such files are still supported on the command-line where the parsing is done by the shell. .RE .TP \f[B]DC_LINE_LENGTH\f[R] If this environment variable exists and contains an integer that is greater than \f[B]1\f[R] and is less than \f[B]UINT16_MAX\f[R] (\f[B]2\[ha]16-1\f[R]), dc(1) will output lines to that length, including the backslash newline combo. The default line length is \f[B]70\f[R]. .RS .PP The special value of \f[B]0\f[R] will disable line length checking and print numbers without regard to line length and without backslashes and newlines. .RE .TP \f[B]DC_SIGINT_RESET\f[R] If dc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), then this environment variable has no effect because dc(1) exits on \f[B]SIGINT\f[R] when not in interactive mode. .RS .PP However, when dc(1) is in interactive mode, then if this environment variable exists and contains an integer, a non-zero value makes dc(1) reset on \f[B]SIGINT\f[R], rather than exit, and zero makes dc(1) exit. If this environment variable exists and is \f[I]not\f[R] an integer, then dc(1) will exit on \f[B]SIGINT\f[R]. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_TTY_MODE\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, then a non-zero value makes dc(1) use TTY mode, and zero makes dc(1) not use TTY mode. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_PROMPT\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, a non-zero value makes dc(1) use a prompt, and zero or a non-integer makes dc(1) not use a prompt. If this environment variable does not exist and \f[B]DC_TTY_MODE\f[R] does, then the value of the \f[B]DC_TTY_MODE\f[R] environment variable is used. .PP This environment variable and the \f[B]DC_TTY_MODE\f[R] environment variable override the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_EXPR_EXIT\f[R] If any expressions or expression files are given on the command-line with \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R], then if this environment variable exists and contains an integer, a non-zero value makes dc(1) exit after executing the expressions and expression files, and a zero value makes dc(1) not exit. .RS .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_DIGIT_CLAMP\f[R] When parsing numbers and if this environment variable exists and contains an integer, a non-zero value makes dc(1) clamp digits that are greater than or equal to the current \f[B]ibase\f[R] so that all such digits are considered equal to the \f[B]ibase\f[R] minus 1, and a zero value disables such clamping so that those digits are always equal to their value, which is multiplied by the power of the \f[B]ibase\f[R]. .RS .PP This never applies to single-digit numbers, as per the bc(1) standard (see the \f[B]STANDARDS\f[R] section). .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .SH EXIT STATUS -.PP dc(1) returns the following exit statuses: .TP \f[B]0\f[R] No error. .TP \f[B]1\f[R] A math error occurred. This follows standard practice of using \f[B]1\f[R] for expected errors, since math errors will happen in the process of normal execution. .RS .PP Math errors include divide by \f[B]0\f[R], taking the square root of a negative number, attempting to convert a negative number to a hardware integer, overflow when converting a number to a hardware integer, overflow when calculating the size of a number, and attempting to use a non-integer where an integer is required. .PP Converting to a hardware integer happens for the second operand of the power (\f[B]\[ha]\f[R]) operator. .RE .TP \f[B]2\f[R] A parse error occurred. .RS .PP Parse errors include unexpected \f[B]EOF\f[R], using an invalid character, failing to find the end of a string or comment, and using a token where it is invalid. .RE .TP \f[B]3\f[R] A runtime error occurred. .RS .PP Runtime errors include assigning an invalid number to any global (\f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R]), giving a bad expression to a \f[B]read()\f[R] call, calling \f[B]read()\f[R] inside of a \f[B]read()\f[R] call, type errors (including attempting to execute a number), and attempting an operation when the stack has too few elements. .RE .TP \f[B]4\f[R] A fatal error occurred. .RS .PP Fatal errors include memory allocation errors, I/O errors, failing to open files, attempting to use files that do not have only ASCII characters (dc(1) only accepts ASCII characters), attempting to open a directory as a file, and giving invalid command-line options. .RE .PP The exit status \f[B]4\f[R] is special; when a fatal error occurs, dc(1) always exits and returns \f[B]4\f[R], no matter what mode dc(1) is in. .PP The other statuses will only be returned when dc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), since dc(1) resets its state (see the \f[B]RESET\f[R] section) and accepts more input when one of those errors occurs in interactive mode. This is also the case when interactive mode is forced by the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .PP These exit statuses allow dc(1) to be used in shell scripting with error checking, and its normal behavior can be forced by using the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .SH INTERACTIVE MODE -.PP Like bc(1), dc(1) has an interactive mode and a non-interactive mode. Interactive mode is turned on automatically when both \f[B]stdin\f[R] and \f[B]stdout\f[R] are hooked to a terminal, but the \f[B]-i\f[R] flag and \f[B]--interactive\f[R] option can turn it on in other situations. .PP In interactive mode, dc(1) attempts to recover from errors (see the \f[B]RESET\f[R] section), and in normal execution, flushes \f[B]stdout\f[R] as soon as execution is done for the current input. dc(1) may also reset on \f[B]SIGINT\f[R] instead of exit, depending on the contents of, or default for, the \f[B]DC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .SH TTY MODE -.PP If \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY, then \[lq]TTY mode\[rq] is considered to be available, and thus, dc(1) can turn on TTY mode, subject to some settings. .PP If there is the environment variable \f[B]DC_TTY_MODE\f[R] in the environment (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then if that environment variable contains a non-zero integer, dc(1) will turn on TTY mode when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY. If the \f[B]DC_TTY_MODE\f[R] environment variable exists but is \f[I]not\f[R] a non-zero integer, then dc(1) will not turn TTY mode on. .PP If the environment variable \f[B]DC_TTY_MODE\f[R] does \f[I]not\f[R] exist, the default setting is used. The default setting can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP TTY mode is different from interactive mode because interactive mode is required in the bc(1) specification (see the \f[B]STANDARDS\f[R] section), and interactive mode requires only \f[B]stdin\f[R] and \f[B]stdout\f[R] to be connected to a terminal. .SS Prompt -.PP If TTY mode is available, then a prompt can be enabled. Like TTY mode itself, it can be turned on or off with an environment variable: \f[B]DC_PROMPT\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP If the environment variable \f[B]DC_PROMPT\f[R] exists and is a non-zero integer, then the prompt is turned on when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options were not used. The read prompt will be turned on under the same conditions, except that the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options must also not be used. .PP However, if \f[B]DC_PROMPT\f[R] does not exist, the prompt can be enabled or disabled with the \f[B]DC_TTY_MODE\f[R] environment variable, the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options, and the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options. See the \f[B]ENVIRONMENT VARIABLES\f[R] and \f[B]OPTIONS\f[R] sections for more details. .SH SIGNAL HANDLING -.PP Sending a \f[B]SIGINT\f[R] will cause dc(1) to do one of two things. .PP If dc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), or the \f[B]DC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or its default, is either not an integer or it is zero, dc(1) will exit. .PP However, if dc(1) is in interactive mode, and the \f[B]DC_SIGINT_RESET\f[R] or its default is an integer and non-zero, then dc(1) will stop executing the current input and reset (see the \f[B]RESET\f[R] section) upon receiving a \f[B]SIGINT\f[R]. .PP Note that \[lq]current input\[rq] can mean one of two things. If dc(1) is processing input from \f[B]stdin\f[R] in interactive mode, it will ask for more input. If dc(1) is processing input from a file in interactive mode, it will stop processing the file and start processing the next file, if one exists, or ask for input from \f[B]stdin\f[R] if no other file exists. .PP This means that if a \f[B]SIGINT\f[R] is sent to dc(1) as it is executing a file, it can seem as though dc(1) did not respond to the signal since it will immediately start executing the next file. This is by design; most files that users execute when interacting with dc(1) have function definitions, which are quick to parse. If a file takes a long time to execute, there may be a bug in that file. The rest of the files could still be executed without problem, allowing the user to continue. .PP \f[B]SIGTERM\f[R] and \f[B]SIGQUIT\f[R] cause dc(1) to clean up and exit, and it uses the default handler for all other signals. .SH LOCALES -.PP This dc(1) ships with support for adding error messages for different locales and thus, supports \f[B]LC_MESSAGES\f[R]. .SH SEE ALSO -.PP bc(1) .SH STANDARDS -.PP The dc(1) utility operators and some behavior are compliant with the operators in the IEEE Std 1003.1-2017 (\[lq]POSIX.1-2017\[rq]) bc(1) specification at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html . .SH BUGS -.PP None are known. Report bugs at https://git.gavinhoward.com/gavin/bc . .SH AUTHOR -.PP -Gavin D. -Howard and contributors. +Gavin D. Howard \c +.MT gavin@gavinhoward.com +.ME \c +\ and contributors. diff --git a/contrib/bc/manuals/dc/EHN.1 b/contrib/bc/manuals/dc/EHN.1 index efeb0bbaecf9..5df155cf5253 100644 --- a/contrib/bc/manuals/dc/EHN.1 +++ b/contrib/bc/manuals/dc/EHN.1 @@ -1,1475 +1,1442 @@ .\" .\" SPDX-License-Identifier: BSD-2-Clause .\" .\" Copyright (c) 2018-2023 Gavin D. Howard and contributors. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions are met: .\" .\" * Redistributions of source code must retain the above copyright notice, .\" this list of conditions and the following disclaimer. .\" .\" * Redistributions in binary form must reproduce the above copyright notice, .\" this list of conditions and the following disclaimer in the documentation .\" and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" .TH "DC" "1" "February 2023" "Gavin D. Howard" "General Commands Manual" .nh .ad l .SH Name -.PP dc - arbitrary-precision decimal reverse-Polish notation calculator .SH SYNOPSIS -.PP \f[B]dc\f[R] [\f[B]-cChiPRvVx\f[R]] [\f[B]--version\f[R]] [\f[B]--help\f[R]] [\f[B]--digit-clamp\f[R]] [\f[B]--no-digit-clamp\f[R]] [\f[B]--interactive\f[R]] [\f[B]--no-prompt\f[R]] [\f[B]--no-read-prompt\f[R]] [\f[B]--extended-register\f[R]] [\f[B]-e\f[R] \f[I]expr\f[R]] [\f[B]--expression\f[R]=\f[I]expr\f[R]\&...] [\f[B]-f\f[R] \f[I]file\f[R]\&...] [\f[B]--file\f[R]=\f[I]file\f[R]\&...] [\f[I]file\f[R]\&...] .SH DESCRIPTION -.PP dc(1) is an arbitrary-precision calculator. It uses a stack (reverse Polish notation) to store numbers and results of computations. Arithmetic operations pop arguments off of the stack and push the results. .PP If no files are given on the command-line, then dc(1) reads from \f[B]stdin\f[R] (see the \f[B]STDIN\f[R] section). Otherwise, those files are processed, and dc(1) will then exit. .PP If a user wants to set up a standard environment, they can use \f[B]DC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). For example, if a user wants the \f[B]scale\f[R] always set to \f[B]10\f[R], they can set \f[B]DC_ENV_ARGS\f[R] to \f[B]-e 10k\f[R], and this dc(1) will always start with a \f[B]scale\f[R] of \f[B]10\f[R]. .SH OPTIONS -.PP The following are the options that dc(1) accepts. .TP \f[B]-C\f[R], \f[B]--no-digit-clamp\f[R] Disables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that the value added to a number from a digit is always that digit\[cq]s value multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-c\f[R] or \f[B]--digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]DC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-c\f[R], \f[B]--digit-clamp\f[R] Enables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that digits that the value added to a number from a digit that is greater than or equal to the ibase is the value of ibase minus 1 all multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-C\f[R] or \f[B]--no-digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]DC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-e\f[R] \f[I]expr\f[R], \f[B]--expression\f[R]=\f[I]expr\f[R] Evaluates \f[I]expr\f[R]. If multiple expressions are given, they are evaluated in order. If files are given as well (see below), the expressions and files are evaluated in the order given. This means that if a file is given before an expression, the file is read in and evaluated first. .RS .PP If this option is given on the command-line (i.e., not in \f[B]DC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, dc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R], whether on the command-line or in \f[B]DC_ENV_ARGS\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, dc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-f\f[R] \f[I]file\f[R], \f[B]--file\f[R]=\f[I]file\f[R] Reads in \f[I]file\f[R] and evaluates it, line by line, as though it were read through \f[B]stdin\f[R]. If expressions are also given (see above), the expressions are evaluated in the order given. .RS .PP If this option is given on the command-line (i.e., not in \f[B]DC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, dc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, dc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-h\f[R], \f[B]--help\f[R] Prints a usage message and exits. .TP \f[B]-I\f[R] \f[I]ibase\f[R], \f[B]--ibase\f[R]=\f[I]ibase\f[R] Sets the builtin variable \f[B]ibase\f[R] to the value \f[I]ibase\f[R] assuming that \f[I]ibase\f[R] is in base 10. It is a fatal error if \f[I]ibase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-i\f[R], \f[B]--interactive\f[R] Forces interactive mode. (See the \f[B]INTERACTIVE MODE\f[R] section.) .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-L\f[R], \f[B]--no-line-length\f[R] Disables line length checking and prints numbers without backslashes and newlines. In other words, this option sets \f[B]BC_LINE_LENGTH\f[R] to \f[B]0\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-O\f[R] \f[I]obase\f[R], \f[B]--obase\f[R]=\f[I]obase\f[R] Sets the builtin variable \f[B]obase\f[R] to the value \f[I]obase\f[R] assuming that \f[I]obase\f[R] is in base 10. It is a fatal error if \f[I]obase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-P\f[R], \f[B]--no-prompt\f[R] Disables the prompt in TTY mode. (The prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a prompt or are not used to having them in dc(1). Most of those users would want to put this option in \f[B]DC_ENV_ARGS\f[R]. .RS .PP These options override the \f[B]DC_PROMPT\f[R] and \f[B]DC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-R\f[R], \f[B]--no-read-prompt\f[R] Disables the read prompt in TTY mode. (The read prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a read prompt or are not used to having them in dc(1). Most of those users would want to put this option in \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). This option is also useful in hash bang lines of dc(1) scripts that prompt for user input. .RS .PP This option does not disable the regular prompt because the read prompt is only used when the \f[B]?\f[R] command is used. .PP These options \f[I]do\f[R] override the \f[B]DC_PROMPT\f[R] and \f[B]DC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), but only for the read prompt. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-S\f[R] \f[I]scale\f[R], \f[B]--scale\f[R]=\f[I]scale\f[R] Sets the builtin variable \f[B]scale\f[R] to the value \f[I]scale\f[R] assuming that \f[I]scale\f[R] is in base 10. It is a fatal error if \f[I]scale\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-v\f[R], \f[B]-V\f[R], \f[B]--version\f[R] Print the version information (copyright header) and exits. .TP \f[B]-x\f[R] \f[B]--extended-register\f[R] Enables extended register mode. See the \f[I]Extended Register Mode\f[R] subsection of the \f[B]REGISTERS\f[R] section for more information. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-z\f[R], \f[B]--leading-zeroes\f[R] Makes dc(1) print all numbers greater than \f[B]-1\f[R] and less than \f[B]1\f[R], and not equal to \f[B]0\f[R], with a leading zero. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .PP All long options are \f[B]non-portable extensions\f[R]. .SH STDIN -.PP If no files are given on the command-line and no files or expressions are given by the \f[B]-f\f[R], \f[B]--file\f[R], \f[B]-e\f[R], or \f[B]--expression\f[R] options, then dc(1) reads from \f[B]stdin\f[R]. .PP However, there is a caveat to this. .PP First, \f[B]stdin\f[R] is evaluated a line at a time. The only exception to this is if a string has been finished, but not ended. This means that, except for escaped brackets, all brackets must be balanced before dc(1) parses and executes. .SH STDOUT -.PP Any non-error output is written to \f[B]stdout\f[R]. In addition, if history (see the \f[B]HISTORY\f[R] section) and the prompt (see the \f[B]TTY MODE\f[R] section) are enabled, both are output to \f[B]stdout\f[R]. .PP \f[B]Note\f[R]: Unlike other dc(1) implementations, this dc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stdout\f[R], so if \f[B]stdout\f[R] is closed, as in -\f[B]dc >&-\f[R], it will quit with an error. +\f[B]dc >&-\f[R], it will quit with an error. This is done so that dc(1) can report problems when \f[B]stdout\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other dc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stdout\f[R] to \f[B]/dev/null\f[R]. .SH STDERR -.PP Any error output is written to \f[B]stderr\f[R]. .PP \f[B]Note\f[R]: Unlike other dc(1) implementations, this dc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stderr\f[R], so if \f[B]stderr\f[R] is closed, as in -\f[B]dc 2>&-\f[R], it will quit with an error. +\f[B]dc 2>&-\f[R], it will quit with an error. This is done so that dc(1) can exit with an error code when \f[B]stderr\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other dc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stderr\f[R] to \f[B]/dev/null\f[R]. .SH SYNTAX -.PP Each item in the input source code, either a number (see the \f[B]NUMBERS\f[R] section) or a command (see the \f[B]COMMANDS\f[R] section), is processed and executed, in order. Input is processed immediately when entered. .PP \f[B]ibase\f[R] is a register (see the \f[B]REGISTERS\f[R] section) that determines how to interpret constant numbers. It is the \[lq]input\[rq] base, or the number base used for interpreting input numbers. \f[B]ibase\f[R] is initially \f[B]10\f[R]. The max allowable value for \f[B]ibase\f[R] is \f[B]16\f[R]. The min allowable value for \f[B]ibase\f[R] is \f[B]2\f[R]. The max allowable value for \f[B]ibase\f[R] can be queried in dc(1) programs with the \f[B]T\f[R] command. .PP \f[B]obase\f[R] is a register (see the \f[B]REGISTERS\f[R] section) that determines how to output results. It is the \[lq]output\[rq] base, or the number base used for outputting numbers. \f[B]obase\f[R] is initially \f[B]10\f[R]. The max allowable value for \f[B]obase\f[R] is \f[B]DC_BASE_MAX\f[R] and can be queried with the \f[B]U\f[R] command. The min allowable value for \f[B]obase\f[R] is \f[B]2\f[R]. Values are output in the specified base. .PP The \f[I]scale\f[R] of an expression is the number of digits in the result of the expression right of the decimal point, and \f[B]scale\f[R] is a register (see the \f[B]REGISTERS\f[R] section) that sets the precision of any operations (with exceptions). \f[B]scale\f[R] is initially \f[B]0\f[R]. \f[B]scale\f[R] cannot be negative. The max allowable value for \f[B]scale\f[R] can be queried in dc(1) programs with the \f[B]V\f[R] command. .SS Comments -.PP Comments go from \f[B]#\f[R] until, and not including, the next newline. This is a \f[B]non-portable extension\f[R]. .SH NUMBERS -.PP Numbers are strings made up of digits, uppercase letters up to \f[B]F\f[R], and at most \f[B]1\f[R] period for a radix. Numbers can have up to \f[B]DC_NUM_MAX\f[R] digits. Uppercase letters are equal to \f[B]9\f[R] plus their position in the alphabet (i.e., \f[B]A\f[R] equals \f[B]10\f[R], or \f[B]9+1\f[R]). .PP If a digit or letter makes no sense with the current value of \f[B]ibase\f[R] (i.e., they are greater than or equal to the current value of \f[B]ibase\f[R]), then the behavior depends on the existence of the \f[B]-c\f[R]/\f[B]--digit-clamp\f[R] or \f[B]-C\f[R]/\f[B]--no-digit-clamp\f[R] options (see the \f[B]OPTIONS\f[R] section), the existence and setting of the \f[B]DC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or the default, which can be queried with the \f[B]-h\f[R]/\f[B]--help\f[R] option. .PP If clamping is off, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are not changed. Instead, their given value is multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*A+3\[ha]0*B\f[R], which is \f[B]3\f[R] times \f[B]10\f[R] plus \f[B]11\f[R], or \f[B]41\f[R]. .PP If clamping is on, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are set to the value of the highest valid digit in \f[B]ibase\f[R] before being multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*2+3\[ha]0*2\f[R], which is \f[B]3\f[R] times \f[B]2\f[R] plus \f[B]2\f[R], or \f[B]8\f[R]. .PP There is one exception to clamping: single-character numbers (i.e., \f[B]A\f[R] alone). Such numbers are never clamped and always take the value they would have in the highest possible \f[B]ibase\f[R]. This means that \f[B]A\f[R] alone always equals decimal \f[B]10\f[R] and \f[B]Z\f[R] alone always equals decimal \f[B]35\f[R]. This behavior is mandated by the standard for bc(1) (see the STANDARDS section) and is meant to provide an easy way to set the current \f[B]ibase\f[R] (with the \f[B]i\f[R] command) regardless of the current value of \f[B]ibase\f[R]. .PP If clamping is on, and the clamped value of a character is needed, use a leading zero, i.e., for \f[B]A\f[R], use \f[B]0A\f[R]. .SH COMMANDS -.PP The valid commands are listed below. .SS Printing -.PP These commands are used for printing. .TP \f[B]p\f[R] Prints the value on top of the stack, whether number or string, and prints a newline after. .RS .PP This does not alter the stack. .RE .TP \f[B]n\f[R] Prints the value on top of the stack, whether number or string, and pops it off of the stack. .TP \f[B]P\f[R] Pops a value off the stack. .RS .PP If the value is a number, it is truncated and the absolute value of the result is printed as though \f[B]obase\f[R] is \f[B]256\f[R] and each digit is interpreted as an 8-bit ASCII character, making it a byte stream. .PP If the value is a string, it is printed without a trailing newline. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]f\f[R] Prints the entire contents of the stack, in order from newest to oldest, without altering anything. .RS .PP Users should use this command when they get lost. .RE .SS Arithmetic -.PP These are the commands used for arithmetic. .TP \f[B]+\f[R] The top two values are popped off the stack, added, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to the max \f[I]scale\f[R] of both operands. .TP \f[B]-\f[R] The top two values are popped off the stack, subtracted, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to the max \f[I]scale\f[R] of both operands. .TP \f[B]*\f[R] The top two values are popped off the stack, multiplied, and the result is pushed onto the stack. If \f[B]a\f[R] is the \f[I]scale\f[R] of the first expression and \f[B]b\f[R] is the \f[I]scale\f[R] of the second expression, the \f[I]scale\f[R] of the result is equal to \f[B]min(a+b,max(scale,a,b))\f[R] where \f[B]min()\f[R] and \f[B]max()\f[R] return the obvious values. .TP \f[B]/\f[R] The top two values are popped off the stack, divided, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The first value popped off of the stack must be non-zero. .RE .TP \f[B]%\f[R] The top two values are popped off the stack, remaindered, and the result is pushed onto the stack. .RS .PP Remaindering is equivalent to 1) Computing \f[B]a/b\f[R] to current \f[B]scale\f[R], and 2) Using the result of step 1 to calculate \f[B]a-(a/b)*b\f[R] to \f[I]scale\f[R] \f[B]max(scale+scale(b),scale(a))\f[R]. .PP The first value popped off of the stack must be non-zero. .RE .TP \f[B]\[ti]\f[R] The top two values are popped off the stack, divided and remaindered, and the results (divided first, remainder second) are pushed onto the stack. This is equivalent to \f[B]x y / x y %\f[R] except that \f[B]x\f[R] and \f[B]y\f[R] are only evaluated once. .RS .PP The first value popped off of the stack must be non-zero. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[ha]\f[R] The top two values are popped off the stack, the second is raised to the power of the first, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The first value popped off of the stack must be an integer, and if that value is negative, the second value popped off of the stack must be non-zero. .RE .TP \f[B]v\f[R] The top value is popped off the stack, its square root is computed, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The value popped off of the stack must be non-negative. .RE .TP \f[B]_\f[R] If this command \f[I]immediately\f[R] precedes a number (i.e., no spaces or other commands), then that number is input as a negative number. .RS .PP Otherwise, the top value on the stack is popped and copied, and the copy is negated and pushed onto the stack. This behavior without a number is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]b\f[R] The top value is popped off the stack, and if it is zero, it is pushed back onto the stack. Otherwise, its absolute value is pushed onto the stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]|\f[R] The top three values are popped off the stack, a modular exponentiation is computed, and the result is pushed onto the stack. .RS .PP The first value popped is used as the reduction modulus and must be an integer and non-zero. The second value popped is used as the exponent and must be an integer and non-negative. The third value popped is the base and must be an integer. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]G\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if they are equal, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]N\f[R] The top value is popped off of the stack, and if it a \f[B]0\f[R], a \f[B]1\f[R] is pushed; otherwise, a \f[B]0\f[R] is pushed. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B](\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is less than the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]{\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is less than or equal to the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B])\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is greater than the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]}\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is greater than or equal to the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]M\f[R] The top two values are popped off of the stack. If they are both non-zero, a \f[B]1\f[R] is pushed onto the stack. If either of them is zero, or both of them are, then a \f[B]0\f[R] is pushed onto the stack. .RS .PP This is like the \f[B]&&\f[R] operator in bc(1), and it is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]m\f[R] The top two values are popped off of the stack. If at least one of them is non-zero, a \f[B]1\f[R] is pushed onto the stack. If both of them are zero, then a \f[B]0\f[R] is pushed onto the stack. .RS .PP This is like the \f[B]||\f[R] operator in bc(1), and it is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Stack Control -.PP These commands control the stack. .TP \f[B]c\f[R] Removes all items from (\[lq]clears\[rq]) the stack. .TP \f[B]d\f[R] Copies the item on top of the stack (\[lq]duplicates\[rq]) and pushes the copy onto the stack. .TP \f[B]r\f[R] Swaps (\[lq]reverses\[rq]) the two top items on the stack. .TP \f[B]R\f[R] Pops (\[lq]removes\[rq]) the top value from the stack. .SS Register Control -.PP These commands control registers (see the \f[B]REGISTERS\f[R] section). .TP \f[B]s\f[R]\f[I]r\f[R] Pops the value off the top of the stack and stores it into register \f[I]r\f[R]. .TP \f[B]l\f[R]\f[I]r\f[R] Copies the value in register \f[I]r\f[R] and pushes it onto the stack. This does not alter the contents of \f[I]r\f[R]. .TP \f[B]S\f[R]\f[I]r\f[R] Pops the value off the top of the (main) stack and pushes it onto the stack of register \f[I]r\f[R]. The previous value of the register becomes inaccessible. .TP \f[B]L\f[R]\f[I]r\f[R] Pops the value off the top of the stack for register \f[I]r\f[R] and push it onto the main stack. The previous value in the stack for register \f[I]r\f[R], if any, is now accessible via the \f[B]l\f[R]\f[I]r\f[R] command. .SS Parameters -.PP These commands control the values of \f[B]ibase\f[R], \f[B]obase\f[R], and \f[B]scale\f[R]. Also see the \f[B]SYNTAX\f[R] section. .TP \f[B]i\f[R] Pops the value off of the top of the stack and uses it to set \f[B]ibase\f[R], which must be between \f[B]2\f[R] and \f[B]16\f[R], inclusive. .RS .PP If the value on top of the stack has any \f[I]scale\f[R], the \f[I]scale\f[R] is ignored. .RE .TP \f[B]o\f[R] Pops the value off of the top of the stack and uses it to set \f[B]obase\f[R], which must be between \f[B]2\f[R] and \f[B]DC_BASE_MAX\f[R], inclusive (see the \f[B]LIMITS\f[R] section). .RS .PP If the value on top of the stack has any \f[I]scale\f[R], the \f[I]scale\f[R] is ignored. .RE .TP \f[B]k\f[R] Pops the value off of the top of the stack and uses it to set \f[B]scale\f[R], which must be non-negative. .RS .PP If the value on top of the stack has any \f[I]scale\f[R], the \f[I]scale\f[R] is ignored. .RE .TP \f[B]I\f[R] Pushes the current value of \f[B]ibase\f[R] onto the main stack. .TP \f[B]O\f[R] Pushes the current value of \f[B]obase\f[R] onto the main stack. .TP \f[B]K\f[R] Pushes the current value of \f[B]scale\f[R] onto the main stack. .TP \f[B]T\f[R] Pushes the maximum allowable value of \f[B]ibase\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]U\f[R] Pushes the maximum allowable value of \f[B]obase\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]V\f[R] Pushes the maximum allowable value of \f[B]scale\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Strings -.PP The following commands control strings. .PP dc(1) can work with both numbers and strings, and registers (see the \f[B]REGISTERS\f[R] section) can hold both strings and numbers. dc(1) always knows whether the contents of a register are a string or a number. .PP While arithmetic operations have to have numbers, and will print an error if given a string, other commands accept strings. .PP Strings can also be executed as macros. For example, if the string \f[B][1pR]\f[R] is executed as a macro, then the code \f[B]1pR\f[R] is executed, meaning that the \f[B]1\f[R] will be printed with a newline after and then popped from the stack. .TP \f[B][\f[R]\f[I]characters\f[R]\f[B]]\f[R] Makes a string containing \f[I]characters\f[R] and pushes it onto the stack. .RS .PP If there are brackets (\f[B][\f[R] and \f[B]]\f[R]) in the string, then they must be balanced. Unbalanced brackets can be escaped using a backslash (\f[B]\[rs]\f[R]) character. .PP If there is a backslash character in the string, the character after it (even another backslash) is put into the string verbatim, but the (first) backslash is not. .RE .TP \f[B]a\f[R] The value on top of the stack is popped. .RS .PP If it is a number, it is truncated and its absolute value is taken. The result mod \f[B]256\f[R] is calculated. If that result is \f[B]0\f[R], push an empty string; otherwise, push a one-character string where the character is the result of the mod interpreted as an ASCII character. .PP If it is a string, then a new string is made. If the original string is empty, the new string is empty. If it is not, then the first character of the original string is used to create the new string as a one-character string. The new string is then pushed onto the stack. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]x\f[R] Pops a value off of the top of the stack. .RS .PP If it is a number, it is pushed back onto the stack. .PP If it is a string, it is executed as a macro. .PP This behavior is the norm whenever a macro is executed, whether by this command or by the conditional execution commands below. .RE .TP \f[B]>\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is greater than the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP For example, \f[B]0 1>a\f[R] will execute the contents of register \f[B]a\f[R], and \f[B]1 0>a\f[R] will not. .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]>\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]!>\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is not greater than the second (less than or equal to), then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]!>\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]<\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is less than the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]<\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]!<\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is not less than the second (greater than or equal to), then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]!<\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]=\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is equal to the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]=\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]!=\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is not equal to the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]!=\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]?\f[R] Reads a line from the \f[B]stdin\f[R] and executes it. This is to allow macros to request input from users. .TP \f[B]q\f[R] During execution of a macro, this exits the execution of that macro and the execution of the macro that executed it. If there are no macros, or only one macro executing, dc(1) exits. .TP \f[B]Q\f[R] Pops a value from the stack which must be non-negative and is used the number of macro executions to pop off of the execution stack. If the number of levels to pop is greater than the number of executing macros, dc(1) exits. .TP \f[B],\f[R] Pushes the depth of the execution stack onto the stack. The execution stack is the stack of string executions. The number that is pushed onto the stack is exactly as many as is needed to make dc(1) exit with the \f[B]Q\f[R] command, so the sequence \f[B],Q\f[R] will make dc(1) exit. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Status -.PP These commands query status of the stack or its top value. .TP \f[B]Z\f[R] Pops a value off of the stack. .RS .PP If it is a number, calculates the number of significant decimal digits it has and pushes the result. It will push \f[B]1\f[R] if the argument is \f[B]0\f[R] with no decimal places. .PP If it is a string, pushes the number of characters the string has. .RE .TP \f[B]X\f[R] Pops a value off of the stack. .RS .PP If it is a number, pushes the \f[I]scale\f[R] of the value onto the stack. .PP If it is a string, pushes \f[B]0\f[R]. .RE .TP \f[B]u\f[R] Pops one value off of the stack. If the value is a number, this pushes \f[B]1\f[R] onto the stack. Otherwise (if it is a string), it pushes \f[B]0\f[R]. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]t\f[R] Pops one value off of the stack. If the value is a string, this pushes \f[B]1\f[R] onto the stack. Otherwise (if it is a number), it pushes \f[B]0\f[R]. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]z\f[R] Pushes the current depth of the stack (before execution of this command) onto the stack. .TP \f[B]y\f[R]\f[I]r\f[R] Pushes the current stack depth of the register \f[I]r\f[R] onto the main stack. .RS .PP Because each register has a depth of \f[B]1\f[R] (with the value \f[B]0\f[R] in the top item) when dc(1) starts, dc(1) requires that each register\[cq]s stack must always have at least one item; dc(1) will give an error and reset otherwise (see the \f[B]RESET\f[R] section). This means that this command will never push \f[B]0\f[R]. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Arrays -.PP These commands manipulate arrays. .TP \f[B]:\f[R]\f[I]r\f[R] Pops the top two values off of the stack. The second value will be stored in the array \f[I]r\f[R] (see the \f[B]REGISTERS\f[R] section), indexed by the first value. .TP \f[B];\f[R]\f[I]r\f[R] Pops the value on top of the stack and uses it as an index into the array \f[I]r\f[R]. The selected value is then pushed onto the stack. .TP \f[B]Y\f[R]\f[I]r\f[R] Pushes the length of the array \f[I]r\f[R] onto the stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Global Settings -.PP These commands retrieve global settings. These are the only commands that require multiple specific characters, and all of them begin with the letter \f[B]g\f[R]. Only the characters below are allowed after the character \f[B]g\f[R]; any other character produces a parse error (see the \f[B]ERRORS\f[R] section). .TP \f[B]gl\f[R] Pushes the line length set by \f[B]DC_LINE_LENGTH\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) onto the stack. .TP \f[B]gx\f[R] Pushes \f[B]1\f[R] onto the stack if extended register mode is on, \f[B]0\f[R] otherwise. See the \f[I]Extended Register Mode\f[R] subsection of the \f[B]REGISTERS\f[R] section for more information. .TP \f[B]gz\f[R] Pushes \f[B]0\f[R] onto the stack if the leading zero setting has not been enabled with the \f[B]-z\f[R] or \f[B]--leading-zeroes\f[R] options (see the \f[B]OPTIONS\f[R] section), non-zero otherwise. .SH REGISTERS -.PP Registers are names that can store strings, numbers, and arrays. (Number/string registers do not interfere with array registers.) .PP Each register is also its own stack, so the current register value is the top of the stack for the register. All registers, when first referenced, have one value (\f[B]0\f[R]) in their stack, and it is a runtime error to attempt to pop that item off of the register stack. .PP In non-extended register mode, a register name is just the single character that follows any command that needs a register name. The only exceptions are: a newline (\f[B]`\[rs]n'\f[R]) and a left bracket (\f[B]`['\f[R]); it is a parse error for a newline or a left bracket to be used as a register name. .SS Extended Register Mode -.PP Unlike most other dc(1) implentations, this dc(1) provides nearly unlimited amounts of registers, if extended register mode is enabled. .PP If extended register mode is enabled (\f[B]-x\f[R] or \f[B]--extended-register\f[R] command-line arguments are given), then normal single character registers are used \f[I]unless\f[R] the character immediately following a command that needs a register name is a space (according to \f[B]isspace()\f[R]) and not a newline (\f[B]`\[rs]n'\f[R]). .PP In that case, the register name is found according to the regex \f[B][a-z][a-z0-9_]*\f[R] (like bc(1) identifiers), and it is a parse error if the next non-space characters do not match that regex. .SH RESET -.PP When dc(1) encounters an error or a signal that it has a non-default handler for, it resets. This means that several things happen. .PP First, any macros that are executing are stopped and popped off the stack. The behavior is not unlike that of exceptions in programming languages. Then the execution point is set so that any code waiting to execute (after all macros returned) is skipped. .PP Thus, when dc(1) resets, it skips any remaining code waiting to be executed. Then, if it is interactive mode, and the error was not a fatal error (see the \f[B]EXIT STATUS\f[R] section), it asks for more input; otherwise, it exits with the appropriate return code. .SH PERFORMANCE -.PP Most dc(1) implementations use \f[B]char\f[R] types to calculate the value of \f[B]1\f[R] decimal digit at a time, but that can be slow. This dc(1) does something different. .PP It uses large integers to calculate more than \f[B]1\f[R] decimal digit at a time. If built in a environment where \f[B]DC_LONG_BIT\f[R] (see the \f[B]LIMITS\f[R] section) is \f[B]64\f[R], then each integer has \f[B]9\f[R] decimal digits. If built in an environment where \f[B]DC_LONG_BIT\f[R] is \f[B]32\f[R] then each integer has \f[B]4\f[R] decimal digits. This value (the number of decimal digits per large integer) is called \f[B]DC_BASE_DIGS\f[R]. .PP In addition, this dc(1) uses an even larger integer for overflow checking. This integer type depends on the value of \f[B]DC_LONG_BIT\f[R], but is always at least twice as large as the integer type used to store digits. .SH LIMITS -.PP The following are the limits on dc(1): .TP \f[B]DC_LONG_BIT\f[R] The number of bits in the \f[B]long\f[R] type in the environment where dc(1) was built. This determines how many decimal digits can be stored in a single large integer (see the \f[B]PERFORMANCE\f[R] section). .TP \f[B]DC_BASE_DIGS\f[R] The number of decimal digits per large integer (see the \f[B]PERFORMANCE\f[R] section). Depends on \f[B]DC_LONG_BIT\f[R]. .TP \f[B]DC_BASE_POW\f[R] The max decimal number that each large integer can store (see \f[B]DC_BASE_DIGS\f[R]) plus \f[B]1\f[R]. Depends on \f[B]DC_BASE_DIGS\f[R]. .TP \f[B]DC_OVERFLOW_MAX\f[R] The max number that the overflow type (see the \f[B]PERFORMANCE\f[R] section) can hold. Depends on \f[B]DC_LONG_BIT\f[R]. .TP \f[B]DC_BASE_MAX\f[R] The maximum output base. Set at \f[B]DC_BASE_POW\f[R]. .TP \f[B]DC_DIM_MAX\f[R] The maximum size of arrays. Set at \f[B]SIZE_MAX-1\f[R]. .TP \f[B]DC_SCALE_MAX\f[R] The maximum \f[B]scale\f[R]. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_STRING_MAX\f[R] The maximum length of strings. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_NAME_MAX\f[R] The maximum length of identifiers. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_NUM_MAX\f[R] The maximum length of a number (in decimal digits), which includes digits after the decimal point. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP Exponent The maximum allowable exponent (positive or negative). Set at \f[B]DC_OVERFLOW_MAX\f[R]. .TP Number of vars The maximum number of vars/arrays. Set at \f[B]SIZE_MAX-1\f[R]. .PP These limits are meant to be effectively non-existent; the limits are so large (at least on 64-bit machines) that there should not be any point at which they become a problem. In fact, memory should be exhausted before these limits should be hit. .SH ENVIRONMENT VARIABLES -.PP As \f[B]non-portable extensions\f[R], dc(1) recognizes the following environment variables: .TP \f[B]DC_ENV_ARGS\f[R] This is another way to give command-line arguments to dc(1). They should be in the same format as all other command-line arguments. These are always processed first, so any files given in \f[B]DC_ENV_ARGS\f[R] will be processed before arguments and files given on the command-line. This gives the user the ability to set up \[lq]standard\[rq] options and files to be used at every invocation. The most useful thing for such files to contain would be useful functions that the user might want every time dc(1) runs. Another use would be to use the \f[B]-e\f[R] option to set \f[B]scale\f[R] to a value other than \f[B]0\f[R]. .RS .PP The code that parses \f[B]DC_ENV_ARGS\f[R] will correctly handle quoted arguments, but it does not understand escape sequences. For example, the string \f[B]\[lq]/home/gavin/some dc file.dc\[rq]\f[R] will be correctly parsed, but the string \f[B]\[lq]/home/gavin/some \[dq]dc\[dq] file.dc\[rq]\f[R] will include the backslashes. .PP The quote parsing will handle either kind of quotes, \f[B]\[cq]\f[R] or \f[B]\[lq]\f[R]. Thus, if you have a file with any number of single quotes in the name, you can use double quotes as the outside quotes, as in \f[B]\[lq]some `dc' file.dc\[rq]\f[R], and vice versa if you have a file with double quotes. However, handling a file with both kinds of quotes in \f[B]DC_ENV_ARGS\f[R] is not supported due to the complexity of the parsing, though such files are still supported on the command-line where the parsing is done by the shell. .RE .TP \f[B]DC_LINE_LENGTH\f[R] If this environment variable exists and contains an integer that is greater than \f[B]1\f[R] and is less than \f[B]UINT16_MAX\f[R] (\f[B]2\[ha]16-1\f[R]), dc(1) will output lines to that length, including the backslash newline combo. The default line length is \f[B]70\f[R]. .RS .PP The special value of \f[B]0\f[R] will disable line length checking and print numbers without regard to line length and without backslashes and newlines. .RE .TP \f[B]DC_SIGINT_RESET\f[R] If dc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), then this environment variable has no effect because dc(1) exits on \f[B]SIGINT\f[R] when not in interactive mode. .RS .PP However, when dc(1) is in interactive mode, then if this environment variable exists and contains an integer, a non-zero value makes dc(1) reset on \f[B]SIGINT\f[R], rather than exit, and zero makes dc(1) exit. If this environment variable exists and is \f[I]not\f[R] an integer, then dc(1) will exit on \f[B]SIGINT\f[R]. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_TTY_MODE\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, then a non-zero value makes dc(1) use TTY mode, and zero makes dc(1) not use TTY mode. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_PROMPT\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, a non-zero value makes dc(1) use a prompt, and zero or a non-integer makes dc(1) not use a prompt. If this environment variable does not exist and \f[B]DC_TTY_MODE\f[R] does, then the value of the \f[B]DC_TTY_MODE\f[R] environment variable is used. .PP This environment variable and the \f[B]DC_TTY_MODE\f[R] environment variable override the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_EXPR_EXIT\f[R] If any expressions or expression files are given on the command-line with \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R], then if this environment variable exists and contains an integer, a non-zero value makes dc(1) exit after executing the expressions and expression files, and a zero value makes dc(1) not exit. .RS .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_DIGIT_CLAMP\f[R] When parsing numbers and if this environment variable exists and contains an integer, a non-zero value makes dc(1) clamp digits that are greater than or equal to the current \f[B]ibase\f[R] so that all such digits are considered equal to the \f[B]ibase\f[R] minus 1, and a zero value disables such clamping so that those digits are always equal to their value, which is multiplied by the power of the \f[B]ibase\f[R]. .RS .PP This never applies to single-digit numbers, as per the bc(1) standard (see the \f[B]STANDARDS\f[R] section). .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .SH EXIT STATUS -.PP dc(1) returns the following exit statuses: .TP \f[B]0\f[R] No error. .TP \f[B]1\f[R] A math error occurred. This follows standard practice of using \f[B]1\f[R] for expected errors, since math errors will happen in the process of normal execution. .RS .PP Math errors include divide by \f[B]0\f[R], taking the square root of a negative number, attempting to convert a negative number to a hardware integer, overflow when converting a number to a hardware integer, overflow when calculating the size of a number, and attempting to use a non-integer where an integer is required. .PP Converting to a hardware integer happens for the second operand of the power (\f[B]\[ha]\f[R]) operator. .RE .TP \f[B]2\f[R] A parse error occurred. .RS .PP Parse errors include unexpected \f[B]EOF\f[R], using an invalid character, failing to find the end of a string or comment, and using a token where it is invalid. .RE .TP \f[B]3\f[R] A runtime error occurred. .RS .PP Runtime errors include assigning an invalid number to any global (\f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R]), giving a bad expression to a \f[B]read()\f[R] call, calling \f[B]read()\f[R] inside of a \f[B]read()\f[R] call, type errors (including attempting to execute a number), and attempting an operation when the stack has too few elements. .RE .TP \f[B]4\f[R] A fatal error occurred. .RS .PP Fatal errors include memory allocation errors, I/O errors, failing to open files, attempting to use files that do not have only ASCII characters (dc(1) only accepts ASCII characters), attempting to open a directory as a file, and giving invalid command-line options. .RE .PP The exit status \f[B]4\f[R] is special; when a fatal error occurs, dc(1) always exits and returns \f[B]4\f[R], no matter what mode dc(1) is in. .PP The other statuses will only be returned when dc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), since dc(1) resets its state (see the \f[B]RESET\f[R] section) and accepts more input when one of those errors occurs in interactive mode. This is also the case when interactive mode is forced by the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .PP These exit statuses allow dc(1) to be used in shell scripting with error checking, and its normal behavior can be forced by using the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .SH INTERACTIVE MODE -.PP Like bc(1), dc(1) has an interactive mode and a non-interactive mode. Interactive mode is turned on automatically when both \f[B]stdin\f[R] and \f[B]stdout\f[R] are hooked to a terminal, but the \f[B]-i\f[R] flag and \f[B]--interactive\f[R] option can turn it on in other situations. .PP In interactive mode, dc(1) attempts to recover from errors (see the \f[B]RESET\f[R] section), and in normal execution, flushes \f[B]stdout\f[R] as soon as execution is done for the current input. dc(1) may also reset on \f[B]SIGINT\f[R] instead of exit, depending on the contents of, or default for, the \f[B]DC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .SH TTY MODE -.PP If \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY, then \[lq]TTY mode\[rq] is considered to be available, and thus, dc(1) can turn on TTY mode, subject to some settings. .PP If there is the environment variable \f[B]DC_TTY_MODE\f[R] in the environment (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then if that environment variable contains a non-zero integer, dc(1) will turn on TTY mode when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY. If the \f[B]DC_TTY_MODE\f[R] environment variable exists but is \f[I]not\f[R] a non-zero integer, then dc(1) will not turn TTY mode on. .PP If the environment variable \f[B]DC_TTY_MODE\f[R] does \f[I]not\f[R] exist, the default setting is used. The default setting can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP TTY mode is different from interactive mode because interactive mode is required in the bc(1) specification (see the \f[B]STANDARDS\f[R] section), and interactive mode requires only \f[B]stdin\f[R] and \f[B]stdout\f[R] to be connected to a terminal. .SS Prompt -.PP If TTY mode is available, then a prompt can be enabled. Like TTY mode itself, it can be turned on or off with an environment variable: \f[B]DC_PROMPT\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP If the environment variable \f[B]DC_PROMPT\f[R] exists and is a non-zero integer, then the prompt is turned on when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options were not used. The read prompt will be turned on under the same conditions, except that the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options must also not be used. .PP However, if \f[B]DC_PROMPT\f[R] does not exist, the prompt can be enabled or disabled with the \f[B]DC_TTY_MODE\f[R] environment variable, the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options, and the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options. See the \f[B]ENVIRONMENT VARIABLES\f[R] and \f[B]OPTIONS\f[R] sections for more details. .SH SIGNAL HANDLING -.PP Sending a \f[B]SIGINT\f[R] will cause dc(1) to do one of two things. .PP If dc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), or the \f[B]DC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or its default, is either not an integer or it is zero, dc(1) will exit. .PP However, if dc(1) is in interactive mode, and the \f[B]DC_SIGINT_RESET\f[R] or its default is an integer and non-zero, then dc(1) will stop executing the current input and reset (see the \f[B]RESET\f[R] section) upon receiving a \f[B]SIGINT\f[R]. .PP Note that \[lq]current input\[rq] can mean one of two things. If dc(1) is processing input from \f[B]stdin\f[R] in interactive mode, it will ask for more input. If dc(1) is processing input from a file in interactive mode, it will stop processing the file and start processing the next file, if one exists, or ask for input from \f[B]stdin\f[R] if no other file exists. .PP This means that if a \f[B]SIGINT\f[R] is sent to dc(1) as it is executing a file, it can seem as though dc(1) did not respond to the signal since it will immediately start executing the next file. This is by design; most files that users execute when interacting with dc(1) have function definitions, which are quick to parse. If a file takes a long time to execute, there may be a bug in that file. The rest of the files could still be executed without problem, allowing the user to continue. .PP \f[B]SIGTERM\f[R] and \f[B]SIGQUIT\f[R] cause dc(1) to clean up and exit, and it uses the default handler for all other signals. .SH SEE ALSO -.PP bc(1) .SH STANDARDS -.PP The dc(1) utility operators and some behavior are compliant with the operators in the IEEE Std 1003.1-2017 (\[lq]POSIX.1-2017\[rq]) bc(1) specification at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html . .SH BUGS -.PP None are known. Report bugs at https://git.gavinhoward.com/gavin/bc . .SH AUTHOR -.PP -Gavin D. -Howard and contributors. +Gavin D. Howard \c +.MT gavin@gavinhoward.com +.ME \c +\ and contributors. diff --git a/contrib/bc/manuals/dc/EN.1 b/contrib/bc/manuals/dc/EN.1 index ce1e6b9be79c..8cdefc2b3522 100644 --- a/contrib/bc/manuals/dc/EN.1 +++ b/contrib/bc/manuals/dc/EN.1 @@ -1,1501 +1,1466 @@ .\" .\" SPDX-License-Identifier: BSD-2-Clause .\" .\" Copyright (c) 2018-2023 Gavin D. Howard and contributors. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions are met: .\" .\" * Redistributions of source code must retain the above copyright notice, .\" this list of conditions and the following disclaimer. .\" .\" * Redistributions in binary form must reproduce the above copyright notice, .\" this list of conditions and the following disclaimer in the documentation .\" and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" .TH "DC" "1" "February 2023" "Gavin D. Howard" "General Commands Manual" .nh .ad l .SH Name -.PP dc - arbitrary-precision decimal reverse-Polish notation calculator .SH SYNOPSIS -.PP \f[B]dc\f[R] [\f[B]-cChiPRvVx\f[R]] [\f[B]--version\f[R]] [\f[B]--help\f[R]] [\f[B]--digit-clamp\f[R]] [\f[B]--no-digit-clamp\f[R]] [\f[B]--interactive\f[R]] [\f[B]--no-prompt\f[R]] [\f[B]--no-read-prompt\f[R]] [\f[B]--extended-register\f[R]] [\f[B]-e\f[R] \f[I]expr\f[R]] [\f[B]--expression\f[R]=\f[I]expr\f[R]\&...] [\f[B]-f\f[R] \f[I]file\f[R]\&...] [\f[B]--file\f[R]=\f[I]file\f[R]\&...] [\f[I]file\f[R]\&...] .SH DESCRIPTION -.PP dc(1) is an arbitrary-precision calculator. It uses a stack (reverse Polish notation) to store numbers and results of computations. Arithmetic operations pop arguments off of the stack and push the results. .PP If no files are given on the command-line, then dc(1) reads from \f[B]stdin\f[R] (see the \f[B]STDIN\f[R] section). Otherwise, those files are processed, and dc(1) will then exit. .PP If a user wants to set up a standard environment, they can use \f[B]DC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). For example, if a user wants the \f[B]scale\f[R] always set to \f[B]10\f[R], they can set \f[B]DC_ENV_ARGS\f[R] to \f[B]-e 10k\f[R], and this dc(1) will always start with a \f[B]scale\f[R] of \f[B]10\f[R]. .SH OPTIONS -.PP The following are the options that dc(1) accepts. .TP \f[B]-C\f[R], \f[B]--no-digit-clamp\f[R] Disables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that the value added to a number from a digit is always that digit\[cq]s value multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-c\f[R] or \f[B]--digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]DC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-c\f[R], \f[B]--digit-clamp\f[R] Enables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that digits that the value added to a number from a digit that is greater than or equal to the ibase is the value of ibase minus 1 all multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-C\f[R] or \f[B]--no-digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]DC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-e\f[R] \f[I]expr\f[R], \f[B]--expression\f[R]=\f[I]expr\f[R] Evaluates \f[I]expr\f[R]. If multiple expressions are given, they are evaluated in order. If files are given as well (see below), the expressions and files are evaluated in the order given. This means that if a file is given before an expression, the file is read in and evaluated first. .RS .PP If this option is given on the command-line (i.e., not in \f[B]DC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, dc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R], whether on the command-line or in \f[B]DC_ENV_ARGS\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, dc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-f\f[R] \f[I]file\f[R], \f[B]--file\f[R]=\f[I]file\f[R] Reads in \f[I]file\f[R] and evaluates it, line by line, as though it were read through \f[B]stdin\f[R]. If expressions are also given (see above), the expressions are evaluated in the order given. .RS .PP If this option is given on the command-line (i.e., not in \f[B]DC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, dc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, dc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-h\f[R], \f[B]--help\f[R] Prints a usage message and exits. .TP \f[B]-I\f[R] \f[I]ibase\f[R], \f[B]--ibase\f[R]=\f[I]ibase\f[R] Sets the builtin variable \f[B]ibase\f[R] to the value \f[I]ibase\f[R] assuming that \f[I]ibase\f[R] is in base 10. It is a fatal error if \f[I]ibase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-i\f[R], \f[B]--interactive\f[R] Forces interactive mode. (See the \f[B]INTERACTIVE MODE\f[R] section.) .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-L\f[R], \f[B]--no-line-length\f[R] Disables line length checking and prints numbers without backslashes and newlines. In other words, this option sets \f[B]BC_LINE_LENGTH\f[R] to \f[B]0\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-O\f[R] \f[I]obase\f[R], \f[B]--obase\f[R]=\f[I]obase\f[R] Sets the builtin variable \f[B]obase\f[R] to the value \f[I]obase\f[R] assuming that \f[I]obase\f[R] is in base 10. It is a fatal error if \f[I]obase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-P\f[R], \f[B]--no-prompt\f[R] Disables the prompt in TTY mode. (The prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a prompt or are not used to having them in dc(1). Most of those users would want to put this option in \f[B]DC_ENV_ARGS\f[R]. .RS .PP These options override the \f[B]DC_PROMPT\f[R] and \f[B]DC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-R\f[R], \f[B]--no-read-prompt\f[R] Disables the read prompt in TTY mode. (The read prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a read prompt or are not used to having them in dc(1). Most of those users would want to put this option in \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). This option is also useful in hash bang lines of dc(1) scripts that prompt for user input. .RS .PP This option does not disable the regular prompt because the read prompt is only used when the \f[B]?\f[R] command is used. .PP These options \f[I]do\f[R] override the \f[B]DC_PROMPT\f[R] and \f[B]DC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), but only for the read prompt. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-S\f[R] \f[I]scale\f[R], \f[B]--scale\f[R]=\f[I]scale\f[R] Sets the builtin variable \f[B]scale\f[R] to the value \f[I]scale\f[R] assuming that \f[I]scale\f[R] is in base 10. It is a fatal error if \f[I]scale\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-v\f[R], \f[B]-V\f[R], \f[B]--version\f[R] Print the version information (copyright header) and exits. .TP \f[B]-x\f[R] \f[B]--extended-register\f[R] Enables extended register mode. See the \f[I]Extended Register Mode\f[R] subsection of the \f[B]REGISTERS\f[R] section for more information. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-z\f[R], \f[B]--leading-zeroes\f[R] Makes dc(1) print all numbers greater than \f[B]-1\f[R] and less than \f[B]1\f[R], and not equal to \f[B]0\f[R], with a leading zero. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .PP All long options are \f[B]non-portable extensions\f[R]. .SH STDIN -.PP If no files are given on the command-line and no files or expressions are given by the \f[B]-f\f[R], \f[B]--file\f[R], \f[B]-e\f[R], or \f[B]--expression\f[R] options, then dc(1) reads from \f[B]stdin\f[R]. .PP However, there is a caveat to this. .PP First, \f[B]stdin\f[R] is evaluated a line at a time. The only exception to this is if a string has been finished, but not ended. This means that, except for escaped brackets, all brackets must be balanced before dc(1) parses and executes. .SH STDOUT -.PP Any non-error output is written to \f[B]stdout\f[R]. In addition, if history (see the \f[B]HISTORY\f[R] section) and the prompt (see the \f[B]TTY MODE\f[R] section) are enabled, both are output to \f[B]stdout\f[R]. .PP \f[B]Note\f[R]: Unlike other dc(1) implementations, this dc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stdout\f[R], so if \f[B]stdout\f[R] is closed, as in -\f[B]dc >&-\f[R], it will quit with an error. +\f[B]dc >&-\f[R], it will quit with an error. This is done so that dc(1) can report problems when \f[B]stdout\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other dc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stdout\f[R] to \f[B]/dev/null\f[R]. .SH STDERR -.PP Any error output is written to \f[B]stderr\f[R]. .PP \f[B]Note\f[R]: Unlike other dc(1) implementations, this dc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stderr\f[R], so if \f[B]stderr\f[R] is closed, as in -\f[B]dc 2>&-\f[R], it will quit with an error. +\f[B]dc 2>&-\f[R], it will quit with an error. This is done so that dc(1) can exit with an error code when \f[B]stderr\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other dc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stderr\f[R] to \f[B]/dev/null\f[R]. .SH SYNTAX -.PP Each item in the input source code, either a number (see the \f[B]NUMBERS\f[R] section) or a command (see the \f[B]COMMANDS\f[R] section), is processed and executed, in order. Input is processed immediately when entered. .PP \f[B]ibase\f[R] is a register (see the \f[B]REGISTERS\f[R] section) that determines how to interpret constant numbers. It is the \[lq]input\[rq] base, or the number base used for interpreting input numbers. \f[B]ibase\f[R] is initially \f[B]10\f[R]. The max allowable value for \f[B]ibase\f[R] is \f[B]16\f[R]. The min allowable value for \f[B]ibase\f[R] is \f[B]2\f[R]. The max allowable value for \f[B]ibase\f[R] can be queried in dc(1) programs with the \f[B]T\f[R] command. .PP \f[B]obase\f[R] is a register (see the \f[B]REGISTERS\f[R] section) that determines how to output results. It is the \[lq]output\[rq] base, or the number base used for outputting numbers. \f[B]obase\f[R] is initially \f[B]10\f[R]. The max allowable value for \f[B]obase\f[R] is \f[B]DC_BASE_MAX\f[R] and can be queried with the \f[B]U\f[R] command. The min allowable value for \f[B]obase\f[R] is \f[B]2\f[R]. Values are output in the specified base. .PP The \f[I]scale\f[R] of an expression is the number of digits in the result of the expression right of the decimal point, and \f[B]scale\f[R] is a register (see the \f[B]REGISTERS\f[R] section) that sets the precision of any operations (with exceptions). \f[B]scale\f[R] is initially \f[B]0\f[R]. \f[B]scale\f[R] cannot be negative. The max allowable value for \f[B]scale\f[R] can be queried in dc(1) programs with the \f[B]V\f[R] command. .SS Comments -.PP Comments go from \f[B]#\f[R] until, and not including, the next newline. This is a \f[B]non-portable extension\f[R]. .SH NUMBERS -.PP Numbers are strings made up of digits, uppercase letters up to \f[B]F\f[R], and at most \f[B]1\f[R] period for a radix. Numbers can have up to \f[B]DC_NUM_MAX\f[R] digits. Uppercase letters are equal to \f[B]9\f[R] plus their position in the alphabet (i.e., \f[B]A\f[R] equals \f[B]10\f[R], or \f[B]9+1\f[R]). .PP If a digit or letter makes no sense with the current value of \f[B]ibase\f[R] (i.e., they are greater than or equal to the current value of \f[B]ibase\f[R]), then the behavior depends on the existence of the \f[B]-c\f[R]/\f[B]--digit-clamp\f[R] or \f[B]-C\f[R]/\f[B]--no-digit-clamp\f[R] options (see the \f[B]OPTIONS\f[R] section), the existence and setting of the \f[B]DC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or the default, which can be queried with the \f[B]-h\f[R]/\f[B]--help\f[R] option. .PP If clamping is off, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are not changed. Instead, their given value is multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*A+3\[ha]0*B\f[R], which is \f[B]3\f[R] times \f[B]10\f[R] plus \f[B]11\f[R], or \f[B]41\f[R]. .PP If clamping is on, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are set to the value of the highest valid digit in \f[B]ibase\f[R] before being multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*2+3\[ha]0*2\f[R], which is \f[B]3\f[R] times \f[B]2\f[R] plus \f[B]2\f[R], or \f[B]8\f[R]. .PP There is one exception to clamping: single-character numbers (i.e., \f[B]A\f[R] alone). Such numbers are never clamped and always take the value they would have in the highest possible \f[B]ibase\f[R]. This means that \f[B]A\f[R] alone always equals decimal \f[B]10\f[R] and \f[B]Z\f[R] alone always equals decimal \f[B]35\f[R]. This behavior is mandated by the standard for bc(1) (see the STANDARDS section) and is meant to provide an easy way to set the current \f[B]ibase\f[R] (with the \f[B]i\f[R] command) regardless of the current value of \f[B]ibase\f[R]. .PP If clamping is on, and the clamped value of a character is needed, use a leading zero, i.e., for \f[B]A\f[R], use \f[B]0A\f[R]. .SH COMMANDS -.PP The valid commands are listed below. .SS Printing -.PP These commands are used for printing. .TP \f[B]p\f[R] Prints the value on top of the stack, whether number or string, and prints a newline after. .RS .PP This does not alter the stack. .RE .TP \f[B]n\f[R] Prints the value on top of the stack, whether number or string, and pops it off of the stack. .TP \f[B]P\f[R] Pops a value off the stack. .RS .PP If the value is a number, it is truncated and the absolute value of the result is printed as though \f[B]obase\f[R] is \f[B]256\f[R] and each digit is interpreted as an 8-bit ASCII character, making it a byte stream. .PP If the value is a string, it is printed without a trailing newline. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]f\f[R] Prints the entire contents of the stack, in order from newest to oldest, without altering anything. .RS .PP Users should use this command when they get lost. .RE .SS Arithmetic -.PP These are the commands used for arithmetic. .TP \f[B]+\f[R] The top two values are popped off the stack, added, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to the max \f[I]scale\f[R] of both operands. .TP \f[B]-\f[R] The top two values are popped off the stack, subtracted, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to the max \f[I]scale\f[R] of both operands. .TP \f[B]*\f[R] The top two values are popped off the stack, multiplied, and the result is pushed onto the stack. If \f[B]a\f[R] is the \f[I]scale\f[R] of the first expression and \f[B]b\f[R] is the \f[I]scale\f[R] of the second expression, the \f[I]scale\f[R] of the result is equal to \f[B]min(a+b,max(scale,a,b))\f[R] where \f[B]min()\f[R] and \f[B]max()\f[R] return the obvious values. .TP \f[B]/\f[R] The top two values are popped off the stack, divided, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The first value popped off of the stack must be non-zero. .RE .TP \f[B]%\f[R] The top two values are popped off the stack, remaindered, and the result is pushed onto the stack. .RS .PP Remaindering is equivalent to 1) Computing \f[B]a/b\f[R] to current \f[B]scale\f[R], and 2) Using the result of step 1 to calculate \f[B]a-(a/b)*b\f[R] to \f[I]scale\f[R] \f[B]max(scale+scale(b),scale(a))\f[R]. .PP The first value popped off of the stack must be non-zero. .RE .TP \f[B]\[ti]\f[R] The top two values are popped off the stack, divided and remaindered, and the results (divided first, remainder second) are pushed onto the stack. This is equivalent to \f[B]x y / x y %\f[R] except that \f[B]x\f[R] and \f[B]y\f[R] are only evaluated once. .RS .PP The first value popped off of the stack must be non-zero. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[ha]\f[R] The top two values are popped off the stack, the second is raised to the power of the first, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The first value popped off of the stack must be an integer, and if that value is negative, the second value popped off of the stack must be non-zero. .RE .TP \f[B]v\f[R] The top value is popped off the stack, its square root is computed, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The value popped off of the stack must be non-negative. .RE .TP \f[B]_\f[R] If this command \f[I]immediately\f[R] precedes a number (i.e., no spaces or other commands), then that number is input as a negative number. .RS .PP Otherwise, the top value on the stack is popped and copied, and the copy is negated and pushed onto the stack. This behavior without a number is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]b\f[R] The top value is popped off the stack, and if it is zero, it is pushed back onto the stack. Otherwise, its absolute value is pushed onto the stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]|\f[R] The top three values are popped off the stack, a modular exponentiation is computed, and the result is pushed onto the stack. .RS .PP The first value popped is used as the reduction modulus and must be an integer and non-zero. The second value popped is used as the exponent and must be an integer and non-negative. The third value popped is the base and must be an integer. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]G\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if they are equal, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]N\f[R] The top value is popped off of the stack, and if it a \f[B]0\f[R], a \f[B]1\f[R] is pushed; otherwise, a \f[B]0\f[R] is pushed. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B](\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is less than the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]{\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is less than or equal to the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B])\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is greater than the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]}\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is greater than or equal to the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]M\f[R] The top two values are popped off of the stack. If they are both non-zero, a \f[B]1\f[R] is pushed onto the stack. If either of them is zero, or both of them are, then a \f[B]0\f[R] is pushed onto the stack. .RS .PP This is like the \f[B]&&\f[R] operator in bc(1), and it is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]m\f[R] The top two values are popped off of the stack. If at least one of them is non-zero, a \f[B]1\f[R] is pushed onto the stack. If both of them are zero, then a \f[B]0\f[R] is pushed onto the stack. .RS .PP This is like the \f[B]||\f[R] operator in bc(1), and it is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Stack Control -.PP These commands control the stack. .TP \f[B]c\f[R] Removes all items from (\[lq]clears\[rq]) the stack. .TP \f[B]d\f[R] Copies the item on top of the stack (\[lq]duplicates\[rq]) and pushes the copy onto the stack. .TP \f[B]r\f[R] Swaps (\[lq]reverses\[rq]) the two top items on the stack. .TP \f[B]R\f[R] Pops (\[lq]removes\[rq]) the top value from the stack. .SS Register Control -.PP These commands control registers (see the \f[B]REGISTERS\f[R] section). .TP \f[B]s\f[R]\f[I]r\f[R] Pops the value off the top of the stack and stores it into register \f[I]r\f[R]. .TP \f[B]l\f[R]\f[I]r\f[R] Copies the value in register \f[I]r\f[R] and pushes it onto the stack. This does not alter the contents of \f[I]r\f[R]. .TP \f[B]S\f[R]\f[I]r\f[R] Pops the value off the top of the (main) stack and pushes it onto the stack of register \f[I]r\f[R]. The previous value of the register becomes inaccessible. .TP \f[B]L\f[R]\f[I]r\f[R] Pops the value off the top of the stack for register \f[I]r\f[R] and push it onto the main stack. The previous value in the stack for register \f[I]r\f[R], if any, is now accessible via the \f[B]l\f[R]\f[I]r\f[R] command. .SS Parameters -.PP These commands control the values of \f[B]ibase\f[R], \f[B]obase\f[R], and \f[B]scale\f[R]. Also see the \f[B]SYNTAX\f[R] section. .TP \f[B]i\f[R] Pops the value off of the top of the stack and uses it to set \f[B]ibase\f[R], which must be between \f[B]2\f[R] and \f[B]16\f[R], inclusive. .RS .PP If the value on top of the stack has any \f[I]scale\f[R], the \f[I]scale\f[R] is ignored. .RE .TP \f[B]o\f[R] Pops the value off of the top of the stack and uses it to set \f[B]obase\f[R], which must be between \f[B]2\f[R] and \f[B]DC_BASE_MAX\f[R], inclusive (see the \f[B]LIMITS\f[R] section). .RS .PP If the value on top of the stack has any \f[I]scale\f[R], the \f[I]scale\f[R] is ignored. .RE .TP \f[B]k\f[R] Pops the value off of the top of the stack and uses it to set \f[B]scale\f[R], which must be non-negative. .RS .PP If the value on top of the stack has any \f[I]scale\f[R], the \f[I]scale\f[R] is ignored. .RE .TP \f[B]I\f[R] Pushes the current value of \f[B]ibase\f[R] onto the main stack. .TP \f[B]O\f[R] Pushes the current value of \f[B]obase\f[R] onto the main stack. .TP \f[B]K\f[R] Pushes the current value of \f[B]scale\f[R] onto the main stack. .TP \f[B]T\f[R] Pushes the maximum allowable value of \f[B]ibase\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]U\f[R] Pushes the maximum allowable value of \f[B]obase\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]V\f[R] Pushes the maximum allowable value of \f[B]scale\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Strings -.PP The following commands control strings. .PP dc(1) can work with both numbers and strings, and registers (see the \f[B]REGISTERS\f[R] section) can hold both strings and numbers. dc(1) always knows whether the contents of a register are a string or a number. .PP While arithmetic operations have to have numbers, and will print an error if given a string, other commands accept strings. .PP Strings can also be executed as macros. For example, if the string \f[B][1pR]\f[R] is executed as a macro, then the code \f[B]1pR\f[R] is executed, meaning that the \f[B]1\f[R] will be printed with a newline after and then popped from the stack. .TP \f[B][\f[R]\f[I]characters\f[R]\f[B]]\f[R] Makes a string containing \f[I]characters\f[R] and pushes it onto the stack. .RS .PP If there are brackets (\f[B][\f[R] and \f[B]]\f[R]) in the string, then they must be balanced. Unbalanced brackets can be escaped using a backslash (\f[B]\[rs]\f[R]) character. .PP If there is a backslash character in the string, the character after it (even another backslash) is put into the string verbatim, but the (first) backslash is not. .RE .TP \f[B]a\f[R] The value on top of the stack is popped. .RS .PP If it is a number, it is truncated and its absolute value is taken. The result mod \f[B]256\f[R] is calculated. If that result is \f[B]0\f[R], push an empty string; otherwise, push a one-character string where the character is the result of the mod interpreted as an ASCII character. .PP If it is a string, then a new string is made. If the original string is empty, the new string is empty. If it is not, then the first character of the original string is used to create the new string as a one-character string. The new string is then pushed onto the stack. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]x\f[R] Pops a value off of the top of the stack. .RS .PP If it is a number, it is pushed back onto the stack. .PP If it is a string, it is executed as a macro. .PP This behavior is the norm whenever a macro is executed, whether by this command or by the conditional execution commands below. .RE .TP \f[B]>\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is greater than the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP For example, \f[B]0 1>a\f[R] will execute the contents of register \f[B]a\f[R], and \f[B]1 0>a\f[R] will not. .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]>\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]!>\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is not greater than the second (less than or equal to), then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]!>\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]<\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is less than the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]<\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]!<\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is not less than the second (greater than or equal to), then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]!<\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]=\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is equal to the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]=\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]!=\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is not equal to the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]!=\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]?\f[R] Reads a line from the \f[B]stdin\f[R] and executes it. This is to allow macros to request input from users. .TP \f[B]q\f[R] During execution of a macro, this exits the execution of that macro and the execution of the macro that executed it. If there are no macros, or only one macro executing, dc(1) exits. .TP \f[B]Q\f[R] Pops a value from the stack which must be non-negative and is used the number of macro executions to pop off of the execution stack. If the number of levels to pop is greater than the number of executing macros, dc(1) exits. .TP \f[B],\f[R] Pushes the depth of the execution stack onto the stack. The execution stack is the stack of string executions. The number that is pushed onto the stack is exactly as many as is needed to make dc(1) exit with the \f[B]Q\f[R] command, so the sequence \f[B],Q\f[R] will make dc(1) exit. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Status -.PP These commands query status of the stack or its top value. .TP \f[B]Z\f[R] Pops a value off of the stack. .RS .PP If it is a number, calculates the number of significant decimal digits it has and pushes the result. It will push \f[B]1\f[R] if the argument is \f[B]0\f[R] with no decimal places. .PP If it is a string, pushes the number of characters the string has. .RE .TP \f[B]X\f[R] Pops a value off of the stack. .RS .PP If it is a number, pushes the \f[I]scale\f[R] of the value onto the stack. .PP If it is a string, pushes \f[B]0\f[R]. .RE .TP \f[B]u\f[R] Pops one value off of the stack. If the value is a number, this pushes \f[B]1\f[R] onto the stack. Otherwise (if it is a string), it pushes \f[B]0\f[R]. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]t\f[R] Pops one value off of the stack. If the value is a string, this pushes \f[B]1\f[R] onto the stack. Otherwise (if it is a number), it pushes \f[B]0\f[R]. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]z\f[R] Pushes the current depth of the stack (before execution of this command) onto the stack. .TP \f[B]y\f[R]\f[I]r\f[R] Pushes the current stack depth of the register \f[I]r\f[R] onto the main stack. .RS .PP Because each register has a depth of \f[B]1\f[R] (with the value \f[B]0\f[R] in the top item) when dc(1) starts, dc(1) requires that each register\[cq]s stack must always have at least one item; dc(1) will give an error and reset otherwise (see the \f[B]RESET\f[R] section). This means that this command will never push \f[B]0\f[R]. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Arrays -.PP These commands manipulate arrays. .TP \f[B]:\f[R]\f[I]r\f[R] Pops the top two values off of the stack. The second value will be stored in the array \f[I]r\f[R] (see the \f[B]REGISTERS\f[R] section), indexed by the first value. .TP \f[B];\f[R]\f[I]r\f[R] Pops the value on top of the stack and uses it as an index into the array \f[I]r\f[R]. The selected value is then pushed onto the stack. .TP \f[B]Y\f[R]\f[I]r\f[R] Pushes the length of the array \f[I]r\f[R] onto the stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Global Settings -.PP These commands retrieve global settings. These are the only commands that require multiple specific characters, and all of them begin with the letter \f[B]g\f[R]. Only the characters below are allowed after the character \f[B]g\f[R]; any other character produces a parse error (see the \f[B]ERRORS\f[R] section). .TP \f[B]gl\f[R] Pushes the line length set by \f[B]DC_LINE_LENGTH\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) onto the stack. .TP \f[B]gx\f[R] Pushes \f[B]1\f[R] onto the stack if extended register mode is on, \f[B]0\f[R] otherwise. See the \f[I]Extended Register Mode\f[R] subsection of the \f[B]REGISTERS\f[R] section for more information. .TP \f[B]gz\f[R] Pushes \f[B]0\f[R] onto the stack if the leading zero setting has not been enabled with the \f[B]-z\f[R] or \f[B]--leading-zeroes\f[R] options (see the \f[B]OPTIONS\f[R] section), non-zero otherwise. .SH REGISTERS -.PP Registers are names that can store strings, numbers, and arrays. (Number/string registers do not interfere with array registers.) .PP Each register is also its own stack, so the current register value is the top of the stack for the register. All registers, when first referenced, have one value (\f[B]0\f[R]) in their stack, and it is a runtime error to attempt to pop that item off of the register stack. .PP In non-extended register mode, a register name is just the single character that follows any command that needs a register name. The only exceptions are: a newline (\f[B]`\[rs]n'\f[R]) and a left bracket (\f[B]`['\f[R]); it is a parse error for a newline or a left bracket to be used as a register name. .SS Extended Register Mode -.PP Unlike most other dc(1) implentations, this dc(1) provides nearly unlimited amounts of registers, if extended register mode is enabled. .PP If extended register mode is enabled (\f[B]-x\f[R] or \f[B]--extended-register\f[R] command-line arguments are given), then normal single character registers are used \f[I]unless\f[R] the character immediately following a command that needs a register name is a space (according to \f[B]isspace()\f[R]) and not a newline (\f[B]`\[rs]n'\f[R]). .PP In that case, the register name is found according to the regex \f[B][a-z][a-z0-9_]*\f[R] (like bc(1) identifiers), and it is a parse error if the next non-space characters do not match that regex. .SH RESET -.PP When dc(1) encounters an error or a signal that it has a non-default handler for, it resets. This means that several things happen. .PP First, any macros that are executing are stopped and popped off the stack. The behavior is not unlike that of exceptions in programming languages. Then the execution point is set so that any code waiting to execute (after all macros returned) is skipped. .PP Thus, when dc(1) resets, it skips any remaining code waiting to be executed. Then, if it is interactive mode, and the error was not a fatal error (see the \f[B]EXIT STATUS\f[R] section), it asks for more input; otherwise, it exits with the appropriate return code. .SH PERFORMANCE -.PP Most dc(1) implementations use \f[B]char\f[R] types to calculate the value of \f[B]1\f[R] decimal digit at a time, but that can be slow. This dc(1) does something different. .PP It uses large integers to calculate more than \f[B]1\f[R] decimal digit at a time. If built in a environment where \f[B]DC_LONG_BIT\f[R] (see the \f[B]LIMITS\f[R] section) is \f[B]64\f[R], then each integer has \f[B]9\f[R] decimal digits. If built in an environment where \f[B]DC_LONG_BIT\f[R] is \f[B]32\f[R] then each integer has \f[B]4\f[R] decimal digits. This value (the number of decimal digits per large integer) is called \f[B]DC_BASE_DIGS\f[R]. .PP In addition, this dc(1) uses an even larger integer for overflow checking. This integer type depends on the value of \f[B]DC_LONG_BIT\f[R], but is always at least twice as large as the integer type used to store digits. .SH LIMITS -.PP The following are the limits on dc(1): .TP \f[B]DC_LONG_BIT\f[R] The number of bits in the \f[B]long\f[R] type in the environment where dc(1) was built. This determines how many decimal digits can be stored in a single large integer (see the \f[B]PERFORMANCE\f[R] section). .TP \f[B]DC_BASE_DIGS\f[R] The number of decimal digits per large integer (see the \f[B]PERFORMANCE\f[R] section). Depends on \f[B]DC_LONG_BIT\f[R]. .TP \f[B]DC_BASE_POW\f[R] The max decimal number that each large integer can store (see \f[B]DC_BASE_DIGS\f[R]) plus \f[B]1\f[R]. Depends on \f[B]DC_BASE_DIGS\f[R]. .TP \f[B]DC_OVERFLOW_MAX\f[R] The max number that the overflow type (see the \f[B]PERFORMANCE\f[R] section) can hold. Depends on \f[B]DC_LONG_BIT\f[R]. .TP \f[B]DC_BASE_MAX\f[R] The maximum output base. Set at \f[B]DC_BASE_POW\f[R]. .TP \f[B]DC_DIM_MAX\f[R] The maximum size of arrays. Set at \f[B]SIZE_MAX-1\f[R]. .TP \f[B]DC_SCALE_MAX\f[R] The maximum \f[B]scale\f[R]. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_STRING_MAX\f[R] The maximum length of strings. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_NAME_MAX\f[R] The maximum length of identifiers. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_NUM_MAX\f[R] The maximum length of a number (in decimal digits), which includes digits after the decimal point. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP Exponent The maximum allowable exponent (positive or negative). Set at \f[B]DC_OVERFLOW_MAX\f[R]. .TP Number of vars The maximum number of vars/arrays. Set at \f[B]SIZE_MAX-1\f[R]. .PP These limits are meant to be effectively non-existent; the limits are so large (at least on 64-bit machines) that there should not be any point at which they become a problem. In fact, memory should be exhausted before these limits should be hit. .SH ENVIRONMENT VARIABLES -.PP As \f[B]non-portable extensions\f[R], dc(1) recognizes the following environment variables: .TP \f[B]DC_ENV_ARGS\f[R] This is another way to give command-line arguments to dc(1). They should be in the same format as all other command-line arguments. These are always processed first, so any files given in \f[B]DC_ENV_ARGS\f[R] will be processed before arguments and files given on the command-line. This gives the user the ability to set up \[lq]standard\[rq] options and files to be used at every invocation. The most useful thing for such files to contain would be useful functions that the user might want every time dc(1) runs. Another use would be to use the \f[B]-e\f[R] option to set \f[B]scale\f[R] to a value other than \f[B]0\f[R]. .RS .PP The code that parses \f[B]DC_ENV_ARGS\f[R] will correctly handle quoted arguments, but it does not understand escape sequences. For example, the string \f[B]\[lq]/home/gavin/some dc file.dc\[rq]\f[R] will be correctly parsed, but the string \f[B]\[lq]/home/gavin/some \[dq]dc\[dq] file.dc\[rq]\f[R] will include the backslashes. .PP The quote parsing will handle either kind of quotes, \f[B]\[cq]\f[R] or \f[B]\[lq]\f[R]. Thus, if you have a file with any number of single quotes in the name, you can use double quotes as the outside quotes, as in \f[B]\[lq]some `dc' file.dc\[rq]\f[R], and vice versa if you have a file with double quotes. However, handling a file with both kinds of quotes in \f[B]DC_ENV_ARGS\f[R] is not supported due to the complexity of the parsing, though such files are still supported on the command-line where the parsing is done by the shell. .RE .TP \f[B]DC_LINE_LENGTH\f[R] If this environment variable exists and contains an integer that is greater than \f[B]1\f[R] and is less than \f[B]UINT16_MAX\f[R] (\f[B]2\[ha]16-1\f[R]), dc(1) will output lines to that length, including the backslash newline combo. The default line length is \f[B]70\f[R]. .RS .PP The special value of \f[B]0\f[R] will disable line length checking and print numbers without regard to line length and without backslashes and newlines. .RE .TP \f[B]DC_SIGINT_RESET\f[R] If dc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), then this environment variable has no effect because dc(1) exits on \f[B]SIGINT\f[R] when not in interactive mode. .RS .PP However, when dc(1) is in interactive mode, then if this environment variable exists and contains an integer, a non-zero value makes dc(1) reset on \f[B]SIGINT\f[R], rather than exit, and zero makes dc(1) exit. If this environment variable exists and is \f[I]not\f[R] an integer, then dc(1) will exit on \f[B]SIGINT\f[R]. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_TTY_MODE\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, then a non-zero value makes dc(1) use TTY mode, and zero makes dc(1) not use TTY mode. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_PROMPT\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, a non-zero value makes dc(1) use a prompt, and zero or a non-integer makes dc(1) not use a prompt. If this environment variable does not exist and \f[B]DC_TTY_MODE\f[R] does, then the value of the \f[B]DC_TTY_MODE\f[R] environment variable is used. .PP This environment variable and the \f[B]DC_TTY_MODE\f[R] environment variable override the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_EXPR_EXIT\f[R] If any expressions or expression files are given on the command-line with \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R], then if this environment variable exists and contains an integer, a non-zero value makes dc(1) exit after executing the expressions and expression files, and a zero value makes dc(1) not exit. .RS .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_DIGIT_CLAMP\f[R] When parsing numbers and if this environment variable exists and contains an integer, a non-zero value makes dc(1) clamp digits that are greater than or equal to the current \f[B]ibase\f[R] so that all such digits are considered equal to the \f[B]ibase\f[R] minus 1, and a zero value disables such clamping so that those digits are always equal to their value, which is multiplied by the power of the \f[B]ibase\f[R]. .RS .PP This never applies to single-digit numbers, as per the bc(1) standard (see the \f[B]STANDARDS\f[R] section). .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .SH EXIT STATUS -.PP dc(1) returns the following exit statuses: .TP \f[B]0\f[R] No error. .TP \f[B]1\f[R] A math error occurred. This follows standard practice of using \f[B]1\f[R] for expected errors, since math errors will happen in the process of normal execution. .RS .PP Math errors include divide by \f[B]0\f[R], taking the square root of a negative number, attempting to convert a negative number to a hardware integer, overflow when converting a number to a hardware integer, overflow when calculating the size of a number, and attempting to use a non-integer where an integer is required. .PP Converting to a hardware integer happens for the second operand of the power (\f[B]\[ha]\f[R]) operator. .RE .TP \f[B]2\f[R] A parse error occurred. .RS .PP Parse errors include unexpected \f[B]EOF\f[R], using an invalid character, failing to find the end of a string or comment, and using a token where it is invalid. .RE .TP \f[B]3\f[R] A runtime error occurred. .RS .PP Runtime errors include assigning an invalid number to any global (\f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R]), giving a bad expression to a \f[B]read()\f[R] call, calling \f[B]read()\f[R] inside of a \f[B]read()\f[R] call, type errors (including attempting to execute a number), and attempting an operation when the stack has too few elements. .RE .TP \f[B]4\f[R] A fatal error occurred. .RS .PP Fatal errors include memory allocation errors, I/O errors, failing to open files, attempting to use files that do not have only ASCII characters (dc(1) only accepts ASCII characters), attempting to open a directory as a file, and giving invalid command-line options. .RE .PP The exit status \f[B]4\f[R] is special; when a fatal error occurs, dc(1) always exits and returns \f[B]4\f[R], no matter what mode dc(1) is in. .PP The other statuses will only be returned when dc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), since dc(1) resets its state (see the \f[B]RESET\f[R] section) and accepts more input when one of those errors occurs in interactive mode. This is also the case when interactive mode is forced by the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .PP These exit statuses allow dc(1) to be used in shell scripting with error checking, and its normal behavior can be forced by using the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .SH INTERACTIVE MODE -.PP Like bc(1), dc(1) has an interactive mode and a non-interactive mode. Interactive mode is turned on automatically when both \f[B]stdin\f[R] and \f[B]stdout\f[R] are hooked to a terminal, but the \f[B]-i\f[R] flag and \f[B]--interactive\f[R] option can turn it on in other situations. .PP In interactive mode, dc(1) attempts to recover from errors (see the \f[B]RESET\f[R] section), and in normal execution, flushes \f[B]stdout\f[R] as soon as execution is done for the current input. dc(1) may also reset on \f[B]SIGINT\f[R] instead of exit, depending on the contents of, or default for, the \f[B]DC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .SH TTY MODE -.PP If \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY, then \[lq]TTY mode\[rq] is considered to be available, and thus, dc(1) can turn on TTY mode, subject to some settings. .PP If there is the environment variable \f[B]DC_TTY_MODE\f[R] in the environment (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then if that environment variable contains a non-zero integer, dc(1) will turn on TTY mode when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY. If the \f[B]DC_TTY_MODE\f[R] environment variable exists but is \f[I]not\f[R] a non-zero integer, then dc(1) will not turn TTY mode on. .PP If the environment variable \f[B]DC_TTY_MODE\f[R] does \f[I]not\f[R] exist, the default setting is used. The default setting can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP TTY mode is different from interactive mode because interactive mode is required in the bc(1) specification (see the \f[B]STANDARDS\f[R] section), and interactive mode requires only \f[B]stdin\f[R] and \f[B]stdout\f[R] to be connected to a terminal. .SS Command-Line History -.PP Command-line history is only enabled if TTY mode is, i.e., that \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the \f[B]DC_TTY_MODE\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and its default do not disable TTY mode. See the \f[B]COMMAND LINE HISTORY\f[R] section for more information. .SS Prompt -.PP If TTY mode is available, then a prompt can be enabled. Like TTY mode itself, it can be turned on or off with an environment variable: \f[B]DC_PROMPT\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP If the environment variable \f[B]DC_PROMPT\f[R] exists and is a non-zero integer, then the prompt is turned on when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options were not used. The read prompt will be turned on under the same conditions, except that the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options must also not be used. .PP However, if \f[B]DC_PROMPT\f[R] does not exist, the prompt can be enabled or disabled with the \f[B]DC_TTY_MODE\f[R] environment variable, the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options, and the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options. See the \f[B]ENVIRONMENT VARIABLES\f[R] and \f[B]OPTIONS\f[R] sections for more details. .SH SIGNAL HANDLING -.PP Sending a \f[B]SIGINT\f[R] will cause dc(1) to do one of two things. .PP If dc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), or the \f[B]DC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or its default, is either not an integer or it is zero, dc(1) will exit. .PP However, if dc(1) is in interactive mode, and the \f[B]DC_SIGINT_RESET\f[R] or its default is an integer and non-zero, then dc(1) will stop executing the current input and reset (see the \f[B]RESET\f[R] section) upon receiving a \f[B]SIGINT\f[R]. .PP Note that \[lq]current input\[rq] can mean one of two things. If dc(1) is processing input from \f[B]stdin\f[R] in interactive mode, it will ask for more input. If dc(1) is processing input from a file in interactive mode, it will stop processing the file and start processing the next file, if one exists, or ask for input from \f[B]stdin\f[R] if no other file exists. .PP This means that if a \f[B]SIGINT\f[R] is sent to dc(1) as it is executing a file, it can seem as though dc(1) did not respond to the signal since it will immediately start executing the next file. This is by design; most files that users execute when interacting with dc(1) have function definitions, which are quick to parse. If a file takes a long time to execute, there may be a bug in that file. The rest of the files could still be executed without problem, allowing the user to continue. .PP \f[B]SIGTERM\f[R] and \f[B]SIGQUIT\f[R] cause dc(1) to clean up and exit, and it uses the default handler for all other signals. The one exception is \f[B]SIGHUP\f[R]; in that case, and only when dc(1) is in TTY mode (see the \f[B]TTY MODE\f[R] section), a \f[B]SIGHUP\f[R] will cause dc(1) to clean up and exit. .SH COMMAND LINE HISTORY -.PP dc(1) supports interactive command-line editing. .PP If dc(1) can be in TTY mode (see the \f[B]TTY MODE\f[R] section), history can be enabled. This means that command-line history can only be enabled when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY. .PP Like TTY mode itself, it can be turned on or off with the environment variable \f[B]DC_TTY_MODE\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP \f[B]Note\f[R]: tabs are converted to 8 spaces. .SH SEE ALSO -.PP bc(1) .SH STANDARDS -.PP The dc(1) utility operators and some behavior are compliant with the operators in the IEEE Std 1003.1-2017 (\[lq]POSIX.1-2017\[rq]) bc(1) specification at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html . .SH BUGS -.PP None are known. Report bugs at https://git.gavinhoward.com/gavin/bc . .SH AUTHOR -.PP -Gavin D. -Howard and contributors. +Gavin D. Howard \c +.MT gavin@gavinhoward.com +.ME \c +\ and contributors. diff --git a/contrib/bc/manuals/dc/H.1 b/contrib/bc/manuals/dc/H.1 index 8baa0b74316e..caa81645addd 100644 --- a/contrib/bc/manuals/dc/H.1 +++ b/contrib/bc/manuals/dc/H.1 @@ -1,1701 +1,1666 @@ .\" .\" SPDX-License-Identifier: BSD-2-Clause .\" .\" Copyright (c) 2018-2023 Gavin D. Howard and contributors. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions are met: .\" .\" * Redistributions of source code must retain the above copyright notice, .\" this list of conditions and the following disclaimer. .\" .\" * Redistributions in binary form must reproduce the above copyright notice, .\" this list of conditions and the following disclaimer in the documentation .\" and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" .TH "DC" "1" "February 2023" "Gavin D. Howard" "General Commands Manual" .nh .ad l .SH Name -.PP dc - arbitrary-precision decimal reverse-Polish notation calculator .SH SYNOPSIS -.PP \f[B]dc\f[R] [\f[B]-cChiPRvVx\f[R]] [\f[B]--version\f[R]] [\f[B]--help\f[R]] [\f[B]--digit-clamp\f[R]] [\f[B]--no-digit-clamp\f[R]] [\f[B]--interactive\f[R]] [\f[B]--no-prompt\f[R]] [\f[B]--no-read-prompt\f[R]] [\f[B]--extended-register\f[R]] [\f[B]-e\f[R] \f[I]expr\f[R]] [\f[B]--expression\f[R]=\f[I]expr\f[R]\&...] [\f[B]-f\f[R] \f[I]file\f[R]\&...] [\f[B]--file\f[R]=\f[I]file\f[R]\&...] [\f[I]file\f[R]\&...] [\f[B]-I\f[R] \f[I]ibase\f[R]] [\f[B]--ibase\f[R]=\f[I]ibase\f[R]] [\f[B]-O\f[R] \f[I]obase\f[R]] [\f[B]--obase\f[R]=\f[I]obase\f[R]] [\f[B]-S\f[R] \f[I]scale\f[R]] [\f[B]--scale\f[R]=\f[I]scale\f[R]] [\f[B]-E\f[R] \f[I]seed\f[R]] [\f[B]--seed\f[R]=\f[I]seed\f[R]] .SH DESCRIPTION -.PP dc(1) is an arbitrary-precision calculator. It uses a stack (reverse Polish notation) to store numbers and results of computations. Arithmetic operations pop arguments off of the stack and push the results. .PP If no files are given on the command-line, then dc(1) reads from \f[B]stdin\f[R] (see the \f[B]STDIN\f[R] section). Otherwise, those files are processed, and dc(1) will then exit. .PP If a user wants to set up a standard environment, they can use \f[B]DC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). For example, if a user wants the \f[B]scale\f[R] always set to \f[B]10\f[R], they can set \f[B]DC_ENV_ARGS\f[R] to \f[B]-e 10k\f[R], and this dc(1) will always start with a \f[B]scale\f[R] of \f[B]10\f[R]. .SH OPTIONS -.PP The following are the options that dc(1) accepts. .TP \f[B]-C\f[R], \f[B]--no-digit-clamp\f[R] Disables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that the value added to a number from a digit is always that digit\[cq]s value multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-c\f[R] or \f[B]--digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]DC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-c\f[R], \f[B]--digit-clamp\f[R] Enables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that digits that the value added to a number from a digit that is greater than or equal to the ibase is the value of ibase minus 1 all multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-C\f[R] or \f[B]--no-digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]DC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-E\f[R] \f[I]seed\f[R], \f[B]--seed\f[R]=\f[I]seed\f[R] Sets the builtin variable \f[B]seed\f[R] to the value \f[I]seed\f[R] assuming that \f[I]seed\f[R] is in base 10. It is a fatal error if \f[I]seed\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-e\f[R] \f[I]expr\f[R], \f[B]--expression\f[R]=\f[I]expr\f[R] Evaluates \f[I]expr\f[R]. If multiple expressions are given, they are evaluated in order. If files are given as well (see below), the expressions and files are evaluated in the order given. This means that if a file is given before an expression, the file is read in and evaluated first. .RS .PP If this option is given on the command-line (i.e., not in \f[B]DC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, dc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R], whether on the command-line or in \f[B]DC_ENV_ARGS\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, dc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-f\f[R] \f[I]file\f[R], \f[B]--file\f[R]=\f[I]file\f[R] Reads in \f[I]file\f[R] and evaluates it, line by line, as though it were read through \f[B]stdin\f[R]. If expressions are also given (see above), the expressions are evaluated in the order given. .RS .PP If this option is given on the command-line (i.e., not in \f[B]DC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, dc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, dc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-h\f[R], \f[B]--help\f[R] Prints a usage message and exits. .TP \f[B]-I\f[R] \f[I]ibase\f[R], \f[B]--ibase\f[R]=\f[I]ibase\f[R] Sets the builtin variable \f[B]ibase\f[R] to the value \f[I]ibase\f[R] assuming that \f[I]ibase\f[R] is in base 10. It is a fatal error if \f[I]ibase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-i\f[R], \f[B]--interactive\f[R] Forces interactive mode. (See the \f[B]INTERACTIVE MODE\f[R] section.) .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-L\f[R], \f[B]--no-line-length\f[R] Disables line length checking and prints numbers without backslashes and newlines. In other words, this option sets \f[B]BC_LINE_LENGTH\f[R] to \f[B]0\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-O\f[R] \f[I]obase\f[R], \f[B]--obase\f[R]=\f[I]obase\f[R] Sets the builtin variable \f[B]obase\f[R] to the value \f[I]obase\f[R] assuming that \f[I]obase\f[R] is in base 10. It is a fatal error if \f[I]obase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-P\f[R], \f[B]--no-prompt\f[R] Disables the prompt in TTY mode. (The prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a prompt or are not used to having them in dc(1). Most of those users would want to put this option in \f[B]DC_ENV_ARGS\f[R]. .RS .PP These options override the \f[B]DC_PROMPT\f[R] and \f[B]DC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-R\f[R], \f[B]--no-read-prompt\f[R] Disables the read prompt in TTY mode. (The read prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a read prompt or are not used to having them in dc(1). Most of those users would want to put this option in \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). This option is also useful in hash bang lines of dc(1) scripts that prompt for user input. .RS .PP This option does not disable the regular prompt because the read prompt is only used when the \f[B]?\f[R] command is used. .PP These options \f[I]do\f[R] override the \f[B]DC_PROMPT\f[R] and \f[B]DC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), but only for the read prompt. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-S\f[R] \f[I]scale\f[R], \f[B]--scale\f[R]=\f[I]scale\f[R] Sets the builtin variable \f[B]scale\f[R] to the value \f[I]scale\f[R] assuming that \f[I]scale\f[R] is in base 10. It is a fatal error if \f[I]scale\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-v\f[R], \f[B]-V\f[R], \f[B]--version\f[R] Print the version information (copyright header) and exits. .TP \f[B]-x\f[R] \f[B]--extended-register\f[R] Enables extended register mode. See the \f[I]Extended Register Mode\f[R] subsection of the \f[B]REGISTERS\f[R] section for more information. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-z\f[R], \f[B]--leading-zeroes\f[R] Makes dc(1) print all numbers greater than \f[B]-1\f[R] and less than \f[B]1\f[R], and not equal to \f[B]0\f[R], with a leading zero. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .PP All long options are \f[B]non-portable extensions\f[R]. .SH STDIN -.PP If no files are given on the command-line and no files or expressions are given by the \f[B]-f\f[R], \f[B]--file\f[R], \f[B]-e\f[R], or \f[B]--expression\f[R] options, then dc(1) reads from \f[B]stdin\f[R]. .PP However, there is a caveat to this. .PP First, \f[B]stdin\f[R] is evaluated a line at a time. The only exception to this is if a string has been finished, but not ended. This means that, except for escaped brackets, all brackets must be balanced before dc(1) parses and executes. .SH STDOUT -.PP Any non-error output is written to \f[B]stdout\f[R]. In addition, if history (see the \f[B]HISTORY\f[R] section) and the prompt (see the \f[B]TTY MODE\f[R] section) are enabled, both are output to \f[B]stdout\f[R]. .PP \f[B]Note\f[R]: Unlike other dc(1) implementations, this dc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stdout\f[R], so if \f[B]stdout\f[R] is closed, as in -\f[B]dc >&-\f[R], it will quit with an error. +\f[B]dc >&-\f[R], it will quit with an error. This is done so that dc(1) can report problems when \f[B]stdout\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other dc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stdout\f[R] to \f[B]/dev/null\f[R]. .SH STDERR -.PP Any error output is written to \f[B]stderr\f[R]. .PP \f[B]Note\f[R]: Unlike other dc(1) implementations, this dc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stderr\f[R], so if \f[B]stderr\f[R] is closed, as in -\f[B]dc 2>&-\f[R], it will quit with an error. +\f[B]dc 2>&-\f[R], it will quit with an error. This is done so that dc(1) can exit with an error code when \f[B]stderr\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other dc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stderr\f[R] to \f[B]/dev/null\f[R]. .SH SYNTAX -.PP Each item in the input source code, either a number (see the \f[B]NUMBERS\f[R] section) or a command (see the \f[B]COMMANDS\f[R] section), is processed and executed, in order. Input is processed immediately when entered. .PP \f[B]ibase\f[R] is a register (see the \f[B]REGISTERS\f[R] section) that determines how to interpret constant numbers. It is the \[lq]input\[rq] base, or the number base used for interpreting input numbers. \f[B]ibase\f[R] is initially \f[B]10\f[R]. The max allowable value for \f[B]ibase\f[R] is \f[B]16\f[R]. The min allowable value for \f[B]ibase\f[R] is \f[B]2\f[R]. The max allowable value for \f[B]ibase\f[R] can be queried in dc(1) programs with the \f[B]T\f[R] command. .PP \f[B]obase\f[R] is a register (see the \f[B]REGISTERS\f[R] section) that determines how to output results. It is the \[lq]output\[rq] base, or the number base used for outputting numbers. \f[B]obase\f[R] is initially \f[B]10\f[R]. The max allowable value for \f[B]obase\f[R] is \f[B]DC_BASE_MAX\f[R] and can be queried with the \f[B]U\f[R] command. The min allowable value for \f[B]obase\f[R] is \f[B]0\f[R]. If \f[B]obase\f[R] is \f[B]0\f[R], values are output in scientific notation, and if \f[B]obase\f[R] is \f[B]1\f[R], values are output in engineering notation. Otherwise, values are output in the specified base. .PP Outputting in scientific and engineering notations are \f[B]non-portable extensions\f[R]. .PP The \f[I]scale\f[R] of an expression is the number of digits in the result of the expression right of the decimal point, and \f[B]scale\f[R] is a register (see the \f[B]REGISTERS\f[R] section) that sets the precision of any operations (with exceptions). \f[B]scale\f[R] is initially \f[B]0\f[R]. \f[B]scale\f[R] cannot be negative. The max allowable value for \f[B]scale\f[R] can be queried in dc(1) programs with the \f[B]V\f[R] command. .PP \f[B]seed\f[R] is a register containing the current seed for the pseudo-random number generator. If the current value of \f[B]seed\f[R] is queried and stored, then if it is assigned to \f[B]seed\f[R] later, the pseudo-random number generator is guaranteed to produce the same sequence of pseudo-random numbers that were generated after the value of \f[B]seed\f[R] was first queried. .PP Multiple values assigned to \f[B]seed\f[R] can produce the same sequence of pseudo-random numbers. Likewise, when a value is assigned to \f[B]seed\f[R], it is not guaranteed that querying \f[B]seed\f[R] immediately after will return the same value. In addition, the value of \f[B]seed\f[R] will change after any call to the \f[B]\[cq]\f[R] command or the \f[B]\[lq]\f[R] command that does not get receive a value of \f[B]0\f[R] or \f[B]1\f[R]. The maximum integer returned by the \f[B]\[cq]\f[R] command can be queried with the \f[B]W\f[R] command. .PP \f[B]Note\f[R]: The values returned by the pseudo-random number generator with the \f[B]\[cq]\f[R] and \f[B]\[lq]\f[R] commands are guaranteed to \f[B]NOT\f[R] be cryptographically secure. This is a consequence of using a seeded pseudo-random number generator. However, they \f[I]are\f[R] guaranteed to be reproducible with identical \f[B]seed\f[R] values. This means that the pseudo-random values from dc(1) should only be used where a reproducible stream of pseudo-random numbers is \f[I]ESSENTIAL\f[R]. In any other case, use a non-seeded pseudo-random number generator. .PP The pseudo-random number generator, \f[B]seed\f[R], and all associated operations are \f[B]non-portable extensions\f[R]. .SS Comments -.PP Comments go from \f[B]#\f[R] until, and not including, the next newline. This is a \f[B]non-portable extension\f[R]. .SH NUMBERS -.PP Numbers are strings made up of digits, uppercase letters up to \f[B]F\f[R], and at most \f[B]1\f[R] period for a radix. Numbers can have up to \f[B]DC_NUM_MAX\f[R] digits. Uppercase letters are equal to \f[B]9\f[R] plus their position in the alphabet (i.e., \f[B]A\f[R] equals \f[B]10\f[R], or \f[B]9+1\f[R]). .PP If a digit or letter makes no sense with the current value of \f[B]ibase\f[R] (i.e., they are greater than or equal to the current value of \f[B]ibase\f[R]), then the behavior depends on the existence of the \f[B]-c\f[R]/\f[B]--digit-clamp\f[R] or \f[B]-C\f[R]/\f[B]--no-digit-clamp\f[R] options (see the \f[B]OPTIONS\f[R] section), the existence and setting of the \f[B]DC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or the default, which can be queried with the \f[B]-h\f[R]/\f[B]--help\f[R] option. .PP If clamping is off, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are not changed. Instead, their given value is multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*A+3\[ha]0*B\f[R], which is \f[B]3\f[R] times \f[B]10\f[R] plus \f[B]11\f[R], or \f[B]41\f[R]. .PP If clamping is on, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are set to the value of the highest valid digit in \f[B]ibase\f[R] before being multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*2+3\[ha]0*2\f[R], which is \f[B]3\f[R] times \f[B]2\f[R] plus \f[B]2\f[R], or \f[B]8\f[R]. .PP There is one exception to clamping: single-character numbers (i.e., \f[B]A\f[R] alone). Such numbers are never clamped and always take the value they would have in the highest possible \f[B]ibase\f[R]. This means that \f[B]A\f[R] alone always equals decimal \f[B]10\f[R] and \f[B]Z\f[R] alone always equals decimal \f[B]35\f[R]. This behavior is mandated by the standard for bc(1) (see the STANDARDS section) and is meant to provide an easy way to set the current \f[B]ibase\f[R] (with the \f[B]i\f[R] command) regardless of the current value of \f[B]ibase\f[R]. .PP If clamping is on, and the clamped value of a character is needed, use a leading zero, i.e., for \f[B]A\f[R], use \f[B]0A\f[R]. .PP In addition, dc(1) accepts numbers in scientific notation. These have the form \f[B]e\f[R]. The exponent (the portion after the \f[B]e\f[R]) must be an integer. An example is \f[B]1.89237e9\f[R], which is equal to \f[B]1892370000\f[R]. Negative exponents are also allowed, so \f[B]4.2890e_3\f[R] is equal to \f[B]0.0042890\f[R]. .PP \f[B]WARNING\f[R]: Both the number and the exponent in scientific notation are interpreted according to the current \f[B]ibase\f[R], but the number is still multiplied by \f[B]10\[ha]exponent\f[R] regardless of the current \f[B]ibase\f[R]. For example, if \f[B]ibase\f[R] is \f[B]16\f[R] and dc(1) is given the number string \f[B]FFeA\f[R], the resulting decimal number will be \f[B]2550000000000\f[R], and if dc(1) is given the number string \f[B]10e_4\f[R], the resulting decimal number will be \f[B]0.0016\f[R]. .PP Accepting input as scientific notation is a \f[B]non-portable extension\f[R]. .SH COMMANDS -.PP The valid commands are listed below. .SS Printing -.PP These commands are used for printing. .PP Note that both scientific notation and engineering notation are available for printing numbers. Scientific notation is activated by assigning \f[B]0\f[R] to \f[B]obase\f[R] using \f[B]0o\f[R], and engineering notation is activated by assigning \f[B]1\f[R] to \f[B]obase\f[R] using \f[B]1o\f[R]. To deactivate them, just assign a different value to \f[B]obase\f[R]. .PP Printing numbers in scientific notation and/or engineering notation is a \f[B]non-portable extension\f[R]. .TP \f[B]p\f[R] Prints the value on top of the stack, whether number or string, and prints a newline after. .RS .PP This does not alter the stack. .RE .TP \f[B]n\f[R] Prints the value on top of the stack, whether number or string, and pops it off of the stack. .TP \f[B]P\f[R] Pops a value off the stack. .RS .PP If the value is a number, it is truncated and the absolute value of the result is printed as though \f[B]obase\f[R] is \f[B]256\f[R] and each digit is interpreted as an 8-bit ASCII character, making it a byte stream. .PP If the value is a string, it is printed without a trailing newline. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]f\f[R] Prints the entire contents of the stack, in order from newest to oldest, without altering anything. .RS .PP Users should use this command when they get lost. .RE .SS Arithmetic -.PP These are the commands used for arithmetic. .TP \f[B]+\f[R] The top two values are popped off the stack, added, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to the max \f[I]scale\f[R] of both operands. .TP \f[B]-\f[R] The top two values are popped off the stack, subtracted, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to the max \f[I]scale\f[R] of both operands. .TP \f[B]*\f[R] The top two values are popped off the stack, multiplied, and the result is pushed onto the stack. If \f[B]a\f[R] is the \f[I]scale\f[R] of the first expression and \f[B]b\f[R] is the \f[I]scale\f[R] of the second expression, the \f[I]scale\f[R] of the result is equal to \f[B]min(a+b,max(scale,a,b))\f[R] where \f[B]min()\f[R] and \f[B]max()\f[R] return the obvious values. .TP \f[B]/\f[R] The top two values are popped off the stack, divided, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The first value popped off of the stack must be non-zero. .RE .TP \f[B]%\f[R] The top two values are popped off the stack, remaindered, and the result is pushed onto the stack. .RS .PP Remaindering is equivalent to 1) Computing \f[B]a/b\f[R] to current \f[B]scale\f[R], and 2) Using the result of step 1 to calculate \f[B]a-(a/b)*b\f[R] to \f[I]scale\f[R] \f[B]max(scale+scale(b),scale(a))\f[R]. .PP The first value popped off of the stack must be non-zero. .RE .TP \f[B]\[ti]\f[R] The top two values are popped off the stack, divided and remaindered, and the results (divided first, remainder second) are pushed onto the stack. This is equivalent to \f[B]x y / x y %\f[R] except that \f[B]x\f[R] and \f[B]y\f[R] are only evaluated once. .RS .PP The first value popped off of the stack must be non-zero. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[ha]\f[R] The top two values are popped off the stack, the second is raised to the power of the first, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The first value popped off of the stack must be an integer, and if that value is negative, the second value popped off of the stack must be non-zero. .RE .TP \f[B]v\f[R] The top value is popped off the stack, its square root is computed, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The value popped off of the stack must be non-negative. .RE .TP \f[B]_\f[R] If this command \f[I]immediately\f[R] precedes a number (i.e., no spaces or other commands), then that number is input as a negative number. .RS .PP Otherwise, the top value on the stack is popped and copied, and the copy is negated and pushed onto the stack. This behavior without a number is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]b\f[R] The top value is popped off the stack, and if it is zero, it is pushed back onto the stack. Otherwise, its absolute value is pushed onto the stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]|\f[R] The top three values are popped off the stack, a modular exponentiation is computed, and the result is pushed onto the stack. .RS .PP The first value popped is used as the reduction modulus and must be an integer and non-zero. The second value popped is used as the exponent and must be an integer and non-negative. The third value popped is the base and must be an integer. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]$\f[R] The top value is popped off the stack and copied, and the copy is truncated and pushed onto the stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[at]\f[R] The top two values are popped off the stack, and the precision of the second is set to the value of the first, whether by truncation or extension. .RS .PP The first value popped off of the stack must be an integer and non-negative. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]H\f[R] The top two values are popped off the stack, and the second is shifted left (radix shifted right) to the value of the first. .RS .PP The first value popped off of the stack must be an integer and non-negative. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]h\f[R] The top two values are popped off the stack, and the second is shifted right (radix shifted left) to the value of the first. .RS .PP The first value popped off of the stack must be an integer and non-negative. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]G\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if they are equal, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]N\f[R] The top value is popped off of the stack, and if it a \f[B]0\f[R], a \f[B]1\f[R] is pushed; otherwise, a \f[B]0\f[R] is pushed. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B](\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is less than the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]{\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is less than or equal to the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B])\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is greater than the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]}\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is greater than or equal to the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]M\f[R] The top two values are popped off of the stack. If they are both non-zero, a \f[B]1\f[R] is pushed onto the stack. If either of them is zero, or both of them are, then a \f[B]0\f[R] is pushed onto the stack. .RS .PP This is like the \f[B]&&\f[R] operator in bc(1), and it is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]m\f[R] The top two values are popped off of the stack. If at least one of them is non-zero, a \f[B]1\f[R] is pushed onto the stack. If both of them are zero, then a \f[B]0\f[R] is pushed onto the stack. .RS .PP This is like the \f[B]||\f[R] operator in bc(1), and it is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Pseudo-Random Number Generator -.PP dc(1) has a built-in pseudo-random number generator. These commands query the pseudo-random number generator. (See Parameters for more information about the \f[B]seed\f[R] value that controls the pseudo-random number generator.) .PP The pseudo-random number generator is guaranteed to \f[B]NOT\f[R] be cryptographically secure. .TP \f[B]\[cq]\f[R] Generates an integer between 0 and \f[B]DC_RAND_MAX\f[R], inclusive (see the \f[B]LIMITS\f[R] section). .RS .PP The generated integer is made as unbiased as possible, subject to the limitations of the pseudo-random number generator. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[lq]\f[R] Pops a value off of the stack, which is used as an \f[B]exclusive\f[R] upper bound on the integer that will be generated. If the bound is negative or is a non-integer, an error is raised, and dc(1) resets (see the \f[B]RESET\f[R] section) while \f[B]seed\f[R] remains unchanged. If the bound is larger than \f[B]DC_RAND_MAX\f[R], the higher bound is honored by generating several pseudo-random integers, multiplying them by appropriate powers of \f[B]DC_RAND_MAX+1\f[R], and adding them together. Thus, the size of integer that can be generated with this command is unbounded. Using this command will change the value of \f[B]seed\f[R], unless the operand is \f[B]0\f[R] or \f[B]1\f[R]. In that case, \f[B]0\f[R] is pushed onto the stack, and \f[B]seed\f[R] is \f[I]not\f[R] changed. .RS .PP The generated integer is made as unbiased as possible, subject to the limitations of the pseudo-random number generator. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Stack Control -.PP These commands control the stack. .TP \f[B]c\f[R] Removes all items from (\[lq]clears\[rq]) the stack. .TP \f[B]d\f[R] Copies the item on top of the stack (\[lq]duplicates\[rq]) and pushes the copy onto the stack. .TP \f[B]r\f[R] Swaps (\[lq]reverses\[rq]) the two top items on the stack. .TP \f[B]R\f[R] Pops (\[lq]removes\[rq]) the top value from the stack. .SS Register Control -.PP These commands control registers (see the \f[B]REGISTERS\f[R] section). .TP \f[B]s\f[R]\f[I]r\f[R] Pops the value off the top of the stack and stores it into register \f[I]r\f[R]. .TP \f[B]l\f[R]\f[I]r\f[R] Copies the value in register \f[I]r\f[R] and pushes it onto the stack. This does not alter the contents of \f[I]r\f[R]. .TP \f[B]S\f[R]\f[I]r\f[R] Pops the value off the top of the (main) stack and pushes it onto the stack of register \f[I]r\f[R]. The previous value of the register becomes inaccessible. .TP \f[B]L\f[R]\f[I]r\f[R] Pops the value off the top of the stack for register \f[I]r\f[R] and push it onto the main stack. The previous value in the stack for register \f[I]r\f[R], if any, is now accessible via the \f[B]l\f[R]\f[I]r\f[R] command. .SS Parameters -.PP These commands control the values of \f[B]ibase\f[R], \f[B]obase\f[R], \f[B]scale\f[R], and \f[B]seed\f[R]. Also see the \f[B]SYNTAX\f[R] section. .TP \f[B]i\f[R] Pops the value off of the top of the stack and uses it to set \f[B]ibase\f[R], which must be between \f[B]2\f[R] and \f[B]16\f[R], inclusive. .RS .PP If the value on top of the stack has any \f[I]scale\f[R], the \f[I]scale\f[R] is ignored. .RE .TP \f[B]o\f[R] Pops the value off of the top of the stack and uses it to set \f[B]obase\f[R], which must be between \f[B]0\f[R] and \f[B]DC_BASE_MAX\f[R], inclusive (see the \f[B]LIMITS\f[R] section and the \f[B]NUMBERS\f[R] section). .RS .PP If the value on top of the stack has any \f[I]scale\f[R], the \f[I]scale\f[R] is ignored. .RE .TP \f[B]k\f[R] Pops the value off of the top of the stack and uses it to set \f[B]scale\f[R], which must be non-negative. .RS .PP If the value on top of the stack has any \f[I]scale\f[R], the \f[I]scale\f[R] is ignored. .RE .TP \f[B]j\f[R] Pops the value off of the top of the stack and uses it to set \f[B]seed\f[R]. The meaning of \f[B]seed\f[R] is dependent on the current pseudo-random number generator but is guaranteed to not change except for new major versions. .RS .PP The \f[I]scale\f[R] and sign of the value may be significant. .PP If a previously used \f[B]seed\f[R] value is used again, the pseudo-random number generator is guaranteed to produce the same sequence of pseudo-random numbers as it did when the \f[B]seed\f[R] value was previously used. .PP The exact value assigned to \f[B]seed\f[R] is not guaranteed to be returned if the \f[B]J\f[R] command is used. However, if \f[B]seed\f[R] \f[I]does\f[R] return a different value, both values, when assigned to \f[B]seed\f[R], are guaranteed to produce the same sequence of pseudo-random numbers. This means that certain values assigned to \f[B]seed\f[R] will not produce unique sequences of pseudo-random numbers. .PP There is no limit to the length (number of significant decimal digits) or \f[I]scale\f[R] of the value that can be assigned to \f[B]seed\f[R]. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]I\f[R] Pushes the current value of \f[B]ibase\f[R] onto the main stack. .TP \f[B]O\f[R] Pushes the current value of \f[B]obase\f[R] onto the main stack. .TP \f[B]K\f[R] Pushes the current value of \f[B]scale\f[R] onto the main stack. .TP \f[B]J\f[R] Pushes the current value of \f[B]seed\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]T\f[R] Pushes the maximum allowable value of \f[B]ibase\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]U\f[R] Pushes the maximum allowable value of \f[B]obase\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]V\f[R] Pushes the maximum allowable value of \f[B]scale\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]W\f[R] Pushes the maximum (inclusive) integer that can be generated with the \f[B]\[cq]\f[R] pseudo-random number generator command. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Strings -.PP The following commands control strings. .PP dc(1) can work with both numbers and strings, and registers (see the \f[B]REGISTERS\f[R] section) can hold both strings and numbers. dc(1) always knows whether the contents of a register are a string or a number. .PP While arithmetic operations have to have numbers, and will print an error if given a string, other commands accept strings. .PP Strings can also be executed as macros. For example, if the string \f[B][1pR]\f[R] is executed as a macro, then the code \f[B]1pR\f[R] is executed, meaning that the \f[B]1\f[R] will be printed with a newline after and then popped from the stack. .TP \f[B][\f[R]\f[I]characters\f[R]\f[B]]\f[R] Makes a string containing \f[I]characters\f[R] and pushes it onto the stack. .RS .PP If there are brackets (\f[B][\f[R] and \f[B]]\f[R]) in the string, then they must be balanced. Unbalanced brackets can be escaped using a backslash (\f[B]\[rs]\f[R]) character. .PP If there is a backslash character in the string, the character after it (even another backslash) is put into the string verbatim, but the (first) backslash is not. .RE .TP \f[B]a\f[R] The value on top of the stack is popped. .RS .PP If it is a number, it is truncated and its absolute value is taken. The result mod \f[B]256\f[R] is calculated. If that result is \f[B]0\f[R], push an empty string; otherwise, push a one-character string where the character is the result of the mod interpreted as an ASCII character. .PP If it is a string, then a new string is made. If the original string is empty, the new string is empty. If it is not, then the first character of the original string is used to create the new string as a one-character string. The new string is then pushed onto the stack. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]x\f[R] Pops a value off of the top of the stack. .RS .PP If it is a number, it is pushed back onto the stack. .PP If it is a string, it is executed as a macro. .PP This behavior is the norm whenever a macro is executed, whether by this command or by the conditional execution commands below. .RE .TP \f[B]>\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is greater than the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP For example, \f[B]0 1>a\f[R] will execute the contents of register \f[B]a\f[R], and \f[B]1 0>a\f[R] will not. .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]>\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]!>\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is not greater than the second (less than or equal to), then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]!>\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]<\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is less than the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]<\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]!<\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is not less than the second (greater than or equal to), then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]!<\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]=\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is equal to the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]=\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]!=\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is not equal to the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]!=\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]?\f[R] Reads a line from the \f[B]stdin\f[R] and executes it. This is to allow macros to request input from users. .TP \f[B]q\f[R] During execution of a macro, this exits the execution of that macro and the execution of the macro that executed it. If there are no macros, or only one macro executing, dc(1) exits. .TP \f[B]Q\f[R] Pops a value from the stack which must be non-negative and is used the number of macro executions to pop off of the execution stack. If the number of levels to pop is greater than the number of executing macros, dc(1) exits. .TP \f[B],\f[R] Pushes the depth of the execution stack onto the stack. The execution stack is the stack of string executions. The number that is pushed onto the stack is exactly as many as is needed to make dc(1) exit with the \f[B]Q\f[R] command, so the sequence \f[B],Q\f[R] will make dc(1) exit. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Status -.PP These commands query status of the stack or its top value. .TP \f[B]Z\f[R] Pops a value off of the stack. .RS .PP If it is a number, calculates the number of significant decimal digits it has and pushes the result. It will push \f[B]1\f[R] if the argument is \f[B]0\f[R] with no decimal places. .PP If it is a string, pushes the number of characters the string has. .RE .TP \f[B]X\f[R] Pops a value off of the stack. .RS .PP If it is a number, pushes the \f[I]scale\f[R] of the value onto the stack. .PP If it is a string, pushes \f[B]0\f[R]. .RE .TP \f[B]u\f[R] Pops one value off of the stack. If the value is a number, this pushes \f[B]1\f[R] onto the stack. Otherwise (if it is a string), it pushes \f[B]0\f[R]. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]t\f[R] Pops one value off of the stack. If the value is a string, this pushes \f[B]1\f[R] onto the stack. Otherwise (if it is a number), it pushes \f[B]0\f[R]. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]z\f[R] Pushes the current depth of the stack (before execution of this command) onto the stack. .TP \f[B]y\f[R]\f[I]r\f[R] Pushes the current stack depth of the register \f[I]r\f[R] onto the main stack. .RS .PP Because each register has a depth of \f[B]1\f[R] (with the value \f[B]0\f[R] in the top item) when dc(1) starts, dc(1) requires that each register\[cq]s stack must always have at least one item; dc(1) will give an error and reset otherwise (see the \f[B]RESET\f[R] section). This means that this command will never push \f[B]0\f[R]. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Arrays -.PP These commands manipulate arrays. .TP \f[B]:\f[R]\f[I]r\f[R] Pops the top two values off of the stack. The second value will be stored in the array \f[I]r\f[R] (see the \f[B]REGISTERS\f[R] section), indexed by the first value. .TP \f[B];\f[R]\f[I]r\f[R] Pops the value on top of the stack and uses it as an index into the array \f[I]r\f[R]. The selected value is then pushed onto the stack. .TP \f[B]Y\f[R]\f[I]r\f[R] Pushes the length of the array \f[I]r\f[R] onto the stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Global Settings -.PP These commands retrieve global settings. These are the only commands that require multiple specific characters, and all of them begin with the letter \f[B]g\f[R]. Only the characters below are allowed after the character \f[B]g\f[R]; any other character produces a parse error (see the \f[B]ERRORS\f[R] section). .TP \f[B]gl\f[R] Pushes the line length set by \f[B]DC_LINE_LENGTH\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) onto the stack. .TP \f[B]gx\f[R] Pushes \f[B]1\f[R] onto the stack if extended register mode is on, \f[B]0\f[R] otherwise. See the \f[I]Extended Register Mode\f[R] subsection of the \f[B]REGISTERS\f[R] section for more information. .TP \f[B]gz\f[R] Pushes \f[B]0\f[R] onto the stack if the leading zero setting has not been enabled with the \f[B]-z\f[R] or \f[B]--leading-zeroes\f[R] options (see the \f[B]OPTIONS\f[R] section), non-zero otherwise. .SH REGISTERS -.PP Registers are names that can store strings, numbers, and arrays. (Number/string registers do not interfere with array registers.) .PP Each register is also its own stack, so the current register value is the top of the stack for the register. All registers, when first referenced, have one value (\f[B]0\f[R]) in their stack, and it is a runtime error to attempt to pop that item off of the register stack. .PP In non-extended register mode, a register name is just the single character that follows any command that needs a register name. The only exceptions are: a newline (\f[B]`\[rs]n'\f[R]) and a left bracket (\f[B]`['\f[R]); it is a parse error for a newline or a left bracket to be used as a register name. .SS Extended Register Mode -.PP Unlike most other dc(1) implentations, this dc(1) provides nearly unlimited amounts of registers, if extended register mode is enabled. .PP If extended register mode is enabled (\f[B]-x\f[R] or \f[B]--extended-register\f[R] command-line arguments are given), then normal single character registers are used \f[I]unless\f[R] the character immediately following a command that needs a register name is a space (according to \f[B]isspace()\f[R]) and not a newline (\f[B]`\[rs]n'\f[R]). .PP In that case, the register name is found according to the regex \f[B][a-z][a-z0-9_]*\f[R] (like bc(1) identifiers), and it is a parse error if the next non-space characters do not match that regex. .SH RESET -.PP When dc(1) encounters an error or a signal that it has a non-default handler for, it resets. This means that several things happen. .PP First, any macros that are executing are stopped and popped off the stack. The behavior is not unlike that of exceptions in programming languages. Then the execution point is set so that any code waiting to execute (after all macros returned) is skipped. .PP Thus, when dc(1) resets, it skips any remaining code waiting to be executed. Then, if it is interactive mode, and the error was not a fatal error (see the \f[B]EXIT STATUS\f[R] section), it asks for more input; otherwise, it exits with the appropriate return code. .SH PERFORMANCE -.PP Most dc(1) implementations use \f[B]char\f[R] types to calculate the value of \f[B]1\f[R] decimal digit at a time, but that can be slow. This dc(1) does something different. .PP It uses large integers to calculate more than \f[B]1\f[R] decimal digit at a time. If built in a environment where \f[B]DC_LONG_BIT\f[R] (see the \f[B]LIMITS\f[R] section) is \f[B]64\f[R], then each integer has \f[B]9\f[R] decimal digits. If built in an environment where \f[B]DC_LONG_BIT\f[R] is \f[B]32\f[R] then each integer has \f[B]4\f[R] decimal digits. This value (the number of decimal digits per large integer) is called \f[B]DC_BASE_DIGS\f[R]. .PP In addition, this dc(1) uses an even larger integer for overflow checking. This integer type depends on the value of \f[B]DC_LONG_BIT\f[R], but is always at least twice as large as the integer type used to store digits. .SH LIMITS -.PP The following are the limits on dc(1): .TP \f[B]DC_LONG_BIT\f[R] The number of bits in the \f[B]long\f[R] type in the environment where dc(1) was built. This determines how many decimal digits can be stored in a single large integer (see the \f[B]PERFORMANCE\f[R] section). .TP \f[B]DC_BASE_DIGS\f[R] The number of decimal digits per large integer (see the \f[B]PERFORMANCE\f[R] section). Depends on \f[B]DC_LONG_BIT\f[R]. .TP \f[B]DC_BASE_POW\f[R] The max decimal number that each large integer can store (see \f[B]DC_BASE_DIGS\f[R]) plus \f[B]1\f[R]. Depends on \f[B]DC_BASE_DIGS\f[R]. .TP \f[B]DC_OVERFLOW_MAX\f[R] The max number that the overflow type (see the \f[B]PERFORMANCE\f[R] section) can hold. Depends on \f[B]DC_LONG_BIT\f[R]. .TP \f[B]DC_BASE_MAX\f[R] The maximum output base. Set at \f[B]DC_BASE_POW\f[R]. .TP \f[B]DC_DIM_MAX\f[R] The maximum size of arrays. Set at \f[B]SIZE_MAX-1\f[R]. .TP \f[B]DC_SCALE_MAX\f[R] The maximum \f[B]scale\f[R]. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_STRING_MAX\f[R] The maximum length of strings. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_NAME_MAX\f[R] The maximum length of identifiers. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_NUM_MAX\f[R] The maximum length of a number (in decimal digits), which includes digits after the decimal point. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_RAND_MAX\f[R] The maximum integer (inclusive) returned by the \f[B]\[cq]\f[R] command, if dc(1). Set at \f[B]2\[ha]DC_LONG_BIT-1\f[R]. .TP Exponent The maximum allowable exponent (positive or negative). Set at \f[B]DC_OVERFLOW_MAX\f[R]. .TP Number of vars The maximum number of vars/arrays. Set at \f[B]SIZE_MAX-1\f[R]. .PP These limits are meant to be effectively non-existent; the limits are so large (at least on 64-bit machines) that there should not be any point at which they become a problem. In fact, memory should be exhausted before these limits should be hit. .SH ENVIRONMENT VARIABLES -.PP As \f[B]non-portable extensions\f[R], dc(1) recognizes the following environment variables: .TP \f[B]DC_ENV_ARGS\f[R] This is another way to give command-line arguments to dc(1). They should be in the same format as all other command-line arguments. These are always processed first, so any files given in \f[B]DC_ENV_ARGS\f[R] will be processed before arguments and files given on the command-line. This gives the user the ability to set up \[lq]standard\[rq] options and files to be used at every invocation. The most useful thing for such files to contain would be useful functions that the user might want every time dc(1) runs. Another use would be to use the \f[B]-e\f[R] option to set \f[B]scale\f[R] to a value other than \f[B]0\f[R]. .RS .PP The code that parses \f[B]DC_ENV_ARGS\f[R] will correctly handle quoted arguments, but it does not understand escape sequences. For example, the string \f[B]\[lq]/home/gavin/some dc file.dc\[rq]\f[R] will be correctly parsed, but the string \f[B]\[lq]/home/gavin/some \[dq]dc\[dq] file.dc\[rq]\f[R] will include the backslashes. .PP The quote parsing will handle either kind of quotes, \f[B]\[cq]\f[R] or \f[B]\[lq]\f[R]. Thus, if you have a file with any number of single quotes in the name, you can use double quotes as the outside quotes, as in \f[B]\[lq]some `dc' file.dc\[rq]\f[R], and vice versa if you have a file with double quotes. However, handling a file with both kinds of quotes in \f[B]DC_ENV_ARGS\f[R] is not supported due to the complexity of the parsing, though such files are still supported on the command-line where the parsing is done by the shell. .RE .TP \f[B]DC_LINE_LENGTH\f[R] If this environment variable exists and contains an integer that is greater than \f[B]1\f[R] and is less than \f[B]UINT16_MAX\f[R] (\f[B]2\[ha]16-1\f[R]), dc(1) will output lines to that length, including the backslash newline combo. The default line length is \f[B]70\f[R]. .RS .PP The special value of \f[B]0\f[R] will disable line length checking and print numbers without regard to line length and without backslashes and newlines. .RE .TP \f[B]DC_SIGINT_RESET\f[R] If dc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), then this environment variable has no effect because dc(1) exits on \f[B]SIGINT\f[R] when not in interactive mode. .RS .PP However, when dc(1) is in interactive mode, then if this environment variable exists and contains an integer, a non-zero value makes dc(1) reset on \f[B]SIGINT\f[R], rather than exit, and zero makes dc(1) exit. If this environment variable exists and is \f[I]not\f[R] an integer, then dc(1) will exit on \f[B]SIGINT\f[R]. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_TTY_MODE\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, then a non-zero value makes dc(1) use TTY mode, and zero makes dc(1) not use TTY mode. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_PROMPT\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, a non-zero value makes dc(1) use a prompt, and zero or a non-integer makes dc(1) not use a prompt. If this environment variable does not exist and \f[B]DC_TTY_MODE\f[R] does, then the value of the \f[B]DC_TTY_MODE\f[R] environment variable is used. .PP This environment variable and the \f[B]DC_TTY_MODE\f[R] environment variable override the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_EXPR_EXIT\f[R] If any expressions or expression files are given on the command-line with \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R], then if this environment variable exists and contains an integer, a non-zero value makes dc(1) exit after executing the expressions and expression files, and a zero value makes dc(1) not exit. .RS .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_DIGIT_CLAMP\f[R] When parsing numbers and if this environment variable exists and contains an integer, a non-zero value makes dc(1) clamp digits that are greater than or equal to the current \f[B]ibase\f[R] so that all such digits are considered equal to the \f[B]ibase\f[R] minus 1, and a zero value disables such clamping so that those digits are always equal to their value, which is multiplied by the power of the \f[B]ibase\f[R]. .RS .PP This never applies to single-digit numbers, as per the bc(1) standard (see the \f[B]STANDARDS\f[R] section). .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .SH EXIT STATUS -.PP dc(1) returns the following exit statuses: .TP \f[B]0\f[R] No error. .TP \f[B]1\f[R] A math error occurred. This follows standard practice of using \f[B]1\f[R] for expected errors, since math errors will happen in the process of normal execution. .RS .PP Math errors include divide by \f[B]0\f[R], taking the square root of a negative number, using a negative number as a bound for the pseudo-random number generator, attempting to convert a negative number to a hardware integer, overflow when converting a number to a hardware integer, overflow when calculating the size of a number, and attempting to use a non-integer where an integer is required. .PP Converting to a hardware integer happens for the second operand of the power (\f[B]\[ha]\f[R]), places (\f[B]\[at]\f[R]), left shift (\f[B]H\f[R]), and right shift (\f[B]h\f[R]) operators. .RE .TP \f[B]2\f[R] A parse error occurred. .RS .PP Parse errors include unexpected \f[B]EOF\f[R], using an invalid character, failing to find the end of a string or comment, and using a token where it is invalid. .RE .TP \f[B]3\f[R] A runtime error occurred. .RS .PP Runtime errors include assigning an invalid number to any global (\f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R]), giving a bad expression to a \f[B]read()\f[R] call, calling \f[B]read()\f[R] inside of a \f[B]read()\f[R] call, type errors (including attempting to execute a number), and attempting an operation when the stack has too few elements. .RE .TP \f[B]4\f[R] A fatal error occurred. .RS .PP Fatal errors include memory allocation errors, I/O errors, failing to open files, attempting to use files that do not have only ASCII characters (dc(1) only accepts ASCII characters), attempting to open a directory as a file, and giving invalid command-line options. .RE .PP The exit status \f[B]4\f[R] is special; when a fatal error occurs, dc(1) always exits and returns \f[B]4\f[R], no matter what mode dc(1) is in. .PP The other statuses will only be returned when dc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), since dc(1) resets its state (see the \f[B]RESET\f[R] section) and accepts more input when one of those errors occurs in interactive mode. This is also the case when interactive mode is forced by the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .PP These exit statuses allow dc(1) to be used in shell scripting with error checking, and its normal behavior can be forced by using the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .SH INTERACTIVE MODE -.PP Like bc(1), dc(1) has an interactive mode and a non-interactive mode. Interactive mode is turned on automatically when both \f[B]stdin\f[R] and \f[B]stdout\f[R] are hooked to a terminal, but the \f[B]-i\f[R] flag and \f[B]--interactive\f[R] option can turn it on in other situations. .PP In interactive mode, dc(1) attempts to recover from errors (see the \f[B]RESET\f[R] section), and in normal execution, flushes \f[B]stdout\f[R] as soon as execution is done for the current input. dc(1) may also reset on \f[B]SIGINT\f[R] instead of exit, depending on the contents of, or default for, the \f[B]DC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .SH TTY MODE -.PP If \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY, then \[lq]TTY mode\[rq] is considered to be available, and thus, dc(1) can turn on TTY mode, subject to some settings. .PP If there is the environment variable \f[B]DC_TTY_MODE\f[R] in the environment (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then if that environment variable contains a non-zero integer, dc(1) will turn on TTY mode when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY. If the \f[B]DC_TTY_MODE\f[R] environment variable exists but is \f[I]not\f[R] a non-zero integer, then dc(1) will not turn TTY mode on. .PP If the environment variable \f[B]DC_TTY_MODE\f[R] does \f[I]not\f[R] exist, the default setting is used. The default setting can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP TTY mode is different from interactive mode because interactive mode is required in the bc(1) specification (see the \f[B]STANDARDS\f[R] section), and interactive mode requires only \f[B]stdin\f[R] and \f[B]stdout\f[R] to be connected to a terminal. .SS Prompt -.PP If TTY mode is available, then a prompt can be enabled. Like TTY mode itself, it can be turned on or off with an environment variable: \f[B]DC_PROMPT\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP If the environment variable \f[B]DC_PROMPT\f[R] exists and is a non-zero integer, then the prompt is turned on when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options were not used. The read prompt will be turned on under the same conditions, except that the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options must also not be used. .PP However, if \f[B]DC_PROMPT\f[R] does not exist, the prompt can be enabled or disabled with the \f[B]DC_TTY_MODE\f[R] environment variable, the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options, and the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options. See the \f[B]ENVIRONMENT VARIABLES\f[R] and \f[B]OPTIONS\f[R] sections for more details. .SH SIGNAL HANDLING -.PP Sending a \f[B]SIGINT\f[R] will cause dc(1) to do one of two things. .PP If dc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), or the \f[B]DC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or its default, is either not an integer or it is zero, dc(1) will exit. .PP However, if dc(1) is in interactive mode, and the \f[B]DC_SIGINT_RESET\f[R] or its default is an integer and non-zero, then dc(1) will stop executing the current input and reset (see the \f[B]RESET\f[R] section) upon receiving a \f[B]SIGINT\f[R]. .PP Note that \[lq]current input\[rq] can mean one of two things. If dc(1) is processing input from \f[B]stdin\f[R] in interactive mode, it will ask for more input. If dc(1) is processing input from a file in interactive mode, it will stop processing the file and start processing the next file, if one exists, or ask for input from \f[B]stdin\f[R] if no other file exists. .PP This means that if a \f[B]SIGINT\f[R] is sent to dc(1) as it is executing a file, it can seem as though dc(1) did not respond to the signal since it will immediately start executing the next file. This is by design; most files that users execute when interacting with dc(1) have function definitions, which are quick to parse. If a file takes a long time to execute, there may be a bug in that file. The rest of the files could still be executed without problem, allowing the user to continue. .PP \f[B]SIGTERM\f[R] and \f[B]SIGQUIT\f[R] cause dc(1) to clean up and exit, and it uses the default handler for all other signals. .SH LOCALES -.PP This dc(1) ships with support for adding error messages for different locales and thus, supports \f[B]LC_MESSAGES\f[R]. .SH SEE ALSO -.PP bc(1) .SH STANDARDS -.PP The dc(1) utility operators and some behavior are compliant with the operators in the IEEE Std 1003.1-2017 (\[lq]POSIX.1-2017\[rq]) bc(1) specification at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html . .SH BUGS -.PP None are known. Report bugs at https://git.gavinhoward.com/gavin/bc . .SH AUTHOR -.PP -Gavin D. -Howard and contributors. +Gavin D. Howard \c +.MT gavin@gavinhoward.com +.ME \c +\ and contributors. diff --git a/contrib/bc/manuals/dc/HN.1 b/contrib/bc/manuals/dc/HN.1 index 2a3926309868..cdfa4ad82617 100644 --- a/contrib/bc/manuals/dc/HN.1 +++ b/contrib/bc/manuals/dc/HN.1 @@ -1,1697 +1,1663 @@ .\" .\" SPDX-License-Identifier: BSD-2-Clause .\" .\" Copyright (c) 2018-2023 Gavin D. Howard and contributors. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions are met: .\" .\" * Redistributions of source code must retain the above copyright notice, .\" this list of conditions and the following disclaimer. .\" .\" * Redistributions in binary form must reproduce the above copyright notice, .\" this list of conditions and the following disclaimer in the documentation .\" and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" .TH "DC" "1" "February 2023" "Gavin D. Howard" "General Commands Manual" .nh .ad l .SH Name -.PP dc - arbitrary-precision decimal reverse-Polish notation calculator .SH SYNOPSIS -.PP \f[B]dc\f[R] [\f[B]-cChiPRvVx\f[R]] [\f[B]--version\f[R]] [\f[B]--help\f[R]] [\f[B]--digit-clamp\f[R]] [\f[B]--no-digit-clamp\f[R]] [\f[B]--interactive\f[R]] [\f[B]--no-prompt\f[R]] [\f[B]--no-read-prompt\f[R]] [\f[B]--extended-register\f[R]] [\f[B]-e\f[R] \f[I]expr\f[R]] [\f[B]--expression\f[R]=\f[I]expr\f[R]\&...] [\f[B]-f\f[R] \f[I]file\f[R]\&...] [\f[B]--file\f[R]=\f[I]file\f[R]\&...] [\f[I]file\f[R]\&...] [\f[B]-I\f[R] \f[I]ibase\f[R]] [\f[B]--ibase\f[R]=\f[I]ibase\f[R]] [\f[B]-O\f[R] \f[I]obase\f[R]] [\f[B]--obase\f[R]=\f[I]obase\f[R]] [\f[B]-S\f[R] \f[I]scale\f[R]] [\f[B]--scale\f[R]=\f[I]scale\f[R]] [\f[B]-E\f[R] \f[I]seed\f[R]] [\f[B]--seed\f[R]=\f[I]seed\f[R]] .SH DESCRIPTION -.PP dc(1) is an arbitrary-precision calculator. It uses a stack (reverse Polish notation) to store numbers and results of computations. Arithmetic operations pop arguments off of the stack and push the results. .PP If no files are given on the command-line, then dc(1) reads from \f[B]stdin\f[R] (see the \f[B]STDIN\f[R] section). Otherwise, those files are processed, and dc(1) will then exit. .PP If a user wants to set up a standard environment, they can use \f[B]DC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). For example, if a user wants the \f[B]scale\f[R] always set to \f[B]10\f[R], they can set \f[B]DC_ENV_ARGS\f[R] to \f[B]-e 10k\f[R], and this dc(1) will always start with a \f[B]scale\f[R] of \f[B]10\f[R]. .SH OPTIONS -.PP The following are the options that dc(1) accepts. .TP \f[B]-C\f[R], \f[B]--no-digit-clamp\f[R] Disables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that the value added to a number from a digit is always that digit\[cq]s value multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-c\f[R] or \f[B]--digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]DC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-c\f[R], \f[B]--digit-clamp\f[R] Enables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that digits that the value added to a number from a digit that is greater than or equal to the ibase is the value of ibase minus 1 all multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-C\f[R] or \f[B]--no-digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]DC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-E\f[R] \f[I]seed\f[R], \f[B]--seed\f[R]=\f[I]seed\f[R] Sets the builtin variable \f[B]seed\f[R] to the value \f[I]seed\f[R] assuming that \f[I]seed\f[R] is in base 10. It is a fatal error if \f[I]seed\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-e\f[R] \f[I]expr\f[R], \f[B]--expression\f[R]=\f[I]expr\f[R] Evaluates \f[I]expr\f[R]. If multiple expressions are given, they are evaluated in order. If files are given as well (see below), the expressions and files are evaluated in the order given. This means that if a file is given before an expression, the file is read in and evaluated first. .RS .PP If this option is given on the command-line (i.e., not in \f[B]DC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, dc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R], whether on the command-line or in \f[B]DC_ENV_ARGS\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, dc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-f\f[R] \f[I]file\f[R], \f[B]--file\f[R]=\f[I]file\f[R] Reads in \f[I]file\f[R] and evaluates it, line by line, as though it were read through \f[B]stdin\f[R]. If expressions are also given (see above), the expressions are evaluated in the order given. .RS .PP If this option is given on the command-line (i.e., not in \f[B]DC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, dc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, dc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-h\f[R], \f[B]--help\f[R] Prints a usage message and exits. .TP \f[B]-I\f[R] \f[I]ibase\f[R], \f[B]--ibase\f[R]=\f[I]ibase\f[R] Sets the builtin variable \f[B]ibase\f[R] to the value \f[I]ibase\f[R] assuming that \f[I]ibase\f[R] is in base 10. It is a fatal error if \f[I]ibase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-i\f[R], \f[B]--interactive\f[R] Forces interactive mode. (See the \f[B]INTERACTIVE MODE\f[R] section.) .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-L\f[R], \f[B]--no-line-length\f[R] Disables line length checking and prints numbers without backslashes and newlines. In other words, this option sets \f[B]BC_LINE_LENGTH\f[R] to \f[B]0\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-O\f[R] \f[I]obase\f[R], \f[B]--obase\f[R]=\f[I]obase\f[R] Sets the builtin variable \f[B]obase\f[R] to the value \f[I]obase\f[R] assuming that \f[I]obase\f[R] is in base 10. It is a fatal error if \f[I]obase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-P\f[R], \f[B]--no-prompt\f[R] Disables the prompt in TTY mode. (The prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a prompt or are not used to having them in dc(1). Most of those users would want to put this option in \f[B]DC_ENV_ARGS\f[R]. .RS .PP These options override the \f[B]DC_PROMPT\f[R] and \f[B]DC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-R\f[R], \f[B]--no-read-prompt\f[R] Disables the read prompt in TTY mode. (The read prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a read prompt or are not used to having them in dc(1). Most of those users would want to put this option in \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). This option is also useful in hash bang lines of dc(1) scripts that prompt for user input. .RS .PP This option does not disable the regular prompt because the read prompt is only used when the \f[B]?\f[R] command is used. .PP These options \f[I]do\f[R] override the \f[B]DC_PROMPT\f[R] and \f[B]DC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), but only for the read prompt. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-S\f[R] \f[I]scale\f[R], \f[B]--scale\f[R]=\f[I]scale\f[R] Sets the builtin variable \f[B]scale\f[R] to the value \f[I]scale\f[R] assuming that \f[I]scale\f[R] is in base 10. It is a fatal error if \f[I]scale\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-v\f[R], \f[B]-V\f[R], \f[B]--version\f[R] Print the version information (copyright header) and exits. .TP \f[B]-x\f[R] \f[B]--extended-register\f[R] Enables extended register mode. See the \f[I]Extended Register Mode\f[R] subsection of the \f[B]REGISTERS\f[R] section for more information. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-z\f[R], \f[B]--leading-zeroes\f[R] Makes dc(1) print all numbers greater than \f[B]-1\f[R] and less than \f[B]1\f[R], and not equal to \f[B]0\f[R], with a leading zero. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .PP All long options are \f[B]non-portable extensions\f[R]. .SH STDIN -.PP If no files are given on the command-line and no files or expressions are given by the \f[B]-f\f[R], \f[B]--file\f[R], \f[B]-e\f[R], or \f[B]--expression\f[R] options, then dc(1) reads from \f[B]stdin\f[R]. .PP However, there is a caveat to this. .PP First, \f[B]stdin\f[R] is evaluated a line at a time. The only exception to this is if a string has been finished, but not ended. This means that, except for escaped brackets, all brackets must be balanced before dc(1) parses and executes. .SH STDOUT -.PP Any non-error output is written to \f[B]stdout\f[R]. In addition, if history (see the \f[B]HISTORY\f[R] section) and the prompt (see the \f[B]TTY MODE\f[R] section) are enabled, both are output to \f[B]stdout\f[R]. .PP \f[B]Note\f[R]: Unlike other dc(1) implementations, this dc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stdout\f[R], so if \f[B]stdout\f[R] is closed, as in -\f[B]dc >&-\f[R], it will quit with an error. +\f[B]dc >&-\f[R], it will quit with an error. This is done so that dc(1) can report problems when \f[B]stdout\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other dc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stdout\f[R] to \f[B]/dev/null\f[R]. .SH STDERR -.PP Any error output is written to \f[B]stderr\f[R]. .PP \f[B]Note\f[R]: Unlike other dc(1) implementations, this dc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stderr\f[R], so if \f[B]stderr\f[R] is closed, as in -\f[B]dc 2>&-\f[R], it will quit with an error. +\f[B]dc 2>&-\f[R], it will quit with an error. This is done so that dc(1) can exit with an error code when \f[B]stderr\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other dc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stderr\f[R] to \f[B]/dev/null\f[R]. .SH SYNTAX -.PP Each item in the input source code, either a number (see the \f[B]NUMBERS\f[R] section) or a command (see the \f[B]COMMANDS\f[R] section), is processed and executed, in order. Input is processed immediately when entered. .PP \f[B]ibase\f[R] is a register (see the \f[B]REGISTERS\f[R] section) that determines how to interpret constant numbers. It is the \[lq]input\[rq] base, or the number base used for interpreting input numbers. \f[B]ibase\f[R] is initially \f[B]10\f[R]. The max allowable value for \f[B]ibase\f[R] is \f[B]16\f[R]. The min allowable value for \f[B]ibase\f[R] is \f[B]2\f[R]. The max allowable value for \f[B]ibase\f[R] can be queried in dc(1) programs with the \f[B]T\f[R] command. .PP \f[B]obase\f[R] is a register (see the \f[B]REGISTERS\f[R] section) that determines how to output results. It is the \[lq]output\[rq] base, or the number base used for outputting numbers. \f[B]obase\f[R] is initially \f[B]10\f[R]. The max allowable value for \f[B]obase\f[R] is \f[B]DC_BASE_MAX\f[R] and can be queried with the \f[B]U\f[R] command. The min allowable value for \f[B]obase\f[R] is \f[B]0\f[R]. If \f[B]obase\f[R] is \f[B]0\f[R], values are output in scientific notation, and if \f[B]obase\f[R] is \f[B]1\f[R], values are output in engineering notation. Otherwise, values are output in the specified base. .PP Outputting in scientific and engineering notations are \f[B]non-portable extensions\f[R]. .PP The \f[I]scale\f[R] of an expression is the number of digits in the result of the expression right of the decimal point, and \f[B]scale\f[R] is a register (see the \f[B]REGISTERS\f[R] section) that sets the precision of any operations (with exceptions). \f[B]scale\f[R] is initially \f[B]0\f[R]. \f[B]scale\f[R] cannot be negative. The max allowable value for \f[B]scale\f[R] can be queried in dc(1) programs with the \f[B]V\f[R] command. .PP \f[B]seed\f[R] is a register containing the current seed for the pseudo-random number generator. If the current value of \f[B]seed\f[R] is queried and stored, then if it is assigned to \f[B]seed\f[R] later, the pseudo-random number generator is guaranteed to produce the same sequence of pseudo-random numbers that were generated after the value of \f[B]seed\f[R] was first queried. .PP Multiple values assigned to \f[B]seed\f[R] can produce the same sequence of pseudo-random numbers. Likewise, when a value is assigned to \f[B]seed\f[R], it is not guaranteed that querying \f[B]seed\f[R] immediately after will return the same value. In addition, the value of \f[B]seed\f[R] will change after any call to the \f[B]\[cq]\f[R] command or the \f[B]\[lq]\f[R] command that does not get receive a value of \f[B]0\f[R] or \f[B]1\f[R]. The maximum integer returned by the \f[B]\[cq]\f[R] command can be queried with the \f[B]W\f[R] command. .PP \f[B]Note\f[R]: The values returned by the pseudo-random number generator with the \f[B]\[cq]\f[R] and \f[B]\[lq]\f[R] commands are guaranteed to \f[B]NOT\f[R] be cryptographically secure. This is a consequence of using a seeded pseudo-random number generator. However, they \f[I]are\f[R] guaranteed to be reproducible with identical \f[B]seed\f[R] values. This means that the pseudo-random values from dc(1) should only be used where a reproducible stream of pseudo-random numbers is \f[I]ESSENTIAL\f[R]. In any other case, use a non-seeded pseudo-random number generator. .PP The pseudo-random number generator, \f[B]seed\f[R], and all associated operations are \f[B]non-portable extensions\f[R]. .SS Comments -.PP Comments go from \f[B]#\f[R] until, and not including, the next newline. This is a \f[B]non-portable extension\f[R]. .SH NUMBERS -.PP Numbers are strings made up of digits, uppercase letters up to \f[B]F\f[R], and at most \f[B]1\f[R] period for a radix. Numbers can have up to \f[B]DC_NUM_MAX\f[R] digits. Uppercase letters are equal to \f[B]9\f[R] plus their position in the alphabet (i.e., \f[B]A\f[R] equals \f[B]10\f[R], or \f[B]9+1\f[R]). .PP If a digit or letter makes no sense with the current value of \f[B]ibase\f[R] (i.e., they are greater than or equal to the current value of \f[B]ibase\f[R]), then the behavior depends on the existence of the \f[B]-c\f[R]/\f[B]--digit-clamp\f[R] or \f[B]-C\f[R]/\f[B]--no-digit-clamp\f[R] options (see the \f[B]OPTIONS\f[R] section), the existence and setting of the \f[B]DC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or the default, which can be queried with the \f[B]-h\f[R]/\f[B]--help\f[R] option. .PP If clamping is off, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are not changed. Instead, their given value is multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*A+3\[ha]0*B\f[R], which is \f[B]3\f[R] times \f[B]10\f[R] plus \f[B]11\f[R], or \f[B]41\f[R]. .PP If clamping is on, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are set to the value of the highest valid digit in \f[B]ibase\f[R] before being multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*2+3\[ha]0*2\f[R], which is \f[B]3\f[R] times \f[B]2\f[R] plus \f[B]2\f[R], or \f[B]8\f[R]. .PP There is one exception to clamping: single-character numbers (i.e., \f[B]A\f[R] alone). Such numbers are never clamped and always take the value they would have in the highest possible \f[B]ibase\f[R]. This means that \f[B]A\f[R] alone always equals decimal \f[B]10\f[R] and \f[B]Z\f[R] alone always equals decimal \f[B]35\f[R]. This behavior is mandated by the standard for bc(1) (see the STANDARDS section) and is meant to provide an easy way to set the current \f[B]ibase\f[R] (with the \f[B]i\f[R] command) regardless of the current value of \f[B]ibase\f[R]. .PP If clamping is on, and the clamped value of a character is needed, use a leading zero, i.e., for \f[B]A\f[R], use \f[B]0A\f[R]. .PP In addition, dc(1) accepts numbers in scientific notation. These have the form \f[B]e\f[R]. The exponent (the portion after the \f[B]e\f[R]) must be an integer. An example is \f[B]1.89237e9\f[R], which is equal to \f[B]1892370000\f[R]. Negative exponents are also allowed, so \f[B]4.2890e_3\f[R] is equal to \f[B]0.0042890\f[R]. .PP \f[B]WARNING\f[R]: Both the number and the exponent in scientific notation are interpreted according to the current \f[B]ibase\f[R], but the number is still multiplied by \f[B]10\[ha]exponent\f[R] regardless of the current \f[B]ibase\f[R]. For example, if \f[B]ibase\f[R] is \f[B]16\f[R] and dc(1) is given the number string \f[B]FFeA\f[R], the resulting decimal number will be \f[B]2550000000000\f[R], and if dc(1) is given the number string \f[B]10e_4\f[R], the resulting decimal number will be \f[B]0.0016\f[R]. .PP Accepting input as scientific notation is a \f[B]non-portable extension\f[R]. .SH COMMANDS -.PP The valid commands are listed below. .SS Printing -.PP These commands are used for printing. .PP Note that both scientific notation and engineering notation are available for printing numbers. Scientific notation is activated by assigning \f[B]0\f[R] to \f[B]obase\f[R] using \f[B]0o\f[R], and engineering notation is activated by assigning \f[B]1\f[R] to \f[B]obase\f[R] using \f[B]1o\f[R]. To deactivate them, just assign a different value to \f[B]obase\f[R]. .PP Printing numbers in scientific notation and/or engineering notation is a \f[B]non-portable extension\f[R]. .TP \f[B]p\f[R] Prints the value on top of the stack, whether number or string, and prints a newline after. .RS .PP This does not alter the stack. .RE .TP \f[B]n\f[R] Prints the value on top of the stack, whether number or string, and pops it off of the stack. .TP \f[B]P\f[R] Pops a value off the stack. .RS .PP If the value is a number, it is truncated and the absolute value of the result is printed as though \f[B]obase\f[R] is \f[B]256\f[R] and each digit is interpreted as an 8-bit ASCII character, making it a byte stream. .PP If the value is a string, it is printed without a trailing newline. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]f\f[R] Prints the entire contents of the stack, in order from newest to oldest, without altering anything. .RS .PP Users should use this command when they get lost. .RE .SS Arithmetic -.PP These are the commands used for arithmetic. .TP \f[B]+\f[R] The top two values are popped off the stack, added, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to the max \f[I]scale\f[R] of both operands. .TP \f[B]-\f[R] The top two values are popped off the stack, subtracted, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to the max \f[I]scale\f[R] of both operands. .TP \f[B]*\f[R] The top two values are popped off the stack, multiplied, and the result is pushed onto the stack. If \f[B]a\f[R] is the \f[I]scale\f[R] of the first expression and \f[B]b\f[R] is the \f[I]scale\f[R] of the second expression, the \f[I]scale\f[R] of the result is equal to \f[B]min(a+b,max(scale,a,b))\f[R] where \f[B]min()\f[R] and \f[B]max()\f[R] return the obvious values. .TP \f[B]/\f[R] The top two values are popped off the stack, divided, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The first value popped off of the stack must be non-zero. .RE .TP \f[B]%\f[R] The top two values are popped off the stack, remaindered, and the result is pushed onto the stack. .RS .PP Remaindering is equivalent to 1) Computing \f[B]a/b\f[R] to current \f[B]scale\f[R], and 2) Using the result of step 1 to calculate \f[B]a-(a/b)*b\f[R] to \f[I]scale\f[R] \f[B]max(scale+scale(b),scale(a))\f[R]. .PP The first value popped off of the stack must be non-zero. .RE .TP \f[B]\[ti]\f[R] The top two values are popped off the stack, divided and remaindered, and the results (divided first, remainder second) are pushed onto the stack. This is equivalent to \f[B]x y / x y %\f[R] except that \f[B]x\f[R] and \f[B]y\f[R] are only evaluated once. .RS .PP The first value popped off of the stack must be non-zero. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[ha]\f[R] The top two values are popped off the stack, the second is raised to the power of the first, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The first value popped off of the stack must be an integer, and if that value is negative, the second value popped off of the stack must be non-zero. .RE .TP \f[B]v\f[R] The top value is popped off the stack, its square root is computed, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The value popped off of the stack must be non-negative. .RE .TP \f[B]_\f[R] If this command \f[I]immediately\f[R] precedes a number (i.e., no spaces or other commands), then that number is input as a negative number. .RS .PP Otherwise, the top value on the stack is popped and copied, and the copy is negated and pushed onto the stack. This behavior without a number is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]b\f[R] The top value is popped off the stack, and if it is zero, it is pushed back onto the stack. Otherwise, its absolute value is pushed onto the stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]|\f[R] The top three values are popped off the stack, a modular exponentiation is computed, and the result is pushed onto the stack. .RS .PP The first value popped is used as the reduction modulus and must be an integer and non-zero. The second value popped is used as the exponent and must be an integer and non-negative. The third value popped is the base and must be an integer. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]$\f[R] The top value is popped off the stack and copied, and the copy is truncated and pushed onto the stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[at]\f[R] The top two values are popped off the stack, and the precision of the second is set to the value of the first, whether by truncation or extension. .RS .PP The first value popped off of the stack must be an integer and non-negative. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]H\f[R] The top two values are popped off the stack, and the second is shifted left (radix shifted right) to the value of the first. .RS .PP The first value popped off of the stack must be an integer and non-negative. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]h\f[R] The top two values are popped off the stack, and the second is shifted right (radix shifted left) to the value of the first. .RS .PP The first value popped off of the stack must be an integer and non-negative. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]G\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if they are equal, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]N\f[R] The top value is popped off of the stack, and if it a \f[B]0\f[R], a \f[B]1\f[R] is pushed; otherwise, a \f[B]0\f[R] is pushed. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B](\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is less than the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]{\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is less than or equal to the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B])\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is greater than the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]}\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is greater than or equal to the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]M\f[R] The top two values are popped off of the stack. If they are both non-zero, a \f[B]1\f[R] is pushed onto the stack. If either of them is zero, or both of them are, then a \f[B]0\f[R] is pushed onto the stack. .RS .PP This is like the \f[B]&&\f[R] operator in bc(1), and it is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]m\f[R] The top two values are popped off of the stack. If at least one of them is non-zero, a \f[B]1\f[R] is pushed onto the stack. If both of them are zero, then a \f[B]0\f[R] is pushed onto the stack. .RS .PP This is like the \f[B]||\f[R] operator in bc(1), and it is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Pseudo-Random Number Generator -.PP dc(1) has a built-in pseudo-random number generator. These commands query the pseudo-random number generator. (See Parameters for more information about the \f[B]seed\f[R] value that controls the pseudo-random number generator.) .PP The pseudo-random number generator is guaranteed to \f[B]NOT\f[R] be cryptographically secure. .TP \f[B]\[cq]\f[R] Generates an integer between 0 and \f[B]DC_RAND_MAX\f[R], inclusive (see the \f[B]LIMITS\f[R] section). .RS .PP The generated integer is made as unbiased as possible, subject to the limitations of the pseudo-random number generator. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[lq]\f[R] Pops a value off of the stack, which is used as an \f[B]exclusive\f[R] upper bound on the integer that will be generated. If the bound is negative or is a non-integer, an error is raised, and dc(1) resets (see the \f[B]RESET\f[R] section) while \f[B]seed\f[R] remains unchanged. If the bound is larger than \f[B]DC_RAND_MAX\f[R], the higher bound is honored by generating several pseudo-random integers, multiplying them by appropriate powers of \f[B]DC_RAND_MAX+1\f[R], and adding them together. Thus, the size of integer that can be generated with this command is unbounded. Using this command will change the value of \f[B]seed\f[R], unless the operand is \f[B]0\f[R] or \f[B]1\f[R]. In that case, \f[B]0\f[R] is pushed onto the stack, and \f[B]seed\f[R] is \f[I]not\f[R] changed. .RS .PP The generated integer is made as unbiased as possible, subject to the limitations of the pseudo-random number generator. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Stack Control -.PP These commands control the stack. .TP \f[B]c\f[R] Removes all items from (\[lq]clears\[rq]) the stack. .TP \f[B]d\f[R] Copies the item on top of the stack (\[lq]duplicates\[rq]) and pushes the copy onto the stack. .TP \f[B]r\f[R] Swaps (\[lq]reverses\[rq]) the two top items on the stack. .TP \f[B]R\f[R] Pops (\[lq]removes\[rq]) the top value from the stack. .SS Register Control -.PP These commands control registers (see the \f[B]REGISTERS\f[R] section). .TP \f[B]s\f[R]\f[I]r\f[R] Pops the value off the top of the stack and stores it into register \f[I]r\f[R]. .TP \f[B]l\f[R]\f[I]r\f[R] Copies the value in register \f[I]r\f[R] and pushes it onto the stack. This does not alter the contents of \f[I]r\f[R]. .TP \f[B]S\f[R]\f[I]r\f[R] Pops the value off the top of the (main) stack and pushes it onto the stack of register \f[I]r\f[R]. The previous value of the register becomes inaccessible. .TP \f[B]L\f[R]\f[I]r\f[R] Pops the value off the top of the stack for register \f[I]r\f[R] and push it onto the main stack. The previous value in the stack for register \f[I]r\f[R], if any, is now accessible via the \f[B]l\f[R]\f[I]r\f[R] command. .SS Parameters -.PP These commands control the values of \f[B]ibase\f[R], \f[B]obase\f[R], \f[B]scale\f[R], and \f[B]seed\f[R]. Also see the \f[B]SYNTAX\f[R] section. .TP \f[B]i\f[R] Pops the value off of the top of the stack and uses it to set \f[B]ibase\f[R], which must be between \f[B]2\f[R] and \f[B]16\f[R], inclusive. .RS .PP If the value on top of the stack has any \f[I]scale\f[R], the \f[I]scale\f[R] is ignored. .RE .TP \f[B]o\f[R] Pops the value off of the top of the stack and uses it to set \f[B]obase\f[R], which must be between \f[B]0\f[R] and \f[B]DC_BASE_MAX\f[R], inclusive (see the \f[B]LIMITS\f[R] section and the \f[B]NUMBERS\f[R] section). .RS .PP If the value on top of the stack has any \f[I]scale\f[R], the \f[I]scale\f[R] is ignored. .RE .TP \f[B]k\f[R] Pops the value off of the top of the stack and uses it to set \f[B]scale\f[R], which must be non-negative. .RS .PP If the value on top of the stack has any \f[I]scale\f[R], the \f[I]scale\f[R] is ignored. .RE .TP \f[B]j\f[R] Pops the value off of the top of the stack and uses it to set \f[B]seed\f[R]. The meaning of \f[B]seed\f[R] is dependent on the current pseudo-random number generator but is guaranteed to not change except for new major versions. .RS .PP The \f[I]scale\f[R] and sign of the value may be significant. .PP If a previously used \f[B]seed\f[R] value is used again, the pseudo-random number generator is guaranteed to produce the same sequence of pseudo-random numbers as it did when the \f[B]seed\f[R] value was previously used. .PP The exact value assigned to \f[B]seed\f[R] is not guaranteed to be returned if the \f[B]J\f[R] command is used. However, if \f[B]seed\f[R] \f[I]does\f[R] return a different value, both values, when assigned to \f[B]seed\f[R], are guaranteed to produce the same sequence of pseudo-random numbers. This means that certain values assigned to \f[B]seed\f[R] will not produce unique sequences of pseudo-random numbers. .PP There is no limit to the length (number of significant decimal digits) or \f[I]scale\f[R] of the value that can be assigned to \f[B]seed\f[R]. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]I\f[R] Pushes the current value of \f[B]ibase\f[R] onto the main stack. .TP \f[B]O\f[R] Pushes the current value of \f[B]obase\f[R] onto the main stack. .TP \f[B]K\f[R] Pushes the current value of \f[B]scale\f[R] onto the main stack. .TP \f[B]J\f[R] Pushes the current value of \f[B]seed\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]T\f[R] Pushes the maximum allowable value of \f[B]ibase\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]U\f[R] Pushes the maximum allowable value of \f[B]obase\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]V\f[R] Pushes the maximum allowable value of \f[B]scale\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]W\f[R] Pushes the maximum (inclusive) integer that can be generated with the \f[B]\[cq]\f[R] pseudo-random number generator command. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Strings -.PP The following commands control strings. .PP dc(1) can work with both numbers and strings, and registers (see the \f[B]REGISTERS\f[R] section) can hold both strings and numbers. dc(1) always knows whether the contents of a register are a string or a number. .PP While arithmetic operations have to have numbers, and will print an error if given a string, other commands accept strings. .PP Strings can also be executed as macros. For example, if the string \f[B][1pR]\f[R] is executed as a macro, then the code \f[B]1pR\f[R] is executed, meaning that the \f[B]1\f[R] will be printed with a newline after and then popped from the stack. .TP \f[B][\f[R]\f[I]characters\f[R]\f[B]]\f[R] Makes a string containing \f[I]characters\f[R] and pushes it onto the stack. .RS .PP If there are brackets (\f[B][\f[R] and \f[B]]\f[R]) in the string, then they must be balanced. Unbalanced brackets can be escaped using a backslash (\f[B]\[rs]\f[R]) character. .PP If there is a backslash character in the string, the character after it (even another backslash) is put into the string verbatim, but the (first) backslash is not. .RE .TP \f[B]a\f[R] The value on top of the stack is popped. .RS .PP If it is a number, it is truncated and its absolute value is taken. The result mod \f[B]256\f[R] is calculated. If that result is \f[B]0\f[R], push an empty string; otherwise, push a one-character string where the character is the result of the mod interpreted as an ASCII character. .PP If it is a string, then a new string is made. If the original string is empty, the new string is empty. If it is not, then the first character of the original string is used to create the new string as a one-character string. The new string is then pushed onto the stack. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]x\f[R] Pops a value off of the top of the stack. .RS .PP If it is a number, it is pushed back onto the stack. .PP If it is a string, it is executed as a macro. .PP This behavior is the norm whenever a macro is executed, whether by this command or by the conditional execution commands below. .RE .TP \f[B]>\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is greater than the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP For example, \f[B]0 1>a\f[R] will execute the contents of register \f[B]a\f[R], and \f[B]1 0>a\f[R] will not. .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]>\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]!>\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is not greater than the second (less than or equal to), then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]!>\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]<\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is less than the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]<\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]!<\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is not less than the second (greater than or equal to), then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]!<\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]=\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is equal to the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]=\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]!=\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is not equal to the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]!=\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]?\f[R] Reads a line from the \f[B]stdin\f[R] and executes it. This is to allow macros to request input from users. .TP \f[B]q\f[R] During execution of a macro, this exits the execution of that macro and the execution of the macro that executed it. If there are no macros, or only one macro executing, dc(1) exits. .TP \f[B]Q\f[R] Pops a value from the stack which must be non-negative and is used the number of macro executions to pop off of the execution stack. If the number of levels to pop is greater than the number of executing macros, dc(1) exits. .TP \f[B],\f[R] Pushes the depth of the execution stack onto the stack. The execution stack is the stack of string executions. The number that is pushed onto the stack is exactly as many as is needed to make dc(1) exit with the \f[B]Q\f[R] command, so the sequence \f[B],Q\f[R] will make dc(1) exit. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Status -.PP These commands query status of the stack or its top value. .TP \f[B]Z\f[R] Pops a value off of the stack. .RS .PP If it is a number, calculates the number of significant decimal digits it has and pushes the result. It will push \f[B]1\f[R] if the argument is \f[B]0\f[R] with no decimal places. .PP If it is a string, pushes the number of characters the string has. .RE .TP \f[B]X\f[R] Pops a value off of the stack. .RS .PP If it is a number, pushes the \f[I]scale\f[R] of the value onto the stack. .PP If it is a string, pushes \f[B]0\f[R]. .RE .TP \f[B]u\f[R] Pops one value off of the stack. If the value is a number, this pushes \f[B]1\f[R] onto the stack. Otherwise (if it is a string), it pushes \f[B]0\f[R]. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]t\f[R] Pops one value off of the stack. If the value is a string, this pushes \f[B]1\f[R] onto the stack. Otherwise (if it is a number), it pushes \f[B]0\f[R]. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]z\f[R] Pushes the current depth of the stack (before execution of this command) onto the stack. .TP \f[B]y\f[R]\f[I]r\f[R] Pushes the current stack depth of the register \f[I]r\f[R] onto the main stack. .RS .PP Because each register has a depth of \f[B]1\f[R] (with the value \f[B]0\f[R] in the top item) when dc(1) starts, dc(1) requires that each register\[cq]s stack must always have at least one item; dc(1) will give an error and reset otherwise (see the \f[B]RESET\f[R] section). This means that this command will never push \f[B]0\f[R]. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Arrays -.PP These commands manipulate arrays. .TP \f[B]:\f[R]\f[I]r\f[R] Pops the top two values off of the stack. The second value will be stored in the array \f[I]r\f[R] (see the \f[B]REGISTERS\f[R] section), indexed by the first value. .TP \f[B];\f[R]\f[I]r\f[R] Pops the value on top of the stack and uses it as an index into the array \f[I]r\f[R]. The selected value is then pushed onto the stack. .TP \f[B]Y\f[R]\f[I]r\f[R] Pushes the length of the array \f[I]r\f[R] onto the stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Global Settings -.PP These commands retrieve global settings. These are the only commands that require multiple specific characters, and all of them begin with the letter \f[B]g\f[R]. Only the characters below are allowed after the character \f[B]g\f[R]; any other character produces a parse error (see the \f[B]ERRORS\f[R] section). .TP \f[B]gl\f[R] Pushes the line length set by \f[B]DC_LINE_LENGTH\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) onto the stack. .TP \f[B]gx\f[R] Pushes \f[B]1\f[R] onto the stack if extended register mode is on, \f[B]0\f[R] otherwise. See the \f[I]Extended Register Mode\f[R] subsection of the \f[B]REGISTERS\f[R] section for more information. .TP \f[B]gz\f[R] Pushes \f[B]0\f[R] onto the stack if the leading zero setting has not been enabled with the \f[B]-z\f[R] or \f[B]--leading-zeroes\f[R] options (see the \f[B]OPTIONS\f[R] section), non-zero otherwise. .SH REGISTERS -.PP Registers are names that can store strings, numbers, and arrays. (Number/string registers do not interfere with array registers.) .PP Each register is also its own stack, so the current register value is the top of the stack for the register. All registers, when first referenced, have one value (\f[B]0\f[R]) in their stack, and it is a runtime error to attempt to pop that item off of the register stack. .PP In non-extended register mode, a register name is just the single character that follows any command that needs a register name. The only exceptions are: a newline (\f[B]`\[rs]n'\f[R]) and a left bracket (\f[B]`['\f[R]); it is a parse error for a newline or a left bracket to be used as a register name. .SS Extended Register Mode -.PP Unlike most other dc(1) implentations, this dc(1) provides nearly unlimited amounts of registers, if extended register mode is enabled. .PP If extended register mode is enabled (\f[B]-x\f[R] or \f[B]--extended-register\f[R] command-line arguments are given), then normal single character registers are used \f[I]unless\f[R] the character immediately following a command that needs a register name is a space (according to \f[B]isspace()\f[R]) and not a newline (\f[B]`\[rs]n'\f[R]). .PP In that case, the register name is found according to the regex \f[B][a-z][a-z0-9_]*\f[R] (like bc(1) identifiers), and it is a parse error if the next non-space characters do not match that regex. .SH RESET -.PP When dc(1) encounters an error or a signal that it has a non-default handler for, it resets. This means that several things happen. .PP First, any macros that are executing are stopped and popped off the stack. The behavior is not unlike that of exceptions in programming languages. Then the execution point is set so that any code waiting to execute (after all macros returned) is skipped. .PP Thus, when dc(1) resets, it skips any remaining code waiting to be executed. Then, if it is interactive mode, and the error was not a fatal error (see the \f[B]EXIT STATUS\f[R] section), it asks for more input; otherwise, it exits with the appropriate return code. .SH PERFORMANCE -.PP Most dc(1) implementations use \f[B]char\f[R] types to calculate the value of \f[B]1\f[R] decimal digit at a time, but that can be slow. This dc(1) does something different. .PP It uses large integers to calculate more than \f[B]1\f[R] decimal digit at a time. If built in a environment where \f[B]DC_LONG_BIT\f[R] (see the \f[B]LIMITS\f[R] section) is \f[B]64\f[R], then each integer has \f[B]9\f[R] decimal digits. If built in an environment where \f[B]DC_LONG_BIT\f[R] is \f[B]32\f[R] then each integer has \f[B]4\f[R] decimal digits. This value (the number of decimal digits per large integer) is called \f[B]DC_BASE_DIGS\f[R]. .PP In addition, this dc(1) uses an even larger integer for overflow checking. This integer type depends on the value of \f[B]DC_LONG_BIT\f[R], but is always at least twice as large as the integer type used to store digits. .SH LIMITS -.PP The following are the limits on dc(1): .TP \f[B]DC_LONG_BIT\f[R] The number of bits in the \f[B]long\f[R] type in the environment where dc(1) was built. This determines how many decimal digits can be stored in a single large integer (see the \f[B]PERFORMANCE\f[R] section). .TP \f[B]DC_BASE_DIGS\f[R] The number of decimal digits per large integer (see the \f[B]PERFORMANCE\f[R] section). Depends on \f[B]DC_LONG_BIT\f[R]. .TP \f[B]DC_BASE_POW\f[R] The max decimal number that each large integer can store (see \f[B]DC_BASE_DIGS\f[R]) plus \f[B]1\f[R]. Depends on \f[B]DC_BASE_DIGS\f[R]. .TP \f[B]DC_OVERFLOW_MAX\f[R] The max number that the overflow type (see the \f[B]PERFORMANCE\f[R] section) can hold. Depends on \f[B]DC_LONG_BIT\f[R]. .TP \f[B]DC_BASE_MAX\f[R] The maximum output base. Set at \f[B]DC_BASE_POW\f[R]. .TP \f[B]DC_DIM_MAX\f[R] The maximum size of arrays. Set at \f[B]SIZE_MAX-1\f[R]. .TP \f[B]DC_SCALE_MAX\f[R] The maximum \f[B]scale\f[R]. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_STRING_MAX\f[R] The maximum length of strings. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_NAME_MAX\f[R] The maximum length of identifiers. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_NUM_MAX\f[R] The maximum length of a number (in decimal digits), which includes digits after the decimal point. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_RAND_MAX\f[R] The maximum integer (inclusive) returned by the \f[B]\[cq]\f[R] command, if dc(1). Set at \f[B]2\[ha]DC_LONG_BIT-1\f[R]. .TP Exponent The maximum allowable exponent (positive or negative). Set at \f[B]DC_OVERFLOW_MAX\f[R]. .TP Number of vars The maximum number of vars/arrays. Set at \f[B]SIZE_MAX-1\f[R]. .PP These limits are meant to be effectively non-existent; the limits are so large (at least on 64-bit machines) that there should not be any point at which they become a problem. In fact, memory should be exhausted before these limits should be hit. .SH ENVIRONMENT VARIABLES -.PP As \f[B]non-portable extensions\f[R], dc(1) recognizes the following environment variables: .TP \f[B]DC_ENV_ARGS\f[R] This is another way to give command-line arguments to dc(1). They should be in the same format as all other command-line arguments. These are always processed first, so any files given in \f[B]DC_ENV_ARGS\f[R] will be processed before arguments and files given on the command-line. This gives the user the ability to set up \[lq]standard\[rq] options and files to be used at every invocation. The most useful thing for such files to contain would be useful functions that the user might want every time dc(1) runs. Another use would be to use the \f[B]-e\f[R] option to set \f[B]scale\f[R] to a value other than \f[B]0\f[R]. .RS .PP The code that parses \f[B]DC_ENV_ARGS\f[R] will correctly handle quoted arguments, but it does not understand escape sequences. For example, the string \f[B]\[lq]/home/gavin/some dc file.dc\[rq]\f[R] will be correctly parsed, but the string \f[B]\[lq]/home/gavin/some \[dq]dc\[dq] file.dc\[rq]\f[R] will include the backslashes. .PP The quote parsing will handle either kind of quotes, \f[B]\[cq]\f[R] or \f[B]\[lq]\f[R]. Thus, if you have a file with any number of single quotes in the name, you can use double quotes as the outside quotes, as in \f[B]\[lq]some `dc' file.dc\[rq]\f[R], and vice versa if you have a file with double quotes. However, handling a file with both kinds of quotes in \f[B]DC_ENV_ARGS\f[R] is not supported due to the complexity of the parsing, though such files are still supported on the command-line where the parsing is done by the shell. .RE .TP \f[B]DC_LINE_LENGTH\f[R] If this environment variable exists and contains an integer that is greater than \f[B]1\f[R] and is less than \f[B]UINT16_MAX\f[R] (\f[B]2\[ha]16-1\f[R]), dc(1) will output lines to that length, including the backslash newline combo. The default line length is \f[B]70\f[R]. .RS .PP The special value of \f[B]0\f[R] will disable line length checking and print numbers without regard to line length and without backslashes and newlines. .RE .TP \f[B]DC_SIGINT_RESET\f[R] If dc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), then this environment variable has no effect because dc(1) exits on \f[B]SIGINT\f[R] when not in interactive mode. .RS .PP However, when dc(1) is in interactive mode, then if this environment variable exists and contains an integer, a non-zero value makes dc(1) reset on \f[B]SIGINT\f[R], rather than exit, and zero makes dc(1) exit. If this environment variable exists and is \f[I]not\f[R] an integer, then dc(1) will exit on \f[B]SIGINT\f[R]. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_TTY_MODE\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, then a non-zero value makes dc(1) use TTY mode, and zero makes dc(1) not use TTY mode. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_PROMPT\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, a non-zero value makes dc(1) use a prompt, and zero or a non-integer makes dc(1) not use a prompt. If this environment variable does not exist and \f[B]DC_TTY_MODE\f[R] does, then the value of the \f[B]DC_TTY_MODE\f[R] environment variable is used. .PP This environment variable and the \f[B]DC_TTY_MODE\f[R] environment variable override the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_EXPR_EXIT\f[R] If any expressions or expression files are given on the command-line with \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R], then if this environment variable exists and contains an integer, a non-zero value makes dc(1) exit after executing the expressions and expression files, and a zero value makes dc(1) not exit. .RS .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_DIGIT_CLAMP\f[R] When parsing numbers and if this environment variable exists and contains an integer, a non-zero value makes dc(1) clamp digits that are greater than or equal to the current \f[B]ibase\f[R] so that all such digits are considered equal to the \f[B]ibase\f[R] minus 1, and a zero value disables such clamping so that those digits are always equal to their value, which is multiplied by the power of the \f[B]ibase\f[R]. .RS .PP This never applies to single-digit numbers, as per the bc(1) standard (see the \f[B]STANDARDS\f[R] section). .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .SH EXIT STATUS -.PP dc(1) returns the following exit statuses: .TP \f[B]0\f[R] No error. .TP \f[B]1\f[R] A math error occurred. This follows standard practice of using \f[B]1\f[R] for expected errors, since math errors will happen in the process of normal execution. .RS .PP Math errors include divide by \f[B]0\f[R], taking the square root of a negative number, using a negative number as a bound for the pseudo-random number generator, attempting to convert a negative number to a hardware integer, overflow when converting a number to a hardware integer, overflow when calculating the size of a number, and attempting to use a non-integer where an integer is required. .PP Converting to a hardware integer happens for the second operand of the power (\f[B]\[ha]\f[R]), places (\f[B]\[at]\f[R]), left shift (\f[B]H\f[R]), and right shift (\f[B]h\f[R]) operators. .RE .TP \f[B]2\f[R] A parse error occurred. .RS .PP Parse errors include unexpected \f[B]EOF\f[R], using an invalid character, failing to find the end of a string or comment, and using a token where it is invalid. .RE .TP \f[B]3\f[R] A runtime error occurred. .RS .PP Runtime errors include assigning an invalid number to any global (\f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R]), giving a bad expression to a \f[B]read()\f[R] call, calling \f[B]read()\f[R] inside of a \f[B]read()\f[R] call, type errors (including attempting to execute a number), and attempting an operation when the stack has too few elements. .RE .TP \f[B]4\f[R] A fatal error occurred. .RS .PP Fatal errors include memory allocation errors, I/O errors, failing to open files, attempting to use files that do not have only ASCII characters (dc(1) only accepts ASCII characters), attempting to open a directory as a file, and giving invalid command-line options. .RE .PP The exit status \f[B]4\f[R] is special; when a fatal error occurs, dc(1) always exits and returns \f[B]4\f[R], no matter what mode dc(1) is in. .PP The other statuses will only be returned when dc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), since dc(1) resets its state (see the \f[B]RESET\f[R] section) and accepts more input when one of those errors occurs in interactive mode. This is also the case when interactive mode is forced by the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .PP These exit statuses allow dc(1) to be used in shell scripting with error checking, and its normal behavior can be forced by using the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .SH INTERACTIVE MODE -.PP Like bc(1), dc(1) has an interactive mode and a non-interactive mode. Interactive mode is turned on automatically when both \f[B]stdin\f[R] and \f[B]stdout\f[R] are hooked to a terminal, but the \f[B]-i\f[R] flag and \f[B]--interactive\f[R] option can turn it on in other situations. .PP In interactive mode, dc(1) attempts to recover from errors (see the \f[B]RESET\f[R] section), and in normal execution, flushes \f[B]stdout\f[R] as soon as execution is done for the current input. dc(1) may also reset on \f[B]SIGINT\f[R] instead of exit, depending on the contents of, or default for, the \f[B]DC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .SH TTY MODE -.PP If \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY, then \[lq]TTY mode\[rq] is considered to be available, and thus, dc(1) can turn on TTY mode, subject to some settings. .PP If there is the environment variable \f[B]DC_TTY_MODE\f[R] in the environment (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then if that environment variable contains a non-zero integer, dc(1) will turn on TTY mode when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY. If the \f[B]DC_TTY_MODE\f[R] environment variable exists but is \f[I]not\f[R] a non-zero integer, then dc(1) will not turn TTY mode on. .PP If the environment variable \f[B]DC_TTY_MODE\f[R] does \f[I]not\f[R] exist, the default setting is used. The default setting can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP TTY mode is different from interactive mode because interactive mode is required in the bc(1) specification (see the \f[B]STANDARDS\f[R] section), and interactive mode requires only \f[B]stdin\f[R] and \f[B]stdout\f[R] to be connected to a terminal. .SS Prompt -.PP If TTY mode is available, then a prompt can be enabled. Like TTY mode itself, it can be turned on or off with an environment variable: \f[B]DC_PROMPT\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP If the environment variable \f[B]DC_PROMPT\f[R] exists and is a non-zero integer, then the prompt is turned on when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options were not used. The read prompt will be turned on under the same conditions, except that the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options must also not be used. .PP However, if \f[B]DC_PROMPT\f[R] does not exist, the prompt can be enabled or disabled with the \f[B]DC_TTY_MODE\f[R] environment variable, the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options, and the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options. See the \f[B]ENVIRONMENT VARIABLES\f[R] and \f[B]OPTIONS\f[R] sections for more details. .SH SIGNAL HANDLING -.PP Sending a \f[B]SIGINT\f[R] will cause dc(1) to do one of two things. .PP If dc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), or the \f[B]DC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or its default, is either not an integer or it is zero, dc(1) will exit. .PP However, if dc(1) is in interactive mode, and the \f[B]DC_SIGINT_RESET\f[R] or its default is an integer and non-zero, then dc(1) will stop executing the current input and reset (see the \f[B]RESET\f[R] section) upon receiving a \f[B]SIGINT\f[R]. .PP Note that \[lq]current input\[rq] can mean one of two things. If dc(1) is processing input from \f[B]stdin\f[R] in interactive mode, it will ask for more input. If dc(1) is processing input from a file in interactive mode, it will stop processing the file and start processing the next file, if one exists, or ask for input from \f[B]stdin\f[R] if no other file exists. .PP This means that if a \f[B]SIGINT\f[R] is sent to dc(1) as it is executing a file, it can seem as though dc(1) did not respond to the signal since it will immediately start executing the next file. This is by design; most files that users execute when interacting with dc(1) have function definitions, which are quick to parse. If a file takes a long time to execute, there may be a bug in that file. The rest of the files could still be executed without problem, allowing the user to continue. .PP \f[B]SIGTERM\f[R] and \f[B]SIGQUIT\f[R] cause dc(1) to clean up and exit, and it uses the default handler for all other signals. .SH SEE ALSO -.PP bc(1) .SH STANDARDS -.PP The dc(1) utility operators and some behavior are compliant with the operators in the IEEE Std 1003.1-2017 (\[lq]POSIX.1-2017\[rq]) bc(1) specification at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html . .SH BUGS -.PP None are known. Report bugs at https://git.gavinhoward.com/gavin/bc . .SH AUTHOR -.PP -Gavin D. -Howard and contributors. +Gavin D. Howard \c +.MT gavin@gavinhoward.com +.ME \c +\ and contributors. diff --git a/contrib/bc/manuals/dc/N.1 b/contrib/bc/manuals/dc/N.1 index b7b501fbed13..5bb0dff18e0a 100644 --- a/contrib/bc/manuals/dc/N.1 +++ b/contrib/bc/manuals/dc/N.1 @@ -1,1723 +1,1687 @@ .\" .\" SPDX-License-Identifier: BSD-2-Clause .\" .\" Copyright (c) 2018-2023 Gavin D. Howard and contributors. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions are met: .\" .\" * Redistributions of source code must retain the above copyright notice, .\" this list of conditions and the following disclaimer. .\" .\" * Redistributions in binary form must reproduce the above copyright notice, .\" this list of conditions and the following disclaimer in the documentation .\" and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" .\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" .TH "DC" "1" "February 2023" "Gavin D. Howard" "General Commands Manual" .nh .ad l .SH Name -.PP dc - arbitrary-precision decimal reverse-Polish notation calculator .SH SYNOPSIS -.PP \f[B]dc\f[R] [\f[B]-cChiPRvVx\f[R]] [\f[B]--version\f[R]] [\f[B]--help\f[R]] [\f[B]--digit-clamp\f[R]] [\f[B]--no-digit-clamp\f[R]] [\f[B]--interactive\f[R]] [\f[B]--no-prompt\f[R]] [\f[B]--no-read-prompt\f[R]] [\f[B]--extended-register\f[R]] [\f[B]-e\f[R] \f[I]expr\f[R]] [\f[B]--expression\f[R]=\f[I]expr\f[R]\&...] [\f[B]-f\f[R] \f[I]file\f[R]\&...] [\f[B]--file\f[R]=\f[I]file\f[R]\&...] [\f[I]file\f[R]\&...] [\f[B]-I\f[R] \f[I]ibase\f[R]] [\f[B]--ibase\f[R]=\f[I]ibase\f[R]] [\f[B]-O\f[R] \f[I]obase\f[R]] [\f[B]--obase\f[R]=\f[I]obase\f[R]] [\f[B]-S\f[R] \f[I]scale\f[R]] [\f[B]--scale\f[R]=\f[I]scale\f[R]] [\f[B]-E\f[R] \f[I]seed\f[R]] [\f[B]--seed\f[R]=\f[I]seed\f[R]] .SH DESCRIPTION -.PP dc(1) is an arbitrary-precision calculator. It uses a stack (reverse Polish notation) to store numbers and results of computations. Arithmetic operations pop arguments off of the stack and push the results. .PP If no files are given on the command-line, then dc(1) reads from \f[B]stdin\f[R] (see the \f[B]STDIN\f[R] section). Otherwise, those files are processed, and dc(1) will then exit. .PP If a user wants to set up a standard environment, they can use \f[B]DC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). For example, if a user wants the \f[B]scale\f[R] always set to \f[B]10\f[R], they can set \f[B]DC_ENV_ARGS\f[R] to \f[B]-e 10k\f[R], and this dc(1) will always start with a \f[B]scale\f[R] of \f[B]10\f[R]. .SH OPTIONS -.PP The following are the options that dc(1) accepts. .TP \f[B]-C\f[R], \f[B]--no-digit-clamp\f[R] Disables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that the value added to a number from a digit is always that digit\[cq]s value multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-c\f[R] or \f[B]--digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]DC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-c\f[R], \f[B]--digit-clamp\f[R] Enables clamping of digits greater than or equal to the current \f[B]ibase\f[R] when parsing numbers. .RS .PP This means that digits that the value added to a number from a digit that is greater than or equal to the ibase is the value of ibase minus 1 all multiplied by the value of ibase raised to the power of the digit\[cq]s position, which starts from 0 at the least significant digit. .PP If this and/or the \f[B]-C\f[R] or \f[B]--no-digit-clamp\f[R] options are given multiple times, the last one given is used. .PP This option overrides the \f[B]DC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-E\f[R] \f[I]seed\f[R], \f[B]--seed\f[R]=\f[I]seed\f[R] Sets the builtin variable \f[B]seed\f[R] to the value \f[I]seed\f[R] assuming that \f[I]seed\f[R] is in base 10. It is a fatal error if \f[I]seed\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-e\f[R] \f[I]expr\f[R], \f[B]--expression\f[R]=\f[I]expr\f[R] Evaluates \f[I]expr\f[R]. If multiple expressions are given, they are evaluated in order. If files are given as well (see below), the expressions and files are evaluated in the order given. This means that if a file is given before an expression, the file is read in and evaluated first. .RS .PP If this option is given on the command-line (i.e., not in \f[B]DC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, dc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R], whether on the command-line or in \f[B]DC_ENV_ARGS\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, dc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-f\f[R] \f[I]file\f[R], \f[B]--file\f[R]=\f[I]file\f[R] Reads in \f[I]file\f[R] and evaluates it, line by line, as though it were read through \f[B]stdin\f[R]. If expressions are also given (see above), the expressions are evaluated in the order given. .RS .PP If this option is given on the command-line (i.e., not in \f[B]DC_ENV_ARGS\f[R], see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then after processing all expressions and files, dc(1) will exit, unless \f[B]-\f[R] (\f[B]stdin\f[R]) was given as an argument at least once to \f[B]-f\f[R] or \f[B]--file\f[R]. However, if any other \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R] arguments are given after \f[B]-f-\f[R] or equivalent is given, dc(1) will give a fatal error and exit. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-h\f[R], \f[B]--help\f[R] Prints a usage message and exits. .TP \f[B]-I\f[R] \f[I]ibase\f[R], \f[B]--ibase\f[R]=\f[I]ibase\f[R] Sets the builtin variable \f[B]ibase\f[R] to the value \f[I]ibase\f[R] assuming that \f[I]ibase\f[R] is in base 10. It is a fatal error if \f[I]ibase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-i\f[R], \f[B]--interactive\f[R] Forces interactive mode. (See the \f[B]INTERACTIVE MODE\f[R] section.) .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-L\f[R], \f[B]--no-line-length\f[R] Disables line length checking and prints numbers without backslashes and newlines. In other words, this option sets \f[B]BC_LINE_LENGTH\f[R] to \f[B]0\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-O\f[R] \f[I]obase\f[R], \f[B]--obase\f[R]=\f[I]obase\f[R] Sets the builtin variable \f[B]obase\f[R] to the value \f[I]obase\f[R] assuming that \f[I]obase\f[R] is in base 10. It is a fatal error if \f[I]obase\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-P\f[R], \f[B]--no-prompt\f[R] Disables the prompt in TTY mode. (The prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a prompt or are not used to having them in dc(1). Most of those users would want to put this option in \f[B]DC_ENV_ARGS\f[R]. .RS .PP These options override the \f[B]DC_PROMPT\f[R] and \f[B]DC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-R\f[R], \f[B]--no-read-prompt\f[R] Disables the read prompt in TTY mode. (The read prompt is only enabled in TTY mode. See the \f[B]TTY MODE\f[R] section.) This is mostly for those users that do not want a read prompt or are not used to having them in dc(1). Most of those users would want to put this option in \f[B]BC_ENV_ARGS\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). This option is also useful in hash bang lines of dc(1) scripts that prompt for user input. .RS .PP This option does not disable the regular prompt because the read prompt is only used when the \f[B]?\f[R] command is used. .PP These options \f[I]do\f[R] override the \f[B]DC_PROMPT\f[R] and \f[B]DC_TTY_MODE\f[R] environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), but only for the read prompt. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-S\f[R] \f[I]scale\f[R], \f[B]--scale\f[R]=\f[I]scale\f[R] Sets the builtin variable \f[B]scale\f[R] to the value \f[I]scale\f[R] assuming that \f[I]scale\f[R] is in base 10. It is a fatal error if \f[I]scale\f[R] is not a valid number. .RS .PP If multiple instances of this option are given, the last is used. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-v\f[R], \f[B]-V\f[R], \f[B]--version\f[R] Print the version information (copyright header) and exits. .TP \f[B]-x\f[R] \f[B]--extended-register\f[R] Enables extended register mode. See the \f[I]Extended Register Mode\f[R] subsection of the \f[B]REGISTERS\f[R] section for more information. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]-z\f[R], \f[B]--leading-zeroes\f[R] Makes dc(1) print all numbers greater than \f[B]-1\f[R] and less than \f[B]1\f[R], and not equal to \f[B]0\f[R], with a leading zero. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .PP All long options are \f[B]non-portable extensions\f[R]. .SH STDIN -.PP If no files are given on the command-line and no files or expressions are given by the \f[B]-f\f[R], \f[B]--file\f[R], \f[B]-e\f[R], or \f[B]--expression\f[R] options, then dc(1) reads from \f[B]stdin\f[R]. .PP However, there is a caveat to this. .PP First, \f[B]stdin\f[R] is evaluated a line at a time. The only exception to this is if a string has been finished, but not ended. This means that, except for escaped brackets, all brackets must be balanced before dc(1) parses and executes. .SH STDOUT -.PP Any non-error output is written to \f[B]stdout\f[R]. In addition, if history (see the \f[B]HISTORY\f[R] section) and the prompt (see the \f[B]TTY MODE\f[R] section) are enabled, both are output to \f[B]stdout\f[R]. .PP \f[B]Note\f[R]: Unlike other dc(1) implementations, this dc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stdout\f[R], so if \f[B]stdout\f[R] is closed, as in -\f[B]dc >&-\f[R], it will quit with an error. +\f[B]dc >&-\f[R], it will quit with an error. This is done so that dc(1) can report problems when \f[B]stdout\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other dc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stdout\f[R] to \f[B]/dev/null\f[R]. .SH STDERR -.PP Any error output is written to \f[B]stderr\f[R]. .PP \f[B]Note\f[R]: Unlike other dc(1) implementations, this dc(1) will issue a fatal error (see the \f[B]EXIT STATUS\f[R] section) if it cannot write to \f[B]stderr\f[R], so if \f[B]stderr\f[R] is closed, as in -\f[B]dc 2>&-\f[R], it will quit with an error. +\f[B]dc 2>&-\f[R], it will quit with an error. This is done so that dc(1) can exit with an error code when \f[B]stderr\f[R] is redirected to a file. .PP If there are scripts that depend on the behavior of other dc(1) implementations, it is recommended that those scripts be changed to redirect \f[B]stderr\f[R] to \f[B]/dev/null\f[R]. .SH SYNTAX -.PP Each item in the input source code, either a number (see the \f[B]NUMBERS\f[R] section) or a command (see the \f[B]COMMANDS\f[R] section), is processed and executed, in order. Input is processed immediately when entered. .PP \f[B]ibase\f[R] is a register (see the \f[B]REGISTERS\f[R] section) that determines how to interpret constant numbers. It is the \[lq]input\[rq] base, or the number base used for interpreting input numbers. \f[B]ibase\f[R] is initially \f[B]10\f[R]. The max allowable value for \f[B]ibase\f[R] is \f[B]16\f[R]. The min allowable value for \f[B]ibase\f[R] is \f[B]2\f[R]. The max allowable value for \f[B]ibase\f[R] can be queried in dc(1) programs with the \f[B]T\f[R] command. .PP \f[B]obase\f[R] is a register (see the \f[B]REGISTERS\f[R] section) that determines how to output results. It is the \[lq]output\[rq] base, or the number base used for outputting numbers. \f[B]obase\f[R] is initially \f[B]10\f[R]. The max allowable value for \f[B]obase\f[R] is \f[B]DC_BASE_MAX\f[R] and can be queried with the \f[B]U\f[R] command. The min allowable value for \f[B]obase\f[R] is \f[B]0\f[R]. If \f[B]obase\f[R] is \f[B]0\f[R], values are output in scientific notation, and if \f[B]obase\f[R] is \f[B]1\f[R], values are output in engineering notation. Otherwise, values are output in the specified base. .PP Outputting in scientific and engineering notations are \f[B]non-portable extensions\f[R]. .PP The \f[I]scale\f[R] of an expression is the number of digits in the result of the expression right of the decimal point, and \f[B]scale\f[R] is a register (see the \f[B]REGISTERS\f[R] section) that sets the precision of any operations (with exceptions). \f[B]scale\f[R] is initially \f[B]0\f[R]. \f[B]scale\f[R] cannot be negative. The max allowable value for \f[B]scale\f[R] can be queried in dc(1) programs with the \f[B]V\f[R] command. .PP \f[B]seed\f[R] is a register containing the current seed for the pseudo-random number generator. If the current value of \f[B]seed\f[R] is queried and stored, then if it is assigned to \f[B]seed\f[R] later, the pseudo-random number generator is guaranteed to produce the same sequence of pseudo-random numbers that were generated after the value of \f[B]seed\f[R] was first queried. .PP Multiple values assigned to \f[B]seed\f[R] can produce the same sequence of pseudo-random numbers. Likewise, when a value is assigned to \f[B]seed\f[R], it is not guaranteed that querying \f[B]seed\f[R] immediately after will return the same value. In addition, the value of \f[B]seed\f[R] will change after any call to the \f[B]\[cq]\f[R] command or the \f[B]\[lq]\f[R] command that does not get receive a value of \f[B]0\f[R] or \f[B]1\f[R]. The maximum integer returned by the \f[B]\[cq]\f[R] command can be queried with the \f[B]W\f[R] command. .PP \f[B]Note\f[R]: The values returned by the pseudo-random number generator with the \f[B]\[cq]\f[R] and \f[B]\[lq]\f[R] commands are guaranteed to \f[B]NOT\f[R] be cryptographically secure. This is a consequence of using a seeded pseudo-random number generator. However, they \f[I]are\f[R] guaranteed to be reproducible with identical \f[B]seed\f[R] values. This means that the pseudo-random values from dc(1) should only be used where a reproducible stream of pseudo-random numbers is \f[I]ESSENTIAL\f[R]. In any other case, use a non-seeded pseudo-random number generator. .PP The pseudo-random number generator, \f[B]seed\f[R], and all associated operations are \f[B]non-portable extensions\f[R]. .SS Comments -.PP Comments go from \f[B]#\f[R] until, and not including, the next newline. This is a \f[B]non-portable extension\f[R]. .SH NUMBERS -.PP Numbers are strings made up of digits, uppercase letters up to \f[B]F\f[R], and at most \f[B]1\f[R] period for a radix. Numbers can have up to \f[B]DC_NUM_MAX\f[R] digits. Uppercase letters are equal to \f[B]9\f[R] plus their position in the alphabet (i.e., \f[B]A\f[R] equals \f[B]10\f[R], or \f[B]9+1\f[R]). .PP If a digit or letter makes no sense with the current value of \f[B]ibase\f[R] (i.e., they are greater than or equal to the current value of \f[B]ibase\f[R]), then the behavior depends on the existence of the \f[B]-c\f[R]/\f[B]--digit-clamp\f[R] or \f[B]-C\f[R]/\f[B]--no-digit-clamp\f[R] options (see the \f[B]OPTIONS\f[R] section), the existence and setting of the \f[B]DC_DIGIT_CLAMP\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or the default, which can be queried with the \f[B]-h\f[R]/\f[B]--help\f[R] option. .PP If clamping is off, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are not changed. Instead, their given value is multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*A+3\[ha]0*B\f[R], which is \f[B]3\f[R] times \f[B]10\f[R] plus \f[B]11\f[R], or \f[B]41\f[R]. .PP If clamping is on, then digits or letters that are greater than or equal to the current value of \f[B]ibase\f[R] are set to the value of the highest valid digit in \f[B]ibase\f[R] before being multiplied by the appropriate power of \f[B]ibase\f[R] and added into the number. This means that, with an \f[B]ibase\f[R] of \f[B]3\f[R], the number \f[B]AB\f[R] is equal to \f[B]3\[ha]1*2+3\[ha]0*2\f[R], which is \f[B]3\f[R] times \f[B]2\f[R] plus \f[B]2\f[R], or \f[B]8\f[R]. .PP There is one exception to clamping: single-character numbers (i.e., \f[B]A\f[R] alone). Such numbers are never clamped and always take the value they would have in the highest possible \f[B]ibase\f[R]. This means that \f[B]A\f[R] alone always equals decimal \f[B]10\f[R] and \f[B]Z\f[R] alone always equals decimal \f[B]35\f[R]. This behavior is mandated by the standard for bc(1) (see the STANDARDS section) and is meant to provide an easy way to set the current \f[B]ibase\f[R] (with the \f[B]i\f[R] command) regardless of the current value of \f[B]ibase\f[R]. .PP If clamping is on, and the clamped value of a character is needed, use a leading zero, i.e., for \f[B]A\f[R], use \f[B]0A\f[R]. .PP In addition, dc(1) accepts numbers in scientific notation. These have the form \f[B]e\f[R]. The exponent (the portion after the \f[B]e\f[R]) must be an integer. An example is \f[B]1.89237e9\f[R], which is equal to \f[B]1892370000\f[R]. Negative exponents are also allowed, so \f[B]4.2890e_3\f[R] is equal to \f[B]0.0042890\f[R]. .PP \f[B]WARNING\f[R]: Both the number and the exponent in scientific notation are interpreted according to the current \f[B]ibase\f[R], but the number is still multiplied by \f[B]10\[ha]exponent\f[R] regardless of the current \f[B]ibase\f[R]. For example, if \f[B]ibase\f[R] is \f[B]16\f[R] and dc(1) is given the number string \f[B]FFeA\f[R], the resulting decimal number will be \f[B]2550000000000\f[R], and if dc(1) is given the number string \f[B]10e_4\f[R], the resulting decimal number will be \f[B]0.0016\f[R]. .PP Accepting input as scientific notation is a \f[B]non-portable extension\f[R]. .SH COMMANDS -.PP The valid commands are listed below. .SS Printing -.PP These commands are used for printing. .PP Note that both scientific notation and engineering notation are available for printing numbers. Scientific notation is activated by assigning \f[B]0\f[R] to \f[B]obase\f[R] using \f[B]0o\f[R], and engineering notation is activated by assigning \f[B]1\f[R] to \f[B]obase\f[R] using \f[B]1o\f[R]. To deactivate them, just assign a different value to \f[B]obase\f[R]. .PP Printing numbers in scientific notation and/or engineering notation is a \f[B]non-portable extension\f[R]. .TP \f[B]p\f[R] Prints the value on top of the stack, whether number or string, and prints a newline after. .RS .PP This does not alter the stack. .RE .TP \f[B]n\f[R] Prints the value on top of the stack, whether number or string, and pops it off of the stack. .TP \f[B]P\f[R] Pops a value off the stack. .RS .PP If the value is a number, it is truncated and the absolute value of the result is printed as though \f[B]obase\f[R] is \f[B]256\f[R] and each digit is interpreted as an 8-bit ASCII character, making it a byte stream. .PP If the value is a string, it is printed without a trailing newline. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]f\f[R] Prints the entire contents of the stack, in order from newest to oldest, without altering anything. .RS .PP Users should use this command when they get lost. .RE .SS Arithmetic -.PP These are the commands used for arithmetic. .TP \f[B]+\f[R] The top two values are popped off the stack, added, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to the max \f[I]scale\f[R] of both operands. .TP \f[B]-\f[R] The top two values are popped off the stack, subtracted, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to the max \f[I]scale\f[R] of both operands. .TP \f[B]*\f[R] The top two values are popped off the stack, multiplied, and the result is pushed onto the stack. If \f[B]a\f[R] is the \f[I]scale\f[R] of the first expression and \f[B]b\f[R] is the \f[I]scale\f[R] of the second expression, the \f[I]scale\f[R] of the result is equal to \f[B]min(a+b,max(scale,a,b))\f[R] where \f[B]min()\f[R] and \f[B]max()\f[R] return the obvious values. .TP \f[B]/\f[R] The top two values are popped off the stack, divided, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The first value popped off of the stack must be non-zero. .RE .TP \f[B]%\f[R] The top two values are popped off the stack, remaindered, and the result is pushed onto the stack. .RS .PP Remaindering is equivalent to 1) Computing \f[B]a/b\f[R] to current \f[B]scale\f[R], and 2) Using the result of step 1 to calculate \f[B]a-(a/b)*b\f[R] to \f[I]scale\f[R] \f[B]max(scale+scale(b),scale(a))\f[R]. .PP The first value popped off of the stack must be non-zero. .RE .TP \f[B]\[ti]\f[R] The top two values are popped off the stack, divided and remaindered, and the results (divided first, remainder second) are pushed onto the stack. This is equivalent to \f[B]x y / x y %\f[R] except that \f[B]x\f[R] and \f[B]y\f[R] are only evaluated once. .RS .PP The first value popped off of the stack must be non-zero. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[ha]\f[R] The top two values are popped off the stack, the second is raised to the power of the first, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The first value popped off of the stack must be an integer, and if that value is negative, the second value popped off of the stack must be non-zero. .RE .TP \f[B]v\f[R] The top value is popped off the stack, its square root is computed, and the result is pushed onto the stack. The \f[I]scale\f[R] of the result is equal to \f[B]scale\f[R]. .RS .PP The value popped off of the stack must be non-negative. .RE .TP \f[B]_\f[R] If this command \f[I]immediately\f[R] precedes a number (i.e., no spaces or other commands), then that number is input as a negative number. .RS .PP Otherwise, the top value on the stack is popped and copied, and the copy is negated and pushed onto the stack. This behavior without a number is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]b\f[R] The top value is popped off the stack, and if it is zero, it is pushed back onto the stack. Otherwise, its absolute value is pushed onto the stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]|\f[R] The top three values are popped off the stack, a modular exponentiation is computed, and the result is pushed onto the stack. .RS .PP The first value popped is used as the reduction modulus and must be an integer and non-zero. The second value popped is used as the exponent and must be an integer and non-negative. The third value popped is the base and must be an integer. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]$\f[R] The top value is popped off the stack and copied, and the copy is truncated and pushed onto the stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[at]\f[R] The top two values are popped off the stack, and the precision of the second is set to the value of the first, whether by truncation or extension. .RS .PP The first value popped off of the stack must be an integer and non-negative. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]H\f[R] The top two values are popped off the stack, and the second is shifted left (radix shifted right) to the value of the first. .RS .PP The first value popped off of the stack must be an integer and non-negative. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]h\f[R] The top two values are popped off the stack, and the second is shifted right (radix shifted left) to the value of the first. .RS .PP The first value popped off of the stack must be an integer and non-negative. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]G\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if they are equal, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]N\f[R] The top value is popped off of the stack, and if it a \f[B]0\f[R], a \f[B]1\f[R] is pushed; otherwise, a \f[B]0\f[R] is pushed. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B](\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is less than the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]{\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is less than or equal to the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B])\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is greater than the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]}\f[R] The top two values are popped off of the stack, they are compared, and a \f[B]1\f[R] is pushed if the first is greater than or equal to the second, or \f[B]0\f[R] otherwise. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]M\f[R] The top two values are popped off of the stack. If they are both non-zero, a \f[B]1\f[R] is pushed onto the stack. If either of them is zero, or both of them are, then a \f[B]0\f[R] is pushed onto the stack. .RS .PP This is like the \f[B]&&\f[R] operator in bc(1), and it is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]m\f[R] The top two values are popped off of the stack. If at least one of them is non-zero, a \f[B]1\f[R] is pushed onto the stack. If both of them are zero, then a \f[B]0\f[R] is pushed onto the stack. .RS .PP This is like the \f[B]||\f[R] operator in bc(1), and it is \f[I]not\f[R] a short-circuit operator. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Pseudo-Random Number Generator -.PP dc(1) has a built-in pseudo-random number generator. These commands query the pseudo-random number generator. (See Parameters for more information about the \f[B]seed\f[R] value that controls the pseudo-random number generator.) .PP The pseudo-random number generator is guaranteed to \f[B]NOT\f[R] be cryptographically secure. .TP \f[B]\[cq]\f[R] Generates an integer between 0 and \f[B]DC_RAND_MAX\f[R], inclusive (see the \f[B]LIMITS\f[R] section). .RS .PP The generated integer is made as unbiased as possible, subject to the limitations of the pseudo-random number generator. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]\[lq]\f[R] Pops a value off of the stack, which is used as an \f[B]exclusive\f[R] upper bound on the integer that will be generated. If the bound is negative or is a non-integer, an error is raised, and dc(1) resets (see the \f[B]RESET\f[R] section) while \f[B]seed\f[R] remains unchanged. If the bound is larger than \f[B]DC_RAND_MAX\f[R], the higher bound is honored by generating several pseudo-random integers, multiplying them by appropriate powers of \f[B]DC_RAND_MAX+1\f[R], and adding them together. Thus, the size of integer that can be generated with this command is unbounded. Using this command will change the value of \f[B]seed\f[R], unless the operand is \f[B]0\f[R] or \f[B]1\f[R]. In that case, \f[B]0\f[R] is pushed onto the stack, and \f[B]seed\f[R] is \f[I]not\f[R] changed. .RS .PP The generated integer is made as unbiased as possible, subject to the limitations of the pseudo-random number generator. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Stack Control -.PP These commands control the stack. .TP \f[B]c\f[R] Removes all items from (\[lq]clears\[rq]) the stack. .TP \f[B]d\f[R] Copies the item on top of the stack (\[lq]duplicates\[rq]) and pushes the copy onto the stack. .TP \f[B]r\f[R] Swaps (\[lq]reverses\[rq]) the two top items on the stack. .TP \f[B]R\f[R] Pops (\[lq]removes\[rq]) the top value from the stack. .SS Register Control -.PP These commands control registers (see the \f[B]REGISTERS\f[R] section). .TP \f[B]s\f[R]\f[I]r\f[R] Pops the value off the top of the stack and stores it into register \f[I]r\f[R]. .TP \f[B]l\f[R]\f[I]r\f[R] Copies the value in register \f[I]r\f[R] and pushes it onto the stack. This does not alter the contents of \f[I]r\f[R]. .TP \f[B]S\f[R]\f[I]r\f[R] Pops the value off the top of the (main) stack and pushes it onto the stack of register \f[I]r\f[R]. The previous value of the register becomes inaccessible. .TP \f[B]L\f[R]\f[I]r\f[R] Pops the value off the top of the stack for register \f[I]r\f[R] and push it onto the main stack. The previous value in the stack for register \f[I]r\f[R], if any, is now accessible via the \f[B]l\f[R]\f[I]r\f[R] command. .SS Parameters -.PP These commands control the values of \f[B]ibase\f[R], \f[B]obase\f[R], \f[B]scale\f[R], and \f[B]seed\f[R]. Also see the \f[B]SYNTAX\f[R] section. .TP \f[B]i\f[R] Pops the value off of the top of the stack and uses it to set \f[B]ibase\f[R], which must be between \f[B]2\f[R] and \f[B]16\f[R], inclusive. .RS .PP If the value on top of the stack has any \f[I]scale\f[R], the \f[I]scale\f[R] is ignored. .RE .TP \f[B]o\f[R] Pops the value off of the top of the stack and uses it to set \f[B]obase\f[R], which must be between \f[B]0\f[R] and \f[B]DC_BASE_MAX\f[R], inclusive (see the \f[B]LIMITS\f[R] section and the \f[B]NUMBERS\f[R] section). .RS .PP If the value on top of the stack has any \f[I]scale\f[R], the \f[I]scale\f[R] is ignored. .RE .TP \f[B]k\f[R] Pops the value off of the top of the stack and uses it to set \f[B]scale\f[R], which must be non-negative. .RS .PP If the value on top of the stack has any \f[I]scale\f[R], the \f[I]scale\f[R] is ignored. .RE .TP \f[B]j\f[R] Pops the value off of the top of the stack and uses it to set \f[B]seed\f[R]. The meaning of \f[B]seed\f[R] is dependent on the current pseudo-random number generator but is guaranteed to not change except for new major versions. .RS .PP The \f[I]scale\f[R] and sign of the value may be significant. .PP If a previously used \f[B]seed\f[R] value is used again, the pseudo-random number generator is guaranteed to produce the same sequence of pseudo-random numbers as it did when the \f[B]seed\f[R] value was previously used. .PP The exact value assigned to \f[B]seed\f[R] is not guaranteed to be returned if the \f[B]J\f[R] command is used. However, if \f[B]seed\f[R] \f[I]does\f[R] return a different value, both values, when assigned to \f[B]seed\f[R], are guaranteed to produce the same sequence of pseudo-random numbers. This means that certain values assigned to \f[B]seed\f[R] will not produce unique sequences of pseudo-random numbers. .PP There is no limit to the length (number of significant decimal digits) or \f[I]scale\f[R] of the value that can be assigned to \f[B]seed\f[R]. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]I\f[R] Pushes the current value of \f[B]ibase\f[R] onto the main stack. .TP \f[B]O\f[R] Pushes the current value of \f[B]obase\f[R] onto the main stack. .TP \f[B]K\f[R] Pushes the current value of \f[B]scale\f[R] onto the main stack. .TP \f[B]J\f[R] Pushes the current value of \f[B]seed\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]T\f[R] Pushes the maximum allowable value of \f[B]ibase\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]U\f[R] Pushes the maximum allowable value of \f[B]obase\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]V\f[R] Pushes the maximum allowable value of \f[B]scale\f[R] onto the main stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]W\f[R] Pushes the maximum (inclusive) integer that can be generated with the \f[B]\[cq]\f[R] pseudo-random number generator command. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Strings -.PP The following commands control strings. .PP dc(1) can work with both numbers and strings, and registers (see the \f[B]REGISTERS\f[R] section) can hold both strings and numbers. dc(1) always knows whether the contents of a register are a string or a number. .PP While arithmetic operations have to have numbers, and will print an error if given a string, other commands accept strings. .PP Strings can also be executed as macros. For example, if the string \f[B][1pR]\f[R] is executed as a macro, then the code \f[B]1pR\f[R] is executed, meaning that the \f[B]1\f[R] will be printed with a newline after and then popped from the stack. .TP \f[B][\f[R]\f[I]characters\f[R]\f[B]]\f[R] Makes a string containing \f[I]characters\f[R] and pushes it onto the stack. .RS .PP If there are brackets (\f[B][\f[R] and \f[B]]\f[R]) in the string, then they must be balanced. Unbalanced brackets can be escaped using a backslash (\f[B]\[rs]\f[R]) character. .PP If there is a backslash character in the string, the character after it (even another backslash) is put into the string verbatim, but the (first) backslash is not. .RE .TP \f[B]a\f[R] The value on top of the stack is popped. .RS .PP If it is a number, it is truncated and its absolute value is taken. The result mod \f[B]256\f[R] is calculated. If that result is \f[B]0\f[R], push an empty string; otherwise, push a one-character string where the character is the result of the mod interpreted as an ASCII character. .PP If it is a string, then a new string is made. If the original string is empty, the new string is empty. If it is not, then the first character of the original string is used to create the new string as a one-character string. The new string is then pushed onto the stack. .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]x\f[R] Pops a value off of the top of the stack. .RS .PP If it is a number, it is pushed back onto the stack. .PP If it is a string, it is executed as a macro. .PP This behavior is the norm whenever a macro is executed, whether by this command or by the conditional execution commands below. .RE .TP \f[B]>\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is greater than the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP For example, \f[B]0 1>a\f[R] will execute the contents of register \f[B]a\f[R], and \f[B]1 0>a\f[R] will not. .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]>\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]!>\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is not greater than the second (less than or equal to), then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]!>\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]<\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is less than the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]<\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]!<\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is not less than the second (greater than or equal to), then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]!<\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]=\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is equal to the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]=\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]!=\f[R]\f[I]r\f[R] Pops two values off of the stack that must be numbers and compares them. If the first value is not equal to the second, then the contents of register \f[I]r\f[R] are executed. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .RE .TP \f[B]!=\f[R]\f[I]r\f[R]\f[B]e\f[R]\f[I]s\f[R] Like the above, but will execute register \f[I]s\f[R] if the comparison fails. .RS .PP If either or both of the values are not numbers, dc(1) will raise an error and reset (see the \f[B]RESET\f[R] section). .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]?\f[R] Reads a line from the \f[B]stdin\f[R] and executes it. This is to allow macros to request input from users. .TP \f[B]q\f[R] During execution of a macro, this exits the execution of that macro and the execution of the macro that executed it. If there are no macros, or only one macro executing, dc(1) exits. .TP \f[B]Q\f[R] Pops a value from the stack which must be non-negative and is used the number of macro executions to pop off of the execution stack. If the number of levels to pop is greater than the number of executing macros, dc(1) exits. .TP \f[B],\f[R] Pushes the depth of the execution stack onto the stack. The execution stack is the stack of string executions. The number that is pushed onto the stack is exactly as many as is needed to make dc(1) exit with the \f[B]Q\f[R] command, so the sequence \f[B],Q\f[R] will make dc(1) exit. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Status -.PP These commands query status of the stack or its top value. .TP \f[B]Z\f[R] Pops a value off of the stack. .RS .PP If it is a number, calculates the number of significant decimal digits it has and pushes the result. It will push \f[B]1\f[R] if the argument is \f[B]0\f[R] with no decimal places. .PP If it is a string, pushes the number of characters the string has. .RE .TP \f[B]X\f[R] Pops a value off of the stack. .RS .PP If it is a number, pushes the \f[I]scale\f[R] of the value onto the stack. .PP If it is a string, pushes \f[B]0\f[R]. .RE .TP \f[B]u\f[R] Pops one value off of the stack. If the value is a number, this pushes \f[B]1\f[R] onto the stack. Otherwise (if it is a string), it pushes \f[B]0\f[R]. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]t\f[R] Pops one value off of the stack. If the value is a string, this pushes \f[B]1\f[R] onto the stack. Otherwise (if it is a number), it pushes \f[B]0\f[R]. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .TP \f[B]z\f[R] Pushes the current depth of the stack (before execution of this command) onto the stack. .TP \f[B]y\f[R]\f[I]r\f[R] Pushes the current stack depth of the register \f[I]r\f[R] onto the main stack. .RS .PP Because each register has a depth of \f[B]1\f[R] (with the value \f[B]0\f[R] in the top item) when dc(1) starts, dc(1) requires that each register\[cq]s stack must always have at least one item; dc(1) will give an error and reset otherwise (see the \f[B]RESET\f[R] section). This means that this command will never push \f[B]0\f[R]. .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Arrays -.PP These commands manipulate arrays. .TP \f[B]:\f[R]\f[I]r\f[R] Pops the top two values off of the stack. The second value will be stored in the array \f[I]r\f[R] (see the \f[B]REGISTERS\f[R] section), indexed by the first value. .TP \f[B];\f[R]\f[I]r\f[R] Pops the value on top of the stack and uses it as an index into the array \f[I]r\f[R]. The selected value is then pushed onto the stack. .TP \f[B]Y\f[R]\f[I]r\f[R] Pushes the length of the array \f[I]r\f[R] onto the stack. .RS .PP This is a \f[B]non-portable extension\f[R]. .RE .SS Global Settings -.PP These commands retrieve global settings. These are the only commands that require multiple specific characters, and all of them begin with the letter \f[B]g\f[R]. Only the characters below are allowed after the character \f[B]g\f[R]; any other character produces a parse error (see the \f[B]ERRORS\f[R] section). .TP \f[B]gl\f[R] Pushes the line length set by \f[B]DC_LINE_LENGTH\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) onto the stack. .TP \f[B]gx\f[R] Pushes \f[B]1\f[R] onto the stack if extended register mode is on, \f[B]0\f[R] otherwise. See the \f[I]Extended Register Mode\f[R] subsection of the \f[B]REGISTERS\f[R] section for more information. .TP \f[B]gz\f[R] Pushes \f[B]0\f[R] onto the stack if the leading zero setting has not been enabled with the \f[B]-z\f[R] or \f[B]--leading-zeroes\f[R] options (see the \f[B]OPTIONS\f[R] section), non-zero otherwise. .SH REGISTERS -.PP Registers are names that can store strings, numbers, and arrays. (Number/string registers do not interfere with array registers.) .PP Each register is also its own stack, so the current register value is the top of the stack for the register. All registers, when first referenced, have one value (\f[B]0\f[R]) in their stack, and it is a runtime error to attempt to pop that item off of the register stack. .PP In non-extended register mode, a register name is just the single character that follows any command that needs a register name. The only exceptions are: a newline (\f[B]`\[rs]n'\f[R]) and a left bracket (\f[B]`['\f[R]); it is a parse error for a newline or a left bracket to be used as a register name. .SS Extended Register Mode -.PP Unlike most other dc(1) implentations, this dc(1) provides nearly unlimited amounts of registers, if extended register mode is enabled. .PP If extended register mode is enabled (\f[B]-x\f[R] or \f[B]--extended-register\f[R] command-line arguments are given), then normal single character registers are used \f[I]unless\f[R] the character immediately following a command that needs a register name is a space (according to \f[B]isspace()\f[R]) and not a newline (\f[B]`\[rs]n'\f[R]). .PP In that case, the register name is found according to the regex \f[B][a-z][a-z0-9_]*\f[R] (like bc(1) identifiers), and it is a parse error if the next non-space characters do not match that regex. .SH RESET -.PP When dc(1) encounters an error or a signal that it has a non-default handler for, it resets. This means that several things happen. .PP First, any macros that are executing are stopped and popped off the stack. The behavior is not unlike that of exceptions in programming languages. Then the execution point is set so that any code waiting to execute (after all macros returned) is skipped. .PP Thus, when dc(1) resets, it skips any remaining code waiting to be executed. Then, if it is interactive mode, and the error was not a fatal error (see the \f[B]EXIT STATUS\f[R] section), it asks for more input; otherwise, it exits with the appropriate return code. .SH PERFORMANCE -.PP Most dc(1) implementations use \f[B]char\f[R] types to calculate the value of \f[B]1\f[R] decimal digit at a time, but that can be slow. This dc(1) does something different. .PP It uses large integers to calculate more than \f[B]1\f[R] decimal digit at a time. If built in a environment where \f[B]DC_LONG_BIT\f[R] (see the \f[B]LIMITS\f[R] section) is \f[B]64\f[R], then each integer has \f[B]9\f[R] decimal digits. If built in an environment where \f[B]DC_LONG_BIT\f[R] is \f[B]32\f[R] then each integer has \f[B]4\f[R] decimal digits. This value (the number of decimal digits per large integer) is called \f[B]DC_BASE_DIGS\f[R]. .PP In addition, this dc(1) uses an even larger integer for overflow checking. This integer type depends on the value of \f[B]DC_LONG_BIT\f[R], but is always at least twice as large as the integer type used to store digits. .SH LIMITS -.PP The following are the limits on dc(1): .TP \f[B]DC_LONG_BIT\f[R] The number of bits in the \f[B]long\f[R] type in the environment where dc(1) was built. This determines how many decimal digits can be stored in a single large integer (see the \f[B]PERFORMANCE\f[R] section). .TP \f[B]DC_BASE_DIGS\f[R] The number of decimal digits per large integer (see the \f[B]PERFORMANCE\f[R] section). Depends on \f[B]DC_LONG_BIT\f[R]. .TP \f[B]DC_BASE_POW\f[R] The max decimal number that each large integer can store (see \f[B]DC_BASE_DIGS\f[R]) plus \f[B]1\f[R]. Depends on \f[B]DC_BASE_DIGS\f[R]. .TP \f[B]DC_OVERFLOW_MAX\f[R] The max number that the overflow type (see the \f[B]PERFORMANCE\f[R] section) can hold. Depends on \f[B]DC_LONG_BIT\f[R]. .TP \f[B]DC_BASE_MAX\f[R] The maximum output base. Set at \f[B]DC_BASE_POW\f[R]. .TP \f[B]DC_DIM_MAX\f[R] The maximum size of arrays. Set at \f[B]SIZE_MAX-1\f[R]. .TP \f[B]DC_SCALE_MAX\f[R] The maximum \f[B]scale\f[R]. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_STRING_MAX\f[R] The maximum length of strings. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_NAME_MAX\f[R] The maximum length of identifiers. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_NUM_MAX\f[R] The maximum length of a number (in decimal digits), which includes digits after the decimal point. Set at \f[B]DC_OVERFLOW_MAX-1\f[R]. .TP \f[B]DC_RAND_MAX\f[R] The maximum integer (inclusive) returned by the \f[B]\[cq]\f[R] command, if dc(1). Set at \f[B]2\[ha]DC_LONG_BIT-1\f[R]. .TP Exponent The maximum allowable exponent (positive or negative). Set at \f[B]DC_OVERFLOW_MAX\f[R]. .TP Number of vars The maximum number of vars/arrays. Set at \f[B]SIZE_MAX-1\f[R]. .PP These limits are meant to be effectively non-existent; the limits are so large (at least on 64-bit machines) that there should not be any point at which they become a problem. In fact, memory should be exhausted before these limits should be hit. .SH ENVIRONMENT VARIABLES -.PP As \f[B]non-portable extensions\f[R], dc(1) recognizes the following environment variables: .TP \f[B]DC_ENV_ARGS\f[R] This is another way to give command-line arguments to dc(1). They should be in the same format as all other command-line arguments. These are always processed first, so any files given in \f[B]DC_ENV_ARGS\f[R] will be processed before arguments and files given on the command-line. This gives the user the ability to set up \[lq]standard\[rq] options and files to be used at every invocation. The most useful thing for such files to contain would be useful functions that the user might want every time dc(1) runs. Another use would be to use the \f[B]-e\f[R] option to set \f[B]scale\f[R] to a value other than \f[B]0\f[R]. .RS .PP The code that parses \f[B]DC_ENV_ARGS\f[R] will correctly handle quoted arguments, but it does not understand escape sequences. For example, the string \f[B]\[lq]/home/gavin/some dc file.dc\[rq]\f[R] will be correctly parsed, but the string \f[B]\[lq]/home/gavin/some \[dq]dc\[dq] file.dc\[rq]\f[R] will include the backslashes. .PP The quote parsing will handle either kind of quotes, \f[B]\[cq]\f[R] or \f[B]\[lq]\f[R]. Thus, if you have a file with any number of single quotes in the name, you can use double quotes as the outside quotes, as in \f[B]\[lq]some `dc' file.dc\[rq]\f[R], and vice versa if you have a file with double quotes. However, handling a file with both kinds of quotes in \f[B]DC_ENV_ARGS\f[R] is not supported due to the complexity of the parsing, though such files are still supported on the command-line where the parsing is done by the shell. .RE .TP \f[B]DC_LINE_LENGTH\f[R] If this environment variable exists and contains an integer that is greater than \f[B]1\f[R] and is less than \f[B]UINT16_MAX\f[R] (\f[B]2\[ha]16-1\f[R]), dc(1) will output lines to that length, including the backslash newline combo. The default line length is \f[B]70\f[R]. .RS .PP The special value of \f[B]0\f[R] will disable line length checking and print numbers without regard to line length and without backslashes and newlines. .RE .TP \f[B]DC_SIGINT_RESET\f[R] If dc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), then this environment variable has no effect because dc(1) exits on \f[B]SIGINT\f[R] when not in interactive mode. .RS .PP However, when dc(1) is in interactive mode, then if this environment variable exists and contains an integer, a non-zero value makes dc(1) reset on \f[B]SIGINT\f[R], rather than exit, and zero makes dc(1) exit. If this environment variable exists and is \f[I]not\f[R] an integer, then dc(1) will exit on \f[B]SIGINT\f[R]. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_TTY_MODE\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, then a non-zero value makes dc(1) use TTY mode, and zero makes dc(1) not use TTY mode. .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_PROMPT\f[R] If TTY mode is \f[I]not\f[R] available (see the \f[B]TTY MODE\f[R] section), then this environment variable has no effect. .RS .PP However, when TTY mode is available, then if this environment variable exists and contains an integer, a non-zero value makes dc(1) use a prompt, and zero or a non-integer makes dc(1) not use a prompt. If this environment variable does not exist and \f[B]DC_TTY_MODE\f[R] does, then the value of the \f[B]DC_TTY_MODE\f[R] environment variable is used. .PP This environment variable and the \f[B]DC_TTY_MODE\f[R] environment variable override the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_EXPR_EXIT\f[R] If any expressions or expression files are given on the command-line with \f[B]-e\f[R], \f[B]--expression\f[R], \f[B]-f\f[R], or \f[B]--file\f[R], then if this environment variable exists and contains an integer, a non-zero value makes dc(1) exit after executing the expressions and expression files, and a zero value makes dc(1) not exit. .RS .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .TP \f[B]DC_DIGIT_CLAMP\f[R] When parsing numbers and if this environment variable exists and contains an integer, a non-zero value makes dc(1) clamp digits that are greater than or equal to the current \f[B]ibase\f[R] so that all such digits are considered equal to the \f[B]ibase\f[R] minus 1, and a zero value disables such clamping so that those digits are always equal to their value, which is multiplied by the power of the \f[B]ibase\f[R]. .RS .PP This never applies to single-digit numbers, as per the bc(1) standard (see the \f[B]STANDARDS\f[R] section). .PP This environment variable overrides the default, which can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .RE .SH EXIT STATUS -.PP dc(1) returns the following exit statuses: .TP \f[B]0\f[R] No error. .TP \f[B]1\f[R] A math error occurred. This follows standard practice of using \f[B]1\f[R] for expected errors, since math errors will happen in the process of normal execution. .RS .PP Math errors include divide by \f[B]0\f[R], taking the square root of a negative number, using a negative number as a bound for the pseudo-random number generator, attempting to convert a negative number to a hardware integer, overflow when converting a number to a hardware integer, overflow when calculating the size of a number, and attempting to use a non-integer where an integer is required. .PP Converting to a hardware integer happens for the second operand of the power (\f[B]\[ha]\f[R]), places (\f[B]\[at]\f[R]), left shift (\f[B]H\f[R]), and right shift (\f[B]h\f[R]) operators. .RE .TP \f[B]2\f[R] A parse error occurred. .RS .PP Parse errors include unexpected \f[B]EOF\f[R], using an invalid character, failing to find the end of a string or comment, and using a token where it is invalid. .RE .TP \f[B]3\f[R] A runtime error occurred. .RS .PP Runtime errors include assigning an invalid number to any global (\f[B]ibase\f[R], \f[B]obase\f[R], or \f[B]scale\f[R]), giving a bad expression to a \f[B]read()\f[R] call, calling \f[B]read()\f[R] inside of a \f[B]read()\f[R] call, type errors (including attempting to execute a number), and attempting an operation when the stack has too few elements. .RE .TP \f[B]4\f[R] A fatal error occurred. .RS .PP Fatal errors include memory allocation errors, I/O errors, failing to open files, attempting to use files that do not have only ASCII characters (dc(1) only accepts ASCII characters), attempting to open a directory as a file, and giving invalid command-line options. .RE .PP The exit status \f[B]4\f[R] is special; when a fatal error occurs, dc(1) always exits and returns \f[B]4\f[R], no matter what mode dc(1) is in. .PP The other statuses will only be returned when dc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), since dc(1) resets its state (see the \f[B]RESET\f[R] section) and accepts more input when one of those errors occurs in interactive mode. This is also the case when interactive mode is forced by the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .PP These exit statuses allow dc(1) to be used in shell scripting with error checking, and its normal behavior can be forced by using the \f[B]-i\f[R] flag or \f[B]--interactive\f[R] option. .SH INTERACTIVE MODE -.PP Like bc(1), dc(1) has an interactive mode and a non-interactive mode. Interactive mode is turned on automatically when both \f[B]stdin\f[R] and \f[B]stdout\f[R] are hooked to a terminal, but the \f[B]-i\f[R] flag and \f[B]--interactive\f[R] option can turn it on in other situations. .PP In interactive mode, dc(1) attempts to recover from errors (see the \f[B]RESET\f[R] section), and in normal execution, flushes \f[B]stdout\f[R] as soon as execution is done for the current input. dc(1) may also reset on \f[B]SIGINT\f[R] instead of exit, depending on the contents of, or default for, the \f[B]DC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .SH TTY MODE -.PP If \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY, then \[lq]TTY mode\[rq] is considered to be available, and thus, dc(1) can turn on TTY mode, subject to some settings. .PP If there is the environment variable \f[B]DC_TTY_MODE\f[R] in the environment (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), then if that environment variable contains a non-zero integer, dc(1) will turn on TTY mode when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY. If the \f[B]DC_TTY_MODE\f[R] environment variable exists but is \f[I]not\f[R] a non-zero integer, then dc(1) will not turn TTY mode on. .PP If the environment variable \f[B]DC_TTY_MODE\f[R] does \f[I]not\f[R] exist, the default setting is used. The default setting can be queried with the \f[B]-h\f[R] or \f[B]--help\f[R] options. .PP TTY mode is different from interactive mode because interactive mode is required in the bc(1) specification (see the \f[B]STANDARDS\f[R] section), and interactive mode requires only \f[B]stdin\f[R] and \f[B]stdout\f[R] to be connected to a terminal. .SS Command-Line History -.PP Command-line history is only enabled if TTY mode is, i.e., that \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the \f[B]DC_TTY_MODE\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section) and its default do not disable TTY mode. See the \f[B]COMMAND LINE HISTORY\f[R] section for more information. .SS Prompt -.PP If TTY mode is available, then a prompt can be enabled. Like TTY mode itself, it can be turned on or off with an environment variable: \f[B]DC_PROMPT\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP If the environment variable \f[B]DC_PROMPT\f[R] exists and is a non-zero integer, then the prompt is turned on when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are connected to a TTY and the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options were not used. The read prompt will be turned on under the same conditions, except that the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options must also not be used. .PP However, if \f[B]DC_PROMPT\f[R] does not exist, the prompt can be enabled or disabled with the \f[B]DC_TTY_MODE\f[R] environment variable, the \f[B]-P\f[R] and \f[B]--no-prompt\f[R] options, and the \f[B]-R\f[R] and \f[B]--no-read-prompt\f[R] options. See the \f[B]ENVIRONMENT VARIABLES\f[R] and \f[B]OPTIONS\f[R] sections for more details. .SH SIGNAL HANDLING -.PP Sending a \f[B]SIGINT\f[R] will cause dc(1) to do one of two things. .PP If dc(1) is not in interactive mode (see the \f[B]INTERACTIVE MODE\f[R] section), or the \f[B]DC_SIGINT_RESET\f[R] environment variable (see the \f[B]ENVIRONMENT VARIABLES\f[R] section), or its default, is either not an integer or it is zero, dc(1) will exit. .PP However, if dc(1) is in interactive mode, and the \f[B]DC_SIGINT_RESET\f[R] or its default is an integer and non-zero, then dc(1) will stop executing the current input and reset (see the \f[B]RESET\f[R] section) upon receiving a \f[B]SIGINT\f[R]. .PP Note that \[lq]current input\[rq] can mean one of two things. If dc(1) is processing input from \f[B]stdin\f[R] in interactive mode, it will ask for more input. If dc(1) is processing input from a file in interactive mode, it will stop processing the file and start processing the next file, if one exists, or ask for input from \f[B]stdin\f[R] if no other file exists. .PP This means that if a \f[B]SIGINT\f[R] is sent to dc(1) as it is executing a file, it can seem as though dc(1) did not respond to the signal since it will immediately start executing the next file. This is by design; most files that users execute when interacting with dc(1) have function definitions, which are quick to parse. If a file takes a long time to execute, there may be a bug in that file. The rest of the files could still be executed without problem, allowing the user to continue. .PP \f[B]SIGTERM\f[R] and \f[B]SIGQUIT\f[R] cause dc(1) to clean up and exit, and it uses the default handler for all other signals. The one exception is \f[B]SIGHUP\f[R]; in that case, and only when dc(1) is in TTY mode (see the \f[B]TTY MODE\f[R] section), a \f[B]SIGHUP\f[R] will cause dc(1) to clean up and exit. .SH COMMAND LINE HISTORY -.PP dc(1) supports interactive command-line editing. .PP If dc(1) can be in TTY mode (see the \f[B]TTY MODE\f[R] section), history can be enabled. This means that command-line history can only be enabled when \f[B]stdin\f[R], \f[B]stdout\f[R], and \f[B]stderr\f[R] are all connected to a TTY. .PP Like TTY mode itself, it can be turned on or off with the environment variable \f[B]DC_TTY_MODE\f[R] (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). .PP \f[B]Note\f[R]: tabs are converted to 8 spaces. .SH SEE ALSO -.PP bc(1) .SH STANDARDS -.PP The dc(1) utility operators and some behavior are compliant with the operators in the IEEE Std 1003.1-2017 (\[lq]POSIX.1-2017\[rq]) bc(1) specification at https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html . .SH BUGS -.PP None are known. Report bugs at https://git.gavinhoward.com/gavin/bc . .SH AUTHOR -.PP -Gavin D. -Howard and contributors. +Gavin D. Howard \c +.MT gavin@gavinhoward.com +.ME \c +\ and contributors.