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

Shell error: redirection unexpected in using bc

Error is in stroke: printf "%15s\t%5d\t%5.2f%%\n" bc <<< "scale=2; 100 * $mac / $totals" > result i cant understand it. Before bc and after $totals" is sign `
user1925442
  • 45
  • 2
  • 2
  • 6
0
votes
2 answers

round each row in a file bash

I'm processing some data using bash. I'm checking whether the data is complete by summing up the first column. It should consist of 0 - 500 with 0.1 increment. Unfortunately, the actual numbers are slightly off so that the sum can equal …
Mr.Weathers
  • 398
  • 1
  • 5
  • 19
0
votes
1 answer

scientific format within bash shell

Within bash shell, I need to add numbers in the format : 0.13281E-04 and -0.79254E-04. So, in the script, I have: tt1=`echo $var_t1| sed -e 's/[eE]+*/\\*10\\^/'` tt2=`echo $var_t2| sed -e 's/[eE]+*/\\*10\\^/'` var_t=`echo "($tt1 + $tt2)/2.0" |…
BaRud
  • 3,055
  • 7
  • 41
  • 89
0
votes
3 answers

Parsing XML - Subtracting values from tags

I have the following xml structure in a very large file: 619709.6044;144998.7059;-090372.58119 0;0;0 255;0;255 How do I subtract the values in the sit tag? The tag is comprised of three values separated by ; Each…
John Mac
  • 3
  • 2
0
votes
1 answer

bcadding very small floats gives 0

Thanks to stack, I learned about floating point imprecision, so I went over to the bc functions. That works great on "normal" floats, but with extremely small floats, say 10^-10 types, bcadd always gives 0. Can someone show me what I'm doing wrong…
user1382306
0
votes
3 answers

Bash bc calculator evaluation, multiple conditions with NOT evaluations

Long story short, is it possible to make bc evaluate the following expression? echo "!(2.2 >= 0) && !(2.2 < 10)" | bc bash: !: event not found I know this works: echo "(2.2 >= 0) && (2.2 < 10)" | bc 1 So what am I doing wrong? The bc man page said…
IDDQD
  • 3,543
  • 8
  • 29
  • 40
0
votes
2 answers

How do I change a couple value/error from the value+-error format to the value(error)format?

I have a few data text files whose lines are values with their error, like these: ... 6 90.3785794742981 0.0952997386139722 40 1028.46336161948 4.41798447319325 ... the third column is the error relativley to the second column. I would like to…
Ferdinando Randisi
  • 4,068
  • 6
  • 32
  • 43
0
votes
1 answer

Executing BC Calculations from C#

i'm writing an app that needs a calculation support (Fun project). However, is there a way to enter input data to the interactive console in BC from C#, and then read the data from the console with C#? I have tried to use dllexport in the c source,…
Biohazard
  • 103
  • 1
  • 1
  • 5
-1
votes
3 answers

working with bc

trying to sum elements in an array using bc,i have a file with names and thier vaules if the names appears 3 times i should multiply its value with 3 then find the sum of all the elements together,im seeing standard input error $ cat foo.txt max…
-1
votes
1 answer

Difference between 'bc' and 'bc -l' in Bash when I try to find modulus of a number

Why does Unix give the result 0 when I execute the following command? echo "7%2" | bc -l And give result 1 when I execute the following command? echo "7%2" | bc
jmandy
  • 1
  • 1
-1
votes
2 answers

How to get printf to treat the variables passed from bc script differently?

I'm writing a program for CodeWars that requires me to return 2 specific variables with a space between them. The following is the script that I am trying to execute !/bin/bash nbMonths() { printf "%.0f\n" $( bc <<< " scale=5 …
tankris
  • 1
  • 5
-1
votes
2 answers

Does syntax of bash variables change when being passed to bc?

I noticed a solution on Codewars which had the following syntax: #!/bin/bash seven () { bc <<< " scale=0 counter=0 m=$1 while( m > 99 ) { counter = counter + 1 x = m / 10 y = m % 10 m = x - 2 * y …
tankris
  • 1
  • 5
-1
votes
1 answer

How to implement an else clause in POSIX bc?

POSIX bc has neither an else clause nor boolean operators. Is there any simple way to do simulate an else clause?
Roland
  • 7,525
  • 13
  • 61
  • 124
-1
votes
2 answers

PHP BC Math library ignore rounding rules

I'm using bcdiv function from PHP to calculate some things, but result is different than it should be. Here is sample code: $val1 = 599.60; $val2 = 60; var_dump(bcdiv($val1, $val2, 0)); // result string(1) "9" // should be…
ptyskju
  • 175
  • 1
  • 3
  • 18
-1
votes
1 answer

How do I get the one-line input in bc?

I want to have multiple values by the input that space is separating each inputs. For example, the input "3 4 5" lets the value a to 3, value b to 4, value c to 5.
1 2 3
18
19