| @@ -1,1317 +1,1340 @@ | | | @@ -1,1317 +1,1340 @@ |
1 | .\" $NetBSD: bc.1,v 1.6 2021/01/07 20:37:33 uwe Exp $ | | 1 | .\" $NetBSD: bc.1,v 1.7 2021/01/07 21:21:35 uwe Exp $ |
2 | .\" | | 2 | .\" |
3 | .\" bc.1 - the bc manual | | 3 | .\" bc.1 - the bc manual |
4 | .\" | | 4 | .\" |
5 | .\" Copyright (C) 1991-1994, 1997, 2000, 2003, 2012-2017 Free Software Foundation, Inc. | | 5 | .\" Copyright (C) 1991-1994, 1997, 2000, 2003, 2012-2017 Free Software Foundation, Inc. |
6 | .\" Copyright (C) 2004, 2017 Philip A. Nelson | | 6 | .\" Copyright (C) 2004, 2017 Philip A. Nelson |
7 | .\" Copyright (C) 2017 Thomas Klausner | | 7 | .\" Copyright (C) 2017 Thomas Klausner |
8 | .\" All rights reserved. | | 8 | .\" All rights reserved. |
9 | .\" | | 9 | .\" |
10 | .\" Redistribution and use in source and binary forms, with or without | | 10 | .\" Redistribution and use in source and binary forms, with or without |
11 | .\" modification, are permitted provided that the following conditions | | 11 | .\" modification, are permitted provided that the following conditions |
12 | .\" are met: | | 12 | .\" are met: |
13 | .\" 1. Redistributions of source code must retain the above copyright | | 13 | .\" 1. Redistributions of source code must retain the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer. | | 14 | .\" notice, this list of conditions and the following disclaimer. |
15 | .\" 2. Redistributions in binary form must reproduce the above copyright | | 15 | .\" 2. Redistributions in binary form must reproduce the above copyright |
16 | .\" notice, this list of conditions and the following disclaimer in the | | 16 | .\" notice, this list of conditions and the following disclaimer in the |
17 | .\" documentation and/or other materials provided with the distribution. | | 17 | .\" documentation and/or other materials provided with the distribution. |
18 | .\" 3. Neither the name of Philip A. Nelson nor the name of the Free Software | | 18 | .\" 3. Neither the name of Philip A. Nelson nor the name of the Free Software |
19 | .\" Foundation may not be used to endorse or promote products derived from | | 19 | .\" Foundation may not be used to endorse or promote products derived from |
20 | .\" this software without specific prior written permission. | | 20 | .\" this software without specific prior written permission. |
21 | .\" | | 21 | .\" |
22 | .\" THIS SOFTWARE IS PROVIDED BY PHILIP A. NELSON ``AS IS'' AND ANY EXPRESS OR | | 22 | .\" THIS SOFTWARE IS PROVIDED BY PHILIP A. NELSON ``AS IS'' AND ANY EXPRESS OR |
23 | .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | | 23 | .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
24 | .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | | 24 | .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
25 | .\" IN NO EVENT SHALL PHILIP A. NELSON OR THE FREE SOFTWARE FOUNDATION BE | | 25 | .\" IN NO EVENT SHALL PHILIP A. NELSON OR THE FREE SOFTWARE FOUNDATION BE |
26 | .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 26 | .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
27 | .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 27 | .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
28 | .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 28 | .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
29 | .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 29 | .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
30 | .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 30 | .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
31 | .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | 31 | .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
32 | .\" THE POSSIBILITY OF SUCH DAMAGE. | | 32 | .\" THE POSSIBILITY OF SUCH DAMAGE. |
33 | .\" | | 33 | .\" |
34 | .\" | | 34 | .\" |
35 | .\" | | 35 | .\" |
36 | .Dd April 16, 2017 | | 36 | .Dd April 16, 2017 |
37 | .Dt BC 1 | | 37 | .Dt BC 1 |
38 | .Os | | 38 | .Os |
39 | .Sh NAME | | 39 | .Sh NAME |
40 | .Nm bc | | 40 | .Nm bc |
41 | .Nd arbitrary precision calculator language | | 41 | .Nd arbitrary precision calculator language |
42 | .Sh SYNOPSIS | | 42 | .Sh SYNOPSIS |
43 | .Nm | | 43 | .Nm |
44 | .Op Fl hilqsvw | | 44 | .Op Fl hilqsvw |
45 | .Op long-options | | 45 | .Op long-options |
46 | .Op Ar | | 46 | .Op Ar |
47 | .Sh DESCRIPTION | | 47 | .Sh DESCRIPTION |
48 | .Nm | | 48 | .Nm |
49 | is a language that supports arbitrary precision numbers | | 49 | is a language that supports arbitrary precision numbers |
50 | with interactive execution of statements. | | 50 | with interactive execution of statements. |
51 | There are some similarities | | 51 | There are some similarities |
52 | in the syntax to the C programming language. | | 52 | in the syntax to the C programming language. |
53 | A standard math library is available by command line option. | | 53 | A standard math library is available by command line option. |
54 | If requested, the math library is defined before processing any files. | | 54 | If requested, the math library is defined before processing any files. |
55 | .Nm | | 55 | .Nm |
56 | starts by processing code from all the files listed | | 56 | starts by processing code from all the files listed |
57 | on the command line in the order listed. | | 57 | on the command line in the order listed. |
58 | After all files have been processed, | | 58 | After all files have been processed, |
59 | .Nm | | 59 | .Nm |
60 | reads from the standard input. | | 60 | reads from the standard input. |
61 | All code is executed as it is read. | | 61 | All code is executed as it is read. |
62 | (If a file contains a command to halt the processor, | | 62 | (If a file contains a command to halt the processor, |
63 | .Nm | | 63 | .Nm |
64 | will never read from the standard input.) | | 64 | will never read from the standard input.) |
65 | .Pp | | 65 | .Pp |
66 | This version of | | 66 | This version of |
67 | .Nm | | 67 | .Nm |
68 | contains several extensions beyond traditional | | 68 | contains several extensions beyond traditional |
69 | .Nm | | 69 | .Nm |
70 | implementations and the POSIX draft standard. | | 70 | implementations and the POSIX draft standard. |
71 | Command line options can cause these extensions to print a warning | | 71 | Command line options can cause these extensions to print a warning |
72 | or to be rejected. | | 72 | or to be rejected. |
73 | This document describes the language accepted by this processor. | | 73 | This document describes the language accepted by this processor. |
74 | Extensions will be identified as such. | | 74 | Extensions will be identified as such. |
75 | .Ss OPTIONS | | 75 | .Ss OPTIONS |
76 | .Bl -tag -width "XXinteractiveXXXXXX" | | 76 | .Bl -tag -width "XXinteractiveXXXXXX" |
77 | .It Fl h , Fl Fl help | | 77 | .It Fl h , Fl Fl help |
78 | Print the usage and exit. | | 78 | Print the usage and exit. |
79 | .It Fl i , Fl Fl interactive | | 79 | .It Fl i , Fl Fl interactive |
80 | Force interactive mode. | | 80 | Force interactive mode. |
81 | .It Fl l , Fl Fl mathlib | | 81 | .It Fl l , Fl Fl mathlib |
82 | Define the standard math library. | | 82 | Define the standard math library. |
83 | .It Fl q , Fl Fl quiet | | 83 | .It Fl q , Fl Fl quiet |
84 | Quiet mode. | | 84 | Quiet mode. |
85 | .It Fl s , Fl Fl standard | | 85 | .It Fl s , Fl Fl standard |
86 | Process exactly the POSIX | | 86 | Process exactly the POSIX |
87 | .Nm | | 87 | .Nm |
88 | language. | | 88 | language. |
89 | .It Fl v , Fl Fl version | | 89 | .It Fl v , Fl Fl version |
90 | Print the version number and copyright and quit. | | 90 | Print the version number and copyright and quit. |
91 | .It Fl w , Fl Fl warn | | 91 | .It Fl w , Fl Fl warn |
92 | Give warnings for extensions to POSIX | | 92 | Give warnings for extensions to POSIX |
93 | .Nm . | | 93 | .Nm . |
94 | .El | | 94 | .El |
95 | .Ss NUMBERS | | 95 | .Ss NUMBERS |
96 | The most basic element in | | 96 | The most basic element in |
97 | .Nm | | 97 | .Nm |
98 | is the number. | | 98 | is the number. |
99 | Numbers are arbitrary precision numbers. | | 99 | Numbers are arbitrary precision numbers. |
100 | This precision is both in the integer | | 100 | This precision is both in the integer |
101 | part and the fractional part. | | 101 | part and the fractional part. |
102 | All numbers are represented internally | | 102 | All numbers are represented internally |
103 | in decimal and all computation is done in decimal. | | 103 | in decimal and all computation is done in decimal. |
104 | (This version of | | 104 | (This version of |
105 | .Nm | | 105 | .Nm |
106 | truncates results from divide and multiply operations.) | | 106 | truncates results from divide and multiply operations.) |
107 | There are two attributes of numbers, the length and the scale. | | 107 | There are two attributes of numbers, the length and the scale. |
108 | The length is the | | 108 | The length is the |
109 | total number of significant decimal digits in a number and the scale | | 109 | total number of significant decimal digits in a number and the scale |
110 | is the total number of decimal digits after the decimal point. | | 110 | is the total number of decimal digits after the decimal point. |
111 | For example: | | 111 | For example: |
112 | .Pp | | 112 | .Pp |
113 | .Bl -inset -offset indent -compact | | 113 | .Bl -inset -offset indent -compact |
114 | .It Li .000001 | | 114 | .It Li .000001 |
115 | has a length of 6 and scale of 6. | | 115 | has a length of 6 and scale of 6. |
116 | .It Li 1935.000 | | 116 | .It Li 1935.000 |
117 | has a length of 7 and a scale of 3. | | 117 | has a length of 7 and a scale of 3. |
118 | .El | | 118 | .El |
119 | .Ss VARIABLES | | 119 | .Ss VARIABLES |
120 | Numbers are stored in two types of variables, simple variables and | | 120 | Numbers are stored in two types of variables, simple variables and |
121 | arrays. | | 121 | arrays. |
122 | Both simple variables and array variables are named. | | 122 | Both simple variables and array variables are named. |
123 | Names begin with a letter followed by any number of letters, digits and | | 123 | Names begin with a letter followed by any number of letters, digits and |
124 | underscores. | | 124 | underscores. |
125 | All letters must be lower case. | | 125 | All letters must be lower case. |
126 | (Full alpha-numeric names are an extension. | | 126 | (Full alpha-numeric names are an extension. |
127 | In POSIX | | 127 | In POSIX |
128 | .Nm | | 128 | .Nm |
129 | all names are a single lower case letter.) | | 129 | all names are a single lower case letter.) |
130 | The type of variable is clear by the context | | 130 | The type of variable is clear by the context |
131 | because all array variable names will be followed by brackets ([]). | | 131 | because all array variable names will be followed by brackets |
| | | 132 | .Pq Ql [] . |
132 | .Pp | | 133 | .Pp |
133 | There are four special variables, | | 134 | There are four special variables, |
134 | .Ic scale , | | 135 | .Ic scale , |
135 | .Ic ibase , | | 136 | .Ic ibase , |
136 | .Ic obase , | | 137 | .Ic obase , |
137 | and | | 138 | and |
138 | .Ic last . | | 139 | .Ic last . |
139 | .Ic scale | | 140 | .Ic scale |
140 | defines how some operations use digits after the decimal point. | | 141 | defines how some operations use digits after the decimal point. |
141 | The default value of | | 142 | The default value of |
142 | .Ic scale | | 143 | .Ic scale |
143 | is 0. | | 144 | is 0. |
144 | .Ic ibase | | 145 | .Ic ibase |
145 | and | | 146 | and |
146 | .Ic obase | | 147 | .Ic obase |
147 | define the conversion base for input and output numbers. | | 148 | define the conversion base for input and output numbers. |
148 | The default for both input and output is base 10. | | 149 | The default for both input and output is base 10. |
149 | .Ic last | | 150 | .Ic last |
150 | (an extension) is a variable that has the value of the last | | 151 | (an extension) is a variable that has the value of the last |
151 | printed number. | | 152 | printed number. |
152 | These will be discussed in further detail where appropriate. | | 153 | These will be discussed in further detail where appropriate. |
153 | All of these variables may have values assigned to them | | 154 | All of these variables may have values assigned to them |
154 | as well as used in expressions. | | 155 | as well as used in expressions. |
155 | .Ss COMMENTS | | 156 | .Ss COMMENTS |
156 | Comments in | | 157 | Comments in |
157 | .Nm | | 158 | .Nm |
158 | start with the characters | | 159 | start with the characters |
159 | .Dq Ic /* | | 160 | .Ql /* |
160 | and end with the characters | | 161 | and end with the characters |
161 | .Dq Ic */ . | | 162 | .Ql */ . |
162 | Comments may start anywhere and appear as a single space in the input. | | 163 | Comments may start anywhere and appear as a single space in the input. |
163 | (This causes comments to delimit other input items. | | 164 | (This causes comments to delimit other input items. |
164 | For example, a comment can not be found in the middle of | | 165 | For example, a comment can not be found in the middle of |
165 | a variable name.) | | 166 | a variable name.) |
166 | Comments include any newlines (end of line) between | | 167 | Comments include any newlines (end of line) between |
167 | the start and the end of the comment. | | 168 | the start and the end of the comment. |
168 | .Pp | | 169 | .Pp |
169 | To support the use of scripts for | | 170 | To support the use of scripts for |
170 | .Nm , | | 171 | .Nm , |
171 | a single line comment has been added as an extension. | | 172 | a single line comment has been added as an extension. |
172 | A single line comment starts at a | | 173 | A single line comment starts at a |
173 | .Dq Ic # | | 174 | .Ql # |
174 | character and continues to the next end of the line. | | 175 | character and continues to the next end of the line. |
175 | The end of line | | 176 | The end of line |
176 | character is not part of the comment and is processed normally. | | 177 | character is not part of the comment and is processed normally. |
177 | .Ss EXPRESSIONS | | 178 | .Ss EXPRESSIONS |
178 | The numbers are manipulated by expressions and statements. | | 179 | The numbers are manipulated by expressions and statements. |
179 | Since the language was designed to be interactive, statements and expressions | | 180 | Since the language was designed to be interactive, statements and expressions |
180 | are executed as soon as possible. | | 181 | are executed as soon as possible. |
181 | There is no | | 182 | There is no |
182 | .Dq main | | 183 | .Dq main |
183 | program. | | 184 | program. |
184 | Instead, code is executed as it is encountered. | | 185 | Instead, code is executed as it is encountered. |
185 | (Functions, discussed in | | 186 | (Functions, discussed in |
186 | detail later, are defined when encountered.) | | 187 | detail later, are defined when encountered.) |
187 | .Pp | | 188 | .Pp |
188 | A simple expression is just a constant. | | 189 | A simple expression is just a constant. |
189 | .Nm | | 190 | .Nm |
190 | converts constants | | 191 | converts constants |
191 | into internal decimal numbers using the current input base, specified | | 192 | into internal decimal numbers using the current input base, specified |
192 | by the variable | | 193 | by the variable |
193 | .Ic ibase . | | 194 | .Ic ibase . |
194 | (There is an exception in functions.) | | 195 | (There is an exception in functions.) |
195 | The legal values of | | 196 | The legal values of |
196 | .Ic ibase | | 197 | .Ic ibase |
197 | are 2 through 36. | | 198 | are 2 through 36. |
198 | (Base values greater than 16 are an extension.) | | 199 | (Base values greater than 16 are an extension.) |
199 | Assigning a value outside this range to | | 200 | Assigning a value outside this range to |
200 | .Ic ibase | | 201 | .Ic ibase |
201 | will result in a value of 2 or 36. | | 202 | will result in a value of 2 or 36. |
202 | Input numbers may contain the characters 0-9 and A-Z. | | 203 | Input numbers may contain the characters 0-9 and A-Z. |
203 | (Note: They must be capitals. | | 204 | (Note: They must be capitals. |
204 | Lower case letters are variable names.) | | 205 | Lower case letters are variable names.) |
205 | Single digit numbers always | | 206 | Single digit numbers always |
206 | have the value of the digit regardless of the value of | | 207 | have the value of the digit regardless of the value of |
207 | .Ic ibase . | | 208 | .Ic ibase . |
208 | (i.e. A = 10.) | | 209 | (i.e.\& |
| | | 210 | .Ql A |
| | | 211 | =\~10.) |
209 | For multi-digit numbers, | | 212 | For multi-digit numbers, |
210 | .Nm | | 213 | .Nm |
211 | changes all input digits greater or equal to ibase to the value of | | 214 | changes all input digits greater or equal to ibase to the value of |
212 | .Ic ibase - 1 . | | 215 | .Ic ibase - 1 . |
213 | This makes the number | | 216 | This makes the number |
214 | .Dq ZZZ | | 217 | .Ql ZZZ |
215 | always be the largest 3 digit number of the input base. | | 218 | always be the largest 3 digit number of the input base. |
216 | .Pp | | 219 | .Pp |
217 | Full expressions are similar to many other high level languages. | | 220 | Full expressions are similar to many other high level languages. |
218 | Since there is only one kind of number, there are no rules for mixing | | 221 | Since there is only one kind of number, there are no rules for mixing |
219 | types. | | 222 | types. |
220 | Instead, there are rules on the scale of expressions. | | 223 | Instead, there are rules on the scale of expressions. |
221 | Every expression has a scale. | | 224 | Every expression has a scale. |
222 | This is derived from the scale of original | | 225 | This is derived from the scale of original |
223 | numbers, the operation performed and in many cases, the value of the | | 226 | numbers, the operation performed and in many cases, the value of the |
224 | variable | | 227 | variable |
225 | .Ic scale . | | 228 | .Ic scale . |
226 | Legal values of the variable | | 229 | Legal values of the variable |
227 | .Ic scale | | 230 | .Ic scale |
228 | are 0 to the maximum number representable by a C integer. | | 231 | are 0 to the maximum number representable by a C integer. |
229 | .Pp | | 232 | .Pp |
230 | In the following descriptions of legal expressions, | | 233 | In the following descriptions of legal expressions, |
231 | .Dq expr | | 234 | .Dq expr |
232 | refers to a complete expression and | | 235 | refers to a complete expression and |
233 | .Dq var | | 236 | .Dq var |
234 | refers to a simple or an array variable. | | 237 | refers to a simple or an array variable. |
235 | A simple variable is just a | | 238 | A simple variable is just a |
236 | .Ic name | | 239 | .Ic name |
237 | and an array variable is specified as | | 240 | and an array variable is specified as |
238 | .Ic name[expr] . | | 241 | .Ic name[expr] . |
239 | Unless specifically mentioned the scale of the result is the maximum scale of the | | 242 | Unless specifically mentioned the scale of the result is the maximum scale of the |
240 | expressions involved. | | 243 | expressions involved. |
241 | .Bl -tag -width 15n | | 244 | .Bl -tag -width 15n |
242 | .It Ic "- expr" | | 245 | .It Ic "- expr" |
243 | The result is the negation of the expression. | | 246 | The result is the negation of the expression. |
244 | .It Ic "++ var" | | 247 | .It Ic "++ var" |
245 | The variable is incremented by one and the new value is the result of | | 248 | The variable is incremented by one and the new value is the result of |
246 | the expression. | | 249 | the expression. |
247 | .It Ic "-- var" | | 250 | .It Ic "-- var" |
248 | The variable is decremented by one and the new value is the result of the | | 251 | The variable is decremented by one and the new value is the result of the |
249 | expression. | | 252 | expression. |
250 | .It Ic "var ++" | | 253 | .It Ic "var ++" |
251 | The result of the expression is the value of | | 254 | The result of the expression is the value of |
252 | the variable and then the variable is incremented by one. | | 255 | the variable and then the variable is incremented by one. |
253 | .It Ic "var --" | | 256 | .It Ic "var --" |
254 | The result of the expression is the value of the variable and then | | 257 | The result of the expression is the value of the variable and then |
255 | the variable is decremented by one. | | 258 | the variable is decremented by one. |
256 | .It Ic "expr + expr" | | 259 | .It Ic "expr + expr" |
257 | The result of the expression is the sum of the two expressions. | | 260 | The result of the expression is the sum of the two expressions. |
258 | .It Ic "expr - expr" | | 261 | .It Ic "expr - expr" |
259 | The result of the expression is the difference of the two expressions. | | 262 | The result of the expression is the difference of the two expressions. |
260 | .It Ic "expr * expr" | | 263 | .It Ic "expr * expr" |
261 | The result of the expression is the product of the two expressions. | | 264 | The result of the expression is the product of the two expressions. |
262 | .It Ic "expr / expr" | | 265 | .It Ic "expr / expr" |
263 | The result of the expression is the quotient of the two expressions. | | 266 | The result of the expression is the quotient of the two expressions. |
264 | The scale of the result is the value of the variable \fBscale\fR. | | 267 | The scale of the result is the value of the variable \fBscale\fR. |
265 | .It Ic "expr % expr" | | 268 | .It Ic "expr % expr" |
266 | The result of the expression is the remainder and it is computed in the | | 269 | The result of the expression is the remainder and it is computed in the |
267 | following way: | | 270 | following way: |
268 | To compute a%b, first a/b is computed to | | 271 | To compute a%b, first a/b is computed to |
269 | .Ic scale | | 272 | .Ic scale |
270 | digits. | | 273 | digits. |
271 | That result is used to compute a-(a/b)*b to the scale of the | | 274 | That result is used to compute a-(a/b)*b to the scale of the |
272 | maximum of | | 275 | maximum of |
273 | .Ic scale + | | 276 | .Ic scale + |
274 | scale(b) and scale(a). | | 277 | scale(b) and scale(a). |
275 | If | | 278 | If |
276 | .Ic scale | | 279 | .Ic scale |
277 | is set to zero and both expressions are integers this expression is the | | 280 | is set to zero and both expressions are integers this expression is the |
278 | integer remainder function. | | 281 | integer remainder function. |
279 | .It Ic "expr ^ expr" | | 282 | .It Ic "expr ^ expr" |
280 | The result of the expression is the value of the first raised to the | | 283 | The result of the expression is the value of the first raised to the |
281 | second. | | 284 | second. |
282 | The second expression must be an integer. | | 285 | The second expression must be an integer. |
283 | (If the second expression is not an integer, a warning is generated and the | | 286 | (If the second expression is not an integer, a warning is generated and the |
284 | expression is truncated to get an integer value.) | | 287 | expression is truncated to get an integer value.) |
285 | The scale of the result is | | 288 | The scale of the result is |
286 | .Ic scale | | 289 | .Ic scale |
287 | if the exponent is negative. | | 290 | if the exponent is negative. |
288 | If the exponent is positive, the scale of the result is the minimum of | | 291 | If the exponent is positive, the scale of the result is the minimum of |
289 | the scale of the first expression times the value of the exponent and | | 292 | the scale of the first expression times the value of the exponent and |
290 | the maximum of | | 293 | the maximum of |
291 | .Ic scale | | 294 | .Ic scale |
292 | and the scale of the first expression. | | 295 | and the scale of the first expression. |
293 | (e.g. scale(a^b) = min(scale(a)*b, max( | | 296 | (e.g. scale(a^b) = min(scale(a)*b, max( |
294 | .Ic scale , | | 297 | .Ic scale , |
295 | scale(a))).) | | 298 | scale(a))).) |
296 | It should be noted | | 299 | It should be noted |
297 | that expr^0 will always return the value of 1. | | 300 | that expr^0 will always return the value of 1. |
298 | .It Ic "( expr )" | | 301 | .It Ic "( expr )" |
299 | This alters the standard precedence to force the evaluation of the | | 302 | This alters the standard precedence to force the evaluation of the |
300 | expression. | | 303 | expression. |
301 | .It Ic "var = expr" | | 304 | .It Ic "var = expr" |
302 | The variable is assigned the value of the expression. | | 305 | The variable is assigned the value of the expression. |
303 | .It Ic "var <op>= expr" | | 306 | .It Ic "var <op>= expr" |
304 | This is equivalent to | | 307 | This is equivalent to |
305 | .Ic "var = var <op> expr" | | 308 | .Ic "var = var <op> expr" |
306 | with the exception that the | | 309 | with the exception that the |
307 | .Dq Ic var | | 310 | .Dq Ic var |
308 | part is evaluated only once. | | 311 | part is evaluated only once. |
309 | This can make a difference if | | 312 | This can make a difference if |
310 | .Dq Ic var | | 313 | .Dq Ic var |
311 | is an array. | | 314 | is an array. |
312 | .El | | 315 | .El |
313 | .Pp | | 316 | .Pp |
314 | Relational expressions are a special kind of expression | | 317 | Relational expressions are a special kind of expression |
315 | that always evaluate to 0 or 1, 0 if the relation is false and 1 if | | 318 | that always evaluate to 0 or 1, 0 if the relation is false and 1 if |
316 | the relation is true. | | 319 | the relation is true. |
317 | These may appear in any legal expression. | | 320 | These may appear in any legal expression. |
318 | (POSIX | | 321 | (POSIX |
319 | .Nm | | 322 | .Nm |
320 | requires that relational expressions are used only in | | 323 | requires that relational expressions are used only in |
321 | .Ic if , | | 324 | .Ic if , |
322 | .Ic while , | | 325 | .Ic while , |
323 | and | | 326 | and |
324 | .Ic for | | 327 | .Ic for |
325 | statements and that only one relational test may be | | 328 | statements and that only one relational test may be |
326 | done in them.) | | 329 | done in them.) |
327 | The relational operators are: | | 330 | The relational operators are: |
328 | .Bl -tag -width 15n | | 331 | .Bl -tag -width 15n |
329 | .It Ic "expr1 < expr2" | | 332 | .It Ic "expr1 < expr2" |
330 | The result is 1 if expr1 is strictly less than expr2. | | 333 | The result is 1 if expr1 is strictly less than expr2. |
331 | .It Ic "expr1 <= expr2" | | 334 | .It Ic "expr1 <= expr2" |
332 | The result is 1 if expr1 is less than or equal to expr2. | | 335 | The result is 1 if expr1 is less than or equal to expr2. |
333 | .It Ic "expr1 > expr2" | | 336 | .It Ic "expr1 > expr2" |
334 | The result is 1 if expr1 is strictly greater than expr2. | | 337 | The result is 1 if expr1 is strictly greater than expr2. |
335 | .It Ic "expr1 >= expr2" | | 338 | .It Ic "expr1 >= expr2" |
336 | The result is 1 if expr1 is greater than or equal to expr2. | | 339 | The result is 1 if expr1 is greater than or equal to expr2. |
337 | .It Ic "expr1 == expr2" | | 340 | .It Ic "expr1 == expr2" |
338 | The result is 1 if expr1 is equal to expr2. | | 341 | The result is 1 if expr1 is equal to expr2. |
339 | .It Ic "expr1 != expr2" | | 342 | .It Ic "expr1 != expr2" |
340 | The result is 1 if expr1 is not equal to expr2. | | 343 | The result is 1 if expr1 is not equal to expr2. |
341 | .El | | 344 | .El |
342 | .Pp | | 345 | .Pp |
343 | Boolean operations are also legal. | | 346 | Boolean operations are also legal. |
344 | (POSIX | | 347 | (POSIX |
345 | .Nm | | 348 | .Nm |
346 | does | | 349 | does |
347 | .Em NOT | | 350 | .Em NOT |
348 | have boolean operations.) | | 351 | have boolean operations.) |
349 | The result of all boolean operations are 0 and 1 | | 352 | The result of all boolean operations are 0 and 1 |
350 | (for false and true) as in relational expressions. | | 353 | (for false and true) as in relational expressions. |
351 | The boolean | | 354 | The boolean |
352 | operators are: | | 355 | operators are: |
353 | .Bl -tag -width 15n | | 356 | .Bl -tag -width 15n |
354 | .It Ic "!expr" | | 357 | .It Ic "!expr" |
355 | The result is 1 if expr is 0. | | 358 | The result is 1 if expr is 0. |
356 | .It Ic "expr && expr" | | 359 | .It Ic "expr && expr" |
357 | The result is 1 if both expressions are non-zero. | | 360 | The result is 1 if both expressions are non-zero. |
358 | .It Ic "expr || expr" | | 361 | .It Ic "expr || expr" |
359 | The result is 1 if either expression is non-zero. | | 362 | The result is 1 if either expression is non-zero. |
360 | .El | | 363 | .El |
361 | .Pp | | 364 | .Pp |
362 | The expression precedence is as follows: (lowest to highest) | | 365 | The expression precedence is as follows: (lowest to highest) |
363 | .Bl -enum -offset indent | | 366 | .Bl -enum -offset indent |
364 | .It | | 367 | .It |
365 | || operator, left associative | | 368 | .Ql || |
| | | 369 | operator, left associative |
366 | .It | | 370 | .It |
367 | && operator, left associative | | 371 | .Ql && |
| | | 372 | operator, left associative |
368 | .It | | 373 | .It |
369 | ! operator, nonassociative | | 374 | .Ql \&! |
| | | 375 | operator, nonassociative |
370 | .It | | 376 | .It |
371 | Relational operators, left associative | | 377 | Relational operators, left associative |
372 | .It | | 378 | .It |
373 | Assignment operator, right associative | | 379 | Assignment operator, right associative |
374 | .It | | 380 | .It |
375 | + and - operators, left associative | | 381 | .Ql + |
| | | 382 | and |
| | | 383 | .Ql - |
| | | 384 | operators, left associative |
376 | .It | | 385 | .It |
377 | *, / and % operators, left associative | | 386 | .Ql * , |
| | | 387 | .Ql / |
| | | 388 | and |
| | | 389 | .Ql % |
| | | 390 | operators, left associative |
378 | .It | | 391 | .It |
379 | ^ operator, right associative | | 392 | .Ql ^ |
| | | 393 | operator, right associative |
380 | .It | | 394 | .It |
381 | unary - operator, nonassociative | | 395 | unary |
| | | 396 | .Ql - |
| | | 397 | operator, nonassociative |
382 | .It | | 398 | .It |
383 | ++ and -- operators, nonassociative | | 399 | .Ql ++ |
| | | 400 | and |
| | | 401 | .Ql -- |
| | | 402 | operators, nonassociative |
384 | .El | | 403 | .El |
385 | .Pp | | 404 | .Pp |
386 | This precedence was chosen so that POSIX compliant | | 405 | This precedence was chosen so that POSIX compliant |
387 | .Nm | | 406 | .Nm |
388 | programs will run correctly. | | 407 | programs will run correctly. |
389 | This will cause the use of the relational and logical operators to | | 408 | This will cause the use of the relational and logical operators to |
390 | have some unusual behavior when used with assignment expressions. | | 409 | have some unusual behavior when used with assignment expressions. |
391 | Consider the expression: | | 410 | Consider the expression: |
392 | .Dl Ic a = 3 < 5 | | 411 | .Dl a = 3 < 5 |
393 | .Pp | | 412 | .Pp |
394 | Most C programmers would assume this would assign the result of | | 413 | Most C programmers would assume this would assign the result of |
395 | .Dq Ic 3 < 5 | | 414 | .Dq Li 3 < 5 |
396 | (the value 1) to the variable | | 415 | (the value 1) to the variable |
397 | .Dq Ic a . | | 416 | .Va a . |
398 | What this does in | | 417 | What this does in |
399 | .Nm | | 418 | .Nm |
400 | is assign the value 3 to the variable | | 419 | is assign the value 3 to the variable |
401 | .Dq Ic a | | 420 | .Va a |
402 | and then compare 3 to 5. | | 421 | and then compare 3 to 5. |
403 | It is best to use parenthesis when using relational and logical operators | | 422 | It is best to use parenthesis when using relational and logical operators |
404 | with the assignment operators. | | 423 | with the assignment operators. |
405 | .Pp | | 424 | .Pp |
406 | There are a few more special expressions that are provided in | | 425 | There are a few more special expressions that are provided in |
407 | .Nm . | | 426 | .Nm . |
408 | These have to do with user defined functions and standard functions. | | 427 | These have to do with user defined functions and standard functions. |
409 | They all appear as | | 428 | They all appear as |
410 | .Do Ar name ( Ar parameters ) Dc . | | 429 | .Do Ar name Ns Li \&( Ns Ar parameters Ns Li \&) Dc . |
411 | See the section on functions for user defined functions. | | 430 | See the section on functions for user defined functions. |
412 | The standard functions are: | | 431 | The standard functions are: |
413 | .Bl -tag -width 20n | | 432 | .Bl -tag -width 20n |
414 | .It Fn length expression | | 433 | .It Fn length expression |
415 | The value of the length function is the number of significant digits in the | | 434 | The value of the length function is the number of significant digits in the |
416 | expression. | | 435 | expression. |
417 | .It Fn read | | 436 | .It Fn read |
418 | The read function (an extension) will read a number from the standard | | 437 | The read function (an extension) will read a number from the standard |
419 | input, regardless of where the function occurs. | | 438 | input, regardless of where the function occurs. |
420 | Beware, this can cause problems with the mixing of data and program in | | 439 | Beware, this can cause problems with the mixing of data and program in |
421 | the standard input. | | 440 | the standard input. |
422 | The best use for this function is in a previously written program that | | 441 | The best use for this function is in a previously written program that |
423 | needs input from the user, but never allows program code to be input | | 442 | needs input from the user, but never allows program code to be input |
424 | from the user. | | 443 | from the user. |
425 | The value of the read function is the number read from | | 444 | The value of the read function is the number read from |
426 | the standard input using the current value of the variable | | 445 | the standard input using the current value of the variable |
427 | .Ic ibase | | 446 | .Ic ibase |
428 | for the conversion base. | | 447 | for the conversion base. |
429 | .It Fn scale expression | | 448 | .It Fn scale expression |
430 | The value of the scale function is the number of digits after the decimal | | 449 | The value of the scale function is the number of digits after the decimal |
431 | point in the expression. | | 450 | point in the expression. |
432 | .It Fn sqrt expression | | 451 | .It Fn sqrt expression |
433 | The value of the sqrt function is the square root of the expression. | | 452 | The value of the sqrt function is the square root of the expression. |
434 | If | | 453 | If |
435 | the expression is negative, a run time error is generated. | | 454 | the expression is negative, a run time error is generated. |
436 | .El | | 455 | .El |
437 | .Ss STATEMENTS | | 456 | .Ss STATEMENTS |
438 | Statements (as in most algebraic languages) provide the sequencing of | | 457 | Statements (as in most algebraic languages) provide the sequencing of |
439 | expression evaluation. | | 458 | expression evaluation. |
440 | In | | 459 | In |
441 | .Nm | | 460 | .Nm |
442 | statements are executed | | 461 | statements are executed |
443 | .Dq as soon as possible . | | 462 | .Dq as soon as possible . |
444 | Execution happens when a newline in encountered and | | 463 | Execution happens when a newline in encountered and |
445 | there is one or more complete statements. | | 464 | there is one or more complete statements. |
446 | Due to this immediate | | 465 | Due to this immediate |
447 | execution, newlines are very important in | | 466 | execution, newlines are very important in |
448 | .Nm . | | 467 | .Nm . |
449 | In fact, both a | | 468 | In fact, both a |
450 | semicolon and a newline are used as statement separators. | | 469 | semicolon and a newline are used as statement separators. |
451 | An improperly placed newline will cause a syntax error. | | 470 | An improperly placed newline will cause a syntax error. |
452 | Because newlines | | 471 | Because newlines |
453 | are statement separators, it is possible to hide a newline by using | | 472 | are statement separators, it is possible to hide a newline by using |
454 | the backslash character. | | 473 | the backslash character. |
455 | The sequence | | 474 | The sequence |
456 | .Dq Ic \e<nl> , | | 475 | .Dq Ic \e<nl> , |
457 | where <nl> is the newline appears to | | 476 | where <nl> is the newline appears to |
458 | .Nm | | 477 | .Nm |
459 | as whitespace instead of a newline. | | 478 | as whitespace instead of a newline. |
460 | A statement list is a series of statements separated by semicolons and | | 479 | A statement list is a series of statements separated by semicolons and |
461 | newlines. | | 480 | newlines. |
462 | The following is a list of | | 481 | The following is a list of |
463 | .Nm | | 482 | .Nm |
464 | statements and what they do: | | 483 | statements and what they do: |
465 | (Things enclosed in brackets ([]) are optional parts of the | | 484 | (Things enclosed in brackets |
| | | 485 | .Pq Oo Oc |
| | | 486 | are optional parts of the |
466 | statement.) | | 487 | statement.) |
467 | .Bl -tag -width 15n | | 488 | .Bl -tag -width 15n |
468 | .It Ar expression | | 489 | .It Ar expression |
469 | This statement does one of two things. | | 490 | This statement does one of two things. |
470 | If the expression starts with | | 491 | If the expression starts with |
471 | .Ao Ar variable Ac Ao Ar assignment Ac ... , | | 492 | .Ao Ar variable Ac Ao Ar assignment Ac ... , |
472 | it is considered to be an assignment statement. | | 493 | it is considered to be an assignment statement. |
473 | If the expression is not an assignment statement, the | | 494 | If the expression is not an assignment statement, the |
474 | expression is evaluated and printed to the output. | | 495 | expression is evaluated and printed to the output. |
475 | After the number is printed, a newline is printed. | | 496 | After the number is printed, a newline is printed. |
476 | For example, | | 497 | For example, |
477 | .Dq Ic a=1 | | 498 | .Dq Ic a=1 |
478 | is an assignment statement and | | 499 | is an assignment statement and |
479 | .Do Ic ( a=1 ) Dc | | 500 | .Do Ic ( a=1 ) Dc |
480 | is an expression that has an embedded assignment. | | 501 | is an expression that has an embedded assignment. |
481 | All numbers that are printed are printed in the base | | 502 | All numbers that are printed are printed in the base |
482 | specified by the variable | | 503 | specified by the variable |
483 | .Ic obase . | | 504 | .Ic obase . |
484 | The legal values for | | 505 | The legal values for |
485 | .Ic obase | | 506 | .Ic obase |
486 | are 2 through | | 507 | are 2 through |
487 | .Dv BC_BASE_MAX . | | 508 | .Dv BC_BASE_MAX . |
488 | (See the section | | 509 | (See the section |
489 | .Sx LIMITS . ) | | 510 | .Sx LIMITS . ) |
490 | For bases 2 through 16, the usual method of writing numbers is used. | | 511 | For bases 2 through 16, the usual method of writing numbers is used. |
491 | For bases greater than 16, | | 512 | For bases greater than 16, |
492 | .Nm | | 513 | .Nm |
493 | uses a multi-character digit method of printing the numbers where each | | 514 | uses a multi-character digit method of printing the numbers where each |
494 | higher base digit is printed as a base 10 number. | | 515 | higher base digit is printed as a base 10 number. |
495 | The multi-character digits are separated by spaces. | | 516 | The multi-character digits are separated by spaces. |
496 | Each digit contains the number of characters required to represent the | | 517 | Each digit contains the number of characters required to represent the |
497 | base ten value of | | 518 | base ten value of |
498 | .Dq Ic obase-1 . | | 519 | .Dq Ic obase-1 . |
499 | Since numbers are of arbitrary | | 520 | Since numbers are of arbitrary |
500 | precision, some numbers may not be printable on a single output line. | | 521 | precision, some numbers may not be printable on a single output line. |
501 | These long numbers will be split across lines using the | | 522 | These long numbers will be split across lines using the |
502 | .Dq \e | | 523 | .Dq \e |
503 | as the last character on a line. | | 524 | as the last character on a line. |
504 | The maximum number of characters printed | | 525 | The maximum number of characters printed |
505 | per line is 70. | | 526 | per line is 70. |
506 | Due to the interactive nature of | | 527 | Due to the interactive nature of |
507 | .Nm bc , | | 528 | .Nm bc , |
508 | printing a number causes the side effect of assigning the printed value to the | | 529 | printing a number causes the side effect of assigning the printed value to the |
509 | special variable | | 530 | special variable |
510 | .Ic last . | | 531 | .Ic last . |
511 | This allows the user to recover the last value printed without having | | 532 | This allows the user to recover the last value printed without having |
512 | to retype the expression that printed the number. | | 533 | to retype the expression that printed the number. |
513 | Assigning to | | 534 | Assigning to |
514 | .Ic last | | 535 | .Ic last |
515 | is legal and will | | 536 | is legal and will |
516 | overwrite the last printed value with the assigned value. | | 537 | overwrite the last printed value with the assigned value. |
517 | The newly assigned value will remain until the next number is printed | | 538 | The newly assigned value will remain until the next number is printed |
518 | or another value is assigned to | | 539 | or another value is assigned to |
519 | .Ic last . | | 540 | .Ic last . |
520 | (Some installations may allow the | | 541 | (Some installations may allow the |
521 | use of a single period (.) which is not part of a number as a short | | 542 | use of a single period |
522 | hand notation for | | 543 | .Pq Ql \&. |
| | | 544 | which is not part of a number as a short hand notation for |
523 | .Ic last . ) | | 545 | .Ic last . ) |
524 | .It Ar "string" | | 546 | .It Ar "string" |
525 | The | | 547 | The |
526 | .Ar string | | 548 | .Ar string |
527 | is printed to the output. | | 549 | is printed to the output. |
528 | Strings start with a double quote | | 550 | Strings start with a double quote |
529 | character and contain all characters until the next double quote character. | | 551 | character and contain all characters until the next double quote character. |
530 | All characters are take literally, including any newline. | | 552 | All characters are take literally, including any newline. |
531 | No newline character is printed after the string. | | 553 | No newline character is printed after the string. |
532 | .It Ic print Ar list | | 554 | .It Ic print Ar list |
533 | The | | 555 | The |
534 | .Ic print | | 556 | .Ic print |
535 | statement (an extension) provides another method of output. | | 557 | statement (an extension) provides another method of output. |
536 | The | | 558 | The |
537 | .Ar list | | 559 | .Ar list |
538 | is a list of strings and expressions separated by commas. | | 560 | is a list of strings and expressions separated by commas. |
539 | Each string or expression is printed in the order of the list. | | 561 | Each string or expression is printed in the order of the list. |
540 | No terminating newline is printed. | | 562 | No terminating newline is printed. |
541 | Expressions are evaluated and their | | 563 | Expressions are evaluated and their |
542 | value is printed and assigned to the variable | | 564 | value is printed and assigned to the variable |
543 | .Ic last . | | 565 | .Ic last . |
544 | Strings in the | | 566 | Strings in the |
545 | .Ic print | | 567 | .Ic print |
546 | statement are printed to the output and may contain special characters. | | 568 | statement are printed to the output and may contain special characters. |
547 | Special characters start with the backslash character (\e). | | 569 | Special characters start with the backslash character |
| | | 570 | .Pq Ql \e . |
548 | The special characters recognized by | | 571 | The special characters recognized by |
549 | .Nm | | 572 | .Nm |
550 | are | | 573 | are |
551 | .Sq a | | 574 | .Ql a |
552 | (alert or bell), | | 575 | (alert or bell), |
553 | .Sq b | | 576 | .Ql b |
554 | (backspace), | | 577 | (backspace), |
555 | .Sq f | | 578 | .Ql f |
556 | (form feed), | | 579 | (form feed), |
557 | .Sq n | | 580 | .Ql n |
558 | (newline), | | 581 | (newline), |
559 | .Sq r | | 582 | .Ql r |
560 | (carriage return), | | 583 | (carriage return), |
561 | .Sq q | | 584 | .Ql q |
562 | (double quote), | | 585 | (double quote), |
563 | .Sq t | | 586 | .Ql t |
564 | (tab), and | | 587 | (tab), and |
565 | .Sq \e | | 588 | .Ql \e |
566 | (backslash). | | 589 | (backslash). |
567 | Any other character following the backslash will be ignored. | | 590 | Any other character following the backslash will be ignored. |
568 | .It { Ar statement_list } | | 591 | .It { Ar statement_list } |
569 | This is the compound statement. | | 592 | This is the compound statement. |
570 | It allows multiple statements to be grouped together for execution. | | 593 | It allows multiple statements to be grouped together for execution. |
571 | .It Ic if ( Ar expression ) Ar statement1 [ Ic else Ar statement2 ] | | 594 | .It Ic if ( Ar expression ) Ar statement1 [ Ic else Ar statement2 ] |
572 | The | | 595 | The |
573 | .Ic if | | 596 | .Ic if |
574 | statement evaluates the | | 597 | statement evaluates the |
575 | .Ar expression | | 598 | .Ar expression |
576 | and executes | | 599 | and executes |
577 | .Ar statement1 | | 600 | .Ar statement1 |
578 | or | | 601 | or |
579 | .Ar statement2 | | 602 | .Ar statement2 |
580 | depending on the value of the | | 603 | depending on the value of the |
581 | .Ar expression . | | 604 | .Ar expression . |
582 | If the | | 605 | If the |
583 | .Ar expression | | 606 | .Ar expression |
584 | is non-zero, | | 607 | is non-zero, |
585 | .Ar statement1 | | 608 | .Ar statement1 |
586 | is executed. | | 609 | is executed. |
587 | If | | 610 | If |
588 | .Ar statement2 | | 611 | .Ar statement2 |
589 | is present and the value of the | | 612 | is present and the value of the |
590 | .Ar expression | | 613 | .Ar expression |
591 | is 0, then | | 614 | is 0, then |
592 | .Ar statement2 | | 615 | .Ar statement2 |
593 | is executed. | | 616 | is executed. |
594 | (The | | 617 | (The |
595 | .Ic else | | 618 | .Ic else |
596 | clause is an extension.) | | 619 | clause is an extension.) |
597 | .It Ic while ( Ar expression ) Ar statement | | 620 | .It Ic while ( Ar expression ) Ar statement |
598 | The | | 621 | The |
599 | .Ic while | | 622 | .Ic while |
600 | statement will execute the | | 623 | statement will execute the |
601 | .Ar statement | | 624 | .Ar statement |
602 | while the | | 625 | while the |
603 | .Ar expression | | 626 | .Ar expression |
604 | is non-zero. | | 627 | is non-zero. |
605 | It evaluates the | | 628 | It evaluates the |
606 | .Ar expression | | 629 | .Ar expression |
607 | before each execution of the | | 630 | before each execution of the |
608 | .Ar statement . | | 631 | .Ar statement . |
609 | Termination of the loop is caused by a zero | | 632 | Termination of the loop is caused by a zero |
610 | .Ar expression | | 633 | .Ar expression |
611 | value or the execution of a | | 634 | value or the execution of a |
612 | .Ic break | | 635 | .Ic break |
613 | statement. | | 636 | statement. |
614 | .It Ic for ( [ Ar expression1 ] ; [ Ar expression2 ] ; [ Ar expression3 ] ) Ar statement | | 637 | .It Ic for ( [ Ar expression1 ] ; [ Ar expression2 ] ; [ Ar expression3 ] ) Ar statement |
615 | The | | 638 | The |
616 | .Ic for | | 639 | .Ic for |
617 | statement controls repeated execution of the | | 640 | statement controls repeated execution of the |
618 | .Ar statement . | | 641 | .Ar statement . |
619 | .Ar Expression1 | | 642 | .Ar Expression1 |
620 | is evaluated before the loop. | | 643 | is evaluated before the loop. |
621 | .Ar Expression2 | | 644 | .Ar Expression2 |
622 | is evaluated | | 645 | is evaluated |
623 | before each execution of the | | 646 | before each execution of the |
624 | .Ar statement . | | 647 | .Ar statement . |
625 | If it is non-zero, the | | 648 | If it is non-zero, the |
626 | .Ar statement | | 649 | .Ar statement |
627 | is evaluated. | | 650 | is evaluated. |
628 | If it is zero, the loop is terminated. | | 651 | If it is zero, the loop is terminated. |
629 | After each execution of the statement, | | 652 | After each execution of the statement, |
630 | .Ar expression3 | | 653 | .Ar expression3 |
631 | is evaluated before the reevaluation of | | 654 | is evaluated before the reevaluation of |
632 | .Ar expression2 . | | 655 | .Ar expression2 . |
633 | If | | 656 | If |
634 | .Ar expression1 | | 657 | .Ar expression1 |
635 | or | | 658 | or |
636 | .Ar expression3 | | 659 | .Ar expression3 |
637 | are missing, nothing is evaluated at the point they would be evaluated. | | 660 | are missing, nothing is evaluated at the point they would be evaluated. |
638 | If | | 661 | If |
639 | .Ar expression2 | | 662 | .Ar expression2 |
640 | is missing, it is the same as substituting the value 1 for | | 663 | is missing, it is the same as substituting the value 1 for |
641 | .Ar expression2 . | | 664 | .Ar expression2 . |
642 | (The optional expressions are an | | 665 | (The optional expressions are an |
643 | extension. | | 666 | extension. |
644 | POSIX | | 667 | POSIX |
645 | .Nm | | 668 | .Nm |
646 | requires all three expressions.) | | 669 | requires all three expressions.) |
647 | The following is equivalent code for the | | 670 | The following is equivalent code for the |
648 | .Ic for | | 671 | .Ic for |
649 | statement: | | 672 | statement: |
650 | .Bd -literal | | 673 | .Bd -literal -offset indent |
651 | expression1; | | 674 | expression1; |
652 | while (expression2) { | | 675 | while (expression2) { |
653 | statement; | | 676 | statement; |
654 | expression3; | | 677 | expression3; |
655 | } | | 678 | } |
656 | .Ed | | 679 | .Ed |
657 | .It Ic break | | 680 | .It Ic break |
658 | This statement causes a forced exit of the most recent enclosing | | 681 | This statement causes a forced exit of the most recent enclosing |
659 | .Ic while | | 682 | .Ic while |
660 | statement or | | 683 | statement or |
661 | .Ic for | | 684 | .Ic for |
662 | statement. | | 685 | statement. |
663 | .It Ic continue | | 686 | .It Ic continue |
664 | The | | 687 | The |
665 | .Ic continue | | 688 | .Ic continue |
666 | statement (an extension) causes the most recent enclosing | | 689 | statement (an extension) causes the most recent enclosing |
667 | .Ic for | | 690 | .Ic for |
668 | statement to start the next iteration. | | 691 | statement to start the next iteration. |
669 | .It Ic halt | | 692 | .It Ic halt |
670 | The | | 693 | The |
671 | .Ic halt | | 694 | .Ic halt |
672 | statement (an extension) is an executed statement that causes the | | 695 | statement (an extension) is an executed statement that causes the |
673 | .Nm | | 696 | .Nm |
674 | processor to quit only when it is executed. | | 697 | processor to quit only when it is executed. |
675 | For example, | | 698 | For example, |
676 | .Dl if (0 == 1) halt | | 699 | .Dl if (0 == 1) halt |
677 | will not cause | | 700 | will not cause |
678 | .Nm | | 701 | .Nm |
679 | to terminate because the halt is not executed. | | 702 | to terminate because the halt is not executed. |
680 | .It Ic return | | 703 | .It Ic return |
681 | Return the value 0 from a function. | | 704 | Return the value 0 from a function. |
682 | (See the section on functions.) | | 705 | (See the section on functions.) |
683 | .It Ic return ( Ar expression ) | | 706 | .It Ic return ( Ar expression ) |
684 | Return the value of the expression from a function. | | 707 | Return the value of the expression from a function. |
685 | (See the section on functions.) | | 708 | (See the section on functions.) |
686 | As an extension, the parenthesis are not required. | | 709 | As an extension, the parenthesis are not required. |
687 | .El | | 710 | .El |
688 | .Ss PSEUDO STATEMENTS | | 711 | .Ss PSEUDO STATEMENTS |
689 | These statements are not statements in the traditional sense. | | 712 | These statements are not statements in the traditional sense. |
690 | They are not executed statements. | | 713 | They are not executed statements. |
691 | Their function is performed at | | 714 | Their function is performed at |
692 | .Dq compile | | 715 | .Dq compile |
693 | time. | | 716 | time. |
694 | .Bl -tag -width 15n | | 717 | .Bl -tag -width 15n |
695 | .It Ic limits | | 718 | .It Ic limits |
696 | Print the local limits enforced by the local version of | | 719 | Print the local limits enforced by the local version of |
697 | .Nm | | 720 | .Nm |
698 | This is an extension. | | 721 | This is an extension. |
699 | .It Ic quit | | 722 | .It Ic quit |
700 | When the | | 723 | When the |
701 | .Ic quit | | 724 | .Ic quit |
702 | statement is read, the | | 725 | statement is read, the |
703 | .Nm | | 726 | .Nm |
704 | processor | | 727 | processor |
705 | is terminated, regardless of where the quit statement is found. | | 728 | is terminated, regardless of where the quit statement is found. |
706 | For example, | | 729 | For example, |
707 | .Dl if (0 == 1) quit | | 730 | .Dl if (0 == 1) quit |
708 | will cause | | 731 | will cause |
709 | .Nm | | 732 | .Nm |
710 | to terminate. | | 733 | to terminate. |
711 | .It Ic warranty | | 734 | .It Ic warranty |
712 | Print a longer warranty notice. | | 735 | Print a longer warranty notice. |
713 | This is an extension. | | 736 | This is an extension. |
714 | .El | | 737 | .El |
715 | .Ss FUNCTIONS | | 738 | .Ss FUNCTIONS |
716 | Functions provide a method of defining a computation that can be executed | | 739 | Functions provide a method of defining a computation that can be executed |
717 | later. | | 740 | later. |
718 | Functions in | | 741 | Functions in |
719 | .Nm | | 742 | .Nm |
720 | always compute a value and return it to the caller. | | 743 | always compute a value and return it to the caller. |
721 | Function definitions are | | 744 | Function definitions are |
722 | .Dq dynamic | | 745 | .Dq dynamic |
723 | in the sense that a function is | | 746 | in the sense that a function is |
724 | undefined until a definition is encountered in the input. | | 747 | undefined until a definition is encountered in the input. |
725 | That definition is then used until another | | 748 | That definition is then used until another |
726 | definition function for the same name is encountered. | | 749 | definition function for the same name is encountered. |
727 | The new definition then replaces the older definition. | | 750 | The new definition then replaces the older definition. |
728 | A function is defined as follows: | | 751 | A function is defined as follows: |
729 | .Bd -literal -offset indent | | 752 | .Bd -literal -offset indent |
730 | define name ( parameters ) { newline | | 753 | define name ( parameters ) { newline |
731 | auto_list statement_list } | | 754 | auto_list statement_list } |
732 | .Ed | | 755 | .Ed |
733 | .Pp | | 756 | .Pp |
734 | A function call is just an expression of the form | | 757 | A function call is just an expression of the form |
735 | .Do Ar name ( Ar parameters ) Dc . | | 758 | .Do Ar name ( Ar parameters ) Dc . |
736 | .Pp | | 759 | .Pp |
737 | Parameters are numbers or arrays (an extension). | | 760 | Parameters are numbers or arrays (an extension). |
738 | In the function definition, zero or more parameters are defined by | | 761 | In the function definition, zero or more parameters are defined by |
739 | listing their names separated by commas. | | 762 | listing their names separated by commas. |
740 | All parameters are call by value parameters. | | 763 | All parameters are call by value parameters. |
741 | Arrays are specified in the parameter definition by | | 764 | Arrays are specified in the parameter definition by |
742 | the notation | | 765 | the notation |
743 | .Do Ar name [ ] Dc . | | 766 | .Do Ar name [ ] Dc . |
744 | In the function call, actual parameters | | 767 | In the function call, actual parameters |
745 | are full expressions for number parameters. | | 768 | are full expressions for number parameters. |
746 | The same notation is used | | 769 | The same notation is used |
747 | for passing arrays as for defining array parameters. | | 770 | for passing arrays as for defining array parameters. |
748 | The named array is passed by value to the function. | | 771 | The named array is passed by value to the function. |
749 | Since function definitions are dynamic, | | 772 | Since function definitions are dynamic, |
750 | parameter numbers and types are checked when a function is called. | | 773 | parameter numbers and types are checked when a function is called. |
751 | Any mismatch in number or types of parameters will cause a runtime error. | | 774 | Any mismatch in number or types of parameters will cause a runtime error. |
752 | A runtime error will also occur for the call to an undefined function. | | 775 | A runtime error will also occur for the call to an undefined function. |
753 | .Pp | | 776 | .Pp |
754 | The | | 777 | The |
755 | .Ar auto_list | | 778 | .Ar auto_list |
756 | is an optional list of variables that are for | | 779 | is an optional list of variables that are for |
757 | .Dq local | | 780 | .Dq local |
758 | use. | | 781 | use. |
759 | The syntax of the auto list (if present) is | | 782 | The syntax of the auto list (if present) is |
760 | .Do Ic auto Ar name , ... ; Dc . | | 783 | .Do Ic auto Ar name , ... ; Dc . |
761 | (The semicolon is optional.) | | 784 | (The semicolon is optional.) |
762 | Each | | 785 | Each |
763 | .Ar name | | 786 | .Ar name |
764 | is the name of an auto variable. | | 787 | is the name of an auto variable. |
765 | Arrays may be specified by using the | | 788 | Arrays may be specified by using the |
766 | same notation as used in parameters. | | 789 | same notation as used in parameters. |
767 | These variables have their | | 790 | These variables have their |
768 | values pushed onto a stack at the start of the function. | | 791 | values pushed onto a stack at the start of the function. |
769 | The variables are then initialized to zero and used throughout the | | 792 | The variables are then initialized to zero and used throughout the |
770 | execution of the function. | | 793 | execution of the function. |
771 | At function exit, these variables are popped so that the original | | 794 | At function exit, these variables are popped so that the original |
772 | value (at the time of the function call) of these variables are | | 795 | value (at the time of the function call) of these variables are |
773 | restored. | | 796 | restored. |
774 | The parameters are really auto variables that are initialized to a | | 797 | The parameters are really auto variables that are initialized to a |
775 | value provided in the function call. | | 798 | value provided in the function call. |
776 | Auto variables are different than traditional local variables | | 799 | Auto variables are different than traditional local variables |
777 | because if function A calls function B, B may access function | | 800 | because if function A calls function B, B may access function |
778 | A's auto variables by just using the same name, unless function B has | | 801 | A's auto variables by just using the same name, unless function B has |
779 | called them auto variables. | | 802 | called them auto variables. |
780 | Due to the fact that auto variables and parameters are pushed onto a | | 803 | Due to the fact that auto variables and parameters are pushed onto a |
781 | stack, | | 804 | stack, |
782 | .Nm | | 805 | .Nm |
783 | supports recursive functions. | | 806 | supports recursive functions. |
784 | .Pp | | 807 | .Pp |
785 | The function body is a list of | | 808 | The function body is a list of |
786 | .Nm | | 809 | .Nm |
787 | statements. | | 810 | statements. |
788 | Again, statements | | 811 | Again, statements |
789 | are separated by semicolons or newlines. | | 812 | are separated by semicolons or newlines. |
790 | Return statements cause the | | 813 | Return statements cause the |
791 | termination of a function and the return of a value. | | 814 | termination of a function and the return of a value. |
792 | There are two | | 815 | There are two |
793 | versions of the return statement. | | 816 | versions of the return statement. |
794 | The first form, | | 817 | The first form, |
795 | .Dq Ic return , | | 818 | .Dq Ic return , |
796 | returns the value 0 to the calling expression. | | 819 | returns the value 0 to the calling expression. |
797 | The second form, | | 820 | The second form, |
798 | .Do Ic return ( Ar expression ) Dc , | | 821 | .Do Ic return ( Ar expression ) Dc , |
799 | computes the value of the expression | | 822 | computes the value of the expression |
800 | and returns that value to the calling expression. | | 823 | and returns that value to the calling expression. |
801 | There is an implied | | 824 | There is an implied |
802 | .Do Ic return ( 0 ) Dc | | 825 | .Do Ic return ( 0 ) Dc |
803 | at the end of every function. | | 826 | at the end of every function. |
804 | This allows a function | | 827 | This allows a function |
805 | to terminate and return 0 without an explicit return statement. | | 828 | to terminate and return 0 without an explicit return statement. |
806 | .Pp | | 829 | .Pp |
807 | Functions also change the usage of the variable | | 830 | Functions also change the usage of the variable |
808 | .Ic ibase . | | 831 | .Ic ibase . |
809 | All constants in the function body will be converted using the value of | | 832 | All constants in the function body will be converted using the value of |
810 | .Ic ibase | | 833 | .Ic ibase |
811 | at the time of the function call. | | 834 | at the time of the function call. |
812 | Changes of | | 835 | Changes of |
813 | .Ic ibase | | 836 | .Ic ibase |
814 | will be ignored during the execution of the function except for the | | 837 | will be ignored during the execution of the function except for the |
815 | standard function | | 838 | standard function |
816 | .Fn read , | | 839 | .Fn read , |
817 | which will always use the current value | | 840 | which will always use the current value |
818 | of | | 841 | of |
819 | .Ic ibase | | 842 | .Ic ibase |
820 | for conversion of numbers. | | 843 | for conversion of numbers. |
821 | .Pp | | 844 | .Pp |
822 | Several extensions have been added to functions. | | 845 | Several extensions have been added to functions. |
823 | First, the format of the definition has been slightly relaxed. | | 846 | First, the format of the definition has been slightly relaxed. |
824 | The standard requires the | | 847 | The standard requires the |
825 | opening brace be on the same line as the | | 848 | opening brace be on the same line as the |
826 | .Ic define | | 849 | .Ic define |
827 | keyword and all other parts must be on following lines. | | 850 | keyword and all other parts must be on following lines. |
828 | This version of | | 851 | This version of |
829 | .Nm | | 852 | .Nm |
830 | will allow any number of newlines before and after the opening brace of the | | 853 | will allow any number of newlines before and after the opening brace of the |
831 | function. | | 854 | function. |
832 | For example, the following definitions are legal. | | 855 | For example, the following definitions are legal. |
833 | .Bd -literal -offset indent | | 856 | .Bd -literal -offset indent |
834 | define d (n) { return (2*n); } | | 857 | define d (n) { return (2*n); } |
835 | define d (n) | | 858 | define d (n) |
836 | { return (2*n); } | | 859 | { return (2*n); } |
837 | .Ed | | 860 | .Ed |
838 | .Pp | | 861 | .Pp |
839 | Functions may be defined as | | 862 | Functions may be defined as |
840 | .Ic void . | | 863 | .Ic void . |
841 | A | | 864 | A |
842 | .Ic void | | 865 | .Ic void |
843 | funtion returns no value and thus may not be used in any place that needs | | 866 | funtion returns no value and thus may not be used in any place that needs |
844 | a value. | | 867 | a value. |
845 | A | | 868 | A |
846 | .Ic void | | 869 | .Ic void |
847 | function does not produce any output when called by itself | | 870 | function does not produce any output when called by itself |
848 | on an input line. | | 871 | on an input line. |
849 | The key word | | 872 | The key word |
850 | .Ic void | | 873 | .Ic void |
851 | is placed between the key word | | 874 | is placed between the key word |
852 | .Ic define | | 875 | .Ic define |
853 | and the function name. | | 876 | and the function name. |
854 | For example, consider the following session. | | 877 | For example, consider the following session. |
855 | .Bd -literal -offset indent | | 878 | .Bd -literal -offset indent |
856 | define py (y) { print "--->", y, "<---", "\n"; } | | 879 | define py (y) { print "--->", y, "<---", "\n"; } |
857 | define void px (x) { print "--->", x, "<---", "\n"; } | | 880 | define void px (x) { print "--->", x, "<---", "\n"; } |
858 | py(1) | | 881 | py(1) |
859 | --->1<--- | | 882 | --->1<--- |
860 | 0 | | 883 | 0 |
861 | px(1) | | 884 | px(1) |
862 | --->1<--- | | 885 | --->1<--- |
863 | .Ed | | 886 | .Ed |
864 | .Pp | | 887 | .Pp |
865 | Since | | 888 | Since |
866 | .Fn py | | 889 | .Fn py |
867 | is not a void function, the call of | | 890 | is not a void function, the call of |
868 | .Ql py(1) | | 891 | .Ql py(1) |
869 | prints the desired output and then prints a second line that is the value of | | 892 | prints the desired output and then prints a second line that is the value of |
870 | the function. | | 893 | the function. |
871 | Since the value of a function that is not given an | | 894 | Since the value of a function that is not given an |
872 | explicit return statement is zero, the zero is printed. | | 895 | explicit return statement is zero, the zero is printed. |
873 | For | | 896 | For |
874 | .Ql px(1) , | | 897 | .Ql px(1) , |
875 | no zero is printed because the function is a void function. | | 898 | no zero is printed because the function is a void function. |
876 | .Pp | | 899 | .Pp |
877 | Also, call by variable for arrays was added. | | 900 | Also, call by variable for arrays was added. |
878 | To declare | | 901 | To declare |
879 | a call by variable array, the declaration of the array parameter in the | | 902 | a call by variable array, the declaration of the array parameter in the |
880 | function definition looks like | | 903 | function definition looks like |
881 | .Do Fa name Ns Li [] Dc . | | 904 | .Do Fa name Ns Li [] Dc . |
882 | The call to the | | 905 | The call to the |
883 | function remains the same as call by value arrays. | | 906 | function remains the same as call by value arrays. |
884 | .Ss MATH LIBRARY | | 907 | .Ss MATH LIBRARY |
885 | If | | 908 | If |
886 | .Nm | | 909 | .Nm |
887 | is invoked with the | | 910 | is invoked with the |
888 | .Fl l | | 911 | .Fl l |
889 | option, a math library is preloaded and the default scale is set to 20. | | 912 | option, a math library is preloaded and the default scale is set to 20. |
890 | The math functions will calculate their | | 913 | The math functions will calculate their |
891 | results to the scale set at the time of their call. | | 914 | results to the scale set at the time of their call. |
892 | The math library defines the following functions: | | 915 | The math library defines the following functions: |
893 | .Bl -tag -width 15n | | 916 | .Bl -tag -width 15n |
894 | .It Fn s x | | 917 | .It Fn s x |
895 | The sine of | | 918 | The sine of |
896 | .Fa x , | | 919 | .Fa x , |
897 | .Fa x | | 920 | .Fa x |
898 | is in radians. | | 921 | is in radians. |
899 | .It Fn c x | | 922 | .It Fn c x |
900 | The cosine of | | 923 | The cosine of |
901 | .Fa x , | | 924 | .Fa x , |
902 | .Fa x | | 925 | .Fa x |
903 | is in radians. | | 926 | is in radians. |
904 | .It Fn a x | | 927 | .It Fn a x |
905 | The arctangent of | | 928 | The arctangent of |
906 | .FA x , | | 929 | .FA x , |
907 | arctangent returns radians. | | 930 | arctangent returns radians. |
908 | .It Fn l x | | 931 | .It Fn l x |
909 | The natural logarithm of | | 932 | The natural logarithm of |
910 | .Fa x . | | 933 | .Fa x . |
911 | .It Fn e x | | 934 | .It Fn e x |
912 | The exponential function of raising | | 935 | The exponential function of raising |
913 | .Va e | | 936 | .Va e |
914 | to the value | | 937 | to the value |
915 | .Fa x . | | 938 | .Fa x . |
916 | .It Fn j n x | | 939 | .It Fn j n x |
917 | The Bessel function of integer order | | 940 | The Bessel function of integer order |
918 | .Fa n | | 941 | .Fa n |
919 | of | | 942 | of |
920 | .Fa x . | | 943 | .Fa x . |
921 | .El | | 944 | .El |
922 | .Sh ENVIRONMENT | | 945 | .Sh ENVIRONMENT |
923 | The following environment variables are processed by | | 946 | The following environment variables are processed by |
924 | .Nm : | | 947 | .Nm : |
925 | .Bl -tag -width 15n | | 948 | .Bl -tag -width 15n |
926 | .It Ev POSIXLY_CORRECT | | 949 | .It Ev POSIXLY_CORRECT |
927 | This is the same as the | | 950 | This is the same as the |
928 | .Fl s | | 951 | .Fl s |
929 | option. | | 952 | option. |
930 | .It Dv BC_ENV_ARGS | | 953 | .It Dv BC_ENV_ARGS |
931 | This is another mechanism to get arguments to | | 954 | This is another mechanism to get arguments to |
932 | .Nm . | | 955 | .Nm . |
933 | The format is the same as the command line arguments. | | 956 | The format is the same as the command line arguments. |
934 | These arguments are processed first, so any files listed in the | | 957 | These arguments are processed first, so any files listed in the |
935 | environment arguments are processed before any command line argument | | 958 | environment arguments are processed before any command line argument |
936 | files. | | 959 | files. |
937 | This allows the user to set up | | 960 | This allows the user to set up |
938 | .Dq standard | | 961 | .Dq standard |
939 | options and files to be | | 962 | options and files to be |
940 | processed at every invocation of | | 963 | processed at every invocation of |
941 | .Nm . | | 964 | .Nm . |
942 | The files in the environment variables would typically contain | | 965 | The files in the environment variables would typically contain |
943 | function definitions for functions the user wants defined every time | | 966 | function definitions for functions the user wants defined every time |
944 | .Nm | | 967 | .Nm |
945 | is run. | | 968 | is run. |
946 | .It Dv BC_LINE_LENGTH | | 969 | .It Dv BC_LINE_LENGTH |
947 | This should be an integer specifying the number of characters in an | | 970 | This should be an integer specifying the number of characters in an |
948 | output line for numbers. | | 971 | output line for numbers. |
949 | This includes the backslash and newline characters for long numbers. | | 972 | This includes the backslash and newline characters for long numbers. |
950 | .El | | 973 | .El |
951 | .Sh EXAMPLES | | 974 | .Sh EXAMPLES |
952 | In | | 975 | In |
953 | .Pa /bin/sh , | | 976 | .Pa /bin/sh , |
954 | the following will assign the value of | | 977 | the following will assign the value of |
955 | .Ar pi | | 978 | .Ar pi |
956 | to the shell variable | | 979 | to the shell variable |
957 | .Ar pi . | | 980 | .Ar pi . |
958 | .Bd -literal -offset indent | | 981 | .Bd -literal -offset indent |
959 | pi=$(echo "scale=10; 4*a(1)" | bc -l) | | 982 | pi=$(echo "scale=10; 4*a(1)" | bc -l) |
960 | .Ed | | 983 | .Ed |
961 | .Pp | | 984 | .Pp |
962 | The following is the definition of the exponential function used in the | | 985 | The following is the definition of the exponential function used in the |
963 | math library. | | 986 | math library. |
964 | This function is written in POSIX | | 987 | This function is written in POSIX |
965 | .Nm . | | 988 | .Nm . |
966 | .Bd -literal -offset indent | | 989 | .Bd -literal -offset indent |
967 | scale = 20 | | 990 | scale = 20 |
968 | | | 991 | |
969 | /* Uses the fact that e^x = (e^(x/2))^2 | | 992 | /* Uses the fact that e^x = (e^(x/2))^2 |
970 | When x is small enough, we use the series: | | 993 | When x is small enough, we use the series: |
971 | e^x = 1 + x + x^2/2! + x^3/3! + ... | | 994 | e^x = 1 + x + x^2/2! + x^3/3! + ... |
972 | */ | | 995 | */ |
973 | | | 996 | |
974 | define e(x) { | | 997 | define e(x) { |
975 | auto a, d, e, f, i, m, v, z | | 998 | auto a, d, e, f, i, m, v, z |
976 | | | 999 | |
977 | /* Check the sign of x. */ | | 1000 | /* Check the sign of x. */ |
978 | if (x<0) { | | 1001 | if (x<0) { |
979 | m = 1 | | 1002 | m = 1 |
980 | x = -x | | 1003 | x = -x |
981 | } | | 1004 | } |
982 | | | 1005 | |
983 | /* Precondition x. */ | | 1006 | /* Precondition x. */ |
984 | z = scale; | | 1007 | z = scale; |
985 | scale = 4 + z + .44*x; | | 1008 | scale = 4 + z + .44*x; |
986 | while (x > 1) { | | 1009 | while (x > 1) { |
987 | f += 1; | | 1010 | f += 1; |
988 | x /= 2; | | 1011 | x /= 2; |
989 | } | | 1012 | } |
990 | | | 1013 | |
991 | /* Initialize the variables. */ | | 1014 | /* Initialize the variables. */ |
992 | v = 1+x | | 1015 | v = 1+x |
993 | a = x | | 1016 | a = x |
994 | d = 1 | | 1017 | d = 1 |
995 | | | 1018 | |
996 | for (i=2; 1; i++) { | | 1019 | for (i=2; 1; i++) { |
997 | e = (a *= x) / (d *= i) | | 1020 | e = (a *= x) / (d *= i) |
998 | if (e == 0) { | | 1021 | if (e == 0) { |
999 | if (f>0) while (f--) v = v*v; | | 1022 | if (f>0) while (f--) v = v*v; |
1000 | scale = z | | 1023 | scale = z |
1001 | if (m) return (1/v); | | 1024 | if (m) return (1/v); |
1002 | return (v/1); | | 1025 | return (v/1); |
1003 | } | | 1026 | } |
1004 | v += e | | 1027 | v += e |
1005 | } | | 1028 | } |
1006 | } | | 1029 | } |
1007 | .Ed | | 1030 | .Ed |
1008 | .Pp | | 1031 | .Pp |
1009 | The following is code that uses the extended features of | | 1032 | The following is code that uses the extended features of |
1010 | .Nm | | 1033 | .Nm |
1011 | to implement a simple program for calculating checkbook balances. | | 1034 | to implement a simple program for calculating checkbook balances. |
1012 | This program is best kept in a file so that it can be used many times | | 1035 | This program is best kept in a file so that it can be used many times |
1013 | without having to retype it at every use. | | 1036 | without having to retype it at every use. |
1014 | .Bd -literal -offset indent | | 1037 | .Bd -literal -offset indent |
1015 | scale=2 | | 1038 | scale=2 |
1016 | print "\enCheck book program!\en" | | 1039 | print "\enCheck book program!\en" |
1017 | print " Remember, deposits are negative transactions.\en" | | 1040 | print " Remember, deposits are negative transactions.\en" |
1018 | print " Exit by a 0 transaction.\en\en" | | 1041 | print " Exit by a 0 transaction.\en\en" |
1019 | | | 1042 | |
1020 | print "Initial balance? "; bal = read() | | 1043 | print "Initial balance? "; bal = read() |
1021 | bal /= 1 | | 1044 | bal /= 1 |
1022 | print "\en" | | 1045 | print "\en" |
1023 | while (1) { | | 1046 | while (1) { |
1024 | "current balance = "; bal | | 1047 | "current balance = "; bal |
1025 | "transaction? "; trans = read() | | 1048 | "transaction? "; trans = read() |
1026 | if (trans == 0) break; | | 1049 | if (trans == 0) break; |
1027 | bal -= trans | | 1050 | bal -= trans |
1028 | bal /= 1 | | 1051 | bal /= 1 |
1029 | } | | 1052 | } |
1030 | quit | | 1053 | quit |
1031 | .Ed | | 1054 | .Ed |
1032 | .Pp | | 1055 | .Pp |
1033 | The following is the definition of the recursive factorial function. | | 1056 | The following is the definition of the recursive factorial function. |
1034 | .Bd -literal -offset indent | | 1057 | .Bd -literal -offset indent |
1035 | define f (x) { | | 1058 | define f (x) { |
1036 | if (x <= 1) return (1); | | 1059 | if (x <= 1) return (1); |
1037 | return (f(x-1) * x); | | 1060 | return (f(x-1) * x); |
1038 | } | | 1061 | } |
1039 | .Ed | | 1062 | .Ed |
1040 | .Ss EDITLINE OPTIONS | | 1063 | .Ss EDITLINE OPTIONS |
1041 | .Nm | | 1064 | .Nm |
1042 | is compiled using the | | 1065 | is compiled using the |
1043 | .Xr editline 3 | | 1066 | .Xr editline 3 |
1044 | library. | | 1067 | library. |
1045 | This allows the user to do editing of lines before sending them | | 1068 | This allows the user to do editing of lines before sending them |
1046 | to | | 1069 | to |
1047 | .Nm . | | 1070 | .Nm . |
1048 | It also allows for a history of previous lines typed. | | 1071 | It also allows for a history of previous lines typed. |
1049 | This adds to | | 1072 | This adds to |
1050 | .Nm | | 1073 | .Nm |
1051 | one more special variable. | | 1074 | one more special variable. |
1052 | This special variable, | | 1075 | This special variable, |
1053 | .Ic history | | 1076 | .Ic history |
1054 | is the number of lines of history retained. | | 1077 | is the number of lines of history retained. |
1055 | The default value of \-1 means that an unlimited | | 1078 | The default value of \-1 means that an unlimited |
1056 | number of history lines are retained. | | 1079 | number of history lines are retained. |
1057 | Setting the value of | | 1080 | Setting the value of |
1058 | .Ic history | | 1081 | .Ic history |
1059 | to a positive number restricts the number of history | | 1082 | to a positive number restricts the number of history |
1060 | lines to the number given. | | 1083 | lines to the number given. |
1061 | The value of 0 disables the history feature. | | 1084 | The value of 0 disables the history feature. |
1062 | For more information, read the user manual for the | | 1085 | For more information, read the user manual for the |
1063 | .Xr editline 3 | | 1086 | .Xr editline 3 |
1064 | library. | | 1087 | library. |
1065 | .Ss DIFFERENCES | | 1088 | .Ss DIFFERENCES |
1066 | This version of | | 1089 | This version of |
1067 | .Nm | | 1090 | .Nm |
1068 | was implemented from the POSIX P1003.2/D11 draft and contains | | 1091 | was implemented from the POSIX P1003.2/D11 draft and contains |
1069 | several differences and extensions relative to the draft and | | 1092 | several differences and extensions relative to the draft and |
1070 | traditional implementations. | | 1093 | traditional implementations. |
1071 | It is not implemented in the traditional way using | | 1094 | It is not implemented in the traditional way using |
1072 | .Xr dc 1 . | | 1095 | .Xr dc 1 . |
1073 | This version is a single process which parses and runs a byte code | | 1096 | This version is a single process which parses and runs a byte code |
1074 | translation of the program. | | 1097 | translation of the program. |
1075 | There is an | | 1098 | There is an |
1076 | .Dq undocumented | | 1099 | .Dq undocumented |
1077 | option | | 1100 | option |
1078 | .Fl ( c ) | | 1101 | .Fl ( c ) |
1079 | that causes the program to output the byte code to | | 1102 | that causes the program to output the byte code to |
1080 | the standard output instead of running it. | | 1103 | the standard output instead of running it. |
1081 | It was mainly used for | | 1104 | It was mainly used for |
1082 | debugging the parser and preparing the math library. | | 1105 | debugging the parser and preparing the math library. |
1083 | .Pp | | 1106 | .Pp |
1084 | A major source of differences is extensions, where a feature is | | 1107 | A major source of differences is extensions, where a feature is |
1085 | extended to add more functionality and additions, where new features | | 1108 | extended to add more functionality and additions, where new features |
1086 | are added. | | 1109 | are added. |
1087 | The following is the list of differences and extensions. | | 1110 | The following is the list of differences and extensions. |
1088 | .Bl -tag -width 15n | | 1111 | .Bl -tag -width 15n |
1089 | .It Ev LANG No environment | | 1112 | .It Ev LANG No environment |
1090 | This version does not conform to the POSIX standard in the processing | | 1113 | This version does not conform to the POSIX standard in the processing |
1091 | of the | | 1114 | of the |
1092 | .Ev LANG | | 1115 | .Ev LANG |
1093 | environment variable and all environment variables starting | | 1116 | environment variable and all environment variables starting |
1094 | with | | 1117 | with |
1095 | .Ev LC_ . | | 1118 | .Ev LC_ . |
1096 | .It names | | 1119 | .It names |
1097 | Traditional and POSIX | | 1120 | Traditional and POSIX |
1098 | .Nm | | 1121 | .Nm |
1099 | have single letter names for functions, variables and arrays. | | 1122 | have single letter names for functions, variables and arrays. |
1100 | They have been extended to be multi-character names that start with a letter and | | 1123 | They have been extended to be multi-character names that start with a letter and |
1101 | may contain letters, numbers and the underscore character. | | 1124 | may contain letters, numbers and the underscore character. |
1102 | .It strings | | 1125 | .It strings |
1103 | Strings are not allowed to contain NUL characters. | | 1126 | Strings are not allowed to contain NUL characters. |
1104 | POSIX says all characters must be included in strings. | | 1127 | POSIX says all characters must be included in strings. |
1105 | .It Ic last | | 1128 | .It Ic last |
1106 | POSIX | | 1129 | POSIX |
1107 | .Nm | | 1130 | .Nm |
1108 | does not have a | | 1131 | does not have a |
1109 | .Ic last | | 1132 | .Ic last |
1110 | variable. | | 1133 | variable. |
1111 | Some implementations | | 1134 | Some implementations |
1112 | of | | 1135 | of |
1113 | .Nm | | 1136 | .Nm |
1114 | use the period | | 1137 | use the period |
1115 | .Pq Ql \&. | | 1138 | .Pq Ql \&. |
1116 | in a similar way. | | 1139 | in a similar way. |
1117 | .It comparisons | | 1140 | .It comparisons |
1118 | POSIX | | 1141 | POSIX |
1119 | .Nm | | 1142 | .Nm |
1120 | allows comparisons only in the | | 1143 | allows comparisons only in the |
1121 | .Ic if | | 1144 | .Ic if |
1122 | statement, the | | 1145 | statement, the |
1123 | .Ic while | | 1146 | .Ic while |
1124 | statement, and the second expression of the | | 1147 | statement, and the second expression of the |
1125 | .Ic for | | 1148 | .Ic for |
1126 | statement. | | 1149 | statement. |
1127 | Also, only one relational operation is allowed in each of those statements. | | 1150 | Also, only one relational operation is allowed in each of those statements. |
1128 | .It Ic if No statement , Ic else No clause | | 1151 | .It Ic if No statement , Ic else No clause |
1129 | POSIX | | 1152 | POSIX |
1130 | .Nm | | 1153 | .Nm |
1131 | does not have an | | 1154 | does not have an |
1132 | .Ic else | | 1155 | .Ic else |
1133 | clause. | | 1156 | clause. |
1134 | .It Ic for No statement | | 1157 | .It Ic for No statement |
1135 | POSIX | | 1158 | POSIX |
1136 | .Nm | | 1159 | .Nm |
1137 | requires all expressions to be present in the | | 1160 | requires all expressions to be present in the |
1138 | .Ic for | | 1161 | .Ic for |
1139 | statement. | | 1162 | statement. |
1140 | .It Li && , || , \&! | | 1163 | .It Li && , || , \&! |
1141 | POSIX | | 1164 | POSIX |
1142 | .Nm | | 1165 | .Nm |
1143 | does not have the logical operators. | | 1166 | does not have the logical operators. |
1144 | .It Ic read No function | | 1167 | .It Ic read No function |
1145 | POSIX | | 1168 | POSIX |
1146 | .Nm | | 1169 | .Nm |
1147 | does not have a | | 1170 | does not have a |
1148 | .Ic read | | 1171 | .Ic read |
1149 | function. | | 1172 | function. |
1150 | .It Ic print No statement | | 1173 | .It Ic print No statement |
1151 | POSIX | | 1174 | POSIX |
1152 | .Nm | | 1175 | .Nm |
1153 | does not have a | | 1176 | does not have a |
1154 | .Ic print | | 1177 | .Ic print |
1155 | statement . | | 1178 | statement . |
1156 | .It Ic continue No statement | | 1179 | .It Ic continue No statement |
1157 | POSIX | | 1180 | POSIX |
1158 | .Nm | | 1181 | .Nm |
1159 | does not have a | | 1182 | does not have a |
1160 | .Ic continue | | 1183 | .Ic continue |
1161 | statement. | | 1184 | statement. |
1162 | .It Ic return No statement | | 1185 | .It Ic return No statement |
1163 | POSIX | | 1186 | POSIX |
1164 | .Nm | | 1187 | .Nm |
1165 | requires parentheses around the return expression. | | 1188 | requires parentheses around the return expression. |
1166 | .It array parameters | | 1189 | .It array parameters |
1167 | POSIX | | 1190 | POSIX |
1168 | .Nm | | 1191 | .Nm |
1169 | does not (currently) support array parameters in full. | | 1192 | does not (currently) support array parameters in full. |
1170 | The POSIX grammar allows for arrays in function definitions, but does | | 1193 | The POSIX grammar allows for arrays in function definitions, but does |
1171 | not provide a method to specify an array as an actual parameter. | | 1194 | not provide a method to specify an array as an actual parameter. |
1172 | (This is most likely an oversight in the grammar.) | | 1195 | (This is most likely an oversight in the grammar.) |
1173 | Traditional implementations of | | 1196 | Traditional implementations of |
1174 | .Nm | | 1197 | .Nm |
1175 | have only call-by-value array parameters. | | 1198 | have only call-by-value array parameters. |
1176 | .It function format | | 1199 | .It function format |
1177 | POSIX | | 1200 | POSIX |
1178 | .Nm | | 1201 | .Nm |
1179 | requires the opening brace on the same line as the | | 1202 | requires the opening brace on the same line as the |
1180 | .Ic define | | 1203 | .Ic define |
1181 | key word and the | | 1204 | key word and the |
1182 | .Ic auto | | 1205 | .Ic auto |
1183 | statement on the next line. | | 1206 | statement on the next line. |
1184 | .It Li =+ , =- , =* , =/ , =% , =^ | | 1207 | .It Li =+ , =- , =* , =/ , =% , =^ |
1185 | POSIX | | 1208 | POSIX |
1186 | .Nm | | 1209 | .Nm |
1187 | does not require these | | 1210 | does not require these |
1188 | .Dq old style | | 1211 | .Dq old style |
1189 | assignment operators to be defined. | | 1212 | assignment operators to be defined. |
1190 | This version may allow these | | 1213 | This version may allow these |
1191 | .Dq old style | | 1214 | .Dq old style |
1192 | assignments. | | 1215 | assignments. |
1193 | Use the | | 1216 | Use the |
1194 | .Ic limits | | 1217 | .Ic limits |
1195 | statement to see if the installed version supports them. | | 1218 | statement to see if the installed version supports them. |
1196 | If it does support the | | 1219 | If it does support the |
1197 | .Dq old style | | 1220 | .Dq old style |
1198 | assignment operators, the statement | | 1221 | assignment operators, the statement |
1199 | .Dq Li a =- 1 | | 1222 | .Dq Li a =- 1 |
1200 | will decrement | | 1223 | will decrement |
1201 | .Va a | | 1224 | .Va a |
1202 | by 1 instead of setting | | 1225 | by 1 instead of setting |
1203 | .Va a | | 1226 | .Va a |
1204 | to the value \-1. | | 1227 | to the value \-1. |
1205 | .It spaces in numbers | | 1228 | .It spaces in numbers |
1206 | Other implementations of | | 1229 | Other implementations of |
1207 | .Nm | | 1230 | .Nm |
1208 | allow spaces in numbers. | | 1231 | allow spaces in numbers. |
1209 | For example, | | 1232 | For example, |
1210 | .Dq Li x=1 3 | | 1233 | .Dq Li x=1 3 |
1211 | would assign the value 13 to the variable | | 1234 | would assign the value 13 to the variable |
1212 | .Va x . | | 1235 | .Va x . |
1213 | The same statement | | 1236 | The same statement |
1214 | would cause a syntax error in this version of | | 1237 | would cause a syntax error in this version of |
1215 | .Nm . | | 1238 | .Nm . |
1216 | .It errors and execution | | 1239 | .It errors and execution |
1217 | This implementation varies from other implementations in terms of what | | 1240 | This implementation varies from other implementations in terms of what |
1218 | code will be executed when syntax and other errors are found in the | | 1241 | code will be executed when syntax and other errors are found in the |
1219 | program. | | 1242 | program. |
1220 | If a syntax error is found in a function definition, error | | 1243 | If a syntax error is found in a function definition, error |
1221 | recovery tries to find the beginning of a statement and continue to | | 1244 | recovery tries to find the beginning of a statement and continue to |
1222 | parse the function. | | 1245 | parse the function. |
1223 | Once a syntax error is found in the function, the | | 1246 | Once a syntax error is found in the function, the |
1224 | function will not be callable and becomes undefined. | | 1247 | function will not be callable and becomes undefined. |
1225 | Syntax errors in the interactive execution code will invalidate the | | 1248 | Syntax errors in the interactive execution code will invalidate the |
1226 | current execution block. | | 1249 | current execution block. |
1227 | The execution block is terminated by an | | 1250 | The execution block is terminated by an |
1228 | end of line that appears after a complete sequence of statements. | | 1251 | end of line that appears after a complete sequence of statements. |
1229 | For example, | | 1252 | For example, |
1230 | .Bd -literal | | 1253 | .Bd -literal |
1231 | a = 1 | | 1254 | a = 1 |
1232 | b = 2 | | 1255 | b = 2 |
1233 | .Ed | | 1256 | .Ed |
1234 | has two execution blocks and | | 1257 | has two execution blocks and |
1235 | .Bd -literal | | 1258 | .Bd -literal |
1236 | { a = 1 | | 1259 | { a = 1 |
1237 | b = 2 } | | 1260 | b = 2 } |
1238 | .Ed | | 1261 | .Ed |
1239 | has one execution block. | | 1262 | has one execution block. |
1240 | Any runtime error will terminate the execution | | 1263 | Any runtime error will terminate the execution |
1241 | of the current execution block. | | 1264 | of the current execution block. |
1242 | A runtime warning will not terminate the current execution block. | | 1265 | A runtime warning will not terminate the current execution block. |
1243 | .It interrupts | | 1266 | .It interrupts |
1244 | During an interactive session, the | | 1267 | During an interactive session, the |
1245 | .Dv SIGINT | | 1268 | .Dv SIGINT |
1246 | signal (usually generated by the control-C character from the | | 1269 | signal (usually generated by the control-C character from the |
1247 | terminal) will cause execution of the current execution block to be | | 1270 | terminal) will cause execution of the current execution block to be |
1248 | interrupted. | | 1271 | interrupted. |
1249 | It will display a | | 1272 | It will display a |
1250 | .Dq runtime | | 1273 | .Dq runtime |
1251 | error indicating which function was interrupted. | | 1274 | error indicating which function was interrupted. |
1252 | After all runtime structures have been cleaned up, a message will be | | 1275 | After all runtime structures have been cleaned up, a message will be |
1253 | printed to notify the user that | | 1276 | printed to notify the user that |
1254 | .Nm | | 1277 | .Nm |
1255 | is ready for more input. | | 1278 | is ready for more input. |
1256 | All previously defined functions remain defined and the value of all | | 1279 | All previously defined functions remain defined and the value of all |
1257 | non-auto variables are the value at the point of interruption. | | 1280 | non-auto variables are the value at the point of interruption. |
1258 | All auto variables and function parameters are removed during the | | 1281 | All auto variables and function parameters are removed during the |
1259 | clean up process. | | 1282 | clean up process. |
1260 | During a non-interactive session, the | | 1283 | During a non-interactive session, the |
1261 | .Dv SIGINT | | 1284 | .Dv SIGINT |
1262 | signal will terminate the entire run of | | 1285 | signal will terminate the entire run of |
1263 | .Nm . | | 1286 | .Nm . |
1264 | .El | | 1287 | .El |
1265 | .Ss LIMITS | | 1288 | .Ss LIMITS |
1266 | The following are the limits currently in place for this | | 1289 | The following are the limits currently in place for this |
1267 | .Nm | | 1290 | .Nm |
1268 | processor. | | 1291 | processor. |
1269 | Some of them may have been changed by an installation. | | 1292 | Some of them may have been changed by an installation. |
1270 | Use the | | 1293 | Use the |
1271 | .Ic limits | | 1294 | .Ic limits |
1272 | statement to see the actual values. | | 1295 | statement to see the actual values. |
1273 | .Bl -tag -width 15n | | 1296 | .Bl -tag -width 15n |
1274 | .It Dv BC_BASE_MAX | | 1297 | .It Dv BC_BASE_MAX |
1275 | The maximum output base is currently set at 999. | | 1298 | The maximum output base is currently set at 999. |
1276 | The maximum input base is 16. | | 1299 | The maximum input base is 16. |
1277 | .It Dv BC_DIM_MAX | | 1300 | .It Dv BC_DIM_MAX |
1278 | This is currently an arbitrary limit of 65535 as distributed. | | 1301 | This is currently an arbitrary limit of 65535 as distributed. |
1279 | Your installation may be different. | | 1302 | Your installation may be different. |
1280 | .It Dv BC_SCALE_MAX | | 1303 | .It Dv BC_SCALE_MAX |
1281 | The number of digits after the decimal point is limited to | | 1304 | The number of digits after the decimal point is limited to |
1282 | .Dv INT_MAX | | 1305 | .Dv INT_MAX |
1283 | digits. | | 1306 | digits. |
1284 | Also, the number of digits before the decimal point is limited to | | 1307 | Also, the number of digits before the decimal point is limited to |
1285 | .Dv INT_MAX | | 1308 | .Dv INT_MAX |
1286 | digits. | | 1309 | digits. |
1287 | .It Dv BC_STRING_MAX | | 1310 | .It Dv BC_STRING_MAX |
1288 | The limit on the number of characters in a string is | | 1311 | The limit on the number of characters in a string is |
1289 | .Dv INT_MAX | | 1312 | .Dv INT_MAX |
1290 | characters. | | 1313 | characters. |
1291 | .It exponent | | 1314 | .It exponent |
1292 | The value of the exponent in the raise operation | | 1315 | The value of the exponent in the raise operation |
1293 | .Pq Ql ^ | | 1316 | .Pq Ql ^ |
1294 | is limited to | | 1317 | is limited to |
1295 | .Dv LONG_MAX . | | 1318 | .Dv LONG_MAX . |
1296 | .It variable names | | 1319 | .It variable names |
1297 | The current limit on the number of unique names is 32767 for each of | | 1320 | The current limit on the number of unique names is 32767 for each of |
1298 | simple variables, arrays and functions. | | 1321 | simple variables, arrays and functions. |
1299 | .El | | 1322 | .El |
1300 | .Sh DIAGNOSTICS | | 1323 | .Sh DIAGNOSTICS |
1301 | If any file on the command line can not be opened, | | 1324 | If any file on the command line can not be opened, |
1302 | .Nm | | 1325 | .Nm |
1303 | will report | | 1326 | will report |
1304 | that the file is unavailable and terminate. | | 1327 | that the file is unavailable and terminate. |
1305 | Also, there are compile | | 1328 | Also, there are compile |
1306 | and run time diagnostics that should be self-explanatory. | | 1329 | and run time diagnostics that should be self-explanatory. |
1307 | .Sh HISTORY | | 1330 | .Sh HISTORY |
1308 | This man page documents bc version nb1.0. | | 1331 | This man page documents bc version nb1.0. |
1309 | .Sh AUTHORS | | 1332 | .Sh AUTHORS |
1310 | .An Philip A. Nelson Aq Mt phil@NetBSD.org | | 1333 | .An Philip A. Nelson Aq Mt phil@NetBSD.org |
1311 | .Ss ACKNOWLEDGEMENTS | | 1334 | .Ss ACKNOWLEDGEMENTS |
1312 | The author would like to thank Steve Sommars for his extensive help in | | 1335 | The author would like to thank Steve Sommars for his extensive help in |
1313 | testing the implementation. | | 1336 | testing the implementation. |
1314 | Many great suggestions were given. | | 1337 | Many great suggestions were given. |
1315 | This is a much better product due to his involvement. | | 1338 | This is a much better product due to his involvement. |
1316 | .Sh BUGS | | 1339 | .Sh BUGS |
1317 | Error recovery is not very good yet. | | 1340 | Error recovery is not very good yet. |