Smoker---------Linux Bash basic features arithmetic operations (5)

Source: Internet
Author: User
Tags arithmetic arithmetic operators modulus

Arithmetic operations

Arithmetic: The oldest, the most basic, and the first parts of mathematics. It studies the properties of numbers and their operations. Accumulating and collating the arithmetic of numbers and numbers in the application process, and then forming the oldest mathematical-arithmetic

Arithmetic operations: Mathematical operations such as addition, subtraction, multiplication, addition, and the root of a number are distinguished from geometric operations.


Symbols for arithmetic operations

Complete the basic arithmetic operation (arithmetic operators) notation, which is the symbol used to handle arithmetic

650) this.width=650; "src=" https://s4.51cto.com/wyfs02/M02/9D/7D/wKioL1mBI7aBHmC4AAFyTuNWOrY877.jpg "title=" arithmetic operations _ Meitu_1.jpg "alt=" Wkiol1mbi7abhmc4aafytunwory877.jpg "/>


1) Addition: the repetition or accumulation of similar things, is the beginning of the numerical operation, not like an Apple + an orange The result can only be equal to two fruits there is a relationship between classification and categorization

Formula: Summand + addend = and (A+b=c)

2) Subtraction: inverse of addition; minus one number equals the opposite number of the number.

Formula: meiosis-meiosis = Difference (a-b=c)

3) Multiplication: The special form of addition; the same number adds up the shortcut. The result of its operation is called product. From a philosophical point of view, multiplication is the result of qualitative change caused by the quantitative change of addition

Formula: Multiplier * multiplier = product (a*b=c)

4) Division: multiplication of inverse; according to the multiplication table, two integers can be written calculation with long Division (straight division). If the divisor has a fractional part (or decimal point), the decimal point will be taken down on the calculation, and if the divisor has a decimal point, the divisor will be shifted with the decimal place of the divisor, until the divisor has no decimal points.

Formula: Dividend ÷ divisor = quotient (a÷b=c)

5) Modulo: The two concepts of modulo operation ("Modulo operation") and take-rest operation ("complementation") overlap but are not identical. The main difference is that there are different operations when dividing a negative integer into a division operation. The modulus is mainly used in computer terminology. The remainder is more of a mathematical concept.

Formula:

1. Seek integer quotient: c = A/b;

2. Calculate the modulus or remainder: R = a-c*b.

Linux: R=a%b (r<b)


6) Narcissus number: Narcissus number refers to an n-bit positive integer (n≥3), and its number on each bit of the sum of n power equals to itself. (Example: 1^3 + 5^3+ 3^3 = 153).


Arithmetic operations in Linux

The data is taken from memory to the register of the CPU, and after the operation, it is stored back into memory.

Stored back: let count= $count +1

Save to Another place: let var= $count +1


1) Let command

Format: Let var= arithmetic operation expression

$ help Letlet is a shell builtinlet:let var= arithmetic operation expression
$ num1=3$ num2=10$ let var= $num 1+ $num 2$ Echo $var 13


2) Variable Reference implementation assignment

Format:

var=$[arithmetic operation expression]

var=$ ((arithmetic expression))


echo $[arithmetic operation expression]

echo $ ((arithmetic expression))

$ var=$[$num 2% $num 1]$ echo $var 1
$ echo $ (($num 2% $num 1)) 1

3) The Command Reference implementation assignment, (the operation symbol in the command, * will be interpreted as a special character, need to be escaped)

Format:

var=$ (expr operand operator operand)

$ var=$ (Expr $num 2 \* $num 1) $ echo $var 30
$ echo $ (Expr $num 2 \* $num 1) 30


Bash built-in random number generator: $RANDOM (2^31-1)

Arbitrary random number in any range: $A

1) Let var= $RANDOM% $A

$ a=60$ Let var= $RANDOM% $A $ echo $var 58

2) var=$[$RAMDOM%60]

[[email protected] ~]# var=$[$RANDOM%60][[email protected] ~]# echo $var 59

3) var=$ (($RANDOM%60))

[[email protected] ~]# var=$ (($RANDOM%60)) [[email protected] ~]# echo $var 13

4) var=$ (expr $RANDOM% 60)

# var=$ (expr $RANDOM%) # echo $var 16


Put the variable back in place: The data stored in the variable is taken out, and after the operation, it is returned .

[[email protected] ~]# Count=1[[email protected] ~]# let count= $count +1[[email protected] ~]# echo $count 2[[email protecte D] ~]# let count= $count +1[[email protected] ~]# echo $count 3

Enhanced assignment: take out the data stored in the variable and put it back in place after the operation

Symbol: + = = *=/=%= ^=

Count+=1 equivalent to count= $count +1

Count-=1 equivalent to count= $count-1

Count*=1 equivalent to count= $count * *

....

[[email protected] ~]# let Count+=1[[email protected] ~]# echo $count 4[[email protected] ~]# let Count+=1[[email protected ] ~]# Echo $count 5

Self-increment, self-reduction

count++ equivalent to Count+=1

count--equivalent to Count-=1

[[email protected] ~]# let count++ #相当于let count+=1[[email protected] ~]# echo $count 6[[email protected] ~]# let count-- #相当于let count-=1 [[email protected] ~]# echo $count 5


This article is from the "Reading" blog, make sure to keep this source http://sonlich.blog.51cto.com/12825953/1952897

Smoker---------Linux Bash basic features arithmetic operations (5)

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.