三角函式



acos() 函式

acos() 函式返回 x 的反餘弦值(以弧度表示)。

語法

以下是 acos() 函式的語法:-

acos(x)

注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式。

引數

  • x - 必須是 -1 到 1 之間的數值。如果 x 大於 1,則會生成“數學域錯誤”。

返回值

此方法返回 x 的反餘弦值(以弧度表示)。結果介於 0 和 pi 之間。

示例

以下示例演示了 acos() 方法的用法:-

from math import acos

x = 0.5
val = acos(x)
print ("x: ",x, "acos(x): ", val)

x = 0.0
val = acos(x)
print ("x: ",x, "acos(x): ", val)

x = -1
val = acos(x)
print ("x: ",x, "acos(x): ", val)

x = 1
val = acos(x)
print ("x: ",x, "acos(x): ", val)

執行以上程式時,會產生以下 輸出:-

x: 0.5 acos(x): 1.0471975511965979
x: 0.0 acos(x): 1.5707963267948966
x: -1 acos(x): 3.141592653589793
x: 1 acos(x): 0.0

asin() 函式

asin() 函式返回 x 的反正弦值(以弧度表示)。

語法

以下是 asin() 函式的語法:-

asin(x)

注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式。

引數

  • x - 必須是 -1 到 1 之間的數值。如果 x 大於 1,則會生成“數學域錯誤”。

返回值

此方法返回 x 的反正弦值(以弧度表示)。結果介於 -pi/2 和 pi/2 之間。

示例

以下示例演示了 asin() 方法的用法。

from math import asin

x = 0.5
val = asin(x)
print ("x: ",x, "asin(x): ", val)

x = 0.0
val = asin(x)
print ("x: ",x, "asin(x): ", val)

x = -1
val = asin(x)
print ("x: ",x, "asin(x): ", val)

x = 1
val = asin(x)
print ("x: ",x, "asin(x): ", val)

執行以上程式時,會產生以下 輸出:-

x: 0.5 asin(x): 0.5235987755982989
x: 0.0 asin(x): 0.0
x: -1 asin(x): -1.5707963267948966
x: 1 asin(x): 1.5707963267948966

atan() 函式

atan() 函式返回 x 的反正切值(以弧度表示)。

語法

以下是 atan() 函式的語法:-

atan(x)

注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式。

引數

  • x - 必須是數值。

返回值

此函式返回 x 的反正切值(以弧度表示)。結果介於 -pi/2 和 pi/2 之間。

示例

以下示例演示了 atan() 方法的用法:-

from math import atan

x = 0.5
val = atan(x)
print ("x: ",x, "atan(x): ", val)

x = 0.0
val = atan(x)
print ("x: ",x, "atan(x): ", val)

x = -1
val = atan(x)
print ("x: ",x, "atan(x): ", val)

x = 1
val = atan(x)
print ("x: ",x, "atan(x): ", val)

執行以上程式時,會產生以下 輸出:-

x: 0.5 atan(x): 0.4636476090008061
x: 0.0 atan(x): 0.0
x: -1 atan(x): -0.7853981633974483
x: 1 atan(x): 0.7853981633974483

atan2() 函式

atan2() 函式返回 atan(y / x)(以弧度表示)。例如,atan(1) 和 atan2(1, 1) 均為 pi/4,但 atan2(-1, -1) 為 -3*pi/4。

語法

以下是 atan2() 函式的語法:-

atan2(y, x)

注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式。

引數

  • y - 必須是以弧度表示的數值。

  • x - 必須是以弧度表示的數值。

返回值

此函式返回 atan(y / x)(以弧度表示)。結果介於 -pi 和 pi 之間。

示例

以下示例演示了 atan2() 方法的用法:-

from math import atan2

x,y = (-0.50,-0.50)
val = atan2(x,y)
print ("x: ",x, "y:",y, "atan2(x,y): ", val)

x,y = (0.50,0.50)
val = atan2(x,y)
print ("x: ",x, "y:",y, "atan2(x,y): ", val)

x,y= (5,5)
val = atan2(x,y)
print ("x: ",x, "y:",y, "atan2(x,y): ", val)

x,y = (-10,10)
val = atan2(x,y)
print ("x: ",x, "y:", y, "atan2(x,y): ", val)

x,y = (10,20)
val = atan2(x,y)
print ("x: ",x, "y:", y, "atan2(x,y): ", val)

執行以上程式時,會產生以下 輸出:-

x: -0.5 y: -0.5 atan2(x,y): -2.356194490192345
x: 0.5 y: 0.5 atan2(x,y): 0.7853981633974483
x: 5 y: 5 atan2(x,y): 0.7853981633974483
x: -10 y: 10 atan2(x,y): -0.7853981633974483
x: 10 y: 20 atan2(x,y): 0.4636476090008061

cos() 函式

cos() 函式返回 x 弧度的餘弦值。

語法

以下是 cos() 函式的語法:

cos(x)

注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式。

引數

  • x - 此值必須是弧度表示的數值。

返回值

此函式返回一個介於 -1 和 1 之間的數值,表示該角度的餘弦值。

示例

以下示例演示了 cos() 方法的使用:

from math import cos, pi

x = 3
val = cos(x)
print ("x: ",x, "cos(x): ", val)

