# 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.

### binary numbers

Conversion from decimal ratio to binary

## 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.
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:

## 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.

## External links

* bc, opengroup.org
* bc man page, man.cat-v.org
* bc, gnu.org – used in Linux and macOS
* bc, freebsd.org
* bc source, cvsweb.openbsd.org
* bc in gavinhoward, github.com – Gavin Howard bc that seems to be used in latest FreeBSD and provides many functions as an extension
* bc (programming language), wikipedia.org
* extensions.bc, x-bc.sourceforge.net – definitions of math fuctions for bc
* Learn X in Y minutes where X=bc, learnxinyminutes.com
* ↑ math.stackexchange question: find-a-fraction-given-the-repeating-binary-expansions