SenseTalkの算術計算
SenseTalkでは、数学的な演算子やさまざまなコマンドや関数を使用して数学的な操作を実行することができます。演算子の使用方法についての詳細な情報は、Expressions ページを参照してください。このページではSenseTalkで表現を使用する基本的な方法を説明し、特定の数学演算子(+、-、*、その他の演算子)と比較や論理演算子などの他の有用な概念について説明するページへのリンクがあります。
SenseTalkの算術コマンドと関数は、数値を操作するための便利なツールを提供しています。算術コマンドは、コンテナに格納された数値を変更するために使用できます。これらのコマンドは+、-、*、/の演算子と同じ算術関数を実行します。違いは、これらのコマンドがオペランドの一つをコンテナから取り出し、計算結果をそのコンテナに戻すことです。算術関数は数値を操作します。これらの関数を使用して、さまざまな方法で数値を操作することができます。
算術計算に関連する話題であるPoints and Rectanglesでは、SenseTalkが幾何学的な点と矩形の概念をどのように理解しているかについて説明しています。任意の2つの数のリスト、またはコンマで区切られた2つの数からなるテキスト文字列は、点として扱うことができます。Points and Rectanglesのページで説明されている関数は、点や矩形の各成分値を抽出するために使用できます。
算術コマンド
算術コマンドは4つあります:
これらを使用して、コンテナに格納された値を変更します。これらのコマンドは+、-、*、/の演算子と同じ算術関数を実行しま す。違いは、これらのコマンドがオペランドの一つをコンテナから取り出し、計算結果をそのコンテナに戻すことです。
Add コマンド
動作: add コマンドは、コンテナ内の値に数値を追加し、合計によりコンテナ内の値を置き換える場合に使用します。値のリストを追加することもできますが、ソースリストと宛先リストの両方が同じ数のアイテムを含んでいる必要があります。ソースリストからの各アイテムは、宛先コンテナの対応するアイテムに追加されます。 NumExpr はソース表現です。これは数値、任意の数式、または他のコンテナであることができます。 Chunk は、コンテナの一部を説明するチャンク表現です(行、単語、アイテム、または文字)。 Container は任意のコンテナです。
Syntax:
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 コマンド
動作: subtract コマンドは、コンテナ内の値から数値を引く場合に使用します。結果によりコンテナ内の値を置き換えます。値のリストを引くことができますが、ソースリストと宛先リストの両方が同じ数のアイテムを含んでいる必要があります。ソースリストからの各アイテムは、宛先コンテナの対応するアイテムから引かれます。 NumExpr はソース表現です。これは数値、任意の数式、または他のコンテナであることができます。 Chunk は、コンテナの一部を説明するチャンク表現です(行、単語、アイテム、または文字)。 Container は任意のコンテナです。
Syntax:
divide score by totalCount
例:
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 コマンド
動作: multiply コマンドは、コンテナ内の値を別の数値で掛ける場合に使用します。結果によりコンテナ内の値を置き換えます。値のリストを掛けることができますが、ソースリストと宛先リストの両方が同じ数のアイテムを含んでいる、またはソースが単一の値であることが必要です。宛先コンテナの各アイテムは、ソースリストの対応するアイテム、またはソース値で掛けられます。 NumExpr はソース表現です。これは数値、任意の数式、または他のコンテナであることができます。 Chunk は、コンテナの一部を説明するチャンク表現です(行、単語、アイテム、または文字)。 Container は任意のコンテナです。
Syntax:
multiply {chunk of} container by numExpr
例:
multiply item 3 of line x of table by 2
Divide コマンド
動作: divide コマンドは、コンテナ内の値を別の数値で除算する場合に使用します。結果によりコンテナ内の値を置き換えます。値のリストを除算することができますが、ソースリストと宛先リストの両方が同じ数のアイテムを含んでいる、またはソースが単一の値であることが必要です。宛先コンテナの各アイテムは、ソースリストの対応するアイテム、またはソース値で除算されます。 NumExpr はソース表現です。これは数値、任意の数式、または他のコンテナであることができます。 Chunk は、コンテナの一部を説明するチャンク表現です(行、単語、アイテム、または文字)。 Container は任意のコンテナです。
Syntax:
divide {chunk of} container by numExpr
例:
divide item 1 of balances by 12
算術関数
これらの関数を使用して数値を操作します。
put 500 as a negative number --> -500
動作: 数値パラメータの絶対値を返します。絶対値はその数値の大きさであり、その符号に関わらず常に正またはゼロです。
Syntax:
abs( numExpr )
構文: the abs of numFactor
numFactor as a positive number
numFactor as a negative number
例:
put abs(-11) -- 11を表示
例
if height is a negative number then put abs(height) into height
Example
put (3 - 16) as a positive number --> 13
例:
Behavior: 値の三角円弧余弦を計算し、ラジアンで表された角度として結果を返します。
Syntax:
put round(6.49) -- 6
abs 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 Function
put highestValue of [3,9,13,21,42] --42
Syntax:
put roundToNearest(643,100) -- 600
{the} exp2 of numFactor
例:
put acosh(2.509178) rounded to the nearest degree --> 90 degrees
put the acosh of 1 --> 0 radians
関連項目:
関連: annuity関数
動作: 指定された期間ごとの利息率と期間数に基づき、一単位の支払いを伴う普通年金の現在価値を計算します。
Syntax:
annuity( interest, periods )
例
put annuity(1%, 32)
関連項目:
- 構文: compound( interest, periods )
- add amount to dollarsVariable
例:
Behavior: 値の三角円弧余弦を計算し、ラジアンで表された角度として結果を返します。
Syntax:
abs( numExpr ) {#exp}
{the} trunc of numFactor](#payment)
例
put asin(0) --> 0 radians
put asin of 1 --> 1.570796 radians
put asin(0.5) as degrees --> 30 degrees
関連項目:
例:
Behavior: 値の逆双曲線サインを計算し、ラジアンで表される角度として結果を返します。
Syntax: exp1( numExpr ) {#exp1}
multiply score by weightingFactor
例
put asinh(0) --> 0 radians
put the asinh of 2.301299 rounded to the nearest degree --> 90 degrees
関連項目:
ATan関数
動作: パラメータの逆三角関数をラジアンで表現した角度として返します。
Syntax:
atan( numExpr )
the atan of numFactor
Example
put atan(19) --> 1.518213 radians
put the atan of 5 --> 1.373401 radians
ATanh Function
subtract rate * pmt from line 4 of amortization
Syntax:
atan( numExpr )
構文: the atan of numFactor
例
put atanh(0.917152) rounded to the nearest degree --> 90 degrees
put the atanh of 0 --> 0 radians
構文:
Behavior: ポイントの三角円弧接線を計算し、ラジアンで表された角度として結果を返します。
Syntax:
put atan(19) -- 1.518213を表示
put trunc(6.8) --> 6
構文:
'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
関連項目:
- 構文:
- add [10,5] to centerPoint
ASinFunction
abs 関数
Syntax:
average( numList )
{the} average of numList
動作: パラメータの合計を返します。numList は数値のリスト、カンマで区切られた数値のリストを評価する式、またはこれらの組み合わせであり、任意の深さでネストすることができます。
例
put average(8, 10, 12) -- 10を表示
例:
この例は、機能の使用方法を示すためのコードの不完全な部分です
if the average of [x,y,z] is greater than z then
put "Z is below average!"
end if
文法:
関連: compound関数
動作: 指定された利率と期間に基づいて、1単位の投資に対する元本プラス利息を計算します。
例:
put compound(7,25%, 6) -- 1.521892を表示
例
put initialInvestment * compound(6.7%, 12) into currentValue
文法:
- 構文: annuity( interest, periods )
- subtract checkAmt from accountBalance
例:
動作: パラメータの三角関数コサインを返します。このパラメータはラジアンで表現される角度です。
Syntax:
cos( numExpr )
{the} cos of numFactor
例:
put cos(18) -- 0.660317を表示
関連項目:
Cosh Function
Behavior: そのパラメータの双曲線余弦 (ラジアンで表される角度) を返します。
Syntax: cos( numExpr ) {#cos}
{the} exp1 of numFactor
例
put cosh(0) --> 1 (no units specified, so 0 radians assumed)
put cosh of 90 degrees --> 2.509178
Cube Root関数
動作: パラメータの立方根を計算 します。
Syntax:
cube root( number )
{the} cube root of number
構文:
例
put cube root (27)--3
関連項目:
例:
動作: パラメータの自然指数(つまり、数学定数eをパラメータの冪に上げたもの)を返します。
Syntax:
exp( numExpr )
{the} exp of numFactor
例:
put exp(2) -- 7.389056
put round(6.5) -- 7
- [exp( numExpr )](#exp1)
- exp2( numExpr ) {#exp2}
例:
動作: パラメータの自然指数の1引いたものを返します(つまり、数学定数eをパラメータの冪に上げ、1を引いたもの)。
Syntax:
exp1( numExpr )
{the} exp1 of numFactor
例:
put expl(2) -- 6.389056
関連項目:
- 例: {#example-55}
exp2関数
例 :
動作: パラメータの冪に2を上げたものを返します。
Syntax:
exp2( numExpr )
{the} exp2 of numFactor
例:
put exp2(8) -- 256
文法:
関連項目: frac 関数
動作: 数値の小数部分を返します。値の整数部分を取得するには trunc 関数を使用します。trunc(x) + frac(x) は常に x と等しいように trunc と frac 関数が定義されています。
Syntax:
frac( numExpr )
{the} frac of numFactor
例:
put frac(81.236) -- .236
例:
動作: パラメータの自然対数を返します。
Syntax:
ln( numExpr )
{the} ln of numFactor
例:
put ln(2) -- 0.693147
関連項目:
Ln1関数- put largestValue of [2,23,45,76] --76
Log10Function
例:
動作: パラメータに1を加えた自然対数を返します。
Syntax: ln1( numExpr ) {the} ln1 of numFactor
例:
put ln1(2) --> 1.098612
関連項目:
例:
put random(12) -- returns any number from 1 to 12
Syntax:
log2( numExpr ) {#log2}
{the} log2 of numFactor
例:
put log10(100) --> 2
put the log10 of one million --> 6
関連:
Ln関数- ln1( numExpr )
- put round(2389 , -2) -- 2400
add 37 to item 2 of line 3 of scores
動作: パラメータのベース2の対数を返します。
Syntax:
log2( numExpr )
{the} log2 of numFactor
Example
put log2(256) -- 8
関連項目:
- ln( numExpr ) {#ln}
- subtract [1,2] from boxDimensions
Log10Function
Maximum, Max, HighestValue, LargestValue 関数
[max | maximum | highestValue | largestValue]( numExpr )
Syntax:
[max | maximum | highestValue | largestValue]( numExpr )
{the} [max | maximum | highestValue | largestValue] of numFactor
動作: パラメータの平均を返します。 _numList_は、数値のリスト、カンマで区切られた数値のリストを評価する式、またはこれらの組み合わせを任意の深さでネストしたものであることができます。
例:
put max(4, 6, 5, 7, 3) -- 7
例
この例は、機能の使用方法を示すためのコードの不完全な部分です
if the maximum of [x,y,z] is z then
put "Z is the greatest!"
end if
Options:
- 関連項目:
minimum関数 - [**構文:**md {the} average of numList
Median 関数
put the median of "2,7,8,10" -- 7.5
Syntax:
median( numList )
[**構文:**md {the} average of numList
構文: {the} average of numList
例:
put median(1, 8, 9, 12) -- 8
関連項目:
Minimum, Min, LowestValue, SmallestValue 関数
put lowestValue of [3,9,13,21,42] --3
Syntax:
[min | minimum | lowestValue | smallestValue]( numList )
put smallestValue of [9,13,21,56,72] --9
パラメータ numList は、数値のリスト、カンマで区切られた数値のリストに評価される式、またはこれらの組み合わせで、任意の深さにネストされたものです。
例:
put min(4,6,5,7,3) -- 3
例:
この例は、機能の使用方法を示すためのコードの不完全な部分です
if the min of [x,y,z] is z then put "Z is the smallest!"
関連:
Payment Function
Behavior: ローンの金額、利率、期間、および支払いの頻度を指定して、ローンの支払い額を返します。
Syntax:
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_がプロパティリストとして与えられた場合、以下を含める必要があります:
- multiply accountBalance by 1 + interestRate
- Rate — ローンの利率 (年あたりのパーセント、calendarQuarter、month、week、day として指定される場合があります — 期間ごとに指定されていない場合は、年利が想定されます)
- Term — ローンの_期間_期間(期間または支払い回数として指定される場合があります。複数の支払いを使用する場合(期間単位なし)、頻度パラメータが必要です)
- add speed * time to item 1 of distances
もし_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
関連項目:
put (random(100) / 100) into randomPercentage
動作: パラメータの値と1の間、または二つの値の間でランダムに生成された整数を返します。
Syntax:
random( numExpr {, secondExpr} )
random( numExpr {, secondExpr} ) {#random}
例:
put random(20,30) -- gets a number from 20 to 30, inclusive
関連項目:
関連項目: Reset Random Command
動作: random 関数やSenseTalkがランダムに物を選択するたびに使用するランダム数生成器のシーケンスをリセットします。ランダム数生成器の特定の seedExpr 値を設定することで、"ランダム"イベントの反復可能なシーケンスを得ることができます。これはテスト目的に非常に便利です。シード値なしで reset random コマンドを使用すると、予測不可能なシーケンスを得ることができます。
Syntax:
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関数
動作: パラメータの値を最も近い整数に四捨五 入して返します。オプションで第二パラメータを提供することで、四捨五入する小数点以下の桁数を指定することができます。負の桁数は小数点の左側で四捨五入します。
Syntax: round( numExpr {, decimalPlaces} ) {#payment} {the} round of numFactor
例
put round(6.49 , 1) -- 6.5
関連項目:
RoundToNearest 関数
動作: 第一パラメータの値を第二パラメータの最も近い全体の倍数に四捨五入して返します。
Syntax:
put roundToNearest(643,25) -- 650
roundToNearest( numExpr, nearestMultiple ) > numExpr rounded to {the} nearest {multiple of} nearestMultiple
例
put 12.16 rounded to the nearest multiple of .25 --> 12.25
関連項目:
例:
動作: パラメータの三角法的な正弦を返します。パラメータはラジアンで表される角度です。
Syntax:
sin( numExpr )
{the} sin of numFactor {#sin}
例:
put sin(18) -- -0.750987
関連項目:
例:
Behavior: そのパラメータの双曲線サイン (ラジアンで表される角度) を返します。
Syntax:
trunc( numExpr )
{the} [平方根 |sqrt]of numFactor sqrt( numExpr )
例
put sinh(0) --> 0 (no units specified, so 0 radians assumed)
put sinh of 90 degrees --> 2.301299
Square Root , SqRt 関数
動作: パラメータの平方根を返します。
Syntax:
sqrt( numExpr )
{the} [square root | sqrt] of numFactor
例:
put sqrt(16) --> 4
関連項目:
- put the cube root of 8--2
- **構文:*
Sum 関数
subtract 1 from property CountDown of gameController
Syntax:
sum( numList )
{the} sum of numList
パラメータ numList は、数値のリスト、カンマで区切られた数値のリストに評価される式、またはこれらの組み合わせで、任意の深さにネストされたものです。
例
put sum("8,1", [10,11], 12) -- shows 42
例:
この例は記述されたとおりに実行されないため、実行するには変更する必要があります
if the sum of [x,y,z] is more than 100 then
put "The sum exceeds 100"
end if
関連項目:
Tan 関数
動作: パラメータの三角法的な正接を返します。パラメータはラジアンで表される角度です。
Syntax:
tan( numExpr )
{the} tan of numFactor
例:
put tan(18) -- -1.137314
関連:
Tanh Function
Behavior: そのパラメータの双曲線正接 (ラジアンで表される角度) を返します。
例:
put the square root of nine --> 3
関連項目:
Trunc 関数
動作: 数字を切り捨てて、パラメータの整数部分を返し、分数部分を破棄します。値の分数部分を取得するには、frac 関数を使用します。trunc(x) + frac(x)は常にxと等しいと定義されているtruncとfrac関数。
Syntax:
trunc( numExpr )
{the} trunc of _numFactor
Example
put trunc(6.49) -- 6
関連項目: