Questions tagged [bc]

Arbitrary-precision arithmetic language.

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

114 questions
34
votes
6 answers

Understand "ibase" and "obase" in case of conversions with bc?

I often use bc utility for converting hex to decimal and vice versa. However, it is always bit trial and error how ibase and obase should be configured. For example here I want to convert hex value C0 to decimal: $ echo "ibase=F;obase=A;C0" |…
Martin
  • 7,516
23
votes
1 answer

bc: Why does `ibase=16; obase=10; FF` returns FF and not 255?

I've been using bc to convert numbers between binary to hex, octal to decimal and others. In the following example, I was trying to convert base 16 (hex) number to binary, octal and decimal. I don't have any problem with the first 2 attempts. $ echo…
user264359
18
votes
3 answers

How to show zero before decimal point in bc?

echo "scale=3;1/8" | bc shows .125 on the screen. How to show 0.125 if the output result is less than one?
Kevin Dong
  • 1,169
14
votes
5 answers

how to make bc to show me 10 and not 10.00

#!/bin/bash q=$(bc <<< "scale=2;$p*100") head -n$q numbers.txt > secondcoordinate.txt That's just part of the script, but I think it's enough to clarify my intentions. p is a variable with just two decimals, so q should be an integer...…
Diego
  • 309
7
votes
1 answer

BC not handling scale = 0 correctly

I defined the cbrt function to return a cube root. I need to get an integer back (even if it is close to the cube root, it will be acceptable in my case). However, when I put the scale as 0 to get an integer, I get numbers which are grossly…
7
votes
2 answers

How to avoid wrapping in bc output

I have the following example in GNU v. 1.06 (I cannot identify a limit pertaining to line length): v=$(bc -l <<<"scale=100;4*a(1)"); echo $v which returns: 3.141592653589793238462643383279502884197169399375105820974944592307\…
6
votes
5 answers

How do I get bc to start decimal fractions with a leading zero

How do I get bc to start decimal fractions with a leading zero? $ bc <<< 'scale=4; 1/3' .3333 I want 0.3333.
Petr Skocik
  • 28,816
2
votes
1 answer

bc - command: not giving appropriate results

I am trying to do simple calculation using bc command in linux. Once i want the value with all the decimal places. other time i want only the integer part Want to get with full decimal places $ echo "scale=10; ((900/1303) * 928)/600" |…
Santhosh
  • 409
2
votes
1 answer

(standard_in) 1: syntax error when using bc

I have written an .awk which executes some operation on a .tr file and writes the output to a file. The END section of .awk file prints this: printf("%15.2f\n%15.5f\n%15.2f\n%15.2f\n%15.2f\n%10.2f\n%10.2f\n%10.5f\n", rThroughput, rAverageDelay,…
Robur_131
  • 221
  • 2
  • 5
  • 11
2
votes
3 answers

Syntax error comparing floats with bc

I'm trying to use the comparison operator of bc and I'm getting a "syntax error on line 1 stdin" error. I need to use the comparison functionality within a korn script because korn doesn't handle floating points well. Example usage below: echo…
Ben
  • 205
0
votes
2 answers

What is the numeral for base greater than 16 in bc?

I recently read about bc and found that it supports obase upto 999. Can anyone point me to the numeral set for bc for base greater than 16.