数学#

介绍#

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

使用这些函数和常量来计算机器人的位置、角度、距离和其他数值。您还可以在角度和弧度之间转换、计算表达式的值,以及处理无穷大和 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.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.pow – Raises a number to a power.

  • math.sqrt – Returns the square root.

  • math.exp – Calculates e to the power of x.

  • math.log – Calculate the logarithm of x.

  • math.log10 – Calculates the base-10 logarithm of x.

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

  • 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

整数或浮点数。

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

# abs_result = 10

# Start threads — Do not delete
start_thread(main)

round#

round 将数字四舍五入到指定的小数位数。

用法:
round(x, ndigits)

范围

描述

x

整数或浮点数。

ndigits

可选。要四舍五入的小数位数。默认值为 0。

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

# round_int_result = 6

# Start threads — Do not delete
start_thread(main)

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

# round_result = 3.14

# Start threads — Do not delete
start_thread(main)

min#

min 返回多个参数或可迭代对象中的最小值。

用法:
min(arg1, arg2, …)min(sequence)

范围

描述

arg1, arg2, …

要比较的数字。

sequence

包含数字的列表、元组或其他序列。

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

# min_result = 1

# Start threads — Do not delete
start_thread(main)

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

# min_list_result = 1

# Start threads — Do not delete
start_thread(main)

max#

max 返回多个参数或可迭代对象的最大值。

用法:
max(arg1, arg2, …)max(sequence)

范围

描述

arg1, arg2, …

要比较的数字。

sequence

包含数字的列表、元组或其他序列。

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

# max_result = 7

# Start threads — Do not delete
start_thread(main)

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

# max_list_result = 25

# Start threads — Do not delete
start_thread(main)

sum#

sum 将可迭代对象中的所有值相加,并带有可选的起始值。

用法:
sum(sequence, start)

范围

描述

sequence

包含数字的列表、元组或其他序列。

start

可选。要添加到总和的值。默认值为 0。

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

# sum_result = 15

# Start threads — Do not delete
start_thread(main)

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

# sum_with_start = 16

# Start threads — Do not delete
start_thread(main)

divmod#

divmod 返回包含除法运算的商和余数的元组。

用法:
divmod(a, b)

范围

描述

a

股息。

b

除数。

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

# divmod_result = (3, 1)

# Start threads — Do not delete
start_thread(main)

pow#

pow 将数字提升为幂,并可选地执行模数运算。

用法:
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)
    console.print(pow_result)

# pow_result = 8

# Start threads — Do not delete
start_thread(main)

int#

int 将数字或字符串转换为整数。从字符串转换时,它也支持进制转换。

用法:
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)
    console.print("{} coins".format(price_int))

# Output: 19 coins

# Start threads — Do not delete
start_thread(main)

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

# Output: 110

# Start threads — Do not delete
start_thread(main)

数学模块#

MicroPython 中的 math 模块提供了执行常见数学计算的附加方法。这些方法包括三角函数、对数和其他数值运算。

math 模块在 VEXcode 中默认导入。

Constants#

常量是预定义的值,在项目执行过程中保持不变。它们可以在计算中使用,无需任何定义或赋值。

圆周率#

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

用法:
math.pi

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

# circle_area = 78.54

# Start threads — Do not delete
start_thread(main)

tau#

tau 给出 2π 的值。

用法:
math.tau

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

# circumference = 31.42

# Start threads — Do not delete
start_thread(main)

e#

e 给出自然对数的底数。

用法:
math.e

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

# e_power = 7.39

# Start threads — Do not delete
start_thread(main)

Trigonometry#

#

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

用法:
math.sin(x)

范围

描述

x

表示弧度角的浮点数或整数。

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

# result = 0.50

# Start threads — Do not delete
start_thread(main)

余弦#

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

用法:
math.cos(x)

范围

描述

x

表示弧度角的浮点数或整数。

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

# result = 0.50

# Start threads — Do not delete
start_thread(main)

棕褐色#

tan calculates the tangent of an angle in radians and returns an integer.

用法:
math.tan(x)

范围

描述

x

表示弧度角的浮点数或整数。

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

# result = 1

# Start threads — Do not delete
start_thread(main)

阿辛#

asin calculates the inverse sine (arc sine) of a number and returns an integer representing the angle in radians.

用法:
math.asin(x)

范围

描述

x

-1 到 1 之间的浮点数或整数。

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

# result = 30

# Start threads — Do not delete
start_thread(main)

阿科斯#

acos calculates the inverse cosine (arc cosine) of a number and returns an integer representing the angle in radians.

用法:
math.acos(x)

范围

描述

x

-1 到 1 之间的浮点数或整数。

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

# result = 60

# Start threads — Do not delete
start_thread(main)

阿坦#

