Linux BC command-line Calculator

Source: Internet
Author: User
Tags ibase natural logarithm

1. Basic usage:

$ BC <<< 5*420$ BC <<< 5+49$ BC <<< 5-41
Or
$ echo "5*4" | bc20$ echo "5+4" | bc9$ echo "5-4" | Bc1

You can also write a calculation to a file and then calculate it once.

$ cat calcfile 5+56+7$ BC < calcfile 1013

You can also use the here Command:

$ BC << here> 1+4> 4-1> 2*4> HERE538

You can use the built-in variable last to refer to the previous result:

$ echo "5-4;last+6" | Bc17
Last can also be replaced with dot number:

$ echo "5-4;. +6 "| Bc17

2. Floating-point arithmetic:

You can use scale to specify the number of digits after the decimal point (the default is 0, which is an integer)

$ echo "sqrt (10)" | bc3$ echo "scale=1;sqrt (10)" | bc3.1$ echo "scale=10;sqrt (10)" | bc3.1622776601

Sometimes you don't get the number of digits you specify:

$ echo "SCALE=10; 1.25*9 "| bc11.25
At this point, you can modify it to:

$ echo "SCALE=10; 1.25*9/1 "| bc11.2500000000

3. The conversion of the numeral:

You can use variable IBase to represent the number of numbers before conversion, and obase to represent the number of converted Numbers

$ Echo ' obase=16;128 ' | Bc80
$ Echo ' ibase=16;obase=a;80 ' | bc128
$ Echo ' obase=2;128 ' | bc10000000
$ Echo ' ibase=2;obase=a;10000000 ' | bc128

$ echo "obase=10;ibase=16;80" | bc128

4. Exponential operation:

$ echo "5^5" | bc3125
$ echo "(5^5) ^5" | bc298023223876953125
Remember that the result of the first exponential operation is to add parentheses.

5. Function library-based operations

BC supports the following functions:

s (x): sine function (x is radians)

C (x): cosine function (x is radians)

A (x): Inverse tangent function (the result is radians)

l (x): Natural logarithm function

E (x): exponential function with E as the base

J (n,x): Bessel function


At this point, the BC is added with the-l parameter (at this point, the scale is 20)


Calculate log1.5 (2) (base 1.5, 2 logarithm):

$ echo "L (2)/L (1.5)" | bc-l1.70951129135145477699

You can also use the awk command:

$ Awk ' BEGIN {printf '%1l.9f\n ', log (2)/log (1.5)} ' 1.709511291

6. Script Function Programming:

$ cat Calc_prog print "Enter a number a\n"; A = read () print "Enter another number b\n"; b = Read () if (b = = a) {print "b = a\n";} if (a < b) {print "B > a\n";} if (a > B) {print "B < a\n";}

$ bc-l Calc_prog

7. Script Simulation Calculator:

$ cat calc.sh #!/bin/bashbc << endscale=2[email protected]end
$./test.sh  1+23

Reference:

http://mylinuxbook.com/linux-command-line-calculator-bc-examples/

Linux BC command-line Calculator

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.