HOME  |    TRAINING  |   FREE TUTORIALS   |   JOBS
Find out more about our new RSS feed.
FREE Tutorial
C# PROGRAMMING: NAMESPACES AND THE BASE CLASSES PART 7 - MATHEMATICAL FUNCTIONS

CATEGORY
SEARCH OUR OTHER TUTORIALS

DESCRIPTION

Mathematical functions are included in the Math class, which is part of the System namespace. You don't therefore need to add any references to your project to make use of these functions.
Click here to be kept informed of our new Tutorials.


This free tutorial is a sample from the book C# Programming with the Public Beta.


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.

Summary

In this chapter we've explained the concept of a namespace, and then gone on a quick tour of some of the functionality available via the .NET SDK base classes. Hopefully these examples will have shown you that the base classes make it very easy to carry out a lot of Windows tasks. Some of these tasks were previously only available via the Win32 SDK, which was not only conceptually harder to program with, but also made it difficult to carry out those tasks from any language other than C++. With the new base classes these tasks can be accomplished with ease from any .NET-aware or COM-aware language.




5 RELATED COURSES AVAILABLE
C# PROGRAMMING
At the end of this course, you will have learned the fundamental skills that are required to design and develop o....
C++ PROGRAMMING
Object oriented programming is fast becoming the leading software design methodology, with C++ becoming ever more....
C PROGRAMMING
This course is design to provide non-C programmers with the essential skills and knowledge necessary to allow the....
C PROGRAMMING IN THE UNIX ENVIRONMENT
This course will provide readers the knowledge to use many of the UNIX 'C' library facilities, interface with the....
MICROSOFT VISUAL C++ 5 AND THE MFC&T 32 BIT WINDOWS PROGRAMMING
A complete 32 bit programming course highlighting the main features regarding the Microsoft Visual C++ programmin....
 
0 RELATED JOBS AVAILABLE
CONTACT US
Thursday 4th December 2008  © COPYRIGHT 2008 - VISUALSOFT