1 .\"
   2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
   3 .\" permission to reproduce portions of its copyrighted documentation.
   4 .\" Original documentation from The Open Group can be obtained online at
   5 .\" http://www.opengroup.org/bookstore/.
   6 .\"
   7 .\" The Institute of Electrical and Electronics Engineers and The Open
   8 .\" Group, have given us permission to reprint portions of their
   9 .\" documentation.
  10 .\"
  11 .\" In the following statement, the phrase ``this text'' refers to portions
  12 .\" of the system documentation.
  13 .\"
  14 .\" Portions of this text are reprinted and reproduced in electronic form
  15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
  16 .\" Standard for Information Technology -- Portable Operating System
  17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
  18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
  19 .\" Engineers, Inc and The Open Group.  In the event of any discrepancy
  20 .\" between these versions and the original IEEE and The Open Group
  21 .\" Standard, the original IEEE and The Open Group Standard is the referee
  22 .\" document.  The original Standard can be obtained online at
  23 .\" http://www.opengroup.org/unix/online.html.
  24 .\"
  25 .\" This notice shall appear on any product containing this material.
  26 .\"
  27 .\" The contents of this file are subject to the terms of the
  28 .\" Common Development and Distribution License (the "License").
  29 .\" You may not use this file except in compliance with the License.
  30 .\"
  31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  32 .\" or http://www.opensolaris.org/os/licensing.
  33 .\" See the License for the specific language governing permissions
  34 .\" and limitations under the License.
  35 .\"
  36 .\" When distributing Covered Code, include this CDDL HEADER in each
  37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  38 .\" If applicable, add the following below this CDDL HEADER, with the
  39 .\" fields enclosed by brackets "[]" replaced with your own identifying
  40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
  41 .\"
  42 .\"
  43 .\" Copyright 1989 AT&T
  44 .\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
  45 .\" Copyright (c) 2003, Sun Microsystems, Inc.  All Rights Reserved.
  46 .\"
  47 .TH BC 1 "Aug 29, 2003"
  48 .SH NAME
  49 bc \- arbitrary precision arithmetic language
  50 .SH SYNOPSIS
  51 .nf
  52 \fB/usr/bin/bc\fR [\fB-c\fR] [\fB-l\fR] [\fIfile\fR]...
  53 .fi
  54 
  55 .LP
  56 .nf
  57 \fB/usr/xpg6/bin/bc\fR [\fB-c\fR] [\fB-l\fR] [\fIfile\fR]...
  58 .fi
  59 
  60 .SH DESCRIPTION
  61 The \fBbc\fR utility implements an arbitrary precision calculator. It takes
  62 input from any files given, then reads from the standard input. If the standard
  63 input and standard output to \fBbc\fR are attached to a terminal, the
  64 invocation of \fBbc\fR is  \fIinteractive\fR, causing behavioral constraints
  65 described in the following sections. \fBbc\fR processes a language that
  66 resembles C and is a preprocessor for the desk calculator program \fBdc\fR,
  67 which it invokes automatically unless the \fB-c\fR option is specified. In this
  68 case the \fBdc\fR input is sent to the standard output instead.
  69 .SH USAGE
  70 The syntax for  \fBbc\fR programs is as follows:
  71 .sp
  72 .ne 2
  73 .na
  74 \fB\fIL\fR\fR
  75 .ad
  76 .RS 5n
  77 Means a letter \fBa\fR\(mi\fBz\fR,
  78 .RE
  79 
  80 .sp
  81 .ne 2
  82 .na
  83 \fB\fIE\fR\fR
  84 .ad
  85 .RS 5n
  86 Means an expression: a (mathematical or logical) value, an operand  that takes
  87 a value, or a combination of operands and operators that evaluates to a value,
  88 .RE
  89 
  90 .sp
  91 .ne 2
  92 .na
  93 \fB\fIS\fR\fR
  94 .ad
  95 .RS 5n
  96 Means a statement.
  97 .RE
  98 
  99 .SS "Comments"
 100 Enclosed in \fB/*\fR and \fB*/\fR.
 101 .SS "Names (Operands)"
 102 .in +2
 103 Simple variables: \fIL\fR.
 104 .in -2
 105 .br
 106 .in +2
 107 Array elements: \fIL\fR [ \fIE\fR ] (up to \fBBC_DIM_MAX\fR dimensions).
 108 .in -2
 109 .br
 110 .in +2
 111 The words \fBibase\fR, \fBobase\fR (limited to \fBBC_BASE_MAX\fR), and
 112 \fBscale\fR (limited to \fBBC_SCALE_MAX\fR).
 113 .in -2
 114 .SS "Other Operands"
 115 Arbitrarily long numbers with optional sign and decimal point. Strings of fewer
 116 than \fBBC_STRING_MAX\fR characters, between double quotes ("). \fB(\fR \fIE\fR
 117 \fB)\fR
 118 .sp
 119 .ne 2
 120 .na
 121 \fB\fBsqrt ( \fR\fIE\fR\fB )\fR\fR
 122 .ad
 123 .RS 21n
 124 Square root
 125 .RE
 126 
 127 .sp
 128 .ne 2
 129 .na
 130 \fB\fBlength ( \fR\fIE\fR\fB )\fR\fR
 131 .ad
 132 .RS 21n
 133 Number of significant decimal digits.
 134 .RE
 135 
 136 .sp
 137 .ne 2
 138 .na
 139 \fB\fBscale ( \fR\fIE\fR\fB )\fR\fR
 140 .ad
 141 .RS 21n
 142 Number of digits right of decimal point.
 143 .RE
 144 
 145 .sp
 146 .ne 2
 147 .na
 148 \fB\fIL \fR\fB( \fR\fIE\fR , ... , \fIE\fR\fB )\fR\fR
 149 .ad
 150 .RS 21n
 151 
 152 .RE
 153 
 154 .SS "Operators"
 155 .ne 2
 156 .na
 157 \fB\fB+   \(mi   *   /   %   ^\fR\fR
 158 .ad
 159 .sp .6
 160 .RS 4n
 161 (\fB%\fR is remainder; \fB^\fR is power)
 162 .RE
 163 
 164 .sp
 165 .ne 2
 166 .na
 167 \fB\fB++   \(mi\(mi \fR\fR
 168 .ad
 169 .sp .6
 170 .RS 4n
 171 (prefix and postfix; apply to names)
 172 .RE
 173 
 174 .sp
 175 .ne 2
 176 .na
 177 \fB\fB==   <=   >=   !=   <   >\fR\fR
 178 .ad
 179 .sp .6
 180 .RS 4n
 181 
 182 .RE
 183 
 184 .sp
 185 .ne 2
 186 .na
 187 \fB\fB=   =+   =\(mi   =*   =/   =%   =^\fR\fR
 188 .ad
 189 .sp .6
 190 .RS 4n
 191 
 192 .RE
 193 
 194 .SS "Statements"
 195 .in +2
 196 \fIE\fR
 197 .in -2
 198 .br
 199 .in +2
 200 \fB{\fR \fIS\fR \fB;\fR.\|.\|. \fB;\fR \fIS\fR \fB}\fR
 201 .in -2
 202 .br
 203 .in +2
 204 \fBif (\fR \fIE\fR \fB)\fR \fIS\fR
 205 .in -2
 206 .br
 207 .in +2
 208 \fBwhile (\fR \fIE\fR \fB)\fR \fIS\fR
 209 .in -2
 210 .br
 211 .in +2
 212 \fBfor (\fR \fIE\fR \fB;\fR \fIE\fR \fB;\fR \fIE\fR \fB)\fR \fIS\fR
 213 .in -2
 214 .br
 215 .in +2
 216 null statement
 217 .in -2
 218 .br
 219 .in +2
 220 \fBbreak\fR
 221 .in -2
 222 .br
 223 .in +2
 224 \fBquit\fR
 225 .in -2
 226 .sp
 227 .LP
 228 \&.string
 229 .SS "Function Definitions"
 230 .in +2
 231 \fBdefine\fR \fIL\fR \fB(\fR \fIL\fR \fB,\fR.\|.\|.\|\fB,\fR \fIL\fR \fB) {\fR
 232 .in -2
 233 .br
 234 .in +2
 235 \fBauto\fR \fIL\fR \fB,\fR.\|.\|.\|\fB,\fR \fIL\fR
 236 .in -2
 237 .br
 238 .in +2
 239 \fIS\fR \fB;\fR.\|.\|.\| \fIS\fR
 240 .in -2
 241 .br
 242 .in +2
 243 \fBreturn (\fR \fIE\fR \fB)\fR
 244 .in -2
 245 .br
 246 .in +2
 247 \fB}\fR
 248 .in -2
 249 .SS "Functions in \fB-l\fR Math Library"
 250 .ne 2
 251 .na
 252 \fB\fBs(\fR\fIx\fR\fB)\fR\fR
 253 .ad
 254 .RS 10n
 255 sine
 256 .RE
 257 
 258 .sp
 259 .ne 2
 260 .na
 261 \fB\fBc(\fR\fIx\fR\fB)\fR\fR
 262 .ad
 263 .RS 10n
 264 cosine
 265 .RE
 266 
 267 .sp
 268 .ne 2
 269 .na
 270 \fB\fBe(\fR\fIx\fR\fB)\fR\fR
 271 .ad
 272 .RS 10n
 273 exponential
 274 .RE
 275 
 276 .sp
 277 .ne 2
 278 .na
 279 \fB\fBl(\fR\fIx\fR\fB)\fR\fR
 280 .ad
 281 .RS 10n
 282 log
 283 .RE
 284 
 285 .sp
 286 .ne 2
 287 .na
 288 \fB\fBa(\fR\fIx\fR\fB)\fR\fR
 289 .ad
 290 .RS 10n
 291 arctangent
 292 .RE
 293 
 294 .sp
 295 .ne 2
 296 .na
 297 \fB\fBj(\fR\fIn\fR\fB,\fR\fIx)\fR\fR
 298 .ad
 299 .RS 10n
 300 Bessel function
 301 .RE
 302 
 303 .sp
 304 .LP
 305 All function arguments are passed by value.
 306 .sp
 307 .LP
 308 The value of a statement that is an expression is printed unless the main
 309 operator is an assignment. Either semicolons or new-lines may separate
 310 statements. Assignment to \fBscale\fR influences the number of digits to be
 311 retained on arithmetic operations in the manner of \fBdc\fR. Assignments to
 312 \fBibase\fR or \fBobase\fR set the input and output number radix respectively.
 313 .sp
 314 .LP
 315 The same letter may be used as an array, a function, and a simple variable
 316 simultaneously. All variables are global to the program. \fBauto\fR variables
 317 are stacked during function calls. When using arrays as function arguments or
 318 defining them as automatic variables, empty square brackets must follow the
 319 array name.
 320 .SH OPTIONS
 321 The following operands are supported:
 322 .sp
 323 .ne 2
 324 .na
 325 \fB\fB-c\fR\fR
 326 .ad
 327 .RS 6n
 328 Compiles only. The output is \fBdc\fR commands that are sent to the standard
 329 output.
 330 .RE
 331 
 332 .SS "/usr/bin/bc"
 333 .ne 2
 334 .na
 335 \fB\fB-l\fR\fR
 336 .ad
 337 .RS 6n
 338 Defines the math functions and initializes \fBscale\fR to \fB20\fR, instead of
 339 the default zero.
 340 .RE
 341 
 342 .SS "/usr/xpg6/bin/bc"
 343 .ne 2
 344 .na
 345 \fB\fB-l\fR\fR
 346 .ad
 347 .RS 6n
 348 Defines the math functions and initializes \fBscale\fR to \fB20\fR, instead of
 349 the default zero. All math results have the scale of \fB20\fR.
 350 .RE
 351 
 352 .SH OPERANDS
 353 The following operands are supported:
 354 .sp
 355 .ne 2
 356 .na
 357 \fB\fIfile\fR\fR
 358 .ad
 359 .RS 8n
 360 A pathname of a text file containing \fBbc\fR program statements. After all
 361 cases of \fIfile\fR have been read, \fBbc\fR reads the standard input.
 362 .RE
 363 
 364 .SH EXAMPLES
 365 \fBExample 1 \fRSetting the precision of a variable
 366 .sp
 367 .LP
 368 In the shell, the following assigns an approximation of the first ten digits of
 369 \fBn\fR to the variable \fIx\fR:
 370 
 371 .sp
 372 .in +2
 373 .nf
 374 \fBx=$(printf "%s\en" 'scale = 10; 104348/33215' | bc)\fR
 375 .fi
 376 .in -2
 377 .sp
 378 
 379 .LP
 380 \fBExample 2 \fRDefining a computing function
 381 .sp
 382 .LP
 383 Defines a function to compute an approximate value of the exponential function:
 384 
 385 .sp
 386 .in +2
 387 .nf
 388 \fBscale = 20
 389 define e(x){
 390         auto a, b, c, i, s
 391         a = 1
 392         b = 1
 393         s = 1
 394         for(i=1; 1==1; i++){
 395                 a = a*x
 396                 b = b*i
 397                 c = a/b
 398                 if(c == 0) return(s)
 399                 s = s+c
 400         }
 401 }\fR
 402 .fi
 403 .in -2
 404 .sp
 405 
 406 .LP
 407 \fBExample 3 \fRPrinting the approximate values of the function
 408 .sp
 409 .LP
 410 Prints approximate values of the exponential function of the first ten
 411 integers:
 412 
 413 .sp
 414 .in +2
 415 .nf
 416 \fBfor(i=1; i<=10; i++) e(i)\fR
 417 .fi
 418 .in -2
 419 .sp
 420 
 421 .sp
 422 .LP
 423 or
 424 
 425 .sp
 426 .in +2
 427 .nf
 428 \fBfor (i = 1; i <= 10; ++i) {         e(i) }\fR
 429 .fi
 430 .in -2
 431 .sp
 432 
 433 .SH ENVIRONMENT VARIABLES
 434 See \fBenviron\fR(5) for descriptions of the following environment variables
 435 that affect the execution of \fBbc\fR: \fBLANG\fR, \fBLC_ALL\fR,
 436 \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, and \fBNLSPATH\fR.
 437 .SH EXIT STATUS
 438 The following exit values are returned:
 439 .sp
 440 .ne 2
 441 .na
 442 \fB\fB0\fR\fR
 443 .ad
 444 .RS 15n
 445 All input files were processed successfully.
 446 .RE
 447 
 448 .sp
 449 .ne 2
 450 .na
 451 \fB\fBunspecified\fR\fR
 452 .ad
 453 .RS 15n
 454 An error occurred.
 455 .RE
 456 
 457 .SH FILES
 458 .ne 2
 459 .na
 460 \fB\fB/usr/lib/lib.b\fR\fR
 461 .ad
 462 .RS 25n
 463 mathematical library
 464 .RE
 465 
 466 .sp
 467 .ne 2
 468 .na
 469 \fB\fB/usr/include/limits.h\fR\fR
 470 .ad
 471 .RS 25n
 472 to define BC_ parameters
 473 .RE
 474 
 475 .SH ATTRIBUTES
 476 See \fBattributes\fR(5) for descriptions of the following attributes:
 477 .sp
 478 
 479 .sp
 480 .TS
 481 box;
 482 c | c
 483 l | l .
 484 ATTRIBUTE TYPE  ATTRIBUTE VALUE
 485 _
 486 Interface Stability     Standard
 487 .TE
 488 
 489 .SH SEE ALSO
 490 \fBdc\fR(1), \fBawk\fR(1), \fBattributes\fR(5), \fBenviron\fR(5),
 491 \fBstandards\fR(5)
 492 .SH NOTES
 493 The \fBbc\fR command does not recognize the logical operators \fB&&\fR and
 494 \fB|\||\fR.
 495 .sp
 496 .LP
 497 The \fBfor\fR statement must have all three expressions (\fIE\fR's).