数学#
導入#
C++標準ライブラリは、VEXcode IQでサポートされている <cmath>の基本的な数学機能を提供します。これらの関数<cmath>使用すると、三角関数、丸め、べき乗などの一般的な数学演算を実行できます。
位置、角度、距離の計算、または度とラジアン間の変換を行う際に使用します。
以下に、利用可能な数学関数、定数、およびユーティリティの一覧を示します。
よく使われる数学関数 – よく使われる数学ツール
定数 – <cmath><cmath>の事前定義値。
三角法 – 角度と辺の関係を計算する。
sin– ラジアン単位の角度の正弦。cos– ラジアン単位の角度のコサイン。tan– ラジアン単位の角度の正接。atan– ラジアン値の逆正接。atan2– 象限を考慮したラジアンでの y/x の逆正接。asin– ラジアン値のアークサイン。acos– ラジアン値のアークコサイン。
丸めと絶対値 – 精度または方向を調整します。
指数と対数 – べき乗、平方根、対数の計算
浮動小数点演算 – 浮動小数点値を検査または分解します。
一般的な数学関数#
abs#
数値の絶対値を返します。負の符号は取り除かれます。
Available Functions1 — 整数の絶対値を返します。
int abs( int x );
Parameters2 — double 型の絶対値を返します。
double abs( double x );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
整数または倍精度浮動小数点数。 |
入力値の絶対値を、入力値と同じ型で返します。
Examples// Display the absolute value of -10
Brain.Screen.print("%d", abs(-10));
// Output: 10
fmin#
2つの値のうち小さい方をdouble型で返します。
Available Functionsdouble fmin( double value1, double value2 );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
比較対象となる最初の値。 |
|
|
比較対象となる2番目の値。 |
2つの入力値のうち小さい方を表す double を返します。
// Display the lower of two values
Brain.Screen.print("%f", fmin(7, 4));
// Output: 4.000000
fmax#
2つの値のうち大きい方をdouble型で返します。
Available Functionsdouble fmax( double value1, double value2 );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
比較対象となる最初の値。 |
|
|
比較対象となる2番目の値。 |
2つの入力値のうち大きい方を表す double を返します。
// Display the higher of two values
Brain.Screen.print("%f", fmax(7, 4));
// Output: 7.000000
pow#
数値をべき乗した値をdouble型で返します。
Available Functionsdouble pow( double base, double exponent );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
基本数。 |
|
|
指数。 |
底を指数で累乗した値を表す double を返します。
// Display 2 to the power of 3
Brain.Screen.print("%f", pow(2, 3));
// Output: 8.000000
定数#
定数とは、プロジェクト期間中固定される、あらかじめ定義された値のことです。定義や割り当てを行うことなく、計算に使用できます。
M_PI#
M_PI 、円周と直径の比である数学定数 π を表します。
const double M_PI;
この定数にはパラメータがありません。
Return Values数学定数πを 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 Values数学定数 e を doubleとして返します。
// Calculate e raised to the power of 2
double result = pow(M_E, 2);
Brain.Screen.print("%f", result);
// Output: 7.389056
三角法#
sin#
ラジアンで表された角度の正弦を計算し、double型の値を返します。
Available Functionsdouble sin( double x );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
ラジアン単位の角度を表すdouble型変数。 |
角度の正弦を表す double を返します。
// 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#
ラジアン単位の角度のコサインを計算し、double型の値を返します。
Available Functionsdouble cos( double x );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
ラジアン単位の角度を表すdouble型変数。 |
角度のコサインを表す double を返します。
// 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#
ラジアン単位の角度の正接を計算し、double型の値を返します。
Available Functionsdouble tan( double x );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
ラジアン単位の角度を表すdouble型変数。 |
角度の正接を表す double を返します。
// 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#
数値の逆正接(アークタンジェント)を計算し、ラジアン単位の角度を表すdouble型の値を返します。
Available Functionsdouble atan( double x );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
2つの値。 |
ラジアン単位のアークタンジェントを表す double を返します。
// 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 の逆正接の主値を計算し、ラジアン単位の角度を表す double 型の値を返します。
Available Functionsdouble atan2( double y, double x );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
y座標を表すdouble型の値。 |
|
|
x座標を表すdouble型の値。 |
y/xのアークタンジェントをラジアン単位で表した double を返します。
// 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#
数値の逆正弦(アークサイン)を計算し、ラジアン単位の角度を表すdouble型の値を返します。
Available Functionsdouble asin( double x );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
-1から1までの間の倍精度浮動小数点数。 |
ラジアン単位のアークサインを表す double を返します。
// 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#
数値の逆余弦(アークコサイン)を計算し、ラジアン単位の角度を表すdouble型の値を返します。
Available Functionsdouble acos( double x );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
-1から1までの間の倍精度浮動小数点数。 |
ラジアン単位のアークコサインを表す double を返します。
// 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#
数値を切り上げてdouble型として返します。
Available Functionsdouble ceil( double x );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
切り上げが必要なダブル。 |
x以上の最小の整数を表す double を返します。
// Round up 9.7
Brain.Screen.print("%f", ceil(9.7));
// Output: 10.000000
floor#
数値を切り捨ててdouble型として返します。
Available Functionsdouble floor( double x );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
2倍の数値は切り捨てる。 |
x以下の最大の整数を表す double を返します。
// Round down 9.7
Brain.Screen.print("%f", floor(9.7));
// Output: 9.000000
trunc#
小数点以下をゼロに設定した数値をdouble型で返します。
Available Functionsdouble trunc( double x );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
切り捨てられるべき倍数。 |
小数点以下を除いた double を返します。
// Truncate 9.7
Brain.Screen.print("%f", trunc(9.7));
// Output: 9.000000
指数と対数#
sqrt#
数値の平方根を返します。
Available Functionsdouble sqrt( double x );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
非負のdouble型。 |
xの平方根を表す double を返します。
// Display the square root of 121
Brain.Screen.print("%f", sqrt(121));
// Output: 11.000000
exp#
数値の指数を計算します。
Available Functionsdouble exp( double x );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
指数を計算するdouble型。 |
eをx乗した値を表す double を返します。
// 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 );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
プラスのダブル。 |
xの自然対数を表す double を返します。
// 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 );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
プラスのダブル。 |
xの10を底とする対数を表す double を返します。
// 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 );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
配当金。 |
|
|
除数。 |
x/y の剰余に x の符号を付けた double を返します。
// 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 );
パラメータ |
タイプ |
説明 |
|---|---|---|
|
|
その大きさが使用される値。 |
|
|
符号が使用される値。 |
x の大きさと y の符号を持つ double を返します。
// Return -10 with the sign of -3 (negative)
double result = copysign(10, -3);
Brain.Screen.print("%f", result);
// Output: -10.000000