数学#

導入#

Mathモジュールには、Pythonに組み込まれている関数と、VEXcode AIMで自動的に利用できる完全 math モジュールが含まれています。これらのツールを使用すると、基本的な算術演算から高度な三角関数、丸め、対数演算まで、あらゆる演算を実行できます。

これらの関数と定数を使用して、ロボットの位置、角度、距離、その他の数値を計算できます。また、度とラジアンの変換、式の評価、無限大やNaNなどの特殊値の操作も可能です。

以下に、利用可能な数学関数、定数、およびユーティリティの一覧を示します。

組み込み関数 – Pythonに付属する一般的な数学ツール。

  • abs – 数値の絶対値を返します。

  • round – 数値を指定された小数点以下の桁数に丸めます。

  • min – 入力値の中で最小値を返します。

  • max – 入力値の中で最大の値を返します。

  • sum – イテラブル内のすべての値を合計します。

  • divmod – 商と余りをタプルとして返します。

  • pow – 数値をべき乗します。オプションで剰余も指定できます。

  • int – 値を整数に変換します。

  • float – 値を浮動小数点数に変換します。

Constants – Predefined values from the math module.

  • math.pi – 定数 π (パイ)。

  • math.tau – 定数 τ (2π)。

  • math.e – オイラー数、自然対数の底。

  • math.inf – 正の無限大。

  • math.nan – 数値ではありません (NaN)。

三角法 – 角度と辺の関係を計算する。

  • math.sin – ラジアン単位の角度の正弦。

  • math.cos – ラジアン単位の角度のコサイン。

  • math.tan – ラジアン単位の角度の正接。

  • math.atan – ラジアン値の逆正接。

  • math.atan2 – 象限を考慮したラジアンでの y/x のアークタンジェント。

  • math.asin – ラジアン値のアークサイン。

  • math.acos – ラジアン値のアークコサイン。

  • math.degrees – ラジアンを度数に変換します。

  • math.radians – 度をラジアンに変換します。

双曲線関数 – 高度な三角関数関連機能。

丸めと絶対値 – 精度または方向を調整します。

  • math.ceil – 最も近い整数に切り上げます。

  • math.floor – 最も近い整数に切り捨てます。

  • math.trunc – 小数部分を削除します。

  • math.fabs – 絶対値を浮動小数点数として返します。

指数と対数 – べき乗、平方根、対数の計算

浮動小数点演算 – 浮動小数点値を検査または分解します。

  • math.modf – float の小数部と整数部を返します。

  • math.frexp – 数を仮数と指数に分解します。

  • math.fmod – 被除数の符号付き剰余。

  • math.copysign – 別の値の符号を持つ値を返します。

  • math.ldexp – x * (2 ** exp)を計算します。

比較と概算 – 許容範囲またはカテゴリに基づいて値を確認します。

  • math.isclose – 2 つの値がほぼ等しいかどうかをテストします。

  • math.isfinite – 数値が有限かどうかをチェックします。

  • math.isinf – 数が無限大かどうかをチェックします。

  • math.isnan – 値が NaN かどうかをチェックします。

誤差とガンマ – 特殊な数学関数。

組み込み機能#

Pythonには、プロジェクト内で数学演算を実行できる組み込み関数がいくつか用意されています。

abs#

abs 、数値の絶対値を返します。負の符号は取り除かれます。

Usage:
abs(x)

パラメータ

説明

x

整数または浮動小数点数。

# Get the absolute value of -10
abs_result = abs(-10)
robot.screen.print(abs_result)

# abs_result = 10

round#

round は、数値を指定された小数点以下の桁数に丸めます。

Usage:
round(x, ndigits)

パラメータ

説明

x

整数または浮動小数点数。

ndigits

オプション。丸める小数点以下の桁数。デフォルト値は0です。

# Round 5.7 to the nearest integer
round_int_result = round(5.7)
robot.screen.print(round_int_result)

# round_int_result = 6

# Round 3.14159 to 2 decimal places
round_result = round(3.14159, 2)
robot.screen.print(round_result)

# round_result = 3.14

min#

min 、複数の引数または反復可能なオブジェクトから最小値を返します。

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

パラメータ

説明

arg1, arg2, …

比較対象となる数値。

sequence

