Base Data Types
  Sort an array
  Format a string
  Parse datatypes
  Compare references
  Compare object values
  Use mathematical functions

Get URL for this page

How Do I...Use mathematical functions?

This example demonstrates using the methods and constants built into the Math class. The methods in the Math class provide you with most of the standard functions you will want to use in daily computations, and are all provided as non-instantiated methods, perfect for quickly being able to perform the function you require. See the .NET Framework SDK for a full listing of all the functions you can access using the Math class. At the end of this page is an example allowing you to invoke all the methods on the Math class, which uses the Reflection namespace to call the methods dynamically.

 
VB Math.aspx

[Run Sample] | [View Source]

The Math class contains a number of standard functions for assisting you with mathematical operations. Because you don't want to actually have a 'Math' object lying around in memory, all the tools you are going to use through Math are provided as non-instantiated functionality. All the methods are intuitive and easy to use.

The following example demonstrates using the Max and Min functions. These functions allow you to determine which is the larger or smaller of two numbers, and returns the identified number back out of the called function.


Dim i As Integer = -10

Dim d1 As double = 39.25
Dim d2 As double = -27.84

Console.WriteLine( Math.Max(i, d1) )   ' returns 39.25
Console.WriteLine( Math.Max(d2, i) )   ' returns -10
Console.WriteLine( Math.Max(d1, d2) )  ' returns 39.25

Console.WriteLine( Math.Min(i, d1) )   ' returns -10
Console.WriteLine( Math.Min(d2, i) )   ' returns -27.84
Console.WriteLine( Math.Min(d1, d2) )  ' returns -27.84
VB

You may think the previous example goes a little overboard, since a demonstration of just two numbers would give you the idea. The purpose is to point out that the data types, and the order of those data types, is different in each example. In the first call to the Max function, an integer and a double are passed, in that order, and then you call the same function, with a double, and then an integer. The Math class provides many overloaded methods, so you can pass many different data types in different orders, and still get the functionality you require, as demonstrated above.

The Ceiling method rounds a number up to the first whole number larger than the number you pass, and the Floor method rounds a number down to the first whole number smaller. The Abs method returns the absolute value of the number you pass (defined as the non-negative distance from zero), and the Sqrt method returns the square root of a number. Note that rather than throwing an exception, if you pass the Sqrt method a negative number then the result is 'NaN', signifying that the result is 'Not a Number'.


'  refer to the previous sample for the variable declarations being used...

'  NB: Floor and Ceiling are fairly meaningless with integer values,
'  since by definition, they are already whole numbers!
Console.WriteLine( Math.Floor(i) )      ' returns -10
Console.WriteLine( Math.Floor(d1) )     ' returns 39
Console.WriteLine( Math.Floor(d2) )     ' returns -28

Console.WriteLine( Math.Ceiling(i) )    ' returns -10
Console.WriteLine( Math.Ceiling(d1)) )  ' returns 40
Console.WriteLine( Math.Ceiling(d2)) )  ' returns -27

Console.WriteLine( Math.Abs(i) )        ' returns 10
Console.WriteLine( Math.Abs(d1) )       ' returns 39.25
Console.WriteLine( Math.Abs(d2) )       ' returns 27.84

Console.WriteLine( Math.Sqrt(d1) )      ' returns 6.26
Console.WriteLine( Math.Sqrt(i) )       ' returns NaN
VB

Using the Math class is fairly straightforward. You can perform many other functions using other methods, including sin, cos, and tan functions, and logarithmic functions. There are two constant values defined in the Math class: PI and E. PI is the constant ratio between a circle and its diameter, and is provided for quick and fairly accurate computations where it is required. E is the natural exponent (generally written as a lowercase e) also used in many mathematical operations. The following code example demonstrates how to invoke each constant.


Console.WriteLine(" e equals {0}", Math.E )
Console.WriteLine(" PI equals {0}", Math.PI )
VB

This final example allows you to invoke all the static methods on the Math class. It uses members in the Reflection namespace to determine the methods available dynamically, and builds a list of all the methods you can select to invoke. For details on the code in this sample, see the How Do I samples listed under the Reflection topic.

 
VB MathRefl.aspx

[Run Sample] | [View Source]


Copyright 2001-2002 Microsoft Corporation. All rights reserved.