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