Matemáticas#

Introducción#

Math includes both built-in Python functions and the full math module, which is automatically available in VEXcode GO. These tools allow you to perform everything from basic arithmetic to advanced trigonometry, rounding, and logarithmic operations.

Usa estas funciones y constantes para calcular posiciones, ángulos, distancias y otros valores numéricos para tu robot. También puedes convertir entre grados y radianes, evaluar expresiones y trabajar con valores especiales como infinito y NaN.

A continuación se muestra una lista de funciones matemáticas, constantes y utilidades disponibles:

Funciones integradas: herramientas matemáticas comunes incluidas con Python.

  • abs – Devuelve el valor absoluto de un número.

  • round – Redondea un número a una cantidad específica de decimales.

  • min – Devuelve el valor de entrada más pequeño.

  • max – Devuelve el mayor de los valores de entrada.

  • suma – Suma todos los valores de un iterable.

  • divmod – Devuelve el cociente y el resto como una tupla.

  • pow – Eleva un número a una potencia, opcionalmente con un módulo.

  • int – Convierte un valor en un entero.

  • float – Convierte un valor en un número de punto flotante.

Constants – Predefined values from the math module.

  • math.pi – La constante π (pi).

  • math.tau – La constante tau (2π).

  • math.e – Número de Euler, base del logaritmo natural.

Trigonometría – Calcular ángulos y relaciones entre lados.

  • math.sin – Seno de un ángulo en radianes.

  • math.cos – Coseno de un ángulo en radianes.

  • math.tan – Tangente de un ángulo en radianes.

  • math.atan – Arcotangente de un valor en radianes.

  • math.atan2 – Arcotangente de y/x en radianes, considerando el cuadrante.

  • math.asin – Arcoseno de un valor en radianes.

  • math.acos – Arcocoseno de un valor en radianes.

  • math.degrees – Convierte radianes a grados.

  • math.radians – Convierte grados a radianes.

Redondeo y valor absoluto: ajuste la precisión o la dirección.

  • math.ceil – Redondea al entero más cercano.

  • math.floor – Redondea hacia abajo al entero más cercano.

  • math.trunc – Elimina la parte decimal.

  • math.fabs – Devuelve el valor absoluto como un flotante.

Exponentes y logaritmos: cálculos de potencia, raíz y logaritmo.

  • math.pow – Eleva un número a una potencia.

  • math.sqrt – Devuelve la raíz cuadrada.

  • math.exp – Calcula e elevado a x.

  • math.log – Calcula el logaritmo de x.

  • math.log10 – calcula el logaritmo en base 10 de x.

Operaciones de punto flotante: inspeccionar o descomponer valores flotantes.

  • math.modf – Devuelve las partes fraccionarias y enteras de un flotante.

  • math.frexp – Descompone un número en mantisa y exponente.

  • math.fmod – Resto con signo del dividendo.

  • math.copysign – Devuelve un valor con el signo de otro.

  • math.ldexp – Computes x * (2 ** exp).

Funciones integradas#

Python proporciona varias funciones integradas que le permiten realizar operaciones matemáticas dentro de su proyecto.

abs#

abs returns the absolute value of a number, removing any negative sign.

Usage:
abs(x)

Parámetro

Descripción

x

Un número entero o flotante.

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

# abs_result = 10

# Start threads — Do not delete
start_thread(main)

round#

round rounds a number to a specified number of decimal places.

Usage:
round(x, ndigits)

Parámetro

Descripción

x

Un número entero o flotante.

ndigits

Opcional. El número de decimales a redondear. El valor predeterminado es 0.

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

# 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)
    console.new_line()

# round_result = 3.14

# Start threads — Do not delete
start_thread(main)

min#

min returns the smallest value from multiple arguments or an iterable.

Usage:
min(arg1, arg2, …) or min(sequence)

Parámetro

Descripción

arg1, arg2, …

Los números para comparar.

sequence

Una lista, tupla u otra secuencia que contiene números.

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

# 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)
    console.new_line()

# min_list_result = 1

# Start threads — Do not delete
start_thread(main)

max#

max returns the largest value from multiple arguments or an iterable.

Usage:
max(arg1, arg2, …) or max(sequence)

Parámetro

Descripción

arg1, arg2, …

Los números para comparar.

sequence

Una lista, tupla u otra secuencia que contiene números.

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

# 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)
    console.new_line()

# max_list_result = 25

# Start threads — Do not delete
start_thread(main)

sum#

sum adds up all values in an iterable, with an optional starting value.

Usage:
sum(sequence, start)

Parámetro

Descripción

sequence

Una lista, tupla u otra secuencia que contiene números.

