数学函数 (Visual Basic)

更新:2007 年 11 月

Visual Basic 6 中的数学函数已经由 .NET Framework 的 System.Math 类中的等效方法取代。

备注

.NET Framework 数学方法在功能上与它们在 Visual Basic 6 中的对应方法相同,但是其中一些的名称稍有不同。例如,Visual Basic 6 Atn 函数的 .NET Framework 等效项是 Atan。下表列出了 Visual Basic 6 数学函数名和等效的 .NET Framework 方法。

Visual Basic 6 函数

.NET Framework 方法

说明

Abs

Abs

返回指定数字的绝对值。

Atn

Atan

返回 Double 值,此值包含其正切为指定数字的角度。

Cos

Cos

返回 Double 值,此值包含指定角度的余弦。

Exp

Exp

返回 Double 值,此值包含升到指定幂的 e(自然对数的底)。

Log

Log

返回 Double 值,此值包含指定数字的对数。此方法是重载方法,可以返回指定数字的自然(底为 e)对数,也可返回指定底情况下的指定数字的对数。

Round

Round

返回 Double 值,此值包含最接近指定值的数字。其他四舍五入函数可用作内部类型的方法,例如 Round

Sgn

Sign

返回表示数字符号的 Integer 值。

Sin

Sin

返回一个 Double 值,指定角度的正弦。

Sqr

Sqrt

返回一个 Double 值,指定数字的平方根。

Tan

Tan

返回一个 Double 值,此值包含角度的正切。

此外,.NET Framework 数学类为三角函数、对数和其他常见的数学函数提供常数和其他静态方法。所有这些都可以用在 Visual Basic 程序中。

若要不受限制地使用这些函数,请将 System.Math 命名空间导入项目,方法为将以下代码添加到源代码顶端:

'Imports System.Math

示例

本示例使用 Math 类的 Abs 方法来计算一个数字的绝对值。

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

本示例使用 Math 类的 Atan 方法计算 pi 的值。

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

本示例使用 Math 类的 Cos 方法返回角度的余弦值。

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

本示例使用 Math 类的 Exp 方法返回以 e 为底的幂。

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

本示例使用 Math 类的 Log 方法返回数字的自然对数。

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

本示例使用 Math 类的 Round 方法将数字舍入为最接近的整数。

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

本示例使用 Math 类的 Sign 方法确定数字的符号。

' 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)

本示例使用 Math 类的 Sin 方法返回角度的正弦值。

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

本示例使用 Math 类的 Sqrt 方法计算数字的平方根。

' 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)

本示例使用 Math 类的 Tan 方法返回角度的正切值。

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

要求

类:Math

命名空间:System

**程序集:**mscorlib(位于 mscorlib.dll 中)

请参见

参考

Rnd 函数 (Visual Basic)

Randomize 函数 (Visual Basic)

导出的数学函数 (Visual Basic)

NaN