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
12
votes
3 answers

Bitwise operations in BC?

$ bc BC> ibase=2 BC> 110&101 // wanna get 100 (standar_in) 8: syntax error Wikipedia informs that the ops are "|, & and ^". It may be that they work only in certain BC-types or I misread something.
otto
  • 393
  • 1
  • 4
  • 12
12
votes
3 answers

Rounding Numbers with bc in Bash

I want to compute an average with 3 decimal figures, rounded to nearest, using bc. For example: average of 3, 3 and 5 should yield 3.667 and average of 3, 3 and 4 should yield 3.333 I tried: echo "scale=3; $sum/$n+0.0005" | bc but scale doesn't…
Yasin Kaya
  • 145
  • 1
  • 1
  • 9
12
votes
4 answers

Bash decimal to base 62 conversion

I would like to reverse the operation performed by the following bash command: $ echo $((62#a39qrT)) 9207903953 i.e. convert decimal 9207903953 to base 62, keeping bash standard of {0..9},{a..z},{A..Z}. I know I can do this by using bc, but I will…
Ram
  • 1,161
  • 1
  • 11
  • 34
9
votes
3 answers

bc arithmetic Error

i am trying to solve this bash script which reads an arithmetic expression from user and echoes it to the output screen with round up of 3 decimal places in the end. sample input 5+50*3/20 + (19*2)/7 sample output 17.929 my code is read x echo…
krrish
  • 353
  • 3
  • 15
8
votes
3 answers

DDD - Bounded Contexts and Multiple Models?

I'm reading about the idea of Bounded Contexts in DDD, and I'm starting to realize that I don't have a clear understanding of exactly what a Model looks like in practice. (I might not even know exactly what a Domain means, either.) Let's look at the…
Hobbes
  • 163
  • 1
  • 8
8
votes
2 answers

Correct usage of bc in a shell script?

I'm simply trying to multiplying some float variables using bc: #!/bin/bash a=2.77 | bc b=2.0 | bc for cc in $(seq 0. 0.001 0.02) do c=${cc} | bc d=$((a * b * c)) | bc echo "$d" | bc done And this does not give me an output. I know…
Omkar Myatra
  • 115
  • 2
  • 2
  • 5
8
votes
1 answer

Pipe output to bc calculator

Short version: I'm trying to get something like this to work in c using piping: echo 3+5 | bc Longer version: Following simple instructions on pipes at http://beej.us/guide/bgipc/output/html/multipage/pipes.html, I tried creating something similar…
Xitac
  • 154
  • 3
  • 9
8
votes
7 answers

Prevent bc from auto truncating leading zeros when converting from hex to binary

I'm trying to convert a hex string to binary. I'm using: echo "ibase=16; obase=2; $line" | BC_LINE_LENGTH=9999 bc It is truncating the leading zeroes. That is, if the hex string is 4F, it is converted to 1001111 and if it is 0F, it is converted to…
gp_xps
  • 639
  • 1
  • 8
  • 12
7
votes
3 answers

bc and its ibase/obase options:

I stumbled over a curious bug, I think: I tried to read "512" as a number to base 6, and output it as base 16: echo "ibase=6;obase=16;512" | bc 161 As you can see, the output is 161, but it should be bc(sic!). I tried with base 10: echo…
user unknown
  • 35,537
  • 11
  • 75
  • 121
7
votes
7 answers

Bash programmation (Cygwin): Illegal Character ^M

I have a problem with a character. I think it's a conversion problem between dos and unix. I have a variable that is a float value. When I print it with the echo command i get: 0.495959 But when I try to make an operation on that value with the bc…
Frencoo
  • 73
  • 1
  • 1
  • 3
7
votes
3 answers

bc: get the sum of a list of num

Jack 10 J 10 A 20 Lu cal 20 A bc U 20 I want to get the sum of these nums: 10+10+20+20+20 = 80 but I can't use cat input|cut -d " " -f 3 to get the num, how can I do it?
NOrder
  • 2,483
  • 5
  • 26
  • 44
6
votes
2 answers

Unique Identifier for certificate Issuer (X509Name)

in my app I'm using the sha256 of the issuer Name (x509CertImpl.getIssuerDN().getName()) and the certificate serial number to uniquely identify a certificate, but now I have realized that other implementations of X509Name as the implementation of…
Jaime Hablutzel
  • 6,117
  • 5
  • 40
  • 57
6
votes
4 answers

ASN.1 Encoding-Decoding

I am currently developing a client-server program, the client in Java/C and server in C. I have to transport cryptographic data(like the client should pass data to Server to encrypt/decrypt, compute digest etc) and the server has to return the…
pimmling
  • 483
  • 5
  • 10
  • 19
6
votes
3 answers

how to pipe bc-calculation into shell variable

I have a calculation on a Linux shell, something like this echo "scale=4;3*2.5" |bc which gives me an result, now I like to pipe the result of this calculation into an Variable so that I could use it later in another command, piping into files…
Seb
  • 173
  • 2
  • 2
  • 7
6
votes
3 answers

BC command not working in CYGWIN

I have downloaded CygWin(32-bit) and installed (on Windows 7 32 bit System) today to run shell script but i found one problem that bc command isn't working. I am getting error -bash: bc: command not found. So please help me out!
Sanket Makani
  • 2,491
  • 2
  • 15
  • 23
1
2
3
18 19