Questions tagged [bc]

an arbitrary precision calculator language used in shell scripting. Use this tag for questions for programming-related uses of bc; general questions about bc usage and troubleshooting are a better fit at Unix & Linux Stack Exchange.

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

281 questions
0
votes
2 answers

What is Entity Object Instance?

I understand that Entity Object Definition is XML data that represents metadata and corresponds to DB table. Example:
0
votes
1 answer

how to use bc inside a for-loop

Inside a loop I have a variable to be used for the calculation, using bc, of another variable. However, the result I get is totally incorrect. I have 32 files, named test0, test1 and so on. My script reads as follow: for i in {0..31}; do declare…
ziulfer
  • 1,339
  • 5
  • 18
  • 30
0
votes
1 answer

illegal character and parse error

top -n 2 -d 5 | grep Cpu | awk 'NR==2' | sed -e "s/\%//g" | sed -e "s/\us,//g" > temp cpu_value=awk '{print $2}' temp cpu_calc=echo "scale=4; 100 - $cpu_value" | bc I am getting the below error: (standard_in) 1: illegal character: ^[ (standard_in)…
Suren
  • 37
  • 7
0
votes
2 answers

libreadline undefined symbol: PC in bash when using | bc -l

I am connecting by ssh to another machine to execute a bash script, whose problematic part is the following: fkeypar "ex1.fef[1]" TSTARTI #### fkeypar is an external command to get some values to assign to the subsequent…
Py-ser
  • 1,860
  • 9
  • 32
  • 58
0
votes
1 answer

* operator, shell script

I've got problem with * operator. I've tried to write a script, that take 3 paremater, first two are number (any type), the third is operator then print out result of the expression. Everything've work fine except with * operator. I've got…
Yukoo
  • 3
  • 1
0
votes
4 answers

GNU bc: -2^3 == -8, but -2^4 == 16?

currently working my way through the superb CS:APP whereupon a curious question arose while doing some two's complement exercises. GNU bc 1.06, default settings - no flags: -2 ^ 3 -8 ... but then ... -2 ^ 4 16 Question Why is -2 ^ 4 equal to…
swisscheese
  • 321
  • 3
  • 12
0
votes
2 answers

Substract 2 values from same column if 2 fields match on same line

I've got a formatted file in this way: 1 223614 225119.537745 Engine45 2 223614 225121.082392 Engine45 3 223614 225124.440309 Engine45 4 223614 225124.763890 Engine45 5 223621 225124.933927 Engine46 6 223614 225124.934205 Engine45 7 223614…
cdjve
  • 3
  • 1
0
votes
2 answers

does GNU bc offers a standard math library?

I'm asking if I need to import some kind of library to perform something like that echo "cos(1)" | bc and use BLAS and trigonometric functions in general.
user2485710
  • 9,451
  • 13
  • 58
  • 102
0
votes
2 answers

passing bash array values to bc

I have a bash array of floating point numbers, say it is called vals and intialized like this -- # load data from the datafile.txt vals=`cat datafile.txt` vals=($vals) The datafile.txt looks like this…
ramgorur
  • 2,104
  • 4
  • 26
  • 39
0
votes
1 answer

How can you echo input to the bc command from bash?

I am trying to create a bash alias or function which can pipe text to the stdin of the unix command bc. I first attempted: alias semitone="echo \"scale=10; e(l(2.))\" | bc" Which bash parses ok, but it fails with a bc runtime error: Runtime error…
ctpenrose
  • 1,467
  • 2
  • 18
  • 28
0
votes
1 answer

System Calls in Perl

$i = system(bc 110^151%14351); print($i); Hey everyone, I am attempting to use a system call to use a calculator installed on my linux machine. The calculators name is GNU bc, for the basic calculator. Basically it is an easy way to calculate…
wheatfairies
  • 355
  • 2
  • 7
  • 19
0
votes
1 answer

How can I pipe the output of curl to bc?

As part of a larger script, I want to get a numerical value stored in a text file on the web, , then open a terminal window, display that value, and send it to bc so I can interactively do calculations on it. I came up with this: konsole --hold -e…
Joseph
  • 733
  • 1
  • 4
  • 20
0
votes
4 answers

BASH without bc :: if [ echo `awk 'BEGIN{print 0.001>0.9}'` -eq 0 ]; then DO SOMETHING; fi

Please help. How can accomplish this if [ echo `awk 'BEGIN{print 0.001>0.9}'` -eq 0 ]; then DO SOMETHING; fi But that is wrong. What I'm trying to do is: if the first number(0.001) if greater than 0.9 then DO SOMETHING. else DO NOTHING The numbers…
andresg3
  • 343
  • 2
  • 6
  • 20
0
votes
1 answer

Is there a better way to simplify this IF statement

Is there a better way to simplify this IF statement. I've been told there is no need of using bc here because this adds to both computational and maintenance complexity to large scripts. Is there a way to avoid the use of bc here: if [ `bc -l <<<…
andresg3
  • 343
  • 2
  • 6
  • 20
0
votes
3 answers

How to find any decrement in the column?

I am trying to find out the decrements in a column and if found then print the last highest value. For example: From 111 to 445 there is a continous increment in the column.But 333 is less then the number before it. 111 aaa 112 aaa 112 aaa …
user2809888