数値を含むリスト、タプル、またはその他のシーケンス。

# Get the smallest number from 3, 7, and 1
min_result = min(3, 7, 1)
robot.screen.print(min_result)

# min_result = 1

# Get the smallest value from a list
min_list_result = min([10, 4, 25, 1])
robot.screen.print(min_list_result)

# min_list_result = 1

max#

max 、複数の引数または反復可能なものから最大値を返します。

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

パラメータ

説明

arg1, arg2, …

比較対象となる数値。

sequence

数値を含むリスト、タプル、またはその他のシーケンス。

# Get the largest number from 3, 7, and 1
max_result = max(3, 7, 1)
robot.screen.print(max_result)

# max_result = 7

# Get the largest value from a list
max_list_result = max([10, 4, 25, 1])
robot.screen.print(max_list_result)

# max_list_result = 25

sum#

sum 、オプションの開始値を指定して、反復可能なすべての値を合計します。

Usage:
sum(sequence, start)

パラメータ

説明

sequence

数値を含むリスト、タプル、またはその他のシーケンス。

start

オプション。合計値に加える値。デフォルト値は0です。

# Calculate the sum of a list of numbers
sum_result = sum([1, 2, 3, 4, 5])
robot.screen.print(sum_result)

# sum_result = 15

# Calculate the sum of a list with a starting value of 10
sum_with_start = sum([1, 2, 3], 10)
robot.screen.print(sum_with_start)

# sum_with_start = 16

divmod#

divmod 、除算演算の商と余りを含むタプルを返します。

Usage:
divmod(a, b)

パラメータ

説明

a

配当金。

b

除数。

# Perform integer division and remainder of 10 / 3
divmod_result = divmod(10, 3)
robot.screen.print(divmod_result)

# divmod_result = (3, 1)

pow#

pow は、数値をべき乗し、必要に応じて剰余演算を実行します。

Usage:
pow(x, y, mod)

パラメータ

説明

x

基本数。

y

指数。

mod

オプション。剰余値。指定した場合、 (x ** y) % modを返します。

# Calculate 2 raised to the power of 3
pow_result = pow(2, 3)
robot.screen.print(pow_result)

# pow_result = 8

# Calculate (5 ** 3) % 7
pow_mod_result = pow(5, 3, 7)
robot.screen.print(pow_mod_result)

# pow_mod_result = 6

int#

int 、数値または文字列を整数に変換します。文字列から変換する場合、基数変換もサポートしています。

Usage:
int(x, base)

パラメータ

説明

x

変換する数値、文字列、またはその他のオブジェクト。

base

オプション。変換に使用する基数。デフォルトは10です。

# Convert a float to an integer to get rid of decimals
price = 19.99
price_int = int(price)
robot.screen.print(f"{price_int} coins")

# Output: 19 coins

# Convert a string into an integer to use in calculations
user_input = "55"
user_number = int(user_input)
robot.screen.print(user_number * 2)

# Output: 110

float#

float は、数値または文字列を浮動小数点数に変換します。

Usage:
float(x)

パラメータ

説明

x

変換する数値、文字列、またはその他のオブジェクト。

# Convert division result to a float
num_apples = 6
num_people = 2
apples_per_person = float(num_apples) / num_people
robot.screen.print(apples_per_person)

# Output: 3.00

# Convert a string into a float to use in calculations
user_input = "23.4"
user_number = float(user_input)
robot.screen.print(user_number * 3)

# Output: 70.20

数学モジュール#

MicroPythonの math モジュールは、一般的な数学計算を実行するための追加メソッドを提供します。これらのメソッドには、三角関数、対数関数、その他の数値演算が含まれます。

VEXcodeでは、 math モジュールがデフォルトでインポートされます。

Constants#

定数とは、プロジェクト期間中固定される、あらかじめ定義された値のことです。定義や割り当てを行うことなく、計算に使用できます。

円周率#

pi 、円周と直径の比である数学定数 π を表します。

Usage:
math.pi

# Calculate the area of a circle with radius 5 using pi
circle_area = math.pi * 5 * 5
robot.screen.print(circle_area)

# circle_area = 78.54

タウ#

tau は 2π の値を与えます。

Usage:
math.tau

# Calculate the circumference of a circle with radius
circumference = math.tau * 5
robot.screen.print(circumference)

