LinuxCommandLibrary

bc

Perform arbitrary-precision arithmetic calculations

TLDR

Start an interactive session

$ bc
copy

Start an interactive session with the standard math library enabled
$ bc [[-i|--interactive]] [[-l|--mathlib]]
copy

Calculate an expression
$ echo '[5 / 3]' | bc
copy

Execute a script
$ bc [path/to/script.bc]
copy

Calculate an expression with the specified scale
$ echo 'scale = [10]; [5 / 3]' | bc
copy

Calculate a sine/cosine/arctangent/natural logarithm/exponential function using mathlib
$ echo '[s|c|a|l|e]([1])' | bc [[-l|--mathlib]]
copy

Execute an inline factorial script
$ echo "define factorial(n) { if (n <= 1) return 1; return n*factorial(n-1); }; factorial([10])" | bc
copy

SYNOPSIS

bc [ -hlwsqv ] [ long-options ] [ file ... ]

PARAMETERS

-h, --help
    Print the help message and exit.

-l, --mathlib
    Define the standard math library.

-w, --warn
    Give warnings for extensions to POSIX bc.

-q, --quiet
    Do not print the normal GNU bc welcome.

-s, --standard
    Process exactly the POSIX bc language.

-v, --version
    Print the version number and copyright and quit.

file ...
    Input file(s) to be processed.

DESCRIPTION

bc is an arbitrary precision calculator language.
It is a command-line utility and programming language that allows you to perform calculations with high accuracy, exceeding the limitations of standard floating-point arithmetic.
bc reads input from files listed in its argument list (if any), then from the standard input.
It executes the code read from each file, performing calculations and executing any specified control structures. It is often used for financial calculations, scientific computations, or any scenario where precise results are crucial. bc supports variables, functions, and control flow constructs, making it a powerful tool for both simple and complex mathematical tasks. You can use scale to specify the number of digits after the decimal point and define functions.

CAVEATS

bc does not provide error messages during runtime. It uses the scale variable to determine the precision of results. Integer division truncates the fractional part.

SCALE

The scale variable controls the number of digits after the decimal point in calculations.
Setting the scale appropriately is crucial for achieving the desired level of precision in bc.

MATH LIBRARY (-L)

The -l option loads the standard math library, providing functions like sine, cosine, arctangent, logarithm, exponential, and square root.

HISTORY

The bc command dates back to the early days of Unix.
It was designed to provide a calculator functionality, especially for arbitrary precision arithmetic, which was not well supported by the standard Unix utilities at the time. The GNU version of bc has expanded on the original functionality, adding features like functions and a more powerful programming model.
The primary developer and maintainer of the GNU bc has been Philip A. Nelson.

SEE ALSO

dc(1), awk(1), calc(1)

Copied to clipboard