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