数学#

導入#

Mathには、Pythonの組み込み関数と、VEXcode IQ(第2世代)で自動的に利用できる math 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 — 自然対数 (底は e)。

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

  • 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)
brain.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)
brain.screen.print(round_int_result)

# round_int_result = 6

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

# min_result = 1

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

# max_result = 7

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

# 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))
brain.screen.print(result)

# result = 0.50

コス#

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

Usage:
math.cos(x)

パラメータ

説明

x

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

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

# result = 0.50

タン#

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

Usage:
math.tan(x)

パラメータ

説明

x

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

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

# result = 1.00

アタン#

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

Usage:
math.atan(x)

パラメータ

説明

x

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

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

# 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 = math.atan2(1.0, 1.0)
result = math.degrees(atan2)
brain.screen.print(result)

# result = 45.00

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))
brain.screen.print(result)

# result = 30.00

アコス#

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

Usage:
math.acos(x)

パラメータ

説明

x

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

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

# result = 60.00

学位#

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

Usage:
math.degrees(x)

パラメータ

説明

x

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

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

# degrees_result = 180.00

ラジアン#

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

Usage:
math.radians(x)

パラメータ

説明

x

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

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

# 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)
brain.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)
brain.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)
brain.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)
brain.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)
brain.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)
brain.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)
brain.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)
brain.screen.print(log_result)

# log_result = 2.00

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)
brain.screen.print(fractional_part)
brain.screen.next_row()
brain.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)
brain.screen.print(mantissa)
brain.screen.next_row()
brain.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)
brain.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)
brain.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)
brain.screen.print(ldexp_result)

# ldexp_result = 48.00