manpagez: man pages & more
info bc
 Home | html | info | man
 [ < ] [ > ] [ << ] [ Up ] [ >> ] [Top] [Contents] [Index] [ ? ]

# 6. Examples

In /bin/sh, the following will assign the value of "pi" to the shell variable pi.

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

The following is the definition of the exponential function used in the math library. This function is written in POSIX `bc`.

 ```scale = 20 /* 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 } } ```

The following is code that uses the extended features of `bc` to implement a simple program for calculating checkbook balances. This program is best kept in a file so that it can be used many times without having to retype it at every use.

 ```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 ```

The following is the definition of the recursive factorial function.

 ```define f (x) { if (x <= 1) return (1); return (f(x-1) * x); } ```

```© manpagez.com 2000-2021
Individual documents may contain additional copyright information.
```