Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Plastic addition and subtraction
Plastic addition and subtraction
. . . It can be achieved in three ways.

1. Addition and subtraction

take for example

a=a+b

b=a-b

a=a-b

Of course, this method is not very good, because it may lead to the loss of accuracy. . . .

For example, a = 3.123456b =14567.000.

After the exchange, the variable value becomes:

a = 1234567.000000 b = 3. 125000

So it is suitable for variables that exchange integer and floating-point values.

2. Multiplication and division

a = a * b; b = a/b; a = a/b;

Multiplication and division is more like the mapping from addition and subtraction to multiplication and division, similar to addition and subtraction: it can handle integer and floating-point variables, but it also has the problem of precision loss when dealing with floating-point variables. But the multiplication and division method requires a little more-b cannot be 0.

As can be seen from the above, addition, subtraction, multiplication and division may overflow, and multiplication and division overflow will be particularly serious. In fact, neither method will overflow. Take addition and subtraction as an example. The addition operation in the first step may cause overflow, but the overflow caused by it will overflow in the subsequent subtraction operation.

3. XOR method

a ^= b; b ^= a; a ^= b; XOR method can complete the exchange of integer variables, but it cannot complete the exchange of floating-point variables.

So these three methods have their own uses, and you should choose according to your own situation. . . . .