README.md
1# `bc`
2
3[![Build Status][13]][14]
4[![codecov][15]][16]
5[![Coverity Scan Build Status][17]][18]
6
7This is an implementation of the [POSIX `bc` calculator][12] that implements
8[GNU `bc`][1] extensions, as well as the period (`.`) extension for the BSD
9flavor of `bc`.
10
11For more information, see this `bc`'s [full manual][2].
12
13This `bc` also includes an implementation of `dc` in the same binary, accessible
14via a symbolic link, which implements all FreeBSD and GNU extensions. (If a
15standalone `dc` binary is desired, `bc` can be copied and renamed to `dc`.) The
16`!` command is omitted; I believe this poses security concerns and that such
17functionality is unnecessary.
18
19For more information, see the `dc`'s [full manual][3].
20
21This `bc` is Free and Open Source Software (FOSS). It is offered under the BSD
222-clause License. Full license text may be found in the [`LICENSE.md`][4] file.
23
24## Prerequisites
25
26This `bc` only requires a C99-compatible compiler and a (mostly) POSIX
272008-compatible system with the XSI (X/Open System Interfaces) option group.
28
29Since POSIX 2008 with XSI requires the existence of a C99 compiler as `c99`, any
30POSIX and XSI-compatible system will have everything needed.
31
32Systems that are known to work:
33
34* Linux
35* FreeBSD
36* OpenBSD
37* NetBSD
38* Mac OSX
39
40Please submit bug reports if this `bc` does not build out of the box on any
41system besides Windows. If Windows binaries are needed, they can be found at
42[xstatic][6].
43
44## Build
45
46This `bc` should build unmodified on any POSIX-compliant system.
47
48For more complex build requirements than the ones below, see the
49[build manual][5].
50
51### Pre-built Binaries
52
53It is possible to download pre-compiled binaries for a wide list of platforms,
54including Linux- and Windows-based systems, from [xstatic][6]. This link always
55points to the latest release of `bc`.
56
57### Default
58
59For the default build with optimization, use the following commands in the root
60directory:
61
62```
63./configure.sh -O3
64make
65```
66
67### One Calculator
68
69To only build `bc`, use the following commands:
70
71```
72./configure.sh --disable-dc
73make
74```
75
76To only build `dc`, use the following commands:
77
78```
79./configure.sh --disable-bc
80make
81```
82
83### Debug
84
85For debug builds, use the following commands in the root directory:
86
87```
88./configure.sh -g
89make
90```
91
92### Install
93
94To install, use the following command:
95
96```
97make install
98```
99
100By default, `bc` and `dc` will be installed in `/usr/local`. For installing in
101other locations, use the `PREFIX` environment variable when running
102`configure.sh` or pass the `--prefix=<prefix>` option to `configure.sh`. See the
103[build manual][5], or run `./configure.sh --help`, for more details.
104
105### Package and Distro Maintainers
106
107#### Recommended Compiler
108
109When I ran benchmarks with my `bc` compiled under `clang`, it performed much
110better than when compiled under `gcc`. I recommend compiling this `bc` with
111`clang`.
112
113#### Recommended Optimizations
114
115I wrote this `bc` with Separation of Concerns, which means that there are many
116small functions that could be inlined. However, they are often called across
117file boundaries, and the default optimizer can only look at the current file,
118which means that they are not inlined.
119
120Thus, because of the way this `bc` is built, it will automatically be slower
121than other `bc` implementations when running scripts with no math. (My `bc`'s
122math is *much* faster, so any non-trivial script should run faster in my `bc`.)
123
124Some, or all, of the difference can be made up with the right optimizations. The
125optimizations I recommend are:
126
1271. `-O3`
1282. `-flto` (link-time optimization)
1293. `-march=native` (optimize for the current CPU)
130
131in that order.
132
133Link-time optimization, in particular, speeds up the `bc` a lot. This is because
134when link-time optimization is turned on, the optimizer can look across files
135and inline *much* more heavily.
136
137For packages that are not built on the oldest supported hardware,
138`-march=native` is not recommended because of the possibility of illegal
139instructions.
140
141#### Stripping Binaries
142
143By default, non-debug binaries are stripped, but stripping can be disabled with
144the `-T` option to `configure.sh`.
145
146#### Using This `bc` as an Alternative
147
148If this `bc` is packaged as an alternative to an already existing `bc` package,
149it is possible to rename it in the build to prevent name collision. To prepend
150to the name, just run the following:
151
152```
153EXECPREFIX=<some_prefix> ./configure.sh
154```
155
156To append to the name, just run the following:
157
158```
159EXECSUFFIX=<some_suffix> ./configure.sh
160```
161
162If a package maintainer wishes to add both a prefix and a suffix, that is
163allowed.
164
165**Note**: The suggested name (and package name) is `bc-gh`.
166
167#### Karatsuba Number
168
169Package and distro maintainers have one tool at their disposal to build this
170`bc` in the optimal configuration: `karatsuba.py`.
171
172This script is not a compile-time or runtime prerequisite; it is for package and
173distro maintainers to run once when a package is being created. It finds the
174optimal Karatsuba number (see the [algorithms manual][7] for more information)
175for the machine that it is running on.
176
177If desired, maintainers can also skip running this script because there is a
178sane default for the Karatsuba number.
179
180## Status
181
182This `bc` is robust.
183
184It is well-tested, fuzzed, and fully standards-compliant (though not certified)
185with POSIX `bc`. The math has been tested with 40+ million random problems, so
186it is as correct as I can make it.
187
188This `bc` can be used as a drop-in replacement for any existing `bc`. This `bc`
189is also compatible with MinGW toolchains, though history is not supported on
190Windows.
191
192In addition, this `bc` is considered complete; i.e., there will be no more
193releases with additional features. However, it *is* actively maintained, so if
194any bugs are found, they will be fixed in new releases. Also, additional
195translations will also be added as they are provided.
196
197## Comparison to GNU `bc`
198
199This `bc` compares favorably to GNU `bc`.
200
201* It has more extensions, which make this `bc` more useful for scripting.
202* This `bc` is a bit more POSIX compliant.
203* It has a much less buggy parser. The GNU `bc` will give parse errors for what
204 is actually valid `bc` code, or should be. For example, putting an `else` on
205 a new line after a brace can cause GNU `bc` to give a parse error.
206* This `bc` has fewer crashes.
207* GNU `bc` calculates the wrong number of significant digits for `length(x)`.
208* GNU `bc` will sometimes print numbers incorrectly. For example, when running
209 it on the file `tests/bc/power.txt` in this repo, GNU `bc` gets all the right
210 answers, but it fails to wrap the numbers at the proper place when outputting
211 to a file.
212* This `bc` is faster. (See [Performance](#performance).)
213
214### Performance
215
216Because this `bc` packs more than `1` decimal digit per hardware integer, this
217`bc` is faster than GNU `bc` and can be *much* faster. Full benchmarks can be
218found at [manuals/benchmarks.md][19].
219
220There is one instance where this `bc` is slower: if scripts are light on math.
221This is because this `bc`'s intepreter is slightly slower than GNU `bc`, but
222that is because it is more robust. See the [benchmarks][19].
223
224## Algorithms
225
226To see what algorithms this `bc` uses, see the [algorithms manual][7].
227
228## Locales
229
230Currently, this `bc` only has support for English (and US English), French and
231German locales. Patches are welcome for translations; use the existing `*.msg`
232files in `locales/` as a starting point.
233
234The message files provided assume that locales apply to all regions where a
235language is used, but this might not be true for, e.g., `fr_CA` and `fr_CH`.
236Any corrections or a confirmation that the current texts are acceptable for
237those regions would be appreciated, too.
238
239## Other Projects
240
241Other projects based on this bc are:
242
243* [busybox `bc`][8]. The busybox maintainers have made their own changes, so any
244 bugs in the busybox `bc` should be reported to them.
245
246* [toybox `bc`][9]. The maintainer has also made his own changes, so bugs in the
247 toybox `bc` should be reported there.
248
249## Language
250
251This `bc` is written in pure ISO C99, using POSIX 2008 APIs.
252
253## Commit Messages
254
255This `bc` uses the commit message guidelines laid out in [this blog post][10].
256
257## Semantic Versioning
258
259This `bc` uses [semantic versioning][11].
260
261## Contents
262
263Items labeled with `(maintainer use only)` are not included in release source
264tarballs.
265
266Files:
267
268 .gitignore The git ignore file (maintainer use only).
269 .travis.yml The Travis CI file (maintainer use only).
270 codecov.yml The Codecov file (maintainer use only).
271 configure.sh The configure script.
272 functions.sh A script with functions used by other scripts.
273 install.sh Install script.
274 karatsuba.py Script to find the optimal Karatsuba number.
275 LICENSE.md A Markdown form of the BSD 2-clause License.
276 link.sh A script to link dc to bc.
277 locale_install.sh A script to install locales, if desired.
278 locale_uninstall.sh A script to uninstall locales.
279 Makefile.in The Makefile template.
280 NOTICE.md List of contributors and copyright owners.
281 RELEASE.md A checklist for making a release (maintainer use only).
282 release.sh A script to test for release (maintainer use only).
283 safe-install.sh Safe install script from musl libc.
284
285Folders:
286
287 gen The bc math library, help texts, and code to generate C source.
288 include All header files.
289 locales Locale files, in .msg format. Patches welcome for translations.
290 manuals Manuals for both programs.
291 src All source code.
292 tests All tests.
293
294[1]: https://www.gnu.org/software/bc/
295[2]: ./manuals/bc.md
296[3]: ./manuals/dc.md
297[4]: ./LICENSE.md
298[5]: ./manuals/build.md
299[6]: https://pkg.musl.cc/bc/
300[7]: ./manuals/algorithms.md
301[8]: https://git.busybox.net/busybox/tree/miscutils/bc.c
302[9]: https://github.com/landley/toybox/blob/master/toys/pending/bc.c
303[10]: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html
304[11]: http://semver.org/
305[12]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html
306[13]: https://travis-ci.com/gavinhoward/bc.svg?branch=master
307[14]: https://travis-ci.com/gavinhoward/bc
308[15]: https://codecov.io/gh/gavinhoward/bc/branch/master/graph/badge.svg
309[16]: https://codecov.io/gh/gavinhoward/bc
310[17]: https://img.shields.io/coverity/scan/16609.svg
311[18]: https://scan.coverity.com/projects/gavinhoward-bc
312[19]: ./manuals/benchmarks.md
313