bc
[-ghilPqsvVw
] [--global-stacks
] [--help
] [--interactive
]
[--mathlib
] [--no-prompt
] [--quiet
] [--standard
] [--warn
]
[--version
] [-e
expr] [--expression=
expr...] [-f
file...]
[-file=
file...] [file...]
bc(1) is an interactive processor for a language first standardized in 1991 by POSIX. (The current standard is here.) 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
.
With all build options, except for extra math, enabled this bc(1) is a drop-in replacement for any bc(1), including (and especially) the GNU bc(1). It is also a drop-in replacement for any bc(1) if extra math is enabled, but it will have extra features not found in other bc(1) implementations.
The following are the options that bc(1) accepts.
-
-g
,--global-stacks
: Turns the globalsibase
,obase
, andscale
into stacks. This includesseed
if bc(1) was built with the extra math option.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,
output(x,b)
(in the extended library) could have been 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.
However, since using this flag means that functions cannot set
ibase
,obase
, orscale
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
, orscale
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.For functions that set
seed
, the value assigned toseed
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 onceseed
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 quits. -
-i
,--interactive
: Forces interactive mode.Per the standard, bc(1) has an interactive mode and a non-interactive mode. The interactive mode is turned on automatically when both
stdin
andstdout
are hooked to a terminal, but this flag can turn it on in other cases. In interactive mode, bc(1) attempts to recover from errors (see the RESET section), and in normal execution, flushesstdout
as soon as execution is done for the current input.This is a non-portable extension.
-
-l
,--mathlib
: Setsscale
(see the Scale section) to20
and loads the included math library before running any code, including any expressions or files specified on the command line.To learn what is in the library, see the LIBRARY section.
-
-P
,--no-prompt
: Disables the prompt in interactive mode. This is mostly for those users that do not want a prompt or are not used to having them inbc
. Most of those users would want to put this option inBC_ENV_ARGS
.If the prompt has been disabled while building bc(1), this option is a no-op.
This is a non-portable extension.
-
-q
,--quiet
: Do not print copyright header. bc(1) will also suppress the header in non-interactive mode.This is mostly for compatibility with the GNU bc(1).
This is a non-portable extension.
-
-s
,--standard
: Process exactly the language defined by the standard and error if any extensions are used.This is a non-portable extension.
-
-v
,-V
,--version
: Print the version information (copyright header) and exit.This is a non-portable extension.
-
-w
,--warn
: Like-s
and--standard
, except that warnings (and not errors) are given for non-standard extensions.This is a non-portable extension.
-
-e
expr,--expression
=expr: Evaluatesexpr
. 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.In other bc(1) implementations, this option causes the program to execute the expressions and then exit. This bc(1) does not, unless the
BC_EXPR_EXIT
is defined (see the ENVIRONMENT VARIABLES section).This is a non-portable extension.
-
-f
file,--file
=file: Reads infile
and evaluates it. If expressions are also given (see above), the expressions are evaluated in the order given.In other bc(1) implementations, this option causes the program to execute the files and then exit. This bc(1) does not, unless the
BC_EXPR_EXIT
is defined (see the ENVIRONMENT VARIABLES section).This is a non-portable extension.
Note: long options are only accepted if bc(1) is built with them enabled.
Any non-error output is written 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 <file> >&-
, 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
.
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 <file> 2>&-
, it will quit with an error. This is
done so that bc(1) can report problems 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
.
The syntax for bc(1) programs is mostly C-like, with some differences. This bc(1) follows the POSIX standard, 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
. The min
allowable value for obase
is 2
, unless bc(1) was built with the extra math
option. If it was, then the min allowable value is 0
. In this case, if obase
is 0
, values are output in scientific notation, and if obase
is 1
, values
are output in engineering notation. (Outputting in scientific or engineering
notation are non-portable extensions.) The max allowable value for obase
can be queried in bc(1) programs with the maxobase()
built in function.
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
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 FUNCTIONS). 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 global variable that
is accessed by a function that it calls, 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 not
surrounded 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.
There are two kinds of comments:
- Block comments are enclosed in
/*
and*/
. - Line comments go from
#
until, and not including, the next newline. This is a non-portable extension.
The following are named expressions in bc(1):
- Variables:
I
- Array Elements:
I[E]
ibase
obase
scale
last
or a single dot (.
)
Number 6 is a non-portable extension.
If bc(1) was built with the extra math option, the following is also a named expression:
seed
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 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, except
if the parameter passed to irand(E)
is 0
or 1
.
There is no limit to the length (number of significant decimal digits) or
scale of the value that can be assigned to seed
.
This command is only available if bc(1) was built with the extra math option.
This is a non-portable extension.
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.
Named expressions are required as the operand of
increment
/decrement
operators and as the left
side of assignment
operators.
The following are valid operands in bc(1):
- Numbers (see Numbers below).
- Array indices (
I[E]
). (E)
: The value ofE
(used to change precedence).sqrt(E)
: The square root ofE
.E
must be non-negative.length(E)
: The number of significant decimal digits inE
.length(I[])
: The number of elements in the arrayI
. This is a non-portable extension.scale(E)
: The scale ofE
.abs(E)
: The absolute value ofE
. This is a non-portable extension.I()
,I(E)
,I(E, E)
, and so on, whereI
is an identifier for a non-void function. TheE
parameters may also be arrays and array references.read()
: Reads a line fromstdin
and uses that as an expression. The result of that expression is the result of theread()
operand. This is a non-portable extension.maxibase()
: The max allowableibase
. This is a non-portable extension.maxobase()
: The max allowableobase
. This is a non-portable extension.maxscale()
: The max allowablescale
. This is a non-portable extension.
If bc(1) was built with the extra math option, the following are also valid operands:
rand()
: A pseudo-random integer between0
(inclusive) andBC_RAND_MAX
(inclusive). Using this operand will change the value ofseed
. This is a non-portable extension.irand(E)
: A pseudo-random integer between0
(inclusive) and the value ofE
(exclusive). IfE
is negative or is a non-integer (scale is not0
), an error is raised, and bc(1) resets (see the RESET section). IfE
is larger thanBC_RAND_MAX
, the higher bound is honored by generating several pseudo-random integers, multiplying them by appropriate powers ofBC_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 ofseed
. IfE
is0
or1
, then0
is returned, andseed
is not changed. This is a non-portable extension.maxrand()
: The max integer returned byrand()
. 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.
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
equal 9
+ their position in the alphabet (i.e., A
equals 10
, or 9 + 1
).
If a digit or letter makes no sense with the current value of ibase
, they are
set to the value of the highest valid digit in ibase
.
Single-character numbers (i.e., A
) take the value that they would have if they
were valid digits, regardless of the value of ibase
. This means that A
always equals decimal 10
and Z
always equals decimal 35
.
In addition, if bc(1) was built with the extra math option, it accepts numbers
in scientific notation. For bc(1), 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.
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 PostfixAssociativity: None
Description:
increment
,decrement
-
-
!
: Type: PrefixAssociativity: None
Description:
negation
,boolean not
-
$
: Type: PostfixAssociativity: None
Description:
truncation
-
@
: Type: BinaryAssociativity: Right
Description:
set precision
-
^
: Type: BinaryAssociativity: Right
Description:
power
-
*
/
%
: Type: BinaryAssociativity: Left
Description:
multiply
,divide
,modulus
-
+
-
: Type: BinaryAssociativity: Left
Description:
add
,subtract
-
<<
>>
: Type: BinaryAssociativity: Left
Description:
shift left
,shift right
-
=
<<=
>>=
+=
-=
*=
/=
%=
^=
@=
: Type: BinaryAssociativity: Right
Description:
assignment
-
==
<=
>=
!=
<
>
: Type: BinaryAssociativity: Left
Description:
relational
-
&&
: Type: BinaryAssociativity: Left
Description:
boolean and
-
||
: Type: BinaryAssociativity: Left
Description:
boolean or
The operators will be described in more detail below.
-
++
--
: The prefix and postfixincrement
anddecrement
operators behave exactly like they would in C. They require a named expression as an operand. -
-
: Thenegation
operator returns0
if a user attempts to negate any expression with the value0
. Otherwise, a copy of the expression with its sign flipped is returned. -
!
: Theboolean not
operator returns1
if the expression is0
, or0
otherwise.This is a non-portable extension.
-
$
: Thetruncation
operator returns a copy of the given expression with all of its scale removed.This is a non-portable extension.
This is only available if bc(1) has been compiled with the extra math option enabled.
-
@
: Theset 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.
This is only available if bc(1) has been compiled with the extra math option enabled.
-
^
: Thepower
operator (not theexclusive 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 second expression must be an integer (no scale), and if it is negative, the first value must be non-zero.
-
*
: Themultiply
operator takes two expressions, multiplies them, and returns the product. Ifa
is the scale of the first expression andb
is the scale of the second expression, the scale of the result is equal tomin(a+b,max(scale,a,b))
wheremin
andmax
return the obvious values. -
/
: Thedivide
operator takes two expressions, divides them, and returns the quotient. The scale of the result shall be the value ofscale
.The second expression must be non-zero.
-
%
: Themodulus
operator takes two expressions,a
andb
, and evaluates them by 1) Computinga/b
to currentscale
and 2) Using the result of step 1 to calculatea-(a/b)*b
to scalemax(scale+scale(b),scale(a))
.The second expression must be non-zero.
-
+
: Theadd
operator takes two expressions,a
andb
, and returns the sum, with a scale equal to the max of the scales ofa
andb
. -
-
: Thesubtract
operator takes two expressions,a
andb
, and returns the difference, with a scale equal to the max of the scales ofa
andb
. -
<<
: Theleft shift
operator takes two expressions,a
andb
, and returns a copy of the value ofa
with its decimal point movedb
places to the right.The second expression must be an integer (no scale) and non-negative.
This is a non-portable extension.
This is only available if bc(1) has been compiled with the extra math option enabled.
-
>>
: Theright shift
operator takes two expressions,a
andb
, and returns a copy of the value ofa
with its decimal point movedb
places to the left.The second expression must be an integer (no scale) and non-negative.
This is a non-portable extension.
This is only available if bc(1) has been compiled with the extra math option enabled.
-
=
<<=
>>=
+=
-=
*=
/=
%=
^=
@=
: Theassignment
operators take two expressions,a
andb
wherea
is a named expression.For
=
,b
is copied and the result is assigned toa
. For all others,a
andb
are applied as operands to the corresponding arithmetic operator and the result is assigned toa
.The
assignment
operators that correspond to operators that are extensions are themselves extensions.Also, those
assignment
operators that are extensions are only available if bc(1) has been compiled with the extra math option enabled. -
==
<=
>=
!=
<
>
: Therelational
operators compare two expressions,a
andb
, and if the relation holds, according to C language semantics, the result is1
. Otherwise, it is0
.Note that unlike in C, these operators have a lower precedence than the
assignment
operators, which means thata=b>c
is interpreted as(a=b)>c
.Also, unlike the standard requires, these operators can appear anywhere any other expressions can be used. This allowance is a non-portable extension.
-
&&
: Theboolean and
operator takes two expressions and returns1
if both expressions are non-zero,0
otherwise.This is not a short-circuit operator.
This is a non-portable extension.
-
||
: Theboolean or
operator takes two expressions and returns1
if one of the expressions is non-zero,0
otherwise.This is not a short-circuit operator.
This is a non-portable extension.
The following items are statements:
E
{
S
;
...;
S
}
if
(
E
)
S
if
(
E
)
S
else
S
while
(
E
)
S
for
(
E
;
E
;
E
)
S
- An empty statement
break
continue
quit
halt
limits
- A string of characters, enclosed in double quotes
print
E
,
...,
E
I()
,I(E)
,I(E, E)
, and so on, whereI
is an identifier for a void function. TheE
parameters may also be arrays and array references.
Numbers 4, 9, 11, 12, 14, and 15 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).
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. If
bc(1) has been built with the extra math option enabled, both scientific
notation and engineering notation are available for printing the results of
expressions. Scientific notation is activated by assigning 0
to obase
(in
any other context, an obase
of 0
is invalid), and engineering notation is
activated by assigning 1
to obase
(which is also invalid in any other
context). 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.
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.
All expressions in a statment are evaluated left to right, except as necessary
to maintain order of operations. This means, for example, that in the expression
i = 0; 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, this means that in the expression
i = 0; 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.
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.
As a non-portable extension, the opening brace of a define
statement may
appear on the next line.
The return statement may also be in the following forms:
return
return
(
)
return
E
The first two, or not specifying a return
statement, is equivalent to
return (0)
, unless the function is a void function.
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.
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.
All of the functions below, including the functions in the
extended library if bc(1) has been compiled with the extra
math option enabled, are available when the -l
or --mathlib
command-line
flags are given.
The standard defines the following functions for the math library:
-
s(x)
: Returns the sine ofx
, which is assumed to be in radians.This is a transcendental function.
-
c(x)
: Returns the cosine ofx
, which is assumed to be in radians.This is a transcendental function.
-
a(x)
: Returns the arctangent ofx
, in radians.This is a transcendental function.
-
l(x)
: Returns the natural logarithm ofx
.This is a transcendental function.
-
e(x)
: Returns the mathematical constante
raised to the power ofx
.This is a transcendental function.
-
j(x, n)
: Returns the bessel integer ordern
(truncated) ofx
.This is a transcendental function.
In addition to the standard library, if bc(1) has been
built with the extra math option, the following functions are available when
either the -l
or --mathlib
options are given.
However, 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.
The extended library is a non-portable extension.
-
p(x, y)
: Calculatesx
to the power ofy
, even ify
is not an integer, and returns the result to the currentscale
.This is a transcendental function.
-
r(x, p)
: Returnsx
rounded top
decimal places according to the rounding mode round half away from0
. -
ceil(x, p)
: Returnsx
rounded top
decimal places according to the rounding mode round away from0
. -
f(x)
: Returns the factorial of the truncated absolute value ofx
. -
perm(n, k)
: Returns the permutation of the truncated absolute value ofn
of the truncated absolute value ofk
, ifk <= n
. If not, it returns0
. -
comb(n, k)
: Returns the combination of the truncated absolute value ofn
of the truncated absolute value ofk
, ifk <= n
. If not, it returns0
. -
l2(x)
: Returns the logarithm base2
ofx
.This is a transcendental function.
-
l10(x)
: Returns the logarithm base10
ofx
.This is a transcendental function.
-
log(x, b)
: Returns the logarithm baseb
ofx
.This is a transcendental function.
-
cbrt(x)
: Returns the cube root ofx
. -
root(x, n)
: Calculates the truncated value ofn
,r
, and returns ther
th root ofx
to the currentscale
.If
r
is0
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 ifr
is even andx
is negative. -
pi(p)
: Returnspi
top
decimal places.This is a transcendental function.
-
t(x)
: Returns the tangent ofx
, which is assumed to be in radians.This is a transcendental function.
-
a2(y, x)
: Returns the arctangent ofy/x
, in radians. If bothy
andx
are equal to0
, it raises an error and causes bc(1) to reset (see the RESET section). Otherwise, ifx
is greater than0
, it returnsa(y/x)
. Ifx
is less than0
, andy
is greater than or equal to0
, it returnsa(y/x) + pi
. Ifx
is less than0
, andy
is less than0
, it returnsa(y/x) - pi
. Ifx
is equal to0
, andy
is greater than0
, it returnspi/2
. Ifx
is equal to0
, andy
is less than0
, it returns-pi/2
.This function is the same as the
atan2()
function in many programming languages.This is a transcendental function.
-
sin(x)
: Returns the sine ofx
, which is assumed to be in radians.This is an alias of
s(x)
.This is a transcendental function.
-
cos(x)
: Returns the cosine ofx
, which is assumed to be in radians.This is an alias of
c(x)
.This is a transcendental function.
-
tan(x)
: Returns the tangent ofx
, which is assumed to be in radians.If
x
is equal to1
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.
-
atan(x)
: Returns the arctangent ofx
, in radians.This is an alias of
a(x)
.This is a transcendental function.
-
atan2(y, x)
: Returns the arctangent ofy/x
, in radians. If bothy
andx
are equal to0
, it raises an error and causes bc(1) to reset (see the RESET section). Otherwise, ifx
is greater than0
, it returnsa(y/x)
. Ifx
is less than0
, andy
is greater than or equal to0
, it returnsa(y/x) + pi
. Ifx
is less than0
, andy
is less than0
, it returnsa(y/x) - pi
. Ifx
is equal to0
, andy
is greater than0
, it returnspi/2
. Ifx
is equal to0
, andy
is less than0
, 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.
-
r2d(x)
: Convertsx
from radians to degrees and returns the result.This is a transcendental function.
-
d2r(x)
: Convertsx
from degrees to radians and returns the result.This is a transcendental function.
-
frand(p)
: Generates a pseudo-random number between0
(inclusive) and1
(exclusive) with the number of decimal digits after the decimal point equal to the truncated absolute value ofp
. Ifp
is not0
, then calling this function will change the value ofseed
. Ifp
is0
, then0
is returned, andseed
is not changed. -
ifrand(i, p)
: Generates a pseudo-random number that is between0
(inclusive) and the truncated absolute value ofi
(exclusive) with the number of decimal digits after the decimal point equal to the truncated absolute value ofp
. If the absolute value ofi
is greater than or equal to2
, andp
is not0
, then calling this function will change the value ofseed
, otherwise,0
is returned andseed
is not changed. -
srand(x)
: Returnsx
with its sign flipped with probability0.5
. In other words, it randomizes the sign ofx
. -
brand()
: Returns a random boolean value (either0
or1
). -
ubytes(x)
: Returns the numbers of unsigned integer bytes required to hold the truncated absolute value ofx
. -
sbytes(x)
: Returns the numbers of signed, two's-complement integer bytes required to hold the truncated value ofx
. -
hex(x)
: Outputs the hexadecimal (base16
) representation ofx
.This is a void function.
-
binary(x)
: Outputs the binary (base2
) representation ofx
.This is a void function.
-
output(x, b)
: Outputs the baseb
representation ofx
.This is a void function.
-
uint(x)
: Outputs the representation, in binary and hexadecimal, ofx
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.
-
int(x)
: Outputs the representation, in binary and hexadecimal, ofx
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.
-
uintn(x, n)
: Outputs the representation, in binary and hexadecimal, ofx
as an unsigned integer inn
bytes. Both outputs are split into bytes separated by spaces.If
x
is not an integer, is negative, or cannot fit inton
bytes, an error message is printed instead, but bc(1) is not reset (see the RESET section).This is a void function.
-
intn(x, n)
: Outputs the representation, in binary and hexadecimal, ofx
as a signed, two's-complement integer inn
bytes. Both outputs are split into bytes separated by spaces.If
x
is not an integer or cannot fit inton
bytes, an error message is printed instead, but bc(1) is not reset (see the RESET section).This is a void function.
-
uint8(x)
: Outputs the representation, in binary and hexadecimal, ofx
as an unsigned integer in1
byte. Both outputs are split into bytes separated by spaces.If
x
is not an integer, is negative, or cannot fit into1
byte, an error message is printed instead, but bc(1) is not reset (see the RESET section).This is a void function.
-
int8(x)
: Outputs the representation, in binary and hexadecimal, ofx
as a signed, two's-complement integer in1
byte. Both outputs are split into bytes separated by spaces.If
x
is not an integer or cannot fit into1
byte, an error message is printed instead, but bc(1) is not reset (see the RESET section).This is a void function.
-
uint16(x)
: Outputs the representation, in binary and hexadecimal, ofx
as an unsigned integer in2
bytes. Both outputs are split into bytes separated by spaces.If
x
is not an integer, is negative, or cannot fit into2
bytes, an error message is printed instead, but bc(1) is not reset (see the RESET section).This is a void function.
-
int16(x)
: Outputs the representation, in binary and hexadecimal, ofx
as a signed, two's-complement integer in2
bytes. Both outputs are split into bytes separated by spaces.If
x
is not an integer or cannot fit into2
bytes, an error message is printed instead, but bc(1) is not reset (see the RESET section).This is a void function.
-
uint32(x)
: Outputs the representation, in binary and hexadecimal, ofx
as an unsigned integer in4
bytes. Both outputs are split into bytes separated by spaces.If
x
is not an integer, is negative, or cannot fit into4
bytes, an error message is printed instead, but bc(1) is not reset (see the RESET section).This is a void function.
-
int32(x)
: Outputs the representation, in binary and hexadecimal, ofx
as a signed, two's-complement integer in4
bytes. Both outputs are split into bytes separated by spaces.If
x
is not an integer or cannot fit into4
bytes, an error message is printed instead, but bc(1) is not reset (see the RESET section).This is a void function.
-
uint64(x)
: Outputs the representation, in binary and hexadecimal, ofx
as an unsigned integer in8
bytes. Both outputs are split into bytes separated by spaces.If
x
is not an integer, is negative, or cannot fit into8
bytes, an error message is printed instead, but bc(1) is not reset (see the RESET section).This is a void function.
-
int64(x)
: Outputs the representation, in binary and hexadecimal, ofx
as a signed, two's-complement integer in8
bytes. Both outputs are split into bytes separated by spaces.If
x
is not an integer or cannot fit into8
bytes, an error message is printed instead, but bc(1) is not reset (see the RESET section).This is a void function.
-
hex_uint(x, n)
: Outputs the representation of the truncated absolute value ofx
as an unsigned integer in hexadecimal usingn
bytes. Not all of the value will be output ifn
is too small.This is a void function.
-
binary_uint(x, n)
: Outputs the representation of the truncated absolute value ofx
as an unsigned integer in binary usingn
bytes. Not all of the value will be output ifn
is too small.This is a void function.
-
output_uint(x, n)
: Outputs the representation of the truncated absolute value ofx
as an unsigned integer in the currentobase
usingn
bytes. Not all of the value will be output ifn
is too small.This is a void function.
-
output_byte(x, i)
: Outputs bytei
of the truncated absolute value ofx
, where0
is the least significant byte andnumber_of_bytes - 1
is the most significant byte.This is a void function.
All transcendental functions can return slightly inaccurate results (up to 1 ULP). This is unavoidable, and this article 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)
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.
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
.
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.
The following are the limits on bc(1):
-
BC_LONG_BIT
: The number of bits in thelong
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 onBC_LONG_BIT
. -
BC_BASE_POW
: The max decimal number that each large integer can store (seeBC_BASE_DIGS
) plus1
. Depends onBC_BASE_DIGS
. -
BC_OVERFLOW_MAX
: The max number that the overflow type (see the PERFORMANCE section) can hold. Depends onBC_LONG_BIT
. -
BC_BASE_MAX
: The maximum output base. Set atBC_BASE_POW
. -
BC_DIM_MAX
: The maximum size of arrays. Set atSIZE_MAX-1
. -
BC_SCALE_MAX
: The maximumscale
. Set atBC_OVERFLOW_MAX-1
. -
BC_STRING_MAX
: The maximum length of strings. Set atBC_OVERFLOW_MAX-1
. -
BC_NAME_MAX
: The maximum length of identifiers. Set atBC_OVERFLOW_MAX-1
. -
BC_NUM_MAX
: The maximum length of a number (in decimal digits), which includes digits after the decimal point. Set atBC_OVERFLOW_MAX-1
. -
BC_RAND_MAX
: The maximum integer (inclusive) returned by therand()
operand, if bc(1) has been built with the extra math option. Set at2^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
.
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.
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 inBC_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 inDC_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 than1
and is less thanUINT16_MAX
(2^16-1
), bc(1) will output lines to that length, including the backslash (\
). The default line length is70
. -
BC_EXPR_EXIT
: If this variable exists (no matter the contents), bc(1) will exit immediately after executing expressions and files given by the-e
and/or-f
command-line options (and any equivalents).
bc(1) returns the following exit statuses:
-
0
: No error. -
1
: A math error occurred. This follows standard practice of using1
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, 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, giving an invalidauto
list, having a duplicateauto
/function parameter, failing to find the end of a code block, attempting to return a value from avoid
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
ibase
,obase
, orscale
; give a bad expression to aread()
call, callingread()
inside of aread()
call, type errors, passing the wrong number of parameters to functions, attempting to call an undefined function, and attempting to use avoid
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,
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
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 -i
.
If bc(1) has been compiled with the signal handling, sending a SIGINT
will
cause bc(1) to stop execution of the current input and reset (see the RESET
section), asking for more input.
Otherwise, SIGTERM
and SIGQUIT
cause bc(1) to clean up and exit, and it uses
the default handler for all other signals.
If bc(1) has not been compiled with signal handling, it uses the default signal handlers for all signals.
bc(1) supports interactive command-line editing, if compiled with the history
option enabled. If stdin
is hooked to a terminal, it is enabled. Previous
lines can be recalled and edited with the arrow keys.
Note: when bc(1) is built with history support, tabs are converted to 8 spaces.
This bc(1) ships with support for adding error messages for different locales.
dc(1)
bc(1) is compliant with the IEEE Std 1003.1-2017 (“POSIX.1-2017”)
specification. The flags -efghiqsvVw
, all long options, and the extensions
noted above are extensions to that specification.
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) ships with support for adding error messages for different locales,
so it supports LC_MESSAGES
.
This bc(1) was made from scratch by Gavin D. Howard.
None are known. Report bugs at https://git.yzena.com/gavin/bc.