bc (Unix)

from Wikipedia, the free encyclopedia

bc (short for basic calculator ) is a numerical programming language that is based on the syntax of C , as well as an interpreter standardized by the POSIX standard , which implements this programming language and whose existence is prescribed in the standard-compliant operating systems. The distinguishing feature of is the ability to calculate with floating point numbers of (almost) any precision ; depending on the version, the limit is at least several thousand digits, but mostly several million to billions. bc

Working method

The interpreter follows the usual requirements for utilities , the so-called utility syntax guidelines, as defined by the POSIX standard . Either (text) files or input on are <stdin>expected as input . In any case <stdout>, the aim of the output is to follow up error messages <stderr>.

bcdoes not use any floating point units available from a computer, but only works internally with integers ; the floating point capability is established by software . Only in this way can language achieve this high level of accuracy that is independent of the hardware available. However, bcdue to the principle involved, it is much slower than software that uses these facilities.

Not only can the calculation accuracy to be achieved be specified, but it bcis also able to work with any (numerical) bases and to convert them into one another. The usual notation is used up to base 16 and digits beyond 9 are represented by the letters A to F. The digits in bases greater than 16, on the other hand, are represented as numbers consisting of several (decimal) individual digits, which are separated by a separator (usually a blank ). Two-digit decimal numbers are used for bases 17-100, three-digit decimal numbers for bases 101-1000, and so on. See also the application examples below.

The structure of the bclanguage standardized by POSIX is based on C and is also specified in the same document (in the form of an LL1 grammar ) (under Extended Description ). It bcinherently contains only a few basic functions such as the exponential function , the trigonometric functions and the natural logarithm . However, you can define further functions yourself.

variants

GNU bc

GNU - bcis a replica of POSIX - bcbut has numerous changes compared to the standard. Unlike some other variants, it is written from scratch in C and is not based on dc . The GNU variant extends the POSIX variant with additional functions.

The differences include expanded options for naming variables, arrays, and functions, an expanded if statement, and more.

Examples

The following examples are generated with a POSIX bcunder AIX (Version 7.1.5, TL 1). Issues are shown in bold below :

Calculations

This example shows how only a function is defined, the nonrekursiv the Faculty calculated. Then this function is called to 10!output. The example is taken from the man page for bcon AIX.

user@system $ bc
define f(n) {
auto i, r;
r = 1;
for (i=2; i<=n; i++) r =* i;
return (r);
}
f(10)
3628800

Change and conversion of different bases

bcdistinguishes between the basis for the input ( ibase ) and the output ( obase ). The representation of the number 1024 (decimal) for bases 25 and 125 is:

user@system $ bc
obase=25
1024
 01 15 24
obase=125
1024
 008 024

Web links

Individual evidence

  1. a b bc specification of the POSIX standard. Retrieved April 25, 2019 .
  2. ^ Utility Syntax Guidelines. Retrieved April 25, 2019 .