In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
As you all know, windows has a calculator tool that we use a lot in our work life. But did you know that Linux also has a calculator?
Of course, I'm probably talking about calculator tools on the command line, not interface calculators. Good Xu is a Linux application development engineer, usually working under the command line, so good Xu is not very good at interface operations.
Start Calculator
Starting this command-line calculator is easy, just type bc under the terminal. BC stands for Basic Calculator. Quitting the calculator is also easy, just type quit.
option
Of course, this calculator can also have options. The commonly used options are as follows:
-i: Forced entry into interactive mode;
-l: defines the standard mathematical library used;
-w: Gives a warning message for POSIX bc extensions;
-q: does not print normal GNU bc environment information;
-v: Display instruction version information;
-h: Display help information for instructions.
basic operations
After starting the calculator, you can calculate inside. The basic operations are as follows:
+ addition
- subtraction
multiplication
/Division
^Index
% remainder
Of course, if it only supports these operations, it is too trivial, and its power is still to come.
expression operation
If you want to compute an expression, type it directly into bc. bc stores the variable, and other expressions can use it directly.
binary switching
As programmers, we often deal with binary, decimal, and hexadecimal, and bc certainly supports these operations. The input and output bases are stored in the ibase and obbase variables, respectively, with a default value of 10 and valid values of 2 through 16. In other words, we can use 3, 5, 7, 9, etc. in addition to the conventional 2, 8, 10, and 16 bases.
However, when setting up, be sure to set obase first, and then set ibase. Because if you set ibase first, then set obase, the number after obase equals is considered to be the base value set by ibase.
precision adjustment
bc has a default precision of 0, which means bc is stored as an integer by default. We can adjust the precision by setting the scale variable.
Use built-in functions
In addition to simple arithmetic operators, bc provides many advanced mathematical functions through external libraries of mathematical functions. In this case, the-l option should be used. Common functions are as follows:
square root of N sqrt(N)
sine of X (X is radians) s(X)
cosine of X (X is radians) c(X)
arcsine of X (return value is radians) a(X)
Natural logarithm of X: l(X)
Exponential logarithm of X: e(X)
programming operation
As a computational language, bc supports simple declarations (variable assignments, interrupts, returns, etc.), compound statements (if, while, for loop, etc.), and custom functions. This function is relatively advanced, and more detailed content can be referred to the official guide.
Operation via pipeline
Direct examples:
You can also use the HERE command:
The ability to use the built-in variable last to refer to the last result:
Last can also be replaced with a dot:
Using bc in a non-interactive interface
So far, we've been using bc under the interactive interface. But a more popular way to use bc is to run bc in scripts that don't have an interactive interface. In this case, you can use the echo command and pipe the input to bc.
Run Results:
A simple calculation of triangle area procedures
Results:
conclusion
bc is a very powerful command line calculator, at least much more powerful than the calculator that comes with window. This tool is especially suitable for linux developers, in the case of no need to open the phone calculator, it is very convenient to perform mathematical operations. At the same time, scripts can be written to perform more complex mathematical operations. More powerful features are yet to be discovered!
For more exciting content, please pay attention to the public number good Linux, the public reply 1024 can get free 5T technical information, including: Linux, C/C++, Python, Raspberry Pi, embedded, Java, artificial intelligence, and so on. Reply to the group in the public number, inviting you to enter the technical exchange group of experts.
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.