Sunday, January 8, 2012

bc

bc  is an arbitrary-precision arithmetic language in unix.
 

Syntax:
bc [-c] [-1] [file...]

bc is an interactive processor for a language that resembles C but providesunlimited precision arithmetic.It takes the input from any of the given files, then reads the standard input. bc is actually a preprocessor for the desk calculator program dc, which it  invokes automatically unless the -c option is present. In this case the dc input is sent to the standard output.

-c: Compile only.The output is sent to the standard output.
-1: Argument stands for the name of an arbitrary precision math library.

The syntax for bc programs is as follows:

here L means the letters a-z

         E means expression

         S means statement

Comments are enclosed in /* and*/

 

Names:

   simple variables:L

   array elements:L[E]

   the words ibase (for input base),  obase( for output base) and  scale

Other operands:

   arbitrarily long numbers with optional sign and decimal point(E)

   sqrt(E)

   length(E) : number of significant decimal digits

   scale(E):   number of digits right of decimal point.

   L(E,...,E)

Operators:

  +-*/%^(% is for remainder and ^ is for power)

   ++,-- (prefix and postfix)

   == <= >= ! < >

   = =+ =- =* =/ =% =^

Statements:

  E

   {S;...;S}

  if(E)S

  while (E)S

  for(E;E;E)S

  null statement

  break

  quit

Function Definitions:

  defineL(L,...,L){

   auto L,..,L

   S;...S

   return(E)

}

Functions in -1 math library:

   s(x): sine

   c(x): cosine

   e(x): exponential

   l(x) :  log

   a(x): arctangent

   j(n,x): Bessel function

All function arguments are passed by value.

The value of  a statement  that is an expression is printed unless the main operator is an assignment. Either semicolons or newlines may separate statements.Assignments to scale influences the number of digits to be retained on arithmetic operations in the manner of dc.Assignments to  ibase  or obase set the input and output number radix respectively.

The same letter may be used as an array, a function and  a simple variable simultaneously. All variables are global to the program. auto variables are pushed down during function calls.When using array  as function arguments, or when defining them as automatic variables,empty square brackets must follow the array name.

The files used for bc command are

/usr/lib/lib.b   mathematical library

/usr/bin/dc     desk calculator proper

Also,

    the bc command does not recognise the logical operators && and ||.
    the for statement must have all three expressions
    the quit statement is interpreter when read not executed.

No comments:

Post a Comment