Prev Up Next
Go backward to 4.14 Text processing nroff - manual pages
Go up to 4 Labs
Go forward to 4.16 sed - A Stream Editor

4.15 bc - Extended Precision Calculator

bc can compute to at least 99 places right of the decimal point and at least 4000 to the left. This may not be fast and it may not be useful! But, it can be done. bc uses dc. dc uses reverse polish notation. It can be used from the keyboard. Most people will probably prefer bc.

If an expression does not include an assignment, the results are printed.

bc is a language as well as a desk-top calculator. The syntax is very much like "C". With the following (and more) exceptions:

  1. No semicolon is needed at the end of a line.
  2. Variable names are only a single character.
  3. The define statement is used to define functions.
Other features of the bc program are:

bc, when invoked with the -l option, has a limited math library. When this is used you have 6 less variable names.

It also deals with multiple bases. The input base is stored in ibase. Be cautious setting conversion bases, bc inputs ALL numbers using ibase. The output base is stored in obase. To convert 4521 from base 7 to base 13:
 
  obase = 13
  ibase = 7 
  4521 
  result returned
The fractional precision of the calculation is maintained in "scale". For example:
scale=50  
(This needs to be less than 100 on some systems.) TABER CSC137

Lab 4.15: Exercise for bc:

your name:





  1. What are the first 4 digits and last 4 digits of 512^512?





    How long did it take to compute on your machine? This can be used as a short and simple benchmark for UNIX systems.





    What computer did you use? (If not gort)




  2. What happens if you use an obase of 100?




  3. If scale is set to 75, what are the last 5 digits of the sine of 1.0 radian

    .8414709848...........................................?????





  4. Convert 4BC4A base 13 to base 5. (Make sure that you set the output base first. If not, the new input base will be used when inputting the value for the output base.)




Turn in this page.
Instructor: ltaber@pima.edu** Red's Home page** The Pima College Site

Prev Up Next