數學#

介紹#

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

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

整數或浮點數。

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

    # abs_result = 10

# VR threads — Do not delete
vr_thread(main)

round#

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

Usage:
round(x, ndigits)

範圍

描述

x

整數或浮點數。

ndigits

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

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

    # round_int_result = 6

# VR threads — Do not delete
vr_thread(main)

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

    # round_result = 3.14

# VR threads — Do not delete
vr_thread(main)

min#

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

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

範圍

描述

arg1, arg2, …

需要比較的數據。

sequence

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

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

    # min_result = 1

# VR threads — Do not delete
vr_thread(main)

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

    # min_list_result = 1

# VR threads — Do not delete
vr_thread(main)

max#

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

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

範圍

描述

arg1, arg2, …

需要比較的數據。

sequence

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

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

    # max_result = 7

# VR threads — Do not delete
vr_thread(main)

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

    # max_list_result = 25

# VR threads — Do not delete
vr_thread(main)

sum#

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

Usage:
sum(sequence, start)

範圍

描述

sequence

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

start

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

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

    # sum_result = 15

# VR threads — Do not delete
vr_thread(main)

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

    # sum_with_start = 16

# VR threads — Do not delete
vr_thread(main)

divmod#

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

Usage:
divmod(a, b)

範圍

描述

a

分紅。

b

除數。

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

    # divmod_result = (3, 1)

# VR threads — Do not delete
vr_thread(main)

pow#

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

Usage:
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)
    brain.print(pow_result)

    # pow_result = 8

# VR threads — Do not delete
vr_thread(main)

int#

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

Usage:
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)
    brain.print("{} coins".format(price_int))

    # Output: 19 coins

# VR threads — Do not delete
vr_thread(main)

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

    # Output: 110

# VR threads — Do not delete
vr_thread(main)

float#

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

Usage:
float(x)

範圍

描述

x

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

def main():
    # Convert division result to a float
    num_apples = 6
    num_people = 2
    apples_per_person = float(num_apples) / num_people
    brain.print(apples_per_person)

    # Output: 3.00

# VR threads — Do not delete
vr_thread(main)

def main():
    # Convert a string into a float to use in calculations
    user_input = "23.4"
    user_number = float(user_input)
    brain.print(user_number * 3)

    # Output: 70.20

# VR threads — Do not delete
vr_thread(main)

數學模組#

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

VEXcode 預設導入 math 模組。

Constants#

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

π#

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

Usage:
math.pi

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

    # circle_area = 78.54

# VR threads — Do not delete
vr_thread(main)

τ#

tau 給出的值為 2π。

Usage:
math.tau

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

    # circumference = 31.42

# VR threads — Do not delete
vr_thread(main)

e#

e 表示自然對數的底數。

Usage:
math.e

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

    # e_power = 7.39

# VR threads — Do not delete
vr_thread(main)

Trigonometry#

罪#

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

Usage:
math.sin(x)

範圍

描述

x

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

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

    # result = 0.50

# VR threads — Do not delete
vr_thread(main)

化學#

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

Usage:
math.cos(x)

範圍

描述

x

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

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

    # result = 0.50

# VR threads — Do not delete
vr_thread(main)

棕褐色#

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

Usage:
math.tan(x)

範圍

描述

x

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

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

    # result = 1.00

# VR threads — Do not delete
vr_thread(main)

阿坦#

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

Usage:
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)
    brain.print(result, precision=2)

    # result = 45.00

# VR threads — Do not delete
vr_thread(main)

atan2#

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

Usage:
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)
    brain.print(result, precision=2)

    # result = 45.00

# VR threads — Do not delete
vr_thread(main)

亞辛#

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

Usage:
math.asin(x)

範圍

描述

x

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

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

    # result = 30.00

# VR threads — Do not delete
vr_thread(main)

acos#

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

Usage:
math.acos(x)

範圍

描述

x

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

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

    # result = 60.00

# VR threads — Do not delete
vr_thread(main)

學位#

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

Usage:
math.degrees(x)

範圍

描述

x

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

def main():
    # Convert pi radians to degrees
    degrees_result = math.degrees(math.pi)
    brain.print(degrees_result, precision=2)

    # degrees_result = 180.00

# VR threads — Do not delete
vr_thread(main)

弧度#

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

Usage:
math.radians(x)

範圍

描述

x

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

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

    # radians_result = 3.14

# VR threads — Do not delete
vr_thread(main)

Rounding & Absolute Values#

天花板#

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

Usage:
math.ceil(x)

範圍

描述

x

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

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

    # ceil_result = 4

# VR threads — Do not delete
vr_thread(main)

地面#

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

Usage:
math.floor(x)

範圍

描述

x

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

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

    # floor_result = 3

# VR threads — Do not delete
vr_thread(main)

截斷#

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

Usage:
math.trunc(x)

範圍

描述

x

待截斷的浮點數。

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

    # trunc_result = 3

# VR threads — Do not delete
vr_thread(main)

工廠#

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

Usage:
math.fabs(x)

範圍

描述

x

浮點數或整數。

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

    # fabs_result = 3.70

# VR threads — Do not delete
vr_thread(main)

Exponents & Logarithms#

火力#

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

Usage:
math.pow(x, y)

範圍

描述

x

浮點數或整數作為基數。

y

浮點數或整數指數。

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

    # power_result = 8.00

# VR threads — Do not delete
vr_thread(main)

平方根#

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

Usage:
math.sqrt(x)

範圍

描述

x

非負浮點數或整數。

def main():
    # Calculate the square root of 16
    sqrt_result = math.sqrt(16)
    brain.print(sqrt_result, precision=2)

    # sqrt_result = 4.00

# VR threads — Do not delete
vr_thread(main)

經驗#

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

Usage:
math.exp(x)

範圍

描述

x

浮點數或整數。

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

    # exp_result = 2.72

# VR threads — Do not delete
vr_thread(main)

紀錄#

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

Usage:
math.log(x)

範圍

描述

x

一個正浮點數或整數。

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

    # log_result = 2.00

# VR threads — Do not delete
vr_thread(main)

10 行#

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

Usage:
math.log10(x)

範圍

描述

x

一個正浮點數或整數。

def main():
    # Calculate the log10 of 100
    log_result = math.log10(100)
    brain.print(log_result)

    # log_result = 2

# VR threads — Do not delete
vr_thread(main)

Floating Point Operations#

modf#

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

Usage:
math.modf(x)

範圍

描述

x

要分解的浮點數或整數。

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

    # fractional_part = 0.14
    # integer_part = 3.00

# VR threads — Do not delete
vr_thread(main)

frexp#

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

Usage:
math.frexp(x)

範圍

描述

x

要分解的浮點數或整數。

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

    # mantissa = 0.50
    # exponent = 5

# VR threads — Do not delete
vr_thread(main)

fmod#

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

Usage:
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)
    brain.print(fmod_result, precision=2)

    # fmod_result = 1.00

# VR threads — Do not delete
vr_thread(main)

簽名#

copysign 傳回帶有 y的 x。

Usage:
math.copysign(x, y)

範圍

描述

x

要修改的值。

y

要複製其符號的值。

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

    # copysign_result = 10.00

# VR threads — Do not delete
vr_thread(main)

ldexp#

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

Usage:
math.ldexp(x, exp)

範圍

描述

x

基準值。

exp

指數。

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

    # ldexp_result = 48.00

# VR threads — Do not delete
vr_thread(main)