數學#

介紹#

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  • math.pi — 常數 π(pi)。

  • math.tau — 常數 tau (2π)。

  • math.e — 歐拉數,自然對數的底數。

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

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

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

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

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

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

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

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

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

內建功能#

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

abs#

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

Usage:
abs(x)

範圍

描述

x

整數或浮點數。

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

# abs_result = 10

round#

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

Usage:
round(x, ndigits)

範圍

描述

x

整數或浮點數。

ndigits

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

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

# round_int_result = 6

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

# round_result = 3.14

min#

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

Usage:
min(arg1, arg2, …) or min(sequence)

範圍

描述

arg1, arg2, …

需要比較的數據。

sequence

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

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

# min_result = 1

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

# min_list_result = 1

max#

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

Usage:
max(arg1, arg2, …) or max(sequence)

範圍

描述

arg1, arg2, …

需要比較的數據。

sequence

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

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

# max_result = 7

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

# max_list_result = 25

sum#

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

Usage:
sum(sequence, start)

範圍

描述

sequence

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

start

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

# Calculate the sum of a list of numbers
sum_result = sum([1, 2, 3, 4, 5])
brain.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)
brain.screen.print(sum_with_start)

# sum_with_start = 16

divmod#

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

Usage:
divmod(a, b)

範圍

描述

a

分紅。

b

除數。

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

# divmod_result = (3, 1)

pow#

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

Usage:
pow(x, y, mod)

範圍

描述

x

基數。

y

指數。

mod

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

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

# pow_result = 8

int#

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

Usage:
int(x, base)

範圍

描述

x

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

base

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

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

# Output: 19 coins

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

# Output: 110

float#

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

Usage:
float(x)

範圍

描述

x

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

# Convert division result to a float
num_apples = 6
num_people = 2
apples_per_person = float(num_apples) / num_people
brain.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)
brain.screen.print(user_number * 3)

# Output: 70.20

數學模組#

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

VEXcode 預設導入 math 模組。

Constants#

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

π#

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

Usage:
math.pi

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

# circle_area = 78.54

τ#

tau 給出的值為 2π。

Usage:
math.tau

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

# circumference = 31.42

e#

e 表示自然對數的底數。

Usage:
math.e

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

# e_power = 7.39

Trigonometry#

罪#

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

Usage:
math.sin(x)

範圍

描述

x

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

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

# result = 0.50

化學#

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

Usage:
math.cos(x)

範圍

描述

x

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

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

# result = 0.50

棕褐色#

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

Usage:
math.tan(x)

範圍

描述

x

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

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

# result = 1.00

阿坦#

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

Usage:
math.atan(x)

範圍

描述

x

浮點數或整數。

# Calculate the arc tangent 
# of 1 in degrees
arc_tan = math.atan(1.0)
result = math.degrees(arc_tan)
brain.screen.print(result)

# result = 45.00

atan2#

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

Usage:
math.atan2(y, x)

範圍

描述

y

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

x

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

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

# result = 45.00

亞辛#

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

Usage:
math.asin(x)

範圍

描述

x

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

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

# result = 30.00

acos#

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

Usage:
math.acos(x)

範圍

描述

x

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

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

# result = 60.00

學位#

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

Usage:
math.degrees(x)

範圍

描述

x

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

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

# degrees_result = 180.00

弧度#

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

Usage:
math.radians(x)

範圍

描述

x

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

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

# radians_result = 3.14

Rounding & Absolute Values#

天花板#

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

Usage:
math.ceil(x)

範圍

描述

x

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

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

# ceil_result = 4

地面#

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

Usage:
math.floor(x)

範圍

描述

x

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

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

# floor_result = 3

截斷#

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

Usage:
math.trunc(x)

範圍

描述

x

待截斷的浮點數。

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

# trunc_result = 3

工廠#

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

Usage:
math.fabs(x)

範圍

描述

x

浮點數或整數。

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

# fabs_result = 3.70

Exponents & Logarithms#

火力#

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

Usage:
math.pow(x, y)

範圍

描述

x

浮點數或整數作為基數。

y

浮點數或整數指數。

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

# power_result = 8.00

平方根#

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

Usage:
math.sqrt(x)

範圍

描述

x

非負浮點數或整數。

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

# sqrt_result = 4.00

經驗#

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

Usage:
math.exp(x)

範圍

描述

x

浮點數或整數。

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

# exp_result = 2.72

紀錄#

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

Usage:
math.log(x)

範圍

描述

x

一個正浮點數或整數。

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

# log_result = 2.00

Floating Point Operations#

modf#

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

Usage:
math.modf(x)

範圍

描述

x

要分解的浮點數或整數。

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

# fractional_part = 0.14
# integer_part = 3.00

frexp#

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

Usage:
math.frexp(x)

範圍

描述

x

要分解的浮點數或整數。

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

# mantissa = 0.50
# exponent = 5

fmod#

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

Usage:
math.fmod(x, y)

範圍

描述

x

分紅。

y

除數。

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

# fmod_result = 1.00

簽名#

copysign 傳回帶有 y的 x。

Usage:
math.copysign(x, y)

範圍

描述

x

要修改的值。

y

要複製其符號的值。

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

# copysign_result = 10.00

ldexp#

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

Usage:
math.ldexp(x, exp)

範圍

描述

x

基準值。

exp

指數。

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

# ldexp_result = 48.00