Commit | Line | Data |
---|---|---|
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 |
21 | is an interactive processor for a language which resembles |
22 | C but provides unlimited precision arithmetic. | |
23 | It takes input from any files given, then reads | |
24 | the standard input. | |
25 | The | |
b5dc1377 | 26 | .Tp Fl l |
5325ced3 | 27 | allow specification |
67aa4194 | 28 | of an arbitrary precision math library. |
b5dc1377 CL |
29 | .Tp Fl c |
30 | .Nm Bc | |
31 | is actually a preprocessor for | |
32 | .Ar dc 1 , | |
33 | which it invokes automatically, unless the | |
34 | .Fl c | |
35 | compile only. | |
36 | option is present. | |
37 | In this case the | |
38 | .Ar dc | |
39 | input is sent to the standard output instead. | |
40 | .Tp | |
41 | .Pp | |
42 | The syntax for | |
43 | .Nm bc | |
67aa4194 KM |
44 | programs is as follows; |
45 | L means letter a-z, | |
46 | E means expression, S means statement. | |
b5dc1377 | 47 | .Pp |
67aa4194 | 48 | Comments |
5325ced3 CL |
49 | .Ds I |
50 | are enclosed in /* and */. | |
51 | .De | |
b5dc1377 | 52 | .Pp |
67aa4194 | 53 | Names |
5325ced3 CL |
54 | .Ds I |
55 | simple variables: L | |
56 | array elements: L [ E ] | |
67aa4194 | 57 | The words `ibase', `obase', and `scale' |
b5dc1377 CL |
58 | .De |
59 | .Pp | |
67aa4194 | 60 | Other operands |
5325ced3 CL |
61 | .Ds I |
62 | arbitrarily long numbers with optional sign and decimal point. | |
63 | \&( E \&) | |
64 | sqrt ( E ) | |
65 | length ( E ) number of significant decimal digits | |
66 | scale ( E ) number of digits right of decimal point | |
67 | L ( E , ... , E ) | |
68 | .De | |
b5dc1377 | 69 | .Pp |
67aa4194 | 70 | Operators |
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 | 78 | Statements |
b5dc1377 | 79 | .Ds I |
67aa4194 | 80 | E |
67aa4194 | 81 | { S ; ... ; S } |
67aa4194 | 82 | if ( E ) S |
67aa4194 | 83 | while ( E ) S |
67aa4194 | 84 | for ( E ; E ; E ) S |
67aa4194 | 85 | null statement |
67aa4194 | 86 | break |
67aa4194 | 87 | quit |
b5dc1377 CL |
88 | .De |
89 | .Pp | |
67aa4194 | 90 | Function definitions |
b5dc1377 | 91 | .Ds I |
67aa4194 | 92 | define L ( L ,..., L ) { |
67aa4194 | 93 | auto L, ... , L |
67aa4194 | 94 | S; ... S |
67aa4194 | 95 | return ( E ) |
67aa4194 | 96 | } |
b5dc1377 CL |
97 | .De |
98 | .Pp | |
99 | Functions in | |
100 | .Fl l | |
67aa4194 | 101 | math library |
5325ced3 | 102 | .Dw j(n,x) |
b5dc1377 CL |
103 | .Dp s(x) |
104 | sine | |
105 | .Dp c(x) | |
106 | cosine | |
107 | .Dp e(x) | |
108 | exponential | |
109 | .Dp l(x) | |
110 | log | |
111 | .Dp a(x) | |
112 | arctangent | |
113 | .Dp j(n,x) | |
114 | Bessel function | |
115 | .Dp | |
116 | .Pp | |
67aa4194 | 117 | All function arguments are passed by value. |
b5dc1377 | 118 | .Pp |
67aa4194 KM |
119 | The value of a statement that is an expression is printed |
120 | unless the main operator is an assignment. | |
121 | Either semicolons or newlines may separate statements. | |
122 | Assignment to | |
b5dc1377 | 123 | .Ar scale |
67aa4194 KM |
124 | influences the number of digits to be retained on arithmetic |
125 | operations in the manner of | |
b5dc1377 | 126 | .Xr dc 1 . |
67aa4194 | 127 | Assignments to |
b5dc1377 | 128 | .Ar ibase |
67aa4194 | 129 | or |
b5dc1377 | 130 | .Ar obase |
67aa4194 | 131 | set the input and output number radix respectively. |
b5dc1377 | 132 | .Pp |
67aa4194 KM |
133 | The same letter may be used as an array, a function, |
134 | and a simple variable simultaneously. | |
135 | All variables are global to the program. | |
136 | `Auto' variables are pushed down during function calls. | |
137 | When using arrays as function arguments | |
138 | or defining them as automatic variables | |
139 | empty square brackets must follow the array name. | |
b5dc1377 | 140 | .Pp |
67aa4194 | 141 | For example |
b5dc1377 CL |
142 | .Pp |
143 | .Ds I | |
67aa4194 KM |
144 | scale = 20 |
145 | define 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 |
161 | defines a function to compute an approximate value of | |
162 | the exponential function and | |
b5dc1377 CL |
163 | .Pp |
164 | .Dl for(i=1; i<=10; i++) e(i) | |
165 | .Pp | |
67aa4194 KM |
166 | prints approximate values of the exponential function of |
167 | the 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 |
177 | L. L. Cherry and R. Morris, | |
b5dc1377 CL |
178 | .Em BC \- An arbitrary precision desk-calculator language |
179 | .Sh HISTORY | |
180 | The | |
181 | .Nm bc | |
182 | command appeared in Version 7 AT&T UNIX. | |
183 | .Sh BUGS | |
184 | No &&, \(or\\(or, or ! operators. | |
67aa4194 | 185 | .br |
5325ced3 | 186 | .Ql For |
67aa4194 KM |
187 | statement must have all three E's. |
188 | .br | |
5325ced3 | 189 | .Ql Quit |
67aa4194 | 190 | is interpreted when read, not when executed. |