1.\" $OpenBSD: bc.1,v 1.17 2004/08/25 21:59:59 jmc Exp $ 2.\" $DragonFly: src/usr.bin/bc/bc.1,v 1.1 2004/09/20 04:20:34 dillon 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 August 8, 1991 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 Ar file ... 47.Sh DESCRIPTION 48.Nm 49is an interactive processor for a language which resembles 50C but provides unlimited precision arithmetic. 51It takes input from any files given, then reads 52the standard input. 53.Pp 54Options available: 55.Bl -tag -width Ds 56.It Fl c 57.Nm 58is actually a preprocessor for 59.Xr dc 1 , 60which it invokes automatically, unless the 61.Fl c 62.Pq compile only 63option is present. 64In this case the generated 65.Xr dc 1 66instructions are sent to the standard output, 67instead of being interpreted by a running 68.Xr dc 1 69process. 70.It Fl l 71Allow specification 72of an arbitrary precision math library. 73.El 74.Pp 75The syntax for 76.Nm 77programs is as follows: 78.Sq L 79means letter a-z; 80.Sq E 81means expression; 82.Sq S 83means statement. 84As a non-portable extension, it is possible to use long names 85in addition to single letter names. 86A long name is a sequence starting with a lowercase letter 87followed by any number of lowercase letters and digits. 88The underscore character 89.Pq Sq _ 90counts as a letter. 91.Pp 92Comments 93.Bd -unfilled -offset indent -compact 94are enclosed in /* and */ 95are enclosed in # and the next newline 96.Ed 97.Pp 98The newline is not part of the line comment, 99which in itself is a non-portable extension. 100.Pp 101Names 102.Bd -unfilled -offset indent -compact 103simple variables: L 104array elements: L [ E ] 105The words `ibase', `obase', and `scale' 106The word `last' or a single dot 107.Ed 108.Pp 109Other operands 110.Bd -unfilled -offset indent -compact 111arbitrarily long numbers with optional sign and decimal point 112( E ) 113sqrt ( E ) 114length ( E ) number of significant decimal digits 115scale ( E ) number of digits right of decimal point 116L ( E , ... , E ) 117.Ed 118.Pp 119The sequence 120.Sq \e<newline><whitespace> 121is ignored within numbers. 122.Pp 123Operators 124.Pp 125The following arithmetic and logical operators can be used. 126The semantics of the operators is the same as in the C language. 127They are listed in order of decreasing precedence. 128Operators in the same group have the same precedence. 129.Bl -column -offset indent "= += \-= *= /= %= ^=" "Associativity" \ 130"multiply, divide, modulus" 131.It Sy "Operator" Ta Sy "Associativity" Ta Sy "Description" 132.It "++ \-\-" Ta "none" Ta "increment, decrement" 133.It "\-" Ta "none" Ta "unary minus" 134.It "^" Ta "right" Ta "power" 135.It "* / %" Ta "left" Ta "multiply, divide, modulus" 136.It "+ \-" Ta "left" Ta "plus, minus" 137.It "= += -= *= /= %= ^=" Ta "right" Ta "assignment" 138.It "== <= >= != < >" Ta "none" Ta "relational" 139.It "!" Ta "none" Ta "boolean not" 140.It "&&" Ta "left" Ta "boolean and" 141.It "||" Ta "left" Ta "boolean or" 142.El 143.Pp 144Note the following: 145.Bl -bullet -offset indent 146.It 147The relational operators may appear in any expression. 148The 149.St -p1003.2 150standard only allows them in the conditional expression of an 151.Sq if , 152.Sq while 153or 154.Sq for 155statement. 156.It 157The relational operators have a lower precedence than the assignment 158operators. 159This has the consequence that the expression 160.Sy a = b < c 161is interpreted as 162.Sy (a = b) < c , 163which is probably not what the programmer intended. 164.It 165In contrast with the C language, the relational operators all have 166the same precedence, and are non-associative. 167The expression 168.Sy a < b < c 169will produce a syntax error. 170.It 171The boolean operators (!, && and ||) are non-portable extensions. 172.It 173The boolean not 174(!) operator has much lower precedence than the same operator in the 175C language. 176This has the consequence that the expression 177.Sy !a < b 178is interpreted as 179.Sy !(a < b) . 180Prudent programmers use parentheses when writing expressions involving 181boolean operators. 182.El 183.Pp 184Statements 185.Bd -unfilled -offset indent -compact 186E 187{ S ; ... ; S } 188if ( E ) S 189if ( E ) S else S 190while ( E ) S 191for ( E ; E ; E ) S 192null statement 193break 194continue 195quit 196a string of characters, enclosed in double quotes 197print E ,..., E 198.Ed 199.Pp 200A string may contain any character, except double quote. 201The if statement with an else branch is a non-portable extension. 202All three E's in a for statement may be empty. 203This is a non-portable extension. 204The continue and print statements are also non-portable extensions. 205.Pp 206The print statement takes a list of comma-separated expressions. 207Each expression in the list is evaluated and the computed 208value is printed and assigned to the variable `last'. 209No trailing newline is printed. 210The expression may also be a string enclosed in double quotes. 211Within these strings the following escape sequences may be used: 212.Sq \ea 213for bell (alert), 214.Sq \eb 215for backspace, 216.Sq \ef 217for formfeed, 218.Sq \en 219for newline, 220.Sq \er 221for carriage return, 222.Sq \et 223for tab, 224.Sq \eq 225for double quote and 226.Sq \e\e 227for backslash. 228Any other character following a backslash will be ignored. 229Strings will not be assigned to `last'. 230.Pp 231Function definitions 232.Bd -unfilled -offset indent 233define L ( L ,..., L ) { 234 auto L, ... , L 235 S; ... S 236 return ( E ) 237} 238.Ed 239.Pp 240As a non-portable extension, the opening brace of the define statement 241may appear on the next line. 242The return statement may also appear in the following forms: 243.Bd -unfilled -offset indent 244return 245return () 246return E 247.Ed 248.Pp 249The first two are equivalent to the statement 250.Dq return 0 . 251The last form is a non-portable extension. 252Not specifying a return statement is equivalent to writing 253.Dq return (0) . 254.Pp 255Functions available in the math library, which is loaded by specifying the 256.Fl l 257flag on the command line 258.Pp 259.Bl -tag -width j(n,x) -offset indent -compact 260.It s(x) 261sine 262.It c(x) 263cosine 264.It e(x) 265exponential 266.It l(x) 267log 268.It a(x) 269arctangent 270.It j(n,x) 271Bessel function 272.El 273.Pp 274All function arguments are passed by value. 275.Pp 276The value of a statement that is an expression is printed 277unless the main operator is an assignment. 278The value printed is assigned to the special variable `last'. 279This is a non-portable extension. 280A single dot may be used as a synonym for `last'. 281Either semicolons or newlines may separate statements. 282Assignment to 283.Ar scale 284influences the number of digits to be retained on arithmetic 285operations in the manner of 286.Xr dc 1 . 287Assignments to 288.Ar ibase 289or 290.Ar obase 291set the input and output number radix respectively. 292.Pp 293The same letter may be used as an array, a function, 294and a simple variable simultaneously. 295All variables are global to the program. 296`Auto' variables are pushed down during function calls. 297When using arrays as function arguments 298or defining them as automatic variables, 299empty square brackets must follow the array name. 300.Pp 301For example 302.Bd -literal -offset indent 303scale = 20 304define e(x){ 305 auto a, b, c, i, s 306 a = 1 307 b = 1 308 s = 1 309 for(i=1; 1==1; i++){ 310 a = a*x 311 b = b*i 312 c = a/b 313 if(c == 0) return(s) 314 s = s+c 315 } 316} 317.Ed 318.Pp 319defines a function to compute an approximate value of 320the exponential function and 321.Pp 322.Dl for(i=1; i<=10; i++) e(i) 323.Pp 324prints approximate values of the exponential function of 325the first ten integers. 326.Sh FILES 327.Bl -tag -width /usr/share/misc/bc.library -compact 328.It Pa /usr/share/misc/bc.library 329math library, read when the 330.Fl l 331option is specified on the command line. 332.El 333.Sh SEE ALSO 334.Xr dc 1 335.Pp 336"BC \- An Arbitrary Precision Desk-Calculator Language", 337.Pa /usr/share/doc/usd/06.bc/ . 338.Sh STANDARDS 339The 340.Nm 341utility is expected to conform to the 342.St -p1003.2 343specification. 344.Sh HISTORY 345The 346.Nm 347first command appeared in 348.At v6 . 349A complete rewrite of the 350.Nm 351command first appeared in 352.Ox 3.5 . 353.Sh AUTHORS 354The original version of the 355.Nm 356command was written by 357.An Robert Morris 358and 359.An Lorinda Cherry . 360The current version of the 361.Nm 362utility was written by 363.An Otto Moerbeek . 364.Sh BUGS 365.Ql Quit 366is interpreted when read, not when executed. 367.Pp 368Some non-portable extensions, as found in the GNU version of the 369.Nm 370utility are not implemented (yet). 371