# circumference = 31.42

e#

e は自然対数の底を表します。

Usage:
math.e

# Calculate e raised to the power of 2
e_power = math.pow(math.e, 2)
robot.screen.print(e_power)

# e_power = 7.39

情報#

inf 浮動小数点数として正の無限大を表します。

Usage:
math.inf

# Check if infinity is infinite
inf_value = math.inf

if math.isinf(inf_value):
    robot.screen.print("Infinity")

# Prints "Infinity"

ナン#

nan は「非数」(NaN) を表す特別な浮動小数点数です。

Usage:
math.nan

# Check if nan is Not a Number
nan_value = math.nan

if math.isnan(nan_value):
    robot.screen.print("Not a Number")

# Prints "Not a Number"

Trigonometry#

罪#

sin はラジアン単位の角度の正弦を計算し、浮動小数点数を返します。

Usage:
math.sin(x)

パラメータ

説明

x

ラジアン単位の角度を表す浮動小数点数または整数。

# Calculate the sine of 30 degrees in radians
sine_result = math.sin(math.radians(30))
robot.screen.print(sine_result)

# sine_result = 0.50

コス#

cos はラジアン単位の角度のコサインを計算し、浮動小数点数を返します。

Usage:
math.cos(x)

パラメータ

説明

x

ラジアン単位の角度を表す浮動小数点数または整数。

# Calculate the cosine of 60 degrees in radians
cosine_result = math.cos(math.radians(60))
robot.screen.print(cosine_result)

# cosine_result = 0.50

タン#

tan はラジアン単位の角度の正接を計算し、浮動小数点数を返します。

Usage:
math.tan(x)

パラメータ

説明

x

ラジアン単位の角度を表す浮動小数点数または整数。

# Calculate the tangent of 45 degrees in radians
tangent_result = math.tan(math.radians(45))
robot.screen.print(tangent_result)

# tangent_result = 1.00

アタン#

atan は数値の逆正接 (アークタンジェント) を計算し、ラジアン単位の角度を表す float を返します。

Usage:
math.atan(x)

パラメータ

説明

x

浮動小数点数または整数。

# Calculate the arc tangent of 1 in degrees
arc_tangent_result = math.degrees(math.atan(1.0))
robot.screen.print(arc_tangent_result)

# arc_tangent_result = 45.00

アタン2#

atan2 、y/x の逆正接の主値を計算し、ラジアン単位の角度を表す float を返します。

Usage:
math.atan2(y, x)

パラメータ

説明

y

y座標を表す浮動小数点数または整数。

x

x座標を表す浮動小数点数または整数。

# Calculate the inverse tangent of 1/1 in degrees
atan2_result = math.degrees(math.atan2(1.0, 1.0))
robot.screen.print(atan2_result)

# atan2_result = 45.00

ASIN#

asin は数値の逆正弦(アークサイン)を計算し、ラジアン単位の角度を表す浮動小数点数を返します。

Usage:
math.asin(x)

パラメータ

説明

x

-1から1までの間の浮動小数点数または整数。

# Calculate the arc sine of 0.5 in degrees
arc_sine_result = math.degrees(math.asin(0.5))
robot.screen.print(arc_sine_result)

# arc_sine_result = 30.00

アコス#

acos は数値の逆余弦(アークコサイン)を計算し、ラジアン単位の角度を表す浮動小数点数を返します。

Usage:
math.acos(x)

パラメータ

説明

x

-1から1までの間の浮動小数点数または整数。

# Calculate the arc cosine of 0.5 in degrees
arc_cosine_result = math.degrees(math.acos(0.5))
robot.screen.print(arc_cosine_result)

# arc_cosine_result = 60.00

学位#

degrees は、角度をラジアンから度数に変換します。

Usage:
math.degrees(x)

パラメータ

説明

x

ラジアン単位の角度を表す浮動小数点数または整数。

# Convert pi radians to degrees
degrees_result = math.degrees(math.pi)
robot.screen.print(degrees_result)

# degrees_result = 180.00

ラジアン#

radians は角度を度からラジアンに変換します。

Usage:
math.radians(x)

パラメータ

説明

x

角度を度数で表した浮動小数点数または整数。

