# 算術運算子 (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

• 在 checked 內容中，如果常數運算式發生溢位，則會發生編譯時期錯誤。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.
• 在 unchecked 內容中，會捨棄目的型別不適用的高序位位元，以便將結果截斷。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