Bc programming language

bc is "an arbitrary precision calculator language" with syntax similar to the C programming language. It is generally used by typing the command bc on a Unix command prompt and entering a mathematical expression, such as (1 + 3) * 2, whereupon 8 will be immediately outputted.

There are currently two main dialects: the rigorously defined POSIX bc, and its direct descendant, the much expanded GNU bc.

Both forms of bc can be executed as either a mathematical scripting language or as an interactive mathematical shell.

Contents

POSIX bc

The POSIX standardised bc language is traditionally written as a program in the 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, array and function names and most standard arithmetic operators as well as the familiar control flow constructs, (if(cond)..., while(cond)... and for(init;cond;inc)...) from C. Unlike C, an if clause may not be followed by an else.

Functions are defined using a define keyword and values are returned from them using a return followed by the return value in parentheses. The auto keyword (optional in C) is used to declare a variable as local to a function.

All numbers and variable contents are fixed precision floating-point numbers whose precision (in decimal places) is determined by the global scale variable.

The numeric base of input (in interactive mode), output and program constants may be specified by setting the reserved ibase (input base) and obase (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 /* and */ (start and end comment) symbols.

Mathematical Operators

Exactly as C

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

+     -     *     /

+=    -=    *=    /=

++    --    <     >
==    !=    <=    >=
( )   [ ]   { }

Similar to C

The modulus operators:

%     %=

... behave exactly like their C counterparts only when the global scale variable is set to 0, i.e. all calculations are integer-only. When scale is greater than 0 the modulus is calculated relative to the smallest positive value greater than zero.

Only Resembling C

The operators:

^     ^=

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

'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. Others functions are available in an external standard library.

Standard Library Functions

bc's standard library contains functions for calculating sine, cosine, arctangent, natural logarithm, the exponential function and the two parameter Bessel function J.

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:

&&     ||      !

... and 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) ) )
}

An equivalent 'Power' Function in GNU bc

# A function to return the integer part of a number
define int(number) {
   auto oldscale
   oldscale = scale
   scale = 0
    number /= 1 /* round number down */
   scale = oldscale
   return number
}

# Use the fact that number^exponent == e^(exponent*log(number))
define power(number,exponent) {
   if (exponent == int(exponent)) {
      return number ^ exponent 
   } else {
      return e( exponent * l(number) )
   }
}

See also

References

ru:bc zh:Bc (Unix)

Navigation

  • Art and Cultures
    • Art (https://academickids.com/encyclopedia/index.php/Art)
    • Architecture (https://academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (https://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (https://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools