数学#
介绍#
数学模块包含内置的 Python 函数和完整的 math 模块,后者在 VEXcode 中自动启用。这些工具使您能够执行从基本算术到高级三角函数、四舍五入和对数运算的各种操作。
使用这些函数和常量可以计算机器人的位置、角度、距离和其他数值。您还可以进行角度和弧度之间的转换、计算表达式以及处理特殊值,例如无穷大和 NaN。
以下是可用的数学函数、常量和实用程序列表:
内置函数 – Python 中包含的常用数学工具。
abs– 返回一个数的绝对值。round– 将数字四舍五入到指定的小数位数。min– 返回输入值中的最小值。max– 返回输入值中的最大值。sum– 将可迭代对象中的所有值相加。divmod– 返回商和余数作为元组。pow– 将一个数提升到幂,可以选择带模数。int– 将值转换为整数。float– 将值转换为浮点数。
常量 – 来自 math 模块的预定义值。
三角学——计算角度和边之间的关系。
math.sin– 弧度角的正弦值。math.cos– 弧度角的余弦值。math.tan– 弧度角的正切值。math.atan– 弧度值的反正切值。math.atan2– y/x 的弧度反正切值,考虑象限。math.asin– 弧度值的反正弦。math.acos– 弧度值的反余弦。math.degrees– 将弧度转换为角度。math.radians– 将角度转换为弧度。
舍入与绝对值 – 调整精度或方向。
math.ceil– 向上取整到最接近的整数。math.floor– 向下取整到最接近的整数。math.trunc– 删除小数部分。math.fabs– 返回浮点数的绝对值。
指数与对数——幂、根和对数计算。
math.log– 计算 x 的自然对数。math.log10– 计算 x 的对数(以 10 为底)。math.pow– 将一个数提升到幂。math.sqrt– 返回平方根。math.exp– 计算 e 的 x 次方。
浮点运算——检查或分解浮点值。
math.modf– 返回浮点数的分数部分和整数部分。math.frexp– 将一个数分解为尾数和指数。math.fmod– 余数,符号与被除数相同。math.copysign– 返回一个值,其符号与另一个值的符号相同。math.ldexp– 计算x * (2 ** exp)。
内置功能#
Python 提供了几个内置函数,允许你在项目中执行数学运算。
abs#
abs 返回一个数的绝对值,去掉任何负号。
Usage:
abs(x)
范围 |
描述 |
|---|---|
|
整数或浮点数。 |
# Get the absolute value of -10
abs_result = abs(-10)
brain.print(abs_result)
# abs_result = 10
round#
round 将数字四舍五入到指定的小数位数。
Usage:
round(x, ndigits)
范围 |
描述 |
|---|---|
|
整数或浮点数。 |
|
可选。四舍五入到小数点后的位数。默认值为 0。 |
# Round 5.7 to the nearest integer
round_int_result = round(5.7)
brain.print(round_int_result)
# round_int_result = 6
# Round 3.14159 to 2 decimal places
round_result = round(3.14159, 2)
brain.print(round_result, precision=2)
# round_result = 3.14
min#
min 返回多个参数或可迭代对象中的最小值。
Usage:
min(arg1, arg2, …) or min(sequence)
范围 |
描述 |
|---|---|
|
需要比较的数据。 |
|
包含数字的列表、元组或其他序列。 |
# Get the smallest number from 3, 7, and 1
min_result = min(3, 7, 1)
brain.print(min_result)
# min_result = 1
# Get the smallest value from a list
min_list_result = min([10, 4, 25, 1])
brain.print(min_list_result)
# min_list_result = 1
max#
max 返回多个参数或可迭代对象中的最大值。
Usage:
max(arg1, arg2, …) or max(sequence)
范围 |
描述 |
|---|---|
|
需要比较的数据。 |
|
包含数字的列表、元组或其他序列。 |
# Get the largest number from 3, 7, and 1
max_result = max(3, 7, 1)
brain.print(max_result)
# max_result = 7
# Get the largest value from a list
max_list_result = max([10, 4, 25, 1])
brain.print(max_list_result)
# max_list_result = 25
sum#
sum 将可迭代对象中的所有值相加,可以指定一个起始值。
Usage:
sum(sequence, start)
范围 |
描述 |
|---|---|
|
包含数字的列表、元组或其他序列。 |
|
可选。要添加到总和中的值。默认值为 0。 |
# Calculate the sum of a list of numbers
sum_result = sum([1, 2, 3, 4, 5])
brain.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.print(sum_with_start)
# sum_with_start = 16
divmod#
divmod 返回一个包含除法运算的商和余数的元组。
Usage:
divmod(a, b)
范围 |
描述 |
|---|---|
|
分红。 |
|
除数。 |
# Perform integer division and remainder of 10 / 3
divmod_result = divmod(10, 3)
brain.print(divmod_result)
# divmod_result = (3, 1)
pow#
pow 将一个数提升到一个幂,并可选择执行取模运算。
Usage:
pow(x, y, mod)
范围 |
描述 |
|---|---|
|
基数。 |
|
指数。 |
|
可选。模值。如果提供,则返回 |
# Calculate 2 raised to the power of 3
pow_result = pow(2, 3)
brain.print(pow_result)
# pow_result = 8
int#
int 将数字或字符串转换为整数。它还支持从字符串转换时进行进制转换。
Usage:
int(x, base)
范围 |
描述 |
|---|---|
|
要转换的数字、字符串或其他对象。 |
|
可选。用于转换的数字基数。默认值为 10。 |
# 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
# 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
float#
float 将数字或字符串转换为浮点数。
Usage:
float(x)
范围 |
描述 |
|---|---|
|
要转换的数字、字符串或其他对象。 |
# 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
# 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
数学模块#
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.print(circle_area, precision=2)
# circle_area = 78.54
e#
e 表示自然对数的底数。
Usage:
math.e
# 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
Trigonometry#
罪#
sin 计算弧度角的正弦值并返回浮点数。
Usage:
math.sin(x)
范围 |
描述 |
|---|---|
|
表示弧度角度的浮点数或整数。 |
# Calculate the sine of
# 30 degrees in radians
result = math.sin(math.radians(30))
brain.print(result, precision=2)
# result = 0.50
化学#
cos 计算弧度角的余弦值并返回浮点数。
Usage:
math.cos(x)
范围 |
描述 |
|---|---|
|
表示弧度角度的浮点数或整数。 |
# Calculate the cosine of
# 60 degrees in radians
result = math.cos(math.radians(60))
brain.print(result, precision=2)
# result = 0.50
棕褐色#
tan 计算弧度角的正切值并返回浮点数。
Usage:
math.tan(x)
范围 |
描述 |
|---|---|
|
表示弧度角度的浮点数或整数。 |
# Calculate the tangent of
# 45 degrees in radians
result = math.tan(math.radians(45))
brain.print(result)
# result = 1.00
亚辛#
asin 计算一个数的反正弦(反正弦)并返回一个表示弧度角度的浮点数。
Usage:
math.asin(x)
范围 |
描述 |
|---|---|
|
介于 -1 和 1 之间的浮点数或整数。 |
# Calculate the arc sine
# of 0.5 in degrees
result = math.degrees(math.asin(0.5))
brain.print(result)
# result = 30.00
acos#
acos 计算一个数的反余弦(反余弦),并返回一个表示弧度角度的浮点数。
Usage:
math.acos(x)
范围 |
描述 |
|---|---|
|
介于 -1 和 1 之间的浮点数或整数。 |
# Calculate the arc cosine
# of 0.5 in degrees
result = math.degrees(math.acos(0.5))
brain.print(result)
# result = 60.00
阿坦#
atan 计算一个数的反正切值(反正切值),并返回一个表示弧度角度的浮点数。
Usage:
math.atan(x)
范围 |
描述 |
|---|---|
|
浮点数或整数。 |
# Calculate the arc tangent
# of 1 in degrees
arc_tan = math.atan(1.0)
result = math.degrees(arc_tan)
brain.print(result)
# result = 45.00
atan2#
atan2 计算 y/x 的反正切函数的主值,并返回一个表示弧度角度的浮点数。
Usage:
math.atan2(y, x)
范围 |
描述 |
|---|---|
|
表示 y 坐标的浮点数或整数。 |
|
表示 x 坐标的浮点数或整数。 |
# Calculate the inverse tangent
# of 1/1 in degrees
atan2 = math.atan2(1.0, 1.0)
result = math.degrees(atan2)
brain.print(result)
# result = 45.00
学位#
degrees 将角度从弧度转换为度,并返回一个浮点数。
Usage:
math.degrees(x)
范围 |
描述 |
|---|---|
|
表示弧度角度的浮点数或整数。 |
# Convert pi radians to degrees
degrees_result = math.degrees(math.pi)
brain.print(degrees_result)
# degrees_result = 180.00
弧度#
radians 将角度从度数转换为弧度(浮点数)。
Usage:
math.radians(x)
范围 |
描述 |
|---|---|
|
表示角度的浮点数或整数,单位为度。 |
# Convert 180 degrees to radians
radians_result = math.radians(180)
brain.print(radians_result, precision=2)
# radians_result = 3.14
Rounding & Absolute Values#
天花板#
ceil 将数字向上取整到最接近的整数。
Usage:
math.ceil(x)
范围 |
描述 |
|---|---|
|
要向上取整的浮点数或整数。 |
# Round 3.7 up to the nearest integer
ceil_result = math.ceil(3.7)
brain.print(ceil_result)
# ceil_result = 4
地面#
floor 将数字向下取整到最接近的整数。
Usage:
math.floor(x)
范围 |
描述 |
|---|---|
|
要向下取整的浮点数或整数。 |
# Round 3.7 down to the nearest integer
floor_result = math.floor(3.7)
brain.print(floor_result)
# floor_result = 3
截断#
trunc 删除数字的小数部分而不进行四舍五入。
Usage:
math.trunc(x)
范围 |
描述 |
|---|---|
|
待截断的浮点数。 |
# Remove the decimal part of 3.7
trunc_result = math.trunc(3.7)
brain.print(trunc_result)
# trunc_result = 3
工厂#
fabs 返回数字的绝对值(浮点数)。
Usage:
math.fabs(x)
范围 |
描述 |
|---|---|
|
浮点数或整数。 |
# Get the absolute value of -3.7
fabs_result = math.fabs(-3.7)
brain.print(fabs_result, precision=1)
# fabs_result = 3.7
Exponents & Logarithms#
日志#
log 计算一个数的对数并返回一个浮点数。
Usage:
math.log(x, base)
范围 |
描述 |
|---|---|
|
一个正浮点数或整数。 |
|
可选。使用的对数底数。默认值为自然对数。 |
# Calculate the natural logarithm
# (base e) of 7.389056
log_result = math.log(7.389056)
brain.print(log_result)
# log_result = 2.00
# Calculate the log base 2 of 8
log_result = math.log(8, 2)
brain.print(log_result)
# log_result = 3.00
10 行#
log10 计算一个数的以 10 为底的对数,并返回一个浮点数。
Usage:
math.log10(x)
范围 |
描述 |
|---|---|
|
一个正浮点数或整数。 |
# Calculate the log base 10 of 100
log_result = math.log10(100)
brain.print(log_result)
# log_result = 2.00
火力#
pow 将 x 提升到 y 次方并返回一个浮点数,即使两个输入都是整数。
Usage:
math.pow(x, y)
范围 |
描述 |
|---|---|
|
浮点数或整数作为基数。 |
|
浮点数或整数指数。 |
# Calculate 2 raised to the power of 3
power_result = math.pow(2, 3)
brain.print(power_result)
# power_result = 8.00
平方根#
sqrt 计算一个数的平方根并返回一个浮点数,即使是完全平方数也是如此。
Usage:
math.sqrt(x)
范围 |
描述 |
|---|---|
|
非负浮点数或整数。 |
# Calculate the square root of 16
square_root = math.sqrt(16)
brain.print(square_root)
# square_root = 4.00
经验#
exp 计算一个数的指数并返回一个浮点数。
Usage:
math.exp(x)
范围 |
描述 |
|---|---|
|
浮点数或整数。 |
# Calculate e raised to the power of 1
exp_result = math.exp(1)
brain.print(exp_result, precision=2)
# exp_result = 2.72
Floating Point Operations#
modf#
modf 将一个数分解成它的小数部分和整数部分,并返回一个元组 (fractional part, integer part),两者都是浮点数。
Usage:
math.modf(x)
范围 |
描述 |
|---|---|
|
要分解的浮点数或整数。 |
# Decompose 3.14159 into fractional and integer parts
fractional_part, integer_part = math.modf(3.14159)
brain.print(fractional_part, precision=2)
brain.print(integer_part)
# fractional_part = 0.14
# integer_part = 3
frexp#
frexp 将一个数字分解为它的尾数和指数,并返回一个元组 (mantissa, exponent),其中尾数是一个浮点数,指数是一个整数。
Usage:
math.frexp(x)
范围 |
描述 |
|---|---|
|
要分解的浮点数或整数。 |
# Decompose 16 into its mantissa and exponent
mantissa, exponent = math.frexp(16)
brain.print(mantissa, precision=2)
brain.print(exponent)
# mantissa = 0.50
# exponent = 5
fmod#
fmod 返回除法的余数,同时保留被除数的符号(x)。
Usage:
math.fmod(x, y)
范围 |
描述 |
|---|---|
|
分红。 |
|
除数。 |
# Calculate remainder of 10 / 3
# that preserves the sign of 10
fmod_result = math.fmod(10, 3)
brain.print(fmod_result)
# fmod_result = 1
版权签名#
copysign 返回带有 y的 x。
Usage:
math.copysign(x, y)
范围 |
描述 |
|---|---|
|
要修改的值。 |
|
要复制其符号的值。 |
# Return -10 with the sign of 3 (positive)
copysign_result = math.copysign(-10, 3)
brain.print(copysign_result)
# copysign_result = 10
ldexp#
ldexp 计算 x * (2 ** exp),这等价于 x * 2exp。
Usage:
math.ldexp(x, exp)
范围 |
描述 |
|---|---|
|
基准值。 |
|
指数。 |
# Compute 3 * (2 ** 4)
ldexp_result = math.ldexp(3, 4)
brain.print(ldexp_result)
# ldexp_result = 48