Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Book Cover C# Programming with the Public Beta
40022 times
Rated
Read 40,022 times

Contents

Related Categories

Namespaces and the Base Classes - Mathematical Functions

Mathematical Functions

The Math class basically contains a large number of static functions to perform operations such as calculating sines, logarithms etc. It also contains two static members, E and PI, which respectively return the values of the mathematical constants e and p. The class is quite basic: it contains all the important mathematical functions, but not much more. There is at present no support for, for example complex numbers or vector or matrix operations. Also the function names will probably annoy many mathematicians since their names often don't follow normal usage as far as case is concerned. We'll demonstrate the use of this class with a simple application, Math, which displays the values of e and p and also the sines of angles between 0 and 90 degrees. The code that we add to the constructor to the main form class looks like this

  AddItem("e is " + Math.E);
  AddItem("pi is " + Math.PI);
  AddItem("");
  AddItem("sines of angles from 0 to 90 degrees:");

  // display sines. Note we must convert from degrees to radians.
  for (double theta=0 ; theta<90.1 ; theta+=22.5)
     AddItem("sin " + theta + " = " + Math.Sin(theta*Math.PI/180));

Note in this code that we need to convert degrees to radians by multiplying by (p/180) since all the math trigonometric functions work in radians.

This code produces this output:

The main mathematical functions include the following:

Note that these mathematical functions all take double as the parameter and return a double, apart from Abs, Max, Min and Sign, which returns 1, 0 or -1 - these functions make sense for any numeric type, for example it is equally valid to take the absolute value of an integer or a floating point value, and so Microsoft have provided overloaded functions that take different .NET numeric types.

Comments

  • Re: [1676] Namespaces and the Base Classes

    Posted by cdm on 06 Jun 2008

    Are you kidding? System.IO.File and System.IO.Directory are static classes. They can't be instantiated. Did any of the authors or editors bother running this code to see if it would work?

  • MSMQ TriggerEvents C#

    Posted by amaretto on 04 Jun 2003

    I'm struggeling with the implementation of simple asynchroneous receive in C# :
    aQueue.enableNotification(eventObject, ref cursor, ref timeout)

    cursor and timeout are values (int) that can be retr...

  • Mistakes?

    Posted by mitzvah on 13 Nov 2002

    After spend a whole afternoon testing code in this article( they didn't work), I found this in msdn:
    [b]All methods of the Directory class are static and can therefore be called without having an ins...