Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Linux basic command-bc

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Bc

Bc is an arithmetic language. Its syntax is similar to c language and can be executed interactively. A standard math library can be obtained through the command line option. If requested, define a math library before processing any files. BC starts with the code that handles all the files. The files listed on the command line are arranged in the order listed. After all the files have been processed, BC reads from standard input. All code is executed at read time. (if the file contains a command to stop the processor, BC will never be read from standard input.

The scope of this command: RedHat, RHEL, Ubuntu, CentOS, SUSE, openSUSE, Fedora.

1. Grammar

Bc [- hlwsqv] [long-options] [file...]

2. List of options

Option

Description

-h |-- help

Help information

-v |-- version

Display command version information

-l |-- mathlib

Define a standard math library

-I |-- interactive

Forced interaction

-w |-- warn

Displays a warning message for POSIX

-s |-- standard

Using the POSIX standard to handle

-Q |-- quiet

Do not display welcome information

3. Description

1) data

The most basic element in bc is numbers. A number is a number of arbitrary precision. This accuracy includes both the integer part and the fraction part. All numbers are represented internally in decimal, and all calculations are represented in decimal. (this version truncates the results of division and multiplication.) Numbers have two attributes: length and decimal places. The length is the total number of valid places in the number, and the decimal place is the number of significant digits after the decimal point. For example, the length of 0.000001 is 6, the decimal place is 6, the length of 1935.000 is 7, and the decimal place is 3.

2) variables

Numbers are stored in two types of variables, simple variables and arrays. The variable name begins with a letter, followed by any number of letters, numbers, and underscores. All letters must be lowercase. (full alphanumeric name is the extension. In POSIX bc, all names are lowercase letters.) The type of the variable is explicit in the context because all array variable names are followed by square brackets ([]).

There are four special variables: scale, ibase, obase, and last. Scale defines how some operations use numbers after the decimal point, and the default value is 0. Ibase and obase define the conversion base for input and output numbers. The default value is cardinality 10. Last (extension) is a variable. It is the value of the last number printed. All of these variables may have values assigned to them, as well as values used in expressions.

3) comments

Comments in bc begin with the character "/ *" and end with the character "* /". Comments can start anywhere and appear as a single space in the input. (this causes comments to separate other entries. For example, no comment is found in the middle of the variable name.) Comments include any new line (end of line) between the beginning and end of the comment.

To support the use of bc scripts, line comments have been added as extensions. Line comments start with the "#" character and continue to the end of the line. The line end character is not part of the comment, but is processed normally.

If any file on the command line cannot be opened, bc reports that the file is unavailable and terminates.

4. About expressions

These numbers are manipulated by expressions and statements. Because the language is designed to be interactive, statements and expressions are executed as soon as possible. There is no "master" program, but executes code when encountered.

A simple expression is just a constant. BC converts constants to internal decimal numbers using the current input base specified by the variable ibase. (there is one exception in the function.) The valid values for ibase are 2 to 16, and after you go beyond this range, the value assigned to ibase may be 2 or 16. The input number may contain the characters 0-9 and Amurf. (note: they must be uppercase letters and lowercase letters are variable names.) No matter what the value of ibase is, the singular number always has the value of that number. (that is, Aban 10.) For multiple digits, bc changes all input digits greater than or equal to ibase to the value of IBASE-1. This allows FFF to always enter a maximum of 3 digits.

Full expressions are similar to many other high-level languages. Because there is only one number, there are no mixed-type rules. On the contrary, there are rules about the accuracy of expressions. Each expression has a precision. This is derived from the precision of the original number, the operation performed, and in many cases the value of the variable scale. The valid value of the variable scale is 0 to the maximum value that can be expressed by c language integers.

In the description of the following legal expression, "expr" refers to a complete expression, and "var" refers to a simple or array variable. Unless otherwise mentioned, the accuracy of the result is the maximum accuracy of the expression involved.

Ordinary expression

Description

-expr

The result is a negative of the expression.

+ + var

The variable increases by 1, and the new value is the result of the expression

-- var

The variable minus 1, and the new value is the result of the expression

Var++

The result of the expression is the value of the variable, and then the variable increases by 1

Var--

The result of the expression is the value of the variable, and then the variable minus 1

Expr * expr

The result of the expression is the product of the two expressions

Expr + expr

The result of the expression is the sum of the two expressions

Expr-expr

The result of the expression is the difference between the two expressions

Expr / expr

The result of the expression is the quotient of the two expressions. The precision of the result is the value of the variable scale.

Expr expr

The result of the expression is "remainder"

Expr ^ expr

The result of the expression is to the power of n, and the second expression must be an integer. If the exponent is negative, then the precision of the result is scale

(expr)

Force the expression to be evaluated

Var = expr

A variable is the value of an expression

Var = expr

Equivalent to "var=varexpr", for example, "var- = expr" is equivalent to "var=var-expr"

Relational expression

A relational expression is a special expression that always evaluates to 0 or 1. If the relationship is true, it evaluates to 1; if the relationship is false, the result is 0. These expressions may appear in any legal expression. (POSIX bc requires relational expressions to be used only for if, while, and statements, and can be tested only once.)

Expr1

< expr2 expr1 expr2 expr1 >

= expr2

Expr1 = = expr2

Expr1! = expr2

Expr1 & & expr2

Expr1 | | expr2

! expr

The precedence of the operator is as follows, increasing from top to bottom:

Operator

Combination mode

| | |

Left union

& &

Left union

!

Do not combine

Relational operator

Left union

Assignment operator

Right union

+ and-

Left union

*, /,%

Left union

^

Right union

Unary operator-

Do not combine

+ + and--

Do not combine

This priority is selected so that POSIX-compliant bc programs run correctly. This results in some unusual behavior of relational and logical operators when used with assignment expressions. For example, the following expression:

A = 3 ", y,"

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report