# Convert 180 degrees to radians
radians_result = math.radians(180)
robot.screen.print(radians_result)

# radians_result = 3.14

Hyperbolics#

シン#

sinh は xの双曲線正弦を計算します。

Usage:
math.sinh(x)

パラメータ

説明

x

入力値を表す浮動小数点数または整数。

# Calculate the hyperbolic sine of 1
sinh_result = math.sinh(1)
robot.screen.print(sinh_result)

# sinh_result = 1.18

コッシュ#

cosh は xの双曲線コサインを計算します。

Usage:
math.cosh(x)

パラメータ

説明

x

入力値を表す浮動小数点数または整数。

# Calculate the hyperbolic cosine of 1
cosh_result = math.cosh(1)
robot.screen.print(cosh_result)

# cosh_result = 1.54

タン#

tanh は xの双曲線正接を計算します。

Usage:
math.tanh(x)

パラメータ

説明

x

入力値を表す浮動小数点数または整数。

# Calculate the hyperbolic tangent of 1
tanh_result = math.tanh(1)
robot.screen.print(tanh_result)

# tanh_result = 0.76

アシン#

asinh は xの逆双曲線正弦を計算します。

Usage:
math.asinh(x)

パラメータ

説明

x

入力値を表す浮動小数点数または整数。

# Calculate the inverse hyperbolic sine of 1
asinh_result = math.asinh(1)
robot.screen.print(asinh_result)

# asinh_result = 0.88

アコシュ#

acosh は xの逆双曲線余弦を計算します。

Usage:
math.acosh(x)

パラメータ

説明

x

1以上の浮動小数点数または整数。

# Calculate the inverse hyperbolic cosine of 2
acosh_result = math.acosh(2)
robot.screen.print(acosh_result)

# acosh_result = 1.32

アタン#

atanh は xの逆双曲線正接を計算します。

Usage:
math.atanh(x)

パラメータ

説明

x

-1から1までの範囲の浮動小数点数(-1は含まない)。

# Calculate the inverse hyperbolic tangent of 0.5
atanh_result = math.atanh(0.5)
robot.screen.print(atanh_result)

# atanh_result = 0.55

Rounding & Absolute Values#

天井#

ceil は数値を最も近い整数に切り上げます。

Usage:
math.ceil(x)

パラメータ

説明

x

切り上げる対象となる浮動小数点数または整数。

# Round 3.7 up to the nearest integer
ceil_result = math.ceil(3.7)
robot.screen.print(ceil_result)

# ceil_result = 4

床#

floor は、数値を最も近い整数に切り捨てます。

Usage:
math.floor(x)

パラメータ

説明

x

切り捨てる浮動小数点数または整数。

# Round 3.7 down to the nearest integer
floor_result = math.floor(3.7)
robot.screen.print(floor_result)

# floor_result = 3

切り取り線#

trunc は、数値の小数部分を丸めずに削除します。

Usage:
math.trunc(x)

パラメータ

説明

x

切り捨てられる浮動小数点数。

# Remove the decimal part of 3.7
trunc_result = math.trunc(3.7)
robot.screen.print(trunc_result)

# trunc_result = 3

ファブス#

fabs は数値の絶対値を浮動小数点数として返します。

Usage:
math.fabs(x)

パラメータ

説明

x

浮動小数点数または整数。

# Get the absolute value of -3.7
fabs_result = math.fabs(-3.7)
robot.screen.print(fabs_result)

# fabs_result = 3.70

Exponents & Logarithms#

捕虜#

pow は、入力が両方とも整数であっても、x を y 乗して浮動小数点数を返します。

Usage:
math.pow(x, y)

パラメータ

説明

x

浮動小数点数または整数の基数。

y

浮動小数点数または整数の指数。

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

# power_result = 8.00

平方根#

sqrt は数値の平方根を計算し、完全平方数であっても浮動小数点数を返します。

Usage:
math.sqrt(x)

パラメータ

説明

x

負でない浮動小数点数または整数。

# Calculate the square root of 16
sqrt_result = math.sqrt(16)
robot.screen.print(sqrt_result)

# sqrt_result = 4.00

exp#

exp は数値の指数を計算し、浮動小数点数を返します。

Usage:
math.exp(x)

パラメータ

説明

x

