数学#

介绍#

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

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

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

内置函数——Python 附带的常见数学工具。

  • abs – Returns the absolute value of a number.

  • round – Rounds a number to a specified number of decimal places.

  • min – Returns the smallest of the input values.

  • max – Returns the largest of the input values.

  • sum – Adds up all values in an iterable.

  • divmod – Returns the quotient and remainder as a tuple.

  • pow – Raises a number to a power, optionally with a modulus.

  • int – Converts a value to an integer.

  • float – Converts a value to a floating-point number.

常量 – 来自math模块的预定义值。

  • math.pi – The constant π (pi).

  • math.tau – The constant tau (2π).

  • math.e – Euler’s number, base of the natural log.

  • math.inf – Positive infinity.

  • math.nan – Not a Number (NaN).

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

  • math.sin – Sine of an angle in radians.

  • math.cos – Cosine of an angle in radians.

  • math.tan – Tangent of an angle in radians.

  • math.atan – Arctangent of a value in radians.

  • math.atan2 – Arctangent of y/x in radians, considering the quadrant.

  • math.asin – Arcsine of a value in radians.

  • math.acos – Arccosine of a value in radians.

  • math.degrees – Converts radians to degrees.

  • math.radians – Converts degrees to radians.

双曲线——高级三角相关函数。

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

  • math.ceil – Rounds up to the nearest integer.

  • math.floor – Rounds down to the nearest integer.

  • math.trunc – Removes the decimal portion.

  • math.fabs – Returns the absolute value as a float.

指数和对数——幂、根和对数计算。

浮点运算——检查或分解浮点值。

  • math.modf – Returns the fractional and integer parts of a float.

  • math.frexp – Decomposes a number into mantissa and exponent.

  • math.fmod – Remainder with sign of the dividend.

  • math.copysign – Returns a value with the sign of another.

  • math.ldexp – Computes 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, ...)min(sequence)

范围

描述

arg1arg2,…

要比较的数字。

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)

范围

描述

arg1arg2,…

要比较的数字。

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#

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

用法:
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 计算 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(不含)之间的浮点数。

# 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

log10#

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

log2#

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 计算 e<sup>x</sup> - 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 将数字分解为小数部分和整数部分,并返回一个元组 (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 * 2<sup>exp</sup>

用法:
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_tolabs_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 检查一个数字是否为无穷大。此方法返回一个布尔值:

  • 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 的伽马函数,该函数推广了实数和复数的阶乘函数。对于整数 ngamma(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 计算 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