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
0 answers

Passing arguments to a bc script

I'm looking for a way to pass command line arguments to a bc script. For example, take this simple bc script that outputs 10 digits of pi (rounded): #!/bin/bc -lq n_digits = 10; scale = n_digits + 1; /* calculation, replace later with a better…
Kresimir
  • 777
  • 5
  • 20
2
votes
1 answer

How can hex addition in BC be made to overflow at the 8 byte limit (uint64)?

I am adding a series of 8196 64-bit unsigned integers, and I need the running-total to "roll-over" back to zero and continue counting from there... just as a "normal" programing language would do at the relevent INT_MAX ceiling. As the test…
Peter.O
  • 6,696
  • 4
  • 30
  • 37
2
votes
4 answers

How can you force bc to follow the scale?

I want to calculate the percentage of a few numbers upto 2 decimal places. Since my total count is pretty high, dividing 100 by it gives a really small number (0.000..) so for this step I have a large scale of 10. The second step where i multiply…
KA97
  • 39
  • 1
  • 5
2
votes
3 answers

Adding leading zeros to a Hex number within bash script

I'm working on a CAN bus project, and trying to send a message to set the time and date. I've worked out how the message needs to be formatted, grabbed the date/time and stored in variables. I've converted them to hex just fine, but I need to add…
JPToadstool
  • 45
  • 1
  • 3
2
votes
2 answers

Bash script: testing if floating point number is in a certain range including negative numbers

I'm trying to test if the variable $test is between -0.9 and 0.9. The following code works with numbers, but if $test is a lower case letter, it says it's a number between -0.9 and 0.9. Is there a better way of doing this, so that letters are not…
vato
  • 99
  • 5
2
votes
2 answers

Why querying a date BC is changed to AD in Java?

My program in Java connects to a Database (Oracle XE 11g) which contains many dates (date format of OracleXE is set to syyyy/mm/dd). Doing a query in the database with negative dates (before Christ) works fine. When I do it in Java, they are all…
StefanoF
  • 21
  • 5
2
votes
1 answer

Can I prevent GNU bc from spewing out zeros?

Suppose I want to solve the equation x + 3 = 40 using GNU bc. One way I could do this would be to start by checking to see if 0 is a solution, then checking 1, and so on, until I get to the right answer. (Obviously not the best way to do algebra,…
2
votes
3 answers

How do I stop bc from splitting lines?

I'm using bc from a bash script to do some quick and dirty BigInteger math but, when I bump up the scale, it starts splitting lines on me: pax> echo 'scale=200 ; 1 / 4' |…
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2
votes
4 answers

Subtract corresponding lines

I have two files, file1.csv 3 1009 7 1012 2 1013 8 1014 and file2.csv 5 1009 3 1010 1 1013 In the shell, I want to subtract the count in the first column in the second file from that in the first file, based on the identifier in the second column.…
user32849
  • 609
  • 1
  • 6
  • 16
2
votes
0 answers

Why does bc in bash script not print values less than 1.0?

Edit As mentioned by chepner in the comments below, I naively misunderstood the bc programming and actually can view the created directories with ls -a This problem was solved elsewhere (link below), but I thought I'd include my own problem to…
Blaisem
  • 557
  • 8
  • 17
2
votes
2 answers

How to divide floating point numbers using variables with integer result?

answer=$(echo $(( length / 1024 )) | bc -l) so length is a variable which has a value like 2402267538. and it needs to be divided by 1024. executing this throws this error ")syntax error: invalid arithmetic operator (error token is "
Animesh
  • 176
  • 1
  • 1
  • 10
2
votes
2 answers

bc (standard_in) 1: syntax error

I am trying to find the location of myfunc in the executable of: #include void myfunc(){ printf("Hello"); } int main(){ } I wrote this script: #!/bin/bash -x start=$(nm -S a.out|grep -w _start) start_addr=$(echo $start | awk…
robert
  • 3,539
  • 3
  • 35
  • 56
2
votes
1 answer

Business Catalyst "Edit" template of Web App, possible for user to change Item from "Enabled" to "Disabled"?

Just wondering if it is possible now for a user to Edit a Web App item and changed it from Enabled to Disable? I'd like for users to be able to Delete their own web app items, but deleting would mean it would disappear from the Admin area, so is it…
Kim Tran
  • 279
  • 4
  • 12
2
votes
1 answer

Error with BouncyCastle and Algorithm

i have error with BouncyCastle and PBEWITHSHAAND256BITAES-CBC-BC Algorithm on Windows My Code: Security.addProvider(new BouncyCastleProvider()); SecretKeyFactory localSecretKeyFactory =…
Szymon
  • 75
  • 12
2
votes
1 answer

bc utility in java, fails for some cases

I am trying to write simple bc utility in java. My program works fine for most cases but is failing for expression 4+4*3-3-2-1/3. basically consecutive - operators. not sure if i covered all edge cases. any suggestions to make it better would be…
juggernaut
  • 126
  • 18