xref: /csrg-svn/usr.bin/bc/bc.1 (revision 44280)
1.\" Copyright (c) 1990 Regents of the University of California.
2.\" All rights reserved.  The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
4.\"
5.\"     @(#)bc.1	6.5 (Berkeley) 06/26/90
6.\"
7.Dd
8.Dt BC 1
9.Os ATT 7th
10.Sh NAME
11.Nm bc
12.Nd arbitrary-precision arithmetic language and calculator
13.Sh SYNOPSIS
14.Nm bc
15.Op Fl c
16.Op Fl l
17.Ar
18.Sh DESCRIPTION
19.Nm Bc
20is an interactive processor for a language which resembles
21C but provides unlimited precision arithmetic.
22It takes input from any files given, then reads
23the standard input.
24The
25.Tp Fl l
26argument stands for the name
27of an arbitrary precision math library.
28.Tp Fl c
29.Nm Bc
30is actually a preprocessor for
31.Ar dc 1 ,
32which it invokes automatically, unless the
33.Fl c
34compile only.
35option is present.
36In this case the
37.Ar dc
38input is sent to the standard output instead.
39.Tp
40.Pp
41The syntax for
42.Nm bc
43programs is as follows;
44L means letter a-z,
45E means expression, S means statement.
46.Pp
47Comments
48.Dl are enclosed in /* and */.
49.Pp
50Names
51.Dl simple variables: L
52.Df I
53array elements: L
54.Op  E
55.De
56.Df I
57The words `ibase', `obase', and `scale'
58.De
59.Pp
60Other operands
61.Dl arbitrarily long numbers with optional sign and decimal point.
62.Dl \&( E \&)
63.Dl sqrt ( E )
64.Dl length ( E )	number of significant decimal digits
65.Dl scale ( E )	number of digits right of decimal point
66.Dl L ( E , ... , E )
67.Pp
68Operators
69.Dl \&+  \-  *  /  %  ^ (% is remainder; ^ is power)
70.Dl \&++   \-\-         (prefix and postfix; apply to names)
71.Dl \&==  <=  >=  !=  <  >
72.Dl \&=  +=  \-=  *=  /=  %=  ^=
73.Pp
74Statements
75.Ds I
76E
77{ S ; ... ; S }
78if ( E ) S
79while ( E ) S
80for ( E ; E ; E ) S
81null statement
82break
83quit
84.De
85.Pp
86Function definitions
87.Ds I
88define L ( L ,..., L ) {
89	auto L, ... , L
90	S; ... S
91	return ( E )
92}
93.De
94.Pp
95Functions in
96.Fl l
97math library
98.Dw ss(x)
99.Dp s(x)
100sine
101.Dp c(x)
102cosine
103.Dp e(x)
104exponential
105.Dp l(x)
106log
107.Dp a(x)
108arctangent
109.Dp j(n,x)
110Bessel function
111.Dp
112.Pp
113All function arguments are passed by value.
114.Pp
115The value of a statement that is an expression is printed
116unless the main operator is an assignment.
117Either semicolons or newlines may separate statements.
118Assignment to
119.Ar scale
120influences the number of digits to be retained on arithmetic
121operations in the manner of
122.Xr dc 1 .
123Assignments to
124.Ar ibase
125or
126.Ar obase
127set the input and output number radix respectively.
128.Pp
129The same letter may be used as an array, a function,
130and a simple variable simultaneously.
131All variables are global to the program.
132`Auto' variables are pushed down during function calls.
133When using arrays as function arguments
134or defining them as automatic variables
135empty square brackets must follow the array name.
136.Pp
137For example
138.Pp
139.Ds I
140scale = 20
141define e(x){
142	auto a, b, c, i, s
143	a = 1
144	b = 1
145	s = 1
146	for(i=1; 1==1; i++){
147		a = a*x
148		b = b*i
149		c = a/b
150		if(c == 0) return(s)
151		s = s+c
152	}
153}
154.De
155.Pp
156.fi
157defines a function to compute an approximate value of
158the exponential function and
159.Pp
160.Dl for(i=1; i<=10; i++) e(i)
161.Pp
162prints approximate values of the exponential function of
163the first ten integers.
164.Sh FILES
165.\" /usr/lib/lib.b	mathematical library
166.Ds L
167.Dw Dc(1)
168.Dp Pa dc(1)	desk calculator proper
169.Sh SEE ALSO
170.Xr dc 1
171.L. .L. .Cherry and R. Morris,
172.Em BC \- An arbitrary precision desk-calculator language
173.Sh HISTORY
174The
175.Nm bc
176command appeared in Version 7 AT&T UNIX.
177.Sh BUGS
178No &&, \(or\\(or, or ! operators.
179.br
180.Li For
181statement must have all three E's.
182.br
183.Li Quit
184is interpreted when read, not when executed.
185