xref: /freebsd/contrib/bc/README.md (revision b306c604df541dede4d0f3cc96188bbf5b6719fe)
1# `bc`
2
3***WARNING: New user registration for <https://git.gavinhoward.com/> is disabled
4because of spam. If you need to report a bug with `bc`, email gavin at this site
5minus the `git.` part for an account, and I will create one for you. Or you can
6report an issue at [GitHub][29].***
7
8***WARNING: This project has moved to [https://git.gavinhoward.com/][20] for
9[these reasons][21], though GitHub will remain a mirror.***
10
11This is an implementation of the [POSIX `bc` calculator][12] that implements
12[GNU `bc`][1] extensions, as well as the period (`.`) extension for the BSD
13flavor of `bc`.
14
15For more information, see this `bc`'s full manual.
16
17This `bc` also includes an implementation of `dc` in the same binary, accessible
18via a symbolic link, which implements all FreeBSD and GNU extensions. (If a
19standalone `dc` binary is desired, `bc` can be copied and renamed to `dc`.) The
20`!` command is omitted; I believe this poses security concerns and that such
21functionality is unnecessary.
22
23For more information, see the `dc`'s full manual.
24
25This `bc` also provides `bc`'s math as a library with C bindings, called `bcl`.
26
27For more information, see the full manual for `bcl`.
28
29## License
30
31This `bc` is Free and Open Source Software (FOSS). It is offered under the BSD
322-clause License. Full license text may be found in the [`LICENSE.md`][4] file.
33
34## Prerequisites
35
36This `bc` only requires either:
37
381.	Windows 10 or later, or
392.	A C99-compatible compiler and a (mostly) POSIX 2008-compatible system with
40	the XSI (X/Open System Interfaces) option group.
41
42Since POSIX 2008 with XSI requires the existence of a C99 compiler as `c99`, any
43POSIX and XSI-compatible system will have everything needed.
44
45POSIX-compatible systems that are known to work:
46
47* Linux
48* FreeBSD
49* OpenBSD
50* NetBSD
51* Mac OSX
52* Solaris* (as long as the Solaris version supports POSIX 2008)
53* AIX
54* HP-UX* (except for history)
55
56In addition, there is compatibility code to make this `bc` work on Windows.
57
58Please submit bug reports if this `bc` does not build out of the box on any
59system.
60
61## Build
62
63This `bc` should build unmodified on any POSIX-compliant system or on Windows
64starting with Windows 10 (though earlier versions may work).
65
66For more complex build requirements than the ones below, see the [build
67manual][5].
68
69### Windows
70
71There is no guarantee that this `bc` will work on any version of Windows earlier
72than Windows 10 (I cannot test on earlier versions), but it is guaranteed to
73work on Windows 10 at least.
74
75Also, if building with MSBuild, the MSBuild bundled with Visual Studio is
76required.
77
78**Note**: Unlike the POSIX-compatible platforms, only one build configuration is
79supported on Windows: extra math and history enabled, NLS (locale support)
80disabled, with both calculators built.
81
82#### `bc`
83
84To build `bc`, you can open the `vs/bc.sln` file in Visual Studio, select the
85configuration, and build.
86
87You can also build using MSBuild with the following from the root directory:
88
89```
90msbuild -property:Configuration=<config> vs/bc.sln
91```
92
93where `<config>` is either one of `Debug` or `Release`.
94
95On Windows, the calculators are built as `vs/bin/<platform>/<config>/bc.exe` and
96`vs/bin/<Platform>/<Config>/dc.exe`, where `<platform>` can be either `Win32` or
97`x64`, and `<config>` can be `Debug` or `Release`.
98
99**Note**: On Windows, `dc.exe` is just copied from `bc.exe`; it is not linked.
100Patches are welcome for a way to do that.
101
102#### `bcl` (Library)
103
104To build the library, you can open the `vs/bcl.sln` file in Visual Studio,
105select the configuration, and build.
106
107You can also build using MSBuild with the following from the root directory:
108
109```
110msbuild -property:Configuration=<config> vs/bcl.sln
111```
112
113where `<config>` is either one of `Debug`, `ReleaseMD`, or `ReleaseMT`.
114
115On Windows, the library is built as `vs/lib/<platform>/<config>/bcl.lib`, where
116`<platform>` can be either `Win32` or `x64`, and `<config>` can be `Debug`,
117`ReleaseMD`, or `ReleaseMT`.
118
119### POSIX-Compatible Systems
120
121On POSIX-compatible systems, `bc` is built as `bin/bc` and `dc` is built as
122`bin/dc` by default.
123
124#### Default
125
126For the default build with optimization, use the following commands in the root
127directory:
128
129```
130./configure.sh -O3
131make
132```
133
134#### One Calculator
135
136To only build `bc`, use the following commands:
137
138```
139./configure.sh --disable-dc
140make
141```
142
143To only build `dc`, use the following commands:
144
145```
146./configure.sh --disable-bc
147make
148```
149
150#### Debug
151
152For debug builds, use the following commands in the root directory:
153
154```
155./configure.sh -g
156make
157```
158
159#### Install
160
161To install, use the following command:
162
163```
164make install
165```
166
167By default, `bc` and `dc` will be installed in `/usr/local`. For installing in
168other locations, use the `PREFIX` environment variable when running
169`configure.sh` or pass the `--prefix=<prefix>` option to `configure.sh`. See the
170[build manual][5], or run `./configure.sh --help`, for more details.
171
172#### Library
173
174To build the math library, pass the `-a` or `--library` options to
175`configure.sh`:
176
177```
178./configure.sh -a
179```
180
181When building the library, the executables are not built. For more information,
182see the [build manual][5].
183
184The library API can be found in [`manuals/bcl.3.md`][26] or `man bcl` once the
185library is installed.
186
187#### Package and Distro Maintainers
188
189This section is for package and distro maintainers.
190
191##### Out-of-Source Builds
192
193Out-of-source builds are supported; just call `configure.sh` from the directory
194where the actual build will happen.
195
196For example, if the source is in `bc`, the build should happen in `build`, then
197call `configure.sh` and `make` like so:
198
199```
200../bc/configure.sh
201make
202```
203
204***WARNING***: The path to `configure.sh` from the build directory must not have
205spaces because `make` does not support target names with spaces.
206
207##### Recommended Compiler
208
209When I ran benchmarks with my `bc` compiled under `clang`, it performed much
210better than when compiled under `gcc`. I recommend compiling this `bc` with
211`clang`.
212
213I also recommend building this `bc` with C11 if you can because `bc` will detect
214a C11 compiler and add `_Noreturn` to any relevant function(s).
215
216##### Recommended Optimizations
217
218I wrote this `bc` with Separation of Concerns, which means that there are many
219small functions that could be inlined. However, they are often called across
220file boundaries, and the default optimizer can only look at the current file,
221which means that they are not inlined.
222
223Thus, because of the way this `bc` is built, it will automatically be slower
224than other `bc` implementations when running scripts with no math. (My `bc`'s
225math is *much* faster, so any non-trivial script should run faster in my `bc`.)
226
227Some, or all, of the difference can be made up with the right optimizations. The
228optimizations I recommend are:
229
2301.	`-O3`
2312.	`-flto` (link-time optimization)
232
233in that order.
234
235Link-time optimization, in particular, speeds up the `bc` a lot. This is because
236when link-time optimization is turned on, the optimizer can look across files
237and inline *much* more heavily.
238
239However, I recommend ***NOT*** using `-march=native`. Doing so will reduce this
240`bc`'s performance, at least when building with link-time optimization. See the
241[benchmarks][19] for more details.
242
243##### Stripping Binaries
244
245By default, non-debug binaries are stripped, but stripping can be disabled with
246the `-T` option to `configure.sh`.
247
248##### Using This `bc` as an Alternative
249
250If this `bc` is packaged as an alternative to an already existing `bc` package,
251it is possible to rename it in the build to prevent name collision. To prepend
252to the name, just run the following:
253
254```
255EXECPREFIX=<some_prefix> ./configure.sh
256```
257
258To append to the name, just run the following:
259
260```
261EXECSUFFIX=<some_suffix> ./configure.sh
262```
263
264If a package maintainer wishes to add both a prefix and a suffix, that is
265allowed.
266
267**Note**: The suggested name (and package name) when `bc` is not available is
268`bc-gh`.
269
270##### Karatsuba Number
271
272Package and distro maintainers have one tool at their disposal to build this
273`bc` in the optimal configuration: `scripts/karatsuba.py`.
274
275This script is not a compile-time or runtime prerequisite; it is for package and
276distro maintainers to run once when a package is being created. It finds the
277optimal Karatsuba number (see the [algorithms manual][7] for more information)
278for the machine that it is running on.
279
280The easiest way to run this script is with `make karatsuba`.
281
282If desired, maintainers can also skip running this script because there is a
283sane default for the Karatsuba number.
284
285## Status
286
287This `bc` is robust.
288
289It is well-tested, fuzzed, and fully standards-compliant (though not certified)
290with POSIX `bc`. The math has been tested with 40+ million random problems, so
291it is as correct as I can make it.
292
293This `bc` can be used as a drop-in replacement for any existing `bc`. This `bc`
294is also compatible with MinGW toolchains.
295
296In addition, this `bc` is considered complete; i.e., there will be no more
297releases with additional features. However, it *is* actively maintained, so if
298any bugs are found, they will be fixed in new releases. Also, additional
299translations will also be added as they are provided.
300
301### Development
302
303If I (Gavin D. Howard) get [hit by a bus][27] and future programmers need to
304handle work themselves, the best place to start is the [Development manual][28].
305
306## Vim Syntax
307
308I have developed (using other people's code to start) [`vim` syntax files][17]
309for this `bc` and `dc`, including the extensions.
310
311## `bc` Libs
312
313I have gathered some excellent [`bc` and `dc` libraries][18]. These libraries
314may prove useful to any serious users.
315
316## Comparison to GNU `bc`
317
318This `bc` compares favorably to GNU `bc`.
319
320* This `bc` builds natively on Windows.
321* It has more extensions, which make this `bc` more useful for scripting. (See
322  [Extensions](#extensions).)
323* This `bc` is a bit more POSIX compliant.
324* It has a much less buggy parser. The GNU `bc` will give parse errors for what
325  is actually valid `bc` code, or should be. For example, putting an `else` on
326  a new line after a brace can cause GNU `bc` to give a parse error.
327* This `bc` has fewer crashes.
328* GNU `bc` calculates the wrong number of significant digits for `length(x)`.
329* GNU `bc` will sometimes print numbers incorrectly. For example, when running
330  it on the file `tests/bc/power.txt` in this repo, GNU `bc` gets all the right
331  answers, but it fails to wrap the numbers at the proper place when outputting
332  to a file.
333* This `bc` is faster. (See [Performance](#performance).)
334
335### Performance
336
337Because this `bc` packs more than `1` decimal digit per hardware integer, this
338`bc` is faster than GNU `bc` and can be *much* faster. Full benchmarks can be
339found at [manuals/benchmarks.md][19].
340
341There is one instance where this `bc` is slower: if scripts are light on math.
342This is because this `bc`'s intepreter is slightly slower than GNU `bc`, but
343that is because it is more robust. See the [benchmarks][19].
344
345### Extensions
346
347Below is a non-comprehensive list of extensions that this `bc` and `dc` have
348that all others do not.
349
350* An extended math library. (See [here][30] for more information.)
351* A command-line prompt.
352* Turning on and off digit clamping. (Digit clamping is about how to treat
353  "invalid" digits for a particular base. GNU `bc` uses it, and the BSD `bc`
354  does not. Mine does both.)
355* A pseudo-random number generator. This includes the ability to set the seed
356  and get reproducible streams of random numbers.
357* The ability to use stacks for the globals `scale`, `ibase`, and `obase`
358  instead of needing to restore them in *every* function.
359* The ability to *not* use non-standard keywords. For example, `abs` is a
360  keyword (a built-in function), but if some script actually defines a function
361  called that, it's possible to tell my `bc` to not treat it as a keyword, which
362  will make the script parses correctly.
363* The ability to turn on and off printing leading zeroes on numbers greater than
364  `-1` and less than `1`.
365* Outputting in scientific and engineering notation.
366* Accepting input in scientific and engineering notation.
367* Passing strings and arrays to the `length()` built-in function. (In `dc`, the
368  `Y` command will do this for arrays, and the `Z` command will do this for both
369  numbers and strings.)
370* The `abs()` built-in function. (This is the `b` command in `dc`.)
371* The `is_number()` and `is_string()` built-in functions. (These tell whether a
372  variable is holding a string or a number, for runtime type checking. The
373  commands are `u` and `t` in `dc`.)
374* For `bc` only, the `divmod()` built-in function for computing a quotient and
375  remainder at the same time.
376* For `bc` only, the `asciify()` built-in function for converting an array to a
377  string.
378* The `$` truncation operator. (It's the same in `bc` and `dc`.)
379* The `@` "set scale" operator. (It's the same in `bc` and `dc`.)
380* The decimal shift operators. (`<<` and `>>` in `bc`, `H` and `h` in `dc`.)
381* Built-in functions or commands to get the max of `scale`, `ibase`, and
382  `obase`.
383* The ability to put strings into variables in `bc`. (This always existed in
384  `dc`.)
385* The `'` command in `dc` for the depth of the execution stack.
386* The `y` command in `dc` for the depth of register stacks.
387* Built-in functions or commands to get the value of certain environment
388  variables that might affect execution.
389* The `stream` keyword to do the same thing as the `P` command in `dc`.
390* Defined order of evaluation.
391* Defined exit statuses.
392* All environment variables other than `POSIXLY_CORRECT`, `BC_ENV_ARGS`, and
393  `BC_LINE_LENGTH`.
394* The ability for users to define their own defaults for various options during
395  build. (See [here][31] for more information.)
396
397## Algorithms
398
399To see what algorithms this `bc` uses, see the [algorithms manual][7].
400
401## Locales
402
403Currently, there is no locale support on Windows.
404
405Additionally, this `bc` only has support for English (and US English), French,
406German, Portuguese, Dutch, Polish, Russian, Japanese, and Chinese locales.
407Patches are welcome for translations; use the existing `*.msg` files in
408`locales/` as a starting point.
409
410In addition, patches for improvements are welcome; the last two messages in
411Portuguese were made with Google Translate, and the Dutch, Polish, Russian,
412Japanese, and Chinese locales were all generated with [DeepL][22].
413
414The message files provided assume that locales apply to all regions where a
415language is used, but this might not be true for, e.g., `fr_CA` and `fr_CH`.
416Any corrections or a confirmation that the current texts are acceptable for
417those regions would be appreciated, too.
418
419## Other Projects
420
421Other projects based on this bc are:
422
423* [busybox `bc`][8]. The busybox maintainers have made their own changes, so any
424  bugs in the busybox `bc` should be reported to them.
425* [toybox `bc`][9]. The maintainer has also made his own changes, so bugs in the
426  toybox `bc` should be reported there.
427* [FreeBSD `bc`][23]. While the `bc` in FreeBSD is kept up-to-date, it is better
428  to [report bugs there][24], as well as [submit patches][25], and the
429  maintainers of the package will contact me if necessary.
430* [Mac OSX `bc`][35]. Any bugs in that `bc` should be reported to me, but do
431  expect bugs because the version is old.
432* [Android Open Source `bc`][32]. Any bugs in that `bc` can be reported here.
433
434This is a non-comprehensive list of Linux distros that use this `bc` as the
435system `bc`:
436
437* [Gentoo][33]; it is a first-class alternative to GNU `bc`, but not exclusive.
438* [Linux from Scratch][34].
439
440Other Linux distros package it as a second-class alternative, usually as `bc-gh`
441or `howard-bc`.
442
443## Language
444
445This `bc` is written in pure ISO C99, using POSIX 2008 APIs with custom Windows
446compatibility code.
447
448## Commit Messages
449
450This `bc` uses the commit message guidelines laid out in [this blog post][10].
451
452## Semantic Versioning
453
454This `bc` uses [semantic versioning][11].
455
456## AI-Free
457
458This repository is 100% AI-Free code.
459
460## Contents
461
462Items labeled with `(maintainer use only)` are not included in release source
463tarballs.
464
465Files:
466
467	.gitignore           The git ignore file (maintainer use only).
468	.gitattributes       The git attributes file (maintainer use only).
469	bcl.pc.in            A template pkg-config file for bcl.
470	configure            A symlink to configure.sh to make packaging easier.
471	configure.sh         The configure script.
472	LICENSE.md           A Markdown form of the BSD 2-clause License.
473	Makefile.in          The Makefile template.
474	NEWS.md              The changelog.
475	NOTICE.md            List of contributors and copyright owners.
476
477Folders:
478
479	benchmarks  A folder of benchmarks for various aspects of bc performance.
480	gen         The bc math library, help texts, and code to generate C source.
481	include     All header files.
482	locales     Locale files, in .msg format. Patches welcome for translations.
483	manuals     Manuals for both programs.
484	src         All source code.
485	scripts     A bunch of shell scripts to help with development and building.
486	tests       All tests.
487	vs          Files needed for the build on Windows.
488
489[1]: https://www.gnu.org/software/bc/
490[4]: ./LICENSE.md
491[5]: ./manuals/build.md
492[7]: ./manuals/algorithms.md
493[8]: https://git.busybox.net/busybox/tree/miscutils/bc.c
494[9]: https://github.com/landley/toybox/blob/master/toys/pending/bc.c
495[10]: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html
496[11]: http://semver.org/
497[12]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html
498[17]: https://git.gavinhoward.com/gavin/vim-bc
499[18]: https://git.gavinhoward.com/gavin/bc_libs
500[19]: ./manuals/benchmarks.md
501[20]: https://git.gavinhoward.com/gavin/bc
502[21]: https://gavinhoward.com/2020/04/i-am-moving-away-from-github/
503[22]: https://www.deepl.com/translator
504[23]: https://cgit.freebsd.org/src/tree/contrib/bc
505[24]: https://bugs.freebsd.org/
506[25]: https://reviews.freebsd.org/
507[26]: ./manuals/bcl.3.md
508[27]: https://en.wikipedia.org/wiki/Bus_factor
509[28]: ./manuals/development.md
510[29]: https://github.com/gavinhoward/bc
511[30]: ./manuals/bc/A.1.md#extended-library
512[31]: ./manuals/build.md#settings
513[32]: https://android.googlesource.com/platform/external/bc/
514[33]: https://github.com/gentoo/gentoo/blob/master/app-alternatives/bc/bc-0.ebuild#L8
515[34]: https://www.linuxfromscratch.org/lfs/view/stable/chapter08/bc.html
516[35]: https://github.com/apple-oss-distributions/bc/tree/main/bc
517