FACTOID # 109: What is in a name? More than 90% of people in Bhutan, Burundi and Burkina Faso are involved in agriculture.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


Encyclopedia > 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 outputted. C is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. ... Filiation of Unix and Unix-like systems Unix (officially trademarked as UNIX®) is a computer operating system originally developed in the 1960s and 1970s by a group of AT&T employees at Bell Labs including Ken Thompson, Dennis Ritchie and Douglas McIlroy. ... Command Prompt on Windows XP. A command prompt (or just prompt) is a character or string of characters used in a command line interface to indicate that the computer is ready to accept typed input. ...


There are currently two main dialects: the rigorously defined POSIX bc, and its direct descendant, the much expanded GNU bc. POSIX or Portable Operating System Interface[1] is the collective name of a family of related standards specified by the IEEE to define the application programming interface (API) for software compatible with variants of the Unix operating system. ... GNU (pronounced ) is a computer operating system - consisting of a kernel, libraries, system utilities, compilers, and end-user application software - composed entirely of free software. ...


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


Though not as well-known, a third dialect, C-BC, also exists, providing a much more expanded version of bc with the incorporation of most of C's syntax for expressions and statements and a system of types.

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. From the GNU Project dc man page: Dc is a reverse-polish desk calculator which supports unlimited precision arithmetic. It is one of the oldest Unix utilities, predating even the invention of the C programming language; like other utilities of that vintage, it has a powerful set of features but...


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. In computer science and mathematics, a variable (IPA pronunciation: ) (sometimes called a pronumeral) is a symbolic representation denoting a quantity or expression. ... This article or section does not cite its references or sources. ... In computer science, a subroutine (function, procedure, or subprogram) is a sequence of code which performs a specific task, as part of a larger program, and is grouped as one, or more, statement blocks; such code is sometimes collected into software libraries. ... In computer science control flow (or alternatively, flow of control) refers to the order in which the individual statements or instructions of an imperative program are performed or executed. ...


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. In mathematics, the base or radix is the number of various unique symbols (digits), including zero, that a positional numeral system uses to represent numbers in a given counting system. ...



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: This article is about operators in mathematics, for other kinds of operators see operator (disambiguation). ...

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

Similar to C

The modulus operators: Mathematical meanings Especially in British/European usage, the modulus of a number is its absolute value. ...

 % %=  

... 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. For the corresponding concept in combinational logic, see XOR gate. ...


'Missing' operators relative to C

The bitwise, boolean and conditional operators: In computer programming, a bitwise operation operates on one or two bit patterns or binary numerals at the level of their individual bits. ... The adjective Boolean [], coined in honor of George Boole, is used in many contexts: An evaluation that results in either of the truth values true or false. A Boolean value is a truth value, either true or false, often coded 1 and 0, respectively. ... Look up conditional in Wiktionary, the free dictionary. ...

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

... 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. In mathematics, a square root of a number x is a number r such that , or in words, a number r whose square (the result of multiplying the number by itself) is x. ...


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. Most standard mathematical functions (including the other inverse trigonometric functions) can be constructed using these. See external links for implementations of many other functions. In mathematics, the trigonometric functions are functions of an angle, important when studying triangles and modeling periodic phenomena. ... In mathematics, the trigonometric functions are functions of an angle, important when studying triangles and modeling periodic phenomena. ... In mathematics, the trigonometric functions are functions of an angle, important when studying triangles and modeling periodic phenomena. ... The natural logarithm, formerly known as the hyperbolic logarithm, is the logarithm to the base e, where e is equal to 2. ... The exponential function is one of the most important functions in mathematics. ... In mathematics, Bessel functions, first defined by the mathematician Daniel Bernoulli and generalized by Friedrich Bessel, are canonical solutions y(x) of Bessels differential equation: for an arbitrary real or complex number α. The most common and important special case is where α is an integer n, then α is referred to...


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: In logical calculus, logical operators or logical connectors serve to connect statements into more complicated compound statements. ...

 && || !  

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

Calculating Pi to 1000 places

 $ bc -l -q scale = 1000; a(1)*4 

See also

C is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. ... From the GNU Project dc man page: Dc is a reverse-polish desk calculator which supports unlimited precision arithmetic. It is one of the oldest Unix utilities, predating even the invention of the C programming language; like other utilities of that vintage, it has a powerful set of features but...

References

The Single UNIX Specification (SUS) is the collective name of a family of standards for computer operating systems to qualify for the name Unix. The SUS is developed and maintained by the Austin Group, based on earlier work by the IEEE and The Open Group. ... This article or section reads like an advertisement. ...

External links

  • Dittmer, I. 1993. Error in Unix commands dc and bc for multiple-precision-arithmetic. SIGNUM Newsl. 28, 2 (Apr. 1993), 8–11.
  • Online version of GNU bc
  • Collection of useful GNU bc functions
  • GNU bc from the Free Software Foundation

  Results from FactBites:
 
Bc programming language - Wikipedia, the free encyclopedia (797 words)
bc is "an arbitrary precision calculator language" with syntax similar to the C programming language.
Both forms of bc can be executed as either a mathematical scripting language or as an interactive mathematical shell.
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.
  More results at FactBites »


 
 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms, 1022, m