Content deleted Content added
No edit summary Tags: Reverted Visual edit |
Stevebroshar (talk | contribs) there is a language, but's primarily a program, a tool, software |
||
(17 intermediate revisions by 15 users not shown) | |||
Line 1:
{{Short description|Arbitrary-precision calculator supporting interactive and scripted use}}{{Not to be confused with|B programming language|C programming language}}{{more footnotes|date=June 2013}}
{{lowercase|title=bc programming language}}
{{Infobox software
Line 17:
| website =
}}
'''bc''', for ''basic calculator'', is an [[Arbitrary-precision arithmetic|arbitrary-precision]] [[math]]ematical [[calculator]] [[computer program|program]] with an input language similar to [[C (programming language)|C]]. It supports both interactive, [[command-line interface|command-line]] [[user-interface]] and script processing.
==Overview==
A typical interactive usage is typing the command <code>bc</code> on a [[Unix]] [[Command-line interface#Command prompt|command prompt]] and entering a mathematical expression, such as {{code|(1 + 3) * 2}}, whereupon {{samp|8}} will be output. While bc can work with arbitrary precision, it actually defaults to zero digits after the decimal point, so the expression {{code|2/3}} yields {{samp|0}} (results are truncated, not rounded). This can surprise new bc users unaware of this fact. The {{code|-l}} option to bc sets the default ''scale'' (digits after the decimal point) to 20 and adds several additional mathematical functions to the language.
==History==
bc first appeared in [[Version 6 Unix]] in 1975. It was written by [[Lorinda Cherry]] of [[Bell Labs]] as a front end to [[dc (computer program)|dc]], an arbitrary-precision calculator written by [[Robert Morris (cryptographer)|Robert Morris]] and Cherry. dc performed arbitrary-precision computations specified in [[reverse Polish notation]]. bc provided a conventional programming-language interface to the same capability via a simple [[compiler]] (a single [[yacc]] source file comprising a few hundred lines of code), which converted a [[C (programming language)|C]]-like syntax into dc notation and [[Pipeline (Unix)|piped]] the results through dc.
In 1991, [[POSIX]] rigorously defined and standardized bc. Four implementations of this standard survive today: The first is the traditional Unix implementation, a front-end to dc, which survives in Unix and [[Plan 9 from Bell Labs|Plan 9]] systems. The second is the [[free software]] [[GNU]] bc, first released in 1991 by Philip A. Nelson. The GNU implementation has numerous extensions beyond the POSIX standard and is no longer a front-end to dc (it is a [[bytecode interpreter]]). The third is a re-implementation by [[OpenBSD]] in 2003. The fourth is an independent implementation by Gavin Howard<ref name=":1">[https://git.gavinhoward.com/gavin/bc An implementation of Unix dc and POSIX bc with GNU and BSD extensions]</ref> that is included in [[Android (operating system)]],<ref name=":2">{{Cite web|url=https://android.googlesource.com/platform/system/core/+/master/shell_and_utilities/README.md|title=Android's shell and utilities|website=android.googlesource.com}}</ref><ref name=":3">{{cite web | url=https://android.googlesource.com/platform/external/bc/ | title=Platform/External/Bc - Git at Google }}</ref> [[FreeBSD]] as of 13.3-RELEASE,<ref name=":4">{{Cite web|url=https://forums.freebsd.org/threads/freebsd-13-3-whats-new-and-how-did-we-get-here.92596/|title=FreeBSD 13.3: What's new, and how did we get here?|date=March 5, 2024|website=The FreeBSD Forums}}</ref><ref name=":5">{{Cite web|url=https://man.freebsd.org/cgi/man.cgi?query=bc&apropos=0&sektion=1&manpath=FreeBSD+14.1-RELEASE&arch=default&format=html|title=bc(1)|website=man.freebsd.org}}</ref><ref name=":6">{{Cite web|url=https://cgit.freebsd.org/src/tree/contrib/bc|title=bc « contrib - src - FreeBSD source tree|website=cgit.freebsd.org}}</ref> and [[macOS]] as of 13.0.<ref name=":7">{{Cite web|url=https://opensource.apple.com/releases/|title=Apple Open Source|website=opensource.apple.com}}</ref><ref name=":8">{{Cite web|url=https://github.com/apple-oss-distributions/bc/tree/main/bc|title=bc/bc at main · apple-oss-distributions/bc|website=GitHub}}</ref><ref name=":9">{{cite web | url=https://gavinhoward.com/2023/02/my-code-conquered-another-os/ | title=My Code Conquered Another OS! | Gavin D. Howard }}</ref>
==Implementations==
===POSIX bc===
The POSIX standardized bc language is traditionally written as a program in the [[dc (computer program)|dc]] programming language to provide a higher level of access to the features of the dc language without the complexities of dc's terse syntax.
In this form, the bc language contains single-letter [[variable (programming)|variable]], [[array data structure|array]] and [[function (programming)|function]] names and most standard arithmetic operators, as well as the familiar [[control-flow]] constructs (<code>'''if('''cond''')'''...</code>, <code>'''while('''cond''')'''...</code> and <code>'''for('''init''';'''cond''';'''inc''')'''...</code>) from C. Unlike C, an '''<code>if</code>''' clause may not be followed by an '''<code>else</code>'''.
Functions are defined using a '''<code>define</code>''' keyword, and values are returned from them using a '''<code>return</code>''' followed by the return value in parentheses. The '''<code>auto</code>''' keyword (optional in C) is used to declare a variable as local to a function.
All numbers and variable contents are [[arbitrary-precision]] numbers whose precision (in decimal places) is determined by the global '''<code>scale</code>''' variable.
The [[radix|numeric base]] of input (in interactive mode), output and program constants may be specified by setting the reserved '''<code>ibase</code>''' (input base) and '''<code>obase</code>''' (output base) variables.
Output is generated by deliberately not assigning the result of a calculation to a variable.
Comments may be added to bc code by use of the C '''<code>/*</code>''' and '''<code>*/</code>''' (start and end comment) symbols.
====Mathematical operators====
Line 199 ⟶ 80:
====="Missing" operators relative to C=====
The [[bitwise operation|bitwise]], [[Boolean logic|
& | ^ && ||
Line 253 ⟶ 134:
===GNU bc===
GNU bc derives from the POSIX standard and includes many
GNU bc variables, arrays and function names may contain more than one character, some more operators have been included from C, and notably, an '''<code>if</code>''' clause may be followed by an '''<code>else</code>'''.
Line 321 ⟶ 202:
</syntaxhighlight>
With some necessary changes to accommodate bc's different syntax, and
<syntaxhighlight lang="bc">
Line 365 ⟶ 246:
* [https://web.archive.org/web/20090501204511/http://manpages.ubuntu.com/manpages/jaunty/en/man1/bc.1posix.html POSIX bc manual page]
* {{man|1|bc|Plan 9}}
* [http://plan9.bell-labs.com/7thEdMan/vol2/bc 7th Edition Unix bc manual page] {{Webarchive|url=https://web.archive.org/web/20061008193920/http://plan9.bell-labs.com/7thEdMan/vol2/bc |date=2006-10-08 }}
* [http://compilers.iecc.com/comparch/article/95-09-015 A comp.compilers article on the design and implementation of C-bc]
* [http://minnie.tuhs.org/cgi-bin/utree.pl?file=V6/usr/source/s1/bc.y 6th Edition Unix bc source code], the first release of bc, from May 1975, compiling bc syntax into dc syntax
Line 377 ⟶ 258:
* [https://www.gnu.org/software/bc/ GNU bc] (and an [http://alpha.gnu.org/gnu/bc/ alpha version]) from the Free Software Foundation
* [https://web.archive.org/web/20160222234922/http://gnuwin32.sourceforge.net/packages/bc.htm bc for Windows] from [[GnuWin32]]
* [https://
* [https://web.archive.org/web/20160304084708/http://x-bc.sourceforge.net/ X-bc] - A Graphical User Interface to bc
** [https://web.archive.org/web/20160304092132/http://x-bc.sourceforge.net/extensions_bc.html extensions.bc] - contains functions of trigonometry, exponential functions, functions of number theory and some mathematical constants
|