In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use the command line calculator GNU bc in Linux". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the command line calculator GNU bc in Linux".
Every modern Linux desktop distribution comes pre-installed with a calculator program with a graphical interface. However, if your workspace is full of command-line windows, you are sure to deal with some number-related problems in one of the command-line windows. Maybe you're looking for a calculator program based on the command line. If so, GNU bc (an acronym for "basic calculator") will be your choice. Of course, there are many command-line-based calculator applications under Linux, and I think GNU bc is the most powerful and useful.
Before the era of GNU, bc was actually a famous precision computing language. Its birth dates back to the Unix period in the 1970s. Bc was originally known as a programming language whose syntax is similar to that of C language. Over time, the original bc evolved into POSIX bc and eventually became the GNU bc it is today.
Characteristics of GNU bc
The current GNU bc is the result of several improvements and enhancements to the early bc. It is currently included in all major GNU/Linux distributions. GNU bc supports input and output of high-precision numbers and a variety of numeric types, such as binary, decimal, and hexadecimal.
If you are familiar with C, you will find that bc uses the same or similar arithmetic operators as C. Supported operators include the arithmetic operator (+, -, *, /,%, +, -), the comparison operator (, = =,! =, =), the logical operator (!, &, |), the bit operator (&, |, ~,), and the compound assignment operator (+ =,-=, * =, / =,% =, & =, | =, & & =, | | =, =). Bc has many useful functions built in, such as square root, sine, cosine, arcsine, natural logarithm, exponent, and so on.
How to use GNU bc
As a command-line-based calculator, there is no limit to the use of GNU bc. In this article, I'll introduce you to several common features of the bc command. If you want more detailed guidance, you can refer to the official guide.
If you don't have a pre-written bc script, you can run bc in interactive mode. In this mode, any declaration or expression that you enter that ends with a carriage return will be evaluated immediately. You need to enter the following command to enter the bc interface. If you want to exit bc, you can type 'quit'' and press enter.
The code is as follows:
$bc
Enter the examples shown below in this article in the bc interface.
Enter expression
If you want to evaluate an arithmetic expression, we can enter the expression at the flashing cursor and press enter to confirm. You can also store the result in a variable and then use it in other expressions.
In a bc interface, there is no limit to the number of command history. Use the up arrow keys to view the previously entered commands. If you want to limit the number of commands that history can be saved, you can set a special variable named history to the value you want. The variable defaults to-1, that is, "there is no limit on the number of history records".
Input / output base switch
What often happens is that you enter an expression and want to use binary or hexadecimal to display the result. Bc allows you to convert between input and output digits. The input and output number bases are stored in the ibase and obase variables, respectively, with a default value of 10 and valid values of 2 to 16 (or the value of the environment variable BCBASEMAX). You only need to change the values of ibase and obase to convert between different bases. Here is an example of finding the sum of two hexadecimal / binary numbers:
It is important to note that I deliberately put obase=16 in front of ibase=16, and vice versa. This is because if I type ibase=16 first, then the 16 in the subsequent obase=16 will be considered a hexadecimal number, which is 22 in decimal. Of course, this is not what we expected.
Adjustment precision
In bc, the precision of numbers is stored in a special variable called scale. This variable represents the number of digits after the decimal point. Scale defaults to 0, which means that all numbers and results are stored as integers. You can adjust the accuracy of the value by changing the value of the special variable scale.
The code is as follows:
Scale=4
Use built-in functions
In addition to simple arithmetic operators, GNU bc provides many advanced mathematical functions through an external library of mathematical functions. You can use the "- l" option to open bc in the command line interface.
Some built-in functions are described here.
Quadratic root of N:
Sqrt (N)
The sine of X (X is radians):
S (X)
The cosine of X (X is radians):
C (X)
The arcsine of X (the return value is radians):
A (X)
Natural logarithm of X:
L (X)
Exponential logarithm of X:
E (X)
Use it as a language
As a computing language, GNU bc supports simple declarations (variable assignments, interrupts, returns, etc.), compound statements (if, while, for loop, etc.), and custom functions. I won't go into the details of these features here, but you can learn how to use them through the official guide. Here is an example of a simple function:
The code is as follows:
Define dummy (x) {
Return (x * x)
}
Dummy (9)
eighty-one
Dummy (4)
sixteen
Using GNU bc in a non-interactive interface
So far, we have been using bc in the interactive interface. However, a more popular way to use bc is to run bc in a script that does not have an interface. In this case, you can use the echo command and pipe to send input to bc. For example:
The code is as follows:
$echo "40 percent 5" | bc
$echo "scale=4; 10 Compact 3" | bc
$echo "obase=16; ibase=2; 11101101101100010" | bc
Thank you for reading, the above is the content of "how to use the command line calculator GNU bc in Linux". After the study of this article, I believe you have a deeper understanding of how to use the command line calculator GNU bc in Linux, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.