數學#

介紹#

數學模組包含內建的 Python 函數和完整的 math 模組,後者在 VEXcode AIM 中自動啟用。這些工具使您能夠執行從基本算術到高級三角函數、四捨五入和對數運算的各種操作。

使用這些函數和常數可以計算機器人的位置、角度、距離和其他數值。您還可以進行角度和弧度之間的轉換、計算表達式以及處理特殊值,例如無限大和 NaN。

以下是可用的數學函數、常數和實用程式清單:

內建函數 – Python 中包含的常用數學工具。

  • abs – 傳回一個數的絕對值。

  • round – 將數字四捨五入到指定的小數位數。

  • min – 傳回輸入值中的最小值。

  • max – 傳回輸入值中的最大值。

  • sum – 將可迭代物件中的所有值相加。

  • divmod – 傳回商數與餘數為元組。

  • pow – 將一個數提升到冪,可以選擇帶模數。

  • int – 將數值轉換為整數。

  • float – 將數值轉換為浮點數。

常數 – 來自 math 模組的預定義值。

三角學——計算角度和邊之間的關係。

雙曲函數——高階三角函數。

舍入與絕對值 – 調整精確度或方向。

指數與對數——冪、根和對數計算。

浮點運算-檢查或分解浮點數值。

  • math.modf – 傳回浮點數的分數部分和整數部分。

  • math.frexp – 將一個數分解為尾數和指數。

  • math.fmod – 餘數,符號與被除數相同。

  • math.copysign – 傳回一個值,其符號與另一個值的符號相同。

  • math.ldexp – 計算 x * (2 ** exp)。

比較與近似-檢查帶有公差或類別的值。

誤差與伽瑪——特殊的數學函數。

內建功能#

Python 提供了幾個內建函數,讓你在專案中執行數學運算。

abs#

abs 回傳一個數的絕對值,去掉任何負號。

用法:
abs(x)

範圍

描述

x

整數或浮點數。

# Get the absolute value of -10
abs_result = abs(-10)
robot.screen.print(abs_result)

# abs_result = 10

round#

round 將數字四捨五入到指定的小數位數。

用法:
round(x, ndigits)

範圍

描述

x

整數或浮點數。

ndigits

可選。四捨五入到小數點後的位數。預設值為 0。

# Round 5.7 to the nearest integer
round_int_result = round(5.7)
robot.screen.print(round_int_result)

# round_int_result = 6

# Round 3.14159 to 2 decimal places
round_result = round(3.14159, 2)
robot.screen.print(round_result)

# round_result = 3.14

min#

min 傳回多個參數或可迭代物件中的最小值。

用法:
min(arg1, arg2, …) or min(sequence)

範圍

描述

arg1, arg2,.……

需要比較的數據。

sequence

包含數字的清單、元組或其他序列。

# Get the smallest number from 3, 7, and 1
min_result = min(3, 7, 1)
robot.screen.print(min_result)

# min_result = 1

# Get the smallest value from a list
min_list_result = min([10, 4, 25, 1])
robot.screen.print(min_list_result)

# min_list_result = 1

max#

max 傳回多個參數或可迭代物件中的最大值。

用法:
max(arg1, arg2, …) 或 max(sequence)

範圍

描述

arg1, arg2,.……

需要比較的數據。

sequence

包含數字的清單、元組或其他序列。

# Get the largest number from 3, 7, and 1
max_result = max(3, 7, 1)
robot.screen.print(max_result)

# max_result = 7

# Get the largest value from a list
max_list_result = max([10, 4, 25, 1])
robot.screen.print(max_list_result)

# max_list_result = 25

sum#

sum 將可迭代物件中的所有值相加,可以指定一個起始值。

用法:
sum(sequence, start)

範圍

描述

sequence

包含數字的清單、元組或其他序列。

start

可選。要加到總和中的值。預設值為 0。

# Calculate the sum of a list of numbers
sum_result = sum([1, 2, 3, 4, 5])
robot.screen.print(sum_result)

# sum_result = 15

# Calculate the sum of a list with a starting value of 10
sum_with_start = sum([1, 2, 3], 10)
robot.screen.print(sum_with_start)

# sum_with_start = 16

divmod#

divmod 傳回一個包含除法運算的商數和餘數的元組。

用法:
divmod(a, b)

範圍

描述

a

分紅。

b

除數。

# Perform integer division and remainder of 10 / 3
divmod_result = divmod(10, 3)
robot.screen.print(divmod_result)

# divmod_result = (3, 1)

pow#

pow 將一個數提升到一個冪,並可選擇執行取模運算。

用法:
pow(x, y, mod)

