數學#

介紹#

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

使用這些函數和常數可以計算機器人的位置、角度、距離和其他數值。您還可以進行角度和弧度之間的轉換、計算表達式以及處理特殊值,例如無限大和 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 回傳一個數的絕對值,去掉任何負號。

用法:
abs(x)

範圍

描述

x

整數或浮點數。

def main():
    # Get the absolute value of -10
    abs_result = abs(-10)
    console.print(abs_result)

# abs_result = 10

# Start threads — Do not delete
start_thread(main)

round#

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

用法:
round(x, ndigits)

範圍

描述

x

整數或浮點數。

ndigits

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

def main():
    # Round 5.7 to the nearest integer
    round_int_result = round(5.7)
    console.print(round_int_result)

# round_int_result = 6

# Start threads — Do not delete
start_thread(main)

def main():
    # Round 3.14159 to 2 decimal places
    round_result = round(3.14159, 2)
    console.print(round_result, precision=2)

# round_result = 3.14

# Start threads — Do not delete
start_thread(main)

min#

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

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

範圍

描述

arg1, arg2, …

需要比較的數據。

sequence

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

def main():
    # Get the smallest number from 3, 7, and 1
    min_result = min(3, 7, 1)
    console.print(min_result)

# min_result = 1

# Start threads — Do not delete
start_thread(main)

def main():
    # Get the smallest value from a list
    min_list_result = min([10, 4, 25, 1])
    console.print(min_list_result)

# min_list_result = 1

# Start threads — Do not delete
start_thread(main)

max#

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

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

範圍

描述

arg1, arg2, …

需要比較的數據。

sequence

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

def main():
    # Get the largest number from 3, 7, and 1
    max_result = max(3, 7, 1)
    console.print(max_result)

# max_result = 7

# Start threads — Do not delete
start_thread(main)

def main():
    # Get the largest value from a list
    max_list_result = max([10, 4, 25, 1])
    console.print(max_list_result)

# max_list_result = 25

# Start threads — Do not delete
start_thread(main)

sum#

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

用法:
sum(sequence, start)

範圍

描述

sequence

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

start

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

def main():
    # Calculate the sum of a list of numbers
    sum_result = sum([1, 2, 3, 4, 5])
    console.print(sum_result)

# sum_result = 15

# Start threads — Do not delete
start_thread(main)

def main():
    # Calculate the sum of a list with a starting value of 10
    sum_with_start = sum([1, 2, 3], 10)
    console.print(sum_with_start)

# sum_with_start = 16

# Start threads — Do not delete
start_thread(main)

divmod#

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

用法:
divmod(a, b)

範圍

描述

a

分紅。

b

除數。

def main():
    # Perform integer division and remainder of 10 / 3
    divmod_result = divmod(10, 3)
    console.print(divmod_result)

# divmod_result = (3, 1)

# Start threads — Do not delete
start_thread(main)

pow#

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

用法:
pow(x, y, mod)

範圍

描述

x

基數。

y

指數。

mod

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

def main():
    # Calculate 2 raised to the power of 3
    pow_result = pow(2, 3)
    console.print(pow_result)

# pow_result = 8

# Start threads — Do not delete
start_thread(main)

int#

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

用法:
int(x, base)

範圍

描述

x

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

base

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

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

# Output: 19 coins

# Start threads — Do not delete
start_thread(main)

def main():
    # Convert a string into an integer to use in calculations
    user_input = "55"
    user_number = int(user_input)
    console.print(user_number * 2)

# Output: 110

# Start threads — Do not delete
start_thread(main)

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

def main():
    # Calculate the area of a circle with radius 5 using pi
    circle_area = math.pi * 5 * 5
    console.print(circle_area, precision=2)

# circle_area = 78.54

# Start threads — Do not delete
start_thread(main)

τ#

tau 給出的值為 2π。

用法:
math.tau

def main():
    # Calculate the circumference of a circle with radius
    circumference = math.tau * 5
    console.print(circumference, precision=2)

# circumference = 31.42

