xref: /original-bsd/usr.bin/bc/bc.1 (revision baf24c0d)
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.6 (Berkeley) 07/24/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
26allow specification
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.Ds I
49are enclosed in /* and */.
50.De
51.Pp
52Names
53.Ds I
54simple variables: L
55array elements: L [ E ]
56The words `ibase', `obase', and `scale'
57.De
58.Pp
59Other operands
60.Ds I
61arbitrarily long numbers with optional sign and decimal point.
62\&( E \&)
63sqrt ( E )
64length ( E )	number of significant decimal digits
65scale ( E )	number of digits right of decimal point
66L ( E , ... , E )
67.De
68.Pp
69Operators
70.Ds I
71\&+  \-  *  /  %  ^ (% is remainder; ^ is power)
72\&++   \-\-         (prefix and postfix; apply to names)
73\&==  <=  >=  !=  <  >
74\&=  +=  \-=  *=  /=  %=  ^=
75.De
76.Pp
77Statements
78.Ds I
79E
80{ S ; ... ; S }
81if ( E ) S
82while ( E ) S
83for ( E ; E ; E ) S
84null statement
85break
86quit
87.De
88.Pp
89Function definitions
90.Ds I
91define L ( L ,..., L ) {
92	auto L, ... , L
93	S; ... S
94	return ( E )
95}
96.De
97.Pp
98Functions in
99.Fl l
100math library
101.Dw j(n,x)
102.Dp s(x)
103sine
104.Dp c(x)
105cosine
106.Dp e(x)
107exponential
108.Dp l(x)
109log
110.Dp a(x)
111arctangent
112.Dp j(n,x)
113Bessel function
114.Dp
115.Pp
116All function arguments are passed by value.
117.Pp
118The value of a statement that is an expression is printed
119unless the main operator is an assignment.
120Either semicolons or newlines may separate statements.
121Assignment to
122.Ar scale
123influences the number of digits to be retained on arithmetic
124operations in the manner of
125.Xr dc 1 .
126Assignments to
127.Ar ibase
128or
129.Ar obase
130set the input and output number radix respectively.
131.Pp
132The same letter may be used as an array, a function,
133and a simple variable simultaneously.
134All variables are global to the program.
135`Auto' variables are pushed down during function calls.
136When using arrays as function arguments
137or defining them as automatic variables
138empty square brackets must follow the array name.
139.Pp
140For example
141.Pp
142.Ds I
143scale = 20
144define e(x){
145	auto a, b, c, i, s
146	a = 1
147	b = 1
148	s = 1
149	for(i=1; 1==1; i++){
150		a = a*x
151		b = b*i
152		c = a/b
153		if(c == 0) return(s)
154		s = s+c
155	}
156}
157.De
158.Pp
159.fi
160defines a function to compute an approximate value of
161the exponential function and
162.Pp
163.Dl for(i=1; i<=10; i++) e(i)
164.Pp
165prints approximate values of the exponential function of
166the first ten integers.
167.Sh FILES
168.\" /usr/lib/lib.b	mathematical library
169.Dw Dc(1)
170.Di L
171.Dp Pa dc(1)	desk calculator proper
172.Dp
173.Sh SEE ALSO
174.Xr dc 1
175.br
176L. L. Cherry and R. Morris,
177.Em BC \- An arbitrary precision desk-calculator language
178.Sh HISTORY
179The
180.Nm bc
181command appeared in Version 7 AT&T UNIX.
182.Sh BUGS
183No &&, \(or\\(or, or ! operators.
184.br
185.Ql For
186statement must have all three E's.
187.br
188.Ql Quit
189is interpreted when read, not when executed.
190