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