Matemáticas#

Introducción#

The C++ standard library provides basic math functionality through {code}, which is supported in VEXcode IQ (2nd gen). These functions let you perform common mathematical operations such as trigonometry, rounding, exponentiation, and more.

Úsalos para calcular posiciones, ángulos, distancias o realizar conversiones entre grados y radianes.

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

Funciones matemáticas comunes – Herramientas matemáticas comunes.

  • abs – Returns the absolute value of a number.

  • fmin – Returns the smallest of the input values.

  • fmax – Returns the largest of the input values.

  • pow – Raises a number to a power.

Constants – Predefined values from {code}.

  • M_PI – The constant π (pi).

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

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

  • sin – Sine of an angle in radians.

  • cos – Cosine of an angle in radians.

  • tan – Tangent of an angle in radians.

  • atan – Arctangent of a value in radians.

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

  • asin – Arcsine of a value in radians.

  • acos – Arccosine of a value in radians.

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

  • ceil – Rounds up to the nearest integer.

  • floor – Rounds down to the nearest integer.

  • trunc – Removes the decimal portion.

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

  • sqrt – Returns the square root.

  • exp – Calculates e to the power of x.

  • log – Natural logarithm (base e).

  • log10 – Base-10 logarithm.

Operaciones de punto flotante: inspeccionar o descomponer valores flotantes.

  • fmod – Remainder with sign of the dividend.

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

Funciones matemáticas comunes#

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.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Display the absolute value of -10
  Brain.Screen.print("%d", abs(-10));

  // Output: 10
}

fmin#

fmin returns the lower of two values as a float.

Usage:
fmin(value1, value2)

Parámetro

Descripción

value1

El primer valor a comparar.

value2

El segundo valor a comparar.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Display the lower of two values
  Brain.Screen.print("%f", fmin(7, 4));

  // Output: 4.000000
}

fmax#

fmax returns the higher of two values as a float.

Usage:
fmax(value1, value2)

Parámetro

Descripción

value1

El primer valor a comparar.

value2

El segundo valor a comparar.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Display the higher of two values
  Brain.Screen.print("%f", fmax(7, 4));

  // Output: 7.000000
}

pow#

pow returns a number raised to a power as a float.

Usage:
pow(base, exponent)

Parámetro

Descripción

base

El número base.

exponent

El exponente.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Display the lower of two values
  Brain.Screen.print("%f", pow(2, 3));

  // Output: 8.000000
}

Constantes#

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

M_PI#

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

Usage:
M_PI

int main() {
  vexcodeInit();

  // Calculate the area of a circle with radius 5
  double area = M_PI * 5 * 5;

  Brain.Screen.print("%f", area);  
  
  // Output: 78.539816
}

M_E#

M_E gives the base of the natural logarithm.

Usage:
M_E

int main() {
  vexcodeInit();

  // Calculate e raised to the power of 2
  double result = pow(M_E, 2);

  Brain.Screen.print("%f", result);  
  
  // Output: 7.389056
}

Trigonometría#

sin#

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

Usage:
sin(x)

Parámetro

Descripción

x

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

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Convert 30 degrees to radians
  double radians = 30 * M_PI / 180.0;

  // Display the sine of 30 degrees
  Brain.Screen.print("%f", sin(radians));

  // Output: 0.500000
}

cos#

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

Usage:
sin(x)

Parámetro

Descripción

x

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

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Convert 60 degrees to radians
  double radians = 60 * M_PI / 180.0;

  // Display the cosine of 60 degrees
  Brain.Screen.print("%f", cos(radians));

  // Output: 0.500000
}

tan#

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

Usage:
tan(x)

Parámetro

Descripción

x

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

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Convert 45 degrees to radians
  double radians = 45 * M_PI / 180.0;

  // Compute tangent of 45 degrees
  Brain.Screen.print("%f", tan(radians));

  // Output: 1.000000
}

atan#

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

Usage:
atan(x)

Parámetro

Descripción

x

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

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // Calculate the arc tangent of 1 in degrees
  double degrees = atan(1.0) * 180.0 / M_PI;

  Brain.Screen.print("%f", degrees);

  // Output: 45.000000
}

atan2#

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

Usage:
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.

int main() {
  vexcodeInit();

  // Calculate the inverse tangent of 1/1 in degrees
  double degrees = atan2(1.0, 1.0) * 180.0 / M_PI;

  Brain.Screen.print("%f", degrees);

  // Output: 45.000000
}

asin#

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

Usage:
asin(x)

Parámetro

Descripción

x

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

int main() {
  vexcodeInit();

  // Calculate the arc sine of 0.5 in degrees
  double degrees = asin(0.5) * 180.0 / M_PI;

  Brain.Screen.print("%f", degrees);
  
  // Output: 30.000000
}

acos#

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

Usage:
acos(x)

Parámetro

Descripción

x

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

int main() {
  vexcodeInit();

  // Calculate the arc cosine of 0.5 in degrees
  double degrees = acos(0.5) * 180.0 / M_PI;

  Brain.Screen.print("%f", degrees);  
  
  // Output: 60.000000
}

Redondeo y valor absoluto#

ceil#

ceil returns a number rounded up as a float.

Usage:
ceil(x)

Parámetro

Descripción

x

Una carroza para ser redondeada.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Round up 9.7
  Brain.Screen.print("%f", ceil(9.7));

  // Output: 10.000000
}

floor#

floor returns a number rounded down as a float.

Usage:
floor(x)

Parámetro

Descripción

x

Un flotante que debe redondearse hacia abajo.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Round down 9.7
  Brain.Screen.print("%f", floor(9.7));

  // Output: 9.000000
}

trunc#

trunc returns a number as a float, with its decimal portion set to zero.

Usage:
trunc(x)

Parámetro

Descripción

x

Un flotador que debe truncarse.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Truncate 9.7
  Brain.Screen.print("%f", trunc(9.7));

  // Output: 9.000000
}

Exponentes y logaritmos#

sqrt#

sqrt returns the square root of a number as a float.

Usage:
sqrt(x)

Parámetro

Descripción

x

Un número entero o flotante no negativo.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Display the square root of 121
  Brain.Screen.print("%f", sqrt(121));

  // Output: 11.000000
}

exp#

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

Usage:
exp(x)

Parámetro

Descripción

x

Un flotante o entero.

int main() {
  vexcodeInit();

  // Calculate e raised to the power of 1
  double result = exp(1);

  Brain.Screen.print("%f", result);
  
  // Output: 2.718282
}

log#

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

Usage:
log(x)

Parámetro

Descripción

x

Un número entero o flotante positivo.

int main() {
  vexcodeInit();

  // Calculate the natural logarithm (base e) of 7.389056
  double result = log(M_E);

  Brain.Screen.print("%f", result);  
  
  // Output: 1.000000
}

log10#

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.

int main() {
  vexcodeInit();

  // Calculate the base-10 logarithm of 1000
  double result = log10(1000);

  Brain.Screen.print("%f", result);  
  
  // Output: 3.000000
}

Operaciones de punto flotante#

fmod#

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

Usage:
fmod(x, y)

Parámetro

Descripción

x

El dividendo.

y

El divisor.

int main() {
  vexcodeInit();

  // Calculate remainder of 10 / -3
  // that preserves the sign of 10
  double result = fmod(10.0, -3.0);

  Brain.Screen.print("%f", result);  
  
  // Output: 1.000000
}

copysign#

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á.

int main() {
  vexcodeInit();

  // Return -10 with the sign of 3 (positive)
  double result = copysign(10, -3);

  Brain.Screen.print("%f", result);  
  
  // Output: -10.000000
}