关于bc的用法

时间:2022-04-28 19:12:47
bc - An arbitrary precision calculator language

 

       -i, --interactive

              Force interactive mode.   交互式对话

#   /* */   注释用

 

A simple variable is just a

              name      单个变量

and an array variable is specified as

              name[expr]  数组

Boolean operations are also legal.

       !expr  The result is 1 if expr is 0.

       expr && expr

              The result is 1 if both expressions are non-zero.

       expr || expr

              The result is 1 if either expression is non-zero.

       length ( expression )

              The value of the length function is the number of significant digits in the expression.

       read ( )

      The  read  function  (an extension) will read a number from the standard input, regardless of where the function occurs.  Beware, this can cause problems with the mixing of data and program in the  standard input. The best use for this function is in a previously written program that needs input from the              user, but never allows program code to be input from the user.  The value of the read function  is  the              number  read  from  the standard input using the current value of the variable ibase for the conversion              base.

 

STATEMENTS

Statements (as in most algebraic languages) provide the sequencing of expression evaluation.

expression

string

print list

{ statement_list }

if ( expression ) statement1 [else statement2]

while ( expression ) statement

              expression1;

              while (expression2) {

                 statement;

                 expression3;

              }

for ( [expression1] ; [expression2] ; [expression3] ) statement

break

continue

halt

return

return ( expression )

FUNCTIONS

define name ( parameters ) { newline

                  auto_list   statement_list }

 

MATH LIBRARY     要调用这些数学函数,使用 �Cl 参数。

       -l, --mathlib

              Define the standard math library.

       s (x)  The sine of x, x is in radians.

       c (x)  The cosine of x, x is in radians.

       a (x)  The arctangent of x, arctangent returns radians.

       l (x)  The natural logarithm of x.

       e (x)  The exponential function of raising e to the value x.

       j (n,x)  The bessel function of integer order n of x.

EXAMPLES

1.     

pi=$(echo "scale=10; 4*a(1)" | bc -l)

2.                   

scale = 20            #  scale 定义 有效小数位数

bc �A�O�H�出整�担�如果要�出小�迭c下位�担�那�N就必�要�绦� scale=number ,那�� number 就是小�迭c位��

              /* Uses the fact that e^x = (e^(x/2))^2

                 When x is small enough, we use the series:

                   e^x = 1 + x + x^2/2! + x^3/3! + ...

              */

              define e(x) {

                auto  a, d, e, f, i, m, v, z

                /* Check the sign of x. */

                if (x<0) {

                  m = 1

                  x = -x

                }

                /* Precondition x. */

                z = scale;

                scale = 4 + z + .44*x;

                while (x > 1) {

                  f += 1;

                  x /= 2;

                }

                /* Initialize the variables. */

                v = 1+x

                a = x

                d = 1

                for (i=2; 1; i++) {

                  e = (a *= x) / (d *= i)

                  if (e == 0) {

                    if (f>0) while (f--)  v = v*v;

                    scale = z

                    if (m) return (1/v);

                    return (v/1);

                  }

                  v += e

                }

              }

3.

scale=2

              print "\nCheck book program!\n"

              print "  Remember, deposits are negative transactions.\n"

              print "  Exit by a 0 transaction.\n\n"

              print "Initial balance? "; bal = read()

              bal /= 1

              print "\n"

              while (1) {

                "current balance = "; bal

                "transaction? "; trans = read()

                if (trans == 0) break;

                bal -= trans

                bal /= 1

              }

              quit

4.            define f (x) {

                if (x <= 1) return (1);

                return (f(x-1) * x);

              }

5.      二、八、十、十六进制转化

[frost:/xin]# echo 'obase=16;ibase=2;1010'|bc

6.         一个递归的应用 IP

#!/usr/bin/bc

 

p=32;

define f(x) {

        if(x<=1) return(p);

p--;

return f(x/2);

}

f(32);

 

后来全用awk 替代了。。。这些东西用的少了

本文出自 “SimpleWorldSimpleLife” 博客,请务必保留此出处http://tanzj.blog.51cto.com/802764/162464