# 算术运算符（C# 参考）Arithmetic operators (C# reference)

## 增量运算符 ++Increment operator ++

### 后缀递增运算符Postfix increment operator

x++ 的结果是此操作前的 x 的值，如以下示例所示：The result of x++ is the value of x before the operation, as the following example shows:

int i = 3;
Console.WriteLine(i);   // output: 3
Console.WriteLine(i++); // output: 3
Console.WriteLine(i);   // output: 4


### 前缀增量运算符Prefix increment operator

++x 的结果是此操作后的 x 的值，如以下示例所示：The result of ++x is the value of x after the operation, as the following example shows:

double a = 1.5;
Console.WriteLine(a);   // output: 1.5
Console.WriteLine(++a); // output: 2.5
Console.WriteLine(a);   // output: 2.5


## 减量运算符 --Decrement operator --

### 后缀递减运算符Postfix decrement operator

x-- 的结果是此操作前的 x 的值，如以下示例所示：The result of x-- is the value of x before the operation, as the following example shows:

int i = 3;
Console.WriteLine(i);   // output: 3
Console.WriteLine(i--); // output: 3
Console.WriteLine(i);   // output: 2


### 前缀减量运算符Prefix decrement operator

--x 的结果是此操作后的 x 的值，如以下示例所示：The result of --x is the value of x after the operation, as the following example shows:

double a = 1.5;
Console.WriteLine(a);   // output: 1.5
Console.WriteLine(--a); // output: 0.5
Console.WriteLine(a);   // output: 0.5


## 一元加和减运算符Unary plus and minus operators

Console.WriteLine(+4);     // output: 4

Console.WriteLine(-4);     // output: -4
Console.WriteLine(-(-4));  // output: 4

uint a = 5;
var b = -a;
Console.WriteLine(b);            // output: -5
Console.WriteLine(b.GetType());  // output: System.Int64

Console.WriteLine(-double.NaN);  // output: NaN


ulong 类型不支持一元 - 运算符。The ulong type doesn't support the unary - operator.

## 乘法运算符 *Multiplication operator *

Console.WriteLine(5 * 2);         // output: 10
Console.WriteLine(0.5 * 2.5);     // output: 1.25
Console.WriteLine(0.1m * 23.4m);  // output: 2.34


## 除法运算符 /Division operator /

### 整数除法Integer division

Console.WriteLine(13 / 5);    // output: 2
Console.WriteLine(-13 / 5);   // output: -2
Console.WriteLine(13 / -5);   // output: -2
Console.WriteLine(-13 / -5);  // output: 2


Console.WriteLine(13 / 5.0);       // output: 2.6

int a = 13;
int b = 5;
Console.WriteLine((double)a / b);  // output: 2.6


### 浮点除法Floating-point division

Console.WriteLine(16.8f / 4.1f);   // output: 4.097561
Console.WriteLine(16.8d / 4.1d);   // output: 4.09756097560976
Console.WriteLine(16.8m / 4.1m);   // output: 4.0975609756097560975609756098


## 余数运算符 %Remainder operator %

### 整数余数Integer remainder

Console.WriteLine(5 % 4);   // output: 1
Console.WriteLine(5 % -4);  // output: 1
Console.WriteLine(-5 % 4);  // output: -1
Console.WriteLine(-5 % -4); // output: -1


### 浮点余数Floating-point remainder

• z（如果不为零）的符号与 x 的符号相同。The sign of z, if non-zero, is the same as the sign of x.
• z 的绝对值是 |x| - n * |y| 得出的值，其中 n 是小于或等于 |x| / |y| 的最大可能整数，|x||y| 分别是 xy 的绝对值。The absolute value of z is the value produced by |x| - n * |y| where n is the largest possible integer that is less than or equal to |x| / |y| and |x| and |y| are the absolute values of x and y, respectively.

Console.WriteLine(-5.2f % 2.0f); // output: -1.2
Console.WriteLine(5.9 % 3.1);    // output: 2.8
Console.WriteLine(5.9m % 3.1m);  // output: 2.8


Console.WriteLine(5 + 4);       // output: 9
Console.WriteLine(5 + 4.3);     // output: 9.3
Console.WriteLine(5.1m + 4.2m); // output: 9.3


## 减法运算符 -Subtraction operator -

Console.WriteLine(47 - 3);      // output: 44
Console.WriteLine(5 - 4.3);     // output: 0.7
Console.WriteLine(7.5m - 2.3m); // output: 5.2


## 复合赋值Compound assignment

x op= y


x = x op y


int a = 5;
a += 9;
Console.WriteLine(a);  // output: 14

a -= 4;
Console.WriteLine(a);  // output: 10

a *= 2;
Console.WriteLine(a);  // output: 20

a /= 4;
Console.WriteLine(a);  // output: 5

a %= 3;
Console.WriteLine(a);  // output: 2


byte a = 200;
byte b = 100;

var c = a + b;
Console.WriteLine(c.GetType());  // output: System.Int32
Console.WriteLine(c);  // output: 300

a += b;
Console.WriteLine(a);  // output: 44


## 运算符优先级和关联性Operator precedence and associativity

• 后缀增量 x++ 和减量 x-- 运算符Postfix increment x++ and decrement x-- operators
• 前缀增量 ++x 和减量 --x 以及一元 +- 运算符Prefix increment ++x and decrement --x and unary + and - operators
• 乘法 */% 运算符Multiplicative *, /, and % operators
• 加法 +- 运算符Additive + and - operators

Console.WriteLine(2 + 2 * 2);   // output: 6
Console.WriteLine((2 + 2) * 2); // output: 8

Console.WriteLine(9 / 5 / 2);   // output: 0
Console.WriteLine(9 / (5 / 2)); // output: 4


## 算术溢出和被零除Arithmetic overflow and division by zero

### 整数算术溢出Integer arithmetic overflow

• 在已检查的上下文中，如果在常数表达式中发生溢出，则会发生编译时错误。In a checked context, if overflow happens in a constant expression, a compile-time error occurs. 否则，当在运行时执行此运算时，则引发 OverflowExceptionOtherwise, when the operation is performed at run time, an OverflowException is thrown.
• 在未检查的上下文中，会通过丢弃任何不适应目标类型的高序位来将结果截断。In an unchecked context, the result is truncated by discarding any high-order bits that don't fit in the destination type.

int a = int.MaxValue;
int b = 3;

Console.WriteLine(unchecked(a + b));  // output: -2147483646
try
{
int d = checked(a + b);
}
catch(OverflowException)
{
Console.WriteLine(\$"Overflow occurred when adding {a} to {b}.");
}


### 浮点运算溢出Floating-point arithmetic overflow

double a = 1.0 / 0.0;
Console.WriteLine(a);                    // output: Infinity
Console.WriteLine(double.IsInfinity(a)); // output: True

Console.WriteLine(double.MaxValue + double.MaxValue); // output: Infinity

double b = 0.0 / 0.0;
Console.WriteLine(b);                // output: NaN
Console.WriteLine(double.IsNaN(b));  // output: True


## 舍入误差Round-off errors

Console.WriteLine(.41f % .2f); // output: 0.00999999

double a = 0.1;
double b = 3 * a;
Console.WriteLine(b == 0.3);   // output: False
Console.WriteLine(b - 0.3);    // output: 5.55111512312578E-17

decimal c = 1 / 3.0m;
decimal d = 3 * c;
Console.WriteLine(d == 1.0m);  // output: False
Console.WriteLine(d);          // output: 0.9999999999999999999999999999