|
Packit |
70b277 |
\input texinfo @c -*-texinfo-*-
|
|
Packit |
70b277 |
@c %**start of header
|
|
Packit |
70b277 |
@setfilename bc.info
|
|
Packit |
70b277 |
@settitle bc Command Manual
|
|
Packit |
70b277 |
@c %**end of header
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@include texi-ver.incl
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@c This file has the new style title page commands.
|
|
Packit |
70b277 |
@c Run `makeinfo' rather than `texinfo-format-buffer'.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@smallbook
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@c tex
|
|
Packit |
70b277 |
@c \overfullrule=0pt
|
|
Packit |
70b277 |
@c end tex
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@ifinfo
|
|
Packit |
70b277 |
@direntry
|
|
Packit |
70b277 |
* bc: (bc). An arbitrary precision calculator language.
|
|
Packit |
70b277 |
@end direntry
|
|
Packit |
70b277 |
@end ifinfo
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@titlepage
|
|
Packit |
70b277 |
@title @command{bc}
|
|
Packit |
70b277 |
@subtitle an arbitrary precision calculator language
|
|
Packit |
70b277 |
@subtitle version @value{BC_VERSION}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@author Philip A. Nelson
|
|
Packit |
70b277 |
@page
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
This manual documents @command{bc}, an arbitrary precision calculator language.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
This manual is part of GNU @command{bc}.@*
|
|
Packit |
70b277 |
@sp 4
|
|
Packit |
70b277 |
Copyright (C) 1991, 1992, 1993, 1994, 1997, 2003, 2006 Free Software Foundation, Inc.
|
|
Packit |
70b277 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
Permission is granted to make and distribute verbatim copies of
|
|
Packit |
70b277 |
this manual provided the copyright notice and this permission notice
|
|
Packit |
70b277 |
are preserved on all copies.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@ignore
|
|
Packit |
70b277 |
Permission is granted to process this file through TeX and print the
|
|
Packit |
70b277 |
results, provided the printed document carries copying permission
|
|
Packit |
70b277 |
notice identical to this one except for the removal of this paragraph
|
|
Packit |
70b277 |
(this paragraph not being relevant to the printed manual).
|
|
Packit |
70b277 |
@end ignore
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
Permission is granted to copy and distribute modified versions of this
|
|
Packit |
70b277 |
manual under the conditions for verbatim copying, provided that the entire
|
|
Packit |
70b277 |
resulting derived work is distributed under the terms of a permission
|
|
Packit |
70b277 |
notice identical to this one.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
Permission is granted to copy and distribute translations of this manual
|
|
Packit |
70b277 |
into another language, under the above conditions for modified versions,
|
|
Packit |
70b277 |
except that this permission notice may be stated in a translation approved
|
|
Packit |
70b277 |
by the Foundation.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
You may contact the author by:
|
|
Packit |
70b277 |
e-mail: @email{phil@@cs.wwu.edu}@*
|
|
Packit |
70b277 |
us-mail: Philip A. Nelson@*
|
|
Packit |
70b277 |
Computer Science Department, 9062@*
|
|
Packit |
70b277 |
Western Washington University@*
|
|
Packit |
70b277 |
Bellingham, WA 98226-9062
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@end titlepage
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@node Top, Introduction, (dir), (dir)
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@menu
|
|
Packit |
70b277 |
* Introduction::
|
|
Packit |
70b277 |
* Basic Elements::
|
|
Packit |
70b277 |
* Expressions::
|
|
Packit |
70b277 |
* Statements::
|
|
Packit |
70b277 |
* Functions::
|
|
Packit |
70b277 |
* Examples::
|
|
Packit |
70b277 |
* Readline and Libedit Options::
|
|
Packit |
70b277 |
* Comparison with Other Implementations::
|
|
Packit |
70b277 |
* Limits::
|
|
Packit |
70b277 |
* Environment Variables::
|
|
Packit |
70b277 |
@end menu
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@node Introduction, Basic Elements, Top, Top
|
|
Packit |
70b277 |
@chapter Introduction
|
|
Packit |
70b277 |
@menu
|
|
Packit |
70b277 |
* Description::
|
|
Packit |
70b277 |
* Command Line Options::
|
|
Packit |
70b277 |
@end menu
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@node Description, Command Line Options, Introduction, Introduction
|
|
Packit |
70b277 |
@section Description
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@command{bc} [ -hlwsqv ] [long-options] [ @var{ file ...} ]
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@command{bc} is a language that supports arbitrary precision numbers
|
|
Packit |
70b277 |
with interactive execution of statements. There are some similarities
|
|
Packit |
70b277 |
in the syntax to the C programming language.
|
|
Packit |
70b277 |
A standard math library is available by command line option.
|
|
Packit |
70b277 |
If requested, the math library is defined before processing any files.
|
|
Packit |
70b277 |
@command{bc} starts by processing code from all the files listed
|
|
Packit |
70b277 |
on the command line in the order listed. After all files have been
|
|
Packit |
70b277 |
processed, @command{bc} reads from the standard input. All code is
|
|
Packit |
70b277 |
executed as it is read. (If a file contains a command to halt the
|
|
Packit |
70b277 |
processor, @command{bc} will never read from the standard input.)
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
This version of @command{bc} contains several extensions beyond
|
|
Packit |
70b277 |
traditional @command{bc} implementations and the POSIX draft standard.
|
|
Packit |
70b277 |
Command line options can cause these extensions to print a warning or to
|
|
Packit |
70b277 |
be rejected. This document describes the language accepted by this
|
|
Packit |
70b277 |
processor. Extensions will be identified as such.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
The author would like to thank Steve Sommars
|
|
Packit |
70b277 |
(@email{Steve.Sommars@@att.com}) for his extensive help in testing the
|
|
Packit |
70b277 |
implementation. Many great suggestions were given. This is a much
|
|
Packit |
70b277 |
better product due to his involvement.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
Email bug reports to @email{bug-bc@@gnu.org}. Be sure to include
|
|
Packit |
70b277 |
the word ``bc'' somewhere in the ``Subject:'' field.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@node Command Line Options, Numbers, Description, Introduction
|
|
Packit |
70b277 |
@section Command Line Options
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@command{bc} takes the following options from the command line:
|
|
Packit |
70b277 |
@table @code
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item -h, --help
|
|
Packit |
70b277 |
Print the usage and exit.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item -l, --mathlib
|
|
Packit |
70b277 |
Define the standard math library.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item -w, --warn
|
|
Packit |
70b277 |
Give warnings for extensions to POSIX @command{bc}.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item -s, --standard
|
|
Packit |
70b277 |
Process exactly the POSIX @command{bc} language.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item -q, --quiet
|
|
Packit |
70b277 |
Do not print the normal GNU @command{bc} welcome.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item -v, --version
|
|
Packit |
70b277 |
Print the version number and copyright and quit.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@end table
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@node Basic Elements, Expressions, Introduction, Top
|
|
Packit |
70b277 |
@chapter Basic Elements
|
|
Packit |
70b277 |
@menu
|
|
Packit |
70b277 |
* Numbers::
|
|
Packit |
70b277 |
* Variables::
|
|
Packit |
70b277 |
* Comments::
|
|
Packit |
70b277 |
@end menu
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@node Numbers, Variables, Command Line Options, Basic Elements
|
|
Packit |
70b277 |
@section Numbers
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
The most basic element in @command{bc} is the number. Numbers are
|
|
Packit |
70b277 |
arbitrary precision numbers. This precision is both in the integer
|
|
Packit |
70b277 |
part and the fractional part. All numbers are represented internally
|
|
Packit |
70b277 |
in decimal and all computation is done in decimal. (This version
|
|
Packit |
70b277 |
truncates results from divide and multiply operations.) There are two
|
|
Packit |
70b277 |
attributes of numbers, the length and the scale. The length is the
|
|
Packit |
70b277 |
total number of digits used by @command{bc} to represent a number and
|
|
Packit |
70b277 |
the scale is the total number of decimal digits after the decimal
|
|
Packit |
70b277 |
point. For example, .000001 has a length of 6 and scale of 6, while
|
|
Packit |
70b277 |
1935.000 has a length of 7 and a scale of 3.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@node Variables, Comments, Numbers, Basic Elements
|
|
Packit |
70b277 |
@section Variables
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
Numbers are stored in two types of variables, simple variables and
|
|
Packit |
70b277 |
arrays. Both simple variables and array variables are named. Names
|
|
Packit |
70b277 |
begin with a letter followed by any number of letters, digits and
|
|
Packit |
70b277 |
underscores. All letters must be lower case. (Full alphanumeric
|
|
Packit |
70b277 |
names are an extension. In POSIX @command{bc} all names are a single
|
|
Packit |
70b277 |
lower case letter.) The type of variable is clear by the context
|
|
Packit |
70b277 |
because all array variable names will be followed by brackets ( [ ] ).
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
There are four special variables, @var{scale}, @var{ibase}, @var{obase}, and
|
|
Packit |
70b277 |
@var{last}. @var{scale} defines how some operations use digits after the
|
|
Packit |
70b277 |
decimal point. The default value of @var{scale} is 0. @var{ibase}
|
|
Packit |
70b277 |
and @var{obase} define the conversion base for input and output
|
|
Packit |
70b277 |
numbers. The default for both input and output is base 10.
|
|
Packit |
70b277 |
@var{last} (an extension) is a variable that has the value of the last
|
|
Packit |
70b277 |
printed number. These will be discussed in further detail where
|
|
Packit |
70b277 |
appropriate. All of these variables may have values assigned to them
|
|
Packit |
70b277 |
as well as used in expressions.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@node Comments, , Variables, Basic Elements
|
|
Packit |
70b277 |
@section Comments
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
Comments in @command{bc} start with the characters @code{/*} and end with
|
|
Packit |
70b277 |
the characters @code{*/}. Comments may start anywhere and appear as a
|
|
Packit |
70b277 |
single space in the input. (This causes comments to delimit other
|
|
Packit |
70b277 |
input items. For example, a comment can not be found in the middle of
|
|
Packit |
70b277 |
a variable name.) Comments include any newlines (end of line) between
|
|
Packit |
70b277 |
the start and the end of the comment.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
To support the use of scripts for @command{bc}, a single line comment has been
|
|
Packit |
70b277 |
added as an extension. A single line comment starts at a @code{#}
|
|
Packit |
70b277 |
character and continues to the next end of the line. The end of line
|
|
Packit |
70b277 |
character is not part of the comment and is processed normally.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@node Expressions, Statements, Basic Elements, Top
|
|
Packit |
70b277 |
@chapter Expressions
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@menu
|
|
Packit |
70b277 |
* About Expressions and Special Variables::
|
|
Packit |
70b277 |
* Basic Expressions::
|
|
Packit |
70b277 |
* Relational Expressions::
|
|
Packit |
70b277 |
* Boolean Expressions::
|
|
Packit |
70b277 |
* Precedence::
|
|
Packit |
70b277 |
* Special Expressions::
|
|
Packit |
70b277 |
@end menu
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@node About Expressions and Special Variables, Basic Expressions, Expressions, Expressions
|
|
Packit |
70b277 |
@section About Expressions and Special Variables
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
The numbers are manipulated by expressions and statements. Since
|
|
Packit |
70b277 |
the language was designed to be interactive, statements and expressions
|
|
Packit |
70b277 |
are executed as soon as possible. There is no main program. Instead,
|
|
Packit |
70b277 |
code is executed as it is encountered. (Functions, discussed in
|
|
Packit |
70b277 |
detail later, are defined when encountered.)
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
A simple expression is just a constant. @command{bc} converts constants
|
|
Packit |
70b277 |
into internal decimal numbers using the current input base, specified by
|
|
Packit |
70b277 |
the variable @var{ibase}. (There is an exception in functions.) The
|
|
Packit |
70b277 |
legal values of @var{ibase} are 2 through 36. (Bases greater than
|
|
Packit |
70b277 |
16 are an extension.) Assigning a value outside
|
|
Packit |
70b277 |
this range to @var{ibase} will result in a value of 2 or 36. Input
|
|
Packit |
70b277 |
numbers may contain the characters 0-9 and A-Z. (Note: They must be
|
|
Packit |
70b277 |
capitals. Lower case letters are variable names.) Single digit numbers
|
|
Packit |
70b277 |
always have the value of the digit regardless of the value of
|
|
Packit |
70b277 |
@var{ibase}. (i.e. A = 10.) For multi-digit numbers, @command{bc}
|
|
Packit |
70b277 |
changes all input digits greater or equal to @var{ibase} to the value of
|
|
Packit |
70b277 |
@var{ibase}-1. This makes the number @code{ZZZ} always be the largest
|
|
Packit |
70b277 |
3 digit number of the input base.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
Full expressions are similar to many other high level languages.
|
|
Packit |
70b277 |
Since there is only one kind of number, there are no rules for mixing
|
|
Packit |
70b277 |
types. Instead, there are rules on the scale of expressions. Every
|
|
Packit |
70b277 |
expression has a scale. This is derived from the scale of original
|
|
Packit |
70b277 |
numbers, the operation performed and in many cases, the value of the
|
|
Packit |
70b277 |
variable @var{scale}. Legal values of the variable @var{scale} are
|
|
Packit |
70b277 |
0 to the maximum number representable by a C integer.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@node Basic Expressions, Relational Expressions, About Expressions and Special Variables, Expressions
|
|
Packit |
70b277 |
@section Basic Expressions
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
In the following descriptions of legal expressions, "expr" refers to a
|
|
Packit |
70b277 |
complete expression and "@var{var}" refers to a simple or an array variable.
|
|
Packit |
70b277 |
A simple variable is just a
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@var{name}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
and an array variable is specified as
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@var{name}[@var{expr}]
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
Unless specifically mentioned the scale of the result is the maximum
|
|
Packit |
70b277 |
scale of the expressions involved.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@table @code
|
|
Packit |
70b277 |
@item - expr
|
|
Packit |
70b277 |
The result is the negation of the expression.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item ++ @var{var}
|
|
Packit |
70b277 |
The variable is incremented by one and the new value is the result of
|
|
Packit |
70b277 |
the expression.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item -- @var{var}
|
|
Packit |
70b277 |
The variable
|
|
Packit |
70b277 |
is decremented by one and the new value is the result of the
|
|
Packit |
70b277 |
expression.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item @var{var} ++
|
|
Packit |
70b277 |
The result of the expression is the value of
|
|
Packit |
70b277 |
the variable and then the variable is incremented by one.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item @var{var} --
|
|
Packit |
70b277 |
The result of the expression is the value of the variable and then
|
|
Packit |
70b277 |
the variable is decremented by one.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item expr + expr
|
|
Packit |
70b277 |
The result of the expression is the sum of the two expressions.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item expr - expr
|
|
Packit |
70b277 |
The result of the expression is the difference of the two expressions.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item expr * expr
|
|
Packit |
70b277 |
The result of the expression is the product of the two expressions.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item expr / expr
|
|
Packit |
70b277 |
The result of the expression is the quotient of the two expressions.
|
|
Packit |
70b277 |
The scale of the result is the value of the variable @code{scale}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item expr % expr
|
|
Packit |
70b277 |
The result of the expression is the "remainder" and it is computed in the
|
|
Packit |
70b277 |
following way. To compute a%b, first a/b is computed to @var{scale}
|
|
Packit |
70b277 |
digits. That result is used to compute a-(a/b)*b to the scale of the
|
|
Packit |
70b277 |
maximum of @var{scale}+scale(b) and scale(a). If @var{scale} is set
|
|
Packit |
70b277 |
to zero and both expressions are integers this expression is the
|
|
Packit |
70b277 |
integer remainder function.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item expr ^ expr
|
|
Packit |
70b277 |
The result of the expression is the value of the first raised to the
|
|
Packit |
70b277 |
second. The second expression must be an integer. (If the second
|
|
Packit |
70b277 |
expression is not an integer, a warning is generated and the
|
|
Packit |
70b277 |
expression is truncated to get an integer value.) The scale of the
|
|
Packit |
70b277 |
result is @var{scale} if the exponent is negative. If the exponent
|
|
Packit |
70b277 |
is positive the scale of the result is the minimum of the scale of the
|
|
Packit |
70b277 |
first expression times the value of the exponent and the maximum of
|
|
Packit |
70b277 |
@var{scale} and the scale of the first expression. (e.g. scale(a^b)
|
|
Packit |
70b277 |
= min(scale(a)*b, max(@var{scale}, scale(a))).) It should be noted
|
|
Packit |
70b277 |
that expr^0 will always return the value of 1.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item ( expr )
|
|
Packit |
70b277 |
This alters the standard precedence to force the evaluation of the
|
|
Packit |
70b277 |
expression.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item @var{var} = expr
|
|
Packit |
70b277 |
The variable is assigned the value of the expression.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item @var{var} <op>= expr
|
|
Packit |
70b277 |
This is equivalent to "@var{var} = @var{var} <op> expr" with the
|
|
Packit |
70b277 |
exception that the "@var{var}" part is evaluated only once. This can
|
|
Packit |
70b277 |
make a difference if "@var{var}" is an array.
|
|
Packit |
70b277 |
@end table
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@node Relational Expressions, Boolean Expressions, Basic Expressions, Expressions
|
|
Packit |
70b277 |
@section Relational Expressions
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
Relational expressions are a special kind of expression that always
|
|
Packit |
70b277 |
evaluate to 0 or 1, 0 if the relation is false and 1 if the relation is
|
|
Packit |
70b277 |
true. These may appear in any legal expression. (POSIX @command{bc}
|
|
Packit |
70b277 |
requires that relational expressions are used only in @code{if},
|
|
Packit |
70b277 |
@code{while}, and @code{for} statements and that only one relational
|
|
Packit |
70b277 |
test may be done in them.) The relational operators are
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@table @code
|
|
Packit |
70b277 |
@item expr1 < expr2
|
|
Packit |
70b277 |
The result is 1 if expr1 is strictly less than expr2.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item expr1 <= expr2
|
|
Packit |
70b277 |
The result is 1 if expr1 is less than or equal to expr2.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item expr1 > expr2
|
|
Packit |
70b277 |
The result is 1 if expr1 is strictly greater than expr2.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item expr1 >= expr2
|
|
Packit |
70b277 |
The result is 1 if expr1 is greater than or equal to expr2.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item expr1 == expr2
|
|
Packit |
70b277 |
The result is 1 if expr1 is equal to expr2.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item expr1 != expr2
|
|
Packit |
70b277 |
The result is 1 if expr1 is not equal to expr2.
|
|
Packit |
70b277 |
@end table
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@node Boolean Expressions, Precedence, Relational Expressions, Expressions
|
|
Packit |
70b277 |
@section Boolean Expressions
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
Boolean operations are also legal. (POSIX @command{bc} does NOT have
|
|
Packit |
70b277 |
boolean operations). The result of all boolean operations are 0 and 1
|
|
Packit |
70b277 |
(for false and true) as in relational expressions. The boolean
|
|
Packit |
70b277 |
operators are:
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@table @code
|
|
Packit |
70b277 |
@item !expr
|
|
Packit |
70b277 |
The result is 1 if expr is 0.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item expr && expr
|
|
Packit |
70b277 |
The result is 1 if both expressions are non-zero.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item expr || expr
|
|
Packit |
70b277 |
The result is 1 if either expression is non-zero.
|
|
Packit |
70b277 |
@end table
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@node Precedence, Special Expressions, Boolean Expressions, Expressions
|
|
Packit |
70b277 |
@section Precedence
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
The expression precedence is as follows: (lowest to highest)
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@example
|
|
Packit |
70b277 |
|| operator, left associative
|
|
Packit |
70b277 |
&& operator, left associative
|
|
Packit |
70b277 |
! operator, nonassociative
|
|
Packit |
70b277 |
Relational operators, left associative
|
|
Packit |
70b277 |
Assignment operator, right associative
|
|
Packit |
70b277 |
+ and - operators, left associative
|
|
Packit |
70b277 |
*, / and % operators, left associative
|
|
Packit |
70b277 |
^ operator, right associative
|
|
Packit |
70b277 |
unary - operator, nonassociative
|
|
Packit |
70b277 |
++ and -- operators, nonassociative
|
|
Packit |
70b277 |
@end example
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
This precedence was chosen so that POSIX compliant @command{bc} programs
|
|
Packit |
70b277 |
will run correctly. This will cause the use of the relational and
|
|
Packit |
70b277 |
logical operators to have some unusual behavior when used with
|
|
Packit |
70b277 |
assignment expressions. Consider the expression:
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@example
|
|
Packit |
70b277 |
a = 3 < 5
|
|
Packit |
70b277 |
@end example
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
Most C programmers would assume this would assign the result of "3 <
|
|
Packit |
70b277 |
5" (the value 1) to the variable "a". What this does in @command{bc} is
|
|
Packit |
70b277 |
assign the value 3 to the variable "a" and then compare 3 to 5. It is
|
|
Packit |
70b277 |
best to use parentheses when using relational and logical operators
|
|
Packit |
70b277 |
with the assignment operators.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@node Special Expressions, , Precedence, Expressions
|
|
Packit |
70b277 |
@section Special Expressions
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
There are a few more special expressions that are provided in
|
|
Packit |
70b277 |
@command{bc}. These have to do with user-defined functions and standard
|
|
Packit |
70b277 |
functions. They all appear as
|
|
Packit |
70b277 |
"@var{name}@code{(}@var{parameters}@code{)}". @xref{Functions}, for
|
|
Packit |
70b277 |
user-defined functions. The standard functions are:
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@table @code
|
|
Packit |
70b277 |
@item length ( @var{expression} )
|
|
Packit |
70b277 |
The value of the length function is the number of significant digits in the
|
|
Packit |
70b277 |
expression.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item read ( )
|
|
Packit |
70b277 |
The @code{read} function (an extension) will read a number from the
|
|
Packit |
70b277 |
standard input, regardless of where the function occurs. Beware, this
|
|
Packit |
70b277 |
can cause problems with the mixing of data and program in the standard
|
|
Packit |
70b277 |
input. The best use for this function is in a previously written
|
|
Packit |
70b277 |
program that needs input from the user, but never allows program code to
|
|
Packit |
70b277 |
be input from the user. The value of the @code{read} function is the
|
|
Packit |
70b277 |
number read from the standard input using the current value of the
|
|
Packit |
70b277 |
variable @var{ibase} for the conversion base.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item scale ( @var{expression} )
|
|
Packit |
70b277 |
The value of the @code{scale} function is the number of digits after the
|
|
Packit |
70b277 |
decimal point in the expression.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item sqrt ( @var{expression} )
|
|
Packit |
70b277 |
The value of the @code{sqrt} function is the square root of the
|
|
Packit |
70b277 |
expression. If the expression is negative, a run time error is
|
|
Packit |
70b277 |
generated.
|
|
Packit |
70b277 |
@end table
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@node Statements, Functions, Expressions, Top
|
|
Packit |
70b277 |
@chapter Statements
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@menu
|
|
Packit |
70b277 |
* Pseudo Statements::
|
|
Packit |
70b277 |
@end menu
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
Statements (as in most algebraic languages) provide the sequencing of
|
|
Packit |
70b277 |
expression evaluation. In @command{bc} statements are executed "as soon
|
|
Packit |
70b277 |
as possible." Execution happens when a newline in encountered and there
|
|
Packit |
70b277 |
is one or more complete statements. Due to this immediate execution,
|
|
Packit |
70b277 |
newlines are very important in @command{bc}. In fact, both a semicolon
|
|
Packit |
70b277 |
and a newline are used as statement separators. An improperly placed
|
|
Packit |
70b277 |
newline will cause a syntax error. Because newlines are statement
|
|
Packit |
70b277 |
separators, it is possible to hide a newline by using the backslash
|
|
Packit |
70b277 |
character. The sequence "\<nl>", where <nl> is the newline appears to
|
|
Packit |
70b277 |
@command{bc} as whitespace instead of a newline. A statement list is a
|
|
Packit |
70b277 |
series of statements separated by semicolons and newlines. The
|
|
Packit |
70b277 |
following is a list of @command{bc} statements and what they do: (Things
|
|
Packit |
70b277 |
enclosed in brackets ( [ ] ) are optional parts of the statement.)
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@table @code
|
|
Packit |
70b277 |
@item @var{expression}
|
|
Packit |
70b277 |
This statement does one of two things. If the expression starts with
|
|
Packit |
70b277 |
"<variable> <assignment> ...", it is considered to be an assignment
|
|
Packit |
70b277 |
statement. If the expression is not an assignment statement, the
|
|
Packit |
70b277 |
expression is evaluated and printed to the output. After the number is
|
|
Packit |
70b277 |
printed, a newline is printed. For example, "a=1" is an assignment
|
|
Packit |
70b277 |
statement and "(a=1)" is an expression that has an embedded assignment.
|
|
Packit |
70b277 |
All numbers that are printed are printed in the base specified by the
|
|
Packit |
70b277 |
variable @var{obase}. The legal values for @var{obase} are 2 through
|
|
Packit |
70b277 |
BC_BASE_MAX (@pxref{Environment Variables}). For bases 2 through 16,
|
|
Packit |
70b277 |
the usual method of writing numbers is used. For bases greater than 16,
|
|
Packit |
70b277 |
@command{bc} uses a multi-character digit method of printing the numbers
|
|
Packit |
70b277 |
where each higher base digit is printed as a base 10 number. The
|
|
Packit |
70b277 |
multi-character digits are separated by spaces. Each digit contains the
|
|
Packit |
70b277 |
number of characters required to represent the base ten value of
|
|
Packit |
70b277 |
"@var{obase} -1". Since numbers are of arbitrary precision, some
|
|
Packit |
70b277 |
numbers may not be printable on a single output line. These long
|
|
Packit |
70b277 |
numbers will be split across lines using the "\" as the last character
|
|
Packit |
70b277 |
on a line. The maximum number of characters printed per line is 70.
|
|
Packit |
70b277 |
Due to the interactive nature of @command{bc}, printing a number causes
|
|
Packit |
70b277 |
the side effect of assigning the printed value to the special variable
|
|
Packit |
70b277 |
@var{last}. This allows the user to recover the last value printed
|
|
Packit |
70b277 |
without having to retype the expression that printed the number.
|
|
Packit |
70b277 |
Assigning to @var{last} is legal and will overwrite the last printed
|
|
Packit |
70b277 |
value with the assigned value. The newly assigned value will remain
|
|
Packit |
70b277 |
until the next number is printed or another value is assigned to
|
|
Packit |
70b277 |
@var{last}. (Some installations may allow the use of a single period
|
|
Packit |
70b277 |
(.) which is not part of a number as a short hand notation for for
|
|
Packit |
70b277 |
@var{last}.)
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item @var{string}
|
|
Packit |
70b277 |
The string is printed to the output. Strings start with a double quote
|
|
Packit |
70b277 |
character and contain all characters until the next double quote character.
|
|
Packit |
70b277 |
All characters are taken literally, including any newline. No newline
|
|
Packit |
70b277 |
character is printed after the string.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item print @var{list}
|
|
Packit |
70b277 |
The @code{print} statement (an extension) provides another method of
|
|
Packit |
70b277 |
output. The @var{list} is a list of strings and expressions separated by
|
|
Packit |
70b277 |
commas. Each string or expression is printed in the order of the list.
|
|
Packit |
70b277 |
No terminating newline is printed. Expressions are evaluated and their
|
|
Packit |
70b277 |
value is printed and assigned to the variable @code{last}. Strings in
|
|
Packit |
70b277 |
the print statement are printed to the output and may contain special
|
|
Packit |
70b277 |
characters. Special characters start with the backslash character (\e).
|
|
Packit |
70b277 |
The special characters recognized by @command{bc} are "a" (alert or
|
|
Packit |
70b277 |
bell), "b" (backspace), "f" (form feed), "n" (newline), "r" (carriage
|
|
Packit |
70b277 |
return), "q" (double quote), "t" (tab), and "\e" (backslash). Any other
|
|
Packit |
70b277 |
character following the backslash will be ignored.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item @{ @var{statement_list} @}
|
|
Packit |
70b277 |
This is the compound statement. It allows multiple statements to be
|
|
Packit |
70b277 |
grouped together for execution.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item if ( @var{expression} ) @var{statement1} [else @var{statement2}]
|
|
Packit |
70b277 |
The if statement evaluates the expression and executes statement1 or
|
|
Packit |
70b277 |
statement2 depending on the value of the expression. If the expression
|
|
Packit |
70b277 |
is non-zero, statement1 is executed. If statement2 is present and
|
|
Packit |
70b277 |
the value of the expression is 0, then statement2 is executed. (The
|
|
Packit |
70b277 |
@code{else} clause is an extension.)
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item while ( @var{expression} ) @var{statement}
|
|
Packit |
70b277 |
The while statement will execute the statement while the expression
|
|
Packit |
70b277 |
is non-zero. It evaluates the expression before each execution of
|
|
Packit |
70b277 |
the statement. Termination of the loop is caused by a zero
|
|
Packit |
70b277 |
expression value or the execution of a @code{break} statement.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item for ( [@var{expression1}] ; [@var{expression2}] ; [@var{expression3}] ) @var{statement}
|
|
Packit |
70b277 |
The @code{for} statement controls repeated execution of the statement.
|
|
Packit |
70b277 |
@var{Expression1} is evaluated before the loop. @var{Expression2} is
|
|
Packit |
70b277 |
evaluated before each execution of the statement. If it is non-zero,
|
|
Packit |
70b277 |
the statement is evaluated. If it is zero, the loop is terminated.
|
|
Packit |
70b277 |
After each execution of the statement, @var{expression3} is evaluated
|
|
Packit |
70b277 |
before the reevaluation of expression2. If @var{expression1} or
|
|
Packit |
70b277 |
@var{expression3} are missing, nothing is evaluated at the point they
|
|
Packit |
70b277 |
would be evaluated. If @var{expression2} is missing, it is the same as
|
|
Packit |
70b277 |
substituting the value 1 for @var{expression2}. (The optional
|
|
Packit |
70b277 |
expressions are an extension. POSIX @command{bc} requires all three
|
|
Packit |
70b277 |
expressions.) The following is equivalent code for the @code{for}
|
|
Packit |
70b277 |
statement:
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@example
|
|
Packit |
70b277 |
expression1;
|
|
Packit |
70b277 |
while (expression2) @{
|
|
Packit |
70b277 |
statement;
|
|
Packit |
70b277 |
expression3;
|
|
Packit |
70b277 |
@}
|
|
Packit |
70b277 |
@end example
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item break
|
|
Packit |
70b277 |
This statement causes a forced exit of the most recent enclosing @code{while}
|
|
Packit |
70b277 |
statement or @code{for} statement.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item continue
|
|
Packit |
70b277 |
The @code{continue} statement (an extension) causes the most recent enclosing
|
|
Packit |
70b277 |
@code{for} statement to start the next iteration.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item halt
|
|
Packit |
70b277 |
The @code{halt} statement (an extension) is an executed statement that
|
|
Packit |
70b277 |
causes the @command{bc} processor to quit only when it is executed. For
|
|
Packit |
70b277 |
example, "if (0 == 1) halt" will not cause @command{bc} to terminate
|
|
Packit |
70b277 |
because the @code{halt} is not executed.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item return
|
|
Packit |
70b277 |
Return the value 0 from a function. (@xref{Functions}.)
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item return ( @var{expression} )
|
|
Packit |
70b277 |
Return the value of the expression from a function. (@xref{Functions}.)
|
|
Packit |
70b277 |
As an extension, the parenthesis are not required.
|
|
Packit |
70b277 |
@end table
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@node Pseudo Statements, , Statements, Statements
|
|
Packit |
70b277 |
@section Pseudo Statements
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
These statements are not statements in the traditional sense. They are
|
|
Packit |
70b277 |
not executed statements. Their function is performed at "compile" time.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@table @code
|
|
Packit |
70b277 |
@item limits
|
|
Packit |
70b277 |
Print the local limits enforced by the local version of @command{bc}. This
|
|
Packit |
70b277 |
is an extension.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item quit
|
|
Packit |
70b277 |
When the @code{quit} statement is read, the @command{bc} processor
|
|
Packit |
70b277 |
is terminated, regardless of where the @code{quit} statement is found. For
|
|
Packit |
70b277 |
example, "if (0 == 1) quit" will cause @command{bc} to terminate.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item warranty
|
|
Packit |
70b277 |
Print a longer warranty notice. This is an extension.
|
|
Packit |
70b277 |
@end table
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@node Functions, Examples, Statements, Top
|
|
Packit |
70b277 |
@chapter Functions
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@menu
|
|
Packit |
70b277 |
* Math Library Functions::
|
|
Packit |
70b277 |
@end menu
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
Functions provide a method of defining a computation that can be
|
|
Packit |
70b277 |
executed later. Functions in @command{bc} always compute a value and
|
|
Packit |
70b277 |
return it to the caller. Function definitions are "dynamic" in the
|
|
Packit |
70b277 |
sense that a function is undefined until a definition is encountered in
|
|
Packit |
70b277 |
the input. That definition is then used until another definition
|
|
Packit |
70b277 |
function for the same name is encountered. The new definition then
|
|
Packit |
70b277 |
replaces the older definition. A function is defined as follows:
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@example
|
|
Packit |
70b277 |
@code{define} @var{name} @code{(} @var{parameters} @code{)} @code{@{} @var{newline}
|
|
Packit |
70b277 |
@var{auto_list statement_list} @code{@}}
|
|
Packit |
70b277 |
@end example
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
A function call is just an expression of the form
|
|
Packit |
70b277 |
"@code{name} @code{(}@var{parameters}@code{)}".
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
Parameters are numbers or arrays (an extension). In the function definition,
|
|
Packit |
70b277 |
zero or more parameters are defined by listing their names separated by
|
|
Packit |
70b277 |
commas. All parameters are call by value parameters.
|
|
Packit |
70b277 |
Arrays are specified in the parameter definition by
|
|
Packit |
70b277 |
the notation "@var{name}@code{[ ]}". In the function call, actual parameters
|
|
Packit |
70b277 |
are full expressions for number parameters. The same notation is used
|
|
Packit |
70b277 |
for passing arrays as for defining array parameters. The named array is
|
|
Packit |
70b277 |
passed by value to the function. Since function definitions are dynamic,
|
|
Packit |
70b277 |
parameter numbers and types are checked when a function is called. Any
|
|
Packit |
70b277 |
mismatch in number or types of parameters will cause a runtime error.
|
|
Packit |
70b277 |
A runtime error will also occur for the call to an undefined function.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
The @var{auto_list} is an optional list of variables that are for
|
|
Packit |
70b277 |
"local" use. The syntax of the auto list (if present) is "@code{auto}
|
|
Packit |
70b277 |
@var{name}, ... ;". (The semicolon is optional.) Each @var{name} is
|
|
Packit |
70b277 |
the name of an auto variable. Arrays may be specified by using the
|
|
Packit |
70b277 |
same notation as used in parameters. These variables have their
|
|
Packit |
70b277 |
values pushed onto a stack at the start of the function. The
|
|
Packit |
70b277 |
variables are then initialized to zero and used throughout the
|
|
Packit |
70b277 |
execution of the function. At function exit, these variables are
|
|
Packit |
70b277 |
popped so that the original value (at the time of the function call)
|
|
Packit |
70b277 |
of these variables are restored. The parameters are really auto
|
|
Packit |
70b277 |
variables that are initialized to a value provided in the function
|
|
Packit |
70b277 |
call.
|
|
Packit |
70b277 |
Auto variables are different than traditional local variables
|
|
Packit |
70b277 |
because if function A calls function B, B may access function
|
|
Packit |
70b277 |
A's auto variables by just using the same name, unless function B has
|
|
Packit |
70b277 |
called them auto variables. Due to the fact that auto variables and
|
|
Packit |
70b277 |
parameters are pushed onto a stack, @command{bc} supports recursive functions.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
The function body is a list of @command{bc} statements. Again, statements
|
|
Packit |
70b277 |
are separated by semicolons or newlines. Return statements cause the
|
|
Packit |
70b277 |
termination of a function and the return of a value. There are two
|
|
Packit |
70b277 |
versions of the return statement. The first form, "@code{return}", returns
|
|
Packit |
70b277 |
the value 0 to the calling expression. The second form,
|
|
Packit |
70b277 |
"@code{return} ( @var{expression} )", computes the value of the expression
|
|
Packit |
70b277 |
and returns that value to the calling expression. There is an implied
|
|
Packit |
70b277 |
"@code{return} (0)" at the end of every function. This allows a function
|
|
Packit |
70b277 |
to terminate and return 0 without an explicit @code{return} statement.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
Functions also change the usage of the variable @var{ibase}. All
|
|
Packit |
70b277 |
constants in the function body will be converted using the value of
|
|
Packit |
70b277 |
@var{ibase} at the time of the function call. Changes of @var{ibase}
|
|
Packit |
70b277 |
will be ignored during the execution of the function except for the
|
|
Packit |
70b277 |
standard function @code{read}, which will always use the current value
|
|
Packit |
70b277 |
of @var{ibase} for conversion of numbers.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
Several extensions have been added to functions. First, the format of
|
|
Packit |
70b277 |
the definition has been slightly relaxed. The standard requires the
|
|
Packit |
70b277 |
opening brace be on the same line as the @code{define} keyword and all
|
|
Packit |
70b277 |
other parts must be on following lines. This version of @command{bc}
|
|
Packit |
70b277 |
will allow any number of newlines before and after the opening brace of
|
|
Packit |
70b277 |
the function. For example, the following definitions are legal.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@example
|
|
Packit |
70b277 |
define d (n) @{ return (2*n); @}
|
|
Packit |
70b277 |
define d (n)
|
|
Packit |
70b277 |
@{ return (2*n); @}
|
|
Packit |
70b277 |
@end example
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
Functions may be defined as @code{void}. A void
|
|
Packit |
70b277 |
funtion returns no value and thus may not be used in any place that needs
|
|
Packit |
70b277 |
a value. A void function does not produce any output when called by itself
|
|
Packit |
70b277 |
on an input line. The key word @code{void} is placed between the key word
|
|
Packit |
70b277 |
@code{define} and the function name. For example, consider the following
|
|
Packit |
70b277 |
session.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@example
|
|
Packit |
70b277 |
define py (y) @{ print "--->", y, "<---", "\n"; @}
|
|
Packit |
70b277 |
define void px (x) @{ print "--->", x, "<---", "\n"; @}
|
|
Packit |
70b277 |
py(1)
|
|
Packit |
70b277 |
--->1<---
|
|
Packit |
70b277 |
0
|
|
Packit |
70b277 |
px(1)
|
|
Packit |
70b277 |
--->1<---
|
|
Packit |
70b277 |
@end example
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
Since @code{py} is not a void function, the call of @code{py(1)} prints
|
|
Packit |
70b277 |
the desired output and then prints a second line that is the value of
|
|
Packit |
70b277 |
the function. Since the value of a function that is not given an
|
|
Packit |
70b277 |
explicit return statement is zero, the zero is printed. For @code{px(1)},
|
|
Packit |
70b277 |
no zero is printed because the function is a void function.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
Also, call by variable for arrays was added. To declare a
|
|
Packit |
70b277 |
call by variable array, the declaration of the array parameter in the
|
|
Packit |
70b277 |
function definition looks like "@code{*}@var{name}@code{[]}". The call
|
|
Packit |
70b277 |
to the function remains the same as call by value arrays.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@node Math Library Functions, , Functions, Functions
|
|
Packit |
70b277 |
@section Math Library Functions
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
If @command{bc} is invoked with the @code{-l} option, a math library is
|
|
Packit |
70b277 |
preloaded and the default @var{scale} is set to 20. The math functions will
|
|
Packit |
70b277 |
calculate their results to the scale set at the time of their call. The
|
|
Packit |
70b277 |
math library defines the following functions:
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@table @code
|
|
Packit |
70b277 |
@item s (@var{x})
|
|
Packit |
70b277 |
The sine of @var{x}, @var{x} is in radians.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item c (@var{x})
|
|
Packit |
70b277 |
The cosine of @var{x}, @var{x} is in radians.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item a (@var{x})
|
|
Packit |
70b277 |
The arctangent of @var{x}, arctangent returns radians.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item l (@var{x})
|
|
Packit |
70b277 |
The natural logarithm of @var{x}.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item e (@var{x})
|
|
Packit |
70b277 |
The exponential function of raising @var{e} to the value @var{x}.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item j (@var{n}, @var{x})
|
|
Packit |
70b277 |
The Bessel function of integer order @var{n} of @var{x}.
|
|
Packit |
70b277 |
@end table
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@node Examples, Readline and Libedit Options, Functions, Top
|
|
Packit |
70b277 |
@chapter Examples
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
In /bin/sh, the following will assign the value of "pi" to the shell
|
|
Packit |
70b277 |
variable @var{pi}.
|
|
Packit |
70b277 |
@example
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
pi=$(echo "scale=10; 4*a(1)" | bc -l)
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@end example
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
The following is the definition of the exponential function used in the
|
|
Packit |
70b277 |
math library. This function is written in POSIX @command{bc}.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@example
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
scale = 20
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* Uses the fact that e^x = (e^(x/2))^2
|
|
Packit |
70b277 |
When x is small enough, we use the series:
|
|
Packit |
70b277 |
e^x = 1 + x + x^2/2! + x^3/3! + ...
|
|
Packit |
70b277 |
*/
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
define e(x) @{
|
|
Packit |
70b277 |
auto a, d, e, f, i, m, v, z
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* Check the sign of x. */
|
|
Packit |
70b277 |
if (x<0) @{
|
|
Packit |
70b277 |
m = 1
|
|
Packit |
70b277 |
x = -x
|
|
Packit |
70b277 |
@}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* Precondition x. */
|
|
Packit |
70b277 |
z = scale;
|
|
Packit |
70b277 |
scale = 4 + z + .44*x;
|
|
Packit |
70b277 |
while (x > 1) @{
|
|
Packit |
70b277 |
f += 1;
|
|
Packit |
70b277 |
x /= 2;
|
|
Packit |
70b277 |
@}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
/* Initialize the variables. */
|
|
Packit |
70b277 |
v = 1+x
|
|
Packit |
70b277 |
a = x
|
|
Packit |
70b277 |
d = 1
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
for (i=2; 1; i++) @{
|
|
Packit |
70b277 |
e = (a *= x) / (d *= i)
|
|
Packit |
70b277 |
if (e == 0) @{
|
|
Packit |
70b277 |
if (f>0) while (f--) v = v*v;
|
|
Packit |
70b277 |
scale = z
|
|
Packit |
70b277 |
if (m) return (1/v);
|
|
Packit |
70b277 |
return (v/1);
|
|
Packit |
70b277 |
@}
|
|
Packit |
70b277 |
v += e
|
|
Packit |
70b277 |
@}
|
|
Packit |
70b277 |
@}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@end example
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
The following is code that uses the extended features of @command{bc} to
|
|
Packit |
70b277 |
implement a simple program for calculating checkbook balances. This
|
|
Packit |
70b277 |
program is best kept in a file so that it can be used many times
|
|
Packit |
70b277 |
without having to retype it at every use.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@example
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
scale=2
|
|
Packit |
70b277 |
print "\nCheck book program\n!"
|
|
Packit |
70b277 |
print " Remember, deposits are negative transactions.\n"
|
|
Packit |
70b277 |
print " Exit by a 0 transaction.\n\n"
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
print "Initial balance? "; bal = read()
|
|
Packit |
70b277 |
bal /= 1
|
|
Packit |
70b277 |
print "\n"
|
|
Packit |
70b277 |
while (1) @{
|
|
Packit |
70b277 |
"current balance = "; bal
|
|
Packit |
70b277 |
"transaction? "; trans = read()
|
|
Packit |
70b277 |
if (trans == 0) break;
|
|
Packit |
70b277 |
bal -= trans
|
|
Packit |
70b277 |
bal /= 1
|
|
Packit |
70b277 |
@}
|
|
Packit |
70b277 |
quit
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@end example
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
The following is the definition of the recursive factorial function.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@example
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
define f (x) @{
|
|
Packit |
70b277 |
if (x <= 1) return (1);
|
|
Packit |
70b277 |
return (f(x-1) * x);
|
|
Packit |
70b277 |
@}
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@end example
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@node Readline and Libedit Options, Comparison with Other Implementations, Examples, Top
|
|
Packit |
70b277 |
@chapter Readline and Libedit Options
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
GNU @command{bc} can be compiled (via a configure option) to use the GNU
|
|
Packit |
70b277 |
@command{readline} input editor library or the BSD @command{libedit}
|
|
Packit |
70b277 |
library. This allows the user to do
|
|
Packit |
70b277 |
more editing of lines before sending them to @command{bc}. It also
|
|
Packit |
70b277 |
allows for a history of previous lines typed. When this option is
|
|
Packit |
70b277 |
selected, @command{bc} has one more special variable. This special
|
|
Packit |
70b277 |
variable, @var{history} is the number of lines of history retained. A
|
|
Packit |
70b277 |
value of -1 means that an unlimited number of history lines are
|
|
Packit |
70b277 |
retained. This is the default value. Setting the value of
|
|
Packit |
70b277 |
@var{history} to a positive number restricts the number of history lines
|
|
Packit |
70b277 |
to the number given. The value of 0 disables the history feature. For
|
|
Packit |
70b277 |
more information, read the user manuals for the GNU @command{readline},
|
|
Packit |
70b277 |
@command{history} and BSD @command{libedit} libraries. One can not
|
|
Packit |
70b277 |
enable both @command{readline} and @command{libedit} at the same time.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@node Comparison with Other Implementations, Limits, Readline and Libedit Options, Top
|
|
Packit |
70b277 |
@chapter Comparison with Other Implementations
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
This version of @command{bc} was implemented from the POSIX P1003.2/D11
|
|
Packit |
70b277 |
draft and contains several differences and extensions relative to the
|
|
Packit |
70b277 |
draft and traditional implementations. It is not implemented in the
|
|
Packit |
70b277 |
traditional way using @command{dc}. This version is a single process
|
|
Packit |
70b277 |
which parses and runs a byte code translation of the program. There is
|
|
Packit |
70b277 |
an "undocumented" option (-c) that causes the program to output the byte
|
|
Packit |
70b277 |
code to the standard output instead of running it. It was mainly used
|
|
Packit |
70b277 |
for debugging the parser and preparing the math library.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
A major source of differences is extensions, where a feature is extended
|
|
Packit |
70b277 |
to add more functionality and additions, where new features are added.
|
|
Packit |
70b277 |
The following is the list of differences and extensions.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@table @asis
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item LANG environment
|
|
Packit |
70b277 |
This version does not conform to the POSIX standard in the processing
|
|
Packit |
70b277 |
of the LANG environment variable and all environment variables starting
|
|
Packit |
70b277 |
with LC_.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item names
|
|
Packit |
70b277 |
Traditional and POSIX @command{bc}
|
|
Packit |
70b277 |
have single letter names for functions, variables and arrays. They have
|
|
Packit |
70b277 |
been extended to be multi-character names that start with a letter and
|
|
Packit |
70b277 |
may contain letters, numbers and the underscore character.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item Strings
|
|
Packit |
70b277 |
Strings are not allowed to contain NUL characters. POSIX says all characters
|
|
Packit |
70b277 |
must be included in strings.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item last
|
|
Packit |
70b277 |
POSIX @command{bc} does not have a \fBlast variable. Some implementations
|
|
Packit |
70b277 |
of @command{bc} use the period (.) in a similar way.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item comparisons
|
|
Packit |
70b277 |
POSIX @command{bc} allows comparisons only in the @code{if} statement,
|
|
Packit |
70b277 |
the @code{while} statement, and the second expression of the @code{for}
|
|
Packit |
70b277 |
statement. Also, only one relational operation is allowed in each of
|
|
Packit |
70b277 |
those statements.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item @code{if} statement, @code{else} clause
|
|
Packit |
70b277 |
POSIX @command{bc} does not have an @code{else} clause.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item @code{for} statement
|
|
Packit |
70b277 |
POSIX @command{bc} requires all expressions to be present in the
|
|
Packit |
70b277 |
@code{for} statement.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item @code{&&,} @code{||}, @code{!}
|
|
Packit |
70b277 |
POSIX @command{bc} does not have the logical operators.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item @code{read} function
|
|
Packit |
70b277 |
POSIX @command{bc} does not have a @code{read} function.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item @code{print} statement
|
|
Packit |
70b277 |
POSIX @command{bc} does not have a @code{print} statement.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item @code{continue} statement
|
|
Packit |
70b277 |
POSIX @command{bc} does not have a continue statement.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item array parameters
|
|
Packit |
70b277 |
POSIX @command{bc} does not (currently) support array parameters in full.
|
|
Packit |
70b277 |
The POSIX grammar allows for arrays in function definitions, but does
|
|
Packit |
70b277 |
not provide a method to specify an array as an actual parameter. (This
|
|
Packit |
70b277 |
is most likely an oversight in the grammar.) Traditional implementations
|
|
Packit |
70b277 |
of @command{bc} have only call by value array parameters.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item function format
|
|
Packit |
70b277 |
POSIX @command{bc} requires the opening brace on the same line as the
|
|
Packit |
70b277 |
@code{define} key word and the @code{auto} statement on the next line.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item @code{=+}, @code{=-}, @code{=*}, @code{=/}, @code{=%}, @code{=^}
|
|
Packit |
70b277 |
POSIX @command{bc} does not require these "old style" assignment
|
|
Packit |
70b277 |
operators to be defined. This version may allow these "old style"
|
|
Packit |
70b277 |
assignments. Use the @code{limits} statement to see if the installed
|
|
Packit |
70b277 |
version supports them. If it does support the "old style" assignment
|
|
Packit |
70b277 |
operators, the statement "a =- 1" will decrement @code{a} by 1 instead
|
|
Packit |
70b277 |
of setting @code{a} to the value -1.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item spaces in numbers
|
|
Packit |
70b277 |
Other implementations of @command{bc} allow spaces in numbers. For example,
|
|
Packit |
70b277 |
"x=1 3" would assign the value 13 to the variable x. The same statement
|
|
Packit |
70b277 |
would cause a syntax error in this version of @command{bc}.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item errors and execution
|
|
Packit |
70b277 |
This implementation varies from other implementations in terms of what
|
|
Packit |
70b277 |
code will be executed when syntax and other errors are found in the
|
|
Packit |
70b277 |
program. If a syntax error is found in a function definition, error
|
|
Packit |
70b277 |
recovery tries to find the beginning of a statement and continue to
|
|
Packit |
70b277 |
parse the function. Once a syntax error is found in the function, the
|
|
Packit |
70b277 |
function will not be callable and becomes undefined.
|
|
Packit |
70b277 |
Syntax errors in the interactive execution code will invalidate the
|
|
Packit |
70b277 |
current execution block. The execution block is terminated by an
|
|
Packit |
70b277 |
end of line that appears after a complete sequence of statements.
|
|
Packit |
70b277 |
For example,
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@example
|
|
Packit |
70b277 |
a = 1
|
|
Packit |
70b277 |
b = 2
|
|
Packit |
70b277 |
@end example
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
has two execution blocks and
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@example
|
|
Packit |
70b277 |
@{ a = 1
|
|
Packit |
70b277 |
b = 2 @}
|
|
Packit |
70b277 |
@end example
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
has one execution block. Any runtime error will terminate the execution
|
|
Packit |
70b277 |
of the current execution block. A runtime warning will not terminate the
|
|
Packit |
70b277 |
current execution block.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item Interrupts
|
|
Packit |
70b277 |
During an interactive session, the SIGINT signal (usually generated by
|
|
Packit |
70b277 |
the control-C character from the terminal) will cause execution of the
|
|
Packit |
70b277 |
current execution block to be interrupted. It will display a "runtime"
|
|
Packit |
70b277 |
error indicating which function was interrupted. After all runtime
|
|
Packit |
70b277 |
structures have been cleaned up, a message will be printed to notify the
|
|
Packit |
70b277 |
user that @command{bc} is ready for more input. All previously defined
|
|
Packit |
70b277 |
functions remain defined and the value of all non-auto variables are the
|
|
Packit |
70b277 |
value at the point of interruption. All auto variables and function
|
|
Packit |
70b277 |
parameters are removed during the clean up process. During a
|
|
Packit |
70b277 |
non-interactive session, the SIGINT signal will terminate the entire run
|
|
Packit |
70b277 |
of @command{bc}.
|
|
Packit |
70b277 |
@end table
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@node Limits, Environment Variables, Comparison with Other Implementations, Top
|
|
Packit |
70b277 |
@chapter Limits
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
The following are the limits currently in place for this @command{bc}
|
|
Packit |
70b277 |
processor. Some of them may have been changed by an installation. Use
|
|
Packit |
70b277 |
the @code{limits} statement to see the actual values.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@table @code
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item BC_BASE_MAX
|
|
Packit |
70b277 |
The maximum output base is currently set at 999. The maximum input base
|
|
Packit |
70b277 |
is 16.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item BC_DIM_MAX
|
|
Packit |
70b277 |
This is currently an arbitrary limit of 65535 as distributed. Your
|
|
Packit |
70b277 |
installation may be different.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item BC_SCALE_MAX
|
|
Packit |
70b277 |
The number of digits after the decimal point is limited to INT_MAX digits.
|
|
Packit |
70b277 |
Also, the number of digits before the decimal point is limited to INT_MAX
|
|
Packit |
70b277 |
digits.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item BC_STRING_MAX
|
|
Packit |
70b277 |
The limit on the number of characters in a string is INT_MAX characters.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item exponent
|
|
Packit |
70b277 |
The value of the exponent in the raise operation (^) is limited to LONG_MAX.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item multiply
|
|
Packit |
70b277 |
The multiply routine may yield incorrect results if a number
|
|
Packit |
70b277 |
has more than LONG_MAX / 90 total digits. For 32 bit longs, this number is
|
|
Packit |
70b277 |
23,860,929 digits.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item variable names
|
|
Packit |
70b277 |
The current limit on the number of unique names is 32767 for each of
|
|
Packit |
70b277 |
simple variables, arrays and functions.
|
|
Packit |
70b277 |
@end table
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@node Environment Variables, , Limits, Top
|
|
Packit |
70b277 |
@chapter Environment Variables
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
The following environment variables are processed by @command{bc}:
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@table @code
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item POSIXLY_CORRECT
|
|
Packit |
70b277 |
This is the same as the -s option (@pxref{Command Line Options}).
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item BC_ENV_ARGS
|
|
Packit |
70b277 |
This is another mechanism to get arguments to @command{bc}. The format
|
|
Packit |
70b277 |
is the same as the command line arguments. These arguments are
|
|
Packit |
70b277 |
processed first, so any files listed in the environment arguments are
|
|
Packit |
70b277 |
processed before any command line argument files. This allows the user
|
|
Packit |
70b277 |
to set up "standard" options and files to be processed at every
|
|
Packit |
70b277 |
invocation of @command{bc}. The files in the environment variables
|
|
Packit |
70b277 |
would typically contain function definitions for functions the user
|
|
Packit |
70b277 |
wants defined every time @command{bc} is run.
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@item BC_LINE_LENGTH
|
|
Packit |
70b277 |
This should be an integer specifying the number of characters in an
|
|
Packit |
70b277 |
output line for numbers. This includes the backslash and newline
|
|
Packit |
70b277 |
characters for long numbers. As an extension, the value of zero disables the
|
|
Packit |
70b277 |
multi-line feature. Any other value of this variable that is less than
|
|
Packit |
70b277 |
3 sets the line length to 70.
|
|
Packit |
70b277 |
@end table
|
|
Packit |
70b277 |
|
|
Packit |
70b277 |
@contents
|
|
Packit |
70b277 |
@bye
|