date and time created 91/01/17 18:23:46 by bostic
[unix-history] / usr / src / usr.bin / bc / bc.1
CommitLineData
dd9e2e8d
CL
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.
67aa4194 4.\"
5325ced3 5.\" @(#)bc.1 6.6 (Berkeley) %G%
b5dc1377
CL
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
67aa4194
KM
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
b5dc1377 25.Tp Fl l
5325ced3 26allow specification
67aa4194 27of an arbitrary precision math library.
b5dc1377
CL
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
67aa4194
KM
43programs is as follows;
44L means letter a-z,
45E means expression, S means statement.
b5dc1377 46.Pp
67aa4194 47Comments
5325ced3
CL
48.Ds I
49are enclosed in /* and */.
50.De
b5dc1377 51.Pp
67aa4194 52Names
5325ced3
CL
53.Ds I
54simple variables: L
55array elements: L [ E ]
67aa4194 56The words `ibase', `obase', and `scale'
b5dc1377
CL
57.De
58.Pp
67aa4194 59Other operands
5325ced3
CL
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
b5dc1377 68.Pp
67aa4194 69Operators
5325ced3
CL
70.Ds I
71\&+ \- * / % ^ (% is remainder; ^ is power)
72\&++ \-\- (prefix and postfix; apply to names)
73\&== <= >= != < >
74\&= += \-= *= /= %= ^=
75.De
b5dc1377 76.Pp
67aa4194 77Statements
b5dc1377 78.Ds I
67aa4194 79E
67aa4194 80{ S ; ... ; S }
67aa4194 81if ( E ) S
67aa4194 82while ( E ) S
67aa4194 83for ( E ; E ; E ) S
67aa4194 84null statement
67aa4194 85break
67aa4194 86quit
b5dc1377
CL
87.De
88.Pp
67aa4194 89Function definitions
b5dc1377 90.Ds I
67aa4194 91define L ( L ,..., L ) {
67aa4194 92 auto L, ... , L
67aa4194 93 S; ... S
67aa4194 94 return ( E )
67aa4194 95}
b5dc1377
CL
96.De
97.Pp
98Functions in
99.Fl l
67aa4194 100math library
5325ced3 101.Dw j(n,x)
b5dc1377
CL
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
67aa4194 116All function arguments are passed by value.
b5dc1377 117.Pp
67aa4194
KM
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
b5dc1377 122.Ar scale
67aa4194
KM
123influences the number of digits to be retained on arithmetic
124operations in the manner of
b5dc1377 125.Xr dc 1 .
67aa4194 126Assignments to
b5dc1377 127.Ar ibase
67aa4194 128or
b5dc1377 129.Ar obase
67aa4194 130set the input and output number radix respectively.
b5dc1377 131.Pp
67aa4194
KM
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.
b5dc1377 139.Pp
67aa4194 140For example
b5dc1377
CL
141.Pp
142.Ds I
67aa4194
KM
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}
b5dc1377
CL
157.De
158.Pp
67aa4194
KM
159.fi
160defines a function to compute an approximate value of
161the exponential function and
b5dc1377
CL
162.Pp
163.Dl for(i=1; i<=10; i++) e(i)
164.Pp
67aa4194
KM
165prints approximate values of the exponential function of
166the first ten integers.
b5dc1377 167.Sh FILES
dd9e2e8d 168.\" /usr/lib/lib.b mathematical library
b5dc1377 169.Dw Dc(1)
5325ced3 170.Di L
b5dc1377 171.Dp Pa dc(1) desk calculator proper
5325ced3 172.Dp
b5dc1377
CL
173.Sh SEE ALSO
174.Xr dc 1
5325ced3
CL
175.br
176L. L. Cherry and R. Morris,
b5dc1377
CL
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.
67aa4194 184.br
5325ced3 185.Ql For
67aa4194
KM
186statement must have all three E's.
187.br
5325ced3 188.Ql Quit
67aa4194 189is interpreted when read, not when executed.