# Start threads — Do not delete
start_thread(main)

e#

e 表示自然對數的底數。

用法:
math.e

def main():
    # Calculate e raised to the power of 2
    e_power = math.pow(math.e, 2)
    console.print(e_power, precision=2)

# e_power = 7.39

# Start threads — Do not delete
start_thread(main)

Trigonometry#

罪#

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

用法:
math.sin(x)

範圍

描述

x

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

def main():
    # Calculate the sine of 
    # 30 degrees in radians
    result = math.sin(math.radians(30))
    console.print(result, precision=2)

# result = 0.50

# Start threads — Do not delete
start_thread(main)

化學#

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

用法:
math.cos(x)

範圍

描述

x

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

def main():
    # Calculate the cosine of 
    # 60 degrees in radians
    result = math.cos(math.radians(60))
    console.print(result, precision=2)

# result = 0.50

# Start threads — Do not delete
start_thread(main)

棕褐色#

tan 計算弧度角的正切值並傳回一個整數。

用法:
math.tan(x)

範圍

描述

x

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

def main():
    # Calculate the tangent of 
    # 45 degrees in radians
    result = math.tan(math.radians(45))
    console.print(result)

# result = 1

# Start threads — Do not delete
start_thread(main)

亞辛#

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

用法:
math.asin(x)

範圍

描述

x

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

def main():
    # Calculate the arc sine 
    # of 0.5 in degrees
    result = math.degrees(math.asin(0.5))
    console.print(result)

# result = 30

# Start threads — Do not delete
start_thread(main)

acos#

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

用法:
math.acos(x)

範圍

描述

x

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

def main():
    # Calculate the arc cosine 
    # of 0.5 in degrees
    result = math.degrees(math.acos(0.5))
    console.print(result)

# result = 60

# Start threads — Do not delete
start_thread(main)

阿坦#

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

用法:
math.atan(x)

範圍

描述

x

浮點數或整數。

def main():
    # Calculate the arc tangent 
    # of 1 in degrees
    arc_tan = math.atan(1.0)
    result = math.degrees(arc_tan)
    console.print(result)

# result = 45

# Start threads — Do not delete
start_thread(main)

atan2#

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

用法:
math.atan2(y, x)

範圍

描述

y

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

x

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

def main():
    # Calculate the inverse tangent 
    # of 1/1 in degrees
    atan2 = math.atan2(1.0, 1.0)
    result = math.degrees(atan2)
    console.print(result)

# result = 45

# Start threads — Do not delete
start_thread(main)

學位#

degrees 將角度從弧度轉換為度,並傳回一個整數。

用法:
math.degrees(x)

範圍

描述

x

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

def main():
    # Convert pi radians to degrees
    degrees_result = math.degrees(math.pi)
    console.print(degrees_result)

# degrees_result = 180

# Start threads — Do not delete
start_thread(main)

弧度#

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

用法:
math.radians(x)

範圍

描述

x

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

def main():
    # Convert 180 degrees to radians
    radians_result = math.radians(180)
    console.print(radians_result, precision=2)

# radians_result = 3.14

# Start threads — Do not delete
start_thread(main)

Rounding & Absolute Values#

天花板#

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

用法:
math.ceil(x)

範圍

描述

x

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

def main():
    # Round 3.7 up to the nearest integer
    ceil_result = math.ceil(3.7)
    console.print(ceil_result)

# ceil_result = 4

# Start threads — Do not delete
start_thread(main)

地面#

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

用法:
math.floor(x)

範圍

描述

x

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

def main():
    # Round 3.7 down to the nearest integer
    floor_result = math.floor(3.7)
    console.print(floor_result)

# floor_result = 3

# Start threads — Do not delete
start_thread(main)

截斷#

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

用法:
math.trunc(x)

範圍

描述

x

待截斷的浮點數。

def main():
    # Remove the decimal part of 3.7
    trunc_result = math.trunc(3.7)
    console.print(trunc_result)

# trunc_result = 3

# Start threads — Do not delete
start_thread(main)

工廠#

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