atan calculates the inverse tangent (arc tangent) of a number and returns an integer representing the angle in radians.

用法:
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)
    console.print(result)

# result = 45

# Start threads — Do not delete
start_thread(main)

反义词#

atan2 计算 y/x 的反正切的主值并返回以弧度表示角度的浮点数。

用法:
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)
    console.print(result)

# result = 45

# Start threads — Do not delete
start_thread(main)

#

degrees converts an angle from radians to degrees and returns an integer.

用法:
math.degrees(x)

范围

描述

x

表示弧度角的浮点数或整数。

def main():
    # Convert pi radians to degrees
    degrees_result = math.degrees(math.pi)
    console.print(degrees_result)

# degrees_result = 180

# Start threads — Do not delete
start_thread(main)

弧度#

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

用法:
math.radians(x)

范围

描述

x

表示角度(以度为单位)的浮点数或整数。

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

# radians_result = 3.14

# Start threads — Do not delete
start_thread(main)

Rounding & Absolute Values#

天花板#

ceil 将数字向上舍入为最接近的整数。

用法:
math.ceil(x)

范围

描述

x

要向上舍入的浮点数或整数。

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

# ceil_result = 4

# Start threads — Do not delete
start_thread(main)

地面#

floor 将数字向下舍入为最接近的整数。

用法:
math.floor(x)

范围

描述

x

要向下舍入的浮点数或整数。

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

# floor_result = 3

# Start threads — Do not delete
start_thread(main)

截断#

trunc 删除数字的小数部分,不进行四舍五入。

用法:
math.trunc(x)

范围

描述

x

要截断的浮点数。

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

# trunc_result = 3

# Start threads — Do not delete
start_thread(main)

晶圆厂#

fabs 以浮点数形式返回数字的绝对值。

用法:
math.fabs(x)

范围

描述

x

浮点数或整数。

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

# fabs_result = 3.7

# Start threads — Do not delete
start_thread(main)

Exponents & Logarithms#

日志#

log 计算一个数字的对数并返回一个浮点数。

用法:
math.log(x, base)

范围

描述

x

正浮点数或整数。

base

可选。要使用的对数底数。默认情况下,此参数为自然对数。

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

# log_result = 2

# Start threads — Do not delete
start_thread(main)

def main():
    # Calculate the log base 2 of 8
    log_result = math.log(8, 2)
    console.print(log_result)

# log_result = 3

# Start threads — Do not delete
start_thread(main)

log10#

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

用法:
math.log10(x)

范围

描述

x

正浮点数或整数。

def main():
    # Calculate the base-10 logarithm of 100
    log10_result = math.log10(100)
    console.print(log10_result)

# log10_result = 2

# Start threads — Do not delete
start_thread(main)

战俘#

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

用法:
math.pow(x, y)

范围

描述

x

浮点数或整数基数。

y

浮点数或整数指数。

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

    # power_result = 8

# Start threads — Do not delete
start_thread(main)

平方根#

sqrt 计算一个数字的平方根并返回一个浮点数,即使是完全平方数。

用法:
math.sqrt(x)

范围

描述

x

非负浮点数或整数。

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

# power_result = 8

# Start threads — Do not delete
start_thread(main)

经验值#

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

用法:
math.exp(x)

范围

描述

x

浮点数或整数。

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

# exp_result = 2.72

# Start threads — Do not delete
start_thread(main)

Floating Point Operations#

修改#

modf 将数字分解为小数部分和整数部分,并返回一个元组 (fractional part, integer part),两者都是浮点数。

用法:
math.modf(x)

范围

描述

x

要分解的浮点数或整数。

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

# fractional_part = 0.14
# integer_part = 3

# Start threads — Do not delete
start_thread(main)

frexp#

frexp 将数字分解为其尾数和指数,并返回一个元组 (mantissa, exponent),其中尾数是浮点数,指数是整数。

用法:
math.frexp(x)

范围

描述

x

要分解的浮点数或整数。

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

# mantissa = 0.50
# exponent = 5

# Start threads — Do not delete
start_thread(main)

fmod#

fmod returns the remainder of division while keeping the sign of the dividend (x).

用法:
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)
    console.print(fmod_result)

# fmod_result = 1

# Start threads — Do not delete
start_thread(main)

版权声明#

copysign 返回带有 y符号的 x 。

用法:
math.copysign(x, y)

范围

描述

x

要修改的值。

y

其符号将被复制的值。

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

# copysign_result = 10

# Start threads — Do not delete
start_thread(main)

ldexp#

ldexp computes x * (2 ** exp), which is equivalent to x * 2exp.

用法:
math.ldexp(x, exp)

范围

描述

x

基值。

exp

指数。

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

# ldexp_result = 48

# Start threads — Do not delete
start_thread(main)