# BC-Floating point calculator, binary conversion for Linux commands

Source: Internet
Author: User

Description of Use
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. The manual page says that BC is an arbitrary precision calculator language, which is a computational language of arbitrary precision, which is a language that provides some grammatical structures, such as conditional judgments, loops, etc., which can be said to be very powerful, But I haven't found a place to use it in practice. Another use is for the conversion of the binary.
Common parameters
In general, we use the BC command without any parameters.
Bc
If you need BC not to output a hint, 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 the BC itself is a command interpreter, to exit it as long as the direct input quit Enter or press Ctrl+d to terminate.
Using the example
Example a command-line way to use BC
[email protected] centos39]# BC
BC 1.06
Copyright 1991-1994, 1997, 1998, Free Software Foundation, Inc.
This is the 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 # reserved Decimal precision is only valid for division, remainder, and
.75
3/4
.75
3%4
0
Scale=0
3%4
3
3^4
81
Ctrl+d
[Email protected] centos39]#
Example two uses BC to calculate by pipeline
[[email protected] centos39]# Echo 3 * 4 | Bc
(standard_in) 1:parse error
[[email protected] centos39]# echo "3 * 4" | Bc
12
[Email protected] centos39]# echo "scale=7; 355/113 "| Bc
3.1415929
[Email protected] centos39]#
Example three-step conversion
[Email protected] ~]# echo "IBASE=16; FFFF "| Bc
65535
[Email protected] ~]# echo "OBASE=16; 1000 "| Bc
3E8
[Email protected] ~]#

We use BC's IBase and Obase methods.

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

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

10-in-Turn 2-system:

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

10-in-turn 16-system:

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

16-in-turn 10-system:

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

Note that the 16-digit F is capitalized if the lowercase result is incorrect. The default is 10 when the top is not specified.
Example four writes multiple expressions in a file together to calculate
[email protected] ~]# cat TEST.BC
123*321
123/321
scale=4;123/321

[email protected] ~]# BC TEST.BC
BC 1.06
Copyright 1991-1994, 1997, 1998, Free Software Foundation, Inc.
This is the free software with absolutely NO WARRANTY.
For details type ' warranty '.
39483
0
.3831
Ctrl+d
[Email protected] ~]#
[email protected] ~]# Cat TEST.BC | Bc
39483
0
.3831
[Email protected] ~]#
Example 51 bash scripts that compute the area of a triangle
First of all to review the knowledge of Junior high School: B for the bottom of the triangle, h for the high triangle, then the area of the Triangle calculation formula is B*H/2.

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

# Shell Program/script to read the base and height of a traingle and find it area
# -------------------------------------------------------------------------
# Copyright (c) 2005 Nixcraft Project # This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script was 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= x Base x Height

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

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

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

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

# Usage:calc_free <count>
# calculation fee, Unit price 0.05 yuan
Calc_fee ()
{
Bc-q <<eof
0.05*\$1
Eof
}

Paste the above code into the terminal.
[Email protected] ~]# # usage:calc_sum <num1> <num2>
[[Email protected] ~]# # calculates two numbers of the and
[Email protected] ~]# calc_sum ()
> {
> Bc-q <<eof
> \$1+\$2
> EOF
>}
[Email protected] ~]#
[Email protected] ~]# # Usage:calc_free <count>
[[Email protected] ~]# # calculation fee, price 0.05 yuan
[Email protected] ~]# Calc_fee ()
> {
> Bc-q <<eof
> 0.05*\$1
> EOF
>}
[Email protected] ~]#
[Email protected] ~]#
[Email protected] ~]# calc_sum 123 321
444
[Email protected] ~]# Calc_fee 1000
50.00
[Email protected] ~]#
Example seven using the math library
There is an article that calculates a 100-bit pi value.
[Email protected] ~]# echo "SCALE=100; A (1) * | Bc
Runtime error (func= (main), adr=11): Function a not defined.
[Email protected] ~]# echo "SCALE=100; A (1) * | Bc-l
3.141592653589793238462643383279502884197169399375105820974944592307\
8164062862089986280348253421170676

Original address

Related Keywords:
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.