bc
on a Unix command prompt and entering a mathematical expression, such as (1 + 3) * 2, whereupon 8 will be output. While bc can work with arbitrary precision, it actually defaults to zero digits after the decimal point - so the expression 2/3 yields 0. This can surprise new bc users unaware of this fact. The "-l" option to bc sets the default scale (digits after the decimal point) to 20, and adds several additional mathematical functions to the language.POSIX BC
The POSIX standardized 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 arbitrary precision 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.
Exactly as C
The following POSIX bc operators behave exactly like their C counterparts:
+ - * /
+= -= *= /=
++ -- < >
== != <= >=
( ) [ ] { }
The modulus operators:
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:
& | ^ && ||
&= |= ^= &&= ||=
<< >>
<<= >>=
?:
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 parameterBessel 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 -l option changes the scale to 20 (source), so things such as modulo may work unexpectedly. For example, write "bc -l" and then the command "print 3%2" outputs 0, instead of 1. But if you do it like "bc -l", "scale=0" and then the command "print 3%2" has 1 as output.
Source : http://en.wikipedia.org/wiki/Bc_programming_language
Source : http://en.wikipedia.org/wiki/Bc_programming_language
No comments:
Post a Comment