Guide to Unix/Explanations/bc

bc is an arbitrary-precision decimal calculator language. The arbitrary-precision means that numbers can contain an arbitrary number of digits before the decimal point, limited by memory; most other languages limit numbers to eight bytes at most. The number of digits after the decimal point is per default zero, but can be set to a fixed number called "scale".

"bc" is often called using the "-l" option. The option loads the standard library containing mostly trigonometric functions available as single letters, and sets the number of decimal places for division to 20.

Overview
Bc at a glance:

Example session
This is an example of a user starting bc, doing two calculations, and exiting with "quit". Note that pressing ^D (control-D) also exits.

$ bc -l 3 + 4 7 2 / 5 .40000000000000000000 quit $

binary numbers
Conversion from decimal ratio to binary

bc 1.06 Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. obase=2 3/14 .0011011011011011011011011011011011011011011011011011011011011011010 1/5 .0011001100110011001100110011001100110011001100110011001100110011001

Example script
''Some versions of "bc" limit function and variable names to one letter. The following example has long function and variable names and must be changed to work on these copies of "bc".''

The following example was tested with OpenBSD bc and has also been verified with Gnu bc version 1.06 patchlevel 2.

This script implements Simpson's rule for integration. The function "integrate" approximates the definite integral from "a" to "b" using "n" parabola sections. The formula for "simpson" is taken from the Wikipedia article. This example integrates the sine function, but different functions can be used by replacing f.

define f(x) { return s(x); } define simpson(a,b) { return ( (b-a)/6 ) * ( f(a) + 4*f((a+b)/2) + f(b) ); } define integrate(a,b,n) { delta = (b - a) / n;  result = 0; for(i = a; (n = n - 1) + 1; i = i + delta) { /*print "calling simpson(", i, ", ", i + delta, ") with n = ", n, "\n";*/ result = result + simpson(i, i + delta); }  return result; }

Put this in a file, say "simpson", load it, and integrate f from 0 to pi (pi is "4*a(1)", 4 times arctangent of 1) with 100 intervals: $ bc -l simpson integrate(0, 4*a(1), 100) 2.00000000067647189101 ^D

Limitations

 * Bc does not support floating-point arithmetic. It supports fixed-point arithmetic, and the fixed number of decimal points can be set to be rather large, via scale.
 * bc does not allow numbers to be entered in the scientific notation, like 5.031E10.
 * No operators for bitwise manipulation (| for or, & for and, ^ for xor, <<, >>) in POSIX and GNU bc.
 * No operators for logical and and or (&&, ||) in POSIX bc; present in GNU bc as an extension.
 * Very limited number of functions in the standard library. Many additional functions are provided by Gavin Howard bc.

Standard library
Standard library is loaded when bc is called with the "-l" option. The library contains mostly trigonometric functions available as single letters. The library is usually implemented in bc itself and thus serves as examples of bc use. The functions included are s (sin), c (cos), a (atan), l (log), e (exp) and j (the bessel function).

Links:
 * libmath.b in bc-21, opensource.apple.com – actually GNU bc used in Linux as well
 * bc.library in freebsd-src, github.com
 * bc.library in openbsd/src, github.com
 * lib.bc in gavinhoward/bc, github.com
 * lib2.bc in gavinhoward/bc, github.com – extension functions

Decimal vs. binary
As specified by POSIX, bc shall behave as if performing the calculation using decimal representation in the digits after decimal point rather than the usual binary representation. One consequence is that 0.1 is exactly representable and that 0.1 * 3 is exactly 0.3, which is not the case in languages that use binary representation.