Questions tagged [bc]

"Basic Calculator," an arbitrary-precision calculator language. General bc questions are a better fit for unix.stackexchange.com, while "programming" questions about bc fit on stackoverflow.com.

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

14 questions
15
votes
3 answers

How to do division with bc (bench calculator) and obtain fractional results?

bc 1/2 0 5/3 1 10/3 3 When a fraction is entered into bc, the result is truncated to an integer. How can this behavior be avoided, such that the output of a division operation is a real number?
user001
  • 3,474
  • 7
  • 24
  • 32
13
votes
5 answers

GNU BC: "modulo" % with scale other than 0

If the scale is other than zero, calculations with %, such as 3%2 and 46%4, tend to output 0. How is the algorithm designed with the scale other than 0? bc scale=10 print 4%3 // output 0
user3672
5
votes
4 answers

Dividing with Gnu's bc

I'm just starting with Gnu's bc and I'm stuck at the very beginning (very discouraging...). I want to divide two numbers and get a float as result: $bc bc 1.06.94 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc. This…
Boldewyn
  • 4,328
  • 5
  • 38
  • 52
3
votes
1 answer

Why is `bc` ignoring my `obase`?

>>> bc bc 1.06 Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. ibase = 16 obase = 56 BE753DE5C17F1B6C9F5D1E8A628B74B0FFC4A7 13 54 52 19 83 18 71…
Alan H.
  • 2,758
  • 8
  • 27
  • 39
2
votes
3 answers

Get GNU bc to print a newline character

GNU bc is very unconventional. I can't find anything online about how to get it to print a newline character. I'm trying to get it to print the first 16 hexadecimal digits of the sines of the integers from 1 to 30. I'd expect something like this to…
Melab
  • 983
  • 6
  • 18
  • 34
2
votes
1 answer

How to tell bash to repeat a script until satisfactory, then output the finished product

I'm looking to repeat this code until I stop it, either by setting a time for which it repeats, pressing ^c, or set number of iterations, and then output the result of repeating it. The code should feed into itself, such that inputting the variables…
Hellreaver
  • 257
  • 2
  • 14
1
vote
2 answers

Wrong results when comparing two numbers with bc (bash)

I’ve encountered an error, probably a bug in bc. I encountered it when trying to compare two numbers on a script in bash. It turns out that when I do: echo "1.1E-2<1.1E-1" | bc -l It returns 1 (as expected). But when I do this for exmaple: echo…
juan monti
  • 11
  • 1
1
vote
1 answer

Multiplication in bc (bench calculator)

$ echo 2*1024|bc 20480 $ echo 2.0*1024|bc 2048.0 $ echo 2*1024.0|bc 2048.0 What's going on here then? Update: dc manages fine $ echo "2 1024 * p"|dc 2048
Stephen Paulger
  • 197
  • 1
  • 8
1
vote
2 answers

Why isn't my Bash script returning the correct answer to this Project Euler?

I'm trying to use Bash to complete Project Euler 13. Below is my code that I just cannot figure out what's wrong with. #!/bin/bash sum=0 while read…
Egrodo
  • 169
  • 6
0
votes
1 answer

Substarct two float numbers in shell script

I have written shell script to substract two float values For eg. below are the two values : debit_amount=7.853117806000353E7 credit_amount=3223649.619999993 val1=$(printf "%f", "$debit_amount") val2=$(printf "%f", "$credit_amount") echo " val1…
user
  • 199
  • 4
  • 16
0
votes
1 answer

Particular bash bc usage not storing result in variable

Unfortunately this result in a empty variable, and bc command print its result to terminal anyway while trying to attribute to aux aux=&(bc -l <<< "scale = 5; c(${arguments[0]}*$constant)") echo "$aux" how can I store this result?
0
votes
1 answer

Best floating point solution for bash script?

I use bash calculator for floating point calculations and I have to use input redirection and backtick (`) symbol in scripts. As normal bracket $[math operation] and expr doesn't support float calculation. Is there any other way to do float…
P K
  • 2,183
  • 11
  • 29
  • 32
0
votes
2 answers

Using variables with bc: syntax error

The original problem This may be more of a bc or bash question than an FFmpeg one, although I would also appreciate being able to prevent ffprobe from printing carriage returns after its output. In a script I'm writing to automate some common tasks…
Hashim Aziz
  • 11,898
  • 35
  • 98
  • 166
0
votes
2 answers

Powershell: evaluate math expression represented as string

In PowerShell, how do I evaluate a mathematical expression stored as a string? How do I get PowerShell to evaluate the following: C:\> $a="30000/1001" C:\> $a 30000/1000 C:\> Desired output: C:\> $a="30000/1001" C:\>
Brian
  • 977
  • 1
  • 9
  • 24