1# `bc` 2 3[![Coverity Scan Build Status][17]][18] 4 5***WARNING: This project has moved to [https://git.yzena.com/][20] for [these 6reasons][21], though GitHub will remain a mirror.*** 7 8This is an implementation of the [POSIX `bc` calculator][12] that implements 9[GNU `bc`][1] extensions, as well as the period (`.`) extension for the BSD 10flavor of `bc`. 11 12For more information, see this `bc`'s full manual. 13 14This `bc` also includes an implementation of `dc` in the same binary, accessible 15via a symbolic link, which implements all FreeBSD and GNU extensions. (If a 16standalone `dc` binary is desired, `bc` can be copied and renamed to `dc`.) The 17`!` command is omitted; I believe this poses security concerns and that such 18functionality is unnecessary. 19 20For more information, see the `dc`'s full manual. 21 22This `bc` also provides `bc`'s math as a library with C bindings, called `bcl`. 23 24For more information, see the full manual for `bcl`. 25 26This `bc` is Free and Open Source Software (FOSS). It is offered under the BSD 272-clause License. Full license text may be found in the [`LICENSE.md`][4] file. 28 29## Prerequisites 30 31This `bc` only requires either: 32 331. Windows 10 or later, or 342. A C99-compatible compiler and a (mostly) POSIX 2008-compatible system with 35 the XSI (X/Open System Interfaces) option group. 36 37Since POSIX 2008 with XSI requires the existence of a C99 compiler as `c99`, any 38POSIX and XSI-compatible system will have everything needed. 39 40POSIX-compatible systems that are known to work: 41 42* Linux 43* FreeBSD 44* OpenBSD 45* NetBSD 46* Mac OSX 47* Solaris* (as long as the Solaris version supports POSIX 2008) 48* AIX 49* HP-UX* (except for history) 50 51In addition, there is compatibility code to make this `bc` work on Windows. 52 53Please submit bug reports if this `bc` does not build out of the box on any 54system. 55 56## Build 57 58### Windows 59 60There is no guarantee that this `bc` will work on any version of Windows earlier 61than Windows 10 (I cannot test on earlier versions), but it is guaranteed to 62work on Windows 10 at least. 63 64Also, if building with MSBuild, the MSBuild bundled with Visual Studio is 65required. 66 67**Note**: Unlike the POSIX-compatible platforms, only one build configuration is 68supported on Windows: extra math and prompt enabled, history and NLS (locale 69support) disabled, with both calculators built. 70 71#### `bc` 72 73To build `bc`, you can open the `bc.sln` file in Visual Studio, select the 74configuration, and build. 75 76You can also build using MSBuild with the following from the root directory: 77 78``` 79msbuild -property:Configuration=<config> bc.sln 80``` 81 82where `<config>` is either one of `Debug` or `Release`. 83 84#### `bcl` (Library) 85 86To build the library, you can open the `bcl.sln` file in Visual Studio, select 87the configuration, and build. 88 89You can also build using MSBuild with the following from the root directory: 90 91``` 92msbuild -property:Configuration=<config> bcl.sln 93``` 94 95where `<config>` is either one of `Debug` or `Release`. 96 97### POSIX-Compatible Systems 98 99This `bc` should build unmodified on any POSIX-compliant system or on Windows 100starting with Windows 10 (though earlier versions may work). 101 102For more complex build requirements than the ones below, see the 103[build manual][5]. 104 105On POSIX-compatible systems, `bc` is built as `bin/bc` and `dc` is built as 106`bin/dc` by default. On Windows, they are built as `Release/bc/bc.exe` and 107`Release/bc/dc.exe`. 108 109**Note**: On Windows, `dc.exe` is just copied from `bc.exe`; it is not linked. 110Patches are welcome for a way to do that. 111 112#### Default 113 114For the default build with optimization, use the following commands in the root 115directory: 116 117``` 118./configure.sh -O3 119make 120``` 121 122#### One Calculator 123 124To only build `bc`, use the following commands: 125 126``` 127./configure.sh --disable-dc 128make 129``` 130 131To only build `dc`, use the following commands: 132 133``` 134./configure.sh --disable-bc 135make 136``` 137 138#### Debug 139 140For debug builds, use the following commands in the root directory: 141 142``` 143./configure.sh -g 144make 145``` 146 147#### Install 148 149To install, use the following command: 150 151``` 152make install 153``` 154 155By default, `bc` and `dc` will be installed in `/usr/local`. For installing in 156other locations, use the `PREFIX` environment variable when running 157`configure.sh` or pass the `--prefix=<prefix>` option to `configure.sh`. See the 158[build manual][5], or run `./configure.sh --help`, for more details. 159 160#### Library 161 162This `bc` does provide a way to build a math library with C bindings. This is 163done by the `-a` or `--library` options to `configure.sh`: 164 165``` 166./configure.sh -a 167``` 168 169When building the library, the executables are not built. For more information, 170see the [build manual][5]. 171 172The library API can be found in [`manuals/bcl.3.md`][26] or `man bcl` once the 173library is installed. 174 175The library is built as `bin/libbcl.a` on POSIX-compatible systems or as 176`Release/bcl/bcl.lib` on Windows. 177 178#### Package and Distro Maintainers 179 180##### Recommended Compiler 181 182When I ran benchmarks with my `bc` compiled under `clang`, it performed much 183better than when compiled under `gcc`. I recommend compiling this `bc` with 184`clang`. 185 186I also recommend building this `bc` with C11 if you can because `bc` will detect 187a C11 compiler and add `_Noreturn` to any relevant function(s). 188 189##### Recommended Optimizations 190 191I wrote this `bc` with Separation of Concerns, which means that there are many 192small functions that could be inlined. However, they are often called across 193file boundaries, and the default optimizer can only look at the current file, 194which means that they are not inlined. 195 196Thus, because of the way this `bc` is built, it will automatically be slower 197than other `bc` implementations when running scripts with no math. (My `bc`'s 198math is *much* faster, so any non-trivial script should run faster in my `bc`.) 199 200Some, or all, of the difference can be made up with the right optimizations. The 201optimizations I recommend are: 202 2031. `-O3` 2042. `-flto` (link-time optimization) 205 206in that order. 207 208Link-time optimization, in particular, speeds up the `bc` a lot. This is because 209when link-time optimization is turned on, the optimizer can look across files 210and inline *much* more heavily. 211 212However, I recommend ***NOT*** using `-march=native`. Doing so will reduce this 213`bc`'s performance, at least when building with link-time optimization. See the 214[benchmarks][19] for more details. 215 216##### Stripping Binaries 217 218By default, non-debug binaries are stripped, but stripping can be disabled with 219the `-T` option to `configure.sh`. 220 221##### Using This `bc` as an Alternative 222 223If this `bc` is packaged as an alternative to an already existing `bc` package, 224it is possible to rename it in the build to prevent name collision. To prepend 225to the name, just run the following: 226 227``` 228EXECPREFIX=<some_prefix> ./configure.sh 229``` 230 231To append to the name, just run the following: 232 233``` 234EXECSUFFIX=<some_suffix> ./configure.sh 235``` 236 237If a package maintainer wishes to add both a prefix and a suffix, that is 238allowed. 239 240**Note**: The suggested name (and package name) when `bc` is not available is 241`bc-gh`. 242 243##### Karatsuba Number 244 245Package and distro maintainers have one tool at their disposal to build this 246`bc` in the optimal configuration: `karatsuba.py`. 247 248This script is not a compile-time or runtime prerequisite; it is for package and 249distro maintainers to run once when a package is being created. It finds the 250optimal Karatsuba number (see the [algorithms manual][7] for more information) 251for the machine that it is running on. 252 253The easiest way to run this script is with `make karatsuba`. 254 255If desired, maintainers can also skip running this script because there is a 256sane default for the Karatsuba number. 257 258## Status 259 260This `bc` is robust. 261 262It is well-tested, fuzzed, and fully standards-compliant (though not certified) 263with POSIX `bc`. The math has been tested with 40+ million random problems, so 264it is as correct as I can make it. 265 266This `bc` can be used as a drop-in replacement for any existing `bc`. This `bc` 267is also compatible with MinGW toolchains, though history is not supported on 268Windows. 269 270In addition, this `bc` is considered complete; i.e., there will be no more 271releases with additional features. However, it *is* actively maintained, so if 272any bugs are found, they will be fixed in new releases. Also, additional 273translations will also be added as they are provided. 274 275## Comparison to GNU `bc` 276 277This `bc` compares favorably to GNU `bc`. 278 279* This `bc` builds natively on Windows. 280* It has more extensions, which make this `bc` more useful for scripting. 281* This `bc` is a bit more POSIX compliant. 282* It has a much less buggy parser. The GNU `bc` will give parse errors for what 283 is actually valid `bc` code, or should be. For example, putting an `else` on 284 a new line after a brace can cause GNU `bc` to give a parse error. 285* This `bc` has fewer crashes. 286* GNU `bc` calculates the wrong number of significant digits for `length(x)`. 287* GNU `bc` will sometimes print numbers incorrectly. For example, when running 288 it on the file `tests/bc/power.txt` in this repo, GNU `bc` gets all the right 289 answers, but it fails to wrap the numbers at the proper place when outputting 290 to a file. 291* This `bc` is faster. (See [Performance](#performance).) 292 293### Performance 294 295Because this `bc` packs more than `1` decimal digit per hardware integer, this 296`bc` is faster than GNU `bc` and can be *much* faster. Full benchmarks can be 297found at [manuals/benchmarks.md][19]. 298 299There is one instance where this `bc` is slower: if scripts are light on math. 300This is because this `bc`'s intepreter is slightly slower than GNU `bc`, but 301that is because it is more robust. See the [benchmarks][19]. 302 303## Algorithms 304 305To see what algorithms this `bc` uses, see the [algorithms manual][7]. 306 307## Locales 308 309Currently, there is no locale support on Windows. 310 311Additionally, this `bc` only has support for English (and US English), French, 312German, Portuguese, Dutch, Polish, Russian, Japanese, and Chinese locales. 313Patches are welcome for translations; use the existing `*.msg` files in 314`locales/` as a starting point. 315 316In addition, patches for improvements are welcome; the last two messages in 317Portuguese were made with Google Translate, and the Dutch, Polish, Russian, 318Japanese, and Chinese locales were all generated with [DeepL][22]. 319 320The message files provided assume that locales apply to all regions where a 321language is used, but this might not be true for, e.g., `fr_CA` and `fr_CH`. 322Any corrections or a confirmation that the current texts are acceptable for 323those regions would be appreciated, too. 324 325## Other Projects 326 327Other projects based on this bc are: 328 329* [busybox `bc`][8]. The busybox maintainers have made their own changes, so any 330 bugs in the busybox `bc` should be reported to them. 331 332* [toybox `bc`][9]. The maintainer has also made his own changes, so bugs in the 333 toybox `bc` should be reported there. 334 335* [FreeBSD `bc`][23]. While the `bc` in FreeBSD is kept up-to-date, it is better 336 to [report bugs there][24], as well as [submit patches][25], and the 337 maintainers of the package will contact me if necessary. 338 339## Language 340 341This `bc` is written in pure ISO C99, using POSIX 2008 APIs with custom Windows 342compatibility code. 343 344## Commit Messages 345 346This `bc` uses the commit message guidelines laid out in [this blog post][10]. 347 348## Semantic Versioning 349 350This `bc` uses [semantic versioning][11]. 351 352## Contents 353 354Items labeled with `(maintainer use only)` are not included in release source 355tarballs. 356 357Files: 358 359 .gitignore The git ignore file (maintainer use only). 360 .gitattributes The git attributes file (maintainer use only). 361 bc.sln The Visual Studio solution file for bc. 362 bc.vcxproj The Visual Studio project file for bc. 363 bc.vcxproj.filters The Visual Studio filters file for bc. 364 bcl.sln The Visual Studio solution file for bcl. 365 bcl.vcxproj The Visual Studio project file for bcl. 366 bcl.vcxproj.filters The Visual Studio filters file for bcl. 367 configure A symlink to configure.sh to make packaging easier. 368 configure.sh The configure script. 369 functions.sh A script with functions used by other scripts. 370 install.sh Install script. 371 karatsuba.py Script to find the optimal Karatsuba number. 372 LICENSE.md A Markdown form of the BSD 2-clause License. 373 link.sh A script to link dc to bc. 374 locale_install.sh A script to install locales, if desired. 375 locale_uninstall.sh A script to uninstall locales. 376 Makefile.in The Makefile template. 377 manpage.sh Script to generate man pages from markdown files 378 (maintainer use only). 379 NOTICE.md List of contributors and copyright owners. 380 RELEASE.md A checklist for making a release (maintainer use only). 381 release.sh A script to test for release (maintainer use only). 382 safe-install.sh Safe install script from musl libc. 383 384Folders: 385 386 gen The bc math library, help texts, and code to generate C source. 387 include All header files. 388 locales Locale files, in .msg format. Patches welcome for translations. 389 manuals Manuals for both programs. 390 src All source code. 391 tests All tests. 392 393[1]: https://www.gnu.org/software/bc/ 394[4]: ./LICENSE.md 395[5]: ./manuals/build.md 396[7]: ./manuals/algorithms.md 397[8]: https://git.busybox.net/busybox/tree/miscutils/bc.c 398[9]: https://github.com/landley/toybox/blob/master/toys/pending/bc.c 399[10]: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html 400[11]: http://semver.org/ 401[12]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html 402[17]: https://img.shields.io/coverity/scan/16609.svg 403[18]: https://scan.coverity.com/projects/gavinhoward-bc 404[19]: ./manuals/benchmarks.md 405[20]: https://git.yzena.com/gavin/bc 406[21]: https://gavinhoward.com/2020/04/i-am-moving-away-from-github/ 407[22]: https://www.deepl.com/translator 408[23]: https://cgit.freebsd.org/src/tree/contrib/bc 409[24]: https://bugs.freebsd.org/ 410[25]: https://reviews.freebsd.org/ 411[26]: ./manuals/bcl.3.md 412