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