数学#
介绍#
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.
可以用它们来计算位置、角度、距离,或者进行角度和弧度之间的转换。
以下是可用的数学函数、常量和实用程序列表:
常用数学函数——常用数学工具。
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}.
三角学——计算角度和边之间的关系。
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.
舍入与绝对值 – 调整精度或方向。
ceil– Rounds up to the nearest integer.floor– Rounds down to the nearest integer.trunc– Removes the decimal portion.
指数与对数——幂、根和对数计算。
sqrt– Returns the square root.exp– Calculates e to the power of x.log– Natural logarithm (base e).log10– Base-10 logarithm.
浮点运算——检查或分解浮点值。
常用数学函数#
abs#
返回一个数的绝对值,去掉任何负号。
Available Functions1 — 返回整数的绝对值。
int abs( int x );
Parameters2 — 返回双精度浮点数的绝对值。
double abs( double x );
范围 |
类型 |
描述 |
|---|---|---|
|
|
整数或双精度浮点数。 |
返回输入值的绝对值,类型与输入值相同。
Examples// Display the absolute value of -10
Brain.Screen.print("%d", abs(-10));
// Output: 10
fmin#
返回两个值中较小的那个,值为双精度浮点数。
Available Functionsdouble fmin( double value1, double value2 );
范围 |
类型 |
描述 |
|---|---|---|
|
|
首先要比较的值。 |
|
|
第二个要比较的值。 |
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#
返回两个值中较大的那个,值为双精度浮点数。
Available Functionsdouble fmax( double value1, double value2 );
范围 |
类型 |
描述 |
|---|---|---|
|
|
首先要比较的值。 |
|
|
第二个要比较的值。 |
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#
返回一个双精度浮点数的幂。
Available Functionsdouble pow( double base, double exponent );
范围 |
类型 |
描述 |
|---|---|---|
|
|
基数。 |
|
|
指数。 |
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
常量#
常量是预先定义好的值,在项目过程中保持不变。它们可以直接用于计算,无需任何定义或赋值。
M_PI#
M_PI gives the mathematical constant π, the ratio of a circle’s circumference to its diameter.
const double M_PI;
该常量没有参数。
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#
数学常数 e(欧拉数),自然对数的底。
Available Constantsconst double M_E;
该常量没有参数。
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
三角学#
sin#
计算弧度角的正弦值并返回一个双精度浮点数。
Available Functionsdouble sin( double x );
范围 |
类型 |
描述 |
|---|---|---|
|
|
表示弧度角度的双精度浮点数。 |
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#
计算弧度角的余弦值并返回一个双精度浮点数。
Available Functionsdouble cos( double x );
范围 |
类型 |
描述 |
|---|---|---|
|
|
表示弧度角度的双精度浮点数。 |
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#
计算弧度角的正切值并返回一个双精度浮点数。
Available Functionsdouble tan( double x );
范围 |
类型 |
描述 |
|---|---|---|
|
|
表示弧度角度的双精度浮点数。 |
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#
计算一个数的反正切值(反正切值),并返回一个表示弧度角度的双精度浮点数。
Available Functionsdouble atan( double x );
范围 |
类型 |
描述 |
|---|---|---|
|
|
双精度值。 |
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#
计算 y/x 的反正切函数的主值,并返回一个表示弧度角度的双精度浮点数。
Available Functionsdouble atan2( double y, double x );
范围 |
类型 |
描述 |
|---|---|---|
|
|
表示 y 坐标的双精度浮点数。 |
|
|
表示 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#
计算一个数的反正弦(反正弦)值,并返回一个表示弧度角度的双精度浮点数。
Available Functionsdouble asin( double x );
范围 |
类型 |
描述 |
|---|---|---|
|
|
介于 -1 和 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#
计算一个数的反余弦值(反余弦值),并返回一个表示弧度角度的双精度浮点数。
Available Functionsdouble acos( double x );
范围 |
类型 |
描述 |
|---|---|---|
|
|
介于 -1 和 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
四舍五入与绝对值#
ceil#
返回一个向上取整的双精度浮点数。
Available Functionsdouble ceil( double x );
范围 |
类型 |
描述 |
|---|---|---|
|
|
一个需要四舍五入的双数。 |
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#
返回向下取整的双精度浮点数。
Available Functionsdouble floor( double x );
范围 |
类型 |
描述 |
|---|---|---|
|
|
双数向下取整。 |
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#
返回一个双精度浮点数,小数部分设为零。
Available Functionsdouble trunc( double x );
范围 |
类型 |
描述 |
|---|---|---|
|
|
待截断的双精度浮点数。 |
Returns a double with the decimal portion removed.
// Truncate 9.7
Brain.Screen.print("%f", trunc(9.7));
// Output: 9.000000
指数与对数#
sqrt#
返回一个数的平方根,结果以浮点数形式给出。
Available Functionsdouble sqrt( double x );
范围 |
类型 |
描述 |
|---|---|---|
|
|
一个非负双精度浮点数。 |
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#
计算一个数的指数并返回一个浮点数。
Available Functionsdouble exp( double x );
范围 |
类型 |
描述 |
|---|---|---|
|
|
用于计算指数的双精度浮点数。 |
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#
计算一个数的自然对数并返回一个浮点数。
Available Functionsdouble log( double x );
范围 |
类型 |
描述 |
|---|---|---|
|
|
正双倍。 |
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#
计算一个数的以 10 为底的对数,并返回一个浮点数。
Available Functionsdouble log10( double x );
范围 |
类型 |
描述 |
|---|---|---|
|
|
正双倍。 |
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
浮点运算#
fmod#
返回除法余数,同时保留被除数的符号。
Available Functionsdouble fmod( double x, double y );
范围 |
类型 |
描述 |
|---|---|---|
|
|
分红。 |
|
|
除数。 |
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#
返回一个数值,其大小为 x,符号为 y。
Available Functionsdouble copysign( double x, double y );
范围 |
类型 |
描述 |
|---|---|---|
|
|
将要使用的值的绝对值。 |
|
|
将使用其符号的值。 |
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