bc (programming language)

While bc can work with arbitrary precision, it actually defaults to zero digits after the decimal point, so the expression 2/3 yields 0 (results are truncated, not rounded).

The -l option to bc sets the default scale (digits after the decimal point) to 20 and adds several additional mathematical functions to the language.

bc provided a conventional programming-language interface to the same capability via a simple compiler (a single yacc source file comprising a few hundred lines of code), which converted a C-like syntax into dc notation and piped the results through dc.

The GNU implementation has numerous extensions beyond the POSIX standard and is no longer a front-end to dc (it is a bytecode interpreter).

The fourth is an independent implementation by Gavin Howard[1] that is included in Android (operating system),[2][3] FreeBSD as of 13.3-RELEASE,[4][5][6] and macOS as of 13.0.

The scale() function for determining the precision (as with the scale variable) of its argument and the length() function for determining the number of significant decimal digits in its argument are also built-in.

It is entirely separate from dc-based implementations of the POSIX standard and is instead written in C. Nevertheless, it is fully backwards compatible as all POSIX bc programs will run unmodified as GNU bc programs.

GNU bc variables, arrays and function names may contain more than one character, some more operators have been included from C, and notably, an if clause may be followed by an else.

Output is achieved either by deliberately not assigning a result of a calculation to a variable (the POSIX way) or by using the added print statement.

Furthermore, a read statement allows the interactive input of a number into a running calculation.

For example, in the Journal of Statistical Software (July 2004, Volume 11, Issue 5), George Marsaglia published the following C code for the cumulative normal distribution: With some necessary changes to accommodate bc's different syntax, and noting that the constant "0.9189..." is actually log(2*PI)/2, this can be translated to the following GNU bc code: bc can be used non-interactively, with input through a pipe.