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

Type of MySQL Decimal value in PHP

I am using BC Math library in PHP, as you know data provided to BC functions MUST BE strings, otherwise very weird results might come out. I am storing decimal values (18,10) in MySQL. My questions are: What is the type of these values when i fetch…
Vladimir
  • 1,602
  • 2
  • 18
  • 40
2
votes
3 answers

What is the difference of using BC keys or default?

Both lines of code: KeyPairGenerator.getInstance("RSA") KeyPairGenerator.getInstance("RSA", "BC") works well. So, what's the differecente using BC or not? Is BC completely compatible with the default RSA used? (using sun JDK 6)
The Student
  • 27,520
  • 68
  • 161
  • 264
2
votes
1 answer

one line using sed and bc together?

I want to add one to the last value at the end of a string in sed. I'm thinking along the lines of cat 0809_data.csv |sed -e 's/\([0-9]\{6\}\).*\(,[^,]*$\)/\1\2/g'| export YEARS = $(echo `grep -o '[^,]*$' + 1`|bc) e.g. 123456, kjhsflk, lksjgrlks,…
d-cubed
  • 1,034
  • 5
  • 30
  • 58
2
votes
2 answers

Using bc as daemon in BASH shell from awk

# mkfifo inp out # bc -ql out & [1] 6766 # # exec 3>inp 4&3 # read a <&4; echo $a .800 # # awk ' BEGIN { printf("4/5\n") >"/dev/fd/3"; exit 1;} ' # read a <&4; echo $a .800 # # awk ' BEGIN { printf("4/5\n")…
2
votes
1 answer

Why doesn't bc show the scale for the result of this expression?

This expression, I think, should return 0.000, not 0. echo "scale = 3 ; 0 / 2" | bc Using echo "scale = 3 ; scale(0 / 2)" | bc outputs 3, so it's obvious that the scale is applies, but it's not displaying the output properly. What's going on?
vmrob
  • 2,966
  • 29
  • 40
2
votes
1 answer

Floating point maths in bash using bc

I am trying to do some floating point maths in bash. I have been examining this thread, as well as many other sources of information: BASH Arithmetic Expressions Just as an example, I am using: c='echo "2\*4.2"|bc' echo $c but getting: echo…
cohara
  • 111
  • 3
  • 19
2
votes
2 answers

Convertin Cartesian to Polar in bash

We got homework to convert Cartesian Coordinates to Polar Coordinates and I don't even know how to start. So any little help would be nice. We have to write code in bash. Here is the example that we got: script.sh 256 128 Result: 286.216 1.107 We…
user3127680
  • 353
  • 2
  • 4
  • 13
2
votes
1 answer

Limit the amount of decimal places in bc

I have this code codonsnum=$(bc -l <<< "$lengthseq/3") How can I limit the result to only 3 decimals? The result is, for example: 2.325234985908723 and I only want 3 decimals.
crocket
  • 37
  • 4
2
votes
3 answers

Bash bc and echo commands

I'm writing small geeklet for geektool, to alert me when sum of inactive and free RAM on my Mac will become slow. I'm not really good with bash, so I have a problem with final output (getting blank). Here is code: inMem=$(top -l 1|awk '/PhysMem/…
2
votes
3 answers

bash: bc command's evaluation of *

I am using the bc command in UNIX to parse some math expressions. I have two small functions: function bashcalc { echo $@ | bc -l } function2 { ... catMinusMouse=$(bashcalc "$cat_angle - $mouse_angle") cos=$(cosine…
bobbyjoe93
  • 178
  • 2
  • 7
2
votes
1 answer

bc command not scaling numbers to decimal places

I am trying to scale my number calculation to one decimal place (with all applicable rounding) using the bc command in linux. This is my test expression: echo "scale 1; 90 - 70.333" | bc However, I end up with 3 decimal places: 19.667 is the…
user195488
2
votes
3 answers

shell script where bc loses precision

#! /bin/sh a1=260 a2=9150 echo "$a1 * 100 / $a2" | bc the output is 2 where it should be 2.8415 why is precision lost although I'm using bc?
A K
  • 737
  • 2
  • 8
  • 17
2
votes
3 answers

Pass a part of perl to bc

I'm trying to write a command that gives me some information about a linux system. The command should be a one liner. It should represent the total amount of interrupts, processes and context-switches. I'm quite new to the linux world, so this isn't…
Write Down
  • 158
  • 2
  • 5
  • 15
2
votes
2 answers

bc command acts strange on obase=10

today i found the 'bc' linux command and found a strange behaviour when calculating with outputbase 10. echo "ibase=16;obase=9;AFBE" | bc 67638 echo "ibase=16;obase=11;AFBE" | bc 09 02 11 08 echo "ibase=16;obase=10;AFBE" | bc AFBE well, command…
ineb
  • 53
  • 4
2
votes
1 answer

Using bash calculator in a vim script - strange behaviour

Does anybody know why this works with bash calculator (bc) in vim: echo system ("echo 3+5 \| dos2unix \| bc -l") ---> output: 8 echo system ("echo 3/5 \| dos2unix \| bc -l") ---> output: .600000000000000 echo system ("echo 3*5 \| dos2unix \|…
Reman
  • 7,931
  • 11
  • 55
  • 97