数学#
介绍#
Math 包含内置 Python 函数和完整的“math”模块,后者在 VEXcode IQ(第二代)中自动启用。这些工具允许您执行从基本算术到高级三角学、舍入和对数运算的所有操作。
使用这些函数和常量来计算机器人的位置、角度、距离和其他数值。您还可以在角度和弧度之间转换、计算表达式的值,以及处理无穷大和 NaN 等特殊值。
以下是可用的数学函数、常量和实用程序的列表:
内置函数——Python 附带的常见数学工具。
abs – 返回数字的绝对值。
round – 将数字四舍五入到指定的小数位数。
min – 返回输入值的最小值。
max – 返回输入值的最大值。
sum —— 将可迭代对象中的所有值相加。
divmod – 以元组形式返回商和余数。
pow – 对数字进行幂运算,可选择使用模数。
int – 将值转换为整数。
float – 将值转换为浮点数。
常量 – 来自“数学”模块的预定义值。
math.pi – 常数 π (圆周率)。
math.tau – 常数 tau (2π)。
math.e – 欧拉数,自然对数的底数。
math.inf – 正无穷大。
math.nan – 非数字 (NaN)。
三角学——计算角度和边之间的关系。
math.sin – 以弧度为单位的角的正弦。
math.cos – 以弧度为单位的角度的余弦。
math.tan – 以弧度为单位的角的正切。
math.atan – 弧度值的反正切。
math.atan2 – 考虑象限,以弧度为单位计算 y/x 的反正切。
math.asin – 弧度值的反正弦。
math.acos – 弧度值的反余弦。
math.degrees – 将弧度转换为度数。
math.radians – 将度数转换为弧度。
双曲线——高级三角相关函数。
math.sinh – 某个值的双曲正弦。
math.cosh – 某个值的双曲余弦。
math.tanh – 某个值的双曲正切。
math.asinh – 反双曲正弦。
math.acosh – 反双曲余弦。
math.atanh – 反双曲正切。
舍入和绝对值——调整精度或方向。
math.ceil – 向上舍入到最接近的整数。
math.floor – 向下舍入到最接近的整数。
math.trunc – 删除小数部分。
math.fabs – 以浮点数形式返回绝对值。
指数和对数——幂、根和对数计算。
math.pow – 对数字进行幂运算。
math.sqrt – 返回平方根。
math.exp – 计算 e 的 x 次方。
math.log – 自然对数(底数为 e)。
math.log10 – 以 10 为底的对数。
math.log2 – 以 2 为底的对数。
math.factorial – 整数的阶乘。
math.expm1 –
e^x - 1
的更准确结果。
浮点运算——检查或分解浮点值。
math.modf – 返回浮点数的小数部分和整数部分。
math.frexp – 将数字分解为尾数和指数。
math.fmod – 被除数的余数(带符号)。
math.copysign – 返回带有另一个值的符号的值。
math.ldexp – 计算
x * (2 ** exp)
。
比较与近似——检查具有公差或类别的值。
math.isclose – 测试两个值是否近似相等。
math.isfinite – 检查一个数字是否有限。
math.isinf – 检查一个数字是否为无限。
math.isnan – 检查一个值是否为 NaN。
误差与伽马——特殊的数学函数。
math.gamma – Gamma 函数(广义阶乘)。
math.lgamma – 对数伽马。
math.erf – 误差函数。
math.erfc – 互补误差函数。
内置函数#
Python 提供了几个内置函数,允许您在项目内部执行数学运算。
腹肌#
abs
返回数字的绝对值,删除任何负号。
用法:
abs(x)
范围 |
描述 |
---|---|
|
整数或浮点数。 |
# Get the absolute value of -10
abs_result = abs(-10)
brain.screen.print(abs_result)
# abs_result = 10
圆形的#
round
将数字四舍五入到指定的小数位数。
用法:
round(x, ndigits)
范围 |
描述 |
---|---|
|
整数或浮点数。 |
|
可选。要四舍五入的小数位数。默认值为 0。 |
# Round 5.7 to the nearest integer
round_int_result = round(5.7)
brain.screen.print(round_int_result)
# round_int_result = 6
# Round 3.14159 to 2 decimal places
round_result = round(3.14159, 2)
brain.screen.print(round_result)
# round_result = 3.14
分钟#
min
从多个参数或可迭代对象中返回最小值。
用法:
min(arg1, arg2, ...)
或 min(sequence)
范围 |
描述 |
---|---|
|
要比较的数字。 |
|
包含数字的列表、元组或其他序列。 |
# Get the smallest number from 3, 7, and 1
min_result = min(3, 7, 1)
brain.screen.print(min_result)
# min_result = 1
# Get the smallest value from a list
min_list_result = min([10, 4, 25, 1])
brain.screen.print(min_list_result)
# min_list_result = 1
最大限度#
max
从多个参数或可迭代对象中返回最大值。
用法:
max(arg1, arg2, ...)
或 max(sequence)
范围 |
描述 |
---|---|
|
要比较的数字。 |
|
包含数字的列表、元组或其他序列。 |
# Get the largest number from 3, 7, and 1
max_result = max(3, 7, 1)
brain.screen.print(max_result)
# max_result = 7
# Get the largest value from a list
max_list_result = max([10, 4, 25, 1])
brain.screen.print(max_list_result)
# max_list_result = 25
和#
sum
将可迭代对象中的所有值相加,并带有可选的起始值。
用法:
sum(sequence, start)
范围 |
描述 |
---|---|
|
包含数字的列表、元组或其他序列。 |
|
可选。要添加到总和的值。默认值为 0。 |
# Calculate the sum of a list of numbers
sum_result = sum([1, 2, 3, 4, 5])
brain.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)
brain.screen.print(sum_with_start)
# sum_with_start = 16
divmod#
divmod
返回一个包含除法运算的商和余数的元组。
用法:
divmod(a,b)
范围 |
描述 |
---|---|
|
股息。 |
|
除数。 |
# Perform integer division and remainder of 10 / 3
divmod_result = divmod(10, 3)
brain.screen.print(divmod_result)
# divmod_result = (3, 1)
战俘#
pow
将数字提升为幂,并可选择执行模数运算。
用法:
pow(x, y, mod)
范围 |
描述 |
---|---|
|
基数。 |
|
指数。 |
|
可选。模数值。如果提供,则返回 |
# Calculate 2 raised to the power of 3
pow_result = pow(2, 3)
brain.screen.print(pow_result)
# pow_result = 8
整数#
int
将数字或字符串转换为整数。从字符串转换时,它还支持进制转换。
用法:
int(x, base)
范围 |
描述 |
---|---|
|
要转换的数字、字符串或其他对象。 |
|
可选。用于转换的进制数。默认值为 10。 |
# Convert a float to an integer to get rid of decimals
price = 19.99
price_int = int(price)
brain.screen.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.screen.print(user_number * 2)
# Output: 110
漂浮#
float
将数字或字符串转换为浮点数。
用法:
float(x)
范围 |
描述 |
---|---|
|
要转换的数字、字符串或其他对象。 |
# Convert division result to a float
num_apples = 6
num_people = 2
apples_per_person = float(num_apples) / num_people
brain.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)
brain.screen.print(user_number * 3)
# Output: 70.20
数学模块#
MicroPython 中的 math 模块提供了执行常见数学计算的附加方法。这些方法包括三角函数、对数和其他数值运算。
VEXcode 中默认导入 math
模块。
常量#
常量是预定义的值,在项目执行过程中保持不变。它们可以在计算中使用,无需任何定义或赋值。
圆周率#
pi
给出数学常数 π,即圆的周长与其直径的比率。
用法:
math.pi
# Calculate the area of a circle with radius 5 using pi
circle_area = math.pi * 5 * 5
brain.screen.print(circle_area)
# circle_area = 78.54
e#
e
给出自然对数的底数。
用法:
math.e
# Calculate e raised to the power of 2
e_power = math.pow(math.e, 2)
brain.screen.print(e_power)
# e_power = 7.39
三角学#
罪#
sin
计算弧度为单位的角度的正弦并返回浮点数。
用法:
math.sin(x)
范围 |
描述 |
---|---|
|
表示弧度角的浮点数或整数。 |
# Calculate the sine of 30 degrees in radians
sine_result = math.sin(math.radians(30))
brain.screen.print(sine_result)
# sine_result = 0.50
余弦#
cos
计算弧度为单位的角度的余弦并返回浮点数。
用法:
math.cos(x)
范围 |
描述 |
---|---|
|
表示弧度角的浮点数或整数。 |
# Calculate the cosine of 60 degrees in radians
cosine_result = math.cos(math.radians(60))
brain.screen.print(cosine_result)
# cosine_result = 0.50
棕褐色#
tan
计算弧度为单位的角度的正切并返回浮点数。
用法:
math.tan(x)
范围 |
描述 |
---|---|
|
表示弧度角的浮点数或整数。 |
# Calculate the tangent of 45 degrees in radians
tangent_result = math.tan(math.radians(45))
brain.screen.print(tangent_result)
# tangent_result = 1.00
阿坦#
atan
计算数字的反正切(反正切)并返回以弧度表示角度的浮点数。
用法:
math.atan(x)
范围 |
描述 |
---|---|
|
浮点数或整数。 |
# Calculate the arc tangent of 1 in degrees
arc_tangent_result = math.degrees(math.atan(1.0))
brain.screen.print(arc_tangent_result)
# arc_tangent_result = 45.00
反义词#
atan2
计算 y/x 的反正切的主值并返回以弧度表示角度的浮点数。
用法:
math.atan2(y,x)
范围 |
描述 |
---|---|
|
表示 y 坐标的浮点数或整数。 |
|
表示 x 坐标的浮点数或整数。 |
# Calculate the inverse tangent of 1/1 in degrees
atan2_result = math.degrees(math.atan2(1.0, 1.0))
brain.screen.print(atan2_result)
# atan2_result = 45.00
阿辛#
asin
计算数字的反正弦(反正弦)并返回以弧度表示角度的浮点数。
用法:
math.asin(x)
范围 |
描述 |
---|---|
|
-1 到 1 之间的浮点数或整数。 |
# Calculate the arc sine of 0.5 in degrees
arc_sine_result = math.degrees(math.asin(0.5))
brain.screen.print(arc_sine_result)
# arc_sine_result = 30.00
阿科斯#
acos
计算数字的反余弦(反余弦)并返回以弧度表示角度的浮点数。
用法:
math.acos(x)
范围 |
描述 |
---|---|
|
-1 到 1 之间的浮点数或整数。 |
# Calculate the arc cosine of 0.5 in degrees
arc_cosine_result = math.degrees(math.acos(0.5))
brain.screen.print(arc_cosine_result)
# arc_cosine_result = 60.00
度#
“度”将角度从弧度转换为度。
用法:
math.degrees(x)
范围 |
描述 |
---|---|
|
表示弧度角的浮点数或整数。 |
# Convert pi radians to degrees
degrees_result = math.degrees(math.pi)
brain.screen.print(degrees_result)
# degrees_result = 180.00
弧度#
“弧度”将角度从度数转换为弧度。
用法:
math.radians(x)
范围 |
描述 |
---|---|
|
表示角度(以度为单位)的浮点数或整数。 |
# Convert 180 degrees to radians
radians_result = math.radians(180)
brain.screen.print(radians_result)
# radians_result = 3.14
四舍五入和绝对值#
天花板#
ceil
将数字向上舍入为最接近的整数。
用法:
math.ceil(x)
范围 |
描述 |
---|---|
|
要向上舍入的浮点数或整数。 |
# Round 3.7 up to the nearest integer
ceil_result = math.ceil(3.7)
brain.screen.print(ceil_result)
# ceil_result = 4
地面#
floor
将数字向下舍入为最接近的整数。
用法:
math.floor(x)
范围 |
描述 |
---|---|
|
要向下舍入的浮点数或整数。 |
# Round 3.7 down to the nearest integer
floor_result = math.floor(3.7)
brain.screen.print(floor_result)
# floor_result = 3
截断#
trunc
删除数字的小数部分而不进行四舍五入。
用法:
math.trunc(x)
范围 |
描述 |
---|---|
|
要截断的浮点数。 |
# Remove the decimal part of 3.7
trunc_result = math.trunc(3.7)
brain.screen.print(trunc_result)
# trunc_result = 3
晶圆厂#
fabs
以浮点数形式返回数字的绝对值。
用法:
math.fabs(x)
范围 |
描述 |
---|---|
|
浮点数或整数。 |
# Get the absolute value of -3.7
fabs_result = math.fabs(-3.7)
brain.screen.print(fabs_result)
# fabs_result = 3.70
指数和对数#
战俘#
pow
将 x 计算 y 次方并返回浮点数,即使两个输入都是整数。
用法:
math.pow(x, y)
范围 |
描述 |
---|---|
|
浮点数或整数基数。 |
|
浮点数或整数指数。 |
# Calculate 2 raised to the power of 3
power_result = math.pow(2, 3)
brain.screen.print(power_result)
# power_result = 8.00
平方根#
sqrt
计算一个数字的平方根并返回一个浮点数,即使是完全平方数。
用法:
math.sqrt(x)
范围 |
描述 |
---|---|
|
非负浮点数或整数。 |
# Calculate the square root of 16
sqrt_result = math.sqrt(16)
brain.screen.print(sqrt_result)
# sqrt_result = 4.00
经验值#
exp
计算一个数字的指数并返回一个浮点数。
用法:
math.exp(x)
范围 |
描述 |
---|---|
|
浮点数或整数。 |
# Calculate e raised to the power of 1
exp_result = math.exp(1)
brain.screen.print(exp_result)
# exp_result = 2.72
日志#
log
计算数字的自然对数并返回浮点数。
用法:
math.log(x)
范围 |
描述 |
---|---|
|
正浮点数或整数。 |
# Calculate the natural logarithm (base e) of 7.389056
log_result = math.log(7.389056)
brain.screen.print(log_result)
# log_result = 2.00
浮点运算#
修改#
modf
将数字分解为小数部分和整数部分,并返回一个元组 (小数部分,整数部分)
,两者都是浮点数。
用法:
math.modf(x)
范围 |
描述 |
---|---|
|
要分解的浮点数或整数。 |
# Decompose 3.14159 into fractional and integer parts
fractional_part, integer_part = math.modf(3.14159)
brain.screen.print(fractional_part)
brain.screen.next_row()
brain.screen.print(integer_part)
# fractional_part = 0.14
# integer_part = 3.00
frexp#
frexp
将数字分解为尾数和指数,并返回一个元组 (尾数,指数)
,其中尾数是浮点数,指数是整数。
用法:
math.frexp(x)
范围 |
描述 |
---|---|
|
要分解的浮点数或整数。 |
# Decompose 16 into its mantissa and exponent
mantissa, exponent = math.frexp(16)
brain.screen.print(mantissa)
brain.screen.next_row()
brain.screen.print(exponent)
# mantissa = 0.50
# exponent = 5
fmod#
fmod
返回除法的余数,同时保留被除数的符号(x
)。
用法:
math.fmod(x,y)
范围 |
描述 |
---|---|
|
股息。 |
|
除数。 |
# Calculate remainder of 10 / 3
# that preserves the sign of 10
fmod_result = math.fmod(10, 3)
brain.screen.print(fmod_result)
# fmod_result = 1.00
版权声明#
copysign
返回带有 y
符号的 x。
用法:
math.copysign(x, y)
范围 |
描述 |
---|---|
|
要修改的值。 |
|
其符号将被复制的值。 |
# Return -10 with the sign of 3 (positive)
copysign_result = math.copysign(-10, 3)
brain.screen.print(copysign_result)
# copysign_result = 10.00
ldexp#
ldexp
计算 x * (2 ** exp)
,等价于 `x * 2exp。
用法:
math.ldexp(x, exp)
范围 |
描述 |
---|---|
|
基值。 |
|
指数。 |
# Compute 3 * (2 ** 4)
ldexp_result = math.ldexp(3, 4)
brain.screen.print(ldexp_result)
# ldexp_result = 48.00
比较与近似#
是有限的#
isfinite
检查一个数字是否有限。此方法返回一个布尔值:
True
- 该数字是有限的。False
- 该数字是无限的。
用法:
math.isfinite(x)
范围 |
描述 |
---|---|
|
要检查的浮点数或整数。 |
# Check if 42 is a finite number (returns True)
is_finite_true = math.isfinite(42)
brain.screen.print(is_finite_true)
# is_finite_true = True
信息#
isinf
检查一个数字是否为无穷大。此方法返回一个布尔值:
True
- 该数字是无限的。False
- 该数字是有限的。
用法:
math.isinf(x)
范围 |
描述 |
---|---|
|
要检查的浮点数或整数。 |
# Check if pi is an infinite number (returns False)
is_inf_false = math.isinf(math.pi)
brain.screen.print(is_inf_false)
# is_inf_true = False
伊斯南#
isnan
检查一个数字是否为 NaN(非数字)。此方法返回一个布尔值:
True
- 该数字为 NaN。False
- 该数字是有效数字。
用法:
math.isnan(x)
范围 |
描述 |
---|---|
|
要检查的浮点数或整数。 |
# Check if 1 is NaN (returns False)
brain.screen.print(is_nan_true)
# is_nan_true = False