メインコンテンツまでスキップ

Calc マクロ

概要

Calc マクロ は、数式を計算して結果を返すマクロです。
四則演算だけでなく、様々な数学関数もサポートしています。

構文

<!calc=数式>

パラメータ

パラメータ説明
数式計算したい数式

戻り値

計算結果を返します。
数式にエラーがある場合は、マクロがそのまま残ります。

サポートする演算子と関数

演算子

演算子意味優先度数式結果
+加算12+32+3=52 + 3 = 55
-減算12-323=12 - 3 = -1-1
*乗算22*32×3=62 \times 3 = 66
/除算26/363=2\frac{6}{3} = 22
**べき乗32**323=82^3 = 88
( )括弧4(2+3)*4(2+3)×4=20(2 + 3) \times 4 = 2020

数学関数

関数意味数式結果
sqrt平方根sqrt(9)9=3\sqrt{9} = 33
sin正弦sin(45)sin(45)\sin(45)0.8509...
cos余弦cos(45)cos(45)\cos(45)0.5253...
tan正接tan(45)tan(45)\tan(45)1.6197...
asin逆正弦asin(0.5)sin1(0.5)\sin^{-1}(0.5)0.5235...
acos逆余弦acos(0.5)cos1(0.5)\cos^{-1}(0.5)1.0471...
atan逆正接atan(1.5)tan1(1.5)\tan^{-1}(1.5)0.9827...
abs絶対値abs(-5)5=5\lvert -5 \rvert = 55
round四捨五入round(1.5)round(1.5)=2\text{round}(1.5) = 22
floor切り捨てfloor(1.9)1.9=1\lfloor 1.9 \rfloor = 11
ceil切り上げceil(1.1)1.1=2\lceil 1.1 \rceil = 22
log10常用対数log10(100)log10(100)=2\log_{10}(100) = 22
log2二進対数log2(8)log2(8)=3\log_{2}(8) = 33
randランダム値rand()0x<10 \leq x < 101の小数
備考

rand() は 0 以上 1 未満のランダムな小数を返します。
特定の範囲の整数が必要な場合は、floor(rand()*範囲)+最小値 のように計算します。

使用例

基本的な計算

シンプルな四則演算を行います。

/execute as @a run scriptevent capi:tell 2 + 3 = <!calc=2+3>

出力例:

2 + 3 = 5

計算式: 2+3=52 + 3 = 5

括弧を使った計算

括弧を使って計算順序を制御します。

/execute as @a run scriptevent capi:tell (2 + 3) × 4 = <!calc=(2+3)*4>

出力例:

(2 + 3) × 4 = 20

計算式: (2+3)×4=20(2 + 3) \times 4 = 20

スコアを使った計算

スコアの値を計算に使用します。

/execute as @a run scriptevent capi:actionbar 合計: <!calc=<!score=a>+<!score=b>>

出力例(a=10, b=20の場合):

合計: 30

計算式: a+b=10+20=30a + b = 10 + 20 = 30

数学関数の使用

平方根を計算します。

/execute as @a run scriptevent capi:tell √9 = <!calc=sqrt(9)>

出力例:

√9 = 3

計算式: 9=3\sqrt{9} = 3

絶対値の計算

マイナスの値を絶対値に変換します。

/execute as @a run scriptevent capi:tell |<!score=money>| = <!calc=abs(<!score=money>)>

出力例(money=-100の場合):

|-100| = 100

計算式: 100=100\lvert -100 \rvert = 100

ダメージ計算

攻撃力と倍率からダメージを計算します。

/execute as @a run scriptevent capi:actionbar ダメージ: <!calc=<!score=attack>*<!score=critical>/100>

出力例(attack=50, critical=150の場合):

ダメージ: 75

計算式: 50×150100=75\frac{50 \times 150}{100} = 75

ランダム値の生成

ランダムな小数(0~1)を生成します。

/execute as @a run scriptevent capi:tell ランダム値: <!calc=rand()>

出力例:

ランダム値: 0.7234

計算式: rand()rand() は 0 以上 1 未満のランダムな小数

ダイスの目(1~6)を生成

rand() を使って 1 から 6 までのランダムな整数を生成します。

/execute as @a run scriptevent capi:tell ダイスの目: <!calc=floor(rand()*6)+1>

出力例:

ダイスの目: 4

計算式: rand()×6+1\lfloor rand() \times 6 \rfloor + 1 は 1 から 6 までのランダムな整数

距離の計算

座標から距離を計算します。

/execute as @a run scriptevent capi:tell 距離: <!calc=sqrt(<!score=dx>**2+<!score=dy>**2+<!score=dz>**2)>

出力例(dx=3, dy=4, dz=0の場合):

距離: 5

計算式: 32+42+02=9+16=25=5\sqrt{3^2 + 4^2 + 0^2} = \sqrt{9 + 16} = \sqrt{25} = 5

小数点の利用

Calc マクロでは小数を直接使用できます。

小数数式結果
0.50.50.50.5
0.250.250.250.25
0.1250.1250.1250.125

変数からの移行

以前の変数構文からマクロ構文への変更:

旧構文(変数)新構文(マクロ)
{calc:2+3}<!calc=2+3>
{calc:sqrt(9)}<!calc=sqrt(9)>
移行のポイント

旧構文の {calc:数式} は新構文の <!calc=数式> に置き換えます。
コロン(:)がイコール(=)に変わることに注意してください。
また、括弧や数学関数のサポートが追加されています。