10 out of 15 rated this helpful - Rate this topic

# Math Functions (Visual Basic)

Visual Studio 2008

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

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

.NET Framework method

Description

Abs

Returns the absolute value of a specified number.

Atn

Returns a Double value containing the angle whose tangent is the specified number.

Cos

Returns a Double value containing the cosine of the specified angle.

Exp

Returns a Double value containing e (the base of natural logarithms) raised to the specified power.

Log

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

Returns a Double value containing the number nearest the specified value. Additional round functions are available as methods of the intrinsic types such as Round.

Sgn

Returns an Integer value indicating the sign of a number.

Sin

Returns a Double value specifying the sine of an angle.

Sqr

Returns a Double value specifying the square root of a number.

Tan

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
```

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

```' Returns 50.3.
Dim MyNumber1 As Double = Math.Abs(50.3)
' Returns 50.3.
Dim MyNumber2 As Double = Math.Abs(-50.3)
```

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

```Public Function GetPi() As Double
' Calculate the value of pi.
Return 4.0 * Math.Atan(1.0)
End Function
```

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

```Public Function Sec(ByVal angle As Double) As Double
' Calculate the secant of angle, in radians.
Return 1.0 / Math.Cos(angle)
End Function
```

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

```Public Function Sinh(ByVal angle As Double) As Double
' Calculate hyperbolic sine of an angle, in radians.
Return (Math.Exp(angle) - Math.Exp(-angle)) / 2.0
End Function
```

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

```Public Function Asinh(ByVal value As Double) As Double
' Calculate inverse hyperbolic sine, in radians.
Return Math.Log(value + Math.Sqrt(value * value + 1.0))
End Function
```

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

```' Returns 3.
Dim MyVar2 As Double = Math.Round(2.8)
```

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

```' Returns 1.
Dim MySign1 As Integer = Math.Sign(12)
' Returns -1.
Dim MySign2 As Integer = Math.Sign(-2.4)
' Returns 0.
Dim MySign3 As Integer = Math.Sign(0)
```

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

```Public Function Csc(ByVal angle As Double) As Double
' Calculate cosecant of an angle, in radians.
Return 1.0 / Math.Sin(angle)
End Function
```

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

```' Returns 2.
Dim MySqr1 As Double = Math.Sqrt(4)
' Returns 4.79583152331272.
Dim MySqr2 As Double = Math.Sqrt(23)
' Returns 0.
Dim MySqr3 As Double = Math.Sqrt(0)
' Returns NaN (not a number).
Dim MySqr4 As Double = Math.Sqrt(-4)
```

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

```Public Function Ctan(ByVal angle As Double) As Double
' Calculate cotangent of an angle, in radians.
Return 1.0 / Math.Tan(angle)
End Function
```

Class: Math

Namespace: System

Assembly: mscorlib (in mscorlib.dll)