|
| 1 | +using System; |
| 2 | + |
| 3 | +namespace operators |
| 4 | +{ |
| 5 | + public static class ArithmeticOperators |
| 6 | + { |
| 7 | + public static void Examples() |
| 8 | + { |
| 9 | + Console.WriteLine("==== ++ and -- operators"); |
| 10 | + Increment(); |
| 11 | + Decrement(); |
| 12 | + |
| 13 | + Console.WriteLine("==== Unary + and - operators"); |
| 14 | + UnaryPlusAndMinus(); |
| 15 | + |
| 16 | + Console.WriteLine("==== *, /, %, +, and - operators"); |
| 17 | + Multiplication(); |
| 18 | + IntegerDivision(); |
| 19 | + IntegerAsFloatingPointDivision(); |
| 20 | + FloatingPointDivision(); |
| 21 | + IntegerRemainder(); |
| 22 | + FloatingPointRemainder(); |
| 23 | + Addition(); |
| 24 | + Subtraction(); |
| 25 | + |
| 26 | + Console.WriteLine("==== Precedence and associativity examples"); |
| 27 | + PrecedenceAndAssociativity(); |
| 28 | + |
| 29 | + Console.WriteLine("==== Compound assignment"); |
| 30 | + CompoundAssignment(); |
| 31 | + CompoundAssignmentWithCast(); |
| 32 | + |
| 33 | + Console.WriteLine("==== Special cases"); |
| 34 | + CheckedUnchecked(); |
| 35 | + FloatingPointOverflow(); |
| 36 | + RoundOffErrors(); |
| 37 | + } |
| 38 | + |
| 39 | + private static void Increment() |
| 40 | + { |
| 41 | + // <SnippetPrefixIncrement> |
| 42 | + double a = 1.5; |
| 43 | + Console.WriteLine(a); // output: 1.5 |
| 44 | + Console.WriteLine(++a); // output: 2.5 |
| 45 | + Console.WriteLine(a); // output: 2.5 |
| 46 | + // </SnippetPrefixIncrement> |
| 47 | + |
| 48 | + // <SnippetPostfixIncrement> |
| 49 | + int i = 3; |
| 50 | + Console.WriteLine(i); // output: 3 |
| 51 | + Console.WriteLine(i++); // output: 3 |
| 52 | + Console.WriteLine(i); // output: 4 |
| 53 | + // </SnippetPostfixIncrement> |
| 54 | + } |
| 55 | + |
| 56 | + private static void Decrement() |
| 57 | + { |
| 58 | + // <SnippetPrefixDecrement> |
| 59 | + double a = 1.5; |
| 60 | + Console.WriteLine(a); // output: 1.5 |
| 61 | + Console.WriteLine(--a); // output: 0.5 |
| 62 | + Console.WriteLine(a); // output: 0.5 |
| 63 | + // </SnippetPrefixDecrement> |
| 64 | + |
| 65 | + // <SnippetPostfixDecrement> |
| 66 | + int i = 3; |
| 67 | + Console.WriteLine(i); // output: 3 |
| 68 | + Console.WriteLine(i--); // output: 3 |
| 69 | + Console.WriteLine(i); // output: 2 |
| 70 | + // </SnippetPostfixDecrement> |
| 71 | + } |
| 72 | + |
| 73 | + private static void UnaryPlusAndMinus() |
| 74 | + { |
| 75 | + // <SnippetUnaryPlusAndMinus> |
| 76 | + Console.WriteLine(+4); // output: 4 |
| 77 | + |
| 78 | + Console.WriteLine(-4); // output: -4 |
| 79 | + Console.WriteLine(-(-4)); // output: 4 |
| 80 | + |
| 81 | + uint a = 5; |
| 82 | + var b = -a; |
| 83 | + Console.WriteLine(b); // output: -5 |
| 84 | + Console.WriteLine(b.GetType()); // output: System.Int64 |
| 85 | + |
| 86 | + Console.WriteLine(-double.NaN); // output: NaN |
| 87 | + // </SnippetUnaryPlusAndMinus> |
| 88 | + } |
| 89 | + |
| 90 | + private static void Multiplication() |
| 91 | + { |
| 92 | + // <SnippetMultiplication> |
| 93 | + Console.WriteLine(5 * 2); // output: 10 |
| 94 | + Console.WriteLine(0.5 * 2.5); // output: 1.25 |
| 95 | + Console.WriteLine(0.1m * 23.4m); // output: 2.34 |
| 96 | + // </SnippetMultiplication> |
| 97 | + } |
| 98 | + |
| 99 | + private static void IntegerDivision() |
| 100 | + { |
| 101 | + // <SnippetIntegerDivision> |
| 102 | + Console.WriteLine(13 / 5); // output: 2 |
| 103 | + Console.WriteLine(-13 / 5); // output: -2 |
| 104 | + Console.WriteLine(13 / -5); // output: -2 |
| 105 | + Console.WriteLine(-13 / -5); // output: 2 |
| 106 | + // </SnippetIntegerDivision> |
| 107 | + } |
| 108 | + |
| 109 | + private static void IntegerAsFloatingPointDivision() |
| 110 | + { |
| 111 | + // <SnippetIntegerAsFloatingPointDivision> |
| 112 | + Console.WriteLine(13 / 5.0); // output: 2.6 |
| 113 | + |
| 114 | + int a = 13; |
| 115 | + int b = 5; |
| 116 | + Console.WriteLine((double)a / b); // output: 2.6 |
| 117 | + // </SnippetIntegerAsFloatingPointDivision> |
| 118 | + } |
| 119 | + |
| 120 | + private static void FloatingPointDivision() |
| 121 | + { |
| 122 | + // <SnippetFloatingPointDivision> |
| 123 | + Console.WriteLine(16.8f / 4.1f); // output: 4.097561 |
| 124 | + Console.WriteLine(16.8d / 4.1d); // output: 4.09756097560976 |
| 125 | + Console.WriteLine(16.8m / 4.1m); // output: 4.0975609756097560975609756098 |
| 126 | + // </SnippetFloatingPointDivision> |
| 127 | + } |
| 128 | + |
| 129 | + private static void IntegerRemainder() |
| 130 | + { |
| 131 | + // <SnippetIntegerRemainder> |
| 132 | + Console.WriteLine(5 % 4); // output: 1 |
| 133 | + Console.WriteLine(5 % -4); // output: 1 |
| 134 | + Console.WriteLine(-5 % 4); // output: -1 |
| 135 | + Console.WriteLine(-5 % -4); // output: -1 |
| 136 | + // </SnippetIntegerRemainder> |
| 137 | + } |
| 138 | + |
| 139 | + private static void FloatingPointRemainder() |
| 140 | + { |
| 141 | + // <SnippetFloatingPointRemainder> |
| 142 | + Console.WriteLine(-5.2f % 2.0f); // output: -1.2 |
| 143 | + Console.WriteLine(5.9 % 3.1); // output: 2.8 |
| 144 | + Console.WriteLine(5.9m % 3.1m); // output: 2.8 |
| 145 | + // </SnippetFloatingPointRemainder> |
| 146 | + } |
| 147 | + |
| 148 | + private static void Addition() |
| 149 | + { |
| 150 | + // <SnippetAddition> |
| 151 | + Console.WriteLine(5 + 4); // output: 9 |
| 152 | + Console.WriteLine(5 + 4.3); // output: 9.3 |
| 153 | + Console.WriteLine(5.1m + 4.2m); // output: 9.3 |
| 154 | + // </SnippetAddition> |
| 155 | + } |
| 156 | + |
| 157 | + private static void Subtraction() |
| 158 | + { |
| 159 | + // <SnippetSubtraction> |
| 160 | + Console.WriteLine(47 - 3); // output: 44 |
| 161 | + Console.WriteLine(5 - 4.3); // output: 0.7 |
| 162 | + Console.WriteLine(7.5m - 2.3m); // output: 5.2 |
| 163 | + // </SnippetSubtraction> |
| 164 | + } |
| 165 | + |
| 166 | + private static void PrecedenceAndAssociativity() |
| 167 | + { |
| 168 | + // <SnippetPrecedenceAndAssociativity> |
| 169 | + Console.WriteLine(2 + 2 * 2); // output: 6 |
| 170 | + Console.WriteLine((2 + 2) * 2); // output: 8 |
| 171 | + |
| 172 | + Console.WriteLine(9 / 5 / 2); // output: 0 |
| 173 | + Console.WriteLine(9 / (5 / 2)); // output: 4 |
| 174 | + // </SnippetPrecedenceAndAssociativity> |
| 175 | + } |
| 176 | + |
| 177 | + private static void CompoundAssignment() |
| 178 | + { |
| 179 | + // <SnippetCompoundAssignment> |
| 180 | + int a = 5; |
| 181 | + a += 9; |
| 182 | + Console.WriteLine(a); // output: 14 |
| 183 | + |
| 184 | + a -= 4; |
| 185 | + Console.WriteLine(a); // output: 10 |
| 186 | + |
| 187 | + a *= 2; |
| 188 | + Console.WriteLine(a); // output: 20 |
| 189 | + |
| 190 | + a /= 4; |
| 191 | + Console.WriteLine(a); // output: 5 |
| 192 | + |
| 193 | + a %= 3; |
| 194 | + Console.WriteLine(a); // output: 2 |
| 195 | + // </SnippetCompoundAssignment> |
| 196 | + } |
| 197 | + |
| 198 | + private static void CompoundAssignmentWithCast() |
| 199 | + { |
| 200 | + // <SnippetCompoundAssignmentWithCast> |
| 201 | + byte a = 200; |
| 202 | + byte b = 100; |
| 203 | + |
| 204 | + var c = a + b; |
| 205 | + Console.WriteLine(c.GetType()); // output: System.Int32 |
| 206 | + Console.WriteLine(c); // output: 300 |
| 207 | + |
| 208 | + a += b; |
| 209 | + Console.WriteLine(a); // output: 44 |
| 210 | + // </SnippetCompoundAssignmentWithCast> |
| 211 | + } |
| 212 | + |
| 213 | + private static void CheckedUnchecked() |
| 214 | + { |
| 215 | + // <SnippetCheckedUnchecked> |
| 216 | + int a = int.MaxValue; |
| 217 | + int b = 3; |
| 218 | + |
| 219 | + Console.WriteLine(unchecked(a + b)); // output: -2147483646 |
| 220 | + try |
| 221 | + { |
| 222 | + int d = checked(a + b); |
| 223 | + } |
| 224 | + catch(OverflowException) |
| 225 | + { |
| 226 | + Console.WriteLine($"Overflow occured when adding {a} to {b}."); |
| 227 | + } |
| 228 | + // </SnippetCheckedUnchecked> |
| 229 | + } |
| 230 | + |
| 231 | + private static void FloatingPointOverflow() |
| 232 | + { |
| 233 | + // <SnippetFloatingPointOverflow> |
| 234 | + double a = 1.0 / 0.0; |
| 235 | + Console.WriteLine(a); // output: Infinity |
| 236 | + Console.WriteLine(double.IsInfinity(a)); // output: True |
| 237 | + |
| 238 | + Console.WriteLine(double.MaxValue + double.MaxValue); // output: Infinity |
| 239 | + |
| 240 | + double b = 0.0 / 0.0; |
| 241 | + Console.WriteLine(b); // output: NaN |
| 242 | + Console.WriteLine(double.IsNaN(b)); // output: True |
| 243 | + // </SnippetFloatingPointOverflow> |
| 244 | + } |
| 245 | + |
| 246 | + private static void RoundOffErrors() |
| 247 | + { |
| 248 | + // <SnippetRoundOffErrors> |
| 249 | + Console.WriteLine(.41f % .2f); // output: 0.00999999 |
| 250 | + |
| 251 | + double a = 0.1; |
| 252 | + double b = 3 * a; |
| 253 | + Console.WriteLine(b == 0.3); // output: False |
| 254 | + Console.WriteLine(b - 0.3); // output: 5.55111512312578E-17 |
| 255 | + |
| 256 | + decimal c = 1 / 3.0m; |
| 257 | + decimal d = 3 * c; |
| 258 | + Console.WriteLine(d == 1.0m); // output: False |
| 259 | + Console.WriteLine(d); // output: 0.9999999999999999999999999999 |
| 260 | + // </SnippetRoundOffErrors> |
| 261 | + } |
| 262 | + } |
| 263 | +} |
0 commit comments