Namespaces and the Base Classes

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.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Better train people and risk they leave – than do nothing and risk they stay.” - Anonymous