start

Opcional. Un valor para añadir a la suma. El valor predeterminado es 0.

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

# 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)
    console.new_line()

# sum_with_start = 16

# Start threads — Do not delete
start_thread(main)

divmod#

divmod returns a tuple containing the quotient and remainder of a division operation.

Usage:
divmod(a, b)

Parámetro

Descripción

a

El dividendo.

b

El divisor.

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

# divmod_result = (3, 1)

# Start threads — Do not delete
start_thread(main)

pow#

pow raises a number to a power and optionally performs a modulus operation.

Usage:
pow(x, y, mod)

Parámetro

Descripción

x

El número base.

y

El exponente.

mod

Optional. A modulus value. If provided, returns (x ** y) % mod.

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

# pow_result = 8

# Start threads — Do not delete
start_thread(main)

int#

int converts a number or string into an integer. It also supports base conversion when converting from a string.

Usage:
int(x, base)

Parámetro

Descripción

x

Un número, cadena u otro objeto para convertir.

base

Opcional. La base numérica que se usará para la conversión. El valor predeterminado es 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))
    console.new_line()

# 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)
    console.new_line()

# Output: 110

# Start threads — Do not delete
start_thread(main)

float#

float converts a number or string into a floating-point number.

Usage:
float(x)

Parámetro

Descripción

x

Un número, cadena u otro objeto para convertir.

def main():
    # Convert division result to a float
    num_apples = 6
    num_people = 2
    apples_per_person = float(num_apples) / num_people
    console.print(apples_per_person)
    console.new_line()

# Output: 3.00

# Start threads — Do not delete
start_thread(main)

def main():
    # Convert a string into a float to use in calculations
    user_input = "23.4"
    user_number = float(user_input)
    console.print(user_number * 3)
    console.new_line()

# Output: 70.20

# Start threads — Do not delete
start_thread(main)

Módulo de Matemáticas#

The math module in MicroPython provides additional methods for performing common mathematical calculations. These methods include trigonometric, logarithmic, and other numerical operations.

The math module is imported by default in VEXcode.

Constants#

Las constantes son valores predefinidos que permanecen fijos durante un proyecto. Pueden utilizarse en cálculos sin necesidad de definirlas ni asignarlas.

pi#

pi gives the mathematical constant π, the ratio of a circle’s circumference to its diameter.

Usage:
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)
    console.new_line()

# circle_area = 78.54

# Start threads — Do not delete
start_thread(main)

tau#

tau gives the value of 2π.

Usage:
math.tau

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

# circumference = 31.42

# Start threads — Do not delete
start_thread(main)

mi#

e gives the base of the natural logarithm.

Usage:
math.e

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

# e_power = 7.39

# Start threads — Do not delete
start_thread(main)

Trigonometry#

pecado#

sin calculates the sine of an angle in radians and returns a float.

Usage:
math.sin(x)

Parámetro

Descripción

x

Un flotante o entero que representa un ángulo en radianes.

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

# result = 0.50

# Start threads — Do not delete
start_thread(main)

porque#

cos calculates the cosine of an angle in radians and returns a float.

Usage:
math.cos(x)

Parámetro

Descripción

x

Un flotante o entero que representa un ángulo en radianes.

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

# result = 0.50

# Start threads — Do not delete
start_thread(main)

broncearse#

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

Usage:
math.tan(x)

Parámetro

Descripción

x

Un flotante o entero que representa un ángulo en radianes.

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

# result = 1.00

# Start threads — Do not delete
start_thread(main)

asiático#

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

Usage:
math.asin(x)

Parámetro

Descripción

x

Un número flotante o entero entre -1 y 1.

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

# result = 30.00

# Start threads — Do not delete
start_thread(main)

acos#

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

Usage:
math.acos(x)

Parámetro

Descripción

x

Un número flotante o entero entre -1 y 1.

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

# result = 60.00

# Start threads — Do not delete
start_thread(main)

Atán#

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

Usage:
math.atan(x)

Parámetro

Descripción

x

Un flotante o entero.

def main():
    # Calculate the arc tangent 
    # of 1 in degrees
    arc_tan = math.atan(1.0)
    result = math.degrees(arc_tan)
    console.print(result)
    console.new_line()

# result = 45.00

# Start threads — Do not delete
start_thread(main)

atan2#

atan2 calculates the principal value of the inverse tangent of y/x and returns a float representing the angle in radians.

Usage:
math.atan2(y, x)

Parámetro

Descripción

y

Un número flotante o entero que representa la coordenada y.

x

Un número flotante o entero que representa la coordenada 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)
    console.new_line()

# result = 45.00

# Start threads — Do not delete
start_thread(main)

