What is 'bc'
- 'bc' (basic calculator) is a command line calculator. It reads "expressions to be evaluated" from stdin and sends output to stdout. So you can pipe the output of any linux command to 'bc' for further calculations and you can pipe the output to any other linux command.
- Since 'bc' reads input from stdin, the common usage is of the form
echo "expr" | bc (or) bc <<< "expr"
Why 'bc':
- You dont have to load a GUI calculator to do simple calculations.
- Precision can be set to any value (most of the graphical calculators has a precision upto 10 digits). Precision is limitted only by the processing power of your computer. You can find the value of pi upto 1000 decimal places within 1.5 mins with a normal computer.
- Available by default with all flavours of linux by default
1.Example for Addition,
Subtraction, Multiplication, Division, Power, square root
sunitha@sunitha-lappy:~$ echo "10+2" | bc
12
sunitha@sunitha-lappy:~$ echo "10-2" | bc
8
sunitha@sunitha-lappy:~$ echo "10*2" | bc
20
sunitha@sunitha-lappy:~$ echo "10/2" | bc
5
This can be written simple as
sunitha@sunitha-lappy:~$ bc <<< "10+2"
12
sunitha@sunitha-lappy:~$ bc <<< "10-2"
8
sunitha@sunitha-lappy:~$ bc <<< "10*2"
20
sunitha@sunitha-lappy:~$ bc <<< "10/2"
5
The scale (number of digits after decimal
point), by default is set to 2
sunitha@sunitha-lappy:~$ bc <<< "scale=2;10/2"
5.00
sunitha@sunitha-lappy:~$ bc <<< "2^3"
8
sunitha@sunitha-lappy:~$ bc <<< "scale=20;sqrt(10)"
3.16227766016837933199
5.00
sunitha@sunitha-lappy:~$ bc <<< "2^3"
8
sunitha@sunitha-lappy:~$ bc <<< "scale=20;sqrt(10)"
3.16227766016837933199
2.Grouping - using parenthesis
sunitha@sunitha-lappy:~$ bc <<< "10+(2*3)"
16
16
3.Relational Expressions - <, >, >=, <=, ==, !=
0
sunitha@sunitha-lappy:~$ bc <<< "5>3"
1
sunitha@sunitha-lappy:~$ bc <<< "5>=3"
1
sunitha@sunitha-lappy:~$ bc <<< "5<=3"
0
sunitha@sunitha-lappy:~$ bc <<< "5==3"
0
sunitha@sunitha-lappy:~$ bc <<< "5!=3"
1
4.Bitwise
Operations:
Base - obase, ibase
Base conversion -- Hexadecimal to Decimal
sunitha@sunitha-lappy:~$ bc <<< "obase=16;124"7C
Base conversion -- Decimal to Hexadecimal
sunitha@sunitha-lappy:~$ bc <<< "ibase=16;7C"124
Hexadecimal calculation
sunitha@sunitha-lappy:~$ bc <<< "obase=16;ibase=16;7C+F"8B
sunitha@sunitha-lappy:~$ bc <<< "ibase=16;obase=10;7C+F"
8B
Binary calculation:
sunitha@sunitha-lappy:~$ bc <<< "obase=2;28"
11100
sunitha@sunitha-lappy:~$ bc <<< "obase=2;ibase=2;11100+10011"
101111
11100
sunitha@sunitha-lappy:~$ bc <<< "obase=2;ibase=2;11100+10011"
101111
5.MATH LIBRARY
If bc is invoked with the -l option, math
library gets preloaded, making a lot more math functions available to
us.
Note: Default scale is set to 20 when bc is started with -l
option
(a) sine,cos,tan and arctangent
Note: pi can be calculated using the
formula, pi=4*arctangent(1)
sunitha@sunitha-lappy:~$ bc -l <<< "pi=4*a(1);pi"
3.14159265358979323844
sunitha@sunitha-lappy:~$ bc -l <<< "pi=4*a(1);s(pi/2)"
1.00000000000000000000
sunitha@sunitha-lappy:~$ bc -l <<< "pi=4*a(1);c(pi/2)"
.00000000000000000001
3.14159265358979323844
sunitha@sunitha-lappy:~$ bc -l <<< "pi=4*a(1);s(pi/2)"
1.00000000000000000000
sunitha@sunitha-lappy:~$ bc -l <<< "pi=4*a(1);c(pi/2)"
.00000000000000000001
(b)exponential and logarithmic
functions
sunitha@sunitha-lappy:~$ bc -l <<< "l(e(10))"9.99999999999999999999
Some Cool tips
* Arbitrary precision
(scale)
bc has a default scale of 0, which is quite annoyoing as bc
<<< 3/2 gives 1 instead of 1.5. bc -l would solve this issue
sunitha@sunitha-laptop:~$ bc <<< 3/2 -l
1.50000000000000000000
1.50000000000000000000
The default scale is 20. But if you need any arbitrary precision, say
you want the default scale to be 5 whenever you invoke bc, follow this
In your .bashrc in your home directory add "export
BC_ENV_ARGS=~/.bc"
Then create the file named ".bc" in your home
directory, with the following one line
scale=5
Now, bc
<<< 5/3 gives 1.66666
* Alarm
To start audacious (the default music player that comes
with ubuntu) after 8 hrs (to server as an alarm)
sleep `bc
<<< 8*60*60`; audacious -p
For more info about 'bc',
refer http://www.gnu.org/software/ bc/manual/html_mono/bc.html
No comments:
Post a Comment