Bc (programming language): Difference between revisions

Content deleted Content added
Undid revision 929486600 by 185.164.138.19 (talk) that's why the heading includes "Missing"
there is a language, but's primarily a program, a tool, software
 
(42 intermediate revisions by 28 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}}
{{more footnotes|date=June 2013}}
{{lowercase|title=bc programming language}}
{{Infobox Software software
| name = bc
| logo =
Line 7:
| screenshot size =
| caption =
| developer = [[Robert Morris (cryptographer)|Robert Morris]] and [[Lorinda Cherry]] of [[Bell Labs]], Philip A. Nelson
| released = {{Release year|df=yes|1975}}
| latest release version =
| latest release date =
| operating system = [[Unix]], [[Unix-like]], operating[[Plan systems9 from Bell Labs|Plan 9]], [[FreeDOS]]
| platform = [[Cross-platform]]
| genre = [[Command (computing)|Command]]
| license =
| website =
}}
'''bc''', for ''basic calculator'' (often referred to as ''bench calculator''), is "''an [[Arbitrary-precision arithmetic|arbitrary-precision]] [[math]]ematical [[calculator]] language''"[[computer program|program]] with syntaxan input language similar to the [[C (programming language)|C programming language]]. bcIt issupports typicallyboth usedinteractive, as[[command-line eitherinterface|command-line]] a[[user-interface]] mathematical scripting language or as an interactiveand mathematicalscript shellprocessing.
 
==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. andIt was written by [[Robert Morris (cryptographer)|Robert Morris]] and [[Lorinda Cherry]] of [[Bell Labs]]. bcas wasa precededfront byend to [[dc (computer program)|dc]], an earlier arbitrary-precision calculator written by the[[Robert sameMorris (cryptographer)|Robert authorsMorris]] and Cherry. dc could doperformed arbitrary-precision calculations,computations butspecified itsin [[reverse Polish notation]] (RPN) syntax was inconvenient for users, and therefore. bc wasprovided writtena asconventional a frontprogramming-endlanguage interface to dc.the bcsame wascapability avia verya simple [[compiler]] (a single [[yacc]] source file withcomprising a few hundred lines of code), which converted thea new,[[C (programming language)|C]]-like, bc syntax into dc's [[postfix notation]] and [[Pipeline (Unix)|piped]] the results through dc.
 
In 1991, [[POSIX]] rigorously defined and standardized bc. TwoFour 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! &#124; Gavin D. Howard }}</ref>
 
==Implementations==
Line 36 ⟶ 37:
All numbers and variable contents are [[arbitrary-precision]] numbers whose precision (in decimal places) is determined by the global '''<code>scale</code>''' variable.
 
