COMPUTE command collation under Linux

Source: Internet
Author: User
Tags arithmetic ibase

In the Linux system, there are often some computing needs, then the following simple comb the next few commonly used calculation commands

(1) BC command
The BC command is a calculator language that supports interactive execution of arbitrary precision. Bash has built-in support for integer arithmetic, but it does not support floating-point operations, and the BC command can be very handy for floating-point operations, and of course integer arithmetic is no longer spoken
Common parameter options:
-I: Forced into interactive mode;
-L: Defines the standard math library used;
-W: A warning message for the extension of POSIX BC;
-Q: Do not print the normal GNU BC Environment information;
-V: Displays the instruction version information;
-H: Displays help information for the instruction.

In the BC working environment, you can use the following calculation symbols:
+ Addition
-Subtraction
* Multiplication
/Division
^ Index
% remainder
Where you do a "divide calculation" or "remainder calculation", you can use the Scale specify the number of digits after the decimal point (default is 0, which is an integer)

Example Description:

[Email protected] ~]# BC-VBC 1.06.95Copyright 1991-1994, 1997, 1998, $, 2004, 2006 free software Foundation, Inc.[[em AIL protected] ~]# BC <<< 5*4  20[[email protected] ~]# BC <<< 5+4  9[[email protected] ~]# BC < ;<< 50-14  36[[email protected] ~]# BC <<< 50/105[[email protected] ~]# BC <<< 50/316[[email PR otected] ~]# BC <<< 3^327

as follows, enter interactive mode:
You can also enter multiple calculations on one line, with commas; Separated .

[Email protected] ~]# BCBC 1.06.95Copyright 1991-1994, 1997, 1998, $, 2004, 2006 free software Foundation, inc.this Free software with absolutely NO WARRANTY. For details type ' warranty '. 3+101310-5510*1010010^210010/2510/33SCALE=410/33.333310%3.00013+4;5*2;5^2;18/4 710254.500010^3;100+50;4000-598; 33*8;899/341000150340226426.4411

The above is calculated using interaction, or you can calculate the result directly without interacting.
1) combined with echo and | conform to
[[email protected] ~]# echo "(6+3) * *" |BC
"
[[ Email protected] ~]# echo 15/4 |BC
3
[[email protected] ~]# echo "SCALE=2;15/4" |BC
3.75
[email  protected] ~]# echo "scale=2;100/30*100-98;20+45;90-70;15^2" |BC
235.00
+

225
[[Email  protected] ~]# echo "3+4;5*2;5^2;18/4" |BC
7


4
2) BC In addition to scale to set the decimal digits, there are ibase and obase to other binary operations.
The following example:
output 16 binary A7 to 10, note that English can only be capitalized
[[email protected] ~] # echo "IBASE=16; A7 "|BC
167
Turn 2 binary 11111111 into 10
[[email protected] ~]# echo" ibase=2;11111111 "|BC
255
input is 16 binary, output is 2 binary
[[email protected] ~] # echo "ibase=16;obase=2; B5-a4 "|BC
10001

3) In addition to the BC can be followed by the file name (specify the file containing the calculation task)

[email protected] ~]# cat Calc.txt20+8956-1734*4530/82^5scale=5100/3200%17[[email protected] ~]# BC CALC.TXTBC 1.06.95Copyright 1991-1994, 1997, 1998, 2004, 2006 free software Foundation, inc.this are free software with ABSOLUTE LY NO WARRANTY. For details type ' warranty '. 10939153033233.33333.00010 does not print normal GNU messages [[email protected] ~]# bc-q Calc.txt10939153033233.33333.00010[[email Protected] ~]# Bc-q < calc.txt10939153033233.33333.00010[[email protected] ~]#

4) You can also use the here Command:

[email protected] ~]# BC << here > 30+56> 30-14> 30*5> scale=3> 30/7> 10%3> 2^7here86161504.28 5.001128

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

[Email protected] ~]# echo "50*4;last+100" | BC  200300

Last can also be replaced with dot number:

[Email protected] ~]# echo "50*4;. +100 "| BC  200300

6) Script Simulation Calculator

[email protected] ~]# cat bc.sh #!/bin/bash BC << EOF  scale=2  [email protected]  Eof[[email protected] ~]# chmod 755 bc.sh[[email protected] ~]#./bc.sh 10*220[[email protected] ~]#./bc.sh 10+3242[[email protected] ~]#/bin/ Bash bc.sh 100/333.33[[email protected] ~]#/bin/bash-x bc.sh 100%13+ bc.03

COMPUTE command collation under Linux

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.