Matemáticas#
Introducción#
The C++ standard library provides basic math functionality through <cmath>{code}, which is supported in VEXcode V5. 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 <cmath>{code}.
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.
Funciones matemáticas comunes#
abs#
Devuelve el valor absoluto de un número, eliminando cualquier signo negativo.
Available Functions1 — Devuelve el valor absoluto de un número entero.
int abs( int x );
Parameters2 — Devuelve el valor absoluto de un double.
double abs( double x );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
Un número entero o decimal. |
Devuelve el valor absoluto de la entrada como el mismo tipo.
Examples// Display the absolute value of -10
Brain.Screen.print("%d", abs(-10));
// Output: 10
fmin#
Devuelve el menor de dos valores como un número decimal.
Available Functionsdouble fmin( double value1, double value2 );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
El primer valor a comparar. |
|
|
El segundo valor a comparar. |
Returns a double representing the smaller of the two input values.
// Display the lower of two values
Brain.Screen.print("%f", fmin(7, 4));
// Output: 4.000000
fmax#
Devuelve el mayor de dos valores como un número decimal.
Available Functionsdouble fmax( double value1, double value2 );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
El primer valor a comparar. |
|
|
El segundo valor a comparar. |
Returns a double representing the larger of the two input values.
// Display the higher of two values
Brain.Screen.print("%f", fmax(7, 4));
// Output: 7.000000
pow#
Devuelve un número elevado a una potencia como un número doble.
Available Functionsdouble pow( double base, double exponent );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
El número base. |
|
|
El exponente. |
Returns a double representing the base raised to the power of the exponent.
// Display 2 to the power of 3
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.
const double M_PI;
Esta constante no tiene parámetros.
Return ValuesReturns the mathematical constant π as a double.
// 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#
La constante matemática e (número de Euler), la base del logaritmo natural.
Available Constantsconst double M_E;
Esta constante no tiene parámetros.
Return ValuesReturns the mathematical constant e as a double.
// 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#
Calcula el seno de un ángulo en radianes y devuelve un valor de tipo double.
Available Functionsdouble sin( double x );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
Un doble que representa un ángulo en radianes. |
Returns a double representing the sine of the angle.
// 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#
Calcula el coseno de un ángulo en radianes y devuelve un valor de tipo double.
Available Functionsdouble cos( double x );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
Un doble que representa un ángulo en radianes. |
Returns a double representing the cosine of the angle.
// 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#
Calcula la tangente de un ángulo en radianes y devuelve un valor de tipo double.
Available Functionsdouble tan( double x );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
Un doble que representa un ángulo en radianes. |
Returns a double representing the tangent of the angle.
// 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#
Calcula la tangente inversa (arco tangente) de un número y devuelve un valor de tipo double que representa el ángulo en radianes.
Available Functionsdouble atan( double x );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
Un valor doble. |
Returns a double representing the arc tangent in radians.
// 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#
Calcula el valor principal de la tangente inversa de y/x y devuelve un valor de tipo double que representa el ángulo en radianes.
Available Functionsdouble atan2( double y, double x );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
Un valor double que representa la coordenada y. |
|
|
Un valor double que representa la coordenada x. |
Returns a double representing the arc tangent of y/x in radians.
// 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#
Calcula el seno inverso (arco seno) de un número y devuelve un valor de tipo double que representa el ángulo en radianes.
Available Functionsdouble asin( double x );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
Un doble entre -1 y 1. |
Returns a double representing the arc sine in radians.
// 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#
Calcula el coseno inverso (arco coseno) de un número y devuelve un valor de tipo double que representa el ángulo en radianes.
Available Functionsdouble acos( double x );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
Un doble entre -1 y 1. |
Returns a double representing the arc cosine in radians.
// 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#
Devuelve un número redondeado al alza como un número de doble precisión.
Available Functionsdouble ceil( double x );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
Un doblete para redondear. |
Returns a double representing the smallest integer greater than or equal to x.
// Round up 9.7
Brain.Screen.print("%f", ceil(9.7));
// Output: 10.000000
floor#
Devuelve un número redondeado hacia abajo como un número de doble precisión.
Available Functionsdouble floor( double x );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
Un doble que se redondeará hacia abajo. |
Returns a double representing the largest integer less than or equal to x.
// Round down 9.7
Brain.Screen.print("%f", floor(9.7));
// Output: 9.000000
trunc#
Devuelve un número de tipo double, con su parte decimal establecida en cero.
Available Functionsdouble trunc( double x );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
Un doble que debe ser truncado. |
Returns a double with the decimal portion removed.
// Truncate 9.7
Brain.Screen.print("%f", trunc(9.7));
// Output: 9.000000
Exponentes y logaritmos#
sqrt#
Devuelve la raíz cuadrada de un número como un número decimal.
Available Functionsdouble sqrt( double x );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
Un doble no negativo. |
Returns a double representing the square root of x.
// Display the square root of 121
Brain.Screen.print("%f", sqrt(121));
// Output: 11.000000
exp#
Calcula la exponencial de un número y devuelve un número decimal.
Available Functionsdouble exp( double x );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
Un doble para calcular la exponencial de. |
Returns a double representing e raised to the power of x.
// Calculate e raised to the power of 1
double result = exp(1);
Brain.Screen.print("%f", result);
// Output: 2.718282
log#
Calcula el logaritmo natural de un número y devuelve un número decimal.
Available Functionsdouble log( double x );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
Un doble positivo. |
Returns a double representing the natural logarithm of x.
// Calculate the natural logarithm (base e) of e
double result = log(M_E);
Brain.Screen.print("%f", result);
// Output: 1.000000
log10#
Calcula el logaritmo en base 10 de un número y devuelve un número decimal.
Available Functionsdouble log10( double x );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
Un doble positivo. |
Returns a double representing the base-10 logarithm of x.
// Calculate the base-10 logarithm of 1000
double result = log10(1000);
Brain.Screen.print("%f", result);
// Output: 3.000000
Operaciones de punto flotante#
fmod#
Devuelve el resto de la división manteniendo el signo del dividendo.
Available Functionsdouble fmod( double x, double y );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
El dividendo. |
|
|
El divisor. |
Returns a double representing the remainder of x/y with the sign of x.
// 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#
Devuelve un número con la magnitud de x y el signo de y.
Available Functionsdouble copysign( double x, double y );
Parámetro |
Tipo |
Descripción |
|---|---|---|
|
|
El valor cuya magnitud se utilizará. |
|
|
El valor cuyo signo se utilizará. |
Returns a double with the magnitude of x and the sign of y.
// Return-10 with the sign of -3 (negative)
double result = copysign(10, -3);
Brain.Screen.print("%f", result);
// Output: -10.000000