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
2
votes
1 answer

A large hex to binary conversion in bc

I'm trying to convert a 32 character long hex string (eg: A41EB86E184207262C12F66E7C480577) to binary. I'm using: echo "ibase=16; obase=2; $line" | bc but its including an unnecessary \ in-between which is not letting me perform any bit operations…
gp_xps
  • 639
  • 1
  • 8
  • 12
2
votes
3 answers

How to decrement floating point variables in a bash/batch script

I have written a program in C that requires four floating point variables to be passed to it as parameters. I would like to make a script that will run this program x times, with those four variables being decreased by a certain amount each time…
Jaguar
  • 19
  • 1
  • 4
2
votes
3 answers

Storing Negative-Dates to Represent BC/BCE (vs AD/CE) in MySQL?

I'm working on a project now that will require me to enable users to store negative-dates in the database. These dates have the potential to span many thousands of years BC, and as late as 'Today.' I've worked on many projects that stored dates, but…
Sampson
  • 265,109
  • 74
  • 539
  • 565
1
vote
3 answers

Using bc on a column of values in a csv

I have a long csv file with 5 columns of values. How can I extract every value from a column and pass this value to bc to extract a cosine on it? I'm trying using awk to extract the values but I fail when I try to pass every single value to…
Roberto
  • 23
  • 4
1
vote
2 answers

Post sales order on custom table in AL language

When we post sales order it create an sales invoice but we want when we post sales order it should post the sales order on custom table same as sales invoice in Business central. I am trying the event subscriber in codeunit the event is…
Numan Ijaz
  • 11
  • 1
1
vote
1 answer

There will be messy code when redirect output of "top" command to a file

Not sure whether there are existing similar questions already. Sometimes we need to execute "top" for once and redirect its out to a file, such as: top -n 1 -o %CPU > top.log But there will be messy code in…
bbrown
  • 41
  • 4
1
vote
0 answers

How to let bash variable continue get value?

our server have different gcc version, and sometime we need to swtich different gcc version base on different project. And I don't want to hard code the gcc in my bashrc, so I want to write a bash function to switch different GCC version while using…
bbrown
  • 41
  • 4
1
vote
1 answer

To how many decimal places is bc accurate?

It is possible to print to several hundred decimal places a square root in bc, as it is in C. However in C it is only accurate to 15. I have checked the square root of 2 to 50 decimal places and it is accurate but what is the limit in bc? I can't…
Ian Stewart
  • 435
  • 3
  • 11
1
vote
1 answer

Does bc not limit a variable's scope?

Define the function in basic calculator bc as define void f () { test=42; print "all done\n"; } I'd have thought the value of test would be limited to the scope of the function f, but no, test equals 42 globally. Is there no way to limit the scope…
Mike Pierce
  • 1,390
  • 1
  • 12
  • 35
1
vote
3 answers

How to make `bc` output a desired number of base-2 binary digits

This outputs 101110 echo "obase=2; 46" | bc How can I make it output 8 digits, like this? : 00101110 I learned the above usage of bc here: Bash shell Decimal to Binary base 2 conversion See also my answer to that question here.
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
1
vote
2 answers

awk not getting correct comparison result on data bash

I have a file file.txt, ch.qos.logback:logback-classic 1.2.0 ch.qos.logback:logback-core 1.2.0 com.fasterxml.jackson.core:jackson-databind 2.10.0 com.fasterxml.jackson.core:jackson-databind 2.6.7 com.fasterxml.jackson.core:jackson-databind…
Shubham Saroj
  • 290
  • 2
  • 12
1
vote
0 answers

Is there any particular reason that PHP doesn't do bcadd() whenever + is used?

Whenever I'm dealing with numbers in PHP which represent money, I'm forced to use bcadd() and the other functions in that "family", instead of simply using the nice symbols such as +, -, / and *. The reason is something about "floats" being…
1
vote
2 answers

Pipe to bc gives inconsistent syntax errors

I have to pipe integers comparisons (and more complex arithmetic and boolean operations) to bc, and I have to keep track of syntax errors when some data is missing. It seems I found some inconsistent behaviour with bc. Of course : echo "1==7" | bc…
Mgrd
  • 13
  • 3
1
vote
1 answer

Bash script Error: (standard_in) 1: syntax error

My bash script is the following #!/bin/bash wc=(5000 10000 20000 50000); for r2 in {0.01 0.1 0.4 0.7 0.95}; do for i in {0..3}; do rr=$(echo "${r2} * 100" | bc) ww=$(echo "${wc[$i]} / ${rr}" | bc) echo $ww done done However, I…
cheng
  • 85
  • 1
  • 11
1
vote
3 answers

bash unable to compare multiple number conditions with bc

Have tried several different syntax methods to make bash test between number ranges for floating point numbers and cannot get this to work. Whole numbers work, so do statements without the && operator. I must be missing something…
Rob Qlder
  • 35
  • 1
  • 6