![]() |
|
|
| |
|
||||
bc is "an arbitrary precision calculator language" with syntax similar to the C programming language. It is generally used by typing the command There are currently two main dialects: the rigorously defined POSIX bc, and its direct descendent, the much expanded GNU bc. Both forms of bc can be executed as either a mathematical scripting language or as an interactive mathematical shell.
POSIX bcThe 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, ( Functions are defined using a All numbers and variable contents are fixed precision floating-point numbers whose precision (in decimal places) is determined by the global The numeric base of input (in interactive mode), output and program constants may be specified by setting the reserved
Comments may be added to bc code by use of the C Mathematical OperatorsExactly as CThe following POSIX bc operators behave exactly like their C counterparts: + - * / += -= *= /= ++ -- < > == != <= >= ( ) [ ] { }
Similar to CThe modulus operators: % %= ... behave exactly like their C counterparts only when the global Only Resembling CThe operators: ^ ^= ... resemble the C bitwise exclusive-or operators, but are in fact the bc integer exponentiation operators. 'Missing' Operators Relative to CThe bitwise, boolean and conditional operators: & | ^ && || ^^ &= |= ^= &&= ||= ^^= << >> <<= >>= ?: ... are not available in POSIX bc. Built-in FunctionsThe Standard Library Functionsbc's standard library contains functions for calculating sine, cosine, arctangent, natural logarithm, the exponential function and the two parameter Bessel function J. GNU bcGNU 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 Output is achieved either by deliberately not assigning a result of a calculation to a variable (the POSIX way) or by using the added Furthermore, a In addition to C-style comments, a The value of the last calculation is always stored within the additional built-in Extra OperatorsThe following logical operators are additional to those in POSIX bc: && || ! ... and are available for use in conditional statements (such as within an FunctionsAll functions available in GNU bc are inherited from POSIX. No further functions are provided as standard with the GNU distribution. Example CodeSince the bc 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 alsoReferences
|
||
|
|
|
|
|
|
Copyright 2008 WordIQ.com - Privacy Policy
::
Terms of Use
:: Contact Us
:: About Us This article is licensed under the GNU Free Documentation License. It uses material from the Wikipedia article "Bc programming language". |