数学#

简介#

Math包括内置Python函数和VEXcode AIM中自动提供的完整 math 模块。这些工具允许您执行从基本算术到高级三角函数、舍入和对数运算的所有操作。

使用这些函数和常量来计算机器人的位置、角度、距离和其他数值。您还可以在角度和弧度之间转换、计算表达式的值,以及处理无穷大和 NaN 等特殊值。

以下是可用的数学函数、常量和实用程序的列表:

内置函数 — Python 自带的常用数学工具。

  • [abs] (#abs) –返回数字的绝对值。

  • [round] (#round) –将数字舍入到指定的小数位数。

  • [min] (#min) –返回最小的输入值。

  • [max] (#max) –返回最大的输入值。

  • [sum] (#sum) –将可迭代中的所有值相加。

  • [divmod] (#divmod) –以元组形式返回商和余数。

  • [pow] (#pow) –计算一个数的幂,可选模运算。

  • [int] (#int) –将值转换为整数。

  • [float] (#float) –将值转换为浮点数。

Constants – Predefined values from the math module.

  • [math.pi] (#pi) –常数π (pi)。

  • [math.tau] (#tau) –常量tau (2π)。

  • [math.e] (#e) –欧拉数,自然对数的底数。

  • [math.inf] (#inf) –正无穷大。

  • [math.nan] (#nan) –不是数字(NaN)。

三角函数 — 计算角度和边之间的关系。

  • [math.sin] (#sin) –弧度角度的正弦值。

  • [math.cos] (#cos) –弧度角度的余弦值。

  • [math.tan] (#tan) –以弧度为单位的角度切线。

  • [math.atan] (#atan) –以弧度为单位的值的反正切。

  • [math.atan2] (#atan2) – y/x在弧度中的反正切,考虑象限。

  • [math.asinmath.asin] (#asin) –以弧度为单位的值的反正弦值。

  • [math.acos] (#acos) –以弧度为单位的值的反余弦。

  • [math.degrees] (#degrees) –将弧度转换为度。

  • [math.radiansmath.radians] (#radians) –将度数转换为弧度。

Hyperbolics – Advanced trig-related functions.

  • [math.sinh] (#sinh) –数值的双曲正弦值。

  • [math.cosh] (#cosh) –数值的双曲余弦。

  • [math.tanh] (#tanh) –数值的双曲正切。

  • [math.asinh] (#asinh) –反双曲正弦。

  • [math.acosh] (#acosh) –反双曲余弦。

  • [math.atanh] (#atanh) –反双曲正切。

舍入与绝对值 — 调整精度或取整方向。

  • [math.ceil] (#ceil) –向上取整到最接近的整数。

  • math.floor – 向下取整到最接近的整数。

  • [math.trunc] (#trunc) –删除小数部分。

  • [math.fabs] (#fabs) –以浮点形式返回绝对值。

指数与对数 — 幂、开方和对数计算。

  • [math.pow] (#pow) –计算一个数的幂。

  • [math.sqrt] (#sqrt) –返回平方根。

  • [math.exp] (#exp) –计算e的x的幂。

  • [math.log] (#log) –自然对数(以e为底)。

  • [math.log10] (#log10) –以10为底的对数。

  • [math.log2] (#log2) –以2为底的对数。

  • [math.factorial] (#factorial) –整数的阶乘。

  • [math.expm1] (#expm1) – 更精确地计算 e^x - 1

浮点数操作 — 检查或分解浮点数值。

  • [math.modf] (#modf) –返回浮点的小数和整数部分。

  • [math.frexp] (#frexp) –将数字分解为尾数和指数。

  • [math.fmod] (#fmod) –符号与被除数相同。

  • [math.copysign] (#copysign) –返回带有另一个数符号的值。

  • [math.ldexp] (#ldexp) –计算 x * (2 ** exp)

比较与近似 — 检查值的容差或类别。

  • [math.isclose] (#isclose) –测试两个值是否近似相等。

  • [math.isfinite] (#isfinite) –检查数字是否有限。

  • [math.isinf] (#isinf) –检查数字是否无限。

  • [math.isnan] (#isnan) –检查值是否为NaN。

误差与伽马函数 — 特殊的数学函数。

  • [math.gamma] (#gamma) –伽玛函数(广义阶乘)。

  • [math.lgamma] (#lgamma) –伽玛函数的自然对数。

  • [math.erf] (#erf) –误差函数。

  • [math.erfc] (#erfc) –互补误差函数。

内置函数#

Python 提供了多种内置函数,允许您在项目中执行数学运算。

abs#

abs 返回数字的绝对值,即去掉负号。

Usage:
abs(x)

参数

描述

x

整数或浮点数。

# Get the absolute value of -10
abs_result = abs(-10)
robot.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)
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 返回多个参数或可迭代参数中的最小值。

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)
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 返回多个参数或可迭代参数中的最大值。

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)
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 将可迭代中的所有值相加,并具有可选的起始值。

Usage:
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 返回包含除法运算的商和余数的元组。

Usage:
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 计算一个数的幂,并可选择进行模运算。

Usage:
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 将数字或字符串转换为整数。在从字符串转换时还支持基数转换。

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)
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 将数字或字符串转换为浮点数。

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#

pi 给出了数学常数π ,即圆的圆周与其直径的比率。

Usage:
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#

tau 的值为2π。

Usage:
math.tau

# Calculate the circumference of a circle with radius
circumference = math.tau * 5
robot.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)
robot.screen.print(e_power)

# e_power = 7.39

inf#

inf 将正无穷大作为浮点数表示。

Usage:
math.inf

# Check if infinity is infinite
inf_value = math.inf

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

# Prints "Infinity"

nan#

nan 表示”Not a Number” (NaN)的特殊浮点数。

Usage:
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#

sin 计算以弧度为单位的角度的正弦值,并返回浮点数。

Usage:
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#

cos 计算以弧度为单位的角度的余弦值,并返回浮点数。

Usage:
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#

tan 计算以弧度为单位的角度的正切,并返回一个浮点数。

Usage:
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#

atan 计算数字的反正切(反正切) ,并返回以弧度表示的角度浮点数。

Usage:
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的反正切的主值,并返回以弧度表示角度的浮点数。

Usage:
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#

asin 计算一个数字的逆正弦 (反正弦),并返回一个以弧度表示的浮点数。

Usage:
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 计算数字的逆余弦(反余弦) ,并返回以弧度表示的角度浮点数。

Usage:
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#

degrees 将角度从弧度转换为度。

Usage:
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#

radians 将角度从度转换为弧度。

Usage:
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#

sinh 计算 x的双曲正弦。

Usage:
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#

cosh 计算 x双曲余弦。

Usage:
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的双曲正切。

Usage:
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#

asinh 计算 x的反双曲正弦。

Usage:
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#

acosh 计算 x的反双曲余弦。

Usage:
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#

atanh 计算 x 的反双曲正切。

Usage:
math.atanh(x)

参数

描述

x

介于 -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 将数字向上取整。

Usage:
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 将数字向下取整。

Usage:
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#

trunc 删除数字的小数部分而不进行舍入。

Usage:
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#

fabs 返回一个浮点数的绝对值。

Usage:
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#

pow 计算 x 的 y 次幂,并返回一个浮点数,即使两个输入都是整数。

Usage:
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#

sqrt 计算一个数的平方根,并返回一个浮点数,即使对于完全平方数也是如此。

Usage:
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#

exp 计算数字的指数并返回浮点数。

Usage:
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 计算数字的自然对数并返回浮点数。

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

log 10#

log10 计算一个数字的以10为底的对数,并返回一个浮点数。

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

log 2#

log2 计算一个数字的以2为底的对数,并返回浮点数,即使 x 是2的幂。

Usage:
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#

factorial 返回整数 x这是 x 所有正整数的乘积。

Usage:
math.factorial(x)

参数

描述

x

一个非负整数。

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

# factorial_result = 120

expm1#

expm1 计算 ex - 1, 对于小 x 更准确。

Usage:
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), 均为浮点数。

Usage:
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),其中尾数是一个浮点数,指数是一个整数。

Usage:
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)的符号。

Usage:
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#

copysign 返回带有 y符号的x。

Usage:
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}

Usage:
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#

isclose 检查两个数字是否在公差范围内大致相等。

Usage:
math.isclose(a, b, rel_tol, abs_tol)

参数

描述

a

要比较的第一个数字。

b

要比较的第二个数字。

rel_tol

可选。a和b之间相对于其大小的最大允许差值。默认值为1e-09 (非常小)。

abs_tol

可选。固定的误差幅度,在比较接近零的数字时很有用。默认值为 0.0。

Note: If both rel_tol and abs_tol are provided, whichever condition is met first determines the result.

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

isfinite 检查数字是否有限。此方法返回一个布尔值:

  • True — — 数字是有限的。

  • False –数字是无限的。

Usage:
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 –数字是有限的。

Usage:
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#

isnan 检查数字是否为NaN (非数字)。此方法返回一个布尔值:

  • True — — 数字是NAN。

  • False –数字为有效数字。

Usage:
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#

gamma 计算 x的gamma函数,它推广了实数和复数的阶乘函数。对于整数 ngamma(n) = (n-1)!

Usage:
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 函数的自然对数。

Usage:
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的误差函数。

Usage:
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)

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