LINUX arbitrary precision Calculator BC usage __linux

Source: Internet
Author: User
Tags echo command ibase parse error
"Purpose description"
Bash has built-in support for integer arithmetic, but does not support floating-point operations, and the BC Command makes it easy to do floating-point operations, and of course integer operations are no longer spoken. The man page says that BC is an arbitrary precision calculator language, a computational language of arbitrary precision, a language that provides some grammatical structures, such as conditional judgments, loops, and so on, which can be said to be very powerful, But I haven't found the occasion to use it in practice. Another use is to make a conversion.
"Common parameters"
In general, we use the BC command without any parameters:
Bc
If you need the BC do not output prompt information, you can add the-Q parameter:
Bc-q
If you want to use a powerful math library, such as trigonometric functions, you need to add the-l parameter:
Bc-l
Because BC itself is a command interpreter, to exit it simply enter quit carriage return or press Ctrl+d to terminate.
"Use Example"
Example one command line using BC
[Root@localhost centos39]# BC
BC 1.06
Copyright 1991-1994, 1997, 1998, Free Software Foundation, Inc.
This are free software with absolutely NO WARRANTY.
For details type ' warranty '.
3+4
7
3-4
-1
3*4
12
3/4
0
SCALE=2;3/4 # Keep The decimal point precision is valid only for division, fetch, and power
.75
3/4
.75
3%4
0
Scale=0
3%4
3
3^4
81
Ctrl+d
[Root@localhost centos39]#
Example two uses the BC to calculate through the pipeline
[Root@localhost centos39]# Echo 3 * 4 | Bc
(standard_in) 1:parse error
[Root@localhost centos39]# echo "3 * 4" | Bc
12
[Root@localhost centos39]# echo "scale=7; 355/113 "| Bc
3.1415929

[Root@localhost centos39]#


In this way, you can also use the size comparison of floating-point data types:

a=1.2

b=3.2

C= ' echo ' $a < $b | BC '

echo "$c"


The above script execution result is 1, and if the result of the comparison is not satisfied (for example, change the above $a < $b to $a > $b), the output result is 0.

Note: It must be noted here that this comparison result is 0 or 1, rather than representing true or false, and therefore cannot be used for conditional judgment after if. Remember to remember.


Sample Triple Conversion
[Root@rhel55 ~]# echo "IBASE=16; FFFF "| Bc
65535
[Root@rhel55 ~]# echo "OBASE=16; 1000 "| Bc
3E8
[Root@rhel55 ~]#


We use the IBase and Obase methods of BC.

IBase is the input of a number, and the obase is the output of the number of the system. Well remember, I am input,o is output.

If you use a command to turn a number, you can use the echo command and the pipe to combine the BC. As follows:

10-in-turn 2-in-system:

$ echo "obase=2;ibase=10;100" | bc1100100

10-in-turn 16-in-system:

$ echo "obase=16;ibase=10;100" | Bc64

16-in-turn 10-in-system:

$ echo "ibase=16;obase=2; F1 "| bc11110001

Note that the F of the 16 binary number is capitalized, if the lowercase result is incorrect. As the top is not specified, the default is 10.
example four writes multiple expressions in a file to calculate together
[Root@rhel55 ~]# Cat TEST.BC
123*321
123/321
scale=4;123/321

[Root@rhel55 ~]# BC TEST.BC
BC 1.06
Copyright 1991-1994, 1997, 1998, Free Software Foundation, Inc.
This are free software with absolutely NO WARRANTY.
For details type ' warranty '.
39483
0
.3831
Ctrl+d
[Root@rhel55 ~]#
[Root@rhel55 ~]# Cat TEST.BC | Bc
39483
0
.3831
[Root@rhel55 ~]#
Example 51 bash script for calculating Triangle area
First, review the knowledge of Junior high School: B means the bottom of the triangle, H is the height of the triangle, then the area of the Triangle calculation formula is B*H/2.

File: area_of_triangle.sh
Bash code
#!/bin/bash

# Shell Program/script to read the base and height of a traingle
# -------------------------------------------------------------------------
# Copyright (c) Nixcraft project # This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script are part of Nixcraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/for more information.
# -------------------------------------------------------------------------
# Formula Info:http://www.mste.uiuc.edu/dildine/heron/triarea.html
# area= (1/2) x Base x Height

Echo-n "Enter base of a triangle:"
Read B

Echo-n "Enter the height of a triangle:"
Read H

# Calculate it and display back
Area=$ (echo "scale=2" (1/2) * $b * $h "|BC)
echo "Area of a triangle is $area"


[ROOT@SMSGW academic]#./area_of_triangle.sh
Enter base of a triangle:123
Enter height of a triangle:321
Area of a triangle is 19741.50
[ROOT@SMSGW academic]#
example six script fragment using the BC command
Bash code
# usage:calc_sum <num1> <num2>
# calculates the number of two and
Calc_sum ()
{
Bc-q <<eof
$1+$2
Eof
}

# Usage:calc_free <count>
# 0.05 yuan for cost calculation
Calc_fee ()
{
Bc-q <<eof
0.05*$1
Eof
}


Paste the above code to the terminal.
[Root@web ~]# # usage:calc_sum <num1> <num2>
[Root@web ~]# # calculates two numbers and
[Root@web ~]# Calc_sum ()
> {
> Bc-q <<eof
> $1+$2
> EOF
>}
[Root@web ~]#
[Root@web ~]# # Usage:calc_free <count>
[Root@web ~]# # 0.05 yuan per unit cost
[Root@web ~]# Calc_fee ()
> {
> Bc-q <<eof
> 0.05*$1
> EOF
>}
[Root@web ~]#
[Root@web ~]#
[Root@web ~]# calc_sum 123 321
444
[Root@web ~]# Calc_fee 1000
50.00
[Root@web ~]#
example seven uses the math library
It is said that the PI value of 100 digits can be calculated.
[Root@web ~]# echo "SCALE=100; A (1) *4 "| Bc
Runtime error (func= (main), adr=11): Function a not defined.
[Root@web ~]# echo "SCALE=100; A (1) *4 "| Bc-l
3.141592653589793238462643383279502884197169399375105820974944592307\
8164062862089986280348253421170676
[Root@web ~]#

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.