grados#

degrees converts an angle from radians to degrees.

Usage:
math.degrees(x)

Parámetro

Descripción

x

Un flotante o entero que representa un ángulo en radianes.

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

# degrees_result = 180.00

# Start threads — Do not delete
start_thread(main)

radianes#

radians converts an angle from degrees to radians.

Usage:
math.radians(x)

Parámetro

Descripción

x

Un número flotante o entero que representa un ángulo en grados.

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

# radians_result = 3.14

# Start threads — Do not delete
start_thread(main)

Rounding & Absolute Values#

fortificar techo#

ceil rounds a number up to the nearest integer.

Usage:
math.ceil(x)

Parámetro

Descripción

x

Un número flotante o entero que se redondeará hacia arriba.

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

# ceil_result = 4

# Start threads — Do not delete
start_thread(main)

piso#

floor rounds a number down to the nearest integer.

Usage:
math.floor(x)

Parámetro

Descripción

x

Un número flotante o entero que se redondeará hacia abajo.

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

# floor_result = 3

# Start threads — Do not delete
start_thread(main)

tronco#

trunc removes the decimal part of a number without rounding.

Usage:
math.trunc(x)

Parámetro

Descripción

x

Un flotador que debe truncarse.

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

# trunc_result = 3

# Start threads — Do not delete
start_thread(main)

fábricas#

fabs returns the absolute value of a number as a float.

Usage:
math.fabs(x)

Parámetro

Descripción

x

Un flotante o entero.

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

# fabs_result = 3.70

# Start threads — Do not delete
start_thread(main)

Exponents & Logarithms#

registro#

log calculates the logarithm of a number and returns a float.

Usage:
math.log(x, base)

Parámetro

Descripción

x

Un número entero o flotante positivo.

base

Opcional. La base del logaritmo que se utilizará. Por defecto, es el logaritmo natural.

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

# 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)
    console.new_line()

# log_result = 3

# Start threads — Do not delete
start_thread(main)

registro10#

log10 calculates the base-10 logarithm of a number and returns a float.

Usage:
math.log10(x)

Parámetro

Descripción

x

Un número entero o flotante positivo.

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

# log10_result = 2

# Start threads — Do not delete
start_thread(main)

poder#

pow raises x to the power of y and returns a float, even if both inputs are integers.

Usage:
math.pow(x, y)

Parámetro

Descripción

x

Una base de número flotante o entero.

y

Un exponente flotante o entero.

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

# power_result = 8.00

raíz cuadrada#

sqrt calculates the square root of a number and returns a float, even for perfect squares.

Usage:
math.sqrt(x)

Parámetro

Descripción

x

Un número entero o flotante no negativo.

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

# power_result = 8.00

# Start threads — Do not delete
start_thread(main)

exp#

exp calculates the exponential of a number and returns a float.

Usage:
math.exp(x)

Parámetro

Descripción

x

Un flotante o entero.

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

# exp_result = 2.72

# Start threads — Do not delete
start_thread(main)

Floating Point Operations#

modf#

modf decomposes a number into its fractional and integer parts and returns a tuple (fractional part, integer part), both as floats.

Usage:
math.modf(x)

Parámetro

Descripción

x

Un flotante o entero para descomponer.

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

# fractional_part = 0.14
# integer_part = 3.00

# Start threads — Do not delete
start_thread(main)

frexp#

frexp decomposes a number into its mantissa and exponent and returns a tuple (mantissa, exponent), where the mantissa is a float and the exponent is an integer.

Usage:
math.frexp(x)

Parámetro

Descripción

x

Un flotante o entero para descomponer.

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

# mantissa = 0.50
# exponent = 5

# Start threads — Do not delete
start_thread(main)

modo de funcionamiento#

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

Usage:
math.fmod(x, y)

Parámetro

Descripción

x

El dividendo.

y

El divisor.

def main():
    # Calculate remainder of 10 / 3
    # that preserves the sign of 10
    fmod_result = math.fmod(10, 3)
    console.print(fmod_result)
    console.new_line()

# fmod_result = 1.00

# Start threads — Do not delete
start_thread(main)

firma de derechos de autor#

copysign returns x with the sign of y.

Usage:
math.copysign(x, y)

Parámetro

Descripción

x

El valor a modificar.

y

El valor cuyo signo se copiará.

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

# copysign_result = 10.00

# Start threads — Do not delete
start_thread(main)

ldexp#

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

Usage:
math.ldexp(x, exp)

Parámetro

Descripción

x

El valor base.

exp

El exponente.

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

# ldexp_result = 48.00

# Start threads — Do not delete
start_thread(main)