shell

An Arbitrary Precision Calculator Language

bc is a language that supports arbitrary precision numbers with interactive execution of statements. There are some similarities in the syntax to the C programming language. A standard math library is available with a command line option. bc starts by processing code from all the files listed on the command line in the listed order. After these have been processed, bc reads from standard input. At this time lines are executed as they are read, in usual interactive fashion.

bc accepts the following command line options:

-h, --help
    Print the usage and exit. 
-l, --mathlib
    Define the standard math library. 
-w, --warn
    Give warnings for extensions to POSIX bc. 
-s, --standard
    Process exactly the POSIX bc language. 
-q, --quiet
    Do not print the normal GNU bc welcome. 
-v, --version
    Print the version number and copyright and quit.

The most basic element in bc is the number. Numbers are of arbitrary precision. This precision refers to both the integer and the fractional parts. Numbers have two attributes: length and scale. The length is the total number of significant decimal digits and the scale is the total number of decimal digits after the decimal point. For example, .000001 has a length of 6 and scale of 6, while 1935.000 has a length of 7 and a scale of 3.

Numbers are stored in two types of variables: simple variables and arrays. Comments in bc start with the characters /* and end with the characters */.

A simple expression is just a constant. bc converts constants into internal decimal numbers using the current input base, specified by the variable ibase. (There is an exception in functions.) Legal values for ibase are 2 through 16. Assigning a value outside this range to ibase will result in a value of 2 or 16. Input numbers may contain the characters 0-9 and A-F. (Note: They must be capitals. Lower case letters are variable names.) Single digit numbers have always the value of the digit regardless of the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes all input digits greater than or equal to ibase to ibase-1. This makes the number FFF always the largest 3 digit number of the input base, which may be convenient in scripting.

Full expressions are similar to the ones in many other high level languages. Since there is only one kind of number, there are no rules for mixing types. Instead, there are rules on the scale of expressions. Every expression has a scale, which is derived from the scale of original numbers, the operation performed and in many cases, the value of the variable scale. Legal values for the variable scale range from 0 to the maximum number representable by a C integer.

In the following example some simple operations are computed:

$ bc
ibase = 10
20/3
6
scale = 3
20/3
6.666
obase = 2
20/3
110.1010101001
obase = 8
8^2
100

Functions provide a method of defining a computation that can be executed later. Functions in bc always compute a value and return it to the caller. Function definitions are “dynamic” in the sense that a function is undefined until a definition is encountered in the input. That definition is then used until another definition function for the same name is encountered. The new definition then replaces the older definition. A function is defined as follows:

define name ( parameters ) { newline
    auto_list   statement_list }

A function call is an expression of the form name (parameters).

Parameters can be numbers or arrays. In the function definition, zero or more parameters are specified by listing their names separated by commas.

$ bc
define p(r,n){      /* Function p uses to arguments */
  auto v            /* v is a local variable */
  v = r^n           /* r raised to the n power */
  return(v)}        /* value returned */
scale = 5
x = p(2.5,2)        /* x = 2.5 ^ 2 */
x                   /* print value of x */
6.25
length(x)           /* number of digits */
3
scale(x)            /* number of places to right of decimal point */
2

For more information about bc check out bc’s project page and its manual.

1 Comment

  • On 01.16.09 Pedro Fortuny said:

    Hi,

    you can use bc as a shell calculator when you need special (unsupported by the shell) operations. For example, a=3.2 ; b=2.3 ; c=`echo "$a * $b" | bc` ; echo $c gives you 7.3 (which is the rounding of the correct 7.36 to the current scale of 1).

speak up

Add your comment below, or trackback from your own site.

Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*Required Fields