C# Divide Numbers (Cast Ints)

Understand the division operator, and evaluate casts for dividing numbers.
Divide. Division uses the forward-slash character. With division, one common problem is related to correctly casting the operands in the division.
With correct syntax we get a double from the division of 2 ints. But if we do not cast an int in the expression, the result may be truncated (and not useful for us).Int, uintCasts
An example. The program declares 2 Int32 variables and then divides the first by the second in 5 different operations. The operations differ in lexical syntax.

Casts: In the first and third assignments, there were no casts on the integers or the entire division was cast at once.

Double

Tip: To coerce the intermediate language to have the correct casts, we cast either operand (number2 and number5) or both operands.

And: When either operand or both operands are cast to double, the output is approximately 0.29.

C# program that divides numbers using System; class Program { static void Main() { // Divide the first number by the second number. int operand1 = 100; int operand2 = 345; // Incorrect division for double: double number1 = operand1 / operand2; Console.WriteLine(number1); // Correct division for double: double number2 = (double)operand1 / operand2; Console.WriteLine(number2); // Incorrect division for double: double number3 = (double)(operand1 / operand2); Console.WriteLine(number3); // Correct division for double: double number4 = (double)operand1 / (double)operand2; Console.WriteLine(number4); // Correct division for double: double number5 = operand1 / (double)operand2; Console.WriteLine(number5); } } Output 0 0.289855072463768 0 0.289855072463768 0.289855072463768
Discussion. The C# language specification provides the definition of all its predefined operators. It describes these predefined operators in the same way as real methods.

So: You can think of the 2 operands on each division expression as parameters to a method with a custom syntax.

Discussion, continued. The C# compiler removes the cast from the number3 assignment. For the 3 assignments where one or both operands are casts, it inserts casts to both operands.

Note: The complexity of this situation is mainly because of the rules by which the C# compiler interprets casts in the source code.

Numeric promotion. In numeric promotion, a parameter to an operator or method can be implicitly converted to match the operator or method signature.

And: All the parameters will be promoted to match the signature. This is an algorithm the C# compiler uses to ensure more programs compile.

Modulo. A modulo operator implements remainder division. This might remind you of doing long division in school, where you must compute the "leftover" part after dividing 2 numbers.Modulo
A summary. We used the casting syntax in division to ensure the correct number of decimal places. You need to cast one or both operands to get a double type.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls