Running bc
bc
Start interactive calculator
bc -l
Math library and scale 20
echo '<expr>' | bc -l
Evaluate an expression from a pipe
bc -e '<expr>'
Evaluate an expression argument
bc <file>
Run a script file, then read stdin
bc -f <file>
Run a file, exit afterwards
bc -q
Skip banner (GNU compatibility)
bc -s
Accept strict POSIX bc only
bc -w
Warn about non-POSIX extensions
bc -S <n> -I <base> -O <base>
Preset scale, ibase, obase
bc -P
Disable the interactive prompt
bc -z
Print leading zero (0.5, not .5)
bc -L
Do not wrap long output lines
bc --version
Show version
BC_LINE_LENGTH=0 bc
Same as -L via environment
Default wrap is 70 digits with a backslash, which breaks
piping long results; 0 turns it off.
BC_ENV_ARGS="-l"
Default options for every run
Precision and bases
scale=<n>
Digits kept after the decimal point
scale(<x>)
Digits after the point of a value
obase=<n>
Output base (2 to max)
ibase=<n>
Input base (2 to 36)
obase=16; <n>
Convert decimal to hex
ibase=16; <HEX>
Read hex as input (uppercase digits)
obase=0
Scientific notation output
obase=1
Engineering notation output
<n>e<exp>
Scientific input, e.g. 1.5e3
<x>@<n>
Compute x at scale n
$<x>
Truncate to scale 0 digits
x<<n x>>n
Shift decimal point by n digits
maxibase() maxobase() maxscale()
Query the limits
last or .
Last printed result
Operators
+ - * / %
Basic arithmetic and remainder
Modulus honours scale; with scale=0 it is the integer
remainder.
^
Power with integer exponent
++x x-- x+=1
Increment and compound assignment
== != < <= > >=
Comparisons giving 1 or 0
&& || !
Boolean and, or, not
length(<x>)
Number of significant digits
divmod(<a>, <b>, r[])
Quotient; remainder lands in r[0]
modexp(<b>, <e>, <m>)
Modular exponentiation (integers)
rand() irand(<n>)
Random number; random 0..n-1
read()
Read an expression from stdin
Math library (-l)
a(<x>)
Arctangent in radians
j(<x>, <n>)
Bessel function of order n
4*a(1)
Pi to current scale
pi(<p>)
Pi at precision p
l(<x>)/l(10)
Log base 10 (or l10(x))
log(<x>, <b>)
Logarithm in any base
p(<x>, <y>)
Power with non-integer exponent
r(<x>, <p>)
Round to p places
ceil(<x>, <p>)
Round away from zero
root(<x>, <n>) cbrt(<x>)
Nth root; cube root
comb(<n>, <k>) perm(<n>, <k>)
Combinations; permutations
fib(<n>)
Fibonacci number
gcd(<a>, <b>) lcm(<a>, <b>)
Greatest common divisor; least common multiple
max(<a>, <b>) min(<a>, <b>)
Larger or smaller of two
band(<a>, <b>) bor(<a>, <b>)
bxor(<a>, <b>)
Bitwise operations on integers
bshl(<x>, <n>) bshr(<x>, <n>)
Bit shifts
hex(<x>) binary(<x>)
Print integer as hex or binary
int8(<x>) uint32(<x>)
Print as signed or unsigned fixed width
r2d(<x>) d2r(<x>)
Radians to degrees and back
Programming
define <f>(<a>, <b>) { ... }
Define a function
define void <f>() { ... }
Function that returns no value
auto <i>, <j>
Local variables (first line in function)
Without auto, variables are global and shared with the caller.
return (<x>)
Return a value
if (<c>) { } else { }
Conditional
while (<c>) { ... }
Loop while true
for (i=0; i<10; i++) { ... }
Counting loop
break continue
Leave or skip a loop iteration
a[<i>] = <x>
Array element (zero-based)
define <f>(*<a>[])
Pass array by reference
print "<text>", <x>, "\n"
Print without implicit newline
"<text>"
String literal prints as is
limits
Print implementation limits