共用方式為


數學函式 (Visual Basic)

更新:2007 年 11 月

Visual Basic 6 中的數學函式在 .NET Framework 中已由 System.Math 類別 (Class) 中的對等方法所取代。

備註

.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 值,其中包含正切函數 (Tangent) 是指定數字的角度。

Cos

Cos

傳回 Double 值,其中包含指定角度的餘弦函數 (Cosine)。

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 數學類別也提供三角、對數及其他常見數學函式的常數和其他靜態 (Static) 方法。這些都可在 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 方法來傳回角度的餘弦函數 (Cosine)。

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 方法來傳回角度的正弦函數 (Sine)。

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 方法來傳回角度的正切函數 (Tangent)。

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