Section 4.2. Mathematical Operators


4.2. Mathematical Operators

C# uses five mathematical operators : four for standard calculations and one to return the remainder when dividing integers. The following sections consider the use of these operators.

4.2.1. Simple Arithmetical Operators (+, -, *, / )

C# offers four operators for simple arithmetic: the addition ( + ), subtraction ( - ), multiplication ( * ), and division ( / ) operators work as you might expect, with the possible exception of integer division.

When you divide two integers, C# divides like a child in the third grade: it throws away any fractional remainder. Thus, dividing 17 by 4 returns a value of 4 (with C# discarding the remainder of 1).

This limitation is specific to integer division. If you do not want the fractional part thrown away, you can use one of the types that support decimal values, such as float or double. Division between two floats (using the / operator) returns a decimal answer. Integer and floating-point division is illustrated in Example 4-1.

Example 4-1. Integer and float division
 using System; public class Tester {  public static void Main(  )  {  int smallInt = 5;  int largeInt = 12;  int intQuotient;  intQuotient = largeInt / smallInt;  Console.WriteLine("Dividing integers. {0} / {1} = {2}",  largeInt, smallInt, intQuotient);  float smallFloat = 5;  float largeFloat = 12;  float FloatQuotient;  FloatQuotient = largeFloat / smallFloat;  Console.WriteLine("Dividing floats. {0} / {1} = {2}",  largeFloat, smallFloat, FloatQuotient);  } } Output: Dividing integers. 12 / 5 = 2 Dividing floats. 12 / 5 = 2.4 

4.2.2. The modulus Operator (%)

C# provides a special operator, modulus ( % ), to retrieve the remainder from integer division. For example, the statement 17%4 returns 1 (the remainder after integer division).

You read that statement as, "Seventeen modulo four equals 1."


Example 4-2 demonstrates the effect of division on integers, floats, doubles, and decimals.

Example 4-2. Modulus operator
 using System; class Values {    static void Main(  )    {       int firstInt, secondInt;       float firstFloat, secondFloat;       double firstDouble, secondDouble;       decimal firstDecimal, secondDecimal;       firstInt = 17;       secondInt = 4;       firstFloat = 17;       secondFloat = 4;       firstDouble = 17;       secondDouble = 4;       firstDecimal = 17;       secondDecimal = 4;       Console.WriteLine( "Integer:\t{0}\nfloat:\t\t{1}",       firstInt / secondInt, firstFloat / secondFloat );       Console.WriteLine( "double:\t\t{0}\ndecimal:\t{1}",       firstDouble / secondDouble, firstDecimal / secondDecimal );       Console.WriteLine( "\nRemainder(modulus) from integer division:\t{0}",       firstInt % secondInt );    } } 

Writeline Control Characters

Consider this line from Example 4-2:

 Console.WriteLine("Integer:\t{0}\nfloat:\t\t{1}\n",      firstInt/secondInt, firstFloat/secondFloat); 

It begins with a call to Console.Writeline( ) , passing in this partial string:

 "Integer:\t{0}\n 

This will print the characters Integer: followed by a tab ( \t ), the first parameter ( {0} ), and a newline character ( \n ).

The next string snippet:

 float:\t\t{1}\n 

is very similar. It prints float: , followed by two tabs (to ensure alignment), the contents of the second parameter ( {1} ), and then another newline. Notice the subsequent line, as well:

 Console.WriteLine( "\nRemainder(modulus) from integer     division:\t{0}",  firstInt%secondInt); 

This time, the string begins with a newline character, which causes a line to be skipped just before the string Modulus : is printed. You can see this effect in the output.


The output looks like this:

 Integer:            4     float:              4.25     double:             4.25     decimal:            4.25     Remainder(modulus) from integer division:         1 

The modulus operator is more than a curiosity ; it greatly simplifies finding every nth value, as you'll see in Chapter 5.




Learning C# 2005
Learning C# 2005: Get Started with C# 2.0 and .NET Programming (2nd Edition)
ISBN: 0596102097
EAN: 2147483647
Year: 2004
Pages: 250

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net