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