1

Squeak 4.1 での除算方法は次のとおりです。

/t1
| t2 |
t1 isInteger
    ifTrue: [t2 := self digitDiv: t1 abs neg: self negative ~~ t1 negative.
        (t2 at: 2)
                = 0
            ifTrue: [^ (t2 at: 1) normalize].
        ^ (Fraction numerator: self denominator: t1) reduced].
^ t1 adaptToInteger: self andSend: #/

コードがわかりません。コードの動作を追跡できるように、コードをデバッグする方法についてのヒントを教えてください。ワークスペースを開くように、4/3 と入力すると、Fraction を調べることができます。オブジェクト self、分子、分母などがあります。どのようにすれば 4/3 に入り、Smalltalk が除算をどのように実装したかを確認できますか?

4

1 に答える 1

6

まず第一に、ソースに何か問題があります。メソッドInteger>>/は実際には次のようになります。

/ aNumber
"Refer to the comment in Number / "
| quoRem |
aNumber isInteger ifTrue:
    [quoRem := self digitDiv: aNumber abs   "*****I've added abs here*****"
                    neg: self negative ~~ aNumber negative.
    (quoRem at: 2) = 0
        ifTrue: [^ (quoRem at: 1) normalize]
        ifFalse: [^ (Fraction numerator: self denominator: aNumber) reduced]].
^ aNumber adaptToInteger: self andSend: #/

次に、このコードは大きな整数に対してのみ使用されます。4 / 3このメソッドを評価する場合は使用されませんが、Fraction を直接作成するSmallInteger>>/が使用されます。

必要なメソッドを呼び出すには、次のように大きな数を使用する必要があります。

12345678901234567890 / 2

この式を選択し、コンテキスト メニューから [デバッグ] を選択します。または、「停止」メッセージを使用してデバッガーを呼び出すことができます。

12345678901234567890 halt / 2

デバッガーがポップアップしたら、[Into] ボタンをクリックしてメソッドにステップインします。

于 2011-02-14T14:14:51.847 に答える