範圍

描述

x

基數。

y

指數。

mod

可選。模值。如果提供,則回傳 (x ** y) % mod。

# Calculate 2 raised to the power of 3
pow_result = pow(2, 3)
robot.screen.print(pow_result)

# pow_result = 8

# Calculate (5 ** 3) % 7
pow_mod_result = pow(5, 3, 7)
robot.screen.print(pow_mod_result)

# pow_mod_result = 6

int#

int 將數字或字串轉換為整數。它還支援從字串轉換時進行進制轉換。

用法:
int(x, base)

範圍

描述

x

要轉換的數字、字串或其他物件。

base

可選。用於轉換的數字基數。預設值為 10。

# Convert a float to an integer to get rid of decimals
price = 19.99
price_int = int(price)
robot.screen.print(f"{price_int} coins")

# Output: 19 coins

# Convert a string into an integer to use in calculations
user_input = "55"
user_number = int(user_input)
robot.screen.print(user_number * 2)

# Output: 110

float#

float 將數字或字串轉換為浮點數。

用法:
float(x)

範圍

描述

x

要轉換的數字、字串或其他物件。

# Convert division result to a float
num_apples = 6
num_people = 2
apples_per_person = float(num_apples) / num_people
robot.screen.print(apples_per_person)

# Output: 3.00

# Convert a string into a float to use in calculations
user_input = "23.4"
user_number = float(user_input)
robot.screen.print(user_number * 3)

# Output: 70.20

數學模組#

MicroPython 中的 math 模組提供了執行常見數學計算的附加方法。這些方法包括三角函數運算、對數運算和其他數值運算。

VEXcode 預設導入 math 模組。

Constants#

常量是預先定義好的數值,在專案過程中保持不變。它們可以直接用於計算,無需任何定義或賦值。

π#

pi 給出了數學常數 π,即圓的周長與其直徑之比。

用法:
math.pi

# Calculate the area of a circle with radius 5 using pi
circle_area = math.pi * 5 * 5
robot.screen.print(circle_area)

# circle_area = 78.54

τ#

tau 給出的值為 2π。

用法:
math.tau

# Calculate the circumference of a circle with radius
circumference = math.tau * 5
robot.screen.print(circumference)

# circumference = 31.42

e#

e 表示自然對數的底數。

用法:
math.e

# Calculate e raised to the power of 2
e_power = math.pow(math.e, 2)
robot.screen.print(e_power)

# e_power = 7.39

資訊#

inf 將正無窮大表示為浮點數。

用法:
math.inf

# Check if infinity is infinite
inf_value = math.inf

if math.isinf(inf_value):
    robot.screen.print("Infinity")

# Prints "Infinity"

楠#

nan 表示「非數字」(NaN)的特殊浮點數。

用法:
math.nan

# Check if nan is Not a Number
nan_value = math.nan

if math.isnan(nan_value):
    robot.screen.print("Not a Number")

# Prints "Not a Number"

Trigonometry#

罪#

sin 計算弧度角的正弦值並傳回浮點數。

用法:
math.sin(x)

範圍

描述

x

表示弧度角度的浮點數或整數。

# Calculate the sine of 30 degrees in radians
sine_result = math.sin(math.radians(30))
robot.screen.print(sine_result)

# sine_result = 0.50

化學#

cos 計算弧度角的餘弦值並傳回浮點數。

用法:
math.cos(x)

範圍

描述

x

表示弧度角度的浮點數或整數。

# Calculate the cosine of 60 degrees in radians
cosine_result = math.cos(math.radians(60))
robot.screen.print(cosine_result)

# cosine_result = 0.50

棕褐色#

tan 計算弧度角的正切值並傳回浮點數。

用法:
math.tan(x)

範圍

描述

x

表示弧度角度的浮點數或整數。

# Calculate the tangent of 45 degrees in radians
tangent_result = math.tan(math.radians(45))
robot.screen.print(tangent_result)

# tangent_result = 1.00

阿坦#

atan 計算一個數的反正切值(反正切值),並傳回一個表示弧度角度的浮點數。

用法:
math.atan(x)

範圍

描述

x

浮點數或整數。

# Calculate the arc tangent of 1 in degrees
arc_tangent_result = math.degrees(math.atan(1.0))
robot.screen.print(arc_tangent_result)

# arc_tangent_result = 45.00

atan2#

atan2 計算 y/x 的反正切函數的主值,並傳回一個表示弧度角度的浮點數。

用法:
math.atan2(y, x)

範圍

描述

y

表示 y 座標的浮點數或整數。

x

表示 x 座標的浮點數或整數。

# Calculate the inverse tangent of 1/1 in degrees
atan2_result = math.degrees(math.atan2(1.0, 1.0))
robot.screen.print(atan2_result)

# atan2_result = 45.00

亞辛#

asin 計算一個數的反正弦(反正弦)並傳回一個表示弧度角度的浮點數。

用法:
math.asin(x)

範圍

描述

x

介於 -1 和 1 之間的浮點數或整數。

# Calculate the arc sine of 0.5 in degrees
arc_sine_result = math.degrees(math.asin(0.5))
robot.screen.print(arc_sine_result)

# arc_sine_result = 30.00

acos#

acos 計算一個數的反餘弦(反餘弦)並傳回一個表示弧度角度的浮點數。

用法:
math.acos(x)

範圍

描述

x

介於 -1 和 1 之間的浮點數或整數。

# Calculate the arc cosine of 0.5 in degrees
arc_cosine_result = math.degrees(math.acos(0.5))
robot.screen.print(arc_cosine_result)

# arc_cosine_result = 60.00

學位#

degrees 將角度從弧度轉換為度。

用法:
math.degrees(x)

範圍

描述

x

表示弧度角度的浮點數或整數。

# Convert pi radians to degrees
degrees_result = math.degrees(math.pi)
robot.screen.print(degrees_result)

# degrees_result = 180.00

弧度#

radians 將角度從度數轉換為弧度。

用法:
math.radians(x)

範圍

描述

x

表示角度的浮點數或整數,單位為度數。

# Convert 180 degrees to radians
radians_result = math.radians(180)
robot.screen.print(radians_result)

# radians_result = 3.14

Hyperbolics#

辛赫#

sinh 計算 x的雙曲正弦值。

用法:
math.sinh(x)

範圍

描述

x

表示輸入數值的浮點數或整數。

# Calculate the hyperbolic sine of 1
sinh_result = math.sinh(1)
robot.screen.print(sinh_result)

# sinh_result = 1.18

棍棒#

cosh 計算 x的雙曲餘弦值。

用法:
math.cosh(x)

範圍

描述

x

表示輸入數值的浮點數或整數。

# Calculate the hyperbolic cosine of 1
cosh_result = math.cosh(1)
robot.screen.print(cosh_result)

# cosh_result = 1.54

tanh#

tanh 計算 x的雙曲正切值。

用法:
math.tanh(x)

範圍

描述

x

表示輸入數值的浮點數或整數。

# Calculate the hyperbolic tangent of 1
tanh_result = math.tanh(1)
robot.screen.print(tanh_result)

# tanh_result = 0.76

阿辛#

asinh 計算 x的反雙曲正弦。

用法:
math.asinh(x)

範圍

描述

x

表示輸入數值的浮點數或整數。

# Calculate the inverse hyperbolic sine of 1
asinh_result = math.asinh(1)
robot.screen.print(asinh_result)

# asinh_result = 0.88

艾科甚#

acosh 計算 x的反雙曲餘弦。

用法:
math.acosh(x)

範圍

描述

x

大於或等於 1 的浮點數或整數。

# Calculate the inverse hyperbolic cosine of 2
acosh_result = math.acosh(2)
robot.screen.print(acosh_result)

# acosh_result = 1.32

阿坦#

atanh 計算 x的反雙曲正切值。

用法:
math.atanh(x)

範圍

描述

x

介於 -1 和 1 之間的浮點數(不含 1)。

# Calculate the inverse hyperbolic tangent of 0.5
atanh_result = math.atanh(0.5)
robot.screen.print(atanh_result)

# atanh_result = 0.55

Rounding & Absolute Values#

天花板#

ceil 將數字向上取整到最接近的整數。

用法:
math.ceil(x)

範圍

描述

x

要向上取整的浮點數或整數。

# Round 3.7 up to the nearest integer
ceil_result = math.ceil(3.7)
robot.screen.print(ceil_result)

# ceil_result = 4

地面#

floor 將數字向下捨去到最接近的整數。

用法:
math.floor(x)

範圍

描述

x

要向下取整的浮點數或整數。

# Round 3.7 down to the nearest integer
floor_result = math.floor(3.7)
robot.screen.print(floor_result)

# floor_result = 3

截斷#

trunc 刪除數字的小數部分而不進行四捨五入。

用法:
math.trunc(x)

範圍

描述

x

待截斷的浮點數。

# Remove the decimal part of 3.7
trunc_result = math.trunc(3.7)
robot.screen.print(trunc_result)

# trunc_result = 3

工廠#

fabs 傳回數字的絕對值(浮點數)。

用法:
math.fabs(x)

範圍

描述

x

浮點數或整數。

# Get the absolute value of -3.7
fabs_result = math.fabs(-3.7)
robot.screen.print(fabs_result)

# fabs_result = 3.70

Exponents & Logarithms#

火力#

pow 將 x 提升到 y 次方並傳回一個浮點數,即使兩個輸入都是整數。

用法:
math.pow(x, y)

範圍

描述

x

浮點數或整數作為基數。

y

浮點數或整數指數。

# Calculate 2 raised to the power of 3
power_result = math.pow(2, 3)
robot.screen.print(power_result)

# power_result = 8.00

平方根#

sqrt 計算一個數的平方根並傳回一個浮點數,即使是完全平方數也是如此。

用法:
math.sqrt(x)

範圍

描述

x

非負浮點數或整數。

# Calculate the square root of 16
sqrt_result = math.sqrt(16)
robot.screen.print(sqrt_result)

# sqrt_result = 4.00

經驗#

exp 計算一個數的指數並傳回一個浮點數。

用法:
math.exp(x)

範圍

描述

x

浮點數或整數。

# Calculate e raised to the power of 1
exp_result = math.exp(1)
robot.screen.print(exp_result)

# exp_result = 2.72

紀錄#

log 計算一個數的自然對數,並傳回一個浮點數。

用法:
math.log(x)

範圍

描述

x

一個正浮點數或整數。

# Calculate the natural logarithm (base e) of 7.389056
log_result = math.log(7.389056)
robot.screen.print(log_result)

# log_result = 2.00

10 行#

log10 計算一個數的以 10 為底的對數,並傳回一個浮點數。

用法:
math.log10(x)

範圍

描述

x

一個正浮點數或整數。

# Calculate the base-10 logarithm of 1000
log10_result = math.log10(1000)
robot.screen.print(log10_result)

# log10_result = 3.00

2#

log2 計算一個數的以 2 為底的對數並傳回一個浮點數,即使 x 是 2 的冪。

用法:
math.log2(x)

範圍

描述

x

一個正浮點數或整數。

# Calculate the base-2 logarithm of 8
log2_result = math.log2(8)
robot.screen.print(log2_result)

# log2_result = 3.00

階乘#

factorial 傳回整數 x的階乘,它是所有小於等於 x正整數的乘積。

用法:
math.factorial(x)

範圍

描述

x

一個非負整數。

# Calculate 5 factorial (5!)
factorial_result = math.factorial(5)
robot.screen.print(factorial_result)

# factorial_result = 120

實驗1#

expm1 計算 ex - 1,對於較小的 x來說更準確。

用法:
math.expm1(x)

範圍

描述

x

指數值。

# Compute expm1(1) (e^1 - 1)
expm1_result = math.expm1(1)
robot.screen.print(expm1_result)

# expm1_result = 1.72

Floating Point Operations#

modf#

modf 將一個數分解成它的小數部分和整數部分,並傳回一個元組 (fractional part, integer part),兩者都是浮點數。

用法:
math.modf(x)

範圍

描述

x

要分解的浮點數或整數。

# Decompose 3.14159 into fractional and integer parts
fractional_part, integer_part = math.modf(3.14159)
robot.screen.print(fractional_part)
robot.screen.next_row()
robot.screen.print(integer_part)

# fractional_part = 0.14
# integer_part = 3.00

frexp#

frexp 將一個數分解成它的尾數和指數,並傳回一個元組 (mantissa, exponent),其中尾數是一個浮點數,指數是一個整數。

用法:
math.frexp(x)

範圍

描述

x

要分解的浮點數或整數。

# Decompose 16 into its mantissa and exponent
mantissa, exponent = math.frexp(16)
robot.screen.print(mantissa)
robot.screen.next_row()
robot.screen.print(exponent)

# mantissa = 0.50
# exponent = 5

fmod#

fmod 傳回除法的餘數,同時保留被除數的符號(x)。

用法:
math.fmod(x, y)

範圍

描述

x

分紅。

y

除數。

# Calculate remainder of 10 / 3
# that preserves the sign of 10
fmod_result = math.fmod(10, 3)
robot.screen.print(fmod_result)

# fmod_result = 1.00

簽名#

copysign 傳回帶有 y的 x。

用法:
math.copysign(x, y)

範圍

描述

x

要修改的值。

y

要複製其符號的值。

# Return -10 with the sign of 3 (positive)
copysign_result = math.copysign(-10, 3)
robot.screen.print(copysign_result)

# copysign_result = 10.00

ldexp#

ldexp 計算 x * (2 ** exp),這等價於 x * 2exp{code}。

