Export (0) Print
Expand All

Math Functions

The math functions in Visual Basic 6 have been replaced by equivalent methods in the System.Math class of the .NET Framework.

Remarks

The .NET Framework math methods are functionally identical to their Visual Basic 6 counterparts, although some have slightly different names. For example, the .NET Framework equivalent of the Visual Basic 6 Atn function is Atan. The following table lists the Visual Basic 6 math function names and the equivalent .NET Framework methods.

Visual Basic 6 function Visual Basic .NET method Description
Abs Math.Abs Method Returns the absolute value of a specified number.
Atn Math.Atan Method Returns a Double value containing the angle whose tangent is the specified number.
Cos Math.Cos Method Returns a Double value containing the cosine of the specified angle.
Exp Math.Exp Method Returns a Double value containing e (the base of natural logarithms) raised to the specified power.
Log Math.Log Method Returns a Double value containing the logarithm of a specified number. This method is overloaded and can return either the natural (base e) logarithm of a specified number or the logarithm of a specified number in a specified base.
Round Math.Round Method Returns a Double value containing the number nearest the specified value. Additional round functions are available as methods of the intrinsic types such as Decimal.Round Method.
Sgn Math.Sign Method Returns an Integer value indicating the sign of a number.
Sin Math.Sin Method Returns a Double value specifying the sine of an angle.
Sqr Math.Sqrt Method Returns a Double value specifying the square root of a number.
Tan Math.Tan Method Returns a Double value containing the tangent of an angle.

In addition, the .NET Framework math class provides constants and other static methods for trigonometric, logarithmic, and other common mathematical functions. All of these can be used in a Visual Basic program.

To use these functions without qualification, import the System.Math namespace into your project by adding the following code to the top of the source code:

Imports System.Math

Requirements

Class: Math Class

Abs Example

This example uses the Abs method of the Math class to compute the absolute value of a number.

Imports System.Math
' Code will not compile unless placed within a Sub or Function
Dim MyNumber As Double
MyNumber = Abs(50.3)    ' Returns 50.3.
MyNumber = Abs(-50.3)   ' Returns 50.3.

Atan Example

This example uses the Atan method of the Math class to calculate the value of pi.

Imports System.Math
' Code will not compile unless placed within a Sub or Function
Dim pi As Double
pi = 4 * Atan(1)   ' Calculate the value of pi.

Cos Example

This example uses the Cos method of the Math class to return the cosine of an angle.

Imports System.Math
' Code will not compile unless placed within a Sub or Function
Dim MyAngle, MySecant As Double
MyAngle = 1.3   ' Define angle in radians.
MySecant = 1 / Cos(MyAngle)   ' Calculate secant.

Exp Example

This example uses the Exp method of the Math class to return e raised to a power.

Imports System.Math
' Code will not compile unless placed within a Sub or Function
Dim MyAngle, MyHSin As Double
' Define angle in radians.
MyAngle = 1.3   
' Calculate hyperbolic sine.
MyHSin = (Exp(MyAngle) - Exp(-1 * MyAngle)) / 2

Log Example

This example uses the Log method of the Math class to return the natural logarithm of a number.

Imports System.Math
' Code will not compile unless placed within a Sub or Function
Dim MyAngle, MyLog As Double
' Define angle in radians.
MyAngle = 1.3
' Calculate inverse hyperbolic sine.
MyLog = Log(MyAngle + Sqrt(MyAngle * MyAngle + 1))

Round Example

This example uses the Round method of the Math class to round a number to the nearest integer.

Imports System.Math
' Code will not compile unless placed within a Sub or Function
Dim MyVar1 As Double = 2.8
Dim MyVar2 As Double
MyVar2 =Round(MyVar1)   ' Returns 3.

Sign Example

This example uses the Sign method of the Math class to determine the sign of a number.

Imports System.Math
' Code will not compile unless placed within a Sub or Function
Dim MyVar1, MyVar2, MyVar3 As Double
Dim MySign As Integer
MyVar1 = 12
MyVar2 = -2.4
MyVar3 = 0
MySign = Sign(MyVar1)   ' Returns 1.
MySign = Sign(MyVar2)   ' Returns -1.
MySign = Sign(MyVar3)   ' Returns 0.

Sin Example

This example uses the Sin method of the Math class to return the sine of an angle.

Imports System.Math
' Code will not compile unless placed within a Sub or Function
Dim MyAngle, MyCosecant As Double
MyAngle = 1.3   ' Define angle in radians.
MyCosecant = 1 / Sin(MyAngle)   ' Calculate cosecant.

Sqrt Example

This example uses the Sqrt method of the Math class to calculate the square root of a number.

Imports System.Math
' Code will not compile unless placed within a Sub or Function
Dim MySqr As Double
MySqr = Sqrt(4)    ' Returns 2.
MySqr = Sqrt(23)   ' Returns 4.79583152331272.
MySqr = Sqrt(0)    ' Returns 0.
MySqr = Sqrt(-4)   ' Returns NaN (not a number).

Tan Example

This example uses the Tan method of the Math class to return the tangent of an angle.

Imports System.Math
' Code will not compile unless placed within a Sub or Function
Dim MyAngle, MyCotangent As Double
MyAngle = 1.3   ' Define angle in radians.
MyCotangent = 1 / Tan(MyAngle)   ' Calculate cotangent.

Requirements

Namespace: System.Math

Assembly: mscorlib (in mscorlib.dll)

See Also

Rnd Function | Randomize Statement | Derived Math Functions | Double.NaN Field

Show:
© 2014 Microsoft