bc (programming language)

This is an old revision of this page, as edited by 46.121.148.242 (talk) at 22:35, 19 May 2022. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

because

bc
Developer(s)Robert Morris and Lorinda Cherry of Bell Labs
Initial release1975, 49–50 years ago
Operating systemUnix, Unix-like, Plan 9, FreeDOS
PlatformCross-platform
TypeCommand

Overview

Bc stands for "because" or "before count"

History

bc t might seem crazy what I am 'bout to say

Sunshine she's here, you can take a break

I'm a hot air balloon that could go to space

With the air, like I don't care, baby by the way

Huh (BC I'm happy)

Clap along if you feel like a room without a roof

(BC I'm happy)

Clap along if you feel like happiness is the truth

(BC I'm happy)

Clap along if you know what happiness is to you

(BC I'm happy)

Clap along if you feel like that's what you wanna do

Here come bad news talking this and that (Yeah)

Well give me all you got, don't hold back (Yeah)

Well I should probably warn you I'll be just fine (Yeah)

No offence to you don't waste your time

Here's why

Clap along if you feel like a room without a roof

(BC I'm happy)

Clap along if you feel like happiness is the truth

(BC I'm happy)

Clap along if you know what happiness is to you

(BC I'm happy)

Clap along if you feel like that's what you wanna do

Uh, bring me down

Can't nothing, bring me down

My level's too high to bring me down

Can't nothing, bring me down, I said

Bring me down, can't nothing

Bring me down

My level's too high to bring me down

Can't nothing, bring me down, I said

Clap along if you feel like a room without a roof

(BC I'm happy)

Clap along if you feel like happiness is the truth

(BCI 'm happy)

Clap along if you know what happiness is to you

(BC I'm happy)

Clap along if you feel like that's what you wanna do

Clap along if you feel like a room without a roof

(Because I'm happy)

Clap along if you feel like happiness is the truth

(Because I'm happy)

Clap along if you know what happiness is to you

(Because I'm happy)

Clap along if you feel like that's what you wanna do

Uh, bring me down (Happy, happy, happy, happy)

Can't nothing (Happy, happy, happy, happy)

Bring me down, my level's too high

To bring me down (Happy, happy, happy, happy)

Can't nothing (Happy, happy, happy, happy)

Bring me down, I said

Clap along if you feel like a room without a roof

(Because I'm happy)

Clap along if you feel like happiness is the truth

(Because I'm happy)

Clap along if you know what happiness is to you (ayy, ayy, ayy)

(Because I'm happy)

Clap along if you feel like that's what you wanna do

Clap along if you feel like a room without a roof

(Because I'm happy)

Clap along if you feel like happiness is the truth

(Because I'm happy)

Clap along if you know what happiness is to you (hey)

(Because I'm happy)

Clap along if you feel like that's what you wanna do

Come on

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 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

"bc i asked you to"

Mathematical operators

Exactly as C

The following POSIX bc operators behave exactly like their C counterparts:

+     -     *     /
+=    -=    *=    /=
++    --    <     >
==    !=    <=    >=
( )   [ ]   { }
Similar to C

The modulus operators, % and %= behave exactly like their C counterparts only when the global scale variable is set to 0, i.e. all calculations are integer-only. Otherwise the computation is done with the appropriate scale. a%b is defined as a-(a/b)*b. Examples:

$ bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
scale=0; 5%3
2
scale=1; 5%3
.2
scale=20; 5%3
.00000000000000000002
Conflicting with C

The operators

^     ^=

superficially resemble the C bitwise exclusive-or operators, but are in fact the bc integer exponentiation operators.

Of particular note, the use of the ^ operator with negative numbers does not follow the C operator precedence. -2^2 gives the answer of 4 under bc rather than −4.

"Missing" operators relative to C

The bitwise, boolean and conditional operators:

&     |     ^     &&    ||
&=    |=    ^=    &&=   ||=
<<    >>
<<=   >>=
?:

are not available in POSIX bc.

Built-in functions

The sqrt() function for calculating square roots is POSIX bc's only built-in mathematical function. Other functions are available in an external standard library.

The scale() function for determining the precision (as with the scale variable) of its argument and the length() function for determining the number of significant decimal digits in its argument are also built-in.

Standard library functions

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.

The bc standard library[1]
bc command Function Description
s(x) Sine Takes x, an angle in radians
c(x) Cosine Takes x, an angle in radians
a(x) Arctangent Returns radians
l(x) Natural logarithm
e(x) Exponential function
j(n,x) Bessel function Returns the order-n Bessel function of x.

The -l option changes the scale to 20,[1] so things such as modulo may work unexpectedly. For example, writing bc -l and then the command print 3%2 outputs 0. But writing scale=0 after bc -l and then the command print 3%2 will output 1.

Plan 9 bc

Plan 9 bc is identical to POSIX bc but for an additional print statement.

GNU bc

GNU bc derives from the POSIX standard and includes many enhancements. 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 if clause may be followed by an else.

Output is achieved either by deliberately not assigning a result of a calculation to a variable (the POSIX way) or by using the added print statement.

Furthermore, a read statement allows the interactive input of a number into a running calculation.

In addition to C-style comments, a # character will cause everything after it until the next new-line to be ignored.

The value of the last calculation is always stored within the additional built-in last variable.

Extra operators

The following logical operators are additional to those in POSIX bc:

&&     ||      !

They are available for use in conditional statements (such as within an if statement). Note, however, that there are still no equivalent bitwise or assignment operations.

Functions

All functions available in GNU bc are inherited from POSIX. No further functions are provided as standard with the GNU distribution.

Example code

Since the bc ^ operator only allows an integer power to its right, one of the first functions a bc user might write is a power function with a floating-point exponent. Both of the below assume the standard library has been included:

A "power" function in POSIX bc

 /* A function to return the integer part of x */
 define i(x) {
    auto s
    s = scale
    scale = 0
    x /= 1   /* round x down */
    scale = s
    return (x)
 }

 /* Use the fact that x^y == e^(y*log(x)) */
 define p(x,y) {
    if (y == i(y)) {
       return (x ^ y)
    }
    return ( e( y * l(x) ) )
 }

Calculating π to 10000 digits

Calculate pi using the builtin arctangent function, a():

$ bc -lq
scale=10000
4*a(1) # The atan of 1 is 45 degrees, which is pi/4 in radians.
       # This may take several minutes to calculate.

A translated C function

Because the syntax of bc is similar to that of C, published numerical functions written in C can often be translated into bc quite easily, which immediately provides the arbitrary precision of bc. For example, in the Journal of Statistical Software (July 2004, Volume 11, Issue 5), George Marsaglia published the following C code for the cumulative normal distribution:

double Phi(double x)
{
    long double s=x,t=0,b=x,q=x*x,i=1;
    while(s!=t)
        s=(t=s)+(b*=q/(i+=2));
    return .5+s*exp(-.5*q-.91893853320467274178L);
}

With some necessary changes to accommodate bc's different syntax, and realizing that the constant "0.9189..." is actually log(2*PI)/2, this can be translated to the following GNU bc code:

define phi(x) {
    auto s,t,b,q,i,const
    s=x; t=0; b=x; q=x*x; i=1
    while(s!=t)
        s=(t=s)+(b*=q/(i+=2))
    const=0.5*l(8*a(1))   # 0.91893...
    return .5+s*e(-.5*q-const)
}

Using bc in shell scripts

bc can be used non-interactively, with input through a pipe. This is useful inside shell scripts. For example:

$ result=$(echo "scale=2; 5 * 7 /3;" | bc)
$ echo $result
11.66

In contrast, note that the bash shell only performs integer arithmetic, e.g.:

$ result=$((5 * 7 /3))
$ echo $result
11

One can also use the here-string idiom (in bash, ksh, csh):

$ bc -l <<< "5*7/3"
11.66666666666666666666

See also

References

  1. ^ a b bc: arbitrary-precision arithmetic language – Shell and Utilities Reference, The Single UNIX Specification, Version 5 from The Open Group