浮動小数点数または整数。

# Calculate e raised to the power of 1
exp_result = math.exp(1)
robot.screen.print(exp_result)

# exp_result = 2.72

ログ#

log は数値の自然対数を計算し、浮動小数点数を返します。

Usage:
math.log(x)

パラメータ

説明

x

正の浮動小数点数または整数。

# Calculate the natural logarithm (base e) of 7.389056
log_result = math.log(7.389056)
robot.screen.print(log_result)

# log_result = 2.00

log10#

log10 は、数値の 10 を底とする対数を計算し、浮動小数点数を返します。

Usage:
math.log10(x)

パラメータ

説明

x

正の浮動小数点数または整数。

# Calculate the base-10 logarithm of 1000
log10_result = math.log10(1000)
robot.screen.print(log10_result)

# log10_result = 3.00

log2#

log2 は、数値の底 2 の対数を計算し、 x が 2 のべき乗であっても float を返します。

Usage:
math.log2(x)

パラメータ

説明

x

正の浮動小数点数または整数。

# Calculate the base-2 logarithm of 8
log2_result = math.log2(8)
robot.screen.print(log2_result)

# log2_result = 3.00

階乗#

factorial 、整数 xの階乗を返します。これは、 xまでのすべての正の整数の積です。

Usage:
math.factorial(x)

パラメータ

説明

x

負でない整数。

# Calculate 5 factorial (5!)
factorial_result = math.factorial(5)
robot.screen.print(factorial_result)

# factorial_result = 120

expm1#

expm1 は ex - 1を計算します。これは、小さな xに対してより正確です。

Usage:
math.expm1(x)

パラメータ

説明

x

指数値。

# Compute expm1(1) (e^1 - 1)
expm1_result = math.expm1(1)
robot.screen.print(expm1_result)

# expm1_result = 1.72

Floating Point Operations#

modf#

modf 、数値を小数部と整数部に分解し、タプル (fractional part, integer part)を返します。どちらも float 型です。

Usage:
math.modf(x)

パラメータ

説明

x

分解する浮動小数点数または整数。

# Decompose 3.14159 into fractional and integer parts
fractional_part, integer_part = math.modf(3.14159)
robot.screen.print(fractional_part)
robot.screen.next_row()
robot.screen.print(integer_part)

# fractional_part = 0.14
# integer_part = 3.00

フランス語#

frexp は、数値を仮数部と指数部に分解し、タプル (mantissa, exponent)を返します。ここで、仮数部は浮動小数点数、指数部は整数です。

Usage:
math.frexp(x)

パラメータ

説明

x

分解する浮動小数点数または整数。

# Decompose 16 into its mantissa and exponent
mantissa, exponent = math.frexp(16)
robot.screen.print(mantissa)
robot.screen.next_row()
robot.screen.print(exponent)

# mantissa = 0.50
# exponent = 5

fmod#

fmod 、被除数の符号を保持したまま除算の余りを返します (x)。

Usage:
math.fmod(x, y)

パラメータ

説明

x

配当金。

y

除数。

# Calculate remainder of 10 / 3
# that preserves the sign of 10
fmod_result = math.fmod(10, 3)
robot.screen.print(fmod_result)

# fmod_result = 1.00

コピーサイン#

copysign は、 yの符号を持つ x を返します。

Usage:
math.copysign(x, y)

パラメータ

説明

x

変更する値。

y

符号がコピーされる値。

# Return -10 with the sign of 3 (positive)
copysign_result = math.copysign(-10, 3)
robot.screen.print(copysign_result)

# copysign_result = 10.00

ldexp#

ldexp x * (2 ** exp)計算します。これは x * 2exp{code}と同等です。

Usage:
math.ldexp(x, exp)

パラメータ

説明

x

基本値。

exp

指数。

# Compute 3 * (2 ** 4)
ldexp_result = math.ldexp(3, 4)
robot.screen.print(ldexp_result)

# ldexp_result = 48.00

Comparison & Approximation#

近い#

isclose は2つの数値が許容範囲内でほぼ等しいかどうかをチェックします。

Usage:
math.isclose(a, b, rel_tol, abs_tol)

パラメータ

説明

a

比較対象となる最初の数値。

b

比較対象となる2番目の数値。

rel_tol

