Working with Math Routines in C#

Basic math operators—such as plus, minus, and modulus—can

get you only so far. It is only a matter of time before you find that you

need more robust math routines. C# has access to a set of math routines

within the base classes. These are available from within the

System.Math namespace. Table 1 presents a number of the math

methods available.

The Math class is sealed. A sealed class cannot be used

for inheritance. Additionally, all the classes and data members are

static, so you can’t create an object of type Math. Instead,

you use the members and methods with the class name.

Table 1: Math Routines in the Math

Class

Method

  Returns
Abs   Returns the absolute

value of a number.

Ceiling   Returns a value that

is the smallest whole number greater than or equal to a given number.

Exp   Returns E raised to a

given power. This is the inverse of Log.

Floor   Returns a value that

is the largest whole number that is less than or equal to the given

number.

IEEERemainder   Returns the

result of a division of two specified numbers. (This division operation

conforms to the remainder operation stated within Section 5.1 of

ANSI/IEEE Std. 754-1985; IEEE Standard for Binary Floating-Point

Arithmetic; Institute of Electrical and Electronics Engineers, Inc;

1985.)

Log   Returns a value that is

the logarithmic value of the given number.

Log10   Returns a value that

is the base 10 logarithm of a given value.

Max   Returns the larger of

two values.

Min   Returns the smaller

of two values.

Pow   Returns the value of

a given value raised to a given power.

Round   Returns a rounded

value for a number. You can specify the precision of the rounded number.

The number .5 would be rounded down.

Sign   Returns a value

indicating the sign of a value. 1 is returned for a negative number, 0

is returned for zero, and 1 is returned for a positive number.

Sqrt   Returns the square

root for a given value.

Acos   Returns the value of

an angle whose cosine is equal to a given number.

Asin   Returns the value of

an angle whose sine is equal to a given number.

Atan   Returns the value of

an angle whose tangent is equal to a given number.

Atan2   Returns the value of

an angle whose tangent is equal to the quotient of two given numbers.

Cos   Returns a value that

is the cosine of a given angle.

Cosh   Returns a value that

is the hyperbolic cosine for a given angle.

Sin   Returns the sine for

a given angle.

Sinh   Returns the

hyperbolic sine for a given angle.

Tan   Returns the tangent

of a specified angle.

Tanh   Returns the

hyperbolic tangent of a given angle.

The Math class also includes two constants:

PI and E. PI returns the value of

[PI] as 3.14159265358979323846. The E data

member returns the value of the logarithmic base,

2.7182818284590452354.

Most of the math methods in Table 1 are easy to understand. Listing 1

presents a couple of the routines in use.

Listing 1: MathApp.cs — Using Some of the Math

Routines

 1:  //  MathApp.cs - Using a Math routine
 2:  //-----------------------------------------------
 3:  using System;
 4:
 5:  class MathApp
 6:  {
 7:     public static void Main()
 8:     {
 9:        int val2;
10:        char disp;
11:
12:        for (double ctr = 0.0; ctr <= 10; ctr += .2)
13:        {
14:           val2 = (int) Math.Round( ( 10 * Math.Sin(ctr))) ;
15:           for( int ctr2 = -10; ctr2 <= 10; ctr2++ )
16:           {
17:              if (ctr2 == val2)
18:                 disp = 'X';
19:              else
20:                 disp = ' ';
21:
22:              Console.Write("{0}", disp);
23:           }
24:           Console.WriteLine(" ");
25:        }
26:     }
27:  }

The following is the output:

          X
            X
              X
                X
                 X
                  X
                   X
                    X
                    X
                    X
                   X
                  X
                 X
               X
             X
           X
         X
       X
      X
    X
  X
 X
X
X
X
X
 X
  X
    X
     X
       X
         X
           X
             X
               X
                 X
                  X
                   X
                    X
                    X
                    X
                   X
                   X
                 X
                X
              X
            X
          X
        X
      X
     X

This listing maps out the Sin method. A for

statement in Lines 12 to 25 loops through double values, incrementing

them by .2 each iteration. The sine of this value is

obtained using the Math.Sin method in Line 14. The sine is a

value from 1.0 to 1.0. To make the display

easier, this value is converted to a value from 10 to

10. This conversion is done by multiplying the returned sine

value by 10 and then rounding the value with the Math.Round

method.

The result of doing the multiplication and rounding is that

val2 is a value from 10 to 10. A

for loop in Line 15 displays a single line of characters.

This line of characters is spaces, with the exception of the character in

the position equal to val2. Line 24 prints another space to

start a new line. The result of this work is a rough display of a sine

curve.

# # #

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read