Content deleted Content added
No edit summary Tags: Reverted Visual edit |
m Reverted edits by 46.121.148.242 (talk) to last version by Ghettoblaster |
||
Line 17:
| website =
}}
'''bc''', for ''basic calculator'' (often referred to as ''bench calculator''), is "''an [[Arbitrary-precision arithmetic|arbitrary-precision]] calculator language''" with syntax similar to the [[C (programming language)|C programming language]]. bc is typically used as either a mathematical scripting language or as an interactive mathematical shell.
==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. Three 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.
==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 [[base (exponentiation)|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====
|