オプション。aとbのサイズに対する、許容される最大差。デフォルト値は1e-09(非常に小さい)です。

abs_tol

オプション。固定誤差範囲。ゼロに近い数値を比較する場合に便利です。デフォルト値は0.0です。

注: rel_tol と abs_tol の両方が指定されている場合、先に満たされた条件が結果を決定します。

# Check if 1.000000001 and 1.0 are close
# within the default tolerance
isclose_result = math.isclose(1.000000001, 1.0)
robot.screen.print(isclose_result)

# isclose_result = True

# Check if 0.0000001 and 0.0 are close
# using absolute tolerance
isclose_result = math.isclose(0.0000001, 0.0, abs_tol=1e-07)
robot.screen.print(isclose_result)

# isclose_result = True

# Check if 1000000.0 and 1000000.1 are close
# with a stricter tolerance
isclose_result = math.isclose(1000000.0, 1000000.1, rel_tol=1e-10)
robot.screen.print(isclose_result)

# isclose_result = False

無限#

isfinite 数値が有限かどうかをチェックします。このメソッドはブール値を返します。

  • True – その数は有限です。

  • False – 数は無限です。

Usage:
math.isfinite(x)

パラメータ

説明

x

チェック対象の浮動小数点数または整数。

# Check if 42 is a finite number (returns True)
is_finite_true = math.isfinite(42)
robot.screen.print(is_finite_true)

# is_finite_true = True

# Check if infinity is a finite number (returns False)
is_finite_false = math.isfinite(math.inf)
robot.screen.print(is_finite_false)

# is_finite_false = False

isinf#

isinf 、数値が無限大かどうかをチェックします。このメソッドはブール値を返します。

  • True – 数は無限です。

  • False – その数は有限です。

Usage:
math.isinf(x)

パラメータ

説明

x

チェック対象の浮動小数点数または整数。

# Check if infinity is an infinite number (returns True)
is_inf_true = math.isinf(math.inf)
robot.screen.print(is_inf_true)

# is_inf_true = True

# Check if 42 is an infinite number (returns False)
is_inf_false = math.isinf(42)
robot.screen.print(is_inf_false)

# is_inf_false = False

イスナン#

isnan 、数値が NaN (非数) かどうかをチェックします。このメソッドはブール値を返します。

  • True – 数値はNaNです。

  • False – その数値は有効な数値です。

Usage:
math.isnan(x)

パラメータ

説明

x

チェック対象の浮動小数点数または整数。

# Check if NaN (Not a Number) is NaN (returns True)
is_nan_true = math.isnan(math.nan)
robot.screen.print(is_nan_true)

# is_nan_true = True

# Check if 42 is NaN (returns False)
is_nan_false = math.isnan(42)
robot.screen.print(is_nan_false)

# is_nan_false = False

Error and Gamma Calculations#

ガンマ#

gamma xのガンマ関数を計算します。これは、実数と複素数の階乗関数を一般化したものです。整数 n、 gamma(n) = (n-1)!。

Usage:
math.gamma(x)

パラメータ

説明

x

正の浮動小数点数または整数。

# Calculate the gamma function of 5 (equivalent to 4!)
gamma_result = math.gamma(5)
robot.screen.print(gamma_result)

# gamma_result = 24.00

lgamma#

lgamma 、 gamma 関数の自然対数を計算します。

Usage:
math.lgamma(x)

パラメータ

説明

x

正の浮動小数点数または整数。

# Calculate the natural logarithm of the
# gamma function of 5
lgamma_result = math.lgamma(5)
robot.screen.print(lgamma_result)

# lgamma_result = 3.18

erf#

erf は xの誤差関数を計算します。

Usage:
math.erf(x)

パラメータ

説明

x

入力値を表す浮動小数点数または整数。

# Calculate the error function of 1
erf_result = math.erf(1)
robot.screen.print(erf_result)

# erf_result = 0.84

erfc#

erfc は xの相補誤差関数を計算します。これは 1 - erf(x)として定義されます。

Usage:
math.erfc(x)

パラメータ

説明

x

入力値を表す浮動小数点数または整数。

# Calculate the complementary error function of 1
erfc_result = math.erfc(1)
robot.screen.print(erfc_result)

# erfc_result = 0.16