用法:
math.ldexp(x, exp)

範圍

描述

x

基準值。

exp

指數。

# Compute 3 * (2 ** 4)
ldexp_result = math.ldexp(3, 4)
robot.screen.print(ldexp_result)

# ldexp_result = 48.00

Comparison & Approximation#

接近#

isclose 檢查兩個數字是否在容差範圍內近似相等。

用法:
math.isclose(a, b, rel_tol, abs_tol)

範圍

描述

a

首先要比較的數字。

b

第二個要比較的數字。

rel_tol

可選。 a 和 b 之間允許的最大差異,相對於它們的大小而言。預設值為 1e-09(非常小)。

abs_tol

可選。固定的誤差範圍,用於比較接近零的數字。預設值為 0.0。

注意:如果同時提供了 rel_tol 和 abs_tol ,則以先滿足的條件為準。

# Check if 1.000000001 and 1.0 are close
# within the default tolerance
isclose_result = math.isclose(1.000000001, 1.0)
robot.screen.print(isclose_result)

# isclose_result = True

# Check if 0.0000001 and 0.0 are close
# using absolute tolerance
isclose_result = math.isclose(0.0000001, 0.0, abs_tol=1e-07)
robot.screen.print(isclose_result)

# isclose_result = True

# Check if 1000000.0 and 1000000.1 are close
# with a stricter tolerance
isclose_result = math.isclose(1000000.0, 1000000.1, rel_tol=1e-10)
robot.screen.print(isclose_result)

# isclose_result = False

有限的#

isfinite 檢查一個數字是否為有限數。此方法傳回布林值:

  • True – 該數字是有限的。

  • False – 這個數字是無限的。

用法:
math.isfinite(x)

範圍

描述

x

要檢查的浮點數或整數。

# Check if 42 is a finite number (returns True)
is_finite_true = math.isfinite(42)
robot.screen.print(is_finite_true)

# is_finite_true = True

# Check if infinity is a finite number (returns False)
is_finite_false = math.isfinite(math.inf)
robot.screen.print(is_finite_false)

# is_finite_false = False

isinf#

isinf 檢查一個數是否為無限大。此方法傳回布林值:

  • True – 該數字是無限的。

  • False – 該數字是有限的。

用法:
math.isinf(x)

範圍

描述

x

要檢查的浮點數或整數。

# Check if infinity is an infinite number (returns True)
is_inf_true = math.isinf(math.inf)
robot.screen.print(is_inf_true)

# is_inf_true = True

# Check if 42 is an infinite number (returns False)
is_inf_false = math.isinf(42)
robot.screen.print(is_inf_false)

# is_inf_false = False

伊斯南#

isnan 檢查數字是否為 NaN(非數字)。此方法傳回布林值:

  • True – 該數字為 NaN。

  • False – 該數字是有效數字。

用法:
math.isnan(x)

範圍

描述

x

要檢查的浮點數或整數。

# Check if NaN (Not a Number) is NaN (returns True)
is_nan_true = math.isnan(math.nan)
robot.screen.print(is_nan_true)

# is_nan_true = True

# Check if 42 is NaN (returns False)
is_nan_false = math.isnan(42)
robot.screen.print(is_nan_false)

# is_nan_false = False

Error and Gamma Calculations#

伽瑪#

gamma 計算 x的伽瑪函數,它是實數和複數階乘函數的推廣。對於整數 n, gamma(n) = (n-1)!。

用法:
math.gamma(x)

範圍

描述

x

一個正浮點數或整數。

# Calculate the gamma function of 5 (equivalent to 4!)
gamma_result = math.gamma(5)
robot.screen.print(gamma_result)

# gamma_result = 24.00

lgamma#

lgamma 計算 gamma 函數的自然對數。

用法:
math.lgamma(x)

範圍

描述

x

一個正浮點數或整數。

# Calculate the natural logarithm of the
# gamma function of 5
lgamma_result = math.lgamma(5)
robot.screen.print(lgamma_result)

# lgamma_result = 3.18

ERF#

erf 計算 x的誤差函數。

用法:
math.erf(x)

範圍

描述

x

表示輸入數值的浮點數或整數。

# Calculate the error function of 1
erf_result = math.erf(1)
robot.screen.print(erf_result)

# erf_result = 0.84

ERFC#

erfc 計算 x的互補誤差函數,定義為 1 - erf(x)。

用法:
math.erfc(x)

範圍

描述

x

表示輸入數值的浮點數或整數。

# Calculate the complementary error function of 1
erfc_result = math.erfc(1)
robot.screen.print(erfc_result)

# erfc_result = 0.16