数学#

導入#

Mathモジュールには、Pythonに組み込まれている関数と、VEXcode 123で自動的に利用できる完全 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.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.pow – 数値をべき乗します。

  • math.sqrt – 平方根を返します。

  • math.exp – e の x 乗を計算します。

  • math.log – x の対数を計算します。

  • math.log10 – x の常用対数を計算します。

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

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

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

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

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

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

組み込み機能#

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

abs#

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

Usage:
abs(x)

パラメータ

説明

x

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

# Get the absolute value of -10
abs_result = abs(-10)
console.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)
console.print(round_int_result)
# round_int_result = 6

# Round 3.14159 to 2 decimal places
round_result = round(3.14159, 2)
console.print(round_result, precision=2)
# 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)
console.print(min_result)
# min_result = 1

# Get the smallest value from a list
min_list_result = min([10, 4, 25, 1])
console.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)
console.print(max_result)
# max_result = 7

# Get the largest value from a list
max_list_result = max([10, 4, 25, 1])
console.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])
console.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)
console.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)
console.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)
console.print(pow_result)
# pow_result = 8

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)
console.print("{} coins".format(price_int))
# Output: 19 coins

# Convert a string into an integer to use in calculations
user_input = "55"
user_number = int(user_input)
console.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
console.print(apples_per_person)
# Output: 3.0

# Convert a string into a float to use in calculations
user_input = "23.4"
user_number = float(user_input)
console.print(user_number * 3)
# Output: 70.2

数学モジュール#

Pythonの 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
console.print(circle_area, precision=2)
# circle_area = 78.54

タウ#

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

Usage:
math.tau

# Calculate the circumference of a circle with radius 5
circumference = math.tau * 5
console.print(circumference, precision=2)
# circumference = 31.42

e#

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

Usage:
math.e

# Calculate e raised to the power of 2
e_power = math.pow(math.e, 2)
console.print(e_power, precision=2)
# e_power = 7.39

Trigonometry#

罪#

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

Usage:
math.sin(x)

パラメータ

説明

x

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

# Calculate the sine of 
# 30 degrees in radians
result = math.sin(math.radians(30))
console.print(result, precision=2)
# result = 0.50

コス#

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

Usage:
math.cos(x)

パラメータ

説明

x

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

# Calculate the cosine of 
# 60 degrees in radians
result = math.cos(math.radians(60))
console.print(result, precision=2)
# result = 0.50

タン#

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

Usage:
math.tan(x)

パラメータ

説明

x

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

# Calculate the tangent of 
# 45 degrees in radians
result = math.tan(math.radians(45))
console.print(result)
# result = 1

ASIN#

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

Usage:
math.asin(x)

パラメータ

説明

x

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

# Calculate the arc sine 
# of 0.5 in degrees
result = math.degrees(math.asin(0.5))
console.print(result)
# result = 30

アコス#

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

Usage:
math.acos(x)

パラメータ

説明

x

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

# Calculate the arc cosine 
# of 0.5 in degrees
result = math.degrees(math.acos(0.5))
console.print(result)
# result = 60

アタン#

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

Usage:
math.atan(x)

パラメータ

説明

x

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

# Calculate the arc tangent 
# of 1 in degrees
arc_tan = math.atan(1.0)
result = math.degrees(arc_tan)
console.print(result)
# result = 45

アタン2#

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

Usage:
math.atan2(y, x)

パラメータ

説明

y

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

x

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

# Calculate the inverse tangent 
# of 1/1 in degrees
atan2 = math.atan2(1.0, 1.0)
result = math.degrees(atan2)
console.print(result)
# result = 45

学位#

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

Usage:
math.degrees(x)

パラメータ

説明

x

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

# Convert pi radians to degrees
degrees_result = math.degrees(math.pi)
console.print(degrees_result)
# degrees_result = 180

ラジアン#

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

Usage:
math.radians(x)

パラメータ

説明

x

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

# Convert 180 degrees to radians
radians_result = math.radians(180)
console.print(radians_result, precision=2)
# radians_result = 3.14

Rounding & Absolute Values#

天井#

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

Usage:
math.ceil(x)

パラメータ

説明

x

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

# Round 3.7 up to the nearest integer
ceil_result = math.ceil(3.7)
console.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)
console.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)
console.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)
console.print(fabs_result, precision=1)
# fabs_result = 3.7

Exponents & Logarithms#

ログ#

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

Usage:
math.log(x, base)

パラメータ

説明

x

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

base

オプション。使用する対数の底を指定します。デフォルトでは自然対数が使用されます。

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

# Calculate the log base 2 of 8
log_result = math.log(8, 2)
console.print(log_result)
# log_result = 3

log10#

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

Usage:
math.log10(x)

パラメータ

説明

x

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

# Calculate the base-10 logarithm of 100
log10_result = math.log10(100)
console.print(log10_result)
# log10_result = 2

捕虜#

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

Usage:
math.pow(x, y)

パラメータ

説明

x

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

y

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

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

# power_result = 8

平方根#

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

Usage:
math.sqrt(x)

パラメータ

説明

x

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

# Calculate the square root of 25
sqrt_result = math.sqrt(25)
console.print(sqrt_result)
# sqrt_result = 5.0

exp#

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

Usage:
math.exp(x)

パラメータ

説明

x

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

# Calculate e raised to the power of 1
exp_result = math.exp(1)
console.print(exp_result, precision=2)
# exp_result = 2.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)
console.print(fractional_part, precision=2)
console.new_line()
console.print(integer_part)
# fractional_part = 0.14
# integer_part = 3

フランス語#

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

Usage:
math.frexp(x)

パラメータ

説明

x

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

# Decompose 16 into its mantissa and exponent
mantissa, exponent = math.frexp(16)
console.print(mantissa, precision=2)
console.new_line()
console.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)
console.print(fmod_result)
# fmod_result = 1

コピーサイン#

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

Usage:
math.copysign(x, y)

パラメータ

説明

x

変更する値。

y

符号がコピーされる値。

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

ldexp#

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

Usage:
math.ldexp(x, exp)

パラメータ

説明

x

基本値。

exp

指数。

# Compute 3 * (2 ** 4)
ldexp_result = math.ldexp(3, 4)
console.print(ldexp_result)
# ldexp_result = 48