x = -3
val = cos(x)
print ("x: ",x, "cos(x): ", val)

x = 0
val = cos(x)
print ("x: ",x, "cos(x): ", val)

x = pi
val = cos(x)
print ("x: ",x, "cos(x): ", val)

x = 2*pi
val = cos(x)
print ("x: ",x, "cos(x): ", val)

執行以上程式時,會產生以下 輸出:-

x: 3 cos(x): -0.9899924966004454
x: -3 cos(x): -0.9899924966004454
x: 0 cos(x): 1.0
x: 3.141592653589793 cos(x): -1.0
x: 6.283185307179586 cos(x): 1.0

dist() 函式

此函式返回兩個點 p 和 q 之間的歐幾里得距離,每個點都以座標序列(或可迭代物件)的形式給出。這兩個點必須具有相同的維度。平面中座標為 (x, y) 和 (a, b) 的兩點之間的歐幾里得距離由 $\mathrm{dist \: ((x,y),(a,b)) \: = \: \sqrt{(x − a)^2 + (y − b)^2}}$ 給出。

語法

math.dist(p, q)

引數

  • pq - 具有兩個數值運算元的可迭代物件。

返回值

此函式返回兩點之間的歐幾里得距離。

示例

from math import dist
p = [3,5]
q = [6,9]
val = dist(p,q)
print ("p: ",p, "q:", q, "dist(p,q): ", val)

p = [0,0]
q = [3,3]
val = dist(p,q)
print ("p: ",p, "q:", q, "dist(p,q): ", val)

它將產生以下輸出

p: [3, 5] q: [6, 9] dist(p,q): 5.0
p: [0, 0] q: [3, 3] dist(p,q): 4.242640687119285

hypot() 函式

hypot() 函式返回歐幾里得範數,即 sqrt(x*x + y*y)。這是從原點到點 (x,y) 的向量的長度。

語法

以下是 hypot() 函式的語法:

hypot(x, y)

注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後才能使用 math 靜態物件呼叫此函式。

引數

  • x - 必須是數值。

  • y - 此值必須是數值。

返回值

此函式返回歐幾里得範數,即 sqrt(x*x + y*y)。

示例

以下示例演示了 hypot() 函式的使用:

from math import hypot

x =3
y =2
val = hypot(x,y)
print ("x: ",x, "y:", y, "hypot(x,y): ", val)

x = -3
y = 3
val = hypot(x,y)
print ("x: ",x, "y:", y, "hypot(x,y): ", val)

x =0
y =2
val = hypot(x,y)
print ("x: ",x, "y:", y, "hypot(x,y): ", val)

執行以上程式時,會產生以下 輸出:-

x: 3 y: 2 hypot(x,y): 3.605551275463989
x: -3 y: 3 hypot(x,y): 4.242640687119285
x: 0 y: 2 hypot(x,y): 2.0

sin() 函式

sin() 函式返回 x(以弧度表示)的正弦值。

語法

以下是 sin() 函式的語法:

math.sin(x)

注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後使用 math 靜態物件呼叫此函式。

引數

  • x - 必須是數值。

返回值

此函式返回一個介於 -1 和 1 之間的數值,表示引數 x 的正弦值。

示例

以下示例演示了 sin() 方法的使用:

from math import sin, pi

x = 3
val = sin(x)
print ("x: ",x, "sin(x): ", val)

x = −3
val = sin(x)
print ("x: ",x, "sin(x): ", val)

x = 0
val = sin(x)
print ("x: ",x, "sin(x): ", val)

x = pi
val = sin(x)
print ("x: ",x, "sin(x): ", val)

x = pi/2
val = sin(x)
print ("x: ",x, "sin(x): ", val)

執行以上程式時,將產生以下輸出:

x: 3 sin(x): 0.1411200080598672
x: -3 sin(x): -0.1411200080598672
x: 0 sin(x): 0.0
x: 3.141592653589793 sin(x): 1.2246467991473532e-16
x: 1.5707963267948966 sin(x): 1.0

tan() 函式

tan() 函式返回 x 弧度的正切值。

語法

以下是 tan() 函式的語法:

tan(x)

注意 - 此函式無法直接訪問,因此我們需要匯入 math 模組,然後才能使用 math 靜態物件呼叫此函式。

引數

  • x - 必須是數值。

返回值

此函式返回一個介於 -1 和 1 之間的數值,表示引數 x 的正切值。

示例

以下示例演示了 tan() 函式的使用:

from math import tan, pi

x = 3
val = tan(x)
print ("x: ",x, "tan(x): ", val)

x = -3
val = tan(x)
print ("x: ",x, "tan(x): ", val)

x = 0
val = tan(x)
print ("x: ",x, "tan(x): ", val)

x = pi
val = tan(x)
print ("x: ",x, "tan(x): ", val)

x = pi/2
val = tan(x)
print ("x: ",x, "tan(x): ", val)

執行以上程式時,會產生以下 輸出:-

x: 3 tan(x): -0.1425465430742778
x: -3 tan(x): 0.1425465430742778
x: 0 tan(x): 0.0
x: 3.141592653589793 tan(x): -1.2246467991473532e-16
x: 1.5707963267948966 tan(x): 1.633123935319537e+16
python_maths.htm
廣告