Home

Mathematics in C#

 

Overview of Numbers

 

Introduction

If you consider it in the purest sense of a computer language like C, C++, Pascal, Visual Basic, and Java, etc, C# doesn't have its own built-in support for mathematics. It must borrow this functionality either from other libraries or from other language. Fortunately, all of this is particularly easy.

To perform the basic algebraic and geometric operations in C#, you can use methods of the Math class of the .NET Framework. As seen in the previous lesson, you can also take advantage of Visual Basic's very powerful library of functions. This library is one of the most extended set of functions of various area of business mathematics.

We know different ways to declare a variable of a numeric type. Here are examples:

using System;

class Program
{
    static int Main()
    {
        short   sNumber;
        int     iNumber;
        double  dNumber;
        decimal mNumber;

        return 0;
    }
}

The Sign of a Number

One of the primary rules to observe in C# is that, after declaring a variable, before using it, it must have been initialized. Here are examples of initializing variables:

using System;

class Program
{
    static int Main()
    {
        short   sNumber = 225;
        int     iNumber = -847779;
        double  dNumber = 9710.275D;
        decimal mNumber = 35292742.884295M;

        Console.WriteLine("Short Integer:      {0}", sNumber);
        Console.WriteLine("Integral Number:    {0}", iNumber);
        Console.WriteLine("Double-Precision:   {0}", dNumber);
        Console.WriteLine("Extended Precision: {0}", mNumber);
	
        return 0;
    }
}

This would produce:

Short Integer:      225
Integral Number:    -847779
Double-Precision:   9710.275
Extended Precision: 35292742.884295
Press any key to continue . . .

When initializing a variable using a constant, you decide whether it is negative, 0 or positive. This is referred to as its sign. If you are getting the value of a variable some other way, you may not know its sign. Although you can use comparison operators to find this out, the Math class provides a method to check it out for you.

To find out about the sign of a value or a numeric variable, you can call the Math.Sign() method. It is overloaded in various versions whose syntaxes are:

public static int Sign(sbyte   value);
public static int Sign(short   value);
public static int Sign(int     value);
public static int Sign(long    value);
public static int Sign(sbyte   value);
public static int Sign(double  value);
public static int Sign(decimal value);

When calling this method, pass the value or the variable you want to consider, as argument. The method returns:

  • -1 if the argument is negative
  • 0 if the argument is 0
  • 1 if the argument is positive

Here are examples of calling the method:

using System;

class Program
{
    static int Main()
    {
        short   sNumber = 225;
        int     iNumber = -847779;
        double  dNumber = 9710.275D;
        decimal mNumber = 35292742.884295M;

        Console.WriteLine("Number: {0} => Sign: {1}",
                          sNumber, Math.Sign(sNumber));
        Console.WriteLine("Number: {0} => Sign: {1}",
                          iNumber, Math.Sign(iNumber));
        Console.WriteLine("Number: {0} => Sign: {1}",
                          dNumber, Math.Sign(dNumber));
        Console.WriteLine("Number: {0} => Sign: {1}\n",
                          mNumber, Math.Sign(mNumber));
	
        return 0;
    }
}

This would produce:

Number: 225 => Sign: 1
Number: -847779 => Sign: -1
Number: 9710.275 => Sign: 1
Number: 35292742.884295 => Sign: 1

Press any key to continue . . .

The Integral Side of a Floating-Point Number

As reviewed in Lesson 1, when dealing with a floating-point number, it consists of an integral side and a precision side; both are separated by a symbol which, in US English, is the period. In some operations, you may want to get the integral side of the value. The Math class can assist you with this.

To get the integral part of a decimal number, the Math class can assist you with the Trancate() method, which is overloaded in two versions whose syntaxes are:

public static double Truncate(double d);
public static double Truncate(double d);

When calling this method, pass it a number or a variable of float, double, or decimal type. The method returns the int side of the value. Here is an example of calling it:

using System;

class Program
{
    static int Main()
    {
        float number = 225.75f;

        Console.WriteLine("The integral part of {0} is {1}\n",
                          number, Math.Truncate(number));
	
        return 0;
    }
}

This would produce:

The integral part of 225.75 is 225

Press any key to continue . . .

The Minimum of Two Values

If you have two numbers, you can find the minimum of both without writing your own code. To assist you with this, the Math class is equipped with a method named Min. This method is overloaded in various versions with each version adapted to each integral or floating-point data type. The syntaxes are:

public static byte    Min(byte    val1, byte    val2);
public static sbyte   Min(sbyte   val1, sbyte   val2);
public static short   Min(short   val1, short   val2);
public static ushort  Min(ushort  val1, ushort  val2);
public static int     Min(int     val1, int     val2);
public static uint    Min(uint    val1, uint    val2);
public static float   Min(float   val1, float   val2);
public static long    Min(long    val1, long    val2);
public static ulong   Min(ulong   val1, ulong   val2);
public static double  Min(double  val1, double  val2);
public static decimal Min(decimal val1, decimal val2);

Here is an example of calling the method:

using System;

class Program
{
    static int Main()
    {
        int number1 = 8025;
        int number2 = 73;

        Console.WriteLine("The minimum of {0} and {1} is {2}",
            number1, number2, Math.Min(number1, number2));
	
        return 0;
    }
}

This would produce:

The minimum of 8025 and 73 is 73
Press any key to continue . . .

The Maximum Integer Value of a Series

As opposed to the minimum of two numbers, you may be interested in the higher of both. To help you find the maximum of two numbers, you can call the Max() method of the Math class. It is overloaded in various versions with one of each type of numeric data. The syntaxes of this method are:

public static byte    Max(byte    val1, byte    val2);
public static sbyte   Max(sbyte   val1, sbyte   val2);
public static short   Max(short   val1, short   val2);
public static ushort  Max(ushort  val1, ushort  val2);
public static int     Max(int     val1, int     val2);
public static uint    Max(uint    val1, uint    val2);
public static float   Max(float   val1, float   val2);
public static long    Max(long    val1, long    val2);
public static ulong   Max(ulong   val1, ulong   val2);
public static double  Max(double  val1, double  val2);
public static decimal Max(decimal val1, decimal val2);

Here is an example of calling the method:

using System;

class Program
{
    static int Main()
    {
        int number1 = 8025;
        int number2 = 73;

        Console.WriteLine("The maximum of {0} and {1} is {2}",
            number1, number2, Math.Max(number1, number2));
	
        return 0;
    }
}

This would produce:

The maximum of 8025 and 73 is 8025
Press any key to continue . . .

 

 

Home Copyright © 2006 FunctionX, Inc. Next