The [[base (exponentiation)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.
Line 55 ⟶ 56:
=====Similar to C=====
The [[Modulus operator|modulus]] operators, <code>%</code> and <code>%=</code> behave exactly like their C counterparts only when the global '''<code>scale</code>''' variable is set to 0, i.e. all calculations are integer-only. Otherwise the computation is done with the appropriate scale. <code>a%b</code> is defined as <code>a-(a/b)*b</code>. Examples:
<sourcesyntaxhighlight lang="console" highlight="6,8,10">
$ bc
bc 1.06
Line 67 ⟶ 68:
scale=20; 5%3
.00000000000000000002
</syntaxhighlight>
</source>
 
=====Conflicting with C=====
Line 79 ⟶ 80:
 
====="Missing" operators relative to C=====
The [[bitwise operation|bitwise]], [[Boolean logic|booleanBoolean]] and [[conditional (programming)|conditional]] operators:
 
& | ^ && ||
Line 97 ⟶ 98:
bc's standard math library (defined with the '''-l''' option) contains functions for calculating [[sine]], [[cosine]], [[arctangent]], [[natural logarithm]], the [[exponential function]] and the two parameter [[Bessel function]] ''J''. Most standard mathematical functions (including the other inverse trigonometric functions) can be constructed using these. See external links for implementations of many other functions.
{| class="wikitable"
|+The bc standard library<ref name=":0">{{Cite webman|url=https://www.gnu.org/software/bc/manual/html_mono/bc.html#SEC18cu|title=bc Command Manual|last=NelsonSUS|first=Philiparbitrary-precision A.|date=20arithmetic March 2001|website=|publisher=[[Free Software Foundation]]|access-date=2017-04-20language}}</ref>
!bc command
!Function
Line 130 ⟶ 131:
 
===Plan 9 bc===
[[Plan 9 from Bell Labs|Plan 9]] bc is identical to POSIX bc but for an additional '''<code>print</code>''' statement.
 
===GNU bc===
GNU bc derives from the POSIX standard and includes many enhancementsextensions. It is entirely separate from dc-based implementations of the POSIX standard and is instead written in C. Nevertheless, it is fully backwards compatible as all POSIX bc programs will run unmodified as GNU bc programs.
 
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 179 ⟶ 180:
</syntaxhighlight>
 
===Calculating π to 10000 placesdigits===
Calculate [[pi]] using the builtin [[Inverse trigonometric functions|arctangent]] function, {{mono|a()}}:
<syntaxhighlight lang="console" highlight="3">
Line 201 ⟶ 202:
</syntaxhighlight>
 
With some necessary changes to accommodate bc's different syntax, and realizingnoting that the constant "0.9189..." is actually log(2*PI)/2, this can be translated to the following GNU bc code:
 
<syntaxhighlight lang="bc">
Line 216 ⟶ 217:
==Using bc in shell scripts==
bc can be used non-interactively, with input through a [[Pipeline (Unix)|pipe]]. This is useful inside [[shell script]]s. For example:
<sourcesyntaxhighlight lang="console">
$ result=$(echo "scale=2; 5 * 7 /3;" | bc)
$ echo $result
11.66
</syntaxhighlight>
</source>
In contrast, note that the [[Bash (Unix shell)|bash shell]] only performs integer arithmetic, e.g.:
<sourcesyntaxhighlight lang="console">
$ result=$((5 * 7 /3))
$ echo $result
11
</syntaxhighlight>
</source>
One can also use the [[Here document|here-string]] idiom (in bash, ksh, csh):
<sourcesyntaxhighlight lang="console">
$ bc -l <<< "5*7/3"
11.66666666666666666666
</syntaxhighlight>
</source>
 
==See also==
Line 244 ⟶ 245:
* [https://www.gnu.org/software/bc/manual/html_mono/bc.html GNU bc manual page]
* [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/magic/man2html/1/bc Plan 9 bc manual page]
* [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
* [http://ftp.gnu.org/gnu/bc/ GNU bc source code]
{{Refend}}
 
==External links==
{{Wikibooks|Guide to Unix|Commands}}
* [https://doi.acm.org/10.1145/152923.152925 Dittmer, I. 1993. Error in Unix commands dc and bc for multiple-precision-arithmetic. SIGNUM Newsl. 28, 2 (Apr. 1993), 8&ndash;11.]
* [http://www.phodd.net/cyrek/gnu-bc/ Collection of useful GNU bc functions]
* [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://github.com/gavinhoward/bc Gavin Howard bc] - another open source implementation of bc by Gavin Howard with GNU and BSD extensions
* [https://web.archive.org/web/20160304084708/http://x-bc.sourceforge.net/ X-bc] - A Graphical User Interface to bc
** [http://x-bc.sourceforge.net/extensions_bc.html extensions.bc] - contains functions of trigonometry, exponential functions, functions of number theory and some mathematical constants
** [https://web.archive.org/web/20160304092132/http://x-bc.sourceforge.net/scientific_constants_bcextensions_bc.html scientific_constantsextensions.bc] - contains particlefunctions massesof trigonometry, basicexponential constantsfunctions, such as speedfunctions of lightnumber in the vacuumtheory and thesome gravitationalmathematical constantconstants
** [https://web.archive.org/web/20160304081309/http://x-bc.sourceforge.net/scientific_constants_bc.html scientific_constants.bc] - contains particle masses, basic constants, such as speed of light in the vacuum and the gravitational constant
 
{{Unix commands}}
{{Plan 9 commands}}
 
[[Category:Software calculators]]
Line 268 ⟶ 272:
[[Category:Standard Unix programs]]
[[Category:Unix SUS2008 utilities]]
[[Category:Plan 9 commands]]