Questions tagged [bc]

Arbitrary-precision arithmetic language.

bc, for basic calculator, is "an arbitrary-precision calculator language" using algebraic infix notation. bc is typically used as either a mathematical scripting language or as an interactive mathematical shell.

Typical Usages

There are two ways bc is typically used.

  • From a Unix command prompt, then interactively entering a mathematical expressions, such as (1 + 3) * 2, which will display 8.

  • From the shell command prompt, where bc reads from standard input. For example $ echo '(1 + 3) * 2' | bc will display 8 in the shell.

While bc can perform calculations to arbitrary precision, its default behavior is to truncate calculations to whole numbers. I.e. entering the expression 2/3 displays 0. This can surprise new bc users. The number of digits that bc displays is determined by a variable scale, which may be changed. For example, running bc interactively, setting scale=7 then entering 2/3 will display .6666666. Starting bc with the -l option loads a math library, setting scale to 20 and loading these math functions

s (x)    The sine of x, x is in radians.
c (x)    The cosine of x, x is in radians.
a (x)    The arctangent of x, arctangent returns radians.
l (x)    The natural logarithm of x.
e (x)    The exponential function of raising e to the value x.
j (n,x)  The bessel function of integer order n of x.

In 1991, POSIX rigorously defined and standardized bc. Two implementations of that standard survive today:

  1. Traditional bc, on Unix and Plan 9 systems.

  2. GNU bc with numerous extensions beyond the POSIX standard, on Linux, etc.

Links & References

112 questions
70
votes
1 answer

Why is 'bc' required to build the Linux kernel?

The Linux kernel minimal building requirements specifies that the calculator bc is required to build kernel v4.10, the minimal version of the tool being 1.06.95. What use is made of bc in this context, and why isn't the C language directly used…
Ikaros
  • 872
  • 6
  • 14
64
votes
4 answers

Are operations on floats supported with bc?

It looks like bc doesn't support float operations. When I do echo 1/8 | bc it gets me a zero. I checked the manual page bc (1), but it doesn't even mention float, so I wonder if it's supported?
daisy
  • 53,527
  • 78
  • 236
  • 383
55
votes
4 answers

How is bc different from dc?

What are the differences between dc and bc calculators? When should I use dc and when bc?
syntagma
  • 12,091
  • 21
  • 57
  • 74
42
votes
6 answers

Append to a pipe and pass on?

I have a simple bash function dividing two numbers: echo "750/12.5" | bc I'd like to take the output from bc and append /24 and pipe said result to another instance of bc. Something like: echo "750/12.5" | bc | echo $1 + "/24" | bc Where $1 is the…
Philip Kirkbride
  • 9,816
  • 25
  • 95
  • 167
34
votes
8 answers

Doing simple math on the command line using bash functions: $1 divided by $2 (using bc perhaps)

Sometimes I need to divide one number by another. It would be great if I could just define a bash function for this. So far, I am forced to use expressions like echo 'scale=25;65320/670' | bc but it would be great if I could define a .bashrc…
ixtmixilix
  • 13,040
  • 27
  • 82
  • 118
34
votes
6 answers

Understand "ibase" and "obase" in case of conversions with bc?

I often use bc utility for converting hex to decimal and vice versa. However, it is always bit trial and error how ibase and obase should be configured. For example here I want to convert hex value C0 to decimal: $ echo "ibase=F;obase=A;C0" |…
Martin
  • 7,284
  • 40
  • 125
  • 208
30
votes
4 answers

python vs bc in evaluating 6^6^6

I am evaluating the expression 6^6^6 using python and bc separately. The content of the python file is print 6**6**6. When I execute time python test.py, I get the output as real 0m0.067s user 0m0.050s sys 0m0.011s And…
ganessh
  • 435
  • 5
  • 7
28
votes
1 answer

Does bc support hex calculations?

I'm trying to do a hex calculation directly with bc, I already specified the scale. echo 'scale=16;c06b1000-c06a5e78' | bc But I still get a zero. What could be wrong?
daisy
  • 53,527
  • 78
  • 236
  • 383
23
votes
1 answer

bc: Why does `ibase=16; obase=10; FF` returns FF and not 255?

I've been using bc to convert numbers between binary to hex, octal to decimal and others. In the following example, I was trying to convert base 16 (hex) number to binary, octal and decimal. I don't have any problem with the first 2 attempts. $ echo…
user264359
16
votes
4 answers

Calculate variable, and output it to another variable

The only calculator I know is bc. I want to add 1 to a variable, and output to another variable. I got the nextnum variable from counting string in a file: nextnum=`grep -o stringtocount file.tpl.php | wc -w` Lets say the nextnum value is 1. When…
apasajja
  • 1,917
  • 5
  • 17
  • 19
16
votes
3 answers

How to show zero before decimal point in bc?

echo "scale=3;1/8" | bc shows .125 on the screen. How to show 0.125 if the output result is less than one?
Kevin Dong
  • 1,139
  • 1
  • 9
  • 18
14
votes
3 answers

BC—automatic full precision multiplication

High, I need to test my arbitrary precision calculator, and bc seems like a nice yardstick to compare to, however, bc does truncate the result of each multiplication to what seems to be the maximum scale of the involved operands each. Is there a…
Petr Skocik
  • 28,176
  • 14
  • 81
  • 141
14
votes
2 answers

Is it possible to save `bc` command line history?

bash has a handy file .bash_history in which it saves the history of commands and on the next execution of bash the history is populated with saved commands. Is it possible to save bc command history to a file in the same way and then load it on…
KamilCuk
  • 850
  • 8
  • 16
12
votes
4 answers

how to make bc to show me 10 and not 10.00

#!/bin/bash q=$(bc <<< "scale=2;$p*100") head -n$q numbers.txt > secondcoordinate.txt That's just part of the script, but I think it's enough to clarify my intentions. p is a variable with just two decimals, so q should be an integer...…
Diego
  • 289
  • 2
  • 6
11
votes
2 answers

How to define a `bc` function for later use?

I've always found bc kind of mysterious and intriguing. It was one of the original Unix programs. And it's a programming language unto itself. So I gladly take any chance I can find to use it. Since bc doesn't seem to include a factorial function, I…
ixtmixilix
  • 13,040
  • 27
  • 82
  • 118
1
2 3 4 5 6 7 8