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