用法:
math.fabs(x)

範圍

描述

x

浮點數或整數。

def main():
    # Get the absolute value of -3.7
    fabs_result = math.fabs(-3.7)
    console.print(fabs_result, precision=1)

# fabs_result = 3.7

# Start threads — Do not delete
start_thread(main)

Exponents & Logarithms#

紀錄#

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

用法:
math.log(x, base)

範圍

描述

x

一個正浮點數或整數。

base

可選。使用的對數底數。預設值為自然對數。

def main():
    # Calculate the natural logarithm
    # (base e) of 7.389056
    log_result = math.log(7.389056)
    console.print(log_result)

# log_result = 2

# Start threads — Do not delete
start_thread(main)

def main():
    # Calculate the log base 2 of 8
    log_result = math.log(8, 2)
    console.print(log_result)

# log_result = 3

# Start threads — Do not delete
start_thread(main)

10 行#

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

用法:
math.log10(x)

範圍

描述

x

一個正浮點數或整數。

def main():
    # Calculate the base-10 logarithm of 100
    log10_result = math.log10(100)
    console.print(log10_result)

# log10_result = 2

# Start threads — Do not delete
start_thread(main)

火力#

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

用法:
math.pow(x, y)

範圍

描述

x

浮點數或整數作為基數。

y

浮點數或整數指數。

def main():
    # Calculate 2 raised to the power of 3
    power_result = math.pow(2, 3)
    console.print(power_result)

    # power_result = 8

# Start threads — Do not delete
start_thread(main)

平方根#

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

用法:
math.sqrt(x)

範圍

描述

x

非負浮點數或整數。

def main():
    # Calculate 2 raised to the power of 3
    power_result = math.pow(2, 3)
    console.print(power_result)

# power_result = 8

# Start threads — Do not delete
start_thread(main)

經驗#

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

用法:
math.exp(x)

範圍

描述

x

浮點數或整數。

def main():
    # Calculate e raised to the power of 1
    exp_result = math.exp(1)
    console.print(exp_result, precision=2)

# exp_result = 2.72

# Start threads — Do not delete
start_thread(main)

Floating Point Operations#

modf#

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

用法:
math.modf(x)

範圍

描述

x

要分解的浮點數或整數。

def main():
    # Decompose 3.14159 into fractional and integer parts
    fractional_part, integer_part = math.modf(3.14159)
    console.print(fractional_part, precision=2)
    console.new_line()
    console.print(integer_part)

# fractional_part = 0.14
# integer_part = 3

# Start threads — Do not delete
start_thread(main)

frexp#

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

用法:
math.frexp(x)

範圍

描述

x

要分解的浮點數或整數。

def main():
    # Decompose 16 into its mantissa and exponent
    mantissa, exponent = math.frexp(16)
    console.print(mantissa, precision=2)
    console.new_line()
    console.print(exponent)

# mantissa = 0.50
# exponent = 5

# Start threads — Do not delete
start_thread(main)

fmod#

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

用法:
math.fmod(x, y)

範圍

描述

x

分紅。

y

除數。

def main():
    # Calculate remainder of 10 / 3
    # that preserves the sign of 10
    fmod_result = math.fmod(10, 3)
    console.print(fmod_result)

# fmod_result = 1

# Start threads — Do not delete
start_thread(main)

簽名#

copysign 回傳帶有 y的 x。

用法:
math.copysign(x, y)

範圍

描述

x

要修改的值。

y

要複製其符號的值。

def main():
    # Return -10 with the sign of 3 (positive)
    copysign_result = math.copysign(-10, 3)
    console.print(copysign_result)

# copysign_result = 10

# Start threads — Do not delete
start_thread(main)

ldexp#

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

用法:
math.ldexp(x, exp)

範圍

描述

x

基準值。

exp

指數。

def main():
    # Compute 3 * (2 ** 4)
    ldexp_result = math.ldexp(3, 4)
    console.print(ldexp_result)

# ldexp_result = 48

# Start threads — Do not delete
start_thread(main)