9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

bc.1 (3756B)


      1 .TH BC 1 
      2 .SH NAME
      3 bc \- arbitrary-precision arithmetic language
      4 .SH SYNOPSIS
      5 .B bc
      6 [
      7 .B -c
      8 ]
      9 [
     10 .B -l
     11 ]
     12 [
     13 .B -s
     14 ]
     15 [
     16 .I file ...
     17 ]
     18 .SH DESCRIPTION
     19 .I Bc
     20 is an interactive processor for a language that resembles
     21 C but provides arithmetic on numbers of arbitrary length with up
     22 to 100 digits right of the decimal point.
     23 It takes input from any files given, then reads
     24 the standard input.
     25 The
     26 .B -l
     27 argument stands for the name
     28 of an arbitrary precision math library.
     29 The
     30 .B -s
     31 argument suppresses the automatic display
     32 of calculation results; all output is via the
     33 .B print
     34 command.
     35 .PP
     36 The following syntax for 
     37 .I bc
     38 programs is like that of C;
     39 .I L
     40 means letter
     41 .BR a - z ,
     42 .I E
     43 means expression,
     44 .I S
     45 means statement.
     46 .TF length(E)
     47 .TP
     48 Lexical
     49 .RS
     50 .HP
     51 comments are enclosed in
     52 .B /* */
     53 .HP
     54 newlines end statements
     55 .RE
     56 .TP
     57 Names
     58 .IP
     59 simple variables:
     60 .I L
     61 .br
     62 array elements:
     63 .IB L [ E ]
     64 .br
     65 The words
     66 .BR ibase ,
     67 .BR obase ,
     68 and
     69 .B scale
     70 .TP
     71 Other operands
     72 .IP
     73 arbitrarily long numbers with optional sign and decimal point.
     74 .RS
     75 .TP
     76 .BI ( E )
     77 .TP
     78 .BI sqrt( E )
     79 .TP
     80 .BI length( E )
     81 number of significant decimal digits
     82 .TP
     83 .BI scale( E )
     84 number of digits right of decimal point
     85 .TP
     86 .IB L ( E , ... ,\fIE\fP)
     87 function call
     88 .RE
     89 .TP
     90 Operators
     91 .RS
     92 .HP
     93 .B "+  -  *  /  %  ^\ "
     94 .RB ( %
     95 is remainder;
     96 .B ^
     97 is power)
     98 .HP
     99 .B "++  --\ "
    100 .TP
    101 .B "==  <=  >=  !=  <  >"
    102 .TP
    103 .B "=  +=  -=  *=  /=  %=  ^="
    104 .RE
    105 .TP
    106 Statements
    107 .RS
    108 .br
    109 .I E
    110 .br
    111 .B {
    112 .I S
    113 .B ;
    114 \&...
    115 .B ;
    116 .I S
    117 .B }
    118 .br
    119 .B "print"
    120 .I E
    121 .br
    122 .B "if ("
    123 .I E
    124 .B )
    125 .I S
    126 .br
    127 .B "while ("
    128 .I E
    129 .B )
    130 .I S
    131 .br
    132 .B "for ("
    133 .I E
    134 .B ;
    135 .I E
    136 .B ;
    137 .I E
    138 .B ")"
    139 .I  S
    140 .br
    141 null statement
    142 .br
    143 .B break
    144 .br
    145 .B quit
    146 .br
    147 \fL"\fRtext\fL"\fR
    148 .RE
    149 .TP
    150 Function definitions
    151 .RS
    152 .br
    153 .B define
    154 .I L
    155 .B (
    156 .I L
    157 .B ,
    158 \&...
    159 .B ,
    160 .I L
    161 .B ){
    162 .PD0
    163 .br
    164 .B auto
    165 .I L
    166 .B ,
    167 \&...
    168 .B ,
    169 .I L
    170 .br
    171 .I S
    172 .B ;
    173 \&...
    174 .B ;
    175 .I  S
    176 .br
    177 .B return
    178 .I E
    179 .LP
    180 .B }
    181 .RE
    182 .TP
    183 Functions in 
    184 .B -l
    185 math library
    186 .RS
    187 .TP
    188 .BI s( x )
    189 sine
    190 .TP
    191 .BI c( x )
    192 cosine
    193 .TP
    194 .BI e( x )
    195 exponential
    196 .TP
    197 .BI l( x )
    198 log
    199 .TP
    200 .BI a( x )
    201 arctangent
    202 .TP
    203 .BI j( "n, x" )
    204 Bessel function
    205 .RE
    206 .PP
    207 .DT
    208 All function arguments are passed by value.
    209 .PD
    210 .PP
    211 The value of an expression at the top level is printed
    212 unless the main operator is an assignment or the
    213 .B -s
    214 command line argument is given.
    215 Text in quotes, which may include newlines, is always printed.
    216 Either semicolons or newlines may separate statements.
    217 Assignment to
    218 .B scale
    219 influences the number of digits to be retained on arithmetic
    220 operations in the manner of
    221 .IR dc (1).
    222 Assignments to
    223 .B ibase
    224 or
    225 .B obase
    226 set the input and output number radix respectively.
    227 .PP
    228 The same letter may be used as an array, a function,
    229 and a simple variable simultaneously.
    230 All variables are global to the program.
    231 Automatic variables are pushed down during function calls.
    232 In a declaration of an array as a function argument
    233 or automatic variable
    234 empty square brackets must follow the array name.
    235 .PP
    236 .I Bc
    237 is actually a preprocessor for
    238 .IR dc (1),
    239 which it invokes automatically, unless the
    240 .B -c
    241 (compile only)
    242 option is present.
    243 In this case the
    244 .I dc
    245 input is sent to the standard output instead.
    246 .SH EXAMPLE
    247 Define a function to compute an approximate value of
    248 the exponential.
    249 Use it to print 10 values.
    250 (The exponential function in the library gives better answers.)
    251 .PP
    252 .EX
    253 scale = 20
    254 define e(x) {
    255 	auto a, b, c, i, s
    256 	a = 1
    257 	b = 1
    258 	s = 1
    259 	for(i=1; 1; i++) {
    260 		a *= x
    261 		b *= i
    262 		c = a/b
    263 		if(c == 0) return s
    264 		s += c
    265 	}
    266 }
    267 for(i=1; i<=10; i++) print e(i)
    268 .EE
    269 .SH FILES
    270 .B \*9/lib/bclib
    271 mathematical library
    272 .SH SOURCE
    273 .B \*9/src/cmd/bc.y
    274 .SH "SEE ALSO"
    275 .IR dc (1), 
    276 .IR hoc (1)
    277 .SH BUGS
    278 No
    279 .LR && ,
    280 .LR || ,
    281 or
    282 .L !
    283 operators.
    284 .PP
    285 A
    286 .L for
    287 statement must have all three
    288 .LR E s.
    289 .PP
    290 A
    291 .L quit
    292 is interpreted when read, not when executed.