An implementation of Unix dc and POSIX bc with GNU and BSD extensions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Gavin Howard bf58e4f208
Fix more benchmark typos
3 years ago
gen Add documentation for the -P/--no-prompt option 3 years ago
include Fix even more style 3 years ago
locales Add more locales 3 years ago
manuals Fix more benchmark typos 3 years ago
src Fix even more style 3 years ago
tests Make the divide script more useful 3 years ago
.gitignore Add more test scripts 3 years ago
.travis.yml Give up on travis Python 3 3 years ago Tweak the license and README 4 years ago Increment the version 3 years ago Update contributors list 3 years ago Add a GNU bug to the README 3 years ago Update the checklist 3 years ago
codecov.yml Add a codecov file 4 years ago Update all documentation that I could find 3 years ago Remove use of `local` from scripts 3 years ago Change all of the license headers 3 years ago Add a script to the karatsuba tests 3 years ago Change all of the license headers 3 years ago Make work for package managers 3 years ago Make locale symlinks work on Arch 3 years ago Make a change to the release script for an external change 3 years ago Make install work for just dc 4 years ago


Build Status codecov Coverity Scan Build Status

This is an implementation of the POSIX bc calculator that implements GNU bc extensions, as well as the period (.) extension for the BSD flavor of bc.

For more information, see this bc's full manual.

This bc also includes an implementation of dc in the same binary, accessible via a symbolic link, which implements all FreeBSD and GNU extensions. (If a standalone dc binary is desired, bc can be copied and renamed to dc.) The ! command is omitted; I believe this poses security concerns and that such functionality is unnecessary.

For more information, see the dc's full manual.

This bc is Free and Open Source Software (FOSS). It is offered under the BSD 2-clause License. Full license text may be found in the file.


This bc only requires a C99-compatible compiler and a (mostly) POSIX 2008-compatible system with the XSI (X/Open System Interfaces) option group.

Since POSIX 2008 with XSI requires the existence of a C99 compiler as c99, any POSIX and XSI-compatible system will have everything needed.

Systems that are known to work:

  • Linux
  • FreeBSD
  • OpenBSD
  • NetBSD
  • Mac OSX

Please submit bug reports if this bc does not build out of the box on any system besides Windows. If Windows binaries are needed, they can be found at xstatic.


This bc should build unmodified on any POSIX-compliant system.

For more complex build requirements than the ones below, see the build manual.

Pre-built Binaries

It is possible to download pre-compiled binaries for a wide list of platforms, including Linux- and Windows-based systems, from xstatic. This link always points to the latest release of bc.


For the default build with optimization, use the following commands in the root directory:

./ -O3

One Calculator

To only build bc, use the following commands:

./ --disable-dc

To only build dc, use the following commands:

./ --disable-bc


For debug builds, use the following commands in the root directory:

./ -g


To install, use the following command:

make install

By default, bc and dc will be installed in /usr/local. For installing in other locations, use the PREFIX environment variable when running or pass the --prefix=<prefix> option to See the build manual, or run ./ --help, for more details.

Package and Distro Maintainers

Using This bc as an Alternative

If this bc is packaged as an alternative to an already existing bc package, it is possible to rename it in the build to prevent name collision. To prepend to the name, just run the following:

EXECPREFIX=<some_prefix> ./

To append to the name, just run the following:

EXECSUFFIX=<some_suffix> ./

If a package maintainer wishes to add both a prefix and a suffix, that is allowed.

Note: The suggested name (and package name) is bc-gh.

Karatsuba Number

Package and distro maintainers have one tool at their disposal to build this bc in the optimal configuration:

This script is not a compile-time or runtime prerequisite; it is for package and distro maintainers to run once when a package is being created. It finds the optimal Karatsuba number (see the algorithms manual for more information) for the machine that it is running on.

If desired, maintainers can also skip running this script because there is a sane default for the Karatsuba number.


This bc is robust.

It is well-tested, fuzzed, and fully standards-compliant (though not certified) with POSIX bc. The math has been tested with 40+ million random problems, so it is as correct as I can make it.

This bc can be used as a drop-in replacement for any existing bc. This bc is also compatible with MinGW toolchains, though history is not supported on Windows.

Comparison to GNU bc

This bc compares favorably to GNU bc.

  • It has more extensions, which make this bc more useful for scripting.
  • This bc is a bit more POSIX compliant.
  • It has a much less buggy parser. The GNU bc will give parse errors for what is actually valid bc code, or should be. For example, putting an else on a new line after a brace can cause GNU bc to give a parse error.
  • This bc has fewer crashes.
  • GNU bc will report errors when there are none. For example, 0 ^ -251 causes GNU bc to report a divide by 0.
  • GNU bc calculates the wrong number of significant digits for length(x).
  • GNU bc will sometimes print numbers incorrectly. For example, when running it on the file tests/bc/power.txt in this repo, GNU bc gets all the right answers, but for some inexplicable reason, it fails to wrap the numbers at the proper place.
  • This bc is faster. (See Performance.)


Because this bc packs more than 1 decimal digit per hardware integer, this bc is faster than GNU bc and can be much faster. Full benchmarks can be found at manuals/

There is one instance where this bc is slower: if scripts are light on math. This is because this bc's intepreter is slightly slower than GNU bc, but that is because it is more robust. See the benchmarks.


To see what algorithms this bc uses, see the algorithms manual.


Currently, this bc only has support for English (and US English), French and German locales. Patches are welcome for translations; use the existing *.msg files in locales/ as a starting point.

The message files provided assume that locales apply to all regions where a language is used, but this might not be true for, e.g., fr_CA and fr_CH. Any corrections or a confirmation that the current texts are acceptable for those regions would be appreciated, too.

Other Projects

Other projects based on this bc are:

  • busybox bc. The busybox maintainers have made their own changes, so any bugs in the busybox bc should be reported to them.

  • toybox bc. The maintainer has also made his own changes, so bugs in the toybox bc should be reported there.


This bc is written in pure ISO C99, using POSIX 2008 APIs.

Commit Messages

This bc uses the commit message guidelines laid out in this blog post.

Semantic Versioning

This bc uses semantic versioning.


Items labeled with (maintainer use only) are not included in release source tarballs.


.gitignore           The git ignore file (maintainer use only).
.travis.yml          The Travis CI file (maintainer use only).
codecov.yml          The Codecov file (maintainer use only).         The configure script.         A script with functions used by other scripts.           Install script.         Script to find the optimal Karatsuba number.           A Markdown form of the BSD 2-clause License.              A script to link dc to bc.    A script to install locales, if desired.  A script to uninstall locales.          The Makefile template.            List of contributors and copyright owners.           A checklist for making a release (maintainer use only).           A script to test for release (maintainer use only).      Safe install script from musl libc.


gen      The bc math library, help texts, and code to generate C source.
include  All header files.
locales  Locale files, in .msg format. Patches welcome for translations.
manuals  Manuals for both programs.
src      All source code.
tests    All tests.