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.

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

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).

Tip

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:	{0}
float:		{1}",
      firstInt / secondInt, firstFloat / secondFloat );
      Console.WriteLine( "double:		{0}
decimal:	{1}",
      firstDouble / secondDouble, firstDecimal / secondDecimal );
      Console.WriteLine( "
Remainder(modulus) from integer division:	{0}",
      firstInt % secondInt );

   }
}

The output looks like this:

    Integer:            4
    float:              4.25
    double:             4.25
    decimal:            4.25

    Remainder(modulus) from integer division:         1

Tip

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

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset