diff options
Diffstat (limited to 'manuals/bc/E.1')
-rw-r--r-- | manuals/bc/E.1 | 442 |
1 files changed, 337 insertions, 105 deletions
diff --git a/manuals/bc/E.1 b/manuals/bc/E.1 index f157f666..ecb8b128 100644 --- a/manuals/bc/E.1 +++ b/manuals/bc/E.1 @@ -1,7 +1,7 @@ .\" .\" SPDX-License-Identifier: BSD-2-Clause .\" -.\" Copyright (c) 2018-2021 Gavin D. Howard and contributors. +.\" 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: @@ -25,26 +25,29 @@ .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.TH "BC" "1" "June 2021" "Gavin D. Howard" "General Commands Manual" +.TH "BC" "1" "February 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]-ghilPqRsvVw\f[R]] [\f[B]--global-stacks\f[R]] +\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[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. -(The current standard is -here (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html).) +(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. @@ -54,6 +57,8 @@ 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 @@ -71,6 +76,91 @@ See the \f[B]BUGS\f[R] section. .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. @@ -144,7 +234,18 @@ 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 quits. +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. @@ -154,6 +255,16 @@ Forces interactive mode. 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 @@ -163,11 +274,23 @@ any expressions or files specified on the command line. 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). +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 @@ -178,11 +301,28 @@ environment variables (see the \f[B]ENVIRONMENT VARIABLES\f[R] section). 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). +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 @@ -246,33 +386,33 @@ multiple times. 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. +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]-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. +\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 (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html) -and error if any extensions are used. +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 exit. +Print the version information (copyright header) and exits. .RS .PP This is a \f[B]non-portable extension\f[R]. @@ -287,45 +427,14 @@ continues normally. 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]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 above), the expressions are evaluated -in the order given. +\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 -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. +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 @@ -335,7 +444,7 @@ All long options are \f[B]non-portable extensions\f[R]. .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) read from \f[B]stdin\f[R]. +bc(1) reads from \f[B]stdin\f[R]. .PP However, there are a few caveats to this. .PP @@ -382,10 +491,9 @@ 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 (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html), -which is a much more thorough resource for the language this bc(1) -accepts. +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 @@ -525,6 +633,14 @@ This is a \f[B]non-portable extension\f[R]. \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. @@ -532,7 +648,7 @@ 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 "10." 4 +.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, @@ -540,13 +656,19 @@ 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 "11." 4 +.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 "12." 4 +.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). @@ -555,37 +677,86 @@ The \f[B]E\f[R] argument(s) may also be arrays of the form (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 "13." 4 +.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 "14." 4 +.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 "15." 4 +.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 "16." 4 +.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] + 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]). -If a digit or letter makes no sense with the current value of -\f[B]ibase\f[R], they are set to the value of the highest valid digit in -\f[B]ibase\f[R]. +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 -Single-character numbers (i.e., \f[B]A\f[R] alone) take the value that -they would have if they were valid digits, regardless of the value of -\f[B]ibase\f[R]. +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. @@ -773,8 +944,7 @@ 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 (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html) +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]. @@ -806,8 +976,8 @@ 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] +\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 @@ -833,9 +1003,11 @@ An empty statement .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] +\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] +\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 @@ -868,6 +1040,25 @@ The \f[B]if\f[R] \f[B]else\f[R] statement does the same thing as in C. 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] +for (i = 0; i < 3; ++i) i; quit +\f[R] +.fi +.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.) @@ -1063,9 +1254,8 @@ 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 (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html) -defines the following functions for the math library: +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. @@ -1117,11 +1307,11 @@ 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 this -article (https://people.eecs.berkeley.edu/~wkahan/LOG10HAF.TXT) explains -why it is impossible and unnecessary to calculate exact results for the +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 @@ -1253,7 +1443,8 @@ at which they become a problem. In fact, memory should be exhausted before these limits should be hit. .SH ENVIRONMENT VARIABLES .PP -bc(1) recognizes the following environment variables: +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 @@ -1295,6 +1486,12 @@ 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 @@ -1355,6 +1552,34 @@ 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: @@ -1434,9 +1659,8 @@ 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 (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html), -bc(1) has an interactive mode and a non-interactive mode. +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. @@ -1468,8 +1692,7 @@ 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 (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html), +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 @@ -1564,11 +1787,17 @@ dc(1) .SH STANDARDS .PP bc(1) is compliant with the IEEE Std 1003.1-2017 -(\[lq]POSIX.1-2017\[rq]) (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html) -specification. +(\[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]. @@ -1577,9 +1806,12 @@ This bc(1) supports error messages for different locales, and thus, it supports \f[B]LC_MESSAGES\f[R]. .SH BUGS .PP -None are known. -Report bugs at https://git.yzena.com/gavin/bc. +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 <gavin@yzena.com> and contributors. +Howard <gavin@gavinhoward.com> and contributors. |