1.\" $OpenBSD: bc.1,v 1.18 2004/10/19 07:36:51 otto Exp $ 2.\" $DragonFly: src/usr.bin/bc/bc.1,v 1.2 2005/04/21 18:50:22 swildner Exp $ 3.\" 4.\" Copyright (C) Caldera International Inc. 2001-2002. 5.\" All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code and documentation must retain the above 11.\" copyright notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. All advertising materials mentioning features or use of this software 16.\" must display the following acknowledgement: 17.\" This product includes software developed or owned by Caldera 18.\" International, Inc. 19.\" 4. Neither the name of Caldera International, Inc. nor the names of other 20.\" contributors may be used to endorse or promote products derived from 21.\" this software without specific prior written permission. 22.\" 23.\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA 24.\" INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR 25.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 26.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 27.\" IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT, 28.\" INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 29.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 30.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 33.\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34.\" POSSIBILITY OF SUCH DAMAGE. 35.\" 36.\" @(#)bc.1 6.8 (Berkeley) 8/8/91 37.\" 38.Dd October 19, 2004 39.Dt BC 1 40.Sh NAME 41.Nm bc 42.Nd arbitrary-precision arithmetic language and calculator 43.Sh SYNOPSIS 44.Nm bc 45.Op Fl cl 46.Op Fl e Ar expression 47.Op Ar file ... 48.Sh DESCRIPTION 49.Nm 50is an interactive processor for a language which resembles 51C but provides unlimited precision arithmetic. 52It takes input from any expressions on the command line and 53any files given, then reads the standard input. 54.Pp 55Options available: 56.Bl -tag -width Ds 57.It Fl c 58.Nm 59is actually a preprocessor for 60.Xr dc 1 , 61which it invokes automatically, unless the 62.Fl c 63.Pq compile only 64option is present. 65In this case the generated 66.Xr dc 1 67instructions are sent to the standard output, 68instead of being interpreted by a running 69.Xr dc 1 70process. 71.It Fl e Ar expression 72Evaluate 73.Ar expression . 74If multiple 75.Fl e 76options are specified, they are processed in the order given, 77separated by newlines. 78.It Fl l 79Allow specification of an arbitrary precision math library. 80The definitions in the library are available to command line 81expressions. 82.El 83.Pp 84The syntax for 85.Nm 86programs is as follows: 87.Sq L 88means letter a-z; 89.Sq E 90means expression; 91.Sq S 92means statement. 93As a non-portable extension, it is possible to use long names 94in addition to single letter names. 95A long name is a sequence starting with a lowercase letter 96followed by any number of lowercase letters and digits. 97The underscore character 98.Pq Sq _ 99counts as a letter. 100.Pp 101Comments 102.Bd -unfilled -offset indent -compact 103are enclosed in /* and */ 104are enclosed in # and the next newline 105.Ed 106.Pp 107The newline is not part of the line comment, 108which in itself is a non-portable extension. 109.Pp 110Names 111.Bd -unfilled -offset indent -compact 112simple variables: L 113array elements: L [ E ] 114The words `ibase', `obase', and `scale' 115The word `last' or a single dot 116.Ed 117.Pp 118Other operands 119.Bd -unfilled -offset indent -compact 120arbitrarily long numbers with optional sign and decimal point 121( E ) 122sqrt ( E ) 123length ( E ) number of significant decimal digits 124scale ( E ) number of digits right of decimal point 125L ( E , ... , E ) 126.Ed 127.Pp 128The sequence 129.Sq \e<newline><whitespace> 130is ignored within numbers. 131.Pp 132Operators 133.Pp 134The following arithmetic and logical operators can be used. 135The semantics of the operators is the same as in the C language. 136They are listed in order of decreasing precedence. 137Operators in the same group have the same precedence. 138.Bl -column -offset indent "= += \-= *= /= %= ^=" "Associativity" \ 139"multiply, divide, modulus" 140.It Sy "Operator" Ta Sy "Associativity" Ta Sy "Description" 141.It "++ \-\-" Ta "none" Ta "increment, decrement" 142.It "\-" Ta "none" Ta "unary minus" 143.It "^" Ta "right" Ta "power" 144.It "* / %" Ta "left" Ta "multiply, divide, modulus" 145.It "+ \-" Ta "left" Ta "plus, minus" 146.It "= += -= *= /= %= ^=" Ta "right" Ta "assignment" 147.It "== <= >= != < >" Ta "none" Ta "relational" 148.It "!" Ta "none" Ta "boolean not" 149.It "&&" Ta "left" Ta "boolean and" 150.It "||" Ta "left" Ta "boolean or" 151.El 152.Pp 153Note the following: 154.Bl -bullet -offset indent 155.It 156The relational operators may appear in any expression. 157The 158.St -p1003.2 159standard only allows them in the conditional expression of an 160.Sq if , 161.Sq while 162or 163.Sq for 164statement. 165.It 166The relational operators have a lower precedence than the assignment 167operators. 168This has the consequence that the expression 169.Sy a = b < c 170is interpreted as 171.Sy (a = b) < c , 172which is probably not what the programmer intended. 173.It 174In contrast with the C language, the relational operators all have 175the same precedence, and are non-associative. 176The expression 177.Sy a < b < c 178will produce a syntax error. 179.It 180The boolean operators (!, && and ||) are non-portable extensions. 181.It 182The boolean not 183(!) operator has much lower precedence than the same operator in the 184C language. 185This has the consequence that the expression 186.Sy !a < b 187is interpreted as 188.Sy !(a < b) . 189Prudent programmers use parentheses when writing expressions involving 190boolean operators. 191.El 192.Pp 193Statements 194.Bd -unfilled -offset indent -compact 195E 196{ S ; ... ; S } 197if ( E ) S 198if ( E ) S else S 199while ( E ) S 200for ( E ; E ; E ) S 201null statement 202break 203continue 204quit 205a string of characters, enclosed in double quotes 206print E ,..., E 207.Ed 208.Pp 209A string may contain any character, except double quote. 210The if statement with an else branch is a non-portable extension. 211All three E's in a for statement may be empty. 212This is a non-portable extension. 213The continue and print statements are also non-portable extensions. 214.Pp 215The print statement takes a list of comma-separated expressions. 216Each expression in the list is evaluated and the computed 217value is printed and assigned to the variable `last'. 218No trailing newline is printed. 219The expression may also be a string enclosed in double quotes. 220Within these strings the following escape sequences may be used: 221.Sq \ea 222for bell (alert), 223.Sq \eb 224for backspace, 225.Sq \ef 226for formfeed, 227.Sq \en 228for newline, 229.Sq \er 230for carriage return, 231.Sq \et 232for tab, 233.Sq \eq 234for double quote and 235.Sq \e\e 236for backslash. 237Any other character following a backslash will be ignored. 238Strings will not be assigned to `last'. 239.Pp 240Function definitions 241.Bd -unfilled -offset indent 242define L ( L ,..., L ) { 243 auto L, ... , L 244 S; ... S 245 return ( E ) 246} 247.Ed 248.Pp 249As a non-portable extension, the opening brace of the define statement 250may appear on the next line. 251The return statement may also appear in the following forms: 252.Bd -unfilled -offset indent 253return 254return () 255return E 256.Ed 257.Pp 258The first two are equivalent to the statement 259.Dq return 0 . 260The last form is a non-portable extension. 261Not specifying a return statement is equivalent to writing 262.Dq return (0) . 263.Pp 264Functions available in the math library, which is loaded by specifying the 265.Fl l 266flag on the command line 267.Pp 268.Bl -tag -width j(n,x) -offset indent -compact 269.It s(x) 270sine 271.It c(x) 272cosine 273.It e(x) 274exponential 275.It l(x) 276log 277.It a(x) 278arctangent 279.It j(n,x) 280Bessel function 281.El 282.Pp 283All function arguments are passed by value. 284.Pp 285The value of a statement that is an expression is printed 286unless the main operator is an assignment. 287The value printed is assigned to the special variable `last'. 288This is a non-portable extension. 289A single dot may be used as a synonym for `last'. 290Either semicolons or newlines may separate statements. 291Assignment to 292.Ar scale 293influences the number of digits to be retained on arithmetic 294operations in the manner of 295.Xr dc 1 . 296Assignments to 297.Ar ibase 298or 299.Ar obase 300set the input and output number radix respectively. 301.Pp 302The same letter may be used as an array, a function, 303and a simple variable simultaneously. 304All variables are global to the program. 305`Auto' variables are pushed down during function calls. 306When using arrays as function arguments 307or defining them as automatic variables, 308empty square brackets must follow the array name. 309.Pp 310For example 311.Bd -literal -offset indent 312scale = 20 313define e(x){ 314 auto a, b, c, i, s 315 a = 1 316 b = 1 317 s = 1 318 for(i=1; 1==1; i++){ 319 a = a*x 320 b = b*i 321 c = a/b 322 if(c == 0) return(s) 323 s = s+c 324 } 325} 326.Ed 327.Pp 328defines a function to compute an approximate value of 329the exponential function and 330.Pp 331.Dl for(i=1; i<=10; i++) e(i) 332.Pp 333prints approximate values of the exponential function of 334the first ten integers. 335.Pp 336.Bd -literal -offset indent 337$ bc -l -e 'scale = 500; 2 * a(2^10000)' -e quit 338.Ed 339.Pp 340prints an approximation of pi. 341.Sh FILES 342.Bl -tag -width /usr/share/misc/bc.library -compact 343.It Pa /usr/share/misc/bc.library 344math library, read when the 345.Fl l 346option is specified on the command line. 347.El 348.Sh SEE ALSO 349.Xr dc 1 350.Pp 351"BC \- An Arbitrary Precision Desk-Calculator Language", 352.Pa /usr/share/doc/usd/06.bc/ . 353.Sh STANDARDS 354The 355.Nm 356utility is expected to conform to the 357.St -p1003.2 358specification. 359.Sh HISTORY 360The 361.Nm 362first command appeared in 363.At v6 . 364A complete rewrite of the 365.Nm 366command first appeared in 367.Ox 3.5 . 368.Sh AUTHORS 369The original version of the 370.Nm 371command was written by 372.An Robert Morris 373and 374.An Lorinda Cherry . 375The current version of the 376.Nm 377utility was written by 378.An Otto Moerbeek . 379.Sh BUGS 380.Ql Quit 381is interpreted when read, not when executed. 382.Pp 383Some non-portable extensions, as found in the GNU version of the 384.Nm 385utility are not implemented (yet). 386