SenseTalkの算術計算
SenseTalkでは、数学的な演算子やさまざまなコマンドや関数を使用して数学的な操作を実行することができます。演算子の使用方法についての詳細な情報は、Expressions ページを参照してください。このページではSenseTalkで表現を使用する基本的な方法を説明し、特定の数学演算子(+、-、*、その他の演算子)と比較や論理演算子などの他の有用な概念について説明するページへのリンクがあります。
SenseTalkの算術コマンドと関数は、数値を操作するための便利なツールを提供しています。算術コマンドは、コンテナに格納された数値を変更するために使用できます。これらのコマンドは+、-、*、/の演算子と同じ算術関数を実行します。違いは、これらのコマンドがオペランドの一つをコンテナから取り出し、計算結果をそのコンテナに戻すことです。算術関数は数値を操作します。これらの関数を使用して、さまざまな方法で数値を操作することができます。
算術計算に関連する話題であるPoints and Rectanglesでは、SenseTalkが幾何学的な点と矩形の概念をどのように理解しているかについて説明しています。任意の2つの数のリスト、またはコンマで区切られた2つの数からなるテキスト文字列は、点として扱うことができます。Points and Rectanglesのページで説明されている関数は、点や矩形の各成分値を抽出するために使用できます。
算術コマンド
算術コマンドは4つあります:
これらを使用して、コンテナに格納された値を変更します。これらのコマンドは+、-、*、/の演算子と同じ算術関数を実行します。違いは、これらのコマンドがオペランドの一つをコンテナから取り出し、計算結果をそのコンテナに戻すことです。
Add コマンド
Behavior: add コマンドは、コンテナ内の値に数値を追加し、合計によりコンテナ内の値を置き換える場合に使用します。値のリストを追加することもできますが、ソースリストと宛先リストの両方が同じ数のアイテムを含んでいる必要があります。ソースリストからの各アイテムは、宛先コンテナの対応するアイテムに追加されます。 NumExpr はソース表現です。これは数値、任意の数式、または他のコンテナであることができます。 Chunk は、コンテナの一部を説明するチャンク表現です(行、単語、アイテム、または文字)。 Container は任意のコンテナです。
構文: add numExpr to {chunk of} container
例:
add amount to dollarsVariable
add 37 to item 2 of line 3 of scores
add speed * time to item 1 of distances
add [10,5] to centerPoint
文法:
Subtract コマンド
Behavior: subtract コマンドは、コンテナ内の値から数値を引く場合に使用します。結果によりコンテナ内の値を置き換えます。値のリストを引くことができますが、ソースリストと宛先リストの両方が同じ数のアイテムを含んでいる必要があります。ソースリストからの各アイテムは、宛先コンテナの対応するアイテムから引かれます。 NumExpr はソース表現です。これは数値、任意の数式、または他のコンテナであることができます。 Chunk は、コンテナの一部を説明するチャンク表現です(行、単語、アイテム、または文字)。 Container は任意のコンテナです。
構文: subtract numExpr from {chunk of} container
関連項目:
subtract checkAmt from accountBalance
subtract 1 from property CountDown of gameController
subtract rate * pmt from line 4 of amortization
subtract [1,2] from boxDimensions
関連項目:
Multiply コマンド
Behavior: multiply コマンドは、コンテナ内の値を別の数値で掛ける場合に使用します。結果によりコンテナ内の値を置き換えます。値のリストを掛けることができますが、ソースリストと宛先リストの両方が同じ数のアイテムを含んでいる、またはソースが単一の値であることが必要です。宛先コンテナの各アイテムは、ソースリストの対応するアイテム、またはソース値で掛けられます。 NumExpr はソース表現です。これは数値、任意の数式、または他のコンテナであることができます。 Chunk は、コンテナの一部を説明するチャンク表現です(行、単語、アイテム、または文字)。 Container は任意のコンテナです。
構文: multiply {chunk of} container by numExpr
例:
multiply score by weightingFactor
multiply accountBalance by 1 + interestRate
multiply item 3 of line x of table by 2
関連:
Divide コマンド
Behavior: divide コマンドは、コンテナ内の値を別の数値で除算する場合に使用します。結果によりコンテナ内の値を置き換えます。値のリストを除算することができますが、ソースリストと宛先リストの両方が同じ数のアイテムを含んでいる、またはソースが単一の値であることが必要です。宛先コンテナの各アイテムは、ソースリストの対応するアイテム、またはソース値で除算されます。 NumExpr はソース表現です。これは数値、任意の数式、または他のコンテナであることができます。 Chunk は、コンテナの一部を説明するチャンク表現です(行、単語、アイテム、または文字)。 Container は任意のコンテナです。
構文: divide {chunk of} container by numExpr
例:
divide score by totalCount
divide item 1 of balances by 12
関連項目:
算術関数
これらの関数を使用して数値を操作します。
Abs, As a positive number, As a negative number Functions
Behavior: 数値パラメータの絶対値を返します。絶対値はその数値の大きさであり、その符号に関わらず常に正またはゼロです。
構文: abs( numExpr ) the abs of numFactor numFactor as a positive number numFactor as a negative number
例:
put abs(-11) --> 11
put the abs of -11 --> 11
例
if height is a negative number then put abs(height) into height
例
put (3 - 16) as a positive number --> 13
put 500 as a negative number --> -500
例: ACos関数
Behavior: 値の三角円弧余弦を計算し、ラジアンで表された角度として結果を返します。
構文: acos( numExpr ) {the} acos of numFactor
例:
put acos(0) --> 1.570796 radians
put the acos of 0 --> 1.570796 radians
put acos(0.5) as degrees --> 60 degrees
関連項目:
ACosh 関数
Behavior: 値の逆双曲余弦を計算し、結果をラジアンで表した角度として返します。
構文: acosh( numExpr )
{the} acosh of numFactor
例:
put acosh(2.509178) rounded to the nearest degree --> 90 degrees
put the acosh of 1 --> 0 radians
関連項目:
Annuity関数
Behavior: 指定された期間ごとの利息率と期間数に基づき、一単位の支払いを伴う普通年金の現在価値を計算します。
構文: annuity( interest, periods )
例
put annuity(1%, 32) --> 27.269589
関連項目:
ASin 関数
Behavior: 値の三角円弧余弦を計算し、ラジアンで表された角度として結果を返します。
構文: asin( numExpr )
{the} asin of numFactor
例
put asin(0) --> 0 radians
put asin of 1 --> 1.570796 radians
put asin(0.5) as degrees --> 30 degrees
関連項目:
ASinh 関数
Behavior: 値の逆双曲線サインを計算し、ラジアンで表される角度として結果を返します。
構文: asinh( numExpr ) {the} asinh of numFactor
例
put asinh(0) --> 0 radians
put the asinh of 2.301299 rounded to the nearest degree --> 90 degrees
関連項目:
ATan関数
Behavior: パラメータの逆三角関数をラジアンで表現した角度として返します。
構文: atan( numExpr ) {the} atan of numFactor
Example
put atan(19) --> 1.518213 radians
put the atan of 5 --> 1.373401 radians
関連:
ATanh 関数
Behavior: 値の逆双曲正接を計算し、結果をラジアンで表した角度として返します。
構文: atanh( numExpr ) {the} atanh of numFactor
例
put atanh(0.917152) rounded to the nearest degree --> 90 degrees
put the atanh of 0 --> 0 radians
関連:
ATanPt 関数
Behavior: ポイントの三角円弧接線を計算し、ラジアンで表された角度として結果を返します。
構文: atanPt( aPoint ) atanPt( xCoord , yCoord ) {the} atanPt of aPoint
'atanPt' 関数は、他の言語の atan2 関数と同じ機能を提供しますが、パラメータとして 1 つの点または座標を y,x ではなく、x,y 順に受け取ります。
例
put atanPt(123,0) --> 0 radians
put the atanPt of [11,56] --> 1.376837 radians
例
set myPoint to [50,50]
put atanPt(myPoint) as degrees --> 45 degrees
関連項目:
Average 関数
動作: パラメータの平均を返します。
構文: average( numList ) {the} average of numList
パラメータ numList は、数値のリスト、カンマで区切られた数値のリストに評価される式、またはこれらの組み合わせで、任意の深さにネストされたものです。
例
put average(8, 10, 12) --> 10
put the average of [3,7,15,84,6,42,11] --> 24
例:
この例は、機能の使用方法を示すためのコードの不完全な部分です
if the average of [x,y,z] is greater than z then
put "Z is below average!"
end if
関連項目:
Compound関数
Behavior: 指定された利率と期間に基づいて、1単位の投資に対する元本プラス利息を計算します。
構文: compound( interest, periods )
例:
put compound(7,25%, 6) -- 1.521892を表示
例
put 10 into initialinvestment
put initialInvestment * compound(6.7%, 12) into currentValue
put currentvalue --> 21.775746
関連項目:
Cos関数
Behavior: パラメータの三角関数コサインを返します。このパラメータはラジアンで表現される角度です。
構文: cos( numExpr ) [{the} cos of numFactor]
例:
put cos(18) -- 0.660317を表示
関連項目:
Cosh 関数
Behavior: そのパラメータの双曲線余弦 (ラジアンで表される角度) を返します。
構文: cosh( numExpr ) {the} cosh of numFactor
例
put cosh(0) --> 1 (no units specified, so 0 radians assumed)
put cosh of 90 degrees --> 2.509178
関連項目:
Cube Root, CbRt関数
Behavior: パラメータの立方根を計算します。
構文: cube root( number ) {the} cube root of number cbrt( numExpr )
例
put cube root (27) --> 3
put the cube root of 8 --> 2
put cbrt(125) --> 5
関連項目:
Exp関数
Behavior: パラメータの自然指数(つまり、数学定数eをパラメータの冪に上げたもの)を返します。
構文: exp( numExpr ) {the} exp of numFactor
例:
put exp(2) -- 7.389056
put the exp of 1 --> 2.718282
関連:
Exp1関数
Behavior: パラメータの自然指数の1引いたものを返します(つまり、数学定数eをパラメータの冪に上げ、1を引いたもの)。
構文: exp1( numExpr ) {the} exp1 of numFactor
例:
put exp1(2) --> 6.389056
put the exp1 of 2 --> 6.389056
関連項目:
Exp2関数
Behavior: パラメータの冪に2を上げたものを返します。
構文: exp2( numExpr )
{the} exp2 of numFactor
例:
put exp2(8) --> 256
put exp2 of 10 --> 1024
関連項目:
frac 関数
Behavior: 数値の小数部分を返します。値の整数部分を取得するには trunc 関数を使用します。trunc(x) + frac(x) は常に x と等しいように trunc と frac 関数が定義されています。
構文: frac( numExpr ) {the} frac of numFactor
例:
put frac(81.236) --> 0.236
put frac of 8.125 --> 0.125
関連項目:
Ln 関数
Behavior: パラメータの自然対数を返します。
構文: ln( numExpr ) {the} ln of numFactor
例:
put ln(2) --> 0.693147
put the ln of 4 --> 1.386294
関連項目:
Ln1 関数
Behavior: パラメータに1を加えた自然対数を返します。
構文:
ln1( numExpr )
{the} ln1 of numFactor
例:
put ln1(2) --> 1.098612
put the ln1 of 8 --> 2.197225
関連項目:
Log10 関数
Behavior: パラメータのベース10の対数を返します。
構文: log10( numExpr ) {the} log10 of numFactor
例:
put log10(100) --> 2
put the log10 of one million --> 6
関連:
Log2 関数
Behavior: パラメータのベース2の対数を返します。
構文: log2( numExpr )
{the} log2 of numFactor
Example
put log2(256) --> 8
put the log2 of 8 --> 3
関連項目:
Maximum, Max, HighestValue, LargestValue 関数
Behavior: パラメータの平均を返します。 _numList_は、数値のリスト、カンマで区切られた数値のリストを評価する式、またはこれらの組み合わせを任意の深さでネストしたものであることができます。
構文: [max | maximum | highestValue | largestValue]( numList ) {the} [max | maximum | highestValue | largestValue] of numList
パラメータ numList は、数値のリスト、カンマで区切られた数値のリストに評価される式、またはこれらの組み合わせで、任意の深さにネストされたものです。
例:
put max(4, 6, 5, 7, 3) --> 7
put largestValue of [2,23,45,76] --> 76
put the highest value of [3,42,9,13,21] --> 42
例
この例は、機能の使用方法を示すためのコードの不完全な部分です
if the maximum of [x,y,z] is z then
put "Z is the greatest!"
end if
関連項目:
Median 関数
Behavior: 数値順に並べた場合のパラメータの中央値 (中間値)、または 2 つの中間値の平均を返します。
構文: median( numList ) {the} median of numList
パラメータ numList は、数値のリスト、カンマで区切られた数値のリストとして評価される式、または任意の深さにネストされたこれらの組み合わせです。numList に含まれる数値が奇数の場合、中央値はソートされた数値リストの中央値になります。それ以外の場合は、中央の 2 つの値の平均になります。
例:
put median(1, 2, 8, 9, 12) --> 8
put the median of "2,7,8,10" --> 7.5
関連項目:
Minimum, Min, LowestValue, SmallestValue 関数
Behavior: Returns the lowest number from a list. The minimum function may be abbreviated as min or given as one of the synonyms lowestValue or smallestValue。
構文: [min | minimum | lowestValue | smallestValue]( numList ) {the} [min | minimum | lowest value | smallest value] of numList
パラメータ numList は、数値のリスト、カンマで区切られた数値のリストに評価される式、またはこれらの組み合わせで、任意の深さにネストされたものです。
例:
put min(4,6,5,7,3) --> 3
put lowestValue of [3,9,13,21,42] --> 3
put smallest value of [9,13,21,56,72] --> 9
例:
この例は、機能の使用方法を示すためのコードの不完全な部分です
if the min of [x,y,z] is z then put "Z is the smallest!"
関連:
Payment 関数
Behavior: ローンの金額、利率、期間、および支払いの頻度を指定して、ローンの支払い額を返します。
構文: payment( loanProperties ) payment( _principal,rate, term {, frequency} )
{the} {daily | weekly | monthly | quarterly | yearly | annual} payment on {a} {loan of} principal {loan} at {a rate of} rate {interest} for {a term of} term { {with} [ [payments | paid] frequency | frequency payments] }
もし_loanProperties_がプロパティリストとして与えられた場合、以下を含める必要があります:
- Principal or Amount — principal amount of the loan (may be specified with a currency unit)
- Rate — ローンの利率 (年あたりのパーセント、calendarQuarter、month、week、day として指定される場合があります — 期間ごとに指定されていない場合は、年利が想定されます)
- Term — ローンの_期間_期間(期間または支払い回数として指定される場合があります。複数の支払いを使用する場合(期間単位なし)、頻度パラメータが必要です)
- 頻度 — ローンの支払い頻度 (ローン期間が期間として指定されている場合はオプション) — 次のいずれかの用語になります: 月次、月、calendarMonths、四半期ごと、calendarQuarters、年次、annually、yearly、years、calendarYears、decades、calendarDecades
もし_principal_が特定の通貨の単位で与えられた場合、返される支払金額も同じ単位になります。もし_term_が期間の単位で指定され、_frequency_が省略された場合、期間の単位が支払いの頻度として使用されます。
例:
put payment(amount:$1000, rate: 6.5% per year, term:60 months) --> $19.566148
put payment($10000,7.5%,60,"months") --> $200.379486
例:
put the monthly payment on a ten thousand dollar loan at 4.5% yearly for 10 years --> $103.638409
put payment on a loan of $50000 at 7 percent interest for 60 months rounded to 2 places --> $990.06
関連項目:
Random 関数(
Behavior: パラメータの値と1の間、または二つの値の間でランダムに生成された整数を返します。
構文: random( numExpr {, secondExpr} ) {the} random of numFactor
例:
put random(20,30) -- gets a number from 20 to 30, inclusive
関連項目:
Reset Random Command
Behavior: random 関数やSenseTalkがランダムに物を選択するたびに使用するランダム数生成器のシーケンスをリセットします。ランダム数生成器の特定の seedExpr 値を設定することで、"ランダム"イベントの反復可能なシーケンスを得ることができます。これはテスト目的に非常に便利です。シード値なしで reset random コマンドを使用すると、予測不可能なシーケンスを得ることができます。
構文: reset random {{with |from}{seed} seedExpr}
例
reset random with seed 27
例
reset random
例
reset random 2
repeat 10 times
put 1..10 into NumberRange
put any item of NumberRange into ReplaceitemNumber
//1行目に示すように、reset random [#]を使用すると、ループを実行するたびに同じリストが生成されます。[#] には、任意のシード番号を指定できます。
log ReplaceitemNumber
//1 行目で "reset random 2" をコメント アウトし、繰り返しループを複数回実行すると、ループを実行するたびに異なるリストが作成されます。
end repeat
関連項目:
Round関数
Behavior: パラメータの値を最も近い整数に四捨五入して返します。オプションで第二パラメータを提供することで、四捨五入する小数点以下の桁数を指定することができます。負の桁数は小数点の左側で四捨五入します。
構文: round( numExpr {, decimalPlaces} ) {the} round of numFactor
例
put round(6.49 , 1) -- 6.5
関連項目:
RoundToNearest 関数
Behavior: 第一パラメータの値を第二パラメータの最も近い全体の倍数に四捨五入して返します。
構文: roundToNearest( numExpr, nearestMultiple ) numExpr rounded to {the} nearest {multiple of} nearestMultiple
例
put roundToNearest(643,100) --> 600
put roundToNearest(643,25) --> 650
put 12.16 rounded to the nearest multiple of .25 --> 12.25
関連項目:
Sin 関数
Behavior: パラメータの三角法的な正弦を返します。パラメータはラジアンで表される角度です。
構文: sin( numExpr ) {the} sin of numFactor
例:
put sin(18) -- -0.750987
関連項目:
Sinh 関数
Behavior: そのパラメータの双曲線サイン (ラジアンで表される角度) を返します。
構文: sinh( numExpr )
{the} sinh of numFactor
例
put sinh(0) --> 0 (no units specified, so 0 radians assumed)
put sinh of 90 degrees --> 2.301299
関連:
Square Root, SqRt 関数
Behavior: パラメータの平方根を返します。
構文: sqrt( numExpr ) {the} [square root | sqrt] of numFactor
例:
put sqrt(16) --> 4
関連項目:
Sum 関数
動作: パラメータの合計を返します。
構文: sum( numList ) {the} sum of numList
パラメータ numList は、数値のリスト、カンマで区切られた数値のリストに評価される式、またはこれらの組み合わせで、任意の深さにネストされたものです。
例
put sum("8,1", [10,11], 12) --> 42
例:
この例は記述されたとおりに実行されないため、実行するには変更する必要があります
if the sum of [x,y,z] is more than 100 then
put "The sum exceeds 100"
end if
関連項目:
Tan 関数
Behavior: パラメータの三角法的な正接を返します。パラメータはラジアンで表される角度です。
構文:
tan( numExpr )
{the} tan of numFactor
例:
put tan(18) --> -1.137314
put tan of 10 --> 0.648361
関連項目:
Tanh 関数
Behavior: そのパラメータの双曲線正接 (ラジアンで表される角度) を返します。
構文: tanh( numExpr )
{the} tanh of numFactor
例:
put tanh(0) --> 0 (no units specified, so 0 radians assumed)
put tanh of 90 degrees --> 0.917152
関連項目:
Trunc 関数
Behavior: 数字を切り捨てて、パラメータの整数部分を返し、分数部分を破棄します。値の分数部分を取得するには、frac 関数を使用します。trunc(x) + frac(x)は常にxと等しいと定義されているtruncとfrac関数。
構文: trunc( numExpr )
{the} trunc of numFactor
Example
put trunc(6.8) --> 6
put trunc(6